Editor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "Editor.h"
  6. #include "WinInterface.h"
  7. #include "TextsWin.h"
  8. #include "Tools.h"
  9. #include <ScpMain.h>
  10. #include "VCLCommon.h"
  11. #include "WinConfiguration.h"
  12. #include "HelpWin.h"
  13. #include <CommDlg.hpp>
  14. //---------------------------------------------------------------------------
  15. #pragma package(smart_init)
  16. #pragma link "TB2Dock"
  17. #pragma link "TBX"
  18. #pragma link "TB2Item"
  19. #pragma link "TB2Toolbar"
  20. #pragma link "TBXStatusBars"
  21. #pragma resource "*.dfm"
  22. //---------------------------------------------------------------------------
  23. TForm * __fastcall ShowEditorForm(const AnsiString FileName, TCustomForm * ParentForm,
  24. TNotifyEvent OnFileChanged, TNotifyEvent OnClose, const AnsiString Caption,
  25. bool ShowWindowButton)
  26. {
  27. TEditorForm * Dialog = new TEditorForm(Application);
  28. try
  29. {
  30. Dialog->ShowWindowButton = ShowWindowButton;
  31. Dialog->FileName = FileName;
  32. Dialog->ParentForm = ParentForm;
  33. Dialog->Caption = Caption.IsEmpty() ? FileName : Caption;
  34. Dialog->OnFileChanged = OnFileChanged;
  35. Dialog->OnWindowClose = OnClose;
  36. Dialog->Show();
  37. }
  38. catch(...)
  39. {
  40. delete Dialog;
  41. throw;
  42. }
  43. return Dialog;
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall ReconfigureEditorForm(TForm * Form)
  47. {
  48. TEditorForm * Editor = dynamic_cast<TEditorForm *>(Form);
  49. assert(Editor != NULL);
  50. Editor->ApplyConfiguration();
  51. }
  52. //---------------------------------------------------------------------------
  53. class TFindDialogEx : public TFindDialog
  54. {
  55. public:
  56. __fastcall virtual TFindDialogEx(TComponent * AOwner) : TFindDialog(AOwner)
  57. {
  58. FHelpMsg = RegisterWindowMessage(HELPMSGSTRING);
  59. }
  60. protected:
  61. unsigned int FHelpMsg;
  62. virtual bool __fastcall MessageHook(TMessage & Msg)
  63. {
  64. bool Result = false;
  65. if (Msg.Msg == FHelpMsg)
  66. {
  67. Application->HelpKeyword(HELP_EDITOR_FIND);
  68. Result = true;
  69. }
  70. if (!Result)
  71. {
  72. Result = TFindDialog::MessageHook(Msg);
  73. }
  74. return Result;
  75. }
  76. };
  77. //---------------------------------------------------------------------------
  78. class TReplaceDialogEx : public TReplaceDialog
  79. {
  80. public:
  81. __fastcall virtual TReplaceDialogEx(TComponent * AOwner) : TReplaceDialog(AOwner)
  82. {
  83. FHelpMsg = RegisterWindowMessage(HELPMSGSTRING);
  84. }
  85. protected:
  86. unsigned int FHelpMsg;
  87. virtual bool __fastcall MessageHook(TMessage & Msg)
  88. {
  89. bool Result = false;
  90. if (Msg.Msg == FHelpMsg)
  91. {
  92. Application->HelpKeyword(HELP_EDITOR_REPLACE);
  93. Result = true;
  94. }
  95. if (!Result)
  96. {
  97. Result = TReplaceDialog::MessageHook(Msg);
  98. }
  99. return Result;
  100. }
  101. };
  102. //---------------------------------------------------------------------------
  103. __fastcall TEditorForm::TEditorForm(TComponent* Owner)
  104. : TForm(Owner)
  105. {
  106. FParentForm = NULL;
  107. FCaretPos.x = -1;
  108. FCaretPos.y = -1;
  109. FLastFindDialog = NULL;
  110. FShowWindowButton = false;
  111. FCloseAnnounced = false;
  112. ApplyConfiguration();
  113. FFindDialog = new TFindDialogEx(this);
  114. FFindDialog->OnFind = FindDialogFind;
  115. FReplaceDialog = new TReplaceDialogEx(this);
  116. FReplaceDialog->OnFind = FindDialogFind;
  117. FReplaceDialog->OnReplace = FindDialogFind;
  118. UseSystemSettings(this);
  119. }
  120. //---------------------------------------------------------------------------
  121. __fastcall TEditorForm::~TEditorForm()
  122. {
  123. // see FormClose for explanation
  124. if (!FCloseAnnounced)
  125. {
  126. DoWindowClose();
  127. }
  128. }
  129. //---------------------------------------------------------------------------
  130. void __fastcall TEditorForm::SetFileName(const AnsiString value)
  131. {
  132. if (value != FFileName)
  133. {
  134. FFileName = value;
  135. if (Visible)
  136. {
  137. LoadFile();
  138. }
  139. }
  140. }
  141. //---------------------------------------------------------------------------
  142. void __fastcall TEditorForm::SetParentForm(TCustomForm * value)
  143. {
  144. if (FParentForm != value)
  145. {
  146. FParentForm = value;
  147. if (value)
  148. {
  149. BoundsRect = value->BoundsRect;
  150. }
  151. }
  152. }
  153. //---------------------------------------------------------------------------
  154. void __fastcall TEditorForm::EditorActionsUpdate(TBasicAction *Action,
  155. bool &Handled)
  156. {
  157. Handled = true;
  158. if (Action == SaveAction)
  159. {
  160. SaveAction->Enabled = EditorMemo->Modified;
  161. }
  162. else if (Action == FindNextAction)
  163. {
  164. FindNextAction->Enabled =
  165. FLastFindDialog != NULL || !FFindDialog->FindText.IsEmpty();
  166. }
  167. else if (Action == PreferencesAction || Action == CloseAction ||
  168. Action == FindAction || Action == ReplaceAction || Action == GoToLineAction ||
  169. Action == HelpAction)
  170. {
  171. ((TAction *)Action)->Enabled = true;
  172. }
  173. else
  174. {
  175. Handled = false;
  176. }
  177. }
  178. //---------------------------------------------------------------------------
  179. void __fastcall TEditorForm::EditorActionsExecute(TBasicAction *Action,
  180. bool &Handled)
  181. {
  182. Handled = true;
  183. if (Action == SaveAction)
  184. {
  185. assert(!FFileName.IsEmpty());
  186. EditorMemo->Lines->SaveToFile(FFileName);
  187. if (FOnFileChanged)
  188. {
  189. FOnFileChanged(this);
  190. }
  191. EditorMemo->Modified = false;
  192. UpdateControls();
  193. }
  194. else if (Action == PreferencesAction)
  195. {
  196. DoPreferencesDialog(pmEditor);
  197. }
  198. else if (Action == CloseAction)
  199. {
  200. Close();
  201. }
  202. else if (Action == FindAction || Action == ReplaceAction)
  203. {
  204. StartFind(Action == FindAction);
  205. }
  206. else if (Action == FindNextAction)
  207. {
  208. if (!FLastFindDialog)
  209. {
  210. FLastFindDialog = FFindDialog;
  211. }
  212. Find();
  213. }
  214. else if (Action == GoToLineAction)
  215. {
  216. GoToLine();
  217. }
  218. else if (Action == EditPaste)
  219. {
  220. // original source: http://home.att.net/~robertdunn/FAQs/Faqs.html
  221. // tell the Rich Edit control to insert unformatted text (CF_TEXT)
  222. REPASTESPECIAL RepasteSpecial = { 0, 0 };
  223. SendMessage(EditorMemo->Handle, EM_PASTESPECIAL, CF_TEXT,
  224. (LPARAM)&RepasteSpecial);
  225. }
  226. else if (Action == HelpAction)
  227. {
  228. FormHelp(this);
  229. }
  230. else
  231. {
  232. Handled = false;
  233. }
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall TEditorForm::FormCloseQuery(TObject * /*Sender*/,
  237. bool &CanClose)
  238. {
  239. if (EditorMemo->Modified)
  240. {
  241. int Answer = MessageDialog(LoadStr(SAVE_CHANGES), qtConfirmation,
  242. qaYes | qaNo | qaCancel);
  243. CanClose = (Answer != qaCancel);
  244. if (Answer == qaYes)
  245. {
  246. SaveAction->Execute();
  247. }
  248. }
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall TEditorForm::ApplyConfiguration()
  252. {
  253. bool PrevModified = EditorMemo->Modified;
  254. assert(Configuration);
  255. EditorMemo->Font->Name = WinConfiguration->Editor.FontName;
  256. EditorMemo->Font->Height = WinConfiguration->Editor.FontHeight;
  257. EditorMemo->Font->Charset = (TFontCharset)WinConfiguration->Editor.FontCharset;
  258. EditorMemo->Font->Style = IntToFontStyles(WinConfiguration->Editor.FontStyle);
  259. EditorMemo->DefAttributes->Assign(EditorMemo->Font);
  260. if (EditorMemo->WordWrap != WinConfiguration->Editor.WordWrap)
  261. {
  262. if (Visible)
  263. {
  264. TStrings * Content = new TStringList();
  265. try
  266. {
  267. EditorMemo->WordWrap = False;
  268. Content->Assign(EditorMemo->Lines);
  269. EditorMemo->WordWrap = WinConfiguration->Editor.WordWrap;
  270. EditorMemo->Lines = Content;
  271. EditorMemo->CaretPos = TPoint(0, 0);
  272. }
  273. __finally
  274. {
  275. delete Content;
  276. }
  277. }
  278. else
  279. {
  280. EditorMemo->WordWrap = WinConfiguration->Editor.WordWrap;
  281. }
  282. }
  283. EditorMemo->Modified = PrevModified;
  284. EditorMemo->ClearUndo();
  285. UpdateControls();
  286. }
  287. //---------------------------------------------------------------------------
  288. void __fastcall TEditorForm::UpdateControls()
  289. {
  290. TPoint ACaretPos = EditorMemo->CaretPos;
  291. if (ACaretPos.x != FCaretPos.x || ACaretPos.y != FCaretPos.y)
  292. {
  293. FCaretPos = ACaretPos;
  294. int Count = EditorMemo->Lines->Count;
  295. StatusBar->Panels->Items[0]->Caption = FMTLOAD(EDITOR_LINE_STATUS,
  296. ((int)FCaretPos.y+1, Count));
  297. StatusBar->Panels->Items[1]->Caption = FMTLOAD(EDITOR_COLUMN_STATUS,
  298. ((int)FCaretPos.x+1));
  299. AnsiString Character;
  300. if (FCaretPos.y >= 0 && FCaretPos.y < EditorMemo->Lines->Count)
  301. {
  302. AnsiString Line = EditorMemo->Lines->Strings[FCaretPos.y];
  303. if (FCaretPos.x+1 <= Line.Length())
  304. {
  305. Character = FMTLOAD(EDITOR_CHARACTER_STATUS,
  306. (int((unsigned char)Line[FCaretPos.x+1]), int((unsigned char)Line[FCaretPos.x+1])));
  307. }
  308. }
  309. StatusBar->Panels->Items[2]->Caption = Character;
  310. }
  311. StatusBar->Panels->Items[3]->Caption =
  312. (EditorMemo->Modified ? LoadStr(EDITOR_MODIFIED) : AnsiString(""));
  313. EditorActions->UpdateAction(SaveAction);
  314. }
  315. //---------------------------------------------------------------------------
  316. void __fastcall TEditorForm::EditorMemoMouseUp(TObject * /*Sender*/,
  317. TMouseButton /*Button*/, TShiftState /*Shift*/, int /*X*/, int /*Y*/)
  318. {
  319. UpdateControls();
  320. }
  321. //---------------------------------------------------------------------------
  322. void __fastcall TEditorForm::EditorMemoKeyUp(TObject * /*Sender*/,
  323. WORD & /*Key*/, TShiftState /*Shift*/)
  324. {
  325. UpdateControls();
  326. }
  327. //---------------------------------------------------------------------------
  328. void __fastcall TEditorForm::EditorMemoChange(TObject * /*Sender*/)
  329. {
  330. UpdateControls();
  331. }
  332. //---------------------------------------------------------------------------
  333. void __fastcall TEditorForm::FindDialogFind(TObject * /*Sender*/)
  334. {
  335. Find();
  336. }
  337. //---------------------------------------------------------------------------
  338. void __fastcall TEditorForm::Find()
  339. {
  340. int NewPos;
  341. int Replacements = 0;
  342. do
  343. {
  344. assert(FLastFindDialog);
  345. TSearchTypes SearchTypes;
  346. // length condition is there to improve performance when large
  347. // block is selected in editor
  348. if (FLastFindDialog == FReplaceDialog &&
  349. (FReplaceDialog->Options.Contains(frReplace) ||
  350. FReplaceDialog->Options.Contains(frReplaceAll)) &&
  351. FReplaceDialog->FindText.Length() == EditorMemo->SelLength &&
  352. AnsiSameText(FReplaceDialog->FindText, EditorMemo->SelText))
  353. {
  354. EditorMemo->SelText = FReplaceDialog->ReplaceText;
  355. Replacements++;
  356. }
  357. TEditorConfiguration EditorConfiguration = WinConfiguration->Editor;
  358. EditorConfiguration.FindText = FLastFindDialog->FindText;
  359. EditorConfiguration.ReplaceText = FReplaceDialog->ReplaceText;
  360. EditorConfiguration.FindMatchCase = FLastFindDialog->Options.Contains(frMatchCase);
  361. EditorConfiguration.FindWholeWord = FLastFindDialog->Options.Contains(frWholeWord);
  362. WinConfiguration->Editor = EditorConfiguration;
  363. if (EditorConfiguration.FindMatchCase)
  364. {
  365. SearchTypes << stMatchCase;
  366. }
  367. if (EditorConfiguration.FindWholeWord)
  368. {
  369. SearchTypes << stWholeWord;
  370. }
  371. NewPos = EditorMemo->FindText(EditorConfiguration.FindText,
  372. EditorMemo->SelLength ? EditorMemo->SelStart+1 : EditorMemo->SelStart,
  373. EditorMemo->Text.Length(), SearchTypes);
  374. if (NewPos >= 0)
  375. {
  376. EditorMemo->SelStart = NewPos;
  377. EditorMemo->SelLength = EditorConfiguration.FindText.Length();
  378. }
  379. if (FLastFindDialog->Handle)
  380. {
  381. PositionFindDialog(true);
  382. }
  383. if (NewPos < 0)
  384. {
  385. if ((Replacements == 0) || FReplaceDialog->Options.Contains(frReplaceAll))
  386. {
  387. // now Screen->ActiveForm can be NULL when other form was meanwhile
  388. // activated and then focus was returned back to "find" dialog
  389. // (non VCL form)
  390. if (Screen->ActiveForm != this)
  391. {
  392. SetFocus();
  393. FLastFindDialog->Execute();
  394. }
  395. if (Replacements == 0)
  396. {
  397. MessageDialog(FMTLOAD(EDITOR_FIND_END, (EditorConfiguration.FindText)), qtInformation, qaOK, HELP_NONE);
  398. }
  399. else if (FReplaceDialog->Options.Contains(frReplaceAll))
  400. {
  401. MessageDialog(FMTLOAD(EDITOR_REPLACE_END, (Replacements)), qtInformation, qaOK, HELP_NONE);
  402. }
  403. }
  404. }
  405. }
  406. while (NewPos >= 0 && FLastFindDialog == FReplaceDialog &&
  407. FReplaceDialog->Options.Contains(frReplaceAll));
  408. }
  409. //---------------------------------------------------------------------------
  410. void __fastcall TEditorForm::FormShow(TObject * /*Sender*/)
  411. {
  412. LoadFile();
  413. CutFormToDesktop(this);
  414. }
  415. //---------------------------------------------------------------------------
  416. void __fastcall TEditorForm::LoadFile()
  417. {
  418. EditorMemo->Lines->LoadFromFile(FFileName);
  419. EditorMemo->Modified = false;
  420. FCaretPos.x = -1;
  421. ApplyConfiguration();
  422. }
  423. //---------------------------------------------------------------------------
  424. bool __fastcall TEditorForm::CursorInUpperPart()
  425. {
  426. HFONT OldFont;
  427. void *DC;
  428. TTextMetric TM;
  429. TRect Rect;
  430. DC = GetDC(EditorMemo->Handle);
  431. OldFont = SelectObject(DC, EditorMemo->Font->Handle);
  432. try
  433. {
  434. GetTextMetrics(DC, &TM);
  435. EditorMemo->Perform(EM_GETRECT, 0, ((int)&Rect));
  436. }
  437. __finally
  438. {
  439. SelectObject(DC, OldFont);
  440. ReleaseDC(EditorMemo->Handle, DC);
  441. }
  442. int VisibleLines = (Rect.Bottom - Rect.Top) / (TM.tmHeight + TM.tmExternalLeading);
  443. int FirstLine = SendMessage(EditorMemo->Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
  444. TPoint CaretPos = EditorMemo->CaretPos;
  445. return (CaretPos.y - FirstLine) < VisibleLines / 2;
  446. }
  447. //---------------------------------------------------------------------------
  448. void __fastcall TEditorForm::PositionFindDialog(bool VerticalOnly)
  449. {
  450. assert(FLastFindDialog);
  451. if (!VerticalOnly)
  452. {
  453. FLastFindDialog->Left = Left + EditorMemo->Left + EditorMemo->Width / 2 - 100;
  454. }
  455. FLastFindDialog->Top = Top + EditorMemo->Top + (EditorMemo->Height / 4) +
  456. (CursorInUpperPart() ? (EditorMemo->Height / 2) : 0) - 40;
  457. }
  458. //---------------------------------------------------------------------------
  459. void __fastcall TEditorForm::StartFind(bool Find)
  460. {
  461. AnsiString Text = EditorMemo->SelText;
  462. TFindOptions Options;
  463. Options << frHideUpDown; // not implemented
  464. Options << frShowHelp;
  465. if (Text.IsEmpty())
  466. {
  467. Text = WinConfiguration->Editor.FindText;
  468. }
  469. TFindDialog * Dialog = Find ? FFindDialog : FReplaceDialog;
  470. if (FLastFindDialog && Dialog != FLastFindDialog && FLastFindDialog->Handle)
  471. {
  472. FLastFindDialog->CloseDialog();
  473. }
  474. FLastFindDialog = Dialog;
  475. if (!Text.IsEmpty())
  476. {
  477. FLastFindDialog->FindText = Text;
  478. }
  479. FReplaceDialog->ReplaceText = WinConfiguration->Editor.ReplaceText;
  480. if (WinConfiguration->Editor.FindMatchCase)
  481. {
  482. Options << frMatchCase;
  483. }
  484. if (WinConfiguration->Editor.FindWholeWord)
  485. {
  486. Options << frWholeWord;
  487. }
  488. FLastFindDialog->Options = Options;
  489. if (!FLastFindDialog->Handle)
  490. {
  491. PositionFindDialog(false);
  492. }
  493. FLastFindDialog->Execute();
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TEditorForm::GoToLine()
  497. {
  498. AnsiString Str;
  499. if (InputDialog(LoadStr(EDITOR_GO_TO_LINE), LoadStr(EDITOR_LINE_NUMBER), Str))
  500. {
  501. int Line = StrToIntDef(Str, -1);
  502. if (Line <= 0 || Line > EditorMemo->Lines->Count)
  503. {
  504. throw Exception(LoadStr(EDITOR_INVALID_LINE));
  505. }
  506. else
  507. {
  508. EditorMemo->CaretPos = TPoint(0, Line-1);
  509. }
  510. }
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall TEditorForm::FormClose(TObject * /*Sender*/,
  514. TCloseAction & Action)
  515. {
  516. // Preferably announce closure here as this is called from within TForm::Close(),
  517. // so the annoucement will be synchronous (and editor manager thus
  518. // will consider the form to be really closed and will not block
  519. // application closure).
  520. // However FormClose is not called when form is closed due to
  521. // application exit, so there is last resort call from destructor.
  522. DoWindowClose();
  523. FCloseAnnounced = true;
  524. Action = caFree;
  525. }
  526. //---------------------------------------------------------------------------
  527. void __fastcall TEditorForm::DoWindowClose()
  528. {
  529. if (FOnWindowClose != NULL)
  530. {
  531. try
  532. {
  533. FOnWindowClose(this);
  534. }
  535. catch(Exception & E)
  536. {
  537. ShowExtendedException(&E);
  538. }
  539. }
  540. }
  541. //---------------------------------------------------------------------------
  542. void __fastcall TEditorForm::SetShowWindowButton(bool value)
  543. {
  544. if (value != ShowWindowButton)
  545. {
  546. FShowWindowButton = value;
  547. RecreateWnd();
  548. }
  549. }
  550. //---------------------------------------------------------------------------
  551. void __fastcall TEditorForm::CreateParams(TCreateParams & Params)
  552. {
  553. TForm::CreateParams(Params);
  554. if (ShowWindowButton)
  555. {
  556. Params.WndParent = GetDesktopWindow();
  557. }
  558. }
  559. //---------------------------------------------------------------------------