MessageDlg.cpp 42 KB

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