Authenticate.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "Authenticate.h"
  6. #include <VCLCommon.h>
  7. #include <TextsWin.h>
  8. #include <Terminal.h>
  9. #include <CoreMain.h>
  10. #include <PasTools.hpp>
  11. #include <CustomWinConfiguration.h>
  12. #include <Character.hpp>
  13. #include <Tools.h>
  14. //---------------------------------------------------------------------------
  15. #pragma package(smart_init)
  16. #pragma link "PasswordEdit"
  17. #pragma resource "*.dfm"
  18. //---------------------------------------------------------------------------
  19. __fastcall TAuthenticateForm::TAuthenticateForm(TComponent * Owner)
  20. : TForm(Owner), FSessionData(NULL), FTerminal(NULL)
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TAuthenticateForm::Init(TTerminal * Terminal)
  25. {
  26. FTerminal = Terminal;
  27. FSessionData = Terminal->SessionData;
  28. UseSystemSettings(this);
  29. FShowAsModalStorage = NULL;
  30. FFocusControl = NULL;
  31. UseDesktopFont(LogView);
  32. FAnimationPainted = false;
  33. FShowNoActivate = false;
  34. FPromptParent = InstructionsLabel->Parent;
  35. FPromptLeft = InstructionsLabel->Left;
  36. FPromptTop = InstructionsLabel->Top;
  37. FPromptRight = FPromptParent->ClientWidth - InstructionsLabel->Width - FPromptLeft;
  38. FPromptEditGap = PromptEdit1->Top - PromptLabel1->Top - PromptLabel1->Height;
  39. FPromptsGap = PromptLabel2->Top - PromptEdit1->Top - PromptEdit1->Height;
  40. ClientHeight = ScaleByTextHeight(this, 270);
  41. FHorizontalLogPadding = ScaleByTextHeight(this, 4);
  42. FVerticalLogPadding = ScaleByTextHeight(this, 3);
  43. FLogTextFormat << tfNoPrefix << tfWordBreak << tfVerticalCenter;
  44. FHintIndex = -1;
  45. }
  46. //---------------------------------------------------------------------------
  47. __fastcall TAuthenticateForm::~TAuthenticateForm()
  48. {
  49. Application->CancelHint();
  50. if (ReleaseAsModal(this, FShowAsModalStorage))
  51. {
  52. UnhookFormActivation(this);
  53. }
  54. }
  55. //---------------------------------------------------------------------------
  56. void __fastcall TAuthenticateForm::ShowAsModal()
  57. {
  58. if (IsApplicationMinimized())
  59. {
  60. FShowNoActivate = true;
  61. }
  62. // Do not call BringToFront when minimized, so that we do not have to use the same hack as in TMessageForm::SetZOrder
  63. ::ShowAsModal(this, FShowAsModalStorage, !FShowNoActivate);
  64. HookFormActivation(this);
  65. }
  66. //---------------------------------------------------------------------------
  67. void __fastcall TAuthenticateForm::CMShowingChanged(TMessage & Message)
  68. {
  69. if (Showing && FShowNoActivate)
  70. {
  71. ShowFormNoActivate(this);
  72. }
  73. else
  74. {
  75. TForm::Dispatch(&Message);
  76. }
  77. }
  78. //---------------------------------------------------------------------------
  79. void __fastcall TAuthenticateForm::WMNCCreate(TWMNCCreate & Message)
  80. {
  81. // bypass TForm::WMNCCreate to prevent disabling "resize"
  82. // (which is done for bsDialog, see comments in CreateParams)
  83. DefaultHandler(&Message);
  84. }
  85. //---------------------------------------------------------------------------
  86. void __fastcall TAuthenticateForm::DoCancel()
  87. {
  88. if (FOnCancel != NULL)
  89. {
  90. FOnCancel(this);
  91. }
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TAuthenticateForm::Dispatch(void * AMessage)
  95. {
  96. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  97. if (Message.Msg == WM_NCCREATE)
  98. {
  99. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  100. }
  101. else if (Message.Msg == WM_CLOSE)
  102. {
  103. DoCancel();
  104. TForm::Dispatch(AMessage);
  105. }
  106. else if (Message.Msg == WM_MANAGES_CAPTION)
  107. {
  108. // caption managed in TAuthenticateForm::AdjustControls()
  109. Message.Result = 1;
  110. }
  111. else if (Message.Msg == CM_SHOWINGCHANGED)
  112. {
  113. CMShowingChanged(Message);
  114. }
  115. else
  116. {
  117. TForm::Dispatch(AMessage);
  118. }
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TAuthenticateForm::CreateParams(TCreateParams & Params)
  122. {
  123. TForm::CreateParams(Params);
  124. // Allow resizing of the window, even if this is bsDialog.
  125. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  126. // reason receive focus, if window is shown atop non-main window
  127. // (like editor)
  128. Params.Style = Params.Style | WS_THICKFRAME;
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TAuthenticateForm::FormShow(TObject * /*Sender*/)
  132. {
  133. AdjustControls();
  134. if (FFocusControl != NULL)
  135. {
  136. ActiveControl = FFocusControl;
  137. }
  138. UnicodeString AnimationName = FSessionData->IsSecure() ? L"ConnectingSecure" : L"ConnectingInsecure";
  139. FFrameAnimation.Init(AnimationPaintBox, AnimationName);
  140. FFrameAnimation.Start();
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TAuthenticateForm::Log(const UnicodeString & Message, const UnicodeString & Additional)
  144. {
  145. // HACK
  146. // The first call to Repaint from TFrameAnimation happens
  147. // even before the form is showing. After it is shown it takes sometime too long
  148. // before the animation is painted, so that the form is closed before the first frame even appers
  149. if (!FAnimationPainted && Showing)
  150. {
  151. AnimationPaintBox->Repaint();
  152. FAnimationPainted = true;
  153. }
  154. int Index = LogView->Items->Add(Message);
  155. FHints.push_back(Additional);
  156. MakeLogItemVisible(Index);
  157. LogView->Repaint();
  158. }
  159. //---------------------------------------------------------------------------
  160. void __fastcall TAuthenticateForm::MakeLogItemVisible(int Index)
  161. {
  162. if (Index < LogView->TopIndex)
  163. {
  164. LogView->TopIndex = Index;
  165. }
  166. else
  167. {
  168. int TotalHeight = 0;
  169. int Index2 = Index;
  170. while ((Index2 >= 0) && TotalHeight < LogView->ClientHeight)
  171. {
  172. TotalHeight += LogItemHeight(Index2);
  173. Index2--;
  174. }
  175. // Index2 is the last item above the first fully visible,
  176. // were the Index on the very bottom.
  177. if (LogView->TopIndex <= Index2 + 1)
  178. {
  179. LogView->TopIndex = Index2 + 2;
  180. }
  181. }
  182. }
  183. //---------------------------------------------------------------------------
  184. void __fastcall TAuthenticateForm::AdjustControls()
  185. {
  186. UnicodeString ACaption;
  187. if (FStatus.IsEmpty())
  188. {
  189. ACaption = FSessionData->SessionName;
  190. }
  191. else
  192. {
  193. ACaption = FORMAT(L"%s - %s", (FStatus, FSessionData->SessionName));
  194. }
  195. Caption = FormatFormCaption(this, ACaption);
  196. }
  197. //---------------------------------------------------------------------------
  198. TLabel * __fastcall TAuthenticateForm::GenerateLabel(int Current, UnicodeString Caption)
  199. {
  200. TLabel * Result = new TLabel(this);
  201. Result->Parent = FPromptParent;
  202. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  203. Result->WordWrap = true;
  204. Result->AutoSize = false;
  205. Result->Top = Current;
  206. Result->Left = FPromptLeft;
  207. Result->Caption = Caption;
  208. int Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  209. Result->Width = Width;
  210. Result->AutoSize = true;
  211. Result->AutoSize = false;
  212. Result->Width = Width;
  213. return Result;
  214. }
  215. //---------------------------------------------------------------------------
  216. TCustomEdit * __fastcall TAuthenticateForm::GenerateEdit(int Current, bool Echo)
  217. {
  218. TCustomEdit * Result = (Echo ? static_cast<TCustomEdit *>(new TEdit(this)) :
  219. static_cast<TCustomEdit *>(new TPasswordEdit(this)));
  220. Result->Parent = FPromptParent;
  221. Result->Anchors = TAnchors() << akLeft << akTop << akRight;
  222. Result->Top = Current;
  223. Result->Left = FPromptLeft;
  224. Result->Width = FPromptParent->ClientWidth - FPromptLeft - FPromptRight;
  225. return Result;
  226. }
  227. //---------------------------------------------------------------------------
  228. void TAuthenticateForm::ExternalLabel(TLabel * Label)
  229. {
  230. Label->OnContextPopup = LabelContextPopup;
  231. Label->PopupMenu = LabelPopupMenu;
  232. UnicodeString Url;
  233. if (ExtractUrl(Label->Caption, Url))
  234. {
  235. Label->Cursor = crHandPoint;
  236. Label->OnClick = LinkClick;
  237. }
  238. }
  239. //---------------------------------------------------------------------------
  240. TList * __fastcall TAuthenticateForm::GeneratePrompt(
  241. TPromptKind Kind, const UnicodeString & Instructions, TStrings * Prompts)
  242. {
  243. while (FPromptParent->ControlCount > 0)
  244. {
  245. delete FPromptParent->Controls[0];
  246. }
  247. TList * Result = new TList;
  248. int Current = FPromptTop;
  249. if (!Instructions.IsEmpty())
  250. {
  251. TLabel * Label = GenerateLabel(Current, Instructions);
  252. ExternalLabel(Label);
  253. Current += Label->Height + FPromptsGap;
  254. }
  255. for (int Index = 0; Index < Prompts->Count; Index++)
  256. {
  257. if (Index > 0)
  258. {
  259. Current += FPromptEditGap;
  260. }
  261. TLabel * Label = GenerateLabel(Current, Prompts->Strings[Index]);
  262. if (Kind == pkKeybInteractive)
  263. {
  264. ExternalLabel(Label);
  265. }
  266. Current += Label->Height + FPromptEditGap;
  267. bool Echo = FLAGSET(int(Prompts->Objects[Index]), pupEcho);
  268. TCustomEdit * Edit = GenerateEdit(Current, Echo);
  269. Result->Add(Edit);
  270. Label->FocusControl = Edit;
  271. Current += Edit->Height;
  272. }
  273. FPromptParent->ClientHeight = Current;
  274. return Result;
  275. }
  276. //---------------------------------------------------------------------------
  277. bool __fastcall TAuthenticateForm::PromptUser(TPromptKind Kind, UnicodeString Name,
  278. UnicodeString Instructions, TStrings * Prompts, TStrings * Results, bool ForceLog,
  279. bool StoredCredentialsTried)
  280. {
  281. bool Result;
  282. TList * Edits = GeneratePrompt(Kind, Instructions, Prompts);
  283. try
  284. {
  285. bool ShowSessionRememberPasswordPanel = false;
  286. bool ShowSavePasswordPanel = false;
  287. TSessionData * Data = NULL;
  288. if (IsPasswordPrompt(Kind, Prompts) && StoredCredentialsTried)
  289. {
  290. Data = StoredSessions->FindSame(FSessionData);
  291. ShowSavePasswordPanel = (Data != NULL) && !Data->Password.IsEmpty();
  292. }
  293. // do not offer to remember password,
  294. // if we are offering to save the password to stored site
  295. if (!ShowSavePasswordPanel &&
  296. (Prompts->Count == 1) &&
  297. FLAGSET(int(Prompts->Objects[0]), pupRemember) &&
  298. DebugAlwaysTrue(IsPasswordOrPassphrasePrompt(Kind, Prompts)))
  299. {
  300. ShowSessionRememberPasswordPanel = true;
  301. }
  302. SavePasswordCheck->Checked = false;
  303. SavePasswordPanel->Visible = ShowSavePasswordPanel;
  304. SessionRememberPasswordCheck->Checked = false;
  305. SessionRememberPasswordPanel->Visible = ShowSessionRememberPasswordPanel;
  306. if (PasswordPanel->AutoSize)
  307. {
  308. PasswordPanel->AutoSize = false;
  309. PasswordPanel->AutoSize = true;
  310. }
  311. PasswordPanel->Realign();
  312. DebugAssert(Results->Count == Edits->Count);
  313. for (int Index = 0; Index < Edits->Count; Index++)
  314. {
  315. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  316. Edit->Text = Results->Strings[Index];
  317. }
  318. Result = Execute(Name, PasswordPanel,
  319. ((Edits->Count > 0) ?
  320. reinterpret_cast<TWinControl *>(Edits->Items[0]) :
  321. static_cast<TWinControl *>(PasswordOKButton)),
  322. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  323. if (Result)
  324. {
  325. for (int Index = 0; Index < Edits->Count; Index++)
  326. {
  327. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  328. Results->Strings[Index] = Edit->Text;
  329. Prompts->Objects[Index] = (TObject *)
  330. ((int(Prompts->Objects[Index]) & ~pupRemember) |
  331. FLAGMASK(((Index == 0) && SessionRememberPasswordCheck->Checked), pupRemember));
  332. }
  333. if (SavePasswordCheck->Checked)
  334. {
  335. DebugAssert(Data != NULL);
  336. DebugAssert(Results->Count >= 1);
  337. FSessionData->Password = Results->Strings[0];
  338. Data->Password = Results->Strings[0];
  339. // modified only, explicit
  340. StoredSessions->Save(false, true);
  341. }
  342. }
  343. }
  344. __finally
  345. {
  346. delete Edits;
  347. }
  348. return Result;
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TAuthenticateForm::UpdateBannerFont()
  352. {
  353. if (BannerMonospacedFontAction->Checked)
  354. {
  355. BannerMemo->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  356. BannerMemo->Font->Size = CustomWinConfiguration->DefaultFixedWidthFontSize;
  357. }
  358. else
  359. {
  360. BannerMemo->ParentFont = true;
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. void __fastcall TAuthenticateForm::Banner(const UnicodeString & Banner,
  365. bool & NeverShowAgain, int Options, unsigned int & Params)
  366. {
  367. BannerMemo->Lines->Text = Banner;
  368. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  369. NeverShowAgainCheck->Checked = NeverShowAgain;
  370. BannerMonospacedFontAction->Checked = FLAGSET(Params, bpMonospacedFont);
  371. UpdateBannerFont();
  372. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  373. BannerCloseButton, BannerCloseButton, false, true, false);
  374. if (Result)
  375. {
  376. NeverShowAgain = NeverShowAgainCheck->Checked;
  377. Params =
  378. (Params & ~bpMonospacedFont) |
  379. FLAGMASK(BannerMonospacedFontAction->Checked, bpMonospacedFont);
  380. }
  381. }
  382. //---------------------------------------------------------------------------
  383. bool __fastcall TAuthenticateForm::Execute(UnicodeString Status, TPanel * Panel,
  384. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  385. bool FixHeight, bool Zoom, bool ForceLog)
  386. {
  387. TModalResult DefaultButtonResult;
  388. TAlign Align = Panel->Align;
  389. try
  390. {
  391. // If not visible yet, the animation was not even initialized yet
  392. if (Visible)
  393. {
  394. FFrameAnimation.Stop();
  395. }
  396. DebugAssert(FStatus.IsEmpty());
  397. FStatus = Status;
  398. DefaultButton->Default = true;
  399. CancelButton->Cancel = true;
  400. DefaultButtonResult = DefaultResult(this);
  401. if (Zoom)
  402. {
  403. Panel->Align = alClient;
  404. }
  405. if (ForceLog || Visible)
  406. {
  407. if (ClientHeight < Panel->Height)
  408. {
  409. ClientHeight = Panel->Height;
  410. }
  411. // Panel being hidden gets not realigned automatically, even if it
  412. // has Align property set
  413. Panel->Top = ClientHeight - Panel->Height;
  414. Panel->Show();
  415. TCursor PrevCursor = Screen->Cursor;
  416. try
  417. {
  418. if (Zoom)
  419. {
  420. TopPanel->Hide();
  421. }
  422. else
  423. {
  424. if (LogView->Items->Count > 0)
  425. {
  426. // To avoid the scrolling effect when setting TopIndex
  427. LogView->Items->BeginUpdate();
  428. try
  429. {
  430. MakeLogItemVisible(LogView->Items->Count - 1);
  431. }
  432. __finally
  433. {
  434. LogView->Items->EndUpdate();
  435. RedrawLog();
  436. }
  437. }
  438. }
  439. Screen->Cursor = crDefault;
  440. if (!Visible)
  441. {
  442. DebugAssert(ForceLog);
  443. ShowAsModal();
  444. }
  445. ActiveControl = FocusControl;
  446. ModalResult = mrNone;
  447. AdjustControls();
  448. do
  449. {
  450. Application->HandleMessage();
  451. }
  452. while (!Application->Terminated && (ModalResult == mrNone));
  453. }
  454. __finally
  455. {
  456. Panel->Hide();
  457. Screen->Cursor = PrevCursor;
  458. if (Zoom)
  459. {
  460. TopPanel->Show();
  461. }
  462. Repaint();
  463. }
  464. }
  465. else
  466. {
  467. int PrevHeight = ClientHeight;
  468. int PrevMinHeight = Constraints->MinHeight;
  469. int PrevMaxHeight = Constraints->MaxHeight;
  470. try
  471. {
  472. Constraints->MinHeight = 0;
  473. ClientHeight = Panel->Height;
  474. if (FixHeight)
  475. {
  476. Constraints->MinHeight = Height;
  477. Constraints->MaxHeight = Height;
  478. }
  479. TopPanel->Hide();
  480. Panel->Show();
  481. FFocusControl = FocusControl;
  482. ShowModal();
  483. }
  484. __finally
  485. {
  486. FFocusControl = NULL;
  487. ClientHeight = PrevHeight;
  488. Constraints->MinHeight = PrevMinHeight;
  489. Constraints->MaxHeight = PrevMaxHeight;
  490. Panel->Hide();
  491. TopPanel->Show();
  492. }
  493. }
  494. }
  495. __finally
  496. {
  497. Panel->Align = Align;
  498. DefaultButton->Default = false;
  499. CancelButton->Cancel = false;
  500. FStatus = L"";
  501. AdjustControls();
  502. FFrameAnimation.Start();
  503. }
  504. bool Result = (ModalResult == DefaultButtonResult);
  505. if (!Result)
  506. {
  507. // This is not nice as it may ultimately route to
  508. // TTerminalThread::Cancel() and throw fatal exception,
  509. // what actually means that any PromptUser call during authentication never
  510. // return false and their fall back/alternative code never occurs.
  511. // It probably needs fixing.
  512. DoCancel();
  513. }
  514. return Result;
  515. }
  516. //---------------------------------------------------------------------------
  517. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  518. {
  519. FormHelp(this);
  520. }
  521. //---------------------------------------------------------------------------
  522. int __fastcall TAuthenticateForm::LogItemHeight(int Index)
  523. {
  524. UnicodeString S = LogView->Items->Strings[Index];
  525. TRect TextRect(0, 0, LogView->ClientWidth - (2 * FHorizontalLogPadding), 0);
  526. LogView->Canvas->Font = LogView->Font;
  527. LogView->Canvas->TextRect(TextRect, S, FLogTextFormat + (TTextFormat() << tfCalcRect));
  528. int Result = TextRect.Height() + (2 * FVerticalLogPadding);
  529. return Result;
  530. }
  531. //---------------------------------------------------------------------------
  532. void __fastcall TAuthenticateForm::LogViewMeasureItem(TWinControl * /*Control*/, int Index,
  533. int & Height)
  534. {
  535. Height = LogItemHeight(Index);
  536. }
  537. //---------------------------------------------------------------------------
  538. void __fastcall TAuthenticateForm::LogViewDrawItem(TWinControl * /*Control*/, int Index,
  539. TRect & Rect, TOwnerDrawState /*State*/)
  540. {
  541. // Reset back to base colors. We do not want to render selection.
  542. // + At initial phases the canvas font will not yet reflect the desktop font.
  543. LogView->Canvas->Font = LogView->Font;
  544. LogView->Canvas->Brush->Color = LogView->Color;
  545. LogView->Canvas->FillRect(Rect);
  546. // tfVerticalCenter does not seem to center the text,
  547. // so we need to deflate the vertical size too
  548. Rect.Inflate(-FHorizontalLogPadding, -FVerticalLogPadding);
  549. UnicodeString S = LogView->Items->Strings[Index];
  550. LogView->Canvas->TextRect(Rect, S, FLogTextFormat);
  551. }
  552. //---------------------------------------------------------------------------
  553. void __fastcall TAuthenticateForm::RedrawLog()
  554. {
  555. // Redraw including the scrollbar (RDW_FRAME)
  556. RedrawWindow(LogView->Handle, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
  557. }
  558. //---------------------------------------------------------------------------
  559. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  560. {
  561. if (LogView->Showing)
  562. {
  563. // Mainly to avoid the scrolling effect when setting TopIndex
  564. LogView->Items->BeginUpdate();
  565. try
  566. {
  567. // Rebuild the list view to force Windows to resend WM_MEASUREITEM
  568. int ItemIndex = LogView->ItemIndex;
  569. int TopIndex = LogView->TopIndex;
  570. std::unique_ptr<TStringList> Items(new TStringList);
  571. Items->Assign(LogView->Items);
  572. LogView->Items->Assign(Items.get());
  573. LogView->ItemIndex = ItemIndex;
  574. LogView->TopIndex = TopIndex;
  575. }
  576. __finally
  577. {
  578. LogView->Items->EndUpdate();
  579. RedrawLog();
  580. }
  581. }
  582. }
  583. //---------------------------------------------------------------------------
  584. void __fastcall TAuthenticateForm::ChangeScale(int M, int D)
  585. {
  586. TForm::ChangeScale(M, D);
  587. // Recreate the list to re-measure the items according to the new font
  588. if (DebugAlwaysTrue(LogView->HandleAllocated()) &&
  589. (LogView->Items->Count > 0))
  590. {
  591. std::unique_ptr<TStrings> Items(new TStringList());
  592. Items->AddStrings(LogView->Items);
  593. LogView->Items->Clear();
  594. LogView->Items->AddStrings(Items.get());
  595. MakeLogItemVisible(LogView->Items->Count - 1);
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. void __fastcall TAuthenticateForm::BannerMemoContextPopup(TObject * Sender, TPoint & MousePos, bool & Handled)
  600. {
  601. MenuPopup(Sender, MousePos, Handled);
  602. }
  603. //---------------------------------------------------------------------------
  604. void __fastcall TAuthenticateForm::BannerMonospacedFontActionExecute(TObject * /*Sender*/)
  605. {
  606. BannerMonospacedFontAction->Checked = !BannerMonospacedFontAction->Checked;
  607. UpdateBannerFont();
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TAuthenticateForm::LogViewMouseMove(TObject *, TShiftState, int X, int Y)
  611. {
  612. int Index = LogView->ItemAtPos(TPoint(X, Y), true);
  613. if (FHintIndex != Index)
  614. {
  615. Application->CancelHint();
  616. FHintIndex = Index;
  617. if (Index >= 0)
  618. {
  619. LogView->Hint = FHints[Index];
  620. }
  621. }
  622. }
  623. //---------------------------------------------------------------------------
  624. bool TAuthenticateForm::ExtractUrl(const UnicodeString & Text, UnicodeString & Url)
  625. {
  626. bool Result = false;
  627. UnicodeString Prefix = HttpsProtocol + ProtocolSeparator;
  628. int P = Text.Pos(Prefix);
  629. if (P == 0)
  630. {
  631. Prefix = HttpProtocol + ProtocolSeparator;
  632. P = Text.Pos(Prefix);
  633. }
  634. if (P > 0)
  635. {
  636. Url = Text.SubString(P, Text.Length() - P + 1);
  637. P = 1;
  638. while (P <= Url.Length())
  639. {
  640. if (TCharacter::IsWhiteSpace(Url[P]))
  641. {
  642. Url.SetLength(P - 1);
  643. }
  644. P++;
  645. }
  646. // At least one letter/digit after the prefix
  647. if ((Url.Length() > Prefix.Length()) &&
  648. (::IsLetter(Url[Prefix.Length() + 1]) || ::IsDigit(Url[Prefix.Length() + 1])))
  649. {
  650. Result = true;
  651. }
  652. }
  653. return Result;
  654. }
  655. //---------------------------------------------------------------------------
  656. void __fastcall TAuthenticateForm::LabelContextPopup(TObject * Sender, const TPoint & MousePos, bool & Handled)
  657. {
  658. TLabel * Label = dynamic_cast<TLabel *>(Sender);
  659. UnicodeString Url;
  660. LabelOpenLinkAction->Visible = ExtractUrl(Label->Caption, Url);
  661. FContextLabel = Label;
  662. MenuPopup(Sender, MousePos, Handled);
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TAuthenticateForm::LabelCopyActionExecute(TObject *)
  666. {
  667. if (DebugAlwaysTrue(FContextLabel != NULL))
  668. {
  669. TInstantOperationVisualizer Visualizer;
  670. CopyToClipboard(FContextLabel->Caption);
  671. }
  672. }
  673. //---------------------------------------------------------------------------
  674. void TAuthenticateForm::LabelOpen(TLabel * Label)
  675. {
  676. UnicodeString Url;
  677. if (ExtractUrl(Label->Caption, Url))
  678. {
  679. OpenBrowser(Url);
  680. }
  681. }
  682. //---------------------------------------------------------------------------
  683. void __fastcall TAuthenticateForm::LabelOpenLinkActionExecute(TObject *)
  684. {
  685. if (DebugAlwaysTrue(FContextLabel != NULL))
  686. {
  687. LabelOpen(FContextLabel);
  688. }
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TAuthenticateForm::LinkClick(TObject * Sender)
  692. {
  693. LabelOpen(DebugNotNull(dynamic_cast<TLabel *>(Sender)));
  694. }
  695. //---------------------------------------------------------------------------