home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_7 / 2.ddi / WEXAMPLE.ZIP / TODOLIST.H < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  11.7 KB  |  407 lines

  1. #if !defined( __TODOLIST_H )
  2. #define __TODOLIST_H
  3.  
  4. //---------------------------------------------------------------------
  5. //
  6. //  TODOLIST.H
  7. //
  8. //      Copyright (c) 1991 by Borland International
  9. //      All Rights Reserved.
  10. //
  11. //  defines the following classes, used in implementing the Todo list:
  12. //
  13. //      TdDate      - extends the Date class from the class
  14. //                    library by providing a constructor that
  15. //                    converts a text string into a date.
  16. //
  17. //      TodoEntry   - holds the data for an entry in the Todo list.
  18. //
  19. //      TodoList    - container for holding Todo entries
  20. //
  21. //      ListBox     - wrapper around the Windows listbox, providing
  22. //                    an interface that fits with the Todo list.
  23. //
  24. //      TodoWindow  - the main window for this application.  There's
  25. //                    nothing displayed in the window except for the
  26. //                    list box.
  27. //
  28. //---------------------------------------------------------------------
  29.  
  30. #if !defined( __WINDOWS_H )
  31. #include <Windows.h>
  32. #endif  // __WINDOWS_H
  33.  
  34. #if !defined( __IOSTREAM_H )
  35. #include <iostream.h>
  36. #endif  // __IOSTREAM_H
  37.  
  38. #if !defined( __SORTABLE_H )
  39. #include "sortable.h"
  40. #endif  // __SORTABLE_H
  41.  
  42. #if !defined( __SORTARRY_H )
  43. #include "sortarry.h"
  44. #endif  // __SORTARRY_H
  45.  
  46. #if !defined( __LDATE_H )
  47. #include "ldate.h"
  48. #endif  // __LDATE_H
  49.  
  50. #if !defined( __TODOWIN_H )
  51. #include "TodoWin.h"
  52. #endif  // __TODOWIN_H
  53.  
  54. #define TodoEntryClass  __firstUserClass
  55.  
  56. //---------------------------------------------------------------------
  57. //
  58. //  class TdDate
  59. //
  60. //      extends the Date class from the class library by providing a
  61. //      constructor that converts a text string into a date.
  62. //
  63. //---------------------------------------------------------------------
  64.  
  65. class TdDate : public Date
  66. {
  67. public:
  68.  
  69.     TdDate();
  70.     TdDate( const Date& );
  71.     TdDate( const char * );
  72. };
  73.  
  74. //---------------------------------------------------------------------
  75. //
  76. //  class TodoEntry
  77. //
  78. //      holds the data for an entry in the Todo list.
  79. //
  80. //---------------------------------------------------------------------
  81.  
  82. class TodoEntry : public Sortable
  83. {
  84.     friend class EditBox;
  85.  
  86. public:
  87.  
  88.     TodoEntry();                // constructor.
  89.  
  90.     BOOL modified();            // indicates whether the entry has
  91.                                 // been modified.  Used in determining
  92.                                 // whether the list should be saved.
  93.  
  94.     virtual int             isEqual( const Object& ) const;
  95.     virtual int             isLessThan( const Object& ) const;
  96.  
  97.     virtual classType       isA() const;
  98.     virtual char            *nameOf() const;
  99.     virtual hashValueType   hashValue() const;
  100.  
  101.     friend istream& operator >> ( istream&, TodoEntry& );
  102.                                 // reads a TodoEntry from a stream.  The
  103.                                 // format used must match the format used
  104.                                 // by operator <<.
  105.  
  106.     friend ostream& operator << ( ostream&, TodoEntry& );
  107.                                 // writes a TodoEntry to a stream.  The
  108.                                 // format used must match the format used
  109.                                 // by operator >>.
  110.  
  111. protected:
  112.  
  113.     virtual void            printOn( ostream& ) const;
  114.                                 // this is the function called by
  115.                                 // operator << when applied to an Object.
  116.                                 // The disambiguation rules of C++ determine
  117.                                 // whether this function or the operator <<
  118.                                 // defined for this class will be called.
  119.                                 // For example:
  120.                                 //
  121.                                 // TodoEntry td;
  122.                                 // cout << td;  // calls the friend function
  123.                                 //              // function defined above
  124.                                 // cout << (Object&)td;
  125.                                 //              // calls the operator << for
  126.                                 //              // Object, which calls
  127.                                 //              // printOn().
  128.  
  129. private:
  130.  
  131.     BOOL dirty;                 // indicates whether this entry has
  132.                                 // been modified.
  133.  
  134.     TdDate dateCreated;
  135.     TdDate dateDue;
  136.     char *text;                 // the note associated with this entry
  137.     int priority;
  138.  
  139. };
  140.  
  141. //---------------------------------------------------------------------
  142. //
  143. //  class TodoList
  144. //
  145. //      container for holding Todo entries.  Currently implemented as
  146. //      a SortedArray, so we don't have to explicitly sort the entries
  147. //      when a new one is added.  The sorting is done according to the
  148. //      operator < () defined for a TodoEntry, which sorts according
  149. //      to the due date.
  150. //
  151. //---------------------------------------------------------------------
  152.  
  153. class TodoList : public SortedArray
  154. {
  155.  
  156. public:
  157.  
  158.     TodoList();
  159.     ~TodoList();
  160.  
  161.     virtual void add( Object& );
  162.                                 // adds an entry to the Todo list.
  163.  
  164.     virtual void detach( const Object&, int = 0 );
  165.                                 // removes an entry from the Todo list.
  166.  
  167.     TodoEntry& operator[]( int );
  168.                                 // returns the entry at the specified index.
  169.  
  170.     int indexOf( const TodoEntry& );
  171.                                 // returns the index of the specified entry.
  172.  
  173.     BOOL modified();            // indicates whether the list has
  174.                                 // been modified by adding or deleting an
  175.                                 // entry.  Used in determining
  176.                                 // whether the list should be saved.
  177.  
  178.     void clear();               // removes all entries from the list.
  179.  
  180.     friend istream& operator >> ( istream&, TodoList& );
  181.                                 // reads a TodoList from a stream.  The
  182.                                 // format used must match the format used
  183.                                 // by operator <<.
  184.  
  185.     friend ostream& operator << ( ostream&, TodoList& );
  186.                                 // writes a TodoList to a stream.  The
  187.                                 // format used must match the format used
  188.                                 // by operator >>.
  189.  
  190. private:
  191.  
  192.     BOOL dirty;                 // indicates whether this list has been
  193.                                 // modified.
  194.  
  195. };
  196.  
  197. //---------------------------------------------------------------------
  198. //
  199. //  class ListBox
  200. //
  201. //      wrapper around the Windows listbox, providing an interface
  202. //      that fits with the Todo list.  This is used to display the
  203. //      Todo list in a window.
  204. //
  205. //---------------------------------------------------------------------
  206.  
  207. class ListBox
  208. {
  209.  
  210. public:
  211.  
  212.     ListBox();
  213.     ~ListBox();
  214.  
  215.     const ListBox& operator = ( const TodoList& );
  216.                                 // copies the entries in the TodoList
  217.                                 // into the list box.
  218.  
  219.     void focus();               // sets focus to the list box.
  220.     void move( const RECT& );   // moves and resizes the list box.
  221.     int current();              // returns the index of the current
  222.                                 // selection.
  223.     void remove( int );         // removes the specified entry from
  224.                                 // the list box.
  225.     void insert( int, const TodoEntry& );
  226.                                 // adds an entry to the list box.
  227.     void replace( int, const TodoEntry& );
  228.                                 // replaces an entry in the list box
  229.                                 // with another entry.
  230.     void select( int );         // selects the specified entry.
  231.     void clear();               // removes all entries.
  232.  
  233.     void create( HWND, HWND, const RECT& );
  234.                                 // builds the list box.  This can't be
  235.                                 // done in the constructor because we
  236.                                 // don't have enough information at the
  237.                                 // time of construction.
  238.  
  239. private:
  240.  
  241.     HWND hListBox;              // handle of the list box.
  242.  
  243. };
  244.  
  245. //---------------------------------------------------------------------
  246. //
  247. //  class TodoWindow
  248. //
  249. //      the main window for this application.  There's nothing displayed
  250. //      in the window except for the list box.
  251. //---------------------------------------------------------------------
  252.  
  253. class TodoWindow : public Window
  254. {
  255. public:
  256.  
  257.     TodoWindow();
  258.     ~TodoWindow();
  259.  
  260. protected:
  261.  
  262.     virtual LONG dispatch( WORD, WORD, LONG );
  263.  
  264.     virtual BOOL registerClass();
  265.     virtual BOOL createWindow();
  266.  
  267. private:
  268.  
  269.     ListBox listBox;            // the list box used by this window.
  270.     TodoList tdl;               // the Todo list being displayed in this
  271.                                 // window.  There's a lot of parallelism
  272.                                 // between the operations of these two
  273.                                 // objects, and it might be worthwhile
  274.                                 // to add a class derived from both
  275.                                 // ListBox and TodoList for use here.
  276.  
  277.     char *fileName;             // path to the file currently being
  278.                                 // used.  0 if there is no file.
  279.  
  280.     void newList();
  281.     void openFile();
  282.     void saveFile();
  283.     void saveFileAs();
  284.     void editBox();
  285.     void newEntry();
  286.     void delEntry();
  287.     void aboutBox();
  288.  
  289.     void fileBox();
  290.     BOOL saveBox();
  291.  
  292.     BOOL getFileName( const char *, BOOL );
  293.  
  294.     void moveListBox();
  295.  
  296.     void readFile();
  297.     void writeFile();
  298.     void checkSave();
  299.  
  300.     BOOL processCommand( WORD, LONG );
  301.  
  302. };
  303.  
  304. //---------------------------------------------------------------------
  305. //
  306. //  inline functions.
  307. //
  308. //---------------------------------------------------------------------
  309.  
  310. inline int min( int a, int b ) { return (a<b) ? a : b; }
  311.  
  312. inline TdDate::TdDate() : Date()
  313. {
  314. }
  315.  
  316. inline TdDate::TdDate( const Date& d ) : Date( d )
  317. {
  318. }
  319.  
  320. inline TodoEntry::TodoEntry() : dirty( FALSE ), priority( 1 ), text( 0 )
  321. {
  322. }
  323.  
  324. inline BOOL TodoEntry::modified()
  325. {
  326.     return dirty;
  327. }
  328.  
  329. inline TodoEntry& TodoList::operator[]( int indx )
  330. {
  331.     return (TodoEntry&)SortedArray::operator[]( indx );
  332. }
  333.  
  334. inline TodoList::TodoList() : SortedArray( 20 )
  335. {
  336. }
  337.  
  338. inline TodoList::~TodoList()
  339. {
  340. }
  341.  
  342. inline ListBox::ListBox() : hListBox( 0 )
  343. {
  344. }
  345.  
  346. inline ListBox::~ListBox()
  347. {
  348.     if( hListBox != 0 )
  349.         DestroyWindow( hListBox );
  350. }
  351.  
  352. inline void ListBox::focus()
  353. {
  354.     if( IsWindow( hListBox ) )
  355.         SetFocus( hListBox );
  356. }
  357.  
  358. inline void ListBox::move( const RECT& wrect )
  359. {
  360.     MoveWindow( hListBox,
  361.                 wrect.left,
  362.                 wrect.top,
  363.                 wrect.right - wrect.left,
  364.                 wrect.bottom - wrect.top,
  365.                 TRUE
  366.               );
  367. }
  368.  
  369. inline int ListBox::current()
  370. {
  371.     return (int)SendMessage( hListBox, LB_GETCURSEL, 0, 0 );
  372. }
  373.  
  374. inline void ListBox::remove( int i )
  375. {
  376.     SendMessage( hListBox, LB_DELETESTRING, i, 0 );
  377.     select( i );
  378. }
  379.  
  380. inline void ListBox::replace( int i, const TodoEntry& tde )
  381. {
  382.     remove( i );
  383.     insert( i, tde );
  384. }
  385.  
  386. inline void ListBox::select( int i )
  387. {
  388.     i = min( i, (int)SendMessage( hListBox, LB_GETCOUNT, 0, 0 ) - 1 );
  389.     SendMessage( hListBox, LB_SETCURSEL, i, 0 );
  390. }
  391.  
  392. inline void ListBox::clear()
  393. {
  394.     SendMessage( hListBox, LB_RESETCONTENT, 0, 0 );
  395. }
  396.  
  397. inline TodoWindow::TodoWindow() : fileName( 0 )
  398. {
  399. }
  400.  
  401. inline TodoWindow::~TodoWindow()
  402. {
  403. }
  404.  
  405. #endif  // __TODOLIST_H
  406.  
  407.