home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / COMMONDG.PAK / FINDDLG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.0 KB  |  197 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: finddlg.c
  9. //
  10. //  PURPOSE: To show the use of the find and replace common dialogs.
  11. //
  12. //
  13. //  FUNCTIONS:
  14. //    InitFindReplace - Register the windows message for find/rep notifications.
  15. //    CmdFindReplace - Opens the find or replace modeless dialog box.
  16. //    MsgFindReplace - Handle the messages generated by the find/replace dialog.
  17. //
  18. //  COMMENTS:
  19. //
  20. //
  21. //
  22. //  SPECIAL INSTRUCTIONS: N/A
  23. //
  24.  
  25. #include <windows.h>            // required for all Windows applications
  26. #ifdef WIN16
  27. #include "win16ext.h"           // required only for win16 applications
  28. #include "commdlg.h"           
  29. #endif
  30. #include "globals.h"            // prototypes specific to this application
  31. #include "resource.h"
  32.  
  33. #define CCHMAXFIND 80
  34. #define CCHMAXREPL 80
  35.  
  36. static HWND hdlg = NULL;        // handle identifying the find/rep dialog
  37.  
  38. static char szFind[CCHMAXFIND] = {'\0'}; // The string to find
  39. static char szRepl[CCHMAXREPL] = {'\0'}; // The string to replace
  40.  
  41.  
  42.  
  43. //
  44. //  FUNCTION: InitFindReplace(VOID)
  45. //
  46. //  PURPOSE: Register the windows message for find/replace notifications
  47. //
  48. //  PARAMETERS:
  49. //    NONE.
  50. //
  51. //  RETURN VALUE:
  52. //    TRUE - Initialization succeeded - the message was registered.
  53. //    FALSE - Initialization failed - the message was not registered.
  54. //
  55. //  COMMENTS:
  56. //
  57. //
  58.  
  59. BOOL InitFindReplace(VOID)
  60. {
  61.     msdiMain.rgmsd[0].uMessage = RegisterWindowMessage(FINDMSGSTRING);
  62.     if (msdiMain.rgmsd[0].uMessage == 0)
  63.     {
  64.         MessageBox(
  65.             NULL,
  66.             "RegisterWindowMessage(FINDMSGSTRING) Failed",
  67.             "Find Dialog Initialization",
  68.             MB_ICONINFORMATION|MB_OK
  69.         );
  70.         return FALSE;
  71.     }
  72.     else
  73.     {
  74.         return TRUE;
  75.     }
  76. }
  77.  
  78.  
  79. //
  80. //  FUNCTION: CmdFindReplace(HWND, WORD, WORD, HWND)
  81. //
  82. //  PURPOSE: Opens the find or replace modeless dialog box.
  83. //
  84. //  PARAMETERS:
  85. //    hwnd     - The window handle.
  86. //    wCommand - IDM_FIND || IDM_REPLACE
  87. //    wNotify   - Notification number (unused)
  88. //    hwndCtrl - NULL (Unused)
  89. //
  90. //  RETURN VALUE:
  91. //    Always returns 0 - message handled.
  92. //
  93. //  COMMENTS:
  94. //    If a find or replace dialog already exists, this command just
  95. //      brings it to the top.
  96. //
  97. //
  98.  
  99. #pragma argsused
  100. LRESULT CmdFindReplace(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  101. {
  102.     static FINDREPLACE fr = {0};   // common dialog box structure
  103.  
  104.     if (hdlg != NULL)
  105.     {
  106.         BringWindowToTop(hdlg);
  107.         return 0;
  108.     }
  109.  
  110.     // Initialize the find/replace structure
  111.  
  112.     fr.lStructSize = sizeof(FINDREPLACE);
  113.     fr.hwndOwner = hwnd;
  114.     fr.lpstrFindWhat = szFind;
  115.     fr.wFindWhatLen = sizeof(szFind);
  116.     fr.lpstrReplaceWith = szRepl;
  117.     fr.wReplaceWithLen = sizeof(szRepl);
  118.  
  119.     // Display the modeless Find/Replace dialog box.
  120.  
  121.     if (wCommand == IDM_FIND)
  122.     {
  123.         hdlg = FindText(&fr);
  124.     }
  125.     else
  126.     {
  127.         hdlg = ReplaceText(&fr);
  128.     }
  129.  
  130.     return 0;
  131. }
  132.  
  133. //
  134. //  FUNCTION: MsgFindReplace(HWND, UINT, WPARAM, LPARAM)
  135. //
  136. //  PURPOSE: Handle the messages generated by the find/replace dialog.
  137. //
  138. //  PARAMETERS:
  139. //
  140. //    hwnd      - Window handle  (Unused)
  141. //    uMessage  - Message number (Unused)
  142. //    wparam    - Extra data     (Unused)
  143. //    lparam    - LPFINDREPLACE
  144. //
  145. //  RETURN VALUE:
  146. //    Always returns 0 - Message handled
  147. //
  148. //  COMMENTS:
  149. //    If FR_DIALOGTERM flag is set, set hdlg to NULL so that the CmdFindReplace
  150. //    function can open another one when it is called.
  151. //    Otherwise call the user defined FindReplace function with the information
  152. //    about the find/replace to do the real work.
  153. //
  154.  
  155. #pragma argsused
  156. LRESULT MsgFindReplace(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  157. {
  158.      LPFINDREPLACE lpfr;
  159.  
  160.      // Extract a pointer to the FINDREPLACE structure from lParam.
  161.  
  162.     lpfr = (FINDREPLACE FAR*) lparam;
  163.  
  164.     // If the system has set the FR_DIALOGTERM flag, invalidate the
  165.     //  handle identifying the dialog box.
  166.  
  167.     if (lpfr->Flags & FR_DIALOGTERM)
  168.     {
  169.         hdlg = NULL;
  170.         lpfr->Flags &= !FR_DIALOGTERM;
  171.         return 0;
  172.      }
  173.  
  174.     // If the dialog box is still valid, call the application-defined
  175.     //   search or replace routine
  176.  
  177.     FindReplace(szFind,
  178.                 szRepl,
  179.                 (BOOL)(lpfr->Flags & FR_FINDNEXT),
  180.                 (BOOL)(lpfr->Flags & FR_REPLACE),
  181.                 (BOOL)(lpfr->Flags & FR_REPLACEALL),
  182.                 (BOOL)(lpfr->Flags & FR_DOWN),
  183.                 (BOOL)(lpfr->Flags & FR_MATCHCASE),
  184.                 (BOOL)(lpfr->Flags & FR_WHOLEWORD));
  185.  
  186.      return 0;
  187. }
  188.  
  189.  
  190. BOOL IsFindReplaceMsg(LPMSG lpmsg)
  191. {
  192.     if (hdlg != NULL)
  193.         return IsDialogMessage(hdlg, lpmsg);
  194.     else
  195.         return FALSE;
  196. }
  197.