Editor.cpp 17 KB

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