Authenticate.cpp 23 KB

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