home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / EDITSEAR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  4.4 KB  |  177 lines

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