MessageDlg.cpp 42 KB

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