MessageDlg.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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. NeverAskAgainCheck = NULL;
  77. FUpdateForShiftStateTimer = NULL;
  78. UseSystemSettingsPre(this);
  79. FDummyForm = new TForm(this);
  80. UseSystemSettings(FDummyForm);
  81. }
  82. //---------------------------------------------------------------------------
  83. __fastcall TMessageForm::~TMessageForm()
  84. {
  85. SAFE_DESTROY(FDummyForm);
  86. SAFE_DESTROY(FUpdateForShiftStateTimer);
  87. }
  88. //---------------------------------------------------------------------------
  89. void __fastcall TMessageForm::HelpButtonSubmit(TObject * /*Sender*/, unsigned int & /*Answer*/)
  90. {
  91. if (HelpKeyword != HELP_NONE)
  92. {
  93. FormHelp(this);
  94. }
  95. else
  96. {
  97. MessageWithNoHelp(GetReportText());
  98. }
  99. }
  100. //---------------------------------------------------------------------------
  101. void __fastcall TMessageForm::ReportButtonSubmit(TObject * /*Sender*/, unsigned int & /*Answer*/)
  102. {
  103. // Report text goes last, as it may exceed URL parameters limit (2048) and get truncated.
  104. // And we need to preserve the other parameters.
  105. UnicodeString Url =
  106. FMTLOAD(ERROR_REPORT_URL2,
  107. (Configuration->ProductVersion, GUIConfiguration->AppliedLocaleHex,
  108. EncodeUrlString(GetReportText())));
  109. OpenBrowser(Url);
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TMessageForm::UpdateForShiftState()
  113. {
  114. TShiftState ShiftState =
  115. KeyboardStateToShiftState() * AllKeyShiftStates();
  116. if (FShiftState != ShiftState)
  117. {
  118. FShiftState = ShiftState;
  119. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  120. {
  121. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  122. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  123. {
  124. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  125. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  126. {
  127. TMenuItem * Item = MenuItems->Items[ItemIndex];
  128. TShiftState GrouppedShiftState(Item->Tag >> 16);
  129. if (Item->Enabled &&
  130. ((ShiftState.Empty() && Item->Default) ||
  131. (!ShiftState.Empty() && (ShiftState == GrouppedShiftState))))
  132. {
  133. Button->Caption = CopyToChar(Item->Caption, L'\t', false);
  134. Button->ModalResult = Item->Tag & 0xFFFF;
  135. DebugAssert(Button->OnClick == NULL);
  136. DebugAssert(Item->OnClick == MenuItemClick);
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. //---------------------------------------------------------------------------
  145. void __fastcall TMessageForm::KeyUp(Word & Key, TShiftState Shift)
  146. {
  147. UpdateForShiftState();
  148. TForm::KeyUp(Key, Shift);
  149. }
  150. //---------------------------------------------------------------------------
  151. void __fastcall TMessageForm::KeyDown(Word & Key, TShiftState Shift)
  152. {
  153. if (Shift.Contains(ssCtrl) && (Key == L'C'))
  154. {
  155. AppLog(L"Copying message to clipboard");
  156. TInstantOperationVisualizer Visualizer;
  157. CopyToClipboard(GetFormText());
  158. }
  159. else
  160. {
  161. if (!Shift.Contains(ssCtrl))
  162. {
  163. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  164. {
  165. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  166. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  167. {
  168. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  169. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  170. {
  171. TMenuItem * Item = MenuItems->Items[ItemIndex];
  172. if (IsAccel(Key, MenuItems->Items[ItemIndex]->Caption))
  173. {
  174. Item->OnClick(Item);
  175. Key = 0;
  176. break;
  177. }
  178. }
  179. }
  180. if (Key == 0)
  181. {
  182. break;
  183. }
  184. }
  185. }
  186. UpdateForShiftState();
  187. TForm::KeyDown(Key, Shift);
  188. }
  189. }
  190. //---------------------------------------------------------------------------
  191. UnicodeString __fastcall TMessageForm::NormalizeNewLines(UnicodeString Text)
  192. {
  193. Text = ReplaceStr(Text, L"\r", L"");
  194. Text = ReplaceStr(Text, L"\n", L"\r\n");
  195. return Text;
  196. }
  197. //---------------------------------------------------------------------------
  198. UnicodeString __fastcall TMessageForm::GetFormText()
  199. {
  200. UnicodeString DividerLine, ButtonCaptions;
  201. DividerLine = GetDividerLine() + sLineBreak;
  202. for (int i = 0; i < ComponentCount - 1; i++)
  203. {
  204. if (dynamic_cast<TButton*>(Components[i]) != NULL)
  205. {
  206. ButtonCaptions += dynamic_cast<TButton*>(Components[i])->Caption +
  207. UnicodeString::StringOfChar(L' ', 3);
  208. }
  209. }
  210. ButtonCaptions = ReplaceStr(ButtonCaptions, L"&", L"");
  211. UnicodeString MoreMessages;
  212. if (MessageMemo != NULL)
  213. {
  214. MoreMessages = MessageMemo->Text + DividerLine;
  215. }
  216. else if ((MessageBrowser != NULL) && CopyTextFromBrowser(MessageBrowser, MoreMessages))
  217. {
  218. if (!EndsStr(sLineBreak, MoreMessages))
  219. {
  220. MoreMessages += sLineBreak;
  221. }
  222. MoreMessages += DividerLine;
  223. }
  224. UnicodeString MessageCaption = NormalizeNewLines(MessageText);
  225. UnicodeString Result = FORMAT(L"%s%s%s%s%s%s%s%s%s%s%s", (DividerLine, Caption, sLineBreak,
  226. DividerLine, MessageCaption, sLineBreak, DividerLine, MoreMessages,
  227. ButtonCaptions, sLineBreak, DividerLine));
  228. return Result;
  229. }
  230. //---------------------------------------------------------------------------
  231. UnicodeString __fastcall TMessageForm::GetReportText()
  232. {
  233. UnicodeString Text = MessageText;
  234. Text = Text.TrimRight();
  235. if (MessageMemo != NULL)
  236. {
  237. Text += L"\n\n" + MessageMemo->Text;
  238. }
  239. // Currently we use browser for updates box only and it has help context,
  240. // and does not have Report button, so we cannot get here.
  241. DebugAssert(MessageBrowser == NULL);
  242. Text = NormalizeNewLines(Text);
  243. UnicodeString ReportErrorText = NormalizeNewLines(FMTLOAD(REPORT_ERROR, (L"")));
  244. Text = ReplaceStr(Text, ReportErrorText, L"");
  245. Text = Trim(Text);
  246. return Text;
  247. }
  248. //---------------------------------------------------------------------------
  249. void __fastcall TMessageForm::CMDialogKey(TWMKeyDown & Message)
  250. {
  251. // this gets used in WinInterface.cpp SetTimeoutEvents
  252. if (OnKeyDown != NULL)
  253. {
  254. OnKeyDown(this, Message.CharCode, KeyDataToShiftState(Message.KeyData));
  255. }
  256. if (Message.CharCode == VK_MENU)
  257. {
  258. bool AnyButtonWithGrouppedCommandsWithShiftState = false;
  259. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  260. {
  261. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  262. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  263. {
  264. // we should check if there are any commands with shift state,
  265. // but it's bit overkill
  266. AnyButtonWithGrouppedCommandsWithShiftState = true;
  267. break;
  268. }
  269. }
  270. // this is to make Alt only alter button meaning (if there is any
  271. // alternable button) and not popup system menu
  272. if (AnyButtonWithGrouppedCommandsWithShiftState)
  273. {
  274. Message.Result = 1;
  275. UpdateForShiftState();
  276. }
  277. else
  278. {
  279. TForm::Dispatch(&Message);
  280. }
  281. }
  282. else if ((Message.CharCode == VK_UP) || (Message.CharCode == VK_DOWN))
  283. {
  284. // WORKAROUND
  285. // noop to make up/down be passed back to button to allow drop down,
  286. // see TMessageButton::WMGetDlgCode
  287. }
  288. else
  289. {
  290. TForm::Dispatch(&Message);
  291. }
  292. }
  293. //---------------------------------------------------------------------------
  294. int __fastcall TMessageForm::ShowModal()
  295. {
  296. if (IsApplicationMinimized())
  297. {
  298. FShowNoActivate = true;
  299. }
  300. int Result = TForm::ShowModal();
  301. UnhookFormActivation(this);
  302. return Result;
  303. }
  304. //---------------------------------------------------------------------------
  305. void __fastcall TMessageForm::SetZOrder(bool TopMost)
  306. {
  307. // WORKAROUND: If application is minimized,
  308. // swallow call to BringToFront() from TForm::ShowModal()
  309. if (FShowNoActivate && TopMost)
  310. {
  311. // noop
  312. }
  313. else
  314. {
  315. TForm::SetZOrder(TopMost);
  316. }
  317. }
  318. //---------------------------------------------------------------------------
  319. void __fastcall TMessageForm::CMShowingChanged(TMessage & Message)
  320. {
  321. if (Showing && FShowNoActivate)
  322. {
  323. ShowFormNoActivate(this);
  324. }
  325. else
  326. {
  327. TForm::Dispatch(&Message);
  328. }
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TMessageForm::Dispatch(void * Message)
  332. {
  333. TMessage * M = reinterpret_cast<TMessage*>(Message);
  334. if (M->Msg == CM_DIALOGKEY)
  335. {
  336. CMDialogKey(*((TWMKeyDown *)Message));
  337. }
  338. else if (M->Msg == CM_SHOWINGCHANGED)
  339. {
  340. CMShowingChanged(*M);
  341. }
  342. else if (M->Msg == CM_DPICHANGED)
  343. {
  344. if (MessageBrowser != NULL)
  345. {
  346. LoadMessageBrowser();
  347. }
  348. if (NeverAskAgainCheck != NULL)
  349. {
  350. AutoSizeCheckBox(NeverAskAgainCheck);
  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. int NeverAskAgainBaseWidth = 0;
  879. if (!NeverAskAgainCaption.IsEmpty())
  880. {
  881. NeverAskAgainBaseWidth = CalculateCheckBoxWidth(Result, NeverAskAgainCaption);
  882. NeverAskAgainWidth = NeverAskAgainBaseWidth + ScaleByTextHeightRunTime(Result, 8); // even more margin
  883. }
  884. int ButtonSpacing = ScaleByTextHeightRunTime(Result, mcButtonSpacing);
  885. int ButtonGroupWidth = NeverAskAgainWidth;
  886. if (!ButtonControls.empty())
  887. {
  888. ButtonGroupWidth += ButtonWidths +
  889. ButtonSpacing * (ButtonControls.size() - 1);
  890. }
  891. DebugAssert((ButtonHeight > 0) && (ButtonWidths > 0));
  892. TPanel * Panel = CreateBlankPanel(Result);
  893. Result->ContentsPanel = Panel;
  894. Panel->Name = MessagePanelName;
  895. Panel->Parent = Result;
  896. Panel->Color = clWindow;
  897. Panel->ParentBackground = false;
  898. Panel->Anchors = TAnchors() << akLeft << akRight << akTop;
  899. Panel->Caption = L"";
  900. int IconWidth = 0;
  901. int IconHeight = 0;
  902. UnicodeString ImageName = AImageName;
  903. if (ImageName.IsEmpty() &&
  904. DebugAlwaysTrue(ImageNames[DlgType] != NULL))
  905. {
  906. ImageName = ImageNames[DlgType];
  907. }
  908. if (DebugAlwaysTrue(!ImageName.IsEmpty()))
  909. {
  910. TImage * Image = new TImage(Result);
  911. Image->Name = L"Image";
  912. Image->Parent = Panel;
  913. LoadDialogImage(Image, ImageName);
  914. Image->SetBounds(HorzMargin, VertMargin, Image->Picture->Width, Image->Picture->Height);
  915. IconWidth = Image->Width + HorzSpacing;
  916. IconHeight = Image->Height;
  917. }
  918. int MaxTextWidth = ScaleByTextHeightRunTime(Result, mcMaxDialogWidth);
  919. // If the message contains SHA-256 hex fingerprint (CERT_TEXT2 on TLS/SSL certificate verification dialog),
  920. // allow wider box to fit it
  921. if (TRegEx::IsMatch(Msg, L"([0-9a-fA-F]{2}[:\-]){31}[0-9a-fA-F]{2}"))
  922. {
  923. MaxTextWidth = MaxTextWidth * 3 / 2;
  924. }
  925. // if the dialog would be wide anyway (overwrite confirmation on Windows XP),
  926. // to fit the buttons, do not restrict the text
  927. if (MaxTextWidth < ButtonGroupWidth - IconWidth)
  928. {
  929. MaxTextWidth = ButtonGroupWidth - IconWidth;
  930. }
  931. UnicodeString BodyMsg = Msg;
  932. BodyMsg = RemoveInteractiveMsgTag(BodyMsg);
  933. UnicodeString MainMsg;
  934. if (ExtractMainInstructions(BodyMsg, MainMsg))
  935. {
  936. Result->MessageText = MainMsg + BodyMsg;
  937. BodyMsg = BodyMsg.TrimLeft();
  938. }
  939. else
  940. {
  941. Result->MessageText = BodyMsg;
  942. }
  943. ApplyTabs(Result->MessageText, L' ', NULL, NULL);
  944. // Windows XP (not sure about Vista) does not support Hair space.
  945. // For Windows XP, we still keep the existing hack by using hard-coded spaces
  946. // in resource string
  947. if (IsWin7())
  948. {
  949. // Have to be padding with spaces (the smallest space defined, hair space = 1px),
  950. // as tabs actually do not tab, just expand to 8 spaces.
  951. // Otherwise we would have to do custom drawing
  952. // (using GetTabbedTextExtent and TabbedTextOut)
  953. const wchar_t HairSpace = L'\x200A';
  954. ApplyTabs(BodyMsg, HairSpace, CalculateWidthOnCanvas, Result->Canvas);
  955. }
  956. DebugAssert(MainMsg.Pos(L"\t") == 0);
  957. int IconTextWidth = -1;
  958. int IconTextHeight = 0;
  959. int ALeft = IconWidth + HorzMargin;
  960. for (int MessageIndex = 0; MessageIndex <= 1; MessageIndex++)
  961. {
  962. UnicodeString LabelMsg;
  963. UnicodeString LabelName;
  964. TColor LabelColor = Graphics::clNone;
  965. HFONT LabelFont = 0;
  966. switch (MessageIndex)
  967. {
  968. case 0:
  969. LabelMsg = MainMsg;
  970. LabelName = MainMessageLabelName;
  971. LabelColor = MainInstructionColor;
  972. LabelFont = MainInstructionFont;
  973. break;
  974. case 1:
  975. LabelMsg = BodyMsg;
  976. LabelName = MessageLabelName;
  977. break;
  978. default:
  979. DebugFail();
  980. break;
  981. }
  982. if (!LabelMsg.IsEmpty())
  983. {
  984. TLabel * Message = new TLabel(Panel);
  985. Message->Parent = Panel;
  986. Message->Name = LabelName;
  987. Message->WordWrap = true;
  988. Message->Caption = LabelMsg;
  989. Message->BiDiMode = Result->BiDiMode;
  990. // added to show & as & for messages containing !& pattern of custom commands
  991. // (suppose that we actually never want to use & as accel in message text)
  992. Message->ShowAccelChar = false;
  993. if (LabelFont != 0)
  994. {
  995. Message->Font->Handle = LabelFont;
  996. if (DebugAlwaysTrue(LabelFont == MainInstructionFont) &&
  997. // When showing an early error message
  998. (Configuration != NULL))
  999. {
  1000. Configuration->Usage->Set(L"ThemeMainInstructionFontSize", Message->Font->Size);
  1001. }
  1002. }
  1003. if (LabelColor != Graphics::clNone)
  1004. {
  1005. Message->Font->Color = LabelColor;
  1006. }
  1007. TRect TextRect;
  1008. SetRect(&TextRect, 0, 0, MaxTextWidth, 0);
  1009. DrawText(Message->Canvas->Handle, LabelMsg.c_str(), LabelMsg.Length() + 1, &TextRect,
  1010. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX |
  1011. Result->DrawTextBiDiModeFlagsReadingOnly());
  1012. int MaxWidth = Monitor->Width - HorzMargin * 2 - IconWidth - 30;
  1013. // 5% buffer for potential WM_DPICHANGED, as after re-scaling the text can otherwise narrowly not fit in.
  1014. // Though note that the buffer is lost on the first re-scale due to the AutoSize
  1015. TextRect.right = MulDiv(TextRect.right, 105, 100);
  1016. // this will truncate the text, we should implement something smarter eventually
  1017. TextRect.right = Min(TextRect.right, MaxWidth);
  1018. IconTextWidth = Max(IconTextWidth, IconWidth + TextRect.Right);
  1019. if (IconTextHeight > 0)
  1020. {
  1021. IconTextHeight += VertMargin;
  1022. }
  1023. Message->SetBounds(ALeft, VertMargin + IconTextHeight, TextRect.Right, TextRect.Bottom);
  1024. IconTextHeight += TextRect.Bottom;
  1025. }
  1026. }
  1027. if (LinkControl != NULL)
  1028. {
  1029. LinkControl->Parent = Panel;
  1030. LinkControl->Left = Panel->ClientWidth - HorzMargin - LinkControl->Width;
  1031. LinkControl->Top = VertMargin + IconTextHeight + VertMargin;
  1032. IconTextHeight += VertMargin + LinkControl->Height;
  1033. }
  1034. DebugAssert((IconTextWidth > 0) && (IconTextHeight > 0));
  1035. IconTextHeight = Max(IconTextHeight, IconHeight);
  1036. int MoreMessageHeight =
  1037. (HasMoreMessages ?
  1038. ScaleByTextHeightRunTime(Result, (MoreMessagesSize.Height > 0 ? MoreMessagesSize.Height : mcMoreMessageHeight)) : 0);
  1039. Panel->SetBounds(0, 0, Result->ClientWidth, VertMargin + IconTextHeight + VertMargin + MoreMessageHeight);
  1040. TControl * MoreMessagesControl = NULL;
  1041. if (HasMoreMessages)
  1042. {
  1043. if (MoreMessages != NULL)
  1044. {
  1045. DebugAssert(MoreMessagesUrl.IsEmpty());
  1046. TMemo * MessageMemo = new TMemo(Panel);
  1047. MoreMessagesControl = MessageMemo;
  1048. MessageMemo->Name = L"MessageMemo";
  1049. MessageMemo->Parent = Panel;
  1050. MessageMemo->ReadOnly = true;
  1051. MessageMemo->WantReturns = False;
  1052. MessageMemo->ScrollBars = ssVertical;
  1053. MessageMemo->Anchors = TAnchors() << akLeft << akRight << akTop;
  1054. MessageMemo->Lines->Text = MoreMessages->Text;
  1055. Result->MessageMemo = MessageMemo;
  1056. }
  1057. else if (DebugAlwaysTrue(!MoreMessagesUrl.IsEmpty()))
  1058. {
  1059. TPanel * MessageBrowserPanel = CreateBlankPanel(Panel);
  1060. MessageBrowserPanel->Parent = Panel;
  1061. MessageBrowserPanel->Anchors = TAnchors() << akLeft << akRight << akTop;
  1062. MessageBrowserPanel->BevelKind = bkTile; // flat border
  1063. Result->MessageBrowserPanel = MessageBrowserPanel;
  1064. MoreMessagesControl = Result->MessageBrowserPanel;
  1065. Result->MessageBrowserUrl = CampaignUrl(MoreMessagesUrl);
  1066. }
  1067. }
  1068. int MinClientWidth =
  1069. ScaleByTextHeightRunTime(Result,
  1070. HasMoreMessages ? (MoreMessagesSize.Width > 0 ? MoreMessagesSize.Width : mcMinDialogwithMoreMessagesWidth) : mcMinDialogWidth);
  1071. int AClientWidth =
  1072. Max(
  1073. (IconTextWidth > ButtonGroupWidth ? IconTextWidth : ButtonGroupWidth) +
  1074. HorzMargin * 2,
  1075. MinClientWidth);
  1076. Result->ClientWidth = AClientWidth;
  1077. Result->ClientHeight =
  1078. Panel->Height + ButtonVertMargin + ButtonHeight + ButtonVertMargin;
  1079. if (!CustomCaption.IsEmpty())
  1080. {
  1081. Result->Caption = CustomCaption;
  1082. }
  1083. else if (DebugAlwaysTrue(DlgType != mtCustom))
  1084. {
  1085. Result->Caption = LoadResourceString(Captions[DlgType]);
  1086. }
  1087. else
  1088. {
  1089. Result->Caption = Application->Title;
  1090. }
  1091. if (MoreMessagesControl != NULL)
  1092. {
  1093. MoreMessagesControl->SetBounds(
  1094. ALeft,
  1095. Panel->Height - MoreMessageHeight,
  1096. Result->ClientWidth - ALeft - HorzMargin,
  1097. MoreMessageHeight - VertMargin);
  1098. }
  1099. int ButtonTop = Panel->Height + ButtonVertMargin;
  1100. int X = Result->ClientWidth - ButtonGroupWidth + NeverAskAgainWidth - HorzMargin;
  1101. for (unsigned int i = 0; i < ButtonControls.size(); i++)
  1102. {
  1103. ButtonControls[i]->SetBounds(
  1104. X, ButtonTop, ButtonControls[i]->Width, ButtonControls[i]->Height);
  1105. X += ButtonControls[i]->Width + ButtonSpacing;
  1106. }
  1107. if (!NeverAskAgainCaption.IsEmpty() &&
  1108. !ButtonControls.empty())
  1109. {
  1110. Result->NeverAskAgainCheck = new TCheckBox(Result);
  1111. Result->NeverAskAgainCheck->Name = L"NeverAskAgainCheck";
  1112. Result->NeverAskAgainCheck->Parent = Result;
  1113. Result->NeverAskAgainCheck->Caption = NeverAskAgainCaption;
  1114. // Previously we set anchor to akBottom, but as we do not do that for buttons, we removed that.
  1115. // When showing window on 100% DPI monitor, with system DPI 100%, but main monitor 150%,
  1116. // the title bar seems to start on 150% DPI reducing later, leaving the form client height
  1117. // sligtly higher than needed and the checkbox being aligned differently than the button.
  1118. // Removing the akBottom aligning improves this sligtly, while the main problem still should be fixed.
  1119. TButton * FirstButton = ButtonControls[0];
  1120. int NeverAskAgainHeight = ScaleByTextHeightRunTime(Result, Result->NeverAskAgainCheck->Height);
  1121. int NeverAskAgainTop = FirstButton->Top + ((FirstButton->Height - NeverAskAgainHeight) / 2);
  1122. int NeverAskAgainLeft = HorzMargin;
  1123. Result->NeverAskAgainCheck->SetBounds(
  1124. NeverAskAgainLeft, NeverAskAgainTop, NeverAskAgainBaseWidth, NeverAskAgainHeight);
  1125. }
  1126. return Result;
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. TForm * __fastcall CreateMoreMessageDialog(const UnicodeString & Msg,
  1130. TStrings * MoreMessages, TMsgDlgType DlgType, unsigned int Answers,
  1131. const TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  1132. unsigned int TimeoutAnswer, TButton ** TimeoutButton, const UnicodeString & ImageName,
  1133. const UnicodeString & NeverAskAgainCaption, const UnicodeString & MoreMessagesUrl,
  1134. TSize MoreMessagesSize, const UnicodeString & CustomCaption)
  1135. {
  1136. return TMessageForm::Create(Msg, MoreMessages, DlgType, Answers,
  1137. Aliases, AliasesCount, TimeoutAnswer, TimeoutButton, ImageName,
  1138. NeverAskAgainCaption, MoreMessagesUrl, MoreMessagesSize, CustomCaption);
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. void __fastcall InsertPanelToMessageDialog(TCustomForm * Form, TPanel * Panel)
  1142. {
  1143. TMessageForm * MessageForm = DebugNotNull(dynamic_cast<TMessageForm *>(Form));
  1144. MessageForm->InsertPanel(Panel);
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall NavigateMessageDialogToUrl(TCustomForm * Form, const UnicodeString & Url)
  1148. {
  1149. TMessageForm * MessageForm = DebugNotNull(dynamic_cast<TMessageForm *>(Form));
  1150. MessageForm->NavigateToUrl(Url);
  1151. }
  1152. //---------------------------------------------------------------------------
  1153. int __fastcall GetMessageDialogContentWidth(TCustomForm * Form)
  1154. {
  1155. TMessageForm * MessageForm = DebugNotNull(dynamic_cast<TMessageForm *>(Form));
  1156. return MessageForm->GetContentWidth();
  1157. }