home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / EDITSEAR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  5.2 KB  |  208 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of class TEditWindow, an edit control that responds to
  6. //   Find, Replace and FindNext commands.
  7. //----------------------------------------------------------------------------
  8. #pragma hdrignore SECTION
  9. #include <owl/owlpch.h>
  10. #include <owl/editsear.h>
  11. #include <owl/applicat.h>
  12. #include <owl/edit.h>
  13. #include <owl/findrepl.h>
  14. #include <cstring.h>
  15.  
  16. #if !defined(SECTION) || SECTION == 1
  17.  
  18. DEFINE_RESPONSE_TABLE1(TEditSearch, TEdit)
  19.   EV_COMMAND(CM_EDITFIND, CmEditFind),
  20.   EV_COMMAND_ENABLE(CM_EDITFIND, CeEditFindReplace),
  21.   EV_COMMAND(CM_EDITREPLACE, CmEditReplace),
  22.   EV_COMMAND_ENABLE(CM_EDITREPLACE, CeEditFindReplace),
  23.   EV_COMMAND(CM_EDITFINDNEXT, CmEditFindNext),
  24.   EV_COMMAND_ENABLE(CM_EDITFINDNEXT, CeEditFindNext),
  25.   EV_REGISTERED(FINDMSGSTRING, EvFindMsg),
  26. END_RESPONSE_TABLE;
  27.  
  28. //
  29. // Construct a TEditSearch window given some initial text.
  30. //
  31. TEditSearch::TEditSearch(TWindow*        parent,
  32.                          int             id,
  33.                          const char far* text,
  34.                          int x, int y, int w, int h,
  35.                          TModule*        module)
  36. :
  37.   TEdit(parent, id, text, x, y, w, h, 0, true, module),
  38.   SearchData(FR_DOWN)
  39. {
  40.   Attr.Style |= ES_NOHIDESEL;
  41.   SearchDialog = 0;
  42.   SearchCmd = 0;
  43. }
  44.  
  45. TEditSearch::~TEditSearch()
  46. {
  47.   delete SearchDialog;
  48. }
  49.  
  50. //
  51. // Post a CM_EDITFIND or a CM_EDITREPLACE to re-open a previously open
  52. // find or replace modeless dialog
  53. //
  54. void
  55. TEditSearch::SetupWindow()
  56. {
  57.   TEdit::SetupWindow();
  58.   if (SearchCmd)
  59.     PostMessage(WM_COMMAND, SearchCmd);
  60. }
  61.  
  62. //
  63. // Perform a search or replace operation based on information in SearchData
  64. //
  65. void
  66. TEditSearch::DoSearch()
  67. {
  68.   do {
  69. #if defined(BI_PLAT_WIN32)
  70.     unsigned long version = ::GetVersion();
  71.     if (version & 0x80000000L) {
  72.       if (GetApplication())
  73.         GetApplication()->PumpWaitingMessages();
  74.     }
  75. #endif
  76.     if (Search(-1, SearchData.FindWhat, bool(SearchData.Flags&FR_MATCHCASE),
  77.                bool(SearchData.Flags&FR_WHOLEWORD),
  78.                !(SearchData.Flags&FR_DOWN)) >= 0) {
  79.       if (SearchData.Flags & (FR_REPLACE|FR_REPLACEALL))
  80.         Insert(SearchData.ReplaceWith);
  81.     }
  82.     else {
  83.       if (SearchData.Flags & (FR_FINDNEXT|FR_REPLACE)) {
  84.         string errTemplate(GetModule()->LoadString(IDS_CANNOTFIND));
  85.         char  errMsg[81];
  86.         wsprintf(errMsg, errTemplate.c_str(), (const char far*)SearchData.FindWhat);
  87.         TWindow* w = SearchDialog ? (TWindow*)SearchDialog : (TWindow*)this;
  88.         w->MessageBox(errMsg, GetApplication()->GetName(),
  89.                       MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL);
  90.       }
  91.       else if (SearchData.Flags & FR_REPLACEALL)
  92.         break;
  93.     }
  94.   } while (SearchData.Flags & FR_REPLACEALL);
  95. }
  96.  
  97. //
  98. // Open the modeless Find commdlg
  99. //
  100. void
  101. TEditSearch::CmEditFind()
  102. {
  103.   if (!SearchCmd) {
  104.     SearchCmd = CM_EDITFIND;
  105.     delete SearchDialog;
  106.     SearchDialog = new TFindDialog(this, SearchData);
  107.     SearchDialog->Create();
  108.   }
  109. }
  110.  
  111. //
  112. // Open the modeless Replace commdlg
  113. //
  114. void
  115. TEditSearch::CmEditReplace()
  116. {
  117.   if (!SearchCmd) {
  118.     SearchCmd = CM_EDITREPLACE;
  119.     delete SearchDialog;
  120.     SearchDialog = new TReplaceDialog(this, SearchData);
  121.     SearchDialog->Create();
  122.   }
  123. }
  124.  
  125. //
  126. // Enable the find or replace option only if no dialog is up
  127. //
  128. void
  129. TEditSearch::CeEditFindReplace(TCommandEnabler& ce)
  130. {
  131.   ce.Enable(!SearchCmd);
  132. }
  133.  
  134. //
  135. // Respond to the possible separate menu command to repeat the search
  136. //
  137. void
  138. TEditSearch::CmEditFindNext()
  139. {
  140.   if (SearchDialog)
  141.     SearchDialog->UpdateData();
  142.   SearchData.Flags |= FR_FINDNEXT;
  143.   DoSearch();
  144. }
  145.  
  146. //
  147. // Only enable FindNext if we've got data to search for
  148. //
  149. void
  150. TEditSearch::CeEditFindNext(TCommandEnabler& ce)
  151. {
  152.   ce.Enable((SearchData.FindWhat && *(SearchData.FindWhat)) ? true : false);
  153. }
  154.  
  155. //
  156. // Respond to the message sent by the modeless find/replace dialog by
  157. // performing a search. Or, if the dialog has terminated, zero search command
  158. //
  159. LRESULT
  160. TEditSearch::EvFindMsg(WPARAM, LPARAM lParam)
  161. {
  162.   PRECONDITION(SearchDialog);
  163.  
  164.   SearchDialog->UpdateData(lParam);
  165.   if (SearchData.Flags & FR_DIALOGTERM)
  166.     SearchCmd = 0;
  167.  
  168.   else
  169.     DoSearch();
  170.   return 0;
  171. }
  172.  
  173.  
  174. #endif
  175. //----------------------------------------------------------------------------
  176. #if !defined(SECTION) || SECTION == 2
  177.  
  178. IMPLEMENT_STREAMABLE1(TEditSearch, TEdit);
  179.  
  180. //
  181. // reads an instance of TEditSearch from the passed ipstream.
  182. // Re-opens the modeless find or replace dialog if one was up.
  183. //
  184. void*
  185. TEditSearch::Streamer::Read(ipstream& is, uint32 /*version*/) const
  186. {
  187.   ReadBaseObject((TEdit*)GetObject(), is);
  188.  
  189.   GetObject()->SearchData.Read(is);
  190.   is >> GetObject()->SearchCmd;
  191.   GetObject()->SearchDialog = 0;
  192.   return GetObject();
  193. }
  194.  
  195. //
  196. // writes the TEditSearch to the passed opstream
  197. //
  198. void
  199. TEditSearch::Streamer::Write(opstream& os) const
  200. {
  201.   WriteBaseObject((TEdit*)GetObject(), os);
  202.  
  203.   GetObject()->SearchData.Write(os);
  204.   os << GetObject()->SearchCmd;
  205. }
  206.  
  207. #endif
  208.