MessageDlg.cpp 41 KB

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