home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 6.ddi / WEXAMPLE.ZIP / TODODLGS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  14.5 KB  |  534 lines

  1. //---------------------------------------------------------------------
  2. //
  3. //  TODODLGS.CPP - part of TODO example program
  4. //
  5. //      Copyright (c) 1991, 1992 by Borland International
  6. //      All Rights Reserved.
  7. //
  8. //---------------------------------------------------------------------
  9.  
  10. #define STRICT
  11.  
  12. #if !defined( __WINDOWS_H )
  13. #include <Windows.h>
  14. #endif  // __WINDOWS_H
  15.  
  16. #if !defined( __STRING_H )
  17. #include <String.h>
  18. #endif  // __STRING_H
  19.  
  20. #if !defined( __TODODLGS_H )
  21. #include "TodoDlgs.h"
  22. #endif  // __TODODLGS_H
  23.  
  24. #if !defined( __TODODEFS_H )
  25. #include "TodoDefs.h"
  26. #endif  // __TODODEFS_H
  27.  
  28. //---------------------------------------------------------------------
  29. //
  30. //  member functions for class AboutBox.
  31. //
  32. //  not much needed here...
  33. //
  34. //---------------------------------------------------------------------
  35.  
  36. LPSTR AboutBox::getDialogName()
  37. {
  38.     return "AboutBox";
  39. }
  40.  
  41. BOOL AboutBox::dispatch( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  42. {
  43.     switch( msg )
  44.         {
  45.  
  46.         case WM_INITDIALOG:
  47.  
  48.             return TRUE;        // no initialization.
  49.  
  50.         case WM_COMMAND:
  51.  
  52.             if( wParam == IDOK || wParam == IDCANCEL )
  53.                 {
  54.                 EndDialog( hDlg, TRUE );  // selecting OK or Cancel
  55.                 return TRUE;              // terminates the dialog
  56.                 }
  57.  
  58.         default:                // if we don't handle it, pass it
  59.                                 // to our parent.
  60.  
  61.             return ModalDialog::dispatch( hDlg, msg, wParam, lParam );
  62.  
  63.         }
  64. }
  65.  
  66. //---------------------------------------------------------------------
  67. //
  68. //  class SaveDir is only used locally, although it makes a handy
  69. //  addition to a toolbox.  When created it saves the current drive
  70. //  and directory, and when destroyed it restores the drive and path
  71. //  to the saved drive and directory.
  72. //
  73. //---------------------------------------------------------------------
  74.  
  75. class SaveDir
  76. {
  77. public:
  78.     SaveDir();
  79.     ~SaveDir();
  80. private:
  81.     int drive;
  82.     char path[MAXPATH];
  83. };
  84.  
  85. SaveDir::SaveDir()
  86. {
  87.     drive = getdisk();
  88.     path[0] = '\\';
  89.     getcurdir( 0, path+1 );
  90. }
  91.  
  92. SaveDir::~SaveDir()
  93. {
  94.     setdisk( drive );
  95.     chdir( path );
  96. }
  97.  
  98. //---------------------------------------------------------------------
  99. //
  100. //  member functions for class FileBox.
  101. //
  102. //---------------------------------------------------------------------
  103.  
  104. const fileAttr = 0x4010;
  105.  
  106. LPSTR FileBox::getDialogName()
  107. {
  108.     return "OpenFile";
  109. }
  110.  
  111. //---------------------------------------------------------------------
  112. //
  113. //  FileBox::FileBox( HWND owner,
  114. //                    const char *c,
  115. //                    const char *p,
  116. //                    const char *s,
  117. //                    BOOL me
  118. //                  );
  119. //
  120. //  constructor.  Massages initial data.
  121. //
  122. //---------------------------------------------------------------------
  123.  
  124. FileBox::FileBox( HWND owner,
  125.                   const char *c,
  126.                   const char *p,
  127.                   const char *s,
  128.                   BOOL me
  129.                 ) :
  130.     ModalDialog( owner ),
  131.     WinBase( ),
  132.     caption( c ),
  133.     iPath( p ),
  134.     iSpec( s ),
  135.     mustExist( me )
  136. {
  137.     strcpy( path, p );
  138.  
  139.     if( path[ strlen( path ) - 1 ] != '\\' )
  140.         strcat( path, "\\" );
  141.  
  142.     strcat( path, s );
  143. }
  144.  
  145. WORD FileBox::run()
  146. {
  147.     SaveDir sd;
  148.  
  149.     if( ModalDialog::run() != 0 )   // run() returns non-zero if
  150.         return 1;                   // the user selected Cancel
  151.     else
  152.         {
  153.         getcwd( res, sizeof res );  // otherwise, set up the selected
  154.         int l = strlen( res );      // path and file name in res.
  155.         if( res[ l-1 ] != '\\' )
  156.             strcat( res, "\\" );
  157.         strcat( res, path );
  158.         return 0;
  159.         }
  160. }
  161.  
  162. //---------------------------------------------------------------------
  163. //
  164. //  handy function for internal use
  165. //
  166. //---------------------------------------------------------------------
  167.  
  168.  
  169. void FileBox::resetDlg( HWND hDlg )
  170. {
  171.     DlgDirList( hDlg, path, IDD_FLIST, IDD_FPATH, fileAttr );
  172.                                     // set the directory list to the
  173.                                     // specified path.
  174.  
  175.     SetDlgItemText( hDlg, IDD_FNAME, path );
  176.                                     // set the file name to the
  177.                                     // specified name.
  178. }
  179.  
  180. //---------------------------------------------------------------------
  181. //
  182. //  void FileBox::initDlg( HWND hDlg );
  183. //
  184. //  initializes the FileBox dialog.
  185. //
  186. //---------------------------------------------------------------------
  187.  
  188.  
  189. void FileBox::initDlg( HWND hDlg )
  190. {
  191.     SendDlgItemMessage( hDlg, IDD_FNAME, EM_LIMITTEXT, MAXPATH, 0 );
  192.                                     // no more than MAXPATH characters
  193.                                     // in the file name.
  194.     resetDlg( hDlg );
  195.     SendMessage( hDlg, WM_SETTEXT, 0, (LONG)(LPSTR)caption );
  196.                                     // display the caption as the
  197.                                     // dialog title.
  198. }
  199.  
  200. //---------------------------------------------------------------------
  201. //
  202. //  BOOL FileBox::flistCmd( HWND hDlg, LPARAM lParam )
  203. //
  204. //  called when the user does something to the directory list box.
  205. //
  206. //---------------------------------------------------------------------
  207.  
  208. BOOL FileBox::flistCmd( HWND hDlg, LPARAM lParam )
  209. {
  210.  
  211.     switch( HIWORD( lParam ) )
  212.         {
  213.         case LBN_SELCHANGE:     // user changed the selection in the
  214.                                 // list box.
  215.  
  216.             if( DlgDirSelect( hDlg, path, IDD_FLIST ) != 0 )
  217.                 strcat( path, iSpec );
  218.                                 // current selection is a
  219.                                 // path, so append the file spec.
  220.  
  221.             SetDlgItemText( hDlg, IDD_FNAME, path );
  222.                                 // display the new path
  223.  
  224.             SendDlgItemMessage( hDlg,
  225.                                 IDD_FNAME,
  226.                                 EM_SETSEL,
  227.                                 0,
  228.                                 MAKELONG( 0, 0x7FFF )
  229.                                 );
  230.                                 // select all characters in the file
  231.                                 // name edit box.
  232.             return TRUE;
  233.  
  234.         case LBN_DBLCLK:        // user has double-clicked a selection
  235.                                 // in the list box.
  236.  
  237.             if( DlgDirSelect( hDlg, path, IDD_FLIST ) != 0 )
  238.                 strcat( path, iSpec );
  239.                                 // current selection is a
  240.                                 // path, so append the file spec.
  241.  
  242.             resetDlg( hDlg );
  243.             return TRUE;
  244.  
  245.         default:
  246.  
  247.             return FALSE;
  248.  
  249.         }
  250. }
  251.  
  252. //---------------------------------------------------------------------
  253. //
  254. //  BOOL FileBox::fnameCmd( HWND hDlg, LPARAM lParam );
  255. //
  256. //  called when the user does something in the edit box containing the
  257. //  file name.
  258. //
  259. //---------------------------------------------------------------------
  260.  
  261. BOOL FileBox::fnameCmd( HWND hDlg, LPARAM lParam )
  262. {
  263.     if( HIWORD( lParam ) == EN_CHANGE )
  264.         {                       // enables or disables the OK button
  265.                                 // depending on whether there are any
  266.                                 // characters in the edit box.
  267.         BOOL active =
  268.         (BOOL)SendMessage( (HWND)LOWORD( lParam ), WM_GETTEXTLENGTH, 0, 0 );
  269.         EnableWindow( GetDlgItem( hDlg, IDOK ), active );
  270.         return TRUE;
  271.         }
  272.     else
  273.         return FALSE;
  274. }
  275.  
  276. //---------------------------------------------------------------------
  277. //
  278. //  void FileBox::okCmd( HWND hDlg );
  279. //
  280. //  called when the user selects the OK button.  Validates the file
  281. //  name.  Resets the dialog if the name is contains wildcards or is
  282. //  invalid, and terminates the dialog if it's a valid file name.
  283. //
  284. //---------------------------------------------------------------------
  285.  
  286. void FileBox::okCmd( HWND hDlg )
  287. {
  288.     GetDlgItemText( hDlg, IDD_FNAME, path, 80 );
  289.                                 // get the specified path.
  290.  
  291.     int len = strlen( path );
  292.     char lastChar = path[ len - 1 ];
  293.  
  294.     if( lastChar == '\\' || lastChar == ':' )
  295.         strcat( path, iSpec );  // if the path ends in '\' or ':' it's not
  296.                                 // a full spec, so append the default spec.
  297.  
  298.     if( strpbrk( path, "*?" ) != 0 )
  299.         {                       // if the path contains a wildcard,
  300.         resetDlg( hDlg );       // reset the dialog and continue.
  301.         return;
  302.         }
  303.  
  304.     if( mustExist == TRUE )     // if we require the file to exist, check it.
  305.         {
  306.         ffblk fileInfo;
  307.         if( findfirst( path, &fileInfo, 0 ) )
  308.             {
  309.             MessageBeep(0);     // file doesn't exist.
  310.             return;
  311.             }
  312.         }
  313.  
  314.     EndDialog( hDlg, TRUE );    // we're done!
  315. }
  316.  
  317. //---------------------------------------------------------------------
  318. //
  319. //  void FileBox::cancelCmd( HWND hDlg );
  320. //
  321. //  called when the user selects the Cancel button.  Terminates the
  322. //  dialog.
  323. //
  324. //---------------------------------------------------------------------
  325.  
  326. void FileBox::cancelCmd( HWND hDlg )
  327. {
  328.     result = 1;
  329.     EndDialog( hDlg, TRUE );
  330. }
  331.  
  332. //---------------------------------------------------------------------
  333. //
  334. //  BOOL FileBox::dispatch( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  335. //
  336. //  dispatches commands.
  337. //
  338. //---------------------------------------------------------------------
  339.  
  340. BOOL FileBox::dispatch( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  341. {
  342.     switch( msg )
  343.         {
  344.         case WM_INITDIALOG:
  345.  
  346.             initDlg( hDlg );
  347.             return TRUE;
  348.  
  349.         case WM_COMMAND:
  350.  
  351.             switch( wParam )
  352.                 {
  353.                 case IDD_FLIST:
  354.  
  355.                     if( flistCmd( hDlg, lParam ) == TRUE )
  356.                         return TRUE;
  357.                     break;
  358.  
  359.                 case IDD_FNAME:
  360.  
  361.                     if( fnameCmd( hDlg, lParam ) == TRUE )
  362.                         return TRUE;
  363.                     break;
  364.  
  365.                 case IDOK:
  366.  
  367.                     okCmd( hDlg );
  368.                     return TRUE;
  369.  
  370.                 case IDCANCEL:
  371.  
  372.                     cancelCmd( hDlg );
  373.                     return TRUE;
  374.  
  375.                 default:
  376.  
  377.                     break;
  378.                 }
  379.         default:
  380.  
  381.             break;
  382.         }
  383.  
  384.     return ModalDialog::dispatch( hDlg, msg, wParam, lParam );
  385.  
  386. }
  387.  
  388. //---------------------------------------------------------------------
  389. //
  390. //  member functions for class EditBox.
  391. //
  392. //---------------------------------------------------------------------
  393.  
  394. LPSTR EditBox::getDialogName()
  395. {
  396.     return "TodoEdit";
  397. }
  398.  
  399. //---------------------------------------------------------------------
  400. //
  401. //  void EditBox::initDlg( HWND hDlg );
  402. //
  403. //  initializes the dialog box.
  404. //
  405. //---------------------------------------------------------------------
  406.  
  407. void EditBox::initDlg( HWND hDlg )
  408. {
  409.     char temp[100];
  410.  
  411.     // set up the current date edit field.
  412.     ostrstream( temp, sizeof( temp ) ) << current.dateCreated << ends;
  413.     SetDlgItemText( hDlg, IDE_DATEENT, temp );
  414.  
  415.     // set up the date due edit field.
  416.     ostrstream( temp, sizeof( temp ) ) << current.dateDue << ends;
  417.     SetDlgItemText( hDlg, IDE_DATEDUE, temp );
  418.  
  419.     // set up the text edit field
  420.     SetDlgItemText( hDlg, IDE_TEXT, current.text );
  421.  
  422.     // set up the correct radio button
  423.     button = IDE_HIGH + 1 - current.priority;
  424.     CheckRadioButton( hDlg, IDE_LOW, IDE_HIGH, button );
  425. }
  426.  
  427. //---------------------------------------------------------------------
  428. //
  429. //  void EditBox::checkButton( HWND hDlg, WPARAM wParam );
  430. //
  431. //  called when the user selects one of the radio buttons.
  432. //
  433. //---------------------------------------------------------------------
  434.  
  435. void EditBox::checkButton( HWND hDlg, WPARAM wParam )
  436. {
  437.     button = wParam;
  438.     CheckRadioButton( hDlg, IDE_LOW, IDE_HIGH, button );
  439. }
  440.  
  441. //---------------------------------------------------------------------
  442. //
  443. //  void EditBox::okCmd( HWND hDlg );
  444. //
  445. //  called when the user selects the OK button.  Copies data from the
  446. //  dialog into the Todo entry and terminates the dialog.
  447. //
  448. //---------------------------------------------------------------------
  449.  
  450. void EditBox::okCmd( HWND hDlg )
  451. {
  452.     char temp[100 ];
  453.  
  454.     //  copy date created from dialog.
  455.     GetDlgItemText( hDlg, IDE_DATEENT, temp, sizeof( temp ) );
  456.     current.dateCreated = temp;
  457.  
  458.     //  copy date due from dialog.
  459.     GetDlgItemText( hDlg, IDE_DATEDUE, temp, sizeof( temp ) );
  460.     current.dateDue = temp;
  461.  
  462.     //  copy text from dialog
  463.     int len = (int)SendDlgItemMessage(hDlg,IDE_TEXT,EM_LINELENGTH,0,0) + 1;
  464.     delete current.text;
  465.     current.text = new char[ len ];
  466.     GetDlgItemText( hDlg, IDE_TEXT, current.text, len );
  467.  
  468.     //  copy priority from dialog.
  469.     current.priority = IDE_HIGH + 1 - button;
  470.  
  471.     //  mark this entry as modified.
  472.     current.dirty = TRUE;
  473.  
  474.     EndDialog( hDlg, TRUE );
  475. }
  476.  
  477. //---------------------------------------------------------------------
  478. //
  479. //  void EditBox::cancelCmd( HWND hDlg );
  480. //
  481. //  called when the user selects the Cancel button.  Terminates the
  482. //  dialog without changing any fields in the entry.
  483. //
  484. //---------------------------------------------------------------------
  485.  
  486. void EditBox::cancelCmd( HWND hDlg )
  487. {
  488.     result = 1;
  489.     EndDialog( hDlg, TRUE );
  490. }
  491.  
  492. //---------------------------------------------------------------------
  493. //
  494. //  BOOL EditBox::dispatch( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  495. //
  496. //  dispatches commands.
  497. //
  498. //---------------------------------------------------------------------
  499.  
  500. BOOL EditBox::dispatch( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  501. {
  502.     switch( msg )
  503.         {
  504.         case WM_INITDIALOG:
  505.  
  506.             initDlg( hDlg );
  507.             return TRUE;
  508.  
  509.         case WM_COMMAND:
  510.  
  511.             switch( wParam )
  512.                 {
  513.                 case IDE_LOW:
  514.                 case IDE_MEDIUM:
  515.                 case IDE_HIGH:
  516.  
  517.                     checkButton( hDlg, wParam );
  518.                     return TRUE;
  519.  
  520.                 case IDOK:
  521.  
  522.                     okCmd( hDlg );
  523.                     return TRUE;
  524.  
  525.                 case IDCANCEL:
  526.  
  527.                     cancelCmd( hDlg );
  528.                     return TRUE;
  529.                 }
  530.         }
  531.  
  532.     return ModalDialog::dispatch( hDlg, msg, wParam, lParam );
  533. }
  534.