home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / wksinst / rwpdemo.pak / STDWNDSB.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-09-09  |  14.7 KB  |  534 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Turbo Pascal for Windows                        }
  5. {       Standard windows unit for ObjectWindows         }
  6. {       Modified for use with BWCC.dll                  }
  7. {                                                       }
  8. {       Copyright (c) 1991 Borland International        }
  9. {                                                       }
  10. {*******************************************************}
  11.  
  12. unit StdWndsB;
  13.  
  14.  
  15. interface
  16.  
  17. {$ifdef BWCC}
  18.  
  19. { add bwcc at end of uses list}
  20.  
  21. uses WObjectB, WinTypes, WinProcs, WinDos, StdDlgs, Strings, Bwcc;
  22.  
  23. {alternate res file}
  24.  
  25. {$R STDWNDSB.RES}
  26.  
  27. {$else}
  28. uses WObjects, WinTypes, WinProcs, WinDos, StdDlgs, Strings;
  29.  
  30. {$R STDWNDS.RES}
  31.  
  32. {$endif}
  33.  
  34. type
  35.  
  36.   { TSearchRec }
  37.   TSearchRec = record
  38.     SearchText: array[0..80] of Char;
  39.     CaseSensitive: Bool;
  40.     ReplaceText: array[0..80] of Char;
  41.     ReplaceAll: Bool;
  42.     PromptOnReplace: Bool;
  43.     IsReplace: Boolean;
  44.   end;
  45.  
  46.   { TEditWindow  }
  47.   PEditWindow = ^TEditWindow;
  48.   TEditWindow = object(TWindow)
  49.     Editor: PEdit;
  50.     SearchRec: TSearchRec;
  51.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  52.     constructor Load(var S: TStream);
  53.     procedure Store(var S: TStream);
  54.     procedure WMSize(var Msg: TMessage);
  55.       virtual wm_First + wm_Size;
  56.     procedure WMSetFocus(var Msg: TMessage);
  57.       virtual wm_First + wm_SetFocus;
  58.     procedure CMEditFind(var Msg: TMessage);
  59.       virtual cm_First + cm_EditFind;
  60.     procedure CMEditFindNext(var Msg: TMessage);
  61.       virtual cm_First + cm_EditFindNext;
  62.     procedure CMEditReplace(var Msg: TMessage);
  63.       virtual cm_First + cm_EditReplace;
  64.   private
  65.     procedure DoSearch;
  66.   end;
  67.  
  68.   { TFileWindow }
  69.   PFileWindow = ^TFileWindow;
  70.   TFileWindow = object(TEditWindow)
  71.     FileName: PChar;
  72.     IsNewFile: Boolean;
  73.     constructor Init(AParent: PWindowsObject; ATitle, AFileName: PChar);
  74.     destructor Done; virtual;
  75.     constructor Load(var S: TStream);
  76.     procedure Store(var S: TStream);
  77.     function CanClear: Boolean; virtual;
  78.     function CanClose: Boolean; virtual;
  79.     procedure NewFile;
  80.     procedure Open;
  81.     procedure Read;
  82.     procedure SetFileName(AFileName: PChar);
  83.     procedure ReplaceWith(AFileName: PChar);
  84.     function Save: Boolean;
  85.     function SaveAs: Boolean;
  86.     procedure SetupWindow; virtual;
  87.     procedure Write;
  88.     procedure CMFileNew(var Msg: TMessage);
  89.       virtual cm_First + cm_FileNew;
  90.     procedure CMFileOpen(var Msg: TMessage);
  91.       virtual cm_First + cm_FileOpen;
  92.     procedure CMFileSave(var Msg: TMessage);
  93.       virtual cm_First + cm_FileSave;
  94.     procedure CMFileSaveAs(var Msg: TMessage);
  95.       virtual cm_First + cm_FileSaveAs;
  96.   end;
  97.  
  98. const
  99.   REditWindow: TStreamRec = (
  100.     ObjType: 80;
  101.     VmtLink: Ofs(TypeOf(TEditWindow)^);
  102.     Load:    @TEditWindow.Load;
  103.     Store:   @TEditWindow.Store);
  104.  
  105. const
  106.   RFileWindow: TStreamRec = (
  107.     ObjType: 81;
  108.     VmtLink: Ofs(TypeOf(TFileWindow)^);
  109.     Load:    @TFileWindow.Load;
  110.     Store:   @TFileWindow.Store);
  111.  
  112. procedure RegisterStdWnds;
  113.  
  114. implementation
  115.  
  116. { TSearchDialog }
  117.  
  118. const
  119.   sd_Search          = MakeIntResource($7F10);
  120.   sd_Replace         = MakeIntResource($7F11);
  121.   id_SearchText      = 100;
  122.   id_CaseSensitive   = 101;
  123.   id_ReplaceText     = 102;
  124.   id_ReplaceAll      = 103;
  125.   id_PromptOnReplace = 104;
  126.  
  127. type
  128.   PSearchDialog = ^TSearchDialog;
  129.   TSearchDialog = object(TDialog)
  130.     constructor Init(AParent: PWindowsObject; Template: PChar;
  131.       var SearchRec: TSearchRec);
  132.   end;
  133.  
  134. constructor TSearchDialog.Init(AParent: PWindowsObject; Template: PChar;
  135.   var SearchRec: TSearchRec);
  136. var
  137.   C: PWindowsObject;
  138. begin
  139.   TDialog.Init(AParent, Template);
  140.   C := New(PEdit, InitResource(@Self, id_SearchText,
  141.     SizeOf(SearchRec.SearchText)));
  142.   C := New(PCheckBox, InitResource(@Self, id_CaseSensitive));
  143.   if Template = sd_Replace then
  144.   begin
  145.     C := New(PEdit, InitResource(@Self, id_ReplaceText,
  146.       SizeOf(SearchRec.ReplaceText)));
  147.     C := New(PCheckBox, InitResource(@Self, id_ReplaceAll));
  148.     C := New(PCheckBox, InitResource(@Self, id_PromptOnReplace));
  149.   end;
  150.   TransferBuffer := @SearchRec;
  151. end;
  152.  
  153. { TEditWindow }
  154.  
  155. { Constructor for a TEditWindow.  Initializes its data fields using passed
  156.   parameters and default values.  Constructs its child edit control. }
  157. constructor TEditWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  158. begin
  159.   TWindow.Init(AParent, ATitle);
  160.   Editor := New(PEdit, Init(@Self, 200, nil, 0, 0, 0, 0, 0, True));
  161.   with Editor^.Attr do
  162.     Style := Style or es_NoHideSel;
  163.   FillChar(SearchRec, SizeOf(SearchRec), #0);
  164. end;
  165.  
  166. { Load a TEditWindow from the given stream }
  167. constructor TEditWindow.Load(var S: TStream);
  168. begin
  169.   TWindow.Load(S);
  170.   GetChildPtr(S, Editor);
  171. end;
  172.  
  173. { Store a TEditWindow to the given stream }
  174. procedure TEditWindow.Store(var S: TStream);
  175. begin
  176.   TWindow.Store(S);
  177.   PutChildPtr(S, Editor);
  178. end;
  179.  
  180. { Responds to an incoming wm_Size message by resizing the child edit
  181.   control according to the size of the TEditWindow's client area. }
  182. procedure TEditWindow.WMSize(var Msg: TMessage);
  183. begin
  184.   TWindow.WMSize(Msg);
  185.   SetWindowPos(Editor^.HWindow, 0, -1, -1, Msg.LParamLo+2, Msg.LParamHi+2,
  186.     swp_NoZOrder);
  187. end;
  188.  
  189. { Responds to an incoming wm_SetFocus message by setting the focus to the
  190.   child edit control. }
  191. procedure TEditWindow.WMSetFocus(var Msg: TMessage);
  192. begin
  193.   SetFocus(Editor^.HWindow);
  194. end;
  195.  
  196. procedure TEditWindow.DoSearch;
  197. var
  198.   S: array[0..80] of Char;
  199.   P: Pointer;
  200.   Rslt: Integer;
  201. begin
  202.   Rslt := 0;
  203.   with SearchRec do
  204.     repeat
  205.       Rslt := Editor^.Search(-1, SearchText, CaseSensitive);
  206.       if Rslt = -1 then
  207.       begin
  208.         if not IsReplace or not ReplaceAll then
  209.         begin
  210.           P := @SearchText;
  211.           WVSPrintF(S, '"%0.60s" not found.', P);
  212. {$ifdef BWCC}
  213.           MessageBox(HWindow, S, 'Find error', mb_OK + mb_IconExclamation);
  214. {$else}
  215.           MessageBox(HWindow, S, 'Find error', mb_OK + mb_IconExclamation);
  216. {$endif}
  217.         end;
  218.       end
  219.       else
  220.         if IsReplace then
  221.           if not PromptOnReplace then Editor^.Insert(ReplaceText)
  222.           else
  223.           begin
  224. {$ifdef BWCC}
  225.             Rslt := BWCCMessageBox(HWindow, 'Replace this occurrence?',
  226.               'Search/Replace', mb_YesNoCancel + mb_IconQuestion);
  227. {$else}
  228.             Rslt := MessageBox(HWindow, 'Replace this occurrence?',
  229.               'Search/Replace', mb_YesNoCancel + mb_IconQuestion);
  230. {$endif}
  231.             if Rslt = id_Yes then Editor^.Insert(ReplaceText)
  232.             else if Rslt = id_Cancel then Exit;
  233.           end;
  234.     until (Rslt = -1) or not ReplaceAll or not IsReplace;
  235. end;
  236.  
  237. procedure TEditWindow.CMEditFind(var Msg: TMessage);
  238. begin
  239.   if Application^.ExecDialog(New(PSearchDialog, Init(@Self,
  240.     sd_Search, SearchRec))) = id_OK then
  241.   begin
  242.     SearchRec.IsReplace := False;
  243.     DoSearch;
  244.   end;
  245. end;
  246.  
  247. procedure TEditWindow.CMEditFindNext(var Msg: TMessage);
  248. begin
  249.   DoSearch;
  250. end;
  251.  
  252. procedure TEditWindow.CMEditReplace(var Msg: TMessage);
  253. begin
  254.   if Application^.ExecDialog(New(PSearchDialog, Init(@Self,
  255.     sd_Replace, SearchRec))) = id_OK then
  256.   begin
  257.     SearchRec.IsReplace := True;
  258.     DoSearch;
  259.   end;
  260. end;
  261.  
  262. { TFileWindow }
  263.  
  264. { Constructor for a TFileWindow.  Initializes its data fields using
  265.   passed parameters and default values. }
  266. constructor TFileWindow.Init(AParent: PWindowsObject; ATitle,
  267.   AFileName: PChar);
  268. begin
  269.   TEditWindow.Init(AParent, ATitle);
  270.   IsNewFile := True;
  271.   FileName := StrNew(AFileName);
  272. end;
  273.  
  274. { Dispose of the file name }
  275. destructor TFileWindow.Done;
  276. begin
  277.   StrDispose(FileName);
  278.   TEditWindow.Done;
  279. end;
  280.  
  281. { Load a TFileWindow from the stream }
  282. constructor TFileWindow.Load(var S: TStream);
  283. begin
  284.   TEditWindow.Load(S);
  285.   FileName := S.StrRead;
  286.   IsNewFile := FileName = nil;
  287. end;
  288.  
  289. { Store a TFileWindow from the stream }
  290. procedure TFileWindow.Store(var S: TStream);
  291. begin
  292.   TEditWindow.Store(S);
  293.   S.StrWrite(FileName);
  294. end;
  295.  
  296. { Performs setup for a TFileWindow, appending 'Untitled' to its caption }
  297. procedure TFileWindow.SetupWindow;
  298. begin
  299.   TEditWindow.SetupWindow;
  300.   SetFileName(FileName);
  301.   if FileName <> nil then Read;
  302. end;
  303.  
  304. { Sets the file name of the window and updates the caption.  Assumes
  305.   that the AFileName parameter and the FileName instance variable were
  306.   allocated by StrNew. }
  307. procedure TFileWindow.SetFileName(AFileName: PChar);
  308. var
  309.   NewCaption: array[0..80] of Char;
  310.   P: array[0..1] of PChar;
  311. begin
  312.   if FileName <> AFileName then
  313.   begin
  314.     StrDispose(FileName);
  315.     FileName := StrNew(AFileName);
  316.   end;
  317.   P[0] := Attr.Title;
  318.   if FileName = nil then P[1] := '(Untitled)'
  319.   else P[1] := AFileName;
  320.   if Attr.Title = nil then SetWindowText(HWindow, P[1])
  321.   else
  322.   begin
  323.     WVSPrintF(NewCaption, '%0.40s - %0.37s', P[0]);
  324.     SetWindowText(HWindow, NewCaption);
  325.   end;
  326. end;
  327.  
  328. { Begins the edit of a new file, after determining that it is Ok to
  329.   clear the TEdit's text. }
  330. procedure TFileWindow.NewFile;
  331. begin
  332.   if CanClear then
  333.   begin
  334.     Editor^.Clear;
  335.     InvalidateRect(Editor^.HWindow, nil, False);
  336.     Editor^.ClearModify;
  337.     IsNewFile := True;
  338.     SetFileName(nil);
  339.   end;
  340. end;
  341.  
  342. { Replaces the current file with the given file. }
  343. procedure TFileWindow.ReplaceWith(AFileName: PChar);
  344. begin
  345.   SetFileName(AFileName);
  346.   Read;
  347.   InvalidateRect(Editor^.HWindow, nil, False);
  348. end;
  349.  
  350. { Brings up a dialog allowing the user to open a file into this
  351.   window.  Save as selecting File|Open from the menus. }
  352. procedure TFileWindow.Open;
  353. var
  354.   TmpName: array[0..fsPathName] of Char;
  355. begin
  356.   if CanClear and (Application^.ExecDialog(New(PFileDialog,
  357.      Init(@Self, PChar(sd_FileOpen), StrCopy(TmpName, '*.*')))) = id_Ok) then
  358.     ReplaceWith(TmpName);
  359. end;
  360.  
  361. { Reads the contents of a previously-specified file into the TEdit
  362.   child control. }
  363. procedure TFileWindow.Read;
  364. const
  365.   BufferSize = 1024;
  366. var
  367.   CharsToRead: LongInt;
  368.   BlockSize: Integer;
  369.   AStream: PDosStream;
  370.   ABuffer: PChar;
  371. begin
  372.   AStream := New(PDosStream, Init(FileName, stOpen));
  373.   ABuffer := MemAlloc(BufferSize + 1);
  374.   CharsToRead := AStream^.GetSize;
  375.   if ABuffer <> nil then
  376.   begin
  377.     Editor^.Clear;
  378.     while CharsToRead > 0 do
  379.     begin
  380.       if CharsToRead > BufferSize then
  381.         BlockSize := BufferSize
  382.       else BlockSize := CharsToRead;
  383.       AStream^.Read(ABuffer^, BlockSize);
  384.       ABuffer[BlockSize] := Char(0);
  385.       Editor^.Insert(ABuffer);
  386.       CharsToRead := CharsToRead - BlockSize;
  387.     end;
  388.     IsNewFile := False;
  389.     Editor^.ClearModify;
  390.     Editor^.SetSelection(0, 0);
  391.     FreeMem(ABuffer, BufferSize + 1);
  392.   end;
  393.   Dispose(AStream, Done);
  394. end;
  395.  
  396. { Saves the contents of the TEdit child control into the file currently
  397.   being editted.  Returns true if the file was saved. }
  398. function TFileWindow.Save: Boolean;
  399. begin
  400.   Save := True;
  401.   if Editor^.IsModified then
  402.     if IsNewFile then Save := SaveAs
  403.     else Write;
  404. end;
  405.  
  406. { Saves the contents of the TEdit child control into a file whose name
  407.   is retrieved from the user, through execution of a "Save" file
  408.   dialog.  Returns true if the file was saved. }
  409. function TFileWindow.SaveAs: Boolean;
  410. var
  411.   TmpName: array[0..fsPathName] of Char;
  412. begin
  413.   SaveAs := False;
  414.   if FileName <> nil then StrCopy(TmpName, FileName)
  415.   else TmpName[0] := #0;
  416.   if Application^.ExecDialog(New(PFileDialog,
  417.       Init(@Self, PChar(sd_FileSave), TmpName))) = id_Ok then
  418.   begin
  419.     SetFileName(TmpName);
  420.     Write;
  421.     SaveAs := True;
  422.   end;
  423. end;
  424.  
  425. { Writes the contents of the TEdit child control to a previously-specified
  426.   file.  If the operation will cause truncation of the text, first confirms
  427.   (through displaying a message box) that it is OK to proceed. }
  428. procedure TFileWindow.Write;
  429. const
  430.   BufferSize = 1024;
  431. var
  432.   CharsToWrite, CharsWritten: LongInt;
  433.   BlockSize: Integer;
  434.   AStream: PDosStream;
  435.   ABuffer: pointer;
  436.   NumLines: Integer;
  437. begin
  438.   NumLines := Editor^.GetNumLines;
  439.   CharsToWrite := Editor^.GetLineIndex(NumLines-1) +
  440.     Editor^.GetLineLength(NumLines-1);
  441.   AStream := New(PDosStream, Init(FileName, stCreate));
  442.   ABuffer := MemAlloc(BufferSize + 1);
  443.   CharsWritten := 0;
  444.   if ABuffer <> nil then
  445.   begin
  446.     while CharsWritten < CharsToWrite do
  447.     begin
  448.       if CharsToWrite - CharsWritten > BufferSize then
  449.         BlockSize := BufferSize
  450.       else BlockSize := CharsToWrite - CharsWritten;
  451.       Editor^.GetSubText(ABuffer, CharsWritten, CharsWritten + BlockSize);
  452.       AStream^.Write(ABuffer^, BlockSize);
  453.       CharsWritten := CharsWritten + BlockSize;
  454.     end;
  455.     IsNewFile := False;
  456.     Editor^.ClearModify;
  457.     FreeMem(ABuffer, BufferSize + 1);
  458.   end;
  459.   Dispose(AStream, Done);
  460. end;
  461.  
  462. { Returns a Boolean value indicating whether or not it is Ok to clear
  463.   the TEdit's text.  Returns True if the text has not been changed, or
  464.   if the user Oks the clearing of the text. }
  465. function TFileWindow.CanClear: Boolean;
  466. var
  467.   S: array[0..fsPathName+27] of Char;
  468.   P: PChar;
  469.   Rslt: Integer;
  470. begin
  471.   CanClear := True;
  472.   if Editor^.IsModified then
  473.   begin
  474.     if FileName = nil then StrCopy(S, 'Untitled file has changed. Save?')
  475.     else
  476.     begin
  477.       P := FileName;
  478.       WVSPrintF(S, 'File "%s" has changed.  Save?', P);
  479.     end;
  480. {$ifdef BWCC}
  481.     Rslt := BWCCMessageBox(HWindow, S, 'File Changed', mb_YesNoCancel or
  482.       mb_IconQuestion);
  483. {$else}
  484.     Rslt := MessageBox(HWindow, S, 'File Changed', mb_YesNoCancel or
  485.       mb_IconQuestion);
  486. {$endif}
  487.     if Rslt = id_Yes then CanClear := Save
  488.     else CanClear := Rslt <> id_Cancel;
  489.   end;
  490. end;
  491.  
  492. { Returns a Boolean value indicating whether or not it is Ok to close
  493.   the TEdit's text.  Returns the result of a call to Self.CanClear. }
  494. function TFileWindow.CanClose: Boolean;
  495. begin
  496.   CanClose := CanClear;
  497. end;
  498.  
  499. { Responds to an incoming "New" command (with a cm_FileNew command
  500.   identifier) by calling Self.New. }
  501. procedure TFileWindow.CMFileNew(var Msg: TMessage);
  502. begin
  503.   NewFile;
  504. end;
  505.  
  506. { Responds to an incoming "Open" command (with a cm_FileOpen command
  507.   identifier) by calling Self.Open. }
  508. procedure TFileWindow.CMFileOpen(var Msg: TMessage);
  509. begin
  510.   Open;
  511. end;
  512.  
  513. { Responds to an incoming "Save" command (with a cm_FileSave command
  514.   identifier) by calling Self.Save. }
  515. procedure TFileWindow.CMFileSave(var Msg: TMessage);
  516. begin
  517.   Save;
  518. end;
  519.  
  520. { Responds to an incoming "SaveAs" command (with a cm_FileSaveAs command
  521.   identifier) by calling Self.SaveAs. }
  522. procedure TFileWindow.CMFileSaveAs(var Msg: TMessage);
  523. begin
  524.   SaveAs;
  525. end;
  526.  
  527. procedure RegisterStdWnds;
  528. begin
  529.   RegisterType(REditWindow);
  530.   RegisterType(RFileWindow);
  531. end;
  532.  
  533. end.
  534.