demoedit.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. program DemoEditor;
  2. {$mode fpc}
  3. uses
  4. {$ifdef UNIX}cwstring,{$endif}Objects,fvconsts,
  5. //Drivers, Views, Menus, StdDlg, App, Editors,Msgbox{$ifdef unix},fvclip { OSC 52 support unit } {$endif},FVCommon;
  6. uDrivers, uViews, uMenus, uStdDlg, uApp, uEditors,uMsgbox{$ifdef unix},ufvclip { OSC 52 support unit } {$endif},uFVCommon;
  7. const
  8. cmShowClip = 102;
  9. cmCopyWin = 240;
  10. cmPasteWin = 241;
  11. type
  12. PEditorApp = ^TEditorApp;
  13. TEditorApp = object(TApplication)
  14. constructor Init;
  15. procedure HandleEvent(var Event: TEvent); virtual;
  16. procedure InitMenuBar; virtual;
  17. procedure InitStatusLine; virtual;
  18. end;
  19. PMyEditWindow = ^TMyEditWindow;
  20. TMyEditWindow = object(TEditWindow)
  21. procedure HandleEvent(var Event: TEvent); virtual;
  22. end;
  23. var
  24. EditorApp: TEditorApp;
  25. ClipWindow: PEditWindow;
  26. function OpenEditor(FileName: FNameStr; Visible: Boolean): PEditWindow;
  27. var
  28. P: PWindow;
  29. R: TRect;
  30. begin
  31. DeskTop^.GetExtent(R);
  32. P := New(PMyEditWindow, Init(R, FileName, wnNoNumber));
  33. if not Visible then P^.Hide;
  34. OpenEditor := PEditWindow(Application^.InsertWindow(P));
  35. end;
  36. procedure TMyEditWindow.HandleEvent(var Event: TEvent);
  37. procedure ClipCopyWin;
  38. var p : pointer;
  39. begin
  40. {$ifdef unix}
  41. if Editor^.SelStart<>Editor^.SelEnd then { Text selected? }
  42. begin
  43. {This is where the magic happens. Parameters are PAnsiChar and Length of data to be copied to global clipboard}
  44. SetGlobalClipboardData( @Editor^.Buffer^[Editor^.BufPtr(Editor^.SelStart)], Editor^.SelEnd - Editor^.SelStart);
  45. end;
  46. {$else}
  47. MessageBox('Not implemented for this platform!', nil, mfInformation + mfOkButton);
  48. {$endif}
  49. end;
  50. procedure ClipPasteWin;
  51. begin
  52. {$ifdef unix}
  53. GetGlobalClipboardData; {Request data from global Clipboard. That's it}
  54. {$else}
  55. MessageBox('Not implemented for this platform!', nil, mfInformation + mfOkButton);
  56. {$endif}
  57. end;
  58. begin
  59. inherited HandleEvent(Event);
  60. case Event.What of
  61. evCommand:
  62. case Event.Command of
  63. cmCopyWin : ClipCopyWin;
  64. cmPasteWin : ClipPasteWin;
  65. else
  66. Exit;
  67. end;
  68. else
  69. Exit;
  70. end;
  71. ClearEvent(Event);
  72. end;
  73. constructor TEditorApp.Init;
  74. var
  75. H: Word;
  76. R: TRect;
  77. begin
  78. inherited Init;
  79. DisableCommands([cmSave, cmSaveAs, cmCut, cmCopy, cmPaste,
  80. {cmCopyWin, cmPasteWin,}
  81. cmClear, cmUndo, cmFind, cmReplace, cmSearchAgain]);
  82. EditorDialog := @StdEditorDialog;
  83. ClipWindow := OpenEditor('', False);
  84. if ClipWindow <> nil then
  85. begin
  86. Clipboard := ClipWindow^.Editor;
  87. Clipboard^.CanUndo := False;
  88. end;
  89. end;
  90. procedure TEditorApp.HandleEvent(var Event: TEvent);
  91. procedure FileOpen;
  92. var
  93. FileName: FNameStr;
  94. begin
  95. FileName := '*.*';
  96. if ExecuteDialog(New(PFileDialog, Init('*.*', 'Open file',
  97. '~N~ame', fdOpenButton, 100)), @FileName) <> cmCancel then
  98. OpenEditor(FileName, True);
  99. end;
  100. procedure FileNew;
  101. begin
  102. OpenEditor('', True);
  103. end;
  104. procedure ChangeDir;
  105. begin
  106. ExecuteDialog(New(PChDirDialog, Init(cdNormal, 0)), nil);
  107. end;
  108. procedure ShowClip;
  109. begin
  110. ClipWindow^.Select;
  111. ClipWindow^.Show;
  112. end;
  113. begin
  114. inherited HandleEvent(Event);
  115. case Event.What of
  116. evCommand:
  117. case Event.Command of
  118. cmOpen: FileOpen;
  119. cmNew: FileNew;
  120. cmChangeDir : ChangeDir;
  121. cmShowClip : ShowClip;
  122. else
  123. Exit;
  124. end;
  125. else
  126. Exit;
  127. end;
  128. ClearEvent(Event);
  129. end;
  130. procedure TEditorApp.InitMenuBar;
  131. var
  132. R: TRect;
  133. begin
  134. GetExtent(R);
  135. R.B.Y := R.A.Y + 1;
  136. MenuBar := New(PMenuBar, Init(R, NewMenu(
  137. NewSubMenu('~F~ile', hcNoContext, NewMenu(
  138. StdFileMenuItems(
  139. nil)),
  140. NewSubMenu('~E~dit', hcNoContext, NewMenu(
  141. StdEditMenuItems(
  142. NewLine(
  143. NewItem('~S~hwo clipboard', '', kbNoKey, cmShowClip, hcNoContext,
  144. NewLine(
  145. NewItem('Cop~y~ to Windows', '', kbNoKey, cmCopyWin, hcNoContext,
  146. NewItem('Paste from ~W~indows', '', kbNoKey, cmPasteWin, hcNoContext,
  147. nil))))))),
  148. NewSubMenu('~S~earch', hcNoContext, NewMenu(
  149. NewItem('~F~ind...', '', kbNoKey, cmFind, hcNoContext,
  150. NewItem('~R~eplace...', '', kbNoKey, cmReplace, hcNoContext,
  151. NewItem('~S~earch again', '', kbNoKey, cmSearchAgain, hcNoContext,
  152. nil)))),
  153. NewSubMenu('~W~indows', hcNoContext, NewMenu(
  154. StdWindowMenuItems(
  155. nil)),
  156. nil)))))));
  157. end;
  158. procedure TEditorApp.InitStatusLine;
  159. var
  160. R: TRect;
  161. begin
  162. GetExtent(R);
  163. R.A.Y := R.B.Y - 1;
  164. New(StatusLine, Init(R,
  165. NewStatusDef(0, $FFFF,
  166. NewStatusKey('~F2~ Save', kbF2, cmSave,
  167. NewStatusKey('~F3~ Open', kbF3, cmOpen,
  168. NewStatusKey('~Alt-F3~ Close', kbAltF3, cmClose,
  169. NewStatusKey('~F5~ Zoom', kbF5, cmZoom,
  170. NewStatusKey('~F6~ Next', kbF6, cmNext,
  171. NewStatusKey('~F10~ Menu', kbF10, cmMenu,
  172. NewStatusKey('', kbCtrlF5, cmResize,
  173. nil))))))),
  174. nil)));
  175. end;
  176. begin
  177. EditorApp.Init;
  178. EditorApp.Run;
  179. EditorApp.Done;
  180. end.