Editor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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)
  26. {
  27. TEditorForm * Dialog = new TEditorForm(Application);
  28. try
  29. {
  30. Dialog->FileName = FileName;
  31. Dialog->ParentForm = ParentForm;
  32. Dialog->Caption = Caption.IsEmpty() ? FileName : Caption;
  33. Dialog->OnFileChanged = OnFileChanged;
  34. Dialog->OnFileReload = OnFileReload;
  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 TRichEdit20 : public TRichEdit
  54. {
  55. public:
  56. virtual __fastcall TRichEdit20(TComponent * AOwner);
  57. void __fastcall SetFormat(AnsiString FontName, int FontHeight,
  58. TFontCharset FontCharset, TFontStyles FontStyles, unsigned int TabSize,
  59. bool AWordWrap);
  60. int __fastcall FindText(const AnsiString SearchStr, int StartPos, int Length,
  61. TSearchTypes Options, bool Down);
  62. void __fastcall Redo();
  63. __property bool SupportsUpSearch = { read = FVersion20 };
  64. __property bool CanRedo = { read = GetCanRedo };
  65. protected:
  66. virtual void __fastcall CreateWnd(void);
  67. virtual void __fastcall CreateParams(TCreateParams & Params);
  68. virtual void __fastcall DestroyWnd();
  69. bool __fastcall GetCanRedo();
  70. void __fastcall ApplyTabSize();
  71. private:
  72. HINSTANCE FLibrary;
  73. bool FVersion20;
  74. bool FWordWrap;
  75. unsigned int FTabSize;
  76. };
  77. //---------------------------------------------------------------------------
  78. __fastcall TRichEdit20::TRichEdit20(TComponent * AOwner) :
  79. TRichEdit(AOwner),
  80. FLibrary(0),
  81. FVersion20(false),
  82. FTabSize(0),
  83. FWordWrap(true)
  84. {
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TRichEdit20::SetFormat(AnsiString FontName, int FontHeight,
  88. TFontCharset FontCharset, TFontStyles FontStyles, unsigned int TabSize,
  89. bool AWordWrap)
  90. {
  91. bool RecalculateTabs = (Font->Name != FontName) || (Font->Height != FontHeight) ||
  92. (TabSize != FTabSize);
  93. LockWindowUpdate(Handle);
  94. Font->Name = FontName;
  95. Font->Height = FontHeight;
  96. Font->Charset = FontCharset;
  97. Font->Style = FontStyles;
  98. DefAttributes->Assign(Font);
  99. FTabSize = TabSize;
  100. if (RecalculateTabs && HandleAllocated())
  101. {
  102. ApplyTabSize();
  103. }
  104. if (FWordWrap != AWordWrap)
  105. {
  106. if (Visible)
  107. {
  108. // Undocumented usage of EM_SETTARGETDEVICE.
  109. // But note that it is used by MFC in CRichEditView::WrapChanged()
  110. SendMessage(Handle, EM_SETTARGETDEVICE, 0, (AWordWrap ? 0 : 1));
  111. }
  112. else
  113. {
  114. WordWrap = AWordWrap;
  115. }
  116. FWordWrap = AWordWrap;
  117. }
  118. LockWindowUpdate(NULL);
  119. }
  120. //---------------------------------------------------------------------------
  121. int __fastcall TRichEdit20::FindText(const AnsiString SearchStr, int StartPos,
  122. int /*Length*/, TSearchTypes Options, bool Down)
  123. {
  124. int Result;
  125. if (FVersion20)
  126. {
  127. ::FINDTEXTEX Find;
  128. memset(&Find, 0, sizeof(Find));
  129. Find.chrg.cpMin = StartPos;
  130. Find.chrg.cpMax = -1;
  131. Find.lpstrText = SearchStr.c_str();
  132. unsigned int Flags =
  133. FLAGMASK(Options.Contains(stWholeWord), FT_WHOLEWORD) |
  134. FLAGMASK(Options.Contains(stMatchCase), FT_MATCHCASE) |
  135. FLAGMASK(Down, FR_DOWN);
  136. Result = SendMessage(Handle, EM_FINDTEXTEX, Flags, (LPARAM)&Find);
  137. }
  138. else
  139. {
  140. assert(Down);
  141. Result = TRichEdit::FindText(SearchStr, StartPos, Text.Length(), Options);
  142. }
  143. return Result;
  144. }
  145. //---------------------------------------------------------------------------
  146. void __fastcall TRichEdit20::Redo()
  147. {
  148. assert(FVersion20);
  149. SendMessage(Handle, EM_REDO, 0, 0);
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall TRichEdit20::CreateWnd(void)
  153. {
  154. TRichEdit::CreateWnd();
  155. ApplyTabSize();
  156. }
  157. //---------------------------------------------------------------------------
  158. void __fastcall TRichEdit20::CreateParams(TCreateParams & Params)
  159. {
  160. const char RichEditModuleName[] = "RICHED20.DLL";
  161. long int OldError;
  162. OldError = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  163. FLibrary = LoadLibrary(RichEditModuleName);
  164. SetErrorMode(OldError);
  165. FVersion20 = (FLibrary != 0);
  166. if (!FVersion20)
  167. {
  168. // fallback to richedit 1.0
  169. TRichEdit::CreateParams(Params);
  170. }
  171. else
  172. {
  173. TCustomMemo::CreateParams(Params);
  174. CreateSubClass(Params, RICHEDIT_CLASS);
  175. Params.Style = Params.Style |
  176. (HideScrollBars ? 0 : ES_DISABLENOSCROLL) |
  177. (HideSelection ? 0 : ES_NOHIDESEL);
  178. Params.WindowClass.style = Params.WindowClass.style &
  179. ~(CS_HREDRAW | CS_VREDRAW);
  180. }
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TRichEdit20::DestroyWnd()
  184. {
  185. TRichEdit::DestroyWnd();
  186. if (FLibrary != 0)
  187. {
  188. FreeLibrary(FLibrary);
  189. }
  190. }
  191. //---------------------------------------------------------------------------
  192. bool __fastcall TRichEdit20::GetCanRedo()
  193. {
  194. return FVersion20 && (SendMessage(Handle, EM_CANREDO, 0, 0) != 0);
  195. }
  196. //---------------------------------------------------------------------------
  197. void __fastcall TRichEdit20::ApplyTabSize()
  198. {
  199. if (FTabSize > 0)
  200. {
  201. HDC DC = GetDC(Handle);
  202. SaveDC(DC);
  203. SetMapMode(DC, MM_TEXT);
  204. SelectObject(DC, Font->Handle);
  205. int LogPixelsX = GetDeviceCaps(DC, LOGPIXELSX);
  206. SIZE Size;
  207. GetTextExtentPoint(DC, AnsiString::StringOfChar('X', FTabSize).c_str(),
  208. FTabSize, &Size);
  209. RestoreDC(DC, -1);
  210. ReleaseDC(Handle, DC);
  211. unsigned int TabTwips = MulDiv(Size.cx, 1440, LogPixelsX);
  212. // save selection
  213. CHARRANGE CharRange;
  214. SendMessage(Handle, EM_EXGETSEL, 0, (LPARAM)&CharRange);
  215. CHARRANGE CharRangeAll;
  216. CharRangeAll.cpMin = 0;
  217. CharRangeAll.cpMax = -1;
  218. SendMessage(Handle, EM_EXSETSEL, 0, (LPARAM)&CharRangeAll);
  219. PARAFORMAT2 ParaFormat;
  220. ParaFormat.cbSize = sizeof(ParaFormat);
  221. ParaFormat.dwMask = PFM_TABSTOPS;
  222. ParaFormat.cTabCount = MAX_TAB_STOPS;
  223. for (int i = 0; i < ParaFormat.cTabCount; i++)
  224. {
  225. ParaFormat.rgxTabs[i] = (i + 1) * TabTwips;
  226. }
  227. SendMessage(Handle, EM_SETPARAFORMAT, 0, (LPARAM)&ParaFormat);
  228. // restore selection
  229. SendMessage(Handle, EM_EXSETSEL, 0, (LPARAM)&CharRange);
  230. }
  231. }
  232. //---------------------------------------------------------------------------
  233. class TFindDialogEx : public TFindDialog
  234. {
  235. public:
  236. __fastcall virtual TFindDialogEx(TComponent * AOwner) : TFindDialog(AOwner)
  237. {
  238. FHelpMsg = RegisterWindowMessage(HELPMSGSTRING);
  239. }
  240. protected:
  241. unsigned int FHelpMsg;
  242. virtual bool __fastcall MessageHook(TMessage & Msg)
  243. {
  244. bool Result = false;
  245. if (Msg.Msg == FHelpMsg)
  246. {
  247. Application->HelpKeyword(HELP_EDITOR_FIND);
  248. Result = true;
  249. }
  250. if (!Result)
  251. {
  252. Result = TFindDialog::MessageHook(Msg);
  253. }
  254. return Result;
  255. }
  256. };
  257. //---------------------------------------------------------------------------
  258. class TReplaceDialogEx : public TReplaceDialog
  259. {
  260. public:
  261. __fastcall virtual TReplaceDialogEx(TComponent * AOwner) : TReplaceDialog(AOwner)
  262. {
  263. FHelpMsg = RegisterWindowMessage(HELPMSGSTRING);
  264. }
  265. protected:
  266. unsigned int FHelpMsg;
  267. virtual bool __fastcall MessageHook(TMessage & Msg)
  268. {
  269. bool Result = false;
  270. if (Msg.Msg == FHelpMsg)
  271. {
  272. Application->HelpKeyword(HELP_EDITOR_REPLACE);
  273. Result = true;
  274. }
  275. if (!Result)
  276. {
  277. Result = TReplaceDialog::MessageHook(Msg);
  278. }
  279. return Result;
  280. }
  281. };
  282. //---------------------------------------------------------------------------
  283. __fastcall TEditorForm::TEditorForm(TComponent* Owner)
  284. : TForm(Owner)
  285. {
  286. EditorMemo = new TRichEdit20(this);
  287. EditorMemo->Parent = this;
  288. EditorMemo->Align = alClient;
  289. EditorMemo->HideSelection = false;
  290. EditorMemo->PlainText = true;
  291. EditorMemo->PopupMenu = EditorPopup;
  292. EditorMemo->ScrollBars = ssBoth;
  293. EditorMemo->WantTabs = true;
  294. EditorMemo->OnChange = EditorMemoChange;
  295. EditorMemo->OnKeyUp = EditorMemoKeyUp;
  296. EditorMemo->OnMouseUp = EditorMemoMouseUp;
  297. FParentForm = NULL;
  298. FCaretPos.x = -1;
  299. FCaretPos.y = -1;
  300. FLastFindDialog = NULL;
  301. FCloseAnnounced = false;
  302. ApplyConfiguration();
  303. FFindDialog = new TFindDialogEx(this);
  304. FFindDialog->OnFind = FindDialogFind;
  305. FReplaceDialog = new TReplaceDialogEx(this);
  306. FReplaceDialog->OnFind = FindDialogFind;
  307. FReplaceDialog->OnReplace = FindDialogFind;
  308. UseSystemSettings(this);
  309. }
  310. //---------------------------------------------------------------------------
  311. __fastcall TEditorForm::~TEditorForm()
  312. {
  313. // see FormClose for explanation
  314. if (!FCloseAnnounced)
  315. {
  316. DoWindowClose();
  317. }
  318. }
  319. //---------------------------------------------------------------------------
  320. void __fastcall TEditorForm::SetFileName(const AnsiString value)
  321. {
  322. if (value != FFileName)
  323. {
  324. FFileName = value;
  325. if (Visible)
  326. {
  327. LoadFile();
  328. }
  329. }
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TEditorForm::SetParentForm(TCustomForm * value)
  333. {
  334. if (FParentForm != value)
  335. {
  336. FParentForm = value;
  337. if (value)
  338. {
  339. Width = value->BoundsRect.Width();
  340. Height = value->BoundsRect.Height();
  341. }
  342. }
  343. }
  344. //---------------------------------------------------------------------------
  345. void __fastcall TEditorForm::EditorActionsUpdate(TBasicAction *Action,
  346. bool &Handled)
  347. {
  348. Handled = true;
  349. if (Action == SaveAction)
  350. {
  351. SaveAction->Enabled = EditorMemo->Modified;
  352. }
  353. else if (Action == FindNextAction)
  354. {
  355. FindNextAction->Enabled =
  356. FLastFindDialog != NULL || !FFindDialog->FindText.IsEmpty();
  357. }
  358. else if (Action == EditRedo)
  359. {
  360. EditRedo->Enabled = EditorMemo->CanRedo;
  361. }
  362. else if (Action == PreferencesAction || Action == CloseAction ||
  363. Action == FindAction || Action == ReplaceAction || Action == GoToLineAction ||
  364. Action == HelpAction || Action == ReloadAction)
  365. {
  366. ((TAction *)Action)->Enabled = true;
  367. }
  368. else
  369. {
  370. Handled = false;
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TEditorForm::EditorActionsExecute(TBasicAction *Action,
  375. bool &Handled)
  376. {
  377. Handled = true;
  378. if (Action == SaveAction)
  379. {
  380. assert(!FFileName.IsEmpty());
  381. EditorMemo->Lines->SaveToFile(FFileName);
  382. if (FOnFileChanged)
  383. {
  384. FOnFileChanged(this);
  385. }
  386. EditorMemo->Modified = false;
  387. UpdateControls();
  388. }
  389. else if (Action == PreferencesAction)
  390. {
  391. DoPreferencesDialog(pmEditor);
  392. }
  393. else if (Action == CloseAction)
  394. {
  395. Close();
  396. }
  397. else if (Action == ReloadAction)
  398. {
  399. Reload();
  400. }
  401. else if (Action == FindAction || Action == ReplaceAction)
  402. {
  403. StartFind(Action == FindAction);
  404. }
  405. else if (Action == FindNextAction)
  406. {
  407. if (!FLastFindDialog)
  408. {
  409. FLastFindDialog = FFindDialog;
  410. }
  411. Find();
  412. }
  413. else if (Action == GoToLineAction)
  414. {
  415. GoToLine();
  416. }
  417. else if (Action == EditRedo)
  418. {
  419. EditorMemo->Redo();
  420. }
  421. else if (Action == EditPaste)
  422. {
  423. // original source: http://home.att.net/~robertdunn/FAQs/Faqs.html
  424. // tell the Rich Edit control to insert unformatted text (CF_TEXT)
  425. REPASTESPECIAL RepasteSpecial = { 0, 0 };
  426. SendMessage(EditorMemo->Handle, EM_PASTESPECIAL, CF_TEXT,
  427. (LPARAM)&RepasteSpecial);
  428. }
  429. else if (Action == HelpAction)
  430. {
  431. FormHelp(this);
  432. }
  433. else
  434. {
  435. Handled = false;
  436. }
  437. }
  438. //---------------------------------------------------------------------------
  439. void __fastcall TEditorForm::FormCloseQuery(TObject * /*Sender*/,
  440. bool &CanClose)
  441. {
  442. if (EditorMemo->Modified)
  443. {
  444. SetFocus();
  445. int Answer = MessageDialog(LoadStr(SAVE_CHANGES), qtConfirmation,
  446. qaYes | qaNo | qaCancel);
  447. CanClose = (Answer != qaCancel);
  448. if (Answer == qaYes)
  449. {
  450. SaveAction->Execute();
  451. }
  452. }
  453. }
  454. //---------------------------------------------------------------------------
  455. void __fastcall TEditorForm::ApplyConfiguration()
  456. {
  457. bool PrevModified = EditorMemo->Modified;
  458. assert(Configuration);
  459. EditorMemo->SetFormat(WinConfiguration->Editor.FontName,
  460. WinConfiguration->Editor.FontHeight, (TFontCharset)WinConfiguration->Editor.FontCharset,
  461. IntToFontStyles(WinConfiguration->Editor.FontStyle), WinConfiguration->Editor.TabSize,
  462. WinConfiguration->Editor.WordWrap);
  463. EditorMemo->Modified = PrevModified;
  464. EditorMemo->ClearUndo();
  465. UpdateControls();
  466. }
  467. //---------------------------------------------------------------------------
  468. void __fastcall TEditorForm::UpdateControls()
  469. {
  470. TPoint ACaretPos = EditorMemo->CaretPos;
  471. if (ACaretPos.x != FCaretPos.x || ACaretPos.y != FCaretPos.y)
  472. {
  473. FCaretPos = ACaretPos;
  474. int Count = EditorMemo->Lines->Count;
  475. StatusBar->Panels->Items[0]->Caption = FMTLOAD(EDITOR_LINE_STATUS,
  476. ((int)FCaretPos.y+1, Count));
  477. StatusBar->Panels->Items[1]->Caption = FMTLOAD(EDITOR_COLUMN_STATUS,
  478. ((int)FCaretPos.x+1));
  479. AnsiString Character;
  480. if (FCaretPos.y >= 0 && FCaretPos.y < EditorMemo->Lines->Count)
  481. {
  482. AnsiString Line = EditorMemo->Lines->Strings[FCaretPos.y];
  483. if (FCaretPos.x+1 <= Line.Length())
  484. {
  485. Character = FMTLOAD(EDITOR_CHARACTER_STATUS2,
  486. (int((unsigned char)Line[FCaretPos.x+1]), int((unsigned char)Line[FCaretPos.x+1])));
  487. }
  488. }
  489. StatusBar->Panels->Items[2]->Caption = Character;
  490. }
  491. StatusBar->Panels->Items[3]->Caption =
  492. (EditorMemo->Modified ? LoadStr(EDITOR_MODIFIED) : AnsiString(""));
  493. EditorActions->UpdateAction(SaveAction);
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TEditorForm::EditorMemoMouseUp(TObject * /*Sender*/,
  497. TMouseButton /*Button*/, TShiftState /*Shift*/, int /*X*/, int /*Y*/)
  498. {
  499. UpdateControls();
  500. }
  501. //---------------------------------------------------------------------------
  502. void __fastcall TEditorForm::EditorMemoKeyUp(TObject * /*Sender*/,
  503. WORD & /*Key*/, TShiftState /*Shift*/)
  504. {
  505. UpdateControls();
  506. }
  507. //---------------------------------------------------------------------------
  508. void __fastcall TEditorForm::EditorMemoChange(TObject * /*Sender*/)
  509. {
  510. UpdateControls();
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall TEditorForm::FindDialogFind(TObject * /*Sender*/)
  514. {
  515. Find();
  516. }
  517. //---------------------------------------------------------------------------
  518. void __fastcall TEditorForm::Find()
  519. {
  520. int NewPos;
  521. int Replacements = 0;
  522. do
  523. {
  524. assert(FLastFindDialog);
  525. TSearchTypes SearchTypes;
  526. // length condition is there to improve performance when large
  527. // block is selected in editor
  528. if (FLastFindDialog == FReplaceDialog &&
  529. (FReplaceDialog->Options.Contains(frReplace) ||
  530. FReplaceDialog->Options.Contains(frReplaceAll)) &&
  531. FReplaceDialog->FindText.Length() == EditorMemo->SelLength &&
  532. AnsiSameText(FReplaceDialog->FindText, EditorMemo->SelText))
  533. {
  534. EditorMemo->SelText = FReplaceDialog->ReplaceText;
  535. Replacements++;
  536. }
  537. TEditorConfiguration EditorConfiguration = WinConfiguration->Editor;
  538. EditorConfiguration.FindText = FLastFindDialog->FindText;
  539. EditorConfiguration.ReplaceText = FReplaceDialog->ReplaceText;
  540. EditorConfiguration.FindMatchCase = FLastFindDialog->Options.Contains(frMatchCase);
  541. EditorConfiguration.FindWholeWord = FLastFindDialog->Options.Contains(frWholeWord);
  542. EditorConfiguration.FindDown = FLastFindDialog->Options.Contains(frDown);
  543. WinConfiguration->Editor = EditorConfiguration;
  544. if (EditorConfiguration.FindMatchCase)
  545. {
  546. SearchTypes << stMatchCase;
  547. }
  548. if (EditorConfiguration.FindWholeWord)
  549. {
  550. SearchTypes << stWholeWord;
  551. }
  552. NewPos = EditorMemo->FindText(EditorConfiguration.FindText,
  553. EditorMemo->SelLength ? EditorMemo->SelStart+1 : EditorMemo->SelStart,
  554. EditorMemo->Text.Length(), SearchTypes, EditorConfiguration.FindDown);
  555. if (NewPos >= 0)
  556. {
  557. EditorMemo->SelStart = NewPos;
  558. EditorMemo->SelLength = EditorConfiguration.FindText.Length();
  559. }
  560. if (FLastFindDialog->Handle)
  561. {
  562. PositionFindDialog(true);
  563. }
  564. if (NewPos < 0)
  565. {
  566. if ((Replacements == 0) || FReplaceDialog->Options.Contains(frReplaceAll))
  567. {
  568. // now Screen->ActiveForm can be NULL when other form was meanwhile
  569. // activated and then focus was returned back to "find" dialog
  570. // (non VCL form)
  571. if (Screen->ActiveForm != this)
  572. {
  573. SetFocus();
  574. FLastFindDialog->Execute();
  575. }
  576. if (Replacements == 0)
  577. {
  578. MessageDialog(FMTLOAD(EDITOR_FIND_END, (EditorConfiguration.FindText)), qtInformation, qaOK, HELP_NONE);
  579. }
  580. else if (FReplaceDialog->Options.Contains(frReplaceAll))
  581. {
  582. MessageDialog(FMTLOAD(EDITOR_REPLACE_END, (Replacements)), qtInformation, qaOK, HELP_NONE);
  583. }
  584. }
  585. }
  586. }
  587. while (NewPos >= 0 && FLastFindDialog == FReplaceDialog &&
  588. FReplaceDialog->Options.Contains(frReplaceAll));
  589. }
  590. //---------------------------------------------------------------------------
  591. void __fastcall TEditorForm::FormShow(TObject * /*Sender*/)
  592. {
  593. LoadFile();
  594. CutFormToDesktop(this);
  595. }
  596. //---------------------------------------------------------------------------
  597. void __fastcall TEditorForm::LoadFile()
  598. {
  599. EditorMemo->Lines->LoadFromFile(FFileName);
  600. EditorMemo->Modified = false;
  601. FCaretPos.x = -1;
  602. ApplyConfiguration();
  603. }
  604. //---------------------------------------------------------------------------
  605. bool __fastcall TEditorForm::CursorInUpperPart()
  606. {
  607. HFONT OldFont;
  608. void *DC;
  609. TTextMetric TM;
  610. TRect Rect;
  611. DC = GetDC(EditorMemo->Handle);
  612. OldFont = SelectObject(DC, EditorMemo->Font->Handle);
  613. try
  614. {
  615. GetTextMetrics(DC, &TM);
  616. EditorMemo->Perform(EM_GETRECT, 0, ((int)&Rect));
  617. }
  618. __finally
  619. {
  620. SelectObject(DC, OldFont);
  621. ReleaseDC(EditorMemo->Handle, DC);
  622. }
  623. int VisibleLines = (Rect.Bottom - Rect.Top) / (TM.tmHeight + TM.tmExternalLeading);
  624. int FirstLine = SendMessage(EditorMemo->Handle, EM_GETFIRSTVISIBLELINE, 0, 0);
  625. TPoint CaretPos = EditorMemo->CaretPos;
  626. return (CaretPos.y - FirstLine) < VisibleLines / 2;
  627. }
  628. //---------------------------------------------------------------------------
  629. void __fastcall TEditorForm::PositionFindDialog(bool VerticalOnly)
  630. {
  631. assert(FLastFindDialog);
  632. if (!VerticalOnly)
  633. {
  634. FLastFindDialog->Left = Left + EditorMemo->Left + EditorMemo->Width / 2 - 100;
  635. }
  636. FLastFindDialog->Top = Top + EditorMemo->Top + (EditorMemo->Height / 4) +
  637. (CursorInUpperPart() ? (EditorMemo->Height / 2) : 0) - 40;
  638. }
  639. //---------------------------------------------------------------------------
  640. void __fastcall TEditorForm::StartFind(bool Find)
  641. {
  642. AnsiString Text = EditorMemo->SelText;
  643. TFindOptions Options;
  644. Options << frShowHelp;
  645. if (Text.IsEmpty())
  646. {
  647. Text = WinConfiguration->Editor.FindText;
  648. }
  649. TFindDialog * Dialog = Find ? FFindDialog : FReplaceDialog;
  650. if (FLastFindDialog && Dialog != FLastFindDialog && FLastFindDialog->Handle)
  651. {
  652. FLastFindDialog->CloseDialog();
  653. }
  654. FLastFindDialog = Dialog;
  655. if (!Text.IsEmpty())
  656. {
  657. FLastFindDialog->FindText = Text;
  658. }
  659. FReplaceDialog->ReplaceText = WinConfiguration->Editor.ReplaceText;
  660. if (WinConfiguration->Editor.FindMatchCase)
  661. {
  662. Options << frMatchCase;
  663. }
  664. if (WinConfiguration->Editor.FindWholeWord)
  665. {
  666. Options << frWholeWord;
  667. }
  668. if (EditorMemo->SupportsUpSearch)
  669. {
  670. if (WinConfiguration->Editor.FindDown)
  671. {
  672. Options << frDown;
  673. }
  674. }
  675. else
  676. {
  677. Options << frHideUpDown; // not implemented
  678. Options << frDown;
  679. }
  680. FLastFindDialog->Options = Options;
  681. if (!FLastFindDialog->Handle)
  682. {
  683. PositionFindDialog(false);
  684. }
  685. FLastFindDialog->Execute();
  686. }
  687. //---------------------------------------------------------------------------
  688. void __fastcall TEditorForm::GoToLine()
  689. {
  690. AnsiString Str;
  691. if (InputDialog(LoadStr(EDITOR_GO_TO_LINE), LoadStr(EDITOR_LINE_NUMBER), Str))
  692. {
  693. int Line = StrToIntDef(Str, -1);
  694. if (Line <= 0 || Line > EditorMemo->Lines->Count)
  695. {
  696. throw Exception(LoadStr(EDITOR_INVALID_LINE));
  697. }
  698. else
  699. {
  700. EditorMemo->CaretPos = TPoint(0, Line-1);
  701. }
  702. }
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TEditorForm::FormClose(TObject * /*Sender*/,
  706. TCloseAction & Action)
  707. {
  708. // Preferably announce closure here as this is called from within TForm::Close(),
  709. // so the annoucement will be synchronous (and editor manager thus
  710. // will consider the form to be really closed and will not block
  711. // application closure).
  712. // However FormClose is not called when form is closed due to
  713. // application exit, so there is last resort call from destructor.
  714. DoWindowClose();
  715. FCloseAnnounced = true;
  716. Action = caFree;
  717. }
  718. //---------------------------------------------------------------------------
  719. void __fastcall TEditorForm::DoWindowClose()
  720. {
  721. if (FOnWindowClose != NULL)
  722. {
  723. try
  724. {
  725. FOnWindowClose(this);
  726. }
  727. catch(Exception & E)
  728. {
  729. ShowExtendedException(&E);
  730. }
  731. }
  732. }
  733. //---------------------------------------------------------------------------
  734. void __fastcall TEditorForm::CreateParams(TCreateParams & Params)
  735. {
  736. TForm::CreateParams(Params);
  737. Params.WndParent = GetDesktopWindow();
  738. }
  739. //---------------------------------------------------------------------------
  740. void __fastcall TEditorForm::Reload()
  741. {
  742. if (!EditorMemo->Modified ||
  743. (MessageDialog(LoadStr(EDITOR_MODIFIED_RELOAD), qtConfirmation,
  744. qaOK | qaCancel) != qaCancel))
  745. {
  746. if (FOnFileReload)
  747. {
  748. FOnFileReload(this);
  749. }
  750. LoadFile();
  751. }
  752. }
  753. //---------------------------------------------------------------------------