Authenticate.cpp 23 KB

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