MessageDlg.cpp 42 KB

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