home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / todo.pak / TODOLIST.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  14KB  |  533 lines

  1. //---------------------------------------------------------------------
  2. //
  3. //  TODOLIST.CPP - part of TODO example program
  4. //
  5. //      Copyright (c) 1991, 1993 by Borland International
  6. //      All Rights Reserved.
  7. //
  8. //---------------------------------------------------------------------
  9.  
  10. #define STRICT
  11.  
  12. #include <strstrea.h>
  13. #include <fstream.h>
  14. #include <ctype.h>
  15. #include <checks.h>
  16. #include <string.h>
  17. #include <windowsx.h>
  18.  
  19. #include "classlib\objstrm.h"
  20. #include "todolist.h"
  21. #include "tododefs.h"
  22. #include "tododlgs.h"
  23.  
  24. //---------------------------------------------------------------------
  25. //
  26. //  ostream& operator << ( ostream&, const TodoEntry& );
  27. //
  28. //  puts a TodoEntry onto an ostream in text form.
  29. //
  30. //---------------------------------------------------------------------
  31.  
  32. ostream& operator << ( ostream& os, const TodoEntry& tde )
  33. {
  34.     char temp[ 256 ];
  35.     ostrstream tstr( temp, sizeof temp );
  36.     tstr << tde.Priority
  37.          << '\t'
  38.          << tde.DateCreated
  39.          << '\t'
  40.          << tde.DateDue
  41.          << '\t'
  42.          << tde.Text
  43.          << ends;
  44.     return os << temp;
  45. }
  46.  
  47. //---------------------------------------------------------------------
  48. //
  49. //  ipstream& operator >> ( ipstream& is, TodoEntry& td );
  50. //  opstream& operator << ( opstream& os, TodoEntry& td );
  51. //
  52. //  inserter and extractor for TodoEntry and persistent streams.  
  53. //  These work together to write entries out to a persistent stream 
  54. //  and read them back in.
  55. //
  56. //---------------------------------------------------------------------
  57.  
  58. ipstream& operator >> ( ipstream& is, TodoEntry& td )
  59. {
  60.     is >> td.Priority >> td.DateDue >> td.DateCreated >> td.Text;
  61.     td.Dirty = FALSE;
  62.     return is;
  63. }
  64.  
  65. opstream& operator << ( opstream& os, const TodoEntry& td )
  66. {
  67.     os << td.Priority << td.DateDue << td.DateCreated << td.Text;
  68.     const_cast<TodoEntry&>(td).Dirty = FALSE;
  69.     return os;
  70. }
  71.  
  72. //---------------------------------------------------------------------
  73. //
  74. //  member functions for class TodoList.
  75. //
  76. //---------------------------------------------------------------------
  77.  
  78. void TodoList::Add( const TodoEntry& e )
  79. {
  80.     Dirty = TRUE;               // mark that the list has been modified
  81.     Vect.Add( e );              // add the entry
  82. }
  83.  
  84. void TodoList::Detach( unsigned idx )
  85. {
  86.     Dirty = TRUE;               // mark that the list has been modified
  87.     Vect.Detach( idx );         // remove the entry
  88. }
  89.  
  90. int TodoList::IndexOf( const TodoEntry& tde )
  91. {
  92.     for( int i = 0; i < Vect.Count(); i++ )
  93.         if( Vect[i] == tde )
  94.             return i;
  95.     return -1;
  96. }
  97.  
  98. static int CheckModified( const TodoEntry& ent, void * )
  99. {
  100.     return ent.Modified();
  101. }
  102.  
  103. BOOL TodoList::Modified() const
  104. {
  105.     if( Dirty == TRUE )         // if we've added or deleted entries
  106.         return TRUE;            // we've been modified
  107.                                 // otherwise, if any entry has been
  108.                                 // modified, the list has been modified.
  109.     else
  110.         return Vect.FirstThat( CheckModified, 0 ) != 0;
  111. }
  112.  
  113. static void MarkEntrySaved( TodoEntry& ent, void * )
  114. {
  115.     ent.Clear();
  116. }
  117.  
  118. void TodoList::MarkSaved() const
  119. {
  120.     const_cast<TodoList&>(*this).Dirty = FALSE;
  121.     const_cast<TodoList&>(*this).Vect.ForEach( MarkEntrySaved, 0 );
  122. }
  123.  
  124. void TodoList::Clear()
  125. {
  126.     Vect.Flush();
  127. }
  128.  
  129. ipstream& operator >> ( ipstream& is, TodoList& td )
  130. {
  131.     unsigned count;
  132.     is >> count;
  133.     while( count-- != 0 )
  134.         {
  135.         TodoEntry temp;
  136.         is >> temp;
  137.         if( !is )
  138.             return is;  // if stream isn't valid, don't try to add.
  139.         td.Add( temp );
  140.         }
  141.     td.MarkSaved();
  142.     return is;
  143. }
  144.  
  145. opstream& operator << ( opstream& os, const TodoList& td )
  146. {
  147.     os << td.Vect.Count();
  148.     TSVectorIteratorImp<TodoEntry> iter( td.Vect );
  149.     while( iter )
  150.         {
  151.         os << iter.Current();
  152.         iter++;
  153.         }
  154.     td.MarkSaved();
  155.     return os;
  156. }
  157.  
  158. //---------------------------------------------------------------------
  159. //
  160. //  const ListBox& ListBox::operator = ( const TodoList& tdl );
  161. //
  162. //  copies the contents of a TodoList into a ListBox.
  163. //
  164. //---------------------------------------------------------------------
  165.  
  166. const ListBox& ListBox::operator = ( const TodoList& tdl )
  167. {
  168.     PRECONDITION( hListBox != 0 );
  169.  
  170.     Clear();
  171.     TSVectorIteratorImp<TodoEntry> iter( tdl.Vect );
  172.     while( iter )
  173.         {
  174.         char buf[100];      // write the entry into a string
  175.                             // and insert that string into
  176.                             // the list box
  177.  
  178.         ostrstream( buf, 100 ) << iter.Current() << ends;
  179.         SendMessage( hListBox, LB_ADDSTRING, NULL, (LONG)(LPSTR)buf );
  180.         iter++;
  181.         }
  182.     Select( 0 );
  183.  
  184.     return *this;
  185. }
  186.  
  187. void ListBox::Insert( int i, const TodoEntry& tde )
  188. {
  189.     char temp[100];
  190.     ostrstream( temp, sizeof( temp ) ) << tde << ends;
  191.  
  192.     SendMessage( hListBox, LB_INSERTSTRING, i, (LONG)(LPSTR)temp );
  193.     Select( i );
  194. }
  195.  
  196. void ListBox::Create( HWND owner, HWND hInst, const RECT &wrect )
  197. {
  198.     hListBox = ::CreateWindow(
  199.         "ListBox", NULL,
  200.         LBS_NOTIFY | WS_BORDER | WS_VSCROLL |
  201.             LBS_USETABSTOPS | WS_CHILD | WS_VISIBLE,
  202.         wrect.left,
  203.         wrect.top,
  204.         wrect.right - wrect.left,
  205.         wrect.bottom - wrect.top,
  206.         (HWND)owner,
  207.         (HMENU)IDC_LISTBOX,
  208.         (HINSTANCE)hInst,
  209.         NULL );
  210.  
  211.     int tabs[] = { 10, 100, 200 };
  212.     SendMessage( hListBox,
  213.                  LB_SETTABSTOPS,
  214.                  sizeof(tabs)/sizeof(*tabs),
  215.                  (LONG)(LPSTR)tabs
  216.                 );
  217.     Focus();
  218. }
  219.  
  220. //---------------------------------------------------------------------
  221. //
  222. //  member functions for class TodoWindow.
  223. //
  224. //  these are mostly self-explanatory.
  225. //
  226. //---------------------------------------------------------------------
  227.  
  228. BOOL TodoWindow::RegisterClass()
  229. {
  230.     WNDCLASS wc;
  231.  
  232.     wc.style = 0;
  233.     wc.lpfnWndProc = Window::WndProc;
  234.     wc.cbClsExtra = 0;
  235.     wc.cbWndExtra = 0;
  236.     wc.hInstance =(HINSTANCE) hInst;
  237.     wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  238.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  239.     wc.hbrBackground =(HBRUSH) GetStockObject( WHITE_BRUSH );
  240.     wc.lpszMenuName = "TodoMenu";
  241.     wc.lpszClassName = "TodoClass";
  242.  
  243.     return ::RegisterClass( &wc );
  244. }
  245.  
  246. BOOL TodoWindow::CreateNewWindow()
  247. {
  248.     hWindow = ::CreateWindow(
  249.         "TodoClass",
  250.         "Todo List",
  251.         WS_OVERLAPPEDWINDOW,
  252.         CW_USEDEFAULT,
  253.         CW_USEDEFAULT,
  254.         CW_USEDEFAULT,
  255.         CW_USEDEFAULT,
  256.         NULL,
  257.         NULL,
  258.         (HINSTANCE)hInst,
  259.         NULL
  260.         );
  261.     if( hWnd() == 0 )
  262.         return FALSE;
  263.  
  264.     Insert();                   // insert this window into the window list
  265.  
  266.     RECT wrect;
  267.     GetClientRect( (HWND)hWnd(), (LPRECT) &wrect);
  268.     LB.Create( (HWND)hWnd(), (HWND)hInst, wrect );
  269.                                 // build a list box in the client rectangle
  270.     LB = Tdl;                   // copy the Todo list into the list box
  271.  
  272.     ::ShowWindow( (HWND)hWnd(), Show );
  273.     ::UpdateWindow( (HWND)hWnd() );
  274.     return TRUE;
  275. }
  276.  
  277. void TodoWindow::ShowAboutBox()
  278. {
  279.     AboutBox ab( (HWND)hWnd() );
  280.     ab.Run();
  281. }
  282.  
  283. void TodoWindow::ShowEditBox()
  284. {
  285.     int cur = LB.Current();
  286.     if( cur == -1 )             // if there's nothing in the list,
  287.         NewEntry();             // need to create an entry
  288.     else
  289.         {
  290.         EditBox ed( (HWND)hWnd(), Tdl[cur] );
  291.         ed.Run();
  292.         LB.Replace( cur, Tdl[cur] );
  293.         }
  294. }
  295.  
  296. void TodoWindow::NewEntry()
  297. {
  298.     TodoEntry tde;
  299.     EditBox ed( (HWND)hWnd(), tde );
  300.  
  301.     if( ed.Run() == 0 )         // ed.Run() returns 0 if terminated by
  302.         {                       // OK, 1 if terminated by Cancel.
  303.         Tdl.Add( tde );
  304.         LB.Insert( Tdl.IndexOf( tde ), tde );
  305.         }
  306. }
  307.  
  308. void TodoWindow::DelEntry()
  309. {
  310.     int cur = LB.Current();
  311.     if( cur == -1 )             // if there's nothing in the list, there's
  312.         return;                 // nothing to delete.
  313.     Tdl.Detach( cur );
  314.     LB.Remove( cur );
  315.     LB.Select( cur );
  316. }
  317.  
  318. void TodoWindow::MoveListBox()
  319. {
  320.     RECT wrect;
  321.     GetClientRect( (HWND)hWnd(), (LPRECT) &wrect);
  322.  
  323.     LB.Move( wrect );
  324. }
  325.  
  326. //---------------------------------------------------------------------
  327. //
  328. //  void TodoWindow::CheckSave();
  329. //
  330. //  checks whether the Todo list has been modified.  If it has, asks
  331. //  the user whether to save the list or not, and if it is to be saved,
  332. //  writes it to a file.
  333. //
  334. //---------------------------------------------------------------------
  335.  
  336. void TodoWindow::CheckSave()
  337. {
  338.     if( Tdl.Modified() == TRUE && ShowSaveBox() == TRUE )
  339.         SaveFile();
  340. }
  341.  
  342. void TodoWindow::NewList()
  343. {
  344.     CheckSave();                // dump the current list
  345.     Tdl.Clear();
  346.     LB.Clear();
  347.     *FileName = '\0';           // mark that there's no file
  348.     *TitleName = '\0';
  349. }
  350.  
  351. void TodoWindow::OpenFile()
  352. {
  353.     CheckSave();                // dump the current list
  354.     Tdl.Clear();
  355.     LB.Clear();
  356.     if( FileBox::GetOpenFileName( hWnd(), FileName, TitleName ) == TRUE )
  357.         ReadFile();             // read new data from the specified file
  358. }
  359.  
  360. void TodoWindow::SaveFile()
  361. {
  362.     if( *TitleName == '\0' )
  363.         SaveFileAs();
  364.     else
  365.         WriteFile();
  366. }
  367.  
  368. void TodoWindow::SaveFileAs()
  369. {
  370.     if( FileBox::GetSaveFileName( hWnd(), FileName, TitleName ) == TRUE )
  371.         WriteFile();
  372. }
  373.  
  374. void TodoWindow::ReadFile()
  375. {
  376.     ifpstream in( FileName );           // open the input file
  377.     in >> Tdl;                          // read the Todo list
  378.     if( !in )                           // check whether read succeeded
  379.         throw( FileError( FileName ) );
  380.     LB = Tdl;                           // build the list box
  381. }
  382.  
  383. void TodoWindow::WriteFile()
  384. {
  385.     ofpstream out( FileName );
  386.     out << Tdl;
  387.     if( !out )                          // check whether write succeeded
  388.         throw( FileError( FileName ) );
  389. }
  390.  
  391. BOOL TodoWindow::ShowSaveBox()
  392. {
  393.     if( MessageBox( (HWND)hWnd(),
  394.         "Save Changes",
  395.         "Current List Modified",
  396.         MB_YESNO | MB_ICONQUESTION ) == IDYES )
  397.         return TRUE;
  398.     else
  399.         return FALSE;
  400. }
  401.  
  402. //---------------------------------------------------------------------
  403. //
  404. //  BOOL TodoWindow::ProcessCommand( WPARAM wParam, LPARAM lParam );
  405. //
  406. //  dispatches commands to the appropriate member functions.
  407. //
  408. //---------------------------------------------------------------------
  409.  
  410. BOOL TodoWindow::ProcessCommand( WPARAM wParam, LPARAM lParam )
  411. {
  412.     switch( GET_WM_COMMAND_ID(wParam, lParam) )
  413.         {
  414.  
  415.         case IDM_QUIT:
  416.             SendMessage((HWND)hWnd(), WM_CLOSE, 0, 0L);
  417.             return TRUE;
  418.  
  419.         case IDM_NEW_LIST:
  420.             NewList();
  421.             return TRUE;
  422.  
  423.         case IDM_OPEN:
  424.             OpenFile();
  425.             return TRUE;
  426.  
  427.         case IDM_SAVE:
  428.             SaveFile();
  429.             return TRUE;
  430.  
  431.         case IDM_SAVEAS:
  432.             SaveFileAs();
  433.             return TRUE;
  434.  
  435.         case IDM_EDIT:
  436.             ShowEditBox();
  437.             return TRUE;
  438.  
  439.         case IDM_NEW_ENTRY:
  440.             NewEntry();
  441.             return TRUE;
  442.  
  443.         case IDM_DEL_ENTRY:
  444.             DelEntry();
  445.             return TRUE;
  446.  
  447.         case IDM_ABOUT:
  448.             ShowAboutBox();
  449.             return TRUE;
  450.  
  451.         case IDC_LISTBOX:
  452.             if( GET_WM_COMMAND_CMD( wParam, lParam ) == LBN_DBLCLK )
  453.                 {
  454.                 ShowEditBox();
  455.                 return TRUE;
  456.                 }
  457.             else
  458.                 return FALSE;
  459.         default:
  460.             return FALSE;
  461.         }
  462. }
  463.  
  464. //---------------------------------------------------------------------
  465. //
  466. //  LONG TodoWindow::Dispatch( UINT msg, WPARAM wParam, LPARAM lParam );
  467. //
  468. //  dispatches messages to the appropriate member functions.
  469. //
  470. //---------------------------------------------------------------------
  471.  
  472. LONG TodoWindow::Dispatch( UINT msg, WPARAM wParam, LPARAM lParam )
  473. {
  474.     switch( msg )
  475.         {
  476.         case WM_COMMAND:
  477.  
  478.             if( ProcessCommand( wParam, lParam ) == TRUE )
  479.                 {
  480.                 LB.Focus();
  481.                 return 0;
  482.                 }
  483.             break;
  484.  
  485.         case WM_MOVE:
  486.         case WM_SIZE:
  487.  
  488.             MoveListBox();
  489.             return 0;
  490.  
  491.         case WM_QUERYENDSESSION:
  492.             return TRUE;
  493.  
  494.         case WM_CLOSE:
  495.  
  496.             CheckSave();
  497.             DestroyWindow( (HWND)hWnd() );
  498.             return 0;
  499.  
  500.         case WM_DESTROY:
  501.         case WM_QUIT:
  502.  
  503.             PostQuitMessage( 0 );
  504.             break;
  505.         }
  506.  
  507.     return Window::Dispatch( msg, wParam, lParam );
  508. }
  509.  
  510. //---------------------------------------------------------------------
  511. //
  512. //  int PASCAL WinMain( HINSTANCE, HINSTANCE, LPSTR, int );
  513. //
  514. //  the main entry point for the program.
  515. //
  516. //---------------------------------------------------------------------
  517.  
  518. int PASCAL WinMain( HINSTANCE hInstance,
  519.                     HINSTANCE hPrevInstance,
  520.                     LPSTR     lpCmd,
  521.                     int       nShow)
  522. {
  523.     WinBase::hInst = hInstance;
  524.     WinBase::hPrevInst = hPrevInstance;
  525.     WinBase::Cmd = lpCmd;
  526.     WinBase::Show = nShow;
  527.  
  528.     TodoWindow td;
  529.     td.Create();
  530.     return td.Run();
  531. }
  532.  
  533.