home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / EDITWND.CPP < prev    next >
Text File  |  1995-08-29  |  5KB  |  171 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. /* --------------------------------------------------------
  4.   EDITWND.CPP
  5.   Defines type TEditWindow, a text editor (cannot perform
  6.   file I/O)).
  7.   -------------------------------------------------------- */
  8.  
  9. #include "editwnd.h"
  10.  
  11. __link(RegEdit)
  12.  
  13. TSearchDialog::TSearchDialog(PTWindowsObject AParent,
  14.                int ResourceId, TSearchStruct &SearchStruct, PTModule AModule)
  15.                : TDialog(AParent, ResourceId, AModule)
  16. {
  17.   new TEdit(this, ID_SEARCHTEXT, sizeof SearchStruct.SearchText,
  18.             GetModule());
  19.   new TCheckBox(this, ID_CASESENSITIVE, (PTGroupBox)NULL, GetModule());
  20.   if ( ResourceId == SD_REPLACE )
  21.   {
  22.     new TEdit(this, ID_REPLACETEXT, sizeof SearchStruct.ReplaceText,
  23.               GetModule());
  24.     new TCheckBox(this, ID_REPLACEALL, (PTGroupBox)NULL, GetModule());
  25.     new TCheckBox(this, ID_PROMPTONREPLACE, (PTGroupBox)NULL, GetModule());
  26.   }
  27.   TransferBuffer = &SearchStruct;
  28. }
  29.  
  30. /* Constructor for a TEditWindow.  Initializes its data fields using
  31.    passed parameters and default values.  Constructs its child edit
  32.    control. */
  33. TEditWindow::TEditWindow(PTWindowsObject AParent, LPSTR ATitle,
  34.                          PTModule AModule)
  35.             : TWindow(AParent, ATitle, AModule)
  36. {
  37.   Editor = new TEdit(this, ID_EDITOR, NULL, 0, 0, 0, 0, 0, TRUE, GetModule());
  38.   Editor->Attr.Style |= ES_NOHIDESEL;
  39.   IsReplaceOp = FALSE;
  40.   memset(&SearchStruct, 0x0, sizeof SearchStruct);
  41. }
  42.  
  43. /* Responds to an incoming WM_SIZE message by resizing the child edit
  44.   control according to the size of the TEditWindow's client area. */
  45. void TEditWindow::WMSize(RTMessage Msg)
  46. {
  47.   TWindow::WMSize(Msg);
  48.   SetWindowPos(Editor->HWindow, 0, -1, -1, LOWORD(Msg.LParam)+2,
  49.     HIWORD(Msg.LParam)+2, SWP_NOZORDER);
  50. }
  51.  
  52. /* Responds to an incoming WM_SETFOCUS message by setting the focus to
  53.    the child edit control. */
  54. void TEditWindow::WMSetFocus(RTMessage)
  55. {
  56.   SetFocus(Editor->HWindow);
  57. }
  58.  
  59. void TEditWindow::DoSearch()
  60. {
  61.   char S[81];
  62.   LPSTR P;
  63.   int Rslt = 0;
  64.   BOOL TextFoundSinceUserInput = FALSE;
  65.  
  66.   do {
  67.     Rslt = Editor->Search(-1, SearchStruct.SearchText,
  68.                                    SearchStruct.CaseSensitive);
  69.     if ( Rslt == -1 )
  70.     {
  71.       if ( !IsReplaceOp || !SearchStruct.ReplaceAll ||
  72.            !TextFoundSinceUserInput )
  73.       {
  74.         P = SearchStruct.SearchText;
  75.         wsprintf(S, "\"%0.60s\" not found.", P);
  76.         MessageBox(HWindow, S, "Find error",
  77.                                     MB_OK | MB_ICONEXCLAMATION);
  78.       }
  79.     }
  80.     else
  81.       if ( IsReplaceOp )
  82.       {
  83.         if ( !SearchStruct.PromptOnReplace )
  84.         {
  85.           TextFoundSinceUserInput = TRUE;
  86.           Editor->Insert(SearchStruct.ReplaceText);
  87.         }
  88.         else
  89.         {
  90.           Rslt = MessageBox(HWindow, "Replace this occurrence?",
  91.                     "Search/Replace", MB_YESNOCANCEL | MB_ICONQUESTION);
  92.           if ( Rslt == IDYES )
  93.             Editor->Insert(SearchStruct.ReplaceText);
  94.           else
  95.             if ( Rslt == IDCANCEL )
  96.               return;
  97.           TextFoundSinceUserInput = FALSE;
  98.         }
  99.       }
  100.   }
  101.   while ( (Rslt != -1) && SearchStruct.ReplaceAll && IsReplaceOp );
  102. }
  103.  
  104. /*  */
  105. void TEditWindow::CMEditFind(RTMessage)
  106. {
  107.   if (GetModule()->ExecDialog( new TSearchDialog(
  108.         this, SD_SEARCH, SearchStruct, GetModule())) == IDOK )
  109.   {
  110.     IsReplaceOp = FALSE;
  111.     DoSearch();
  112.   }
  113. }
  114.  
  115. /*  */
  116. void TEditWindow::CMEditFindNext(RTMessage)
  117. {
  118.   DoSearch();
  119. }
  120.  
  121. /*  */
  122. void TEditWindow::CMEditReplace(RTMessage)
  123. {
  124.   if (GetModule()->ExecDialog( new TSearchDialog(
  125.         this, SD_REPLACE, SearchStruct, GetModule())) == IDOK )
  126.   {
  127.     IsReplaceOp = TRUE;
  128.     DoSearch();
  129.   }
  130. }
  131.  
  132. /* Reads an instance of TEditWindow from the passed ipstream. */
  133. void *TEditWindow::read(ipstream& is)
  134. {
  135.   TWindow::read(is);
  136.  
  137.   GetChildPtr(is, (PTWindowsObject)Editor);
  138.  
  139.   is.readBytes(SearchStruct.SearchText, 81);
  140.   is >> SearchStruct.CaseSensitive;
  141.   is.readBytes(SearchStruct.ReplaceText, 81);
  142.   is >> SearchStruct.ReplaceAll >> SearchStruct.PromptOnReplace;
  143.  
  144.   is >> IsReplaceOp;
  145.   return this;
  146. }
  147.  
  148. /* Writes the TEditWindow to the passed opstream. */
  149. void TEditWindow::write(opstream& os)
  150. {
  151.   TWindow::write(os);
  152.  
  153.   PutChildPtr(os, Editor);
  154.  
  155.   os.writeBytes(SearchStruct.SearchText, 81);
  156.   os << SearchStruct.CaseSensitive;
  157.   os.writeBytes(SearchStruct.ReplaceText, 81);
  158.   os << SearchStruct.ReplaceAll << SearchStruct.PromptOnReplace;
  159.  
  160.   os << IsReplaceOp;
  161. }
  162.  
  163. TStreamable *TEditWindow::build()
  164. {
  165.   return new TEditWindow(streamableInit);
  166. }
  167.  
  168. TStreamableClass RegEditWindow("TEditWindow",
  169.                  TEditWindow::build,
  170.                      __DELTA(TEditWindow));
  171.