home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / WEXAMPLE.ZIP / TODOLIST.H < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  11.6 KB  |  413 lines

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