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

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