MessageDlg.cpp 42 KB

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