MessageDlg.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. //---------------------------------------------------------------------------
  2. #include <FormsPCH.h>
  3. #pragma hdrstop
  4. #include <Vcl.Imaging.pngimage.hpp>
  5. #include <WebBrowserEx.hpp>
  6. #include <RegularExpressions.hpp>
  7. #include <Setup.h>
  8. #include <WinApi.h>
  9. #include "MessageDlg.h"
  10. //---------------------------------------------------------------------------
  11. #pragma resource "*.dfm"
  12. //---------------------------------------------------------------------------
  13. const UnicodeString MessagePanelName(L"Panel");
  14. const UnicodeString MainMessageLabelName(L"MainMessage");
  15. const UnicodeString MessageLabelName(L"Message");
  16. const UnicodeString YesButtonName(L"Yes");
  17. const UnicodeString OKButtonName(L"OK");
  18. //---------------------------------------------------------------------------
  19. class TMessageButton : public TButton
  20. {
  21. public:
  22. __fastcall TMessageButton(TComponent * Owner);
  23. protected:
  24. virtual void __fastcall Dispatch(void * Message);
  25. virtual void __fastcall CreateWnd();
  26. private:
  27. void __fastcall WMGetDlgCode(TWMGetDlgCode & Message);
  28. };
  29. //---------------------------------------------------------------------------
  30. __fastcall TMessageButton::TMessageButton(TComponent * Owner) :
  31. TButton(Owner)
  32. {
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TMessageButton::Dispatch(void * Message)
  36. {
  37. TMessage * M = reinterpret_cast<TMessage*>(Message);
  38. if (M->Msg == WM_GETDLGCODE)
  39. {
  40. WMGetDlgCode(*((TWMGetDlgCode *)Message));
  41. }
  42. else
  43. {
  44. TButton::Dispatch(Message);
  45. }
  46. }
  47. //---------------------------------------------------------------------------
  48. void __fastcall TMessageButton::WMGetDlgCode(TWMGetDlgCode & Message)
  49. {
  50. TButton::Dispatch(&Message);
  51. // WORKAROUND
  52. // Windows default handler returns DLGC_WANTARROWS for split buttons,
  53. // what prevent left/right keys from being used for focusing next/previous buttons/controls.
  54. // Overrwide that. Though note that we need to pass the up/down keys back to button
  55. // to allow drop down, see TMessageForm::CMDialogKey
  56. Message.Result = Message.Result & ~DLGC_WANTARROWS;
  57. }
  58. //---------------------------------------------------------------------------
  59. void __fastcall TMessageButton::CreateWnd()
  60. {
  61. TButton::CreateWnd();
  62. SetExplorerTheme(this);
  63. }
  64. //---------------------------------------------------------------------------
  65. __fastcall TMessageForm::TMessageForm(TComponent * AOwner) : TForm(AOwner)
  66. {
  67. FShowNoActivate = false;
  68. MessageMemo = NULL;
  69. MessageBrowserPanel = NULL;
  70. MessageBrowser = NULL;
  71. NeverAskAgainCheck = NULL;
  72. FUpdateForShiftStateTimer = NULL;
  73. UseSystemSettingsPre(this);
  74. }
  75. //---------------------------------------------------------------------------
  76. __fastcall TMessageForm::~TMessageForm()
  77. {
  78. SAFE_DESTROY(FUpdateForShiftStateTimer);
  79. }
  80. //---------------------------------------------------------------------------
  81. void __fastcall TMessageForm::HelpButtonSubmit(TObject * /*Sender*/, unsigned int & /*Answer*/)
  82. {
  83. if (HelpKeyword != HELP_NONE)
  84. {
  85. FormHelp(this);
  86. }
  87. else
  88. {
  89. MessageWithNoHelp(GetReportText());
  90. }
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TMessageForm::ReportButtonSubmit(TObject * /*Sender*/, unsigned int & /*Answer*/)
  94. {
  95. // Report text goes last, as it may exceed URL parameters limit (2048) and get truncated.
  96. // And we need to preserve the other parameters.
  97. UnicodeString Url =
  98. FMTLOAD(ERROR_REPORT_URL2,
  99. (Configuration->ProductVersion, GUIConfiguration->AppliedLocaleHex,
  100. EncodeUrlString(GetReportText())));
  101. OpenBrowser(Url);
  102. }
  103. //---------------------------------------------------------------------------
  104. void __fastcall TMessageForm::UpdateForShiftState()
  105. {
  106. TShiftState ShiftState =
  107. KeyboardStateToShiftState() * AllKeyShiftStates();
  108. if (FShiftState != ShiftState)
  109. {
  110. FShiftState = ShiftState;
  111. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  112. {
  113. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  114. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  115. {
  116. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  117. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  118. {
  119. TMenuItem * Item = MenuItems->Items[ItemIndex];
  120. TShiftState GrouppedShiftState(Item->Tag >> 16);
  121. if (Item->Enabled &&
  122. ((ShiftState.Empty() && Item->Default) ||
  123. (!ShiftState.Empty() && (ShiftState == GrouppedShiftState))))
  124. {
  125. Button->Caption = CopyToChar(Item->Caption, L'\t', false);
  126. Button->ModalResult = Item->Tag & 0xFFFF;
  127. DebugAssert(Button->OnClick == NULL);
  128. DebugAssert(Item->OnClick == MenuItemClick);
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TMessageForm::KeyUp(Word & Key, TShiftState Shift)
  138. {
  139. UpdateForShiftState();
  140. TForm::KeyUp(Key, Shift);
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TMessageForm::KeyDown(Word & Key, TShiftState Shift)
  144. {
  145. if (Shift.Contains(ssCtrl) && (Key == L'C'))
  146. {
  147. AppLog(L"Copying message to clipboard");
  148. TInstantOperationVisualizer Visualizer;
  149. CopyToClipboard(GetFormText());
  150. }
  151. else
  152. {
  153. if (!Shift.Contains(ssCtrl))
  154. {
  155. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  156. {
  157. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  158. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  159. {
  160. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  161. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  162. {
  163. TMenuItem * Item = MenuItems->Items[ItemIndex];
  164. if (IsAccel(Key, MenuItems->Items[ItemIndex]->Caption))
  165. {
  166. Item->OnClick(Item);
  167. Key = 0;
  168. break;
  169. }
  170. }
  171. }
  172. if (Key == 0)
  173. {
  174. break;
  175. }
  176. }
  177. }
  178. UpdateForShiftState();
  179. TForm::KeyDown(Key, Shift);
  180. }
  181. }
  182. //---------------------------------------------------------------------------
  183. UnicodeString __fastcall TMessageForm::NormalizeNewLines(UnicodeString Text)
  184. {
  185. Text = ReplaceStr(Text, L"\r", L"");
  186. Text = ReplaceStr(Text, L"\n", L"\r\n");
  187. return Text;
  188. }
  189. //---------------------------------------------------------------------------
  190. UnicodeString __fastcall TMessageForm::GetFormText()
  191. {
  192. UnicodeString DividerLine, ButtonCaptions;
  193. DividerLine = GetDividerLine() + sLineBreak;
  194. for (int i = 0; i < ComponentCount - 1; i++)
  195. {
  196. if (dynamic_cast<TButton*>(Components[i]) != NULL)
  197. {
  198. ButtonCaptions += dynamic_cast<TButton*>(Components[i])->Caption +
  199. UnicodeString::StringOfChar(L' ', 3);
  200. }
  201. }
  202. ButtonCaptions = ReplaceStr(ButtonCaptions, L"&", L"");
  203. UnicodeString MoreMessages;
  204. if (MessageMemo != NULL)
  205. {
  206. MoreMessages = MessageMemo->Text + DividerLine;
  207. }
  208. else if ((MessageBrowser != NULL) && CopyTextFromBrowser(MessageBrowser, MoreMessages))
  209. {
  210. if (!EndsStr(sLineBreak, MoreMessages))
  211. {
  212. MoreMessages += sLineBreak;
  213. }
  214. MoreMessages += DividerLine;
  215. }
  216. UnicodeString MessageCaption = NormalizeNewLines(MessageText);
  217. UnicodeString Result = FORMAT(L"%s%s%s%s%s%s%s%s%s%s%s", (DividerLine, Caption, sLineBreak,
  218. DividerLine, MessageCaption, sLineBreak, DividerLine, MoreMessages,
  219. ButtonCaptions, sLineBreak, DividerLine));
  220. return Result;
  221. }
  222. //---------------------------------------------------------------------------
  223. UnicodeString __fastcall TMessageForm::GetReportText()
  224. {
  225. UnicodeString Text = MessageText;
  226. Text = Text.TrimRight();
  227. if (MessageMemo != NULL)
  228. {
  229. Text += L"\n\n" + MessageMemo->Text;
  230. }
  231. // Currently we use browser for updates box only and it has help context,
  232. // and does not have Report button, so we cannot get here.
  233. DebugAssert(MessageBrowser == NULL);
  234. Text = NormalizeNewLines(Text);
  235. UnicodeString ReportErrorText = NormalizeNewLines(FMTLOAD(REPORT_ERROR, (L"")));
  236. Text = ReplaceStr(Text, ReportErrorText, L"");
  237. Text = Trim(Text);
  238. return Text;
  239. }
  240. //---------------------------------------------------------------------------
  241. void __fastcall TMessageForm::CMDialogKey(TWMKeyDown & Message)
  242. {
  243. // this gets used in WinInterface.cpp SetTimeoutEvents
  244. if (OnKeyDown != NULL)
  245. {
  246. OnKeyDown(this, Message.CharCode, KeyDataToShiftState(Message.KeyData));
  247. }
  248. if (Message.CharCode == VK_MENU)
  249. {
  250. bool AnyButtonWithGrouppedCommandsWithShiftState = false;
  251. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  252. {
  253. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  254. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  255. {
  256. // we should check if there are any commands with shift state,
  257. // but it's bit overkill
  258. AnyButtonWithGrouppedCommandsWithShiftState = true;
  259. break;
  260. }
  261. }
  262. // this is to make Alt only alter button meaning (if there is any
  263. // alternable button) and not popup system menu
  264. if (AnyButtonWithGrouppedCommandsWithShiftState)
  265. {
  266. Message.Result = 1;
  267. UpdateForShiftState();
  268. }
  269. else
  270. {
  271. TForm::Dispatch(&Message);
  272. }
  273. }
  274. else if ((Message.CharCode == VK_UP) || (Message.CharCode == VK_DOWN))
  275. {
  276. // WORKAROUND
  277. // noop to make up/down be passed back to button to allow drop down,
  278. // see TMessageButton::WMGetDlgCode
  279. }
  280. else
  281. {
  282. TForm::Dispatch(&Message);
  283. }
  284. }
  285. //---------------------------------------------------------------------------
  286. int __fastcall TMessageForm::ShowModal()
  287. {
  288. if (IsApplicationMinimized())
  289. {
  290. FShowNoActivate = true;
  291. }
  292. int Result = TForm::ShowModal();
  293. UnhookFormActivation(this);
  294. return Result;
  295. }
  296. //---------------------------------------------------------------------------
  297. void __fastcall TMessageForm::SetZOrder(bool TopMost)
  298. {
  299. // WORKAROUND: If application is minimized,
  300. // swallow call to BringToFront() from TForm::ShowModal()
  301. if (FShowNoActivate && TopMost)
  302. {
  303. // noop
  304. }
  305. else
  306. {
  307. TForm::SetZOrder(TopMost);
  308. }
  309. }
  310. //---------------------------------------------------------------------------
  311. void __fastcall TMessageForm::CMShowingChanged(TMessage & Message)
  312. {
  313. if (Showing && FShowNoActivate)
  314. {
  315. ShowFormNoActivate(this);
  316. }
  317. else
  318. {
  319. TForm::Dispatch(&Message);
  320. }
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TMessageForm::Dispatch(void * Message)
  324. {
  325. TMessage * M = reinterpret_cast<TMessage*>(Message);
  326. if (M->Msg == CM_DIALOGKEY)
  327. {
  328. CMDialogKey(*((TWMKeyDown *)Message));
  329. }
  330. else if (M->Msg == CM_SHOWINGCHANGED)
  331. {
  332. CMShowingChanged(*M);
  333. }
  334. else
  335. {
  336. TForm::Dispatch(Message);
  337. }
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TMessageForm::FormAfterMonitorDpiChanged(TObject *, int OldDPI, int NewDPI)
  341. {
  342. DebugUsedParam2(OldDPI, NewDPI);
  343. if (MessageBrowser != NULL)
  344. {
  345. LoadMessageBrowser();
  346. }
  347. if (NeverAskAgainCheck != NULL)
  348. {
  349. AutoSizeCheckBox(NeverAskAgainCheck);
  350. }
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TMessageForm::CreateParams(TCreateParams & Params)
  354. {
  355. TForm::CreateParams(Params);
  356. if ((Screen != NULL) && (Screen->ActiveForm != NULL) &&
  357. Screen->ActiveForm->HandleAllocated())
  358. {
  359. Params.WndParent = Screen->ActiveForm->Handle;
  360. }
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall TMessageForm::CreateWnd()
  364. {
  365. TForm::CreateWnd();
  366. ApplyColorMode(this);
  367. }
  368. //---------------------------------------------------------------------------
  369. void __fastcall TMessageForm::LoadMessageBrowser()
  370. {
  371. NavigateToUrl(MessageBrowserUrl);
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TMessageForm::DoShow()
  375. {
  376. UseSystemSettingsPost(this);
  377. if (!MessageBrowserUrl.IsEmpty() &&
  378. // Guard against repeated calls to DoOpen()
  379. (MessageBrowser == NULL))
  380. {
  381. // Web Browser component does not seem to work,
  382. // when created before any window is shown.
  383. // I.e. when the message dialog is the first window (like when /update is used).
  384. // So we have to delay its creation until at least the dialog box is shown.
  385. MessageBrowser = CreateBrowserViewer(MessageBrowserPanel, LoadStr(MESSAGE_LOADING));
  386. MessageBrowser->SendToBack();
  387. LoadMessageBrowser();
  388. }
  389. // Need OnShow to be called only after MessageBrowser is created,
  390. // so that the implementation (InsertDonateLink) can call InsertPanelToMessageDialog
  391. TForm::DoShow();
  392. }
  393. //---------------------------------------------------------------------------
  394. void __fastcall TMessageForm::ButtonSubmit(TObject * Sender)
  395. {
  396. unsigned int Answer = 0;
  397. FButtonSubmitEvents[Sender](Sender, Answer);
  398. if (Answer != 0)
  399. {
  400. ModalResult = Answer;
  401. }
  402. }
  403. //---------------------------------------------------------------------------
  404. void __fastcall TMessageForm::MenuItemClick(TObject * Sender)
  405. {
  406. TMenuItem * Item = DebugNotNull(dynamic_cast<TMenuItem *>(Sender));
  407. ModalResult = (Item->Tag & 0xFFFF);
  408. }
  409. //---------------------------------------------------------------------------
  410. void __fastcall TMessageForm::UpdateForShiftStateTimer(TObject * /*Sender*/)
  411. {
  412. // this is needed to reflect shift state, even when we do not have a keyboard
  413. // focus, what happens when drop down menu is popped up
  414. UpdateForShiftState();
  415. }
  416. //---------------------------------------------------------------------------
  417. void __fastcall TMessageForm::ButtonDropDownClick(TObject * /*Sender*/)
  418. {
  419. // as optimization, do not waste time running timer, unless
  420. // user pops up drop down menu. we do not have a way to stop timer, once
  421. // it closes, but functionaly it does not matter
  422. if (FUpdateForShiftStateTimer == NULL)
  423. {
  424. FUpdateForShiftStateTimer = new TTimer(this);
  425. FUpdateForShiftStateTimer->Interval = 50;
  426. FUpdateForShiftStateTimer->OnTimer = UpdateForShiftStateTimer;
  427. }
  428. }
  429. //---------------------------------------------------------------------------
  430. static const ResourceString * Captions[] = { &_SMsgDlgWarning, &_SMsgDlgError, &_SMsgDlgInformation,
  431. &_SMsgDlgConfirm, NULL };
  432. static const wchar_t * ImageNames[] = { L"Warning", L"Error", L"Information",
  433. L"Help Blue", NULL };
  434. const int mcHorzMargin = 8;
  435. const int mcVertMargin = 13;
  436. const int mcHorzSpacing = 12;
  437. const int mcButtonVertMargin = 7;
  438. const int mcButtonSpacing = 5;
  439. // includes mcVertMargin
  440. const int mcMoreMessageHeight = 86;
  441. // approximately what Windows Vista task dialogs use,
  442. // actually they probably has fixed width
  443. const int mcMaxDialogWidth = 340;
  444. const int mcMinDialogWidth = 310;
  445. const int mcMinDialogwithMoreMessagesWidth = 400;
  446. //---------------------------------------------------------------------------
  447. static UnicodeString __fastcall GetKeyNameStr(int Key)
  448. {
  449. wchar_t Buf[MAX_PATH];
  450. LONG VirtualKey = MapVirtualKey(Key, MAPVK_VK_TO_VSC);
  451. VirtualKey <<= 16;
  452. if (GetKeyNameText(VirtualKey, Buf, LENOF(Buf)) > 0)
  453. {
  454. NULL_TERMINATE(Buf);
  455. }
  456. else
  457. {
  458. Buf[0] = L'\0';
  459. }
  460. return Buf;
  461. }
  462. //---------------------------------------------------------------------------
  463. TButton * __fastcall TMessageForm::CreateButton(
  464. UnicodeString Name, UnicodeString Caption, unsigned int Answer,
  465. TButtonSubmitEvent OnSubmit, bool IsTimeoutButton,
  466. int GroupWith, TShiftState GrouppedShiftState, bool ElevationRequired, bool MenuButton,
  467. TAnswerButtons & AnswerButtons, bool HasMoreMessages, int & ButtonWidths)
  468. {
  469. UnicodeString MeasureCaption = Caption;
  470. if (IsTimeoutButton)
  471. {
  472. MeasureCaption = FMTLOAD(TIMEOUT_BUTTON, (MeasureCaption, 99));
  473. }
  474. TRect TextRect;
  475. DrawText(Canvas->Handle,
  476. UnicodeString(MeasureCaption).c_str(), -1,
  477. &TextRect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE |
  478. DrawTextBiDiModeFlagsReadingOnly());
  479. int CurButtonWidth = TextRect.Right - TextRect.Left + ScaleByTextHeightRunTime(this, 16);
  480. if (ElevationRequired)
  481. {
  482. // Elevation icon
  483. CurButtonWidth += ScaleByTextHeightRunTime(this, 16);
  484. }
  485. if (MenuButton)
  486. {
  487. CurButtonWidth += ScaleByTextHeightRunTime(this, 16);
  488. }
  489. TButton * Button = NULL;
  490. if ((GroupWith >= 0) &&
  491. DebugAlwaysTrue(AnswerButtons.find(GroupWith) != AnswerButtons.end()))
  492. {
  493. TButton * GroupWithButton = AnswerButtons[GroupWith];
  494. if (GroupWithButton->DropDownMenu == NULL)
  495. {
  496. GroupWithButton->Style = TCustomButton::bsSplitButton;
  497. GroupWithButton->DropDownMenu = new TPopupMenu(this);
  498. // cannot handle subitems with shift state,
  499. // if the button has its own handler
  500. // (though it may not be the case still here)
  501. DebugAssert(GroupWithButton->OnClick == NULL);
  502. TMenuItem * Item = new TMenuItem(GroupWithButton->DropDownMenu);
  503. GroupWithButton->DropDownMenu->Items->Add(Item);
  504. GroupWithButton->OnDropDownClick = ButtonDropDownClick;
  505. Item->Caption = GroupWithButton->Caption;
  506. Item->OnClick = MenuItemClick;
  507. DebugAssert(GroupWithButton->ModalResult <= 0xFFFF);
  508. Item->Tag = GroupWithButton->ModalResult;
  509. Item->Default = true;
  510. }
  511. TMenuItem * Item = new TMenuItem(GroupWithButton->DropDownMenu);
  512. GroupWithButton->DropDownMenu->Items->Add(Item);
  513. // See ShortCutToText in Vcl.Menus.pas
  514. if (GrouppedShiftState == (TShiftState() << ssAlt))
  515. {
  516. Caption = Caption + L"\t" + GetKeyNameStr(VK_MENU);
  517. }
  518. else if (GrouppedShiftState == (TShiftState() << ssCtrl))
  519. {
  520. Caption = Caption + L"\t" + GetKeyNameStr(VK_CONTROL);
  521. }
  522. else if (GrouppedShiftState == (TShiftState() << ssShift))
  523. {
  524. Caption = Caption + L"\t" + GetKeyNameStr(VK_SHIFT);
  525. }
  526. else
  527. {
  528. // do not support combined shift states yet
  529. DebugAssert(GrouppedShiftState == TShiftState());
  530. }
  531. Item->Caption = Caption;
  532. if (OnSubmit != NULL)
  533. {
  534. Item->OnClick = ButtonSubmit;
  535. FButtonSubmitEvents[Item] = OnSubmit;
  536. }
  537. else
  538. {
  539. Item->OnClick = MenuItemClick;
  540. DebugAssert((Answer <= 0xFFFF) && (GrouppedShiftState.ToInt() <= 0xFFFF));
  541. Item->Tag = Answer + (GrouppedShiftState.ToInt() << 16);
  542. }
  543. // Hard-coded drop down button width (do not know how to ask for system width).
  544. // Also we do not update the max button width for the default groupped
  545. // button caption. We just blindly hope that captions of advanced commands
  546. // are always longer than the caption of simple default command
  547. CurButtonWidth += ScaleByTextHeightRunTime(this, 15);
  548. // never shrink buttons below their default width
  549. if (GroupWithButton->Width < CurButtonWidth)
  550. {
  551. ButtonWidths += CurButtonWidth - GroupWithButton->Width;
  552. GroupWithButton->Width = CurButtonWidth;
  553. }
  554. DebugAssert(!ElevationRequired);
  555. }
  556. else
  557. {
  558. Button = new TMessageButton(this);
  559. Button->Name = Name;
  560. Button->Parent = this;
  561. Button->Caption = Caption;
  562. if (OnSubmit != NULL)
  563. {
  564. Button->OnClick = ButtonSubmit;
  565. FButtonSubmitEvents[Button] = OnSubmit;
  566. }
  567. else
  568. {
  569. Button->ModalResult = Answer;
  570. }
  571. if (HasMoreMessages)
  572. {
  573. Button->Anchors = TAnchors() << akBottom << akLeft;
  574. }
  575. // never shrink buttons below our default UI button with
  576. Button->Width = std::max(ScaleByTextHeightRunTime(this, 80), CurButtonWidth);
  577. Button->ElevationRequired = ElevationRequired;
  578. ButtonWidths += Button->Width;
  579. if (MenuButton)
  580. {
  581. ::MenuButton(Button);
  582. }
  583. }
  584. return Button;
  585. }
  586. //---------------------------------------------------------------------------
  587. TControl * TMessageForm::GetContentsControls()
  588. {
  589. return static_cast<TControl *>(DebugNotNull(MessageBrowser))->Parent;
  590. }
  591. //---------------------------------------------------------------------------
  592. void __fastcall TMessageForm::InsertPanel(TPanel * Panel)
  593. {
  594. if (DebugAlwaysTrue(MessageBrowser != NULL))
  595. {
  596. // we currently use this for updates message box only
  597. TControl * ContentsControl = GetContentsControls();
  598. Panel->Parent = ContentsPanel;
  599. Panel->Width = ContentsControl->Width;
  600. Panel->Left = ContentsControl->Left;
  601. int ContentsBottom = ContentsControl->Top + ContentsControl->Height;
  602. Panel->Top = ContentsBottom + ((ContentsPanel->Height - ContentsBottom) / 2);
  603. Height = Height + Panel->Height;
  604. ContentsPanel->Height = ContentsPanel->Height + Panel->Height;
  605. // The panel itself does not need this, as the ParentBackground (true by default)
  606. // has the same effect, but an eventual TStaticText on the panel
  607. // uses a wrong background color, if panel's ParentColor is not set.
  608. Panel->ParentColor = true;
  609. }
  610. }
  611. //---------------------------------------------------------------------------
  612. int __fastcall TMessageForm::GetContentWidth()
  613. {
  614. int Result = 0;
  615. if (DebugAlwaysTrue(MessageBrowser != NULL))
  616. {
  617. // we currently use this for updates message box only
  618. Result = GetContentsControls()->Width;
  619. }
  620. return Result;
  621. }
  622. //---------------------------------------------------------------------------
  623. static UnicodeString UrlColor(TColor Color)
  624. {
  625. UnicodeString Result = ColorToWebColorStr(Color);
  626. DebugAssert(Result.Length() == 7);
  627. if (DebugAlwaysTrue(StartsStr(L"#", Result)))
  628. {
  629. Result.Delete(1, 1);
  630. }
  631. return Result;
  632. }
  633. //---------------------------------------------------------------------------
  634. void __fastcall TMessageForm::NavigateToUrl(const UnicodeString & Url)
  635. {
  636. if (DebugAlwaysTrue(MessageBrowser != NULL))
  637. {
  638. UnicodeString StyleParams =
  639. FORMAT(L"fontsize=%d&textcolor=%s&backcolor=%s", (Font->Size, UrlColor(Font->Color), UrlColor(GetControlColor(GetContentsControls()))));
  640. UnicodeString FullUrl = AppendUrlParams(Url, StyleParams);
  641. NavigateBrowserToUrl(MessageBrowser, FullUrl);
  642. }
  643. }
  644. //---------------------------------------------------------------------------
  645. void __fastcall AnswerNameAndCaption(
  646. unsigned int Answer, UnicodeString & Name, UnicodeString & Caption)
  647. {
  648. switch (Answer)
  649. {
  650. case qaYes:
  651. Caption = LoadStr(_SMsgDlgYes.Identifier);
  652. Name = YesButtonName;
  653. break;
  654. case qaNo:
  655. Caption = LoadStr(_SMsgDlgNo.Identifier);
  656. Name = L"No";
  657. break;
  658. case qaOK:
  659. Caption = LoadStr(_SMsgDlgOK.Identifier);
  660. Name = OKButtonName;
  661. break;
  662. case qaCancel:
  663. Caption = LoadStr(_SMsgDlgCancel.Identifier);
  664. Name = L"Cancel";
  665. break;
  666. case qaAbort:
  667. Caption = LoadStr(_SMsgDlgAbort.Identifier);
  668. Name = L"Abort";
  669. break;
  670. case qaRetry:
  671. Caption = LoadStr(_SMsgDlgRetry.Identifier);
  672. Name = L"Retry";
  673. break;
  674. case qaIgnore:
  675. Caption = LoadStr(_SMsgDlgIgnore.Identifier);
  676. Name = L"Ignore";
  677. break;
  678. // Own variant to avoid accelerator conflict with "Abort" button.
  679. // Note that as of now, ALL_BUTTON is never actually used,
  680. // because qaAll is always aliased
  681. case qaAll:
  682. Caption = LoadStr(ALL_BUTTON);
  683. Name = L"All";
  684. break;
  685. case qaNoToAll:
  686. Caption = LoadStr(_SMsgDlgNoToAll.Identifier);
  687. Name = L"NoToAll";
  688. break;
  689. // Own variant to avoid accelerator conflict with "Abort" button.
  690. case qaYesToAll:
  691. Caption = LoadStr(YES_TO_ALL_BUTTON);
  692. Name = L"YesToAll";
  693. break;
  694. case qaHelp:
  695. Caption = LoadStr(_SMsgDlgHelp.Identifier);
  696. Name = L"Help";
  697. break;
  698. case qaSkip:
  699. Caption = LoadStr(SKIP_BUTTON);
  700. Name = L"Skip";
  701. break;
  702. case qaReport:
  703. Caption = LoadStr(REPORT_BUTTON);
  704. Name = L"Report";
  705. break;
  706. default:
  707. DebugFail();
  708. throw Exception(L"Undefined answer");
  709. }
  710. }
  711. //---------------------------------------------------------------------------
  712. static int __fastcall CalculateWidthOnCanvas(UnicodeString Text, void * Arg)
  713. {
  714. TCanvas * Canvas = static_cast<TCanvas *>(Arg);
  715. return Canvas->TextWidth(Text);
  716. }
  717. //---------------------------------------------------------------------------
  718. TForm * __fastcall TMessageForm::Create(const UnicodeString & Msg,
  719. TStrings * MoreMessages, TMsgDlgType DlgType, unsigned int Answers,
  720. const TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  721. unsigned int TimeoutAnswer, TButton ** TimeoutButton, const UnicodeString & AImageName,
  722. const UnicodeString & NeverAskAgainCaption, const UnicodeString & MoreMessagesUrl,
  723. TSize MoreMessagesSize, const UnicodeString & CustomCaption)
  724. {
  725. unsigned int DefaultAnswer;
  726. // In general, we should not have Yes and OK on the same query.
  727. // But we do for "hostkey" prompt, where they are aliased and OK is groupped with Yes.
  728. // There we need Yes to be the default.
  729. if (FLAGSET(Answers, qaYes))
  730. {
  731. DefaultAnswer = qaYes;
  732. }
  733. else if (FLAGSET(Answers, qaOK))
  734. {
  735. DefaultAnswer = qaOK;
  736. }
  737. else
  738. {
  739. DefaultAnswer = qaRetry;
  740. }
  741. unsigned int CancelAnswer = ::CancelAnswer(Answers);
  742. if (TimeoutButton != NULL)
  743. {
  744. *TimeoutButton = NULL;
  745. }
  746. TMessageForm * Result = SafeFormCreate<TMessageForm>();
  747. TColor MainInstructionColor;
  748. HFONT MainInstructionFont;
  749. HFONT InstructionFont;
  750. GetInstrutionsTheme(MainInstructionColor, MainInstructionFont, InstructionFont);
  751. // There is probably some theme we can load the load dark mode color from
  752. if (UseDarkModeForControl(Result))
  753. {
  754. MainInstructionColor = GetLinkColor(Result);
  755. }
  756. if (InstructionFont != 0)
  757. {
  758. Result->Font->Handle = InstructionFont;
  759. }
  760. else
  761. {
  762. Result->Font->Assign(Screen->MessageFont);
  763. }
  764. // Can be possibly nul when error occurs before configuration is created
  765. if (Configuration != NULL)
  766. {
  767. Configuration->Usage->Set(L"ThemeMessageFontSize", Result->Font->Size);
  768. }
  769. // make sure we consider sizes of the monitor,
  770. // that is set in DoFormWindowProc(CM_SHOWINGCHANGED) later.
  771. Forms::TMonitor * Monitor = FormMonitor(Result);
  772. bool HasMoreMessages = (MoreMessages != NULL) || !MoreMessagesUrl.IsEmpty();
  773. Result->BiDiMode = Application->BiDiMode;
  774. Result->BorderStyle = bsDialog;
  775. Result->Canvas->Font = Result->Font;
  776. Result->KeyPreview = true;
  777. int HorzMargin = ScaleByTextHeightRunTime(Result, mcHorzMargin);
  778. int VertMargin = ScaleByTextHeightRunTime(Result, mcVertMargin);
  779. int HorzSpacing = ScaleByTextHeightRunTime(Result, mcHorzSpacing);
  780. int ButtonVertMargin = ScaleByTextHeightRunTime(Result, mcButtonVertMargin);
  781. int ButtonWidths = 0;
  782. int ButtonHeight = -1;
  783. std::vector<TButton *> ButtonControls;
  784. TStaticText * LinkControl = NULL;
  785. TAnswerButtons AnswerButtons;
  786. for (unsigned int Answer = qaFirst; Answer <= qaLast; Answer = Answer << 1)
  787. {
  788. if (FLAGSET(Answers, Answer))
  789. {
  790. DebugAssert(Answer != mrCancel);
  791. UnicodeString Caption;
  792. UnicodeString Name;
  793. AnswerNameAndCaption(Answer, Name, Caption);
  794. TButtonSubmitEvent OnSubmit = NULL;
  795. int GroupWith = -1;
  796. TShiftState GrouppedShiftState;
  797. bool ElevationRequired = false;
  798. bool MenuButton = false;
  799. UnicodeString ActionAlias;
  800. if (Aliases != NULL)
  801. {
  802. for (unsigned int i = 0; i < AliasesCount; i++)
  803. {
  804. if (Answer == Aliases[i].Button)
  805. {
  806. if (!Aliases[i].Alias.IsEmpty())
  807. {
  808. Caption = Aliases[i].Alias;
  809. }
  810. OnSubmit = Aliases[i].OnSubmit;
  811. GroupWith = Aliases[i].GroupWith;
  812. GrouppedShiftState = Aliases[i].GrouppedShiftState;
  813. ElevationRequired = Aliases[i].ElevationRequired;
  814. MenuButton = Aliases[i].MenuButton;
  815. ActionAlias = Aliases[i].ActionAlias;
  816. DebugAssert((OnSubmit == NULL) || (GrouppedShiftState == TShiftState()));
  817. break;
  818. }
  819. }
  820. }
  821. // implemented for a one link only for now
  822. if (!ActionAlias.IsEmpty() &&
  823. DebugAlwaysTrue(LinkControl == NULL) &&
  824. DebugAlwaysTrue(OnSubmit != NULL) &&
  825. DebugAlwaysTrue(GroupWith < 0))
  826. {
  827. LinkControl = new TStaticText(Result);
  828. LinkControl->Name = Name;
  829. LinkControl->Caption = ActionAlias;
  830. LinkControl->Alignment = taRightJustify;
  831. LinkControl->Anchors = TAnchors() << akRight << akTop;
  832. LinkControl->OnClick = Result->ButtonSubmit;
  833. Result->FButtonSubmitEvents[LinkControl] = OnSubmit;
  834. }
  835. else
  836. {
  837. // we hope that all grouped-with buttons are for answer with greater
  838. // value that the answer to be grouped with
  839. if (GroupWith >= 0)
  840. {
  841. if (DebugAlwaysFalse(GroupWith >= static_cast<int>(Answer)) ||
  842. DebugAlwaysFalse(Answer == TimeoutAnswer) ||
  843. DebugAlwaysFalse(Answer == DefaultAnswer) ||
  844. DebugAlwaysFalse(Answer == CancelAnswer))
  845. {
  846. GroupWith = -1;
  847. }
  848. }
  849. bool IsTimeoutButton = (TimeoutButton != NULL) && (Answer == TimeoutAnswer);
  850. if (Answer == qaHelp)
  851. {
  852. DebugAssert(OnSubmit == NULL);
  853. OnSubmit = Result->HelpButtonSubmit;
  854. }
  855. if (Answer == qaReport)
  856. {
  857. DebugAssert(OnSubmit == NULL);
  858. OnSubmit = Result->ReportButtonSubmit;
  859. }
  860. TButton * Button = Result->CreateButton(
  861. Name, Caption, Answer,
  862. OnSubmit, IsTimeoutButton, GroupWith, GrouppedShiftState, ElevationRequired, MenuButton,
  863. AnswerButtons, HasMoreMessages, ButtonWidths);
  864. if (Button != NULL)
  865. {
  866. ButtonControls.push_back(Button);
  867. Button->Default = (Answer == DefaultAnswer);
  868. Button->Cancel = (Answer == CancelAnswer);
  869. if (ButtonHeight < 0)
  870. {
  871. ButtonHeight = Button->Height;
  872. }
  873. DebugAssert(ButtonHeight == Button->Height);
  874. AnswerButtons.insert(TAnswerButtons::value_type(Answer, Button));
  875. if (IsTimeoutButton)
  876. {
  877. *TimeoutButton = Button;
  878. }
  879. }
  880. }
  881. }
  882. }
  883. int NeverAskAgainWidth = 0;
  884. int NeverAskAgainBaseWidth = 0;
  885. if (!NeverAskAgainCaption.IsEmpty())
  886. {
  887. NeverAskAgainBaseWidth = CalculateCheckBoxWidth(Result, NeverAskAgainCaption);
  888. NeverAskAgainWidth = NeverAskAgainBaseWidth + ScaleByTextHeightRunTime(Result, 8); // even more margin
  889. }
  890. int ButtonSpacing = ScaleByTextHeightRunTime(Result, mcButtonSpacing);
  891. int ButtonGroupWidth = NeverAskAgainWidth;
  892. if (!ButtonControls.empty())
  893. {
  894. ButtonGroupWidth += ButtonWidths +
  895. ButtonSpacing * (ButtonControls.size() - 1);
  896. }
  897. DebugAssert((ButtonHeight > 0) && (ButtonWidths > 0));
  898. TPanel * Panel = CreateBlankPanel(Result);
  899. Result->ContentsPanel = Panel;
  900. Panel->Name = MessagePanelName;
  901. Panel->Parent = Result;
  902. Panel->ParentBackground = false;
  903. Panel->Anchors = TAnchors() << akLeft << akRight << akTop;
  904. Panel->Caption = L"";
  905. int IconWidth = 0;
  906. int IconHeight = 0;
  907. UnicodeString ImageName = AImageName;
  908. int DlgTypeIndex = static_cast<int>(DlgType);
  909. if (ImageName.IsEmpty() &&
  910. DebugAlwaysTrue(ImageNames[DlgTypeIndex] != NULL))
  911. {
  912. ImageName = ImageNames[DlgTypeIndex];
  913. }
  914. if (DebugAlwaysTrue(!ImageName.IsEmpty()))
  915. {
  916. TImage * Image = new TImage(Result);
  917. Image->Name = L"Image";
  918. Image->Parent = Panel;
  919. LoadDialogImage(Image, ImageName);
  920. Image->SetBounds(HorzMargin, VertMargin, Image->Picture->Width, Image->Picture->Height);
  921. IconWidth = Image->Width + HorzSpacing;
  922. IconHeight = Image->Height;
  923. }
  924. int MaxTextWidth = ScaleByTextHeightRunTime(Result, mcMaxDialogWidth);
  925. // If the message contains SHA-256 hex fingerprint (CERT_TEXT2 on TLS/SSL certificate verification dialog),
  926. // allow wider box to fit it
  927. if (TRegEx::IsMatch(Msg, L"([0-9a-fA-F]{2}[:\\-]){31}[0-9a-fA-F]{2}"))
  928. {
  929. MaxTextWidth = MaxTextWidth * 3 / 2;
  930. }
  931. // if the dialog would be wide anyway (overwrite confirmation on Windows XP),
  932. // to fit the buttons, do not restrict the text
  933. if (MaxTextWidth < ButtonGroupWidth - IconWidth)
  934. {
  935. MaxTextWidth = ButtonGroupWidth - IconWidth;
  936. }
  937. UnicodeString BodyMsg = Msg;
  938. BodyMsg = RemoveInteractiveMsgTag(BodyMsg);
  939. UnicodeString MainMsg;
  940. if (ExtractMainInstructions(BodyMsg, MainMsg))
  941. {
  942. Result->MessageText = MainMsg + BodyMsg;
  943. BodyMsg = BodyMsg.TrimLeft();
  944. }
  945. else
  946. {
  947. Result->MessageText = BodyMsg;
  948. }
  949. ApplyTabs(Result->MessageText, L' ', NULL, NULL);
  950. // Windows XP (not sure about Vista) does not support Hair space.
  951. // For Windows XP, we still keep the existing hack by using hard-coded spaces
  952. // in resource string
  953. if (IsWin7())
  954. {
  955. // Have to be padding with spaces (the smallest space defined, hair space = 1px),
  956. // as tabs actually do not tab, just expand to 8 spaces.
  957. // Otherwise we would have to do custom drawing
  958. // (using GetTabbedTextExtent and TabbedTextOut)
  959. const wchar_t HairSpace = L'\x200A';
  960. ApplyTabs(BodyMsg, HairSpace, CalculateWidthOnCanvas, Result->Canvas);
  961. }
  962. DebugAssert(MainMsg.Pos(L"\t") == 0);
  963. int IconTextWidth = -1;
  964. int IconTextHeight = 0;
  965. int ALeft = IconWidth + HorzMargin;
  966. for (int MessageIndex = 0; MessageIndex <= 1; MessageIndex++)
  967. {
  968. UnicodeString LabelMsg;
  969. UnicodeString LabelName;
  970. TColor LabelColor = Graphics::clNone;
  971. HFONT LabelFont = 0;
  972. switch (MessageIndex)
  973. {
  974. case 0:
  975. LabelMsg = MainMsg;
  976. LabelName = MainMessageLabelName;
  977. LabelColor = MainInstructionColor;
  978. LabelFont = MainInstructionFont;
  979. break;
  980. case 1:
  981. LabelMsg = BodyMsg;
  982. LabelName = MessageLabelName;
  983. break;
  984. default:
  985. DebugFail();
  986. break;
  987. }
  988. if (!LabelMsg.IsEmpty())
  989. {
  990. TLabel * Message = new TLabel(Panel);
  991. Message->Parent = Panel;
  992. Message->Name = LabelName;
  993. Message->WordWrap = true;
  994. Message->Caption = LabelMsg;
  995. Message->BiDiMode = Result->BiDiMode;
  996. // added to show & as & for messages containing !& pattern of custom commands
  997. // (suppose that we actually never want to use & as accel in message text)
  998. Message->ShowAccelChar = false;
  999. if (LabelFont != 0)
  1000. {
  1001. Message->Font->Handle = LabelFont;
  1002. if (DebugAlwaysTrue(LabelFont == MainInstructionFont) &&
  1003. // When showing an early error message
  1004. (Configuration != NULL))
  1005. {
  1006. Configuration->Usage->Set(L"ThemeMainInstructionFontSize", Message->Font->Size);
  1007. }
  1008. }
  1009. if (LabelColor != Graphics::clNone)
  1010. {
  1011. Message->Font->Color = LabelColor;
  1012. }
  1013. TRect TextRect;
  1014. SetRect(&TextRect, 0, 0, MaxTextWidth, 0);
  1015. DrawText(Message->Canvas->Handle, LabelMsg.c_str(), LabelMsg.Length() + 1, &TextRect,
  1016. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX |
  1017. Result->DrawTextBiDiModeFlagsReadingOnly());
  1018. int MaxWidth = Monitor->Width - HorzMargin * 2 - IconWidth - 30;
  1019. // 5% buffer for potential WM_DPICHANGED, as after re-scaling the text can otherwise narrowly not fit in.
  1020. // Though note that the buffer is lost on the first re-scale due to the AutoSize
  1021. TextRect.right = MulDiv(TextRect.right, 105, 100);
  1022. // this will truncate the text, we should implement something smarter eventually
  1023. TextRect.right = Min(TextRect.right, MaxWidth);
  1024. IconTextWidth = Max(IconTextWidth, IconWidth + TextRect.Right);
  1025. if (IconTextHeight > 0)
  1026. {
  1027. IconTextHeight += VertMargin;
  1028. }
  1029. Message->SetBounds(ALeft, VertMargin + IconTextHeight, TextRect.Right, TextRect.Bottom);
  1030. IconTextHeight += TextRect.Bottom;
  1031. }
  1032. }
  1033. if (LinkControl != NULL)
  1034. {
  1035. LinkControl->Parent = Panel;
  1036. LinkActionLabel(LinkControl);
  1037. LinkControl->Left = Panel->ClientWidth - HorzMargin - LinkControl->Width;
  1038. LinkControl->Top = VertMargin + IconTextHeight + VertMargin;
  1039. IconTextHeight += VertMargin + LinkControl->Height;
  1040. }
  1041. DebugAssert((IconTextWidth > 0) && (IconTextHeight > 0));
  1042. IconTextHeight = Max(IconTextHeight, IconHeight);
  1043. int MoreMessageHeight =
  1044. (HasMoreMessages ?
  1045. ScaleByTextHeightRunTime(Result, (MoreMessagesSize.Height > 0 ? MoreMessagesSize.Height : mcMoreMessageHeight)) : 0);
  1046. Panel->SetBounds(0, 0, Result->ClientWidth, VertMargin + IconTextHeight + VertMargin + MoreMessageHeight);
  1047. TControl * MoreMessagesControl = NULL;
  1048. if (HasMoreMessages)
  1049. {
  1050. if (MoreMessages != NULL)
  1051. {
  1052. DebugAssert(MoreMessagesUrl.IsEmpty());
  1053. TMemo * MessageMemo = CreateMemo(Panel);
  1054. MoreMessagesControl = MessageMemo;
  1055. MessageMemo->Name = L"MessageMemo";
  1056. MessageMemo->Parent = Panel;
  1057. MessageMemo->ReadOnly = true;
  1058. MessageMemo->WantReturns = False;
  1059. MessageMemo->ScrollBars = ssVertical;
  1060. MessageMemo->Anchors = TAnchors() << akLeft << akRight << akTop;
  1061. MessageMemo->Lines->Text = MoreMessages->Text;
  1062. Result->MessageMemo = MessageMemo;
  1063. }
  1064. else if (DebugAlwaysTrue(!MoreMessagesUrl.IsEmpty()))
  1065. {
  1066. TPanel * MessageBrowserPanel = CreateBlankPanel(Panel);
  1067. MessageBrowserPanel->Parent = Panel;
  1068. MessageBrowserPanel->Anchors = TAnchors() << akLeft << akRight << akTop;
  1069. MessageBrowserPanel->BevelKind = bkTile; // flat border
  1070. Result->MessageBrowserPanel = MessageBrowserPanel;
  1071. MoreMessagesControl = Result->MessageBrowserPanel;
  1072. Result->MessageBrowserUrl = CampaignUrl(MoreMessagesUrl);
  1073. }
  1074. }
  1075. int MinClientWidth =
  1076. ScaleByTextHeightRunTime(Result,
  1077. HasMoreMessages ? (MoreMessagesSize.Width > 0 ? MoreMessagesSize.Width : mcMinDialogwithMoreMessagesWidth) : mcMinDialogWidth);
  1078. int AClientWidth =
  1079. Max(
  1080. (IconTextWidth > ButtonGroupWidth ? IconTextWidth : ButtonGroupWidth) +
  1081. HorzMargin * 2,
  1082. MinClientWidth);
  1083. Result->ClientWidth = AClientWidth;
  1084. Result->ClientHeight =
  1085. Panel->Height + ButtonVertMargin + ButtonHeight + ButtonVertMargin;
  1086. if (!CustomCaption.IsEmpty())
  1087. {
  1088. Result->Caption = CustomCaption;
  1089. }
  1090. else if (DebugAlwaysTrue(DlgType != mtCustom))
  1091. {
  1092. Result->Caption = LoadResourceString(Captions[DlgTypeIndex]);
  1093. }
  1094. else
  1095. {
  1096. Result->Caption = Application->Title;
  1097. }
  1098. if (MoreMessagesControl != NULL)
  1099. {
  1100. MoreMessagesControl->SetBounds(
  1101. ALeft,
  1102. Panel->Height - MoreMessageHeight,
  1103. Result->ClientWidth - ALeft - HorzMargin,
  1104. MoreMessageHeight - VertMargin);
  1105. }
  1106. int ButtonTop = Panel->Height + ButtonVertMargin;
  1107. int X = Result->ClientWidth - ButtonGroupWidth + NeverAskAgainWidth - HorzMargin;
  1108. for (unsigned int i = 0; i < ButtonControls.size(); i++)
  1109. {
  1110. ButtonControls[i]->SetBounds(
  1111. X, ButtonTop, ButtonControls[i]->Width, ButtonControls[i]->Height);
  1112. X += ButtonControls[i]->Width + ButtonSpacing;
  1113. }
  1114. if (!NeverAskAgainCaption.IsEmpty() &&
  1115. !ButtonControls.empty())
  1116. {
  1117. Result->NeverAskAgainCheck = CreateCheckBox(Result);
  1118. Result->NeverAskAgainCheck->Name = L"NeverAskAgainCheck";
  1119. Result->NeverAskAgainCheck->Parent = Result;
  1120. Result->NeverAskAgainCheck->Caption = NeverAskAgainCaption;
  1121. // Previously we set anchor to akBottom, but as we do not do that for buttons, we removed that.
  1122. // When showing window on 100% DPI monitor, with system DPI 100%, but main monitor 150%,
  1123. // the title bar seems to start on 150% DPI reducing later, leaving the form client height
  1124. // sligtly higher than needed and the checkbox being aligned differently than the button.
  1125. // Removing the akBottom aligning improves this sligtly, while the main problem still should be fixed.
  1126. TButton * FirstButton = ButtonControls[0];
  1127. int NeverAskAgainHeight = ScaleByTextHeightRunTime(Result, Result->NeverAskAgainCheck->Height);
  1128. int NeverAskAgainTop = FirstButton->Top + ((FirstButton->Height - NeverAskAgainHeight) / 2);
  1129. int NeverAskAgainLeft = HorzMargin + ScaleByTextHeightRunTime(Result, 2); // checkbox indentation
  1130. Result->NeverAskAgainCheck->SetBounds(
  1131. NeverAskAgainLeft, NeverAskAgainTop, NeverAskAgainBaseWidth, NeverAskAgainHeight);
  1132. }
  1133. return Result;
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. TForm * __fastcall CreateMoreMessageDialog(const UnicodeString & Msg,
  1137. TStrings * MoreMessages, TMsgDlgType DlgType, unsigned int Answers,
  1138. const TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  1139. unsigned int TimeoutAnswer, TButton ** TimeoutButton, const UnicodeString & ImageName,
  1140. const UnicodeString & NeverAskAgainCaption, const UnicodeString & MoreMessagesUrl,
  1141. TSize MoreMessagesSize, const UnicodeString & CustomCaption)
  1142. {
  1143. return TMessageForm::Create(Msg, MoreMessages, DlgType, Answers,
  1144. Aliases, AliasesCount, TimeoutAnswer, TimeoutButton, ImageName,
  1145. NeverAskAgainCaption, MoreMessagesUrl, MoreMessagesSize, CustomCaption);
  1146. }
  1147. //---------------------------------------------------------------------------
  1148. void __fastcall InsertPanelToMessageDialog(TCustomForm * Form, TPanel * Panel)
  1149. {
  1150. TMessageForm * MessageForm = DebugNotNull(dynamic_cast<TMessageForm *>(Form));
  1151. MessageForm->InsertPanel(Panel);
  1152. }
  1153. //---------------------------------------------------------------------------
  1154. void __fastcall NavigateMessageDialogToUrl(TCustomForm * Form, const UnicodeString & Url)
  1155. {
  1156. TMessageForm * MessageForm = DebugNotNull(dynamic_cast<TMessageForm *>(Form));
  1157. MessageForm->NavigateToUrl(Url);
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. int __fastcall GetMessageDialogContentWidth(TCustomForm * Form)
  1161. {
  1162. TMessageForm * MessageForm = DebugNotNull(dynamic_cast<TMessageForm *>(Form));
  1163. return MessageForm->GetContentWidth();
  1164. }