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 <Setup.h>
  19. #include <WinApi.h>
  20. #include "MessageDlg.h"
  21. //---------------------------------------------------------------------------
  22. #ifndef NO_RESOURCES
  23. #pragma resource "*.dfm"
  24. #endif
  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. FUpdateForShiftStateTimer = NULL;
  78. UseSystemSettingsPre(this);
  79. FDummyForm = new TForm(this);
  80. UseSystemSettings(FDummyForm);
  81. }
  82. //---------------------------------------------------------------------------
  83. __fastcall TMessageForm::~TMessageForm()
  84. {
  85. SAFE_DESTROY(FDummyForm);
  86. SAFE_DESTROY(FUpdateForShiftStateTimer);
  87. }
  88. //---------------------------------------------------------------------------
  89. void __fastcall TMessageForm::HelpButtonSubmit(TObject * /*Sender*/, unsigned int & /*Answer*/)
  90. {
  91. if (HelpKeyword != HELP_NONE)
  92. {
  93. FormHelp(this);
  94. }
  95. else
  96. {
  97. MessageWithNoHelp(GetReportText());
  98. }
  99. }
  100. //---------------------------------------------------------------------------
  101. void __fastcall TMessageForm::ReportButtonSubmit(TObject * /*Sender*/, unsigned int & /*Answer*/)
  102. {
  103. // Report text goes last, as it may exceed URL parameters limit (2048) and get truncated.
  104. // And we need to preserve the other parameters.
  105. UnicodeString Url =
  106. FMTLOAD(ERROR_REPORT_URL2,
  107. (Configuration->ProductVersion, GUIConfiguration->AppliedLocaleHex,
  108. EncodeUrlString(GetReportText())));
  109. OpenBrowser(Url);
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TMessageForm::UpdateForShiftState()
  113. {
  114. TShiftState ShiftState =
  115. KeyboardStateToShiftState() * AllKeyShiftStates();
  116. if (FShiftState != ShiftState)
  117. {
  118. FShiftState = ShiftState;
  119. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  120. {
  121. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  122. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  123. {
  124. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  125. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  126. {
  127. TMenuItem * Item = MenuItems->Items[ItemIndex];
  128. TShiftState GrouppedShiftState(Item->Tag >> 16);
  129. if (Item->Enabled &&
  130. ((ShiftState.Empty() && Item->Default) ||
  131. (!ShiftState.Empty() && (ShiftState == GrouppedShiftState))))
  132. {
  133. Button->Caption = CopyToChar(Item->Caption, L'\t', false);
  134. Button->ModalResult = Item->Tag & 0xFFFF;
  135. DebugAssert(Button->OnClick == NULL);
  136. DebugAssert(Item->OnClick == MenuItemClick);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. //---------------------------------------------------------------------------
  145. void __fastcall TMessageForm::KeyUp(Word & Key, TShiftState Shift)
  146. {
  147. UpdateForShiftState();
  148. TForm::KeyUp(Key, Shift);
  149. }
  150. //---------------------------------------------------------------------------
  151. void __fastcall TMessageForm::KeyDown(Word & Key, TShiftState Shift)
  152. {
  153. if (Shift.Contains(ssCtrl) && (Key == L'C'))
  154. {
  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 = UnicodeString::StringOfChar(L'-', 27) + 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)
  216. {
  217. MessageBrowser->SelectAll();
  218. MessageBrowser->CopyToClipBoard();
  219. if (NonEmptyTextFromClipboard(MoreMessages))
  220. {
  221. if (!EndsStr(sLineBreak, MoreMessages))
  222. {
  223. MoreMessages += sLineBreak;
  224. }
  225. MoreMessages += DividerLine;
  226. }
  227. MessageBrowser->DoCommand(L"UNSELECT");
  228. }
  229. UnicodeString MessageCaption = NormalizeNewLines(MessageText);
  230. UnicodeString Result = FORMAT(L"%s%s%s%s%s%s%s%s%s%s%s", (DividerLine, Caption, sLineBreak,
  231. DividerLine, MessageCaption, sLineBreak, DividerLine, MoreMessages,
  232. ButtonCaptions, sLineBreak, DividerLine));
  233. return Result;
  234. }
  235. //---------------------------------------------------------------------------
  236. UnicodeString __fastcall TMessageForm::GetReportText()
  237. {
  238. UnicodeString Text = MessageText;
  239. Text = Text.TrimRight();
  240. if (MessageMemo != NULL)
  241. {
  242. Text += L"\n\n" + MessageMemo->Text;
  243. }
  244. // Currently we use browser for updates box only and it has help context,
  245. // and does not have Report button, so we cannot get here.
  246. DebugAssert(MessageBrowser == NULL);
  247. Text = NormalizeNewLines(Text);
  248. UnicodeString ReportErrorText = NormalizeNewLines(FMTLOAD(REPORT_ERROR, (L"")));
  249. Text = ReplaceStr(Text, ReportErrorText, L"");
  250. Text = Trim(Text);
  251. return Text;
  252. }
  253. //---------------------------------------------------------------------------
  254. void __fastcall TMessageForm::CMDialogKey(TWMKeyDown & Message)
  255. {
  256. // this gets used in WinInterface.cpp SetTimeoutEvents
  257. if (OnKeyDown != NULL)
  258. {
  259. OnKeyDown(this, Message.CharCode, KeyDataToShiftState(Message.KeyData));
  260. }
  261. if (Message.CharCode == VK_MENU)
  262. {
  263. bool AnyButtonWithGrouppedCommandsWithShiftState = false;
  264. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  265. {
  266. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  267. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  268. {
  269. // we should check if there are any commands with shift state,
  270. // but it's bit overkill
  271. AnyButtonWithGrouppedCommandsWithShiftState = true;
  272. break;
  273. }
  274. }
  275. // this is to make Alt only alter button meaning (if there is any
  276. // alternable button) and not popup system menu
  277. if (AnyButtonWithGrouppedCommandsWithShiftState)
  278. {
  279. Message.Result = 1;
  280. UpdateForShiftState();
  281. }
  282. else
  283. {
  284. TForm::Dispatch(&Message);
  285. }
  286. }
  287. else if ((Message.CharCode == VK_UP) || (Message.CharCode == VK_DOWN))
  288. {
  289. // WORKAROUND
  290. // noop to make up/down be passed back to button to allow drop down,
  291. // see TMessageButton::WMGetDlgCode
  292. }
  293. else
  294. {
  295. TForm::Dispatch(&Message);
  296. }
  297. }
  298. //---------------------------------------------------------------------------
  299. int __fastcall TMessageForm::ShowModal()
  300. {
  301. if (IsApplicationMinimized())
  302. {
  303. FShowNoActivate = true;
  304. }
  305. int Result = TForm::ShowModal();
  306. UnhookFormActivation(this);
  307. return Result;
  308. }
  309. //---------------------------------------------------------------------------
  310. void __fastcall TMessageForm::SetZOrder(bool TopMost)
  311. {
  312. // WORKAROUND: If application is minimized,
  313. // swallow call to BringToFront() from TForm::ShowModal()
  314. if (FShowNoActivate && TopMost)
  315. {
  316. // noop
  317. }
  318. else
  319. {
  320. TForm::SetZOrder(TopMost);
  321. }
  322. }
  323. //---------------------------------------------------------------------------
  324. void __fastcall TMessageForm::CMShowingChanged(TMessage & Message)
  325. {
  326. if (Showing && FShowNoActivate)
  327. {
  328. ShowFormNoActivate(this);
  329. }
  330. else
  331. {
  332. TForm::Dispatch(&Message);
  333. }
  334. }
  335. //---------------------------------------------------------------------------
  336. void __fastcall TMessageForm::Dispatch(void * Message)
  337. {
  338. TMessage * M = reinterpret_cast<TMessage*>(Message);
  339. if (M->Msg == CM_DIALOGKEY)
  340. {
  341. CMDialogKey(*((TWMKeyDown *)Message));
  342. }
  343. else if (M->Msg == CM_SHOWINGCHANGED)
  344. {
  345. CMShowingChanged(*M);
  346. }
  347. else if (M->Msg == CM_DPICHANGED)
  348. {
  349. if (MessageBrowser != NULL)
  350. {
  351. LoadMessageBrowser();
  352. }
  353. TForm::Dispatch(Message);
  354. }
  355. else
  356. {
  357. TForm::Dispatch(Message);
  358. }
  359. }
  360. //---------------------------------------------------------------------------
  361. void __fastcall TMessageForm::CreateParams(TCreateParams & Params)
  362. {
  363. TForm::CreateParams(Params);
  364. if ((Screen != NULL) && (Screen->ActiveForm != NULL) &&
  365. Screen->ActiveForm->HandleAllocated())
  366. {
  367. Params.WndParent = Screen->ActiveForm->Handle;
  368. }
  369. }
  370. //---------------------------------------------------------------------------
  371. void __fastcall TMessageForm::LoadMessageBrowser()
  372. {
  373. NavigateToUrl(MessageBrowserUrl);
  374. }
  375. //---------------------------------------------------------------------------
  376. void __fastcall TMessageForm::DoShow()
  377. {
  378. UseSystemSettingsPost(this);
  379. if (!MessageBrowserUrl.IsEmpty() &&
  380. // Guard against repeated calls to DoOpen()
  381. (MessageBrowser == NULL))
  382. {
  383. // Web Browser component does not seem to work,
  384. // when created before any window is shown.
  385. // I.e. when the message dialog is the first window (like when /update is used).
  386. // So we have to delay its creation until at least the dialog box is shown.
  387. MessageBrowser = CreateBrowserViewer(MessageBrowserPanel, LoadStr(MESSAGE_LOADING));
  388. MessageBrowser->SendToBack();
  389. LoadMessageBrowser();
  390. }
  391. // Need OnShow to be called only after MessageBrowser is created,
  392. // so that the implementation (InsertDonateLink) can call InsertPanelToMessageDialog
  393. TForm::DoShow();
  394. }
  395. //---------------------------------------------------------------------------
  396. void __fastcall TMessageForm::ButtonSubmit(TObject * Sender)
  397. {
  398. unsigned int Answer = 0;
  399. FButtonSubmitEvents[Sender](Sender, Answer);
  400. if (Answer != 0)
  401. {
  402. ModalResult = Answer;
  403. }
  404. }
  405. //---------------------------------------------------------------------------
  406. void __fastcall TMessageForm::MenuItemClick(TObject * Sender)
  407. {
  408. TMenuItem * Item = DebugNotNull(dynamic_cast<TMenuItem *>(Sender));
  409. ModalResult = (Item->Tag & 0xFFFF);
  410. }
  411. //---------------------------------------------------------------------------
  412. void __fastcall TMessageForm::UpdateForShiftStateTimer(TObject * /*Sender*/)
  413. {
  414. // this is needed to reflect shift state, even when we do not have a keyboard
  415. // focus, what happens when drop down menu is popped up
  416. UpdateForShiftState();
  417. }
  418. //---------------------------------------------------------------------------
  419. void __fastcall TMessageForm::ButtonDropDownClick(TObject * /*Sender*/)
  420. {
  421. // as optimization, do not waste time running timer, unless
  422. // user pops up drop down menu. we do not have a way to stop timer, once
  423. // it closes, but functionaly it does not matter
  424. if (FUpdateForShiftStateTimer == NULL)
  425. {
  426. FUpdateForShiftStateTimer = new TTimer(this);
  427. FUpdateForShiftStateTimer->Interval = 50;
  428. FUpdateForShiftStateTimer->OnTimer = UpdateForShiftStateTimer;
  429. }
  430. }
  431. //---------------------------------------------------------------------------
  432. const ResourceString * Captions[] = { &_SMsgDlgWarning, &_SMsgDlgError, &_SMsgDlgInformation,
  433. &_SMsgDlgConfirm, NULL };
  434. const wchar_t * ImageNames[] = { L"Warning", L"Error", L"Information",
  435. L"Help Blue", NULL };
  436. const int mcHorzMargin = 10;
  437. const int mcVertMargin = 13;
  438. const int mcHorzSpacing = 12;
  439. const int mcButtonVertMargin = 7;
  440. const int mcButtonSpacing = 5;
  441. // includes mcVertMargin
  442. const int mcMoreMessageHeight = 86;
  443. // approximately what Windows Vista task dialogs use,
  444. // actually they probably has fixed width
  445. const int mcMaxDialogWidth = 340;
  446. const int mcMinDialogWidth = 310;
  447. const int mcMinDialogwithMoreMessagesWidth = 400;
  448. //---------------------------------------------------------------------------
  449. static UnicodeString __fastcall GetKeyNameStr(int Key)
  450. {
  451. wchar_t Buf[MAX_PATH];
  452. LONG VirtualKey = MapVirtualKey(Key, MAPVK_VK_TO_VSC);
  453. VirtualKey <<= 16;
  454. if (GetKeyNameText(VirtualKey, Buf, LENOF(Buf)) > 0)
  455. {
  456. NULL_TERMINATE(Buf);
  457. }
  458. else
  459. {
  460. Buf[0] = L'\0';
  461. }
  462. return Buf;
  463. }
  464. //---------------------------------------------------------------------------
  465. TButton * __fastcall TMessageForm::CreateButton(
  466. UnicodeString Name, UnicodeString Caption, unsigned int Answer,
  467. TButtonSubmitEvent OnSubmit, bool IsTimeoutButton,
  468. int GroupWith, TShiftState GrouppedShiftState, bool ElevationRequired, bool MenuButton,
  469. TAnswerButtons & AnswerButtons, bool HasMoreMessages, int & ButtonWidths)
  470. {
  471. UnicodeString MeasureCaption = Caption;
  472. if (IsTimeoutButton)
  473. {
  474. MeasureCaption = FMTLOAD(TIMEOUT_BUTTON, (MeasureCaption, 99));
  475. }
  476. TRect TextRect;
  477. DrawText(Canvas->Handle,
  478. UnicodeString(MeasureCaption).c_str(), -1,
  479. &TextRect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE |
  480. DrawTextBiDiModeFlagsReadingOnly());
  481. int CurButtonWidth = TextRect.Right - TextRect.Left + ScaleByTextHeightRunTime(this, 16);
  482. if (ElevationRequired && IsVista())
  483. {
  484. // Elevation icon
  485. CurButtonWidth += ScaleByTextHeightRunTime(this, 16);
  486. }
  487. if (MenuButton)
  488. {
  489. CurButtonWidth += ScaleByTextHeightRunTime(this, 16);
  490. }
  491. TButton * Button = NULL;
  492. if (SupportsSplitButton() &&
  493. (GroupWith >= 0) &&
  494. DebugAlwaysTrue(AnswerButtons.find(GroupWith) != AnswerButtons.end()))
  495. {
  496. TButton * GroupWithButton = AnswerButtons[GroupWith];
  497. if (GroupWithButton->DropDownMenu == NULL)
  498. {
  499. GroupWithButton->Style = TCustomButton::bsSplitButton;
  500. GroupWithButton->DropDownMenu = new TPopupMenu(this);
  501. // cannot handle subitems with shift state,
  502. // if the button has its own handler
  503. // (though it may not be the case still here)
  504. DebugAssert(GroupWithButton->OnClick == NULL);
  505. TMenuItem * Item = new TMenuItem(GroupWithButton->DropDownMenu);
  506. GroupWithButton->DropDownMenu->Items->Add(Item);
  507. GroupWithButton->OnDropDownClick = ButtonDropDownClick;
  508. Item->Caption = GroupWithButton->Caption;
  509. Item->OnClick = MenuItemClick;
  510. DebugAssert(GroupWithButton->ModalResult <= 0xFFFF);
  511. Item->Tag = GroupWithButton->ModalResult;
  512. Item->Default = true;
  513. }
  514. TMenuItem * Item = new TMenuItem(GroupWithButton->DropDownMenu);
  515. GroupWithButton->DropDownMenu->Items->Add(Item);
  516. // See ShortCutToText in Vcl.Menus.pas
  517. if (GrouppedShiftState == (TShiftState() << ssAlt))
  518. {
  519. Caption = Caption + L"\t" + GetKeyNameStr(VK_MENU);
  520. }
  521. else if (GrouppedShiftState == (TShiftState() << ssCtrl))
  522. {
  523. Caption = Caption + L"\t" + GetKeyNameStr(VK_CONTROL);
  524. }
  525. else if (GrouppedShiftState == (TShiftState() << ssShift))
  526. {
  527. Caption = Caption + L"\t" + GetKeyNameStr(VK_SHIFT);
  528. }
  529. else
  530. {
  531. // do not support combined shift states yet
  532. DebugAssert(GrouppedShiftState == TShiftState());
  533. }
  534. Item->Caption = Caption;
  535. if (OnSubmit != NULL)
  536. {
  537. Item->OnClick = ButtonSubmit;
  538. FButtonSubmitEvents[Item] = OnSubmit;
  539. }
  540. else
  541. {
  542. Item->OnClick = MenuItemClick;
  543. DebugAssert((Answer <= 0xFFFF) && (GrouppedShiftState.ToInt() <= 0xFFFF));
  544. Item->Tag = Answer + (GrouppedShiftState.ToInt() << 16);
  545. }
  546. // Hard-coded drop down button width (do not know how to ask for system width).
  547. // Also we do not update the max button width for the default groupped
  548. // button caption. We just blindly hope that captions of advanced commands
  549. // are always longer than the caption of simple default command
  550. CurButtonWidth += ScaleByTextHeightRunTime(this, 15);
  551. // never shrink buttons below their default width
  552. if (GroupWithButton->Width < CurButtonWidth)
  553. {
  554. ButtonWidths += CurButtonWidth - GroupWithButton->Width;
  555. GroupWithButton->Width = CurButtonWidth;
  556. }
  557. DebugAssert(!ElevationRequired);
  558. }
  559. else
  560. {
  561. Button = new TMessageButton(this);
  562. Button->Name = Name;
  563. Button->Parent = this;
  564. Button->Caption = Caption;
  565. // Scale buttons using regular font, so that they are as large as buttons
  566. // on other dialogs (note that they are still higher than Windows Task dialog
  567. // buttons)
  568. Button->Height = ScaleByTextHeightRunTime(FDummyForm, Button->Height);
  569. Button->Width = ScaleByTextHeightRunTime(FDummyForm, Button->Width);
  570. if (OnSubmit != NULL)
  571. {
  572. Button->OnClick = ButtonSubmit;
  573. FButtonSubmitEvents[Button] = OnSubmit;
  574. }
  575. else
  576. {
  577. Button->ModalResult = Answer;
  578. }
  579. if (HasMoreMessages)
  580. {
  581. Button->Anchors = TAnchors() << akBottom << akLeft;
  582. }
  583. // never shrink buttons below their default width
  584. if (Button->Width < CurButtonWidth)
  585. {
  586. Button->Width = CurButtonWidth;
  587. }
  588. Button->ElevationRequired = ElevationRequired;
  589. ButtonWidths += Button->Width;
  590. if (MenuButton)
  591. {
  592. ::MenuButton(Button);
  593. }
  594. }
  595. return Button;
  596. }
  597. //---------------------------------------------------------------------------
  598. void __fastcall TMessageForm::InsertPanel(TPanel * Panel)
  599. {
  600. if (DebugAlwaysTrue(MessageBrowser != NULL))
  601. {
  602. // we currently use this for updates message box only
  603. TControl * ContentsControl = static_cast<TControl *>(DebugNotNull(MessageBrowser))->Parent;
  604. Panel->Width = ContentsControl->Width;
  605. Panel->Left = ContentsControl->Left;
  606. int ContentsBottom = ContentsControl->Top + ContentsControl->Height;
  607. Panel->Top = ContentsBottom + ((ContentsPanel->Height - ContentsBottom) / 2);
  608. Height = Height + Panel->Height;
  609. ContentsPanel->Height = ContentsPanel->Height + Panel->Height;
  610. Panel->Parent = ContentsPanel;
  611. // The panel itself does not need this, as the ParentBackground (true by default)
  612. // has the same effect, but an eventual TStaticText on the panel
  613. // uses a wrong background color, if panel's ParentColor is not set.
  614. Panel->ParentColor = true;
  615. }
  616. }
  617. //---------------------------------------------------------------------------
  618. int __fastcall TMessageForm::GetContentWidth()
  619. {
  620. int Result = 0;
  621. if (DebugAlwaysTrue(MessageBrowser != NULL))
  622. {
  623. // we currently use this for updates message box only
  624. TControl * ContentsControl = static_cast<TControl *>(DebugNotNull(MessageBrowser))->Parent;
  625. Result = ContentsControl->Width;
  626. }
  627. return Result;
  628. }
  629. //---------------------------------------------------------------------------
  630. void __fastcall TMessageForm::NavigateToUrl(const UnicodeString & Url)
  631. {
  632. if (DebugAlwaysTrue(MessageBrowser != NULL))
  633. {
  634. UnicodeString FontSizeParam = FORMAT(L"fontsize=%d", (Font->Size));
  635. UnicodeString FullUrl = AppendUrlParams(Url, FontSizeParam);
  636. NavigateBrowserToUrl(MessageBrowser, FullUrl);
  637. }
  638. }
  639. //---------------------------------------------------------------------------
  640. void __fastcall TMessageForm::ReadState(TReader * Reader)
  641. {
  642. TForm::ReadState(Reader);
  643. // Before we change form font
  644. RecordFormImplicitRescale(this);
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall AnswerNameAndCaption(
  648. unsigned int Answer, UnicodeString & Name, UnicodeString & Caption)
  649. {
  650. switch (Answer)
  651. {
  652. case qaYes:
  653. Caption = LoadStr(_SMsgDlgYes.Identifier);
  654. Name = YesButtonName;
  655. break;
  656. case qaNo:
  657. Caption = LoadStr(_SMsgDlgNo.Identifier);
  658. Name = L"No";
  659. break;
  660. case qaOK:
  661. Caption = LoadStr(_SMsgDlgOK.Identifier);
  662. Name = OKButtonName;
  663. break;
  664. case qaCancel:
  665. Caption = LoadStr(_SMsgDlgCancel.Identifier);
  666. Name = L"Cancel";
  667. break;
  668. case qaAbort:
  669. Caption = LoadStr(_SMsgDlgAbort.Identifier);
  670. Name = L"Abort";
  671. break;
  672. case qaRetry:
  673. Caption = LoadStr(_SMsgDlgRetry.Identifier);
  674. Name = L"Retry";
  675. break;
  676. case qaIgnore:
  677. Caption = LoadStr(_SMsgDlgIgnore.Identifier);
  678. Name = L"Ignore";
  679. break;
  680. // Own variant to avoid accelerator conflict with "Abort" button.
  681. // Note that as of now, ALL_BUTTON is never actually used,
  682. // because qaAll is always aliased
  683. case qaAll:
  684. Caption = LoadStr(ALL_BUTTON);
  685. Name = L"All";
  686. break;
  687. case qaNoToAll:
  688. Caption = LoadStr(_SMsgDlgNoToAll.Identifier);
  689. Name = L"NoToAll";
  690. break;
  691. // Own variant to avoid accelerator conflict with "Abort" button.
  692. case qaYesToAll:
  693. Caption = LoadStr(YES_TO_ALL_BUTTON);
  694. Name = L"YesToAll";
  695. break;
  696. case qaHelp:
  697. Caption = LoadStr(_SMsgDlgHelp.Identifier);
  698. Name = L"Help";
  699. break;
  700. case qaSkip:
  701. Caption = LoadStr(SKIP_BUTTON);
  702. Name = L"Skip";
  703. break;
  704. case qaReport:
  705. Caption = LoadStr(REPORT_BUTTON);
  706. Name = L"Report";
  707. break;
  708. default:
  709. DebugFail();
  710. throw Exception(L"Undefined answer");
  711. }
  712. }
  713. //---------------------------------------------------------------------------
  714. static int __fastcall CalculateWidthOnCanvas(UnicodeString Text, void * Arg)
  715. {
  716. TCanvas * Canvas = static_cast<TCanvas *>(Arg);
  717. return Canvas->TextWidth(Text);
  718. }
  719. //---------------------------------------------------------------------------
  720. TForm * __fastcall TMessageForm::Create(const UnicodeString & Msg,
  721. TStrings * MoreMessages, TMsgDlgType DlgType, unsigned int Answers,
  722. const TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  723. unsigned int TimeoutAnswer, TButton ** TimeoutButton, const UnicodeString & AImageName,
  724. const UnicodeString & NeverAskAgainCaption, const UnicodeString & MoreMessagesUrl,
  725. TSize MoreMessagesSize, const UnicodeString & CustomCaption)
  726. {
  727. unsigned int DefaultAnswer;
  728. if (FLAGSET(Answers, qaOK))
  729. {
  730. DefaultAnswer = qaOK;
  731. }
  732. else if (FLAGSET(Answers, qaYes))
  733. {
  734. DefaultAnswer = qaYes;
  735. }
  736. else
  737. {
  738. DefaultAnswer = qaRetry;
  739. }
  740. unsigned int CancelAnswer = ::CancelAnswer(Answers);
  741. if (TimeoutButton != NULL)
  742. {
  743. *TimeoutButton = NULL;
  744. }
  745. TColor MainInstructionColor;
  746. HFONT MainInstructionFont;
  747. HFONT InstructionFont;
  748. GetInstrutionsTheme(MainInstructionColor, MainInstructionFont, InstructionFont);
  749. TMessageForm * Result = SafeFormCreate<TMessageForm>();
  750. if (InstructionFont != 0)
  751. {
  752. Result->Font->Handle = InstructionFont;
  753. }
  754. else
  755. {
  756. Result->Font->Assign(Screen->MessageFont);
  757. }
  758. // Can be possibly nul when error occurs before configuration is created
  759. if (Configuration != NULL)
  760. {
  761. Configuration->Usage->Set(L"ThemeMessageFontSize", Result->Font->Size);
  762. }
  763. // make sure we consider sizes of the monitor,
  764. // that is set in DoFormWindowProc(CM_SHOWINGCHANGED) later.
  765. Forms::TMonitor * Monitor = FormMonitor(Result);
  766. bool HasMoreMessages = (MoreMessages != NULL) || !MoreMessagesUrl.IsEmpty();
  767. Result->BiDiMode = Application->BiDiMode;
  768. Result->BorderStyle = bsDialog;
  769. Result->Canvas->Font = Result->Font;
  770. Result->KeyPreview = true;
  771. int HorzMargin = ScaleByTextHeightRunTime(Result, mcHorzMargin);
  772. int VertMargin = ScaleByTextHeightRunTime(Result, mcVertMargin);
  773. int HorzSpacing = ScaleByTextHeightRunTime(Result, mcHorzSpacing);
  774. int ButtonVertMargin = ScaleByTextHeightRunTime(Result, mcButtonVertMargin);
  775. int ButtonWidths = 0;
  776. int ButtonHeight = -1;
  777. std::vector<TButton *> ButtonControls;
  778. TStaticText * LinkControl = NULL;
  779. TAnswerButtons AnswerButtons;
  780. for (unsigned int Answer = qaFirst; Answer <= qaLast; Answer = Answer << 1)
  781. {
  782. if (FLAGSET(Answers, Answer))
  783. {
  784. DebugAssert(Answer != mrCancel);
  785. UnicodeString Caption;
  786. UnicodeString Name;
  787. AnswerNameAndCaption(Answer, Name, Caption);
  788. TButtonSubmitEvent OnSubmit = NULL;
  789. int GroupWith = -1;
  790. TShiftState GrouppedShiftState;
  791. bool ElevationRequired = false;
  792. bool MenuButton = false;
  793. UnicodeString ActionAlias;
  794. if (Aliases != NULL)
  795. {
  796. for (unsigned int i = 0; i < AliasesCount; i++)
  797. {
  798. if (Answer == Aliases[i].Button)
  799. {
  800. if (!Aliases[i].Alias.IsEmpty())
  801. {
  802. Caption = Aliases[i].Alias;
  803. }
  804. OnSubmit = Aliases[i].OnSubmit;
  805. GroupWith = Aliases[i].GroupWith;
  806. GrouppedShiftState = Aliases[i].GrouppedShiftState;
  807. ElevationRequired = Aliases[i].ElevationRequired;
  808. MenuButton = Aliases[i].MenuButton;
  809. ActionAlias = Aliases[i].ActionAlias;
  810. DebugAssert((OnSubmit == NULL) || (GrouppedShiftState == TShiftState()));
  811. break;
  812. }
  813. }
  814. }
  815. // implemented for a one link only for now
  816. if (!ActionAlias.IsEmpty() &&
  817. DebugAlwaysTrue(LinkControl == NULL) &&
  818. DebugAlwaysTrue(OnSubmit != NULL) &&
  819. DebugAlwaysTrue(GroupWith < 0))
  820. {
  821. LinkControl = new TStaticText(Result);
  822. LinkControl->Name = Name;
  823. LinkControl->Caption = ActionAlias;
  824. LinkControl->Alignment = taRightJustify;
  825. LinkControl->Anchors = TAnchors() << akRight << akTop;
  826. LinkActionLabel(LinkControl);
  827. LinkControl->OnClick = Result->ButtonSubmit;
  828. Result->FButtonSubmitEvents[LinkControl] = OnSubmit;
  829. }
  830. else
  831. {
  832. // we hope that all grouped-with buttons are for answer with greater
  833. // value that the answer to be grouped with
  834. if (GroupWith >= 0)
  835. {
  836. if (DebugAlwaysFalse(GroupWith >= static_cast<int>(Answer)) ||
  837. DebugAlwaysFalse(Answer == TimeoutAnswer) &&
  838. DebugAlwaysFalse(Answer == DefaultAnswer) &&
  839. DebugAlwaysFalse(Answer == CancelAnswer))
  840. {
  841. GroupWith = -1;
  842. }
  843. }
  844. bool IsTimeoutButton = (TimeoutButton != NULL) && (Answer == TimeoutAnswer);
  845. if (Answer == qaHelp)
  846. {
  847. DebugAssert(OnSubmit == NULL);
  848. OnSubmit = Result->HelpButtonSubmit;
  849. }
  850. if (Answer == qaReport)
  851. {
  852. DebugAssert(OnSubmit == NULL);
  853. OnSubmit = Result->ReportButtonSubmit;
  854. }
  855. TButton * Button = Result->CreateButton(
  856. Name, Caption, Answer,
  857. OnSubmit, IsTimeoutButton, GroupWith, GrouppedShiftState, ElevationRequired, MenuButton,
  858. AnswerButtons, HasMoreMessages, ButtonWidths);
  859. if (Button != NULL)
  860. {
  861. ButtonControls.push_back(Button);
  862. Button->Default = (Answer == DefaultAnswer);
  863. Button->Cancel = (Answer == CancelAnswer);
  864. if (ButtonHeight < 0)
  865. {
  866. ButtonHeight = Button->Height;
  867. }
  868. DebugAssert(ButtonHeight == Button->Height);
  869. AnswerButtons.insert(TAnswerButtons::value_type(Answer, Button));
  870. if (IsTimeoutButton)
  871. {
  872. *TimeoutButton = Button;
  873. }
  874. }
  875. }
  876. }
  877. }
  878. int NeverAskAgainWidth = 0;
  879. if (!NeverAskAgainCaption.IsEmpty())
  880. {
  881. NeverAskAgainWidth =
  882. ScaleByTextHeightRunTime(Result, 16) + // checkbox
  883. Result->Canvas->TextWidth(NeverAskAgainCaption) +
  884. ScaleByTextHeightRunTime(Result, 16); // 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 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 (CheckWin32Version(6, 1))
  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->Name = LabelName;
  982. Message->Parent = Panel;
  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. }