home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / WRITEPAD.PAK / FINDDLG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.3 KB  |  253 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
  15. //                             notifications.
  16. //    CmdSearchFindReplace - Opens the find or replace modeless dialog box.
  17. //    CmdSearchFindNext    - Opens the find or replace modeless dialog box.
  18. //    MsgFindReplace       - Handle the messages generated by the 
  19. //                             find/replace dialog.
  20. //
  21. //  COMMENTS:
  22. //
  23. //
  24. //
  25. //  SPECIAL INSTRUCTIONS: N/A
  26. //
  27.  
  28. #include <windows.h>            // required for all Windows applications
  29. #include "globals.h"            // prototypes specific to this application
  30. #include "resource.h"
  31.  
  32. #define CCHMAXFIND 80
  33. #define CCHMAXREPL 80
  34.  
  35. static HWND hdlg = NULL;        // handle identifying the find/rep dialog
  36. static WORD wCommCur = 0;       // Current command - find or replace
  37. static char szFind[CCHMAXFIND] = {'\0'}; // The string to find
  38. static char szRepl[CCHMAXREPL] = {'\0'}; // The string to replace
  39.  
  40. // common dialog box structure
  41. static FINDREPLACE fr = {0,0,0,FR_DOWN|FR_MATCHCASE,0};
  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.     return msdiMain.rgmsd[0].uMessage != 0;
  63. }
  64.  
  65. BOOL IsFindReplaceMsg(LPMSG lpmsg)
  66. {
  67.     if (hdlg != NULL)
  68.         return IsDialogMessage(hdlg, lpmsg);
  69.     else
  70.         return FALSE;
  71. }
  72.  
  73. BOOL CanFind(VOID)
  74. {
  75.     return szFind[0] != '\0';
  76. }
  77.  
  78.  
  79. //
  80. //  FUNCTION: CmdSearchFindReplace(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 CmdSearchFindReplace(HWND hwnd,
  101.                              WORD wCommand, 
  102.                              WORD wNotify, 
  103.                              HWND hwndCtrl) 
  104. {
  105.     if (hdlg != NULL)
  106.     {
  107.         if (wCommand == wCommCur)
  108.         {
  109.             BringWindowToTop(hdlg);
  110.             return 0;
  111.         }
  112.         else 
  113.         {
  114.             DestroyWindow(hdlg);
  115.         }
  116.     }
  117.     wCommCur = wCommand;
  118.  
  119.     // Initialize the find/replace structure
  120.  
  121.     fr.lStructSize = sizeof(FINDREPLACE);
  122.     fr.hwndOwner = hwnd;
  123.     fr.lpstrFindWhat = szFind;
  124.     fr.wFindWhatLen = sizeof(szFind);
  125.     fr.lpstrReplaceWith = szRepl;
  126.     fr.wReplaceWithLen = sizeof(szRepl);
  127.  
  128.     // Display the modeless Find/Replace dialog box.
  129.  
  130.     if (wCommand == IDM_FIND)
  131.     {
  132.         hdlg = FindText(&fr);
  133.     }
  134.     else
  135.     {
  136.         hdlg = ReplaceText(&fr);
  137.     }
  138.  
  139.     return 0;
  140. }
  141.  
  142. //
  143. //  FUNCTION: MsgFindReplace(HWND, UINT, WPARAM, LPARAM)
  144. //
  145. //  PURPOSE: Handle the messages generated by the find/replace dialog.
  146. //
  147. //  PARAMETERS:
  148. //
  149. //    hwnd      - Window handle  (Unused)
  150. //    uMessage  - Message number (Unused)
  151. //    wparam    - Extra data     (Unused)
  152. //    lparam    - LPFINDREPLACE
  153. //
  154. //  RETURN VALUE:
  155. //    Always returns 0 - Message handled
  156. //
  157. //  COMMENTS:
  158. //    If FR_DIALOGTERM flag is set, set hdlg to NULL so that the CmdFindReplace
  159. //    function can open another one when it is called.
  160. //    Otherwise call the user defined FindReplace function with the information
  161. //    about the find/replace to do the real work.
  162. //
  163.  
  164. #pragma argsused
  165. LRESULT MsgFindReplace(HWND hwnd,
  166.                        UINT uMessage, 
  167.                        WPARAM wparam, 
  168.                        LPARAM lparam)
  169. {
  170.     LPFINDREPLACE lpfr;
  171.     FRT frt;
  172.  
  173.      // Extract a pointer to the FINDREPLACE structure from lParam.
  174.  
  175.     lpfr = (FINDREPLACE FAR*) lparam;
  176.  
  177.     // If the system has set the FR_DIALOGTERM flag, invalidate the
  178.     //  handle identifying the dialog box.
  179.  
  180.     if (lpfr->Flags & FR_DIALOGTERM)
  181.     {
  182.         hdlg = NULL;
  183.         lpfr->Flags &= ~FR_DIALOGTERM;
  184.         return 0;
  185.     }
  186.  
  187.     // Determine if the request is a find, replace, or replace all.
  188.  
  189.     if (lpfr->Flags & FR_FINDNEXT)
  190.     {
  191.         frt = frtFind;
  192.     }
  193.     else 
  194.         if (lpfr->Flags & FR_REPLACE) 
  195.         {
  196.             frt = frtReplace;
  197.         }
  198.     else
  199.         if (lpfr->Flags & FR_REPLACEALL)
  200.         {
  201.             frt = frtRepAll;
  202.         }
  203.      else
  204.         {
  205.             return 0;
  206.         }
  207.  
  208.     // If the dialog box is still valid, call the application-defined
  209.     //   search or replace routine
  210.  
  211.     FindReplace(szFind, 
  212.                 szRepl,
  213.                 frt,
  214.                 MAKEBOOL(lpfr->Flags & FR_DOWN),
  215.                 MAKEBOOL(lpfr->Flags & FR_MATCHCASE),
  216.                 MAKEBOOL(lpfr->Flags & FR_WHOLEWORD));
  217.  
  218.      return 0;
  219. }
  220.  
  221. //
  222. //  FUNCTION: CmdFindNext(HWND, WORD, WORD, HWND)
  223. //
  224. //  PURPOSE: Opens the find or replace modeless dialog box.
  225. //
  226. //  PARAMETERS:
  227. //    hwnd     - The window handle.
  228. //    wCommand - IDM_FINDPREV || IDM_FINDNEXT
  229. //    wNotify   - Notification number (unused)
  230. //    hwndCtrl - NULL (Unused)
  231. //
  232. //  RETURN VALUE:
  233. //    Always returns 0 - message handled.
  234. //
  235. //  COMMENTS:
  236. //    If a find or replace dialog already exists, this command just
  237. //      brings it to the top.
  238. //
  239. //
  240.  
  241. #pragma argsused
  242. LRESULT CmdSearchFindNext(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  243. {
  244.      FindReplace(szFind,
  245.                      szRepl,
  246.                      frtFind,
  247.                      wCommand == IDM_FINDNEXT,
  248.                      MAKEBOOL(fr.Flags & FR_MATCHCASE),
  249.                      MAKEBOOL(fr.Flags & FR_WHOLEWORD));
  250.  
  251.     return 0;
  252. }
  253.