MessageDlg.cpp 41 KB

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