MessageDlg.cpp 41 KB

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