MessageDlg.cpp 42 KB

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