Authenticate.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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, true);
  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 = FStatus + TitleSeparator + FSessionData->SessionName;
  194. }
  195. Caption = FormatFormCaption(this, ACaption);
  196. }
  197. //---------------------------------------------------------------------------
  198. TLabel * __fastcall TAuthenticateForm::GenerateLabel(int Current, UnicodeString Caption)
  199. {
  200. TLabel * Result = new TUIStateAwareLabel(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. DeleteChildren(FPromptParent);
  244. TList * Result = new TList;
  245. int Current = FPromptTop;
  246. if (!Instructions.IsEmpty())
  247. {
  248. TLabel * Label = GenerateLabel(Current, Instructions);
  249. ExternalLabel(Label);
  250. Current += Label->Height + FPromptsGap;
  251. }
  252. for (int Index = 0; Index < Prompts->Count; Index++)
  253. {
  254. if (Index > 0)
  255. {
  256. Current += FPromptEditGap;
  257. }
  258. TLabel * Label = GenerateLabel(Current, Prompts->Strings[Index]);
  259. if (Kind == pkKeybInteractive)
  260. {
  261. ExternalLabel(Label);
  262. }
  263. Current += Label->Height + FPromptEditGap;
  264. bool Echo = FLAGSET(int(Prompts->Objects[Index]), pupEcho);
  265. TCustomEdit * Edit = GenerateEdit(Current, Echo);
  266. Result->Add(Edit);
  267. Label->FocusControl = Edit;
  268. Current += Edit->Height;
  269. }
  270. FPromptParent->ClientHeight = Current;
  271. return Result;
  272. }
  273. //---------------------------------------------------------------------------
  274. bool __fastcall TAuthenticateForm::PromptUser(TPromptKind Kind, UnicodeString Name,
  275. UnicodeString Instructions, TStrings * Prompts, TStrings * Results, bool ForceLog,
  276. bool StoredCredentialsTried)
  277. {
  278. bool Result;
  279. TList * Edits = GeneratePrompt(Kind, Instructions, Prompts);
  280. try
  281. {
  282. bool ShowSessionRememberPasswordPanel = false;
  283. bool ShowSavePasswordPanel = false;
  284. TSessionData * Data = NULL;
  285. if (IsPasswordPrompt(Kind, Prompts) && StoredCredentialsTried)
  286. {
  287. Data = StoredSessions->FindSame(FSessionData);
  288. ShowSavePasswordPanel = (Data != NULL) && !Data->Password.IsEmpty();
  289. }
  290. // do not offer to remember password,
  291. // if we are offering to save the password to stored site
  292. if (!ShowSavePasswordPanel &&
  293. (Prompts->Count == 1) &&
  294. FLAGSET(int(Prompts->Objects[0]), pupRemember) &&
  295. DebugAlwaysTrue(IsPasswordOrPassphrasePrompt(Kind, Prompts)))
  296. {
  297. ShowSessionRememberPasswordPanel = true;
  298. }
  299. SavePasswordCheck->Checked = false;
  300. SavePasswordPanel->Visible = ShowSavePasswordPanel;
  301. SessionRememberPasswordCheck->Checked = false;
  302. SessionRememberPasswordPanel->Visible = ShowSessionRememberPasswordPanel;
  303. if (PasswordPanel->AutoSize)
  304. {
  305. PasswordPanel->AutoSize = false;
  306. PasswordPanel->AutoSize = true;
  307. }
  308. PasswordPanel->Realign();
  309. DebugAssert(Results->Count == Edits->Count);
  310. for (int Index = 0; Index < Edits->Count; Index++)
  311. {
  312. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  313. Edit->Text = Results->Strings[Index];
  314. }
  315. Result = Execute(Name, PasswordPanel,
  316. ((Edits->Count > 0) ?
  317. reinterpret_cast<TWinControl *>(Edits->Items[0]) :
  318. static_cast<TWinControl *>(PasswordOKButton)),
  319. PasswordOKButton, PasswordCancelButton, true, false, ForceLog);
  320. if (Result)
  321. {
  322. for (int Index = 0; Index < Edits->Count; Index++)
  323. {
  324. TCustomEdit * Edit = reinterpret_cast<TCustomEdit *>(Edits->Items[Index]);
  325. Results->Strings[Index] = Edit->Text;
  326. Prompts->Objects[Index] = (TObject *)
  327. ((int(Prompts->Objects[Index]) & ~pupRemember) |
  328. FLAGMASK(((Index == 0) && SessionRememberPasswordCheck->Checked), pupRemember));
  329. }
  330. if (SavePasswordCheck->Checked)
  331. {
  332. DebugAssert(Data != NULL);
  333. DebugAssert(Results->Count >= 1);
  334. FSessionData->Password = Results->Strings[0];
  335. Data->Password = Results->Strings[0];
  336. // modified only, explicit
  337. StoredSessions->Save(false, true);
  338. }
  339. }
  340. }
  341. __finally
  342. {
  343. delete Edits;
  344. }
  345. return Result;
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TAuthenticateForm::UpdateBannerFont()
  349. {
  350. if (BannerMonospacedFontAction->Checked)
  351. {
  352. BannerMemo->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  353. BannerMemo->Font->Size = CustomWinConfiguration->DefaultFixedWidthFontSize;
  354. }
  355. else
  356. {
  357. BannerMemo->ParentFont = true;
  358. }
  359. }
  360. //---------------------------------------------------------------------------
  361. void __fastcall TAuthenticateForm::Banner(const UnicodeString & Banner,
  362. bool & NeverShowAgain, int Options, unsigned int & Params)
  363. {
  364. BannerMemo->Lines->Text = Banner;
  365. NeverShowAgainCheck->Visible = FLAGCLEAR(Options, boDisableNeverShowAgain);
  366. NeverShowAgainCheck->Checked = NeverShowAgain;
  367. BannerMonospacedFontAction->Checked = FLAGSET(Params, bpMonospacedFont);
  368. UpdateBannerFont();
  369. bool Result = Execute(LoadStr(AUTHENTICATION_BANNER), BannerPanel, BannerCloseButton,
  370. BannerCloseButton, BannerCloseButton, false, true, false);
  371. if (Result)
  372. {
  373. NeverShowAgain = NeverShowAgainCheck->Checked;
  374. Params =
  375. (Params & ~bpMonospacedFont) |
  376. FLAGMASK(BannerMonospacedFontAction->Checked, bpMonospacedFont);
  377. }
  378. }
  379. //---------------------------------------------------------------------------
  380. bool __fastcall TAuthenticateForm::Execute(UnicodeString Status, TPanel * Panel,
  381. TWinControl * FocusControl, TButton * DefaultButton, TButton * CancelButton,
  382. bool FixHeight, bool Zoom, bool ForceLog)
  383. {
  384. TModalResult DefaultButtonResult;
  385. TAlign Align = Panel->Align;
  386. try
  387. {
  388. // If not visible yet, the animation was not even initialized yet
  389. if (Visible)
  390. {
  391. FFrameAnimation.Stop();
  392. }
  393. DebugAssert(FStatus.IsEmpty());
  394. FStatus = Status;
  395. DefaultButton->Default = true;
  396. CancelButton->Cancel = true;
  397. DefaultButtonResult = DefaultResult(this);
  398. if (Zoom)
  399. {
  400. Panel->Align = alClient;
  401. }
  402. if (ForceLog || Visible)
  403. {
  404. if (ClientHeight < Panel->Height)
  405. {
  406. ClientHeight = Panel->Height;
  407. }
  408. // Panel being hidden gets not realigned automatically, even if it
  409. // has Align property set
  410. Panel->Top = ClientHeight - Panel->Height;
  411. Panel->Show();
  412. TCursor PrevCursor = Screen->Cursor;
  413. try
  414. {
  415. if (Zoom)
  416. {
  417. TopPanel->Hide();
  418. }
  419. else
  420. {
  421. if (LogView->Items->Count > 0)
  422. {
  423. // To avoid the scrolling effect when setting TopIndex
  424. LogView->Items->BeginUpdate();
  425. try
  426. {
  427. MakeLogItemVisible(LogView->Items->Count - 1);
  428. }
  429. __finally
  430. {
  431. LogView->Items->EndUpdate();
  432. RedrawLog();
  433. }
  434. }
  435. }
  436. Screen->Cursor = crDefault;
  437. if (!Visible)
  438. {
  439. DebugAssert(ForceLog);
  440. ShowAsModal();
  441. }
  442. ActiveControl = FocusControl;
  443. ModalResult = mrNone;
  444. AdjustControls();
  445. do
  446. {
  447. Application->HandleMessage();
  448. }
  449. while (!Application->Terminated && (ModalResult == mrNone));
  450. }
  451. __finally
  452. {
  453. Panel->Hide();
  454. Screen->Cursor = PrevCursor;
  455. if (Zoom)
  456. {
  457. TopPanel->Show();
  458. }
  459. Repaint();
  460. }
  461. }
  462. else
  463. {
  464. int PrevHeight = ClientHeight;
  465. int PrevMinHeight = Constraints->MinHeight;
  466. int PrevMaxHeight = Constraints->MaxHeight;
  467. try
  468. {
  469. Constraints->MinHeight = 0;
  470. ClientHeight = Panel->Height;
  471. if (FixHeight)
  472. {
  473. Constraints->MinHeight = Height;
  474. Constraints->MaxHeight = Height;
  475. }
  476. TopPanel->Hide();
  477. Panel->Show();
  478. FFocusControl = FocusControl;
  479. ShowModal();
  480. }
  481. __finally
  482. {
  483. FFocusControl = NULL;
  484. ClientHeight = PrevHeight;
  485. Constraints->MinHeight = PrevMinHeight;
  486. Constraints->MaxHeight = PrevMaxHeight;
  487. Panel->Hide();
  488. TopPanel->Show();
  489. }
  490. }
  491. }
  492. __finally
  493. {
  494. Panel->Align = Align;
  495. DefaultButton->Default = false;
  496. CancelButton->Cancel = false;
  497. FStatus = L"";
  498. AdjustControls();
  499. FFrameAnimation.Start();
  500. }
  501. bool Result = (ModalResult == DefaultButtonResult);
  502. if (!Result)
  503. {
  504. // This is not nice as it may ultimately route to
  505. // TTerminalThread::Cancel() and throw fatal exception,
  506. // what actually means that any PromptUser call during authentication never
  507. // return false and their fall back/alternative code never occurs.
  508. // It probably needs fixing.
  509. DoCancel();
  510. }
  511. return Result;
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall TAuthenticateForm::HelpButtonClick(TObject * /*Sender*/)
  515. {
  516. FormHelp(this);
  517. }
  518. //---------------------------------------------------------------------------
  519. int __fastcall TAuthenticateForm::LogItemHeight(int Index)
  520. {
  521. UnicodeString S = LogView->Items->Strings[Index];
  522. TRect TextRect(0, 0, LogView->ClientWidth - (2 * FHorizontalLogPadding), 0);
  523. LogView->Canvas->Font = LogView->Font;
  524. LogView->Canvas->TextRect(TextRect, S, FLogTextFormat + (TTextFormat() << tfCalcRect));
  525. int Result = TextRect.Height() + (2 * FVerticalLogPadding);
  526. return Result;
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TAuthenticateForm::LogViewMeasureItem(TWinControl * /*Control*/, int Index,
  530. int & Height)
  531. {
  532. Height = LogItemHeight(Index);
  533. }
  534. //---------------------------------------------------------------------------
  535. void __fastcall TAuthenticateForm::LogViewDrawItem(TWinControl * /*Control*/, int Index,
  536. TRect & Rect, TOwnerDrawState /*State*/)
  537. {
  538. // Reset back to base colors. We do not want to render selection.
  539. // + At initial phases the canvas font will not yet reflect the desktop font.
  540. LogView->Canvas->Font = LogView->Font;
  541. LogView->Canvas->Brush->Color = LogView->Color;
  542. LogView->Canvas->FillRect(Rect);
  543. // tfVerticalCenter does not seem to center the text,
  544. // so we need to deflate the vertical size too
  545. Rect.Inflate(-FHorizontalLogPadding, -FVerticalLogPadding);
  546. UnicodeString S = LogView->Items->Strings[Index];
  547. LogView->Canvas->TextRect(Rect, S, FLogTextFormat);
  548. }
  549. //---------------------------------------------------------------------------
  550. void __fastcall TAuthenticateForm::RedrawLog()
  551. {
  552. // Redraw including the scrollbar (RDW_FRAME)
  553. RedrawWindow(LogView->Handle, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_UPDATENOW);
  554. }
  555. //---------------------------------------------------------------------------
  556. void __fastcall TAuthenticateForm::FormResize(TObject * /*Sender*/)
  557. {
  558. if (LogView->Showing)
  559. {
  560. // Mainly to avoid the scrolling effect when setting TopIndex
  561. LogView->Items->BeginUpdate();
  562. try
  563. {
  564. // Rebuild the list view to force Windows to resend WM_MEASUREITEM
  565. int ItemIndex = LogView->ItemIndex;
  566. int TopIndex = LogView->TopIndex;
  567. std::unique_ptr<TStringList> Items(new TStringList);
  568. Items->Assign(LogView->Items);
  569. LogView->Items->Assign(Items.get());
  570. LogView->ItemIndex = ItemIndex;
  571. LogView->TopIndex = TopIndex;
  572. }
  573. __finally
  574. {
  575. LogView->Items->EndUpdate();
  576. RedrawLog();
  577. }
  578. }
  579. }
  580. //---------------------------------------------------------------------------
  581. void __fastcall TAuthenticateForm::ChangeScale(int M, int D, bool isDpiChange)
  582. {
  583. TForm::ChangeScale(M, D, isDpiChange);
  584. // Recreate the list to re-measure the items according to the new font
  585. if (DebugAlwaysTrue(LogView->HandleAllocated()) &&
  586. (LogView->Items->Count > 0))
  587. {
  588. std::unique_ptr<TStrings> Items(new TStringList());
  589. Items->AddStrings(LogView->Items);
  590. LogView->Items->Clear();
  591. LogView->Items->AddStrings(Items.get());
  592. MakeLogItemVisible(LogView->Items->Count - 1);
  593. }
  594. }
  595. //---------------------------------------------------------------------------
  596. void __fastcall TAuthenticateForm::BannerMemoContextPopup(TObject * Sender, TPoint & MousePos, bool & Handled)
  597. {
  598. MenuPopup(Sender, MousePos, Handled);
  599. }
  600. //---------------------------------------------------------------------------
  601. void __fastcall TAuthenticateForm::BannerMonospacedFontActionExecute(TObject * /*Sender*/)
  602. {
  603. BannerMonospacedFontAction->Checked = !BannerMonospacedFontAction->Checked;
  604. UpdateBannerFont();
  605. }
  606. //---------------------------------------------------------------------------
  607. void __fastcall TAuthenticateForm::LogViewMouseMove(TObject *, TShiftState, int X, int Y)
  608. {
  609. int Index = LogView->ItemAtPos(TPoint(X, Y), true);
  610. if (FHintIndex != Index)
  611. {
  612. Application->CancelHint();
  613. FHintIndex = Index;
  614. if (Index >= 0)
  615. {
  616. LogView->Hint = FHints[Index];
  617. }
  618. }
  619. }
  620. //---------------------------------------------------------------------------
  621. bool TAuthenticateForm::ExtractUrl(const UnicodeString & Text, UnicodeString & Url)
  622. {
  623. bool Result = false;
  624. UnicodeString Prefix = HttpsProtocol + ProtocolSeparator;
  625. int P = Text.Pos(Prefix);
  626. if (P == 0)
  627. {
  628. Prefix = HttpProtocol + ProtocolSeparator;
  629. P = Text.Pos(Prefix);
  630. }
  631. if (P > 0)
  632. {
  633. Url = Text.SubString(P, Text.Length() - P + 1);
  634. P = 1;
  635. while (P <= Url.Length())
  636. {
  637. #pragma warn -dpr
  638. if (TCharacter::IsWhiteSpace(Url[P]))
  639. #pragma warn .dpr
  640. {
  641. Url.SetLength(P - 1);
  642. }
  643. P++;
  644. }
  645. // At least one letter/digit after the prefix
  646. if ((Url.Length() > Prefix.Length()) &&
  647. (::IsLetter(Url[Prefix.Length() + 1]) || ::IsDigit(Url[Prefix.Length() + 1])))
  648. {
  649. Result = true;
  650. }
  651. }
  652. return Result;
  653. }
  654. //---------------------------------------------------------------------------
  655. void __fastcall TAuthenticateForm::LabelContextPopup(TObject * Sender, const TPoint & MousePos, bool & Handled)
  656. {
  657. TLabel * Label = dynamic_cast<TLabel *>(Sender);
  658. UnicodeString Url;
  659. LabelOpenLinkAction2->Visible = ExtractUrl(Label->Caption, Url);
  660. FContextLabel = Label;
  661. MenuPopup(Sender, MousePos, Handled);
  662. }
  663. //---------------------------------------------------------------------------
  664. void __fastcall TAuthenticateForm::LabelCopyActionExecute(TObject *)
  665. {
  666. if (DebugAlwaysTrue(FContextLabel != NULL))
  667. {
  668. TInstantOperationVisualizer Visualizer;
  669. CopyToClipboard(FContextLabel->Caption);
  670. }
  671. }
  672. //---------------------------------------------------------------------------
  673. void TAuthenticateForm::LabelOpen(TLabel * Label)
  674. {
  675. UnicodeString Url;
  676. if (ExtractUrl(Label->Caption, Url))
  677. {
  678. OpenBrowser(Url);
  679. }
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TAuthenticateForm::LabelOpenLinkAction2Execute(TObject *)
  683. {
  684. if (DebugAlwaysTrue(FContextLabel != NULL))
  685. {
  686. LabelOpen(FContextLabel);
  687. }
  688. }
  689. //---------------------------------------------------------------------------
  690. void __fastcall TAuthenticateForm::LinkClick(TObject * Sender)
  691. {
  692. LabelOpen(DebugNotNull(dynamic_cast<TLabel *>(Sender)));
  693. }
  694. //---------------------------------------------------------------------------