1
0

MessageDlg.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. }
  80. //---------------------------------------------------------------------------
  81. __fastcall TMessageForm::~TMessageForm()
  82. {
  83. SAFE_DESTROY(FUpdateForShiftStateTimer);
  84. }
  85. //---------------------------------------------------------------------------
  86. void __fastcall TMessageForm::HelpButtonSubmit(TObject * /*Sender*/, unsigned int & /*Answer*/)
  87. {
  88. if (HelpKeyword != HELP_NONE)
  89. {
  90. FormHelp(this);
  91. }
  92. else
  93. {
  94. MessageWithNoHelp(GetReportText());
  95. }
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TMessageForm::ReportButtonSubmit(TObject * /*Sender*/, unsigned int & /*Answer*/)
  99. {
  100. // Report text goes last, as it may exceed URL parameters limit (2048) and get truncated.
  101. // And we need to preserve the other parameters.
  102. UnicodeString Url =
  103. FMTLOAD(ERROR_REPORT_URL2,
  104. (Configuration->ProductVersion, GUIConfiguration->AppliedLocaleHex,
  105. EncodeUrlString(GetReportText())));
  106. OpenBrowser(Url);
  107. }
  108. //---------------------------------------------------------------------------
  109. void __fastcall TMessageForm::UpdateForShiftState()
  110. {
  111. TShiftState ShiftState =
  112. KeyboardStateToShiftState() * AllKeyShiftStates();
  113. if (FShiftState != ShiftState)
  114. {
  115. FShiftState = ShiftState;
  116. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  117. {
  118. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  119. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  120. {
  121. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  122. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  123. {
  124. TMenuItem * Item = MenuItems->Items[ItemIndex];
  125. TShiftState GrouppedShiftState(Item->Tag >> 16);
  126. if (Item->Enabled &&
  127. ((ShiftState.Empty() && Item->Default) ||
  128. (!ShiftState.Empty() && (ShiftState == GrouppedShiftState))))
  129. {
  130. Button->Caption = CopyToChar(Item->Caption, L'\t', false);
  131. Button->ModalResult = Item->Tag & 0xFFFF;
  132. DebugAssert(Button->OnClick == NULL);
  133. DebugAssert(Item->OnClick == MenuItemClick);
  134. break;
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. //---------------------------------------------------------------------------
  142. void __fastcall TMessageForm::KeyUp(Word & Key, TShiftState Shift)
  143. {
  144. UpdateForShiftState();
  145. TForm::KeyUp(Key, Shift);
  146. }
  147. //---------------------------------------------------------------------------
  148. void __fastcall TMessageForm::KeyDown(Word & Key, TShiftState Shift)
  149. {
  150. if (Shift.Contains(ssCtrl) && (Key == L'C'))
  151. {
  152. AppLog(L"Copying message to clipboard");
  153. TInstantOperationVisualizer Visualizer;
  154. CopyToClipboard(GetFormText());
  155. }
  156. else
  157. {
  158. if (!Shift.Contains(ssCtrl))
  159. {
  160. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  161. {
  162. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  163. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  164. {
  165. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  166. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  167. {
  168. TMenuItem * Item = MenuItems->Items[ItemIndex];
  169. if (IsAccel(Key, MenuItems->Items[ItemIndex]->Caption))
  170. {
  171. Item->OnClick(Item);
  172. Key = 0;
  173. break;
  174. }
  175. }
  176. }
  177. if (Key == 0)
  178. {
  179. break;
  180. }
  181. }
  182. }
  183. UpdateForShiftState();
  184. TForm::KeyDown(Key, Shift);
  185. }
  186. }
  187. //---------------------------------------------------------------------------
  188. UnicodeString __fastcall TMessageForm::NormalizeNewLines(UnicodeString Text)
  189. {
  190. Text = ReplaceStr(Text, L"\r", L"");
  191. Text = ReplaceStr(Text, L"\n", L"\r\n");
  192. return Text;
  193. }
  194. //---------------------------------------------------------------------------
  195. UnicodeString __fastcall TMessageForm::GetFormText()
  196. {
  197. UnicodeString DividerLine, ButtonCaptions;
  198. DividerLine = GetDividerLine() + sLineBreak;
  199. for (int i = 0; i < ComponentCount - 1; i++)
  200. {
  201. if (dynamic_cast<TButton*>(Components[i]) != NULL)
  202. {
  203. ButtonCaptions += dynamic_cast<TButton*>(Components[i])->Caption +
  204. UnicodeString::StringOfChar(L' ', 3);
  205. }
  206. }
  207. ButtonCaptions = ReplaceStr(ButtonCaptions, L"&", L"");
  208. UnicodeString MoreMessages;
  209. if (MessageMemo != NULL)
  210. {
  211. MoreMessages = MessageMemo->Text + DividerLine;
  212. }
  213. else if ((MessageBrowser != NULL) && CopyTextFromBrowser(MessageBrowser, MoreMessages))
  214. {
  215. if (!EndsStr(sLineBreak, MoreMessages))
  216. {
  217. MoreMessages += sLineBreak;
  218. }
  219. MoreMessages += DividerLine;
  220. }
  221. UnicodeString MessageCaption = NormalizeNewLines(MessageText);
  222. UnicodeString Result = FORMAT(L"%s%s%s%s%s%s%s%s%s%s%s", (DividerLine, Caption, sLineBreak,
  223. DividerLine, MessageCaption, sLineBreak, DividerLine, MoreMessages,
  224. ButtonCaptions, sLineBreak, DividerLine));
  225. return Result;
  226. }
  227. //---------------------------------------------------------------------------
  228. UnicodeString __fastcall TMessageForm::GetReportText()
  229. {
  230. UnicodeString Text = MessageText;
  231. Text = Text.TrimRight();
  232. if (MessageMemo != NULL)
  233. {
  234. Text += L"\n\n" + MessageMemo->Text;
  235. }
  236. // Currently we use browser for updates box only and it has help context,
  237. // and does not have Report button, so we cannot get here.
  238. DebugAssert(MessageBrowser == NULL);
  239. Text = NormalizeNewLines(Text);
  240. UnicodeString ReportErrorText = NormalizeNewLines(FMTLOAD(REPORT_ERROR, (L"")));
  241. Text = ReplaceStr(Text, ReportErrorText, L"");
  242. Text = Trim(Text);
  243. return Text;
  244. }
  245. //---------------------------------------------------------------------------
  246. void __fastcall TMessageForm::CMDialogKey(TWMKeyDown & Message)
  247. {
  248. // this gets used in WinInterface.cpp SetTimeoutEvents
  249. if (OnKeyDown != NULL)
  250. {
  251. OnKeyDown(this, Message.CharCode, KeyDataToShiftState(Message.KeyData));
  252. }
  253. if (Message.CharCode == VK_MENU)
  254. {
  255. bool AnyButtonWithGrouppedCommandsWithShiftState = false;
  256. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  257. {
  258. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  259. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  260. {
  261. // we should check if there are any commands with shift state,
  262. // but it's bit overkill
  263. AnyButtonWithGrouppedCommandsWithShiftState = true;
  264. break;
  265. }
  266. }
  267. // this is to make Alt only alter button meaning (if there is any
  268. // alternable button) and not popup system menu
  269. if (AnyButtonWithGrouppedCommandsWithShiftState)
  270. {
  271. Message.Result = 1;
  272. UpdateForShiftState();
  273. }
  274. else
  275. {
  276. TForm::Dispatch(&Message);
  277. }
  278. }
  279. else if ((Message.CharCode == VK_UP) || (Message.CharCode == VK_DOWN))
  280. {
  281. // WORKAROUND
  282. // noop to make up/down be passed back to button to allow drop down,
  283. // see TMessageButton::WMGetDlgCode
  284. }
  285. else
  286. {
  287. TForm::Dispatch(&Message);
  288. }
  289. }
  290. //---------------------------------------------------------------------------
  291. int __fastcall TMessageForm::ShowModal()
  292. {
  293. if (IsApplicationMinimized())
  294. {
  295. FShowNoActivate = true;
  296. }
  297. int Result = TForm::ShowModal();
  298. UnhookFormActivation(this);
  299. return Result;
  300. }
  301. //---------------------------------------------------------------------------
  302. void __fastcall TMessageForm::SetZOrder(bool TopMost)
  303. {
  304. // WORKAROUND: If application is minimized,
  305. // swallow call to BringToFront() from TForm::ShowModal()
  306. if (FShowNoActivate && TopMost)
  307. {
  308. // noop
  309. }
  310. else
  311. {
  312. TForm::SetZOrder(TopMost);
  313. }
  314. }
  315. //---------------------------------------------------------------------------
  316. void __fastcall TMessageForm::CMShowingChanged(TMessage & Message)
  317. {
  318. if (Showing && FShowNoActivate)
  319. {
  320. ShowFormNoActivate(this);
  321. }
  322. else
  323. {
  324. TForm::Dispatch(&Message);
  325. }
  326. }
  327. //---------------------------------------------------------------------------
  328. void __fastcall TMessageForm::Dispatch(void * Message)
  329. {
  330. TMessage * M = reinterpret_cast<TMessage*>(Message);
  331. if (M->Msg == CM_DIALOGKEY)
  332. {
  333. CMDialogKey(*((TWMKeyDown *)Message));
  334. }
  335. else if (M->Msg == CM_SHOWINGCHANGED)
  336. {
  337. CMShowingChanged(*M);
  338. }
  339. else
  340. {
  341. TForm::Dispatch(Message);
  342. }
  343. }
  344. //---------------------------------------------------------------------------
  345. void __fastcall TMessageForm::FormAfterMonitorDpiChanged(TObject *, int OldDPI, int NewDPI)
  346. {
  347. DebugUsedParam2(OldDPI, NewDPI);
  348. if (MessageBrowser != NULL)
  349. {
  350. LoadMessageBrowser();
  351. }
  352. if (NeverAskAgainCheck != NULL)
  353. {
  354. AutoSizeCheckBox(NeverAskAgainCheck);
  355. }
  356. }
  357. //---------------------------------------------------------------------------
  358. void __fastcall TMessageForm::CreateParams(TCreateParams & Params)
  359. {
  360. TForm::CreateParams(Params);
  361. if ((Screen != NULL) && (Screen->ActiveForm != NULL) &&
  362. Screen->ActiveForm->HandleAllocated())
  363. {
  364. Params.WndParent = Screen->ActiveForm->Handle;
  365. }
  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. const ResourceString * Captions[] = { &_SMsgDlgWarning, &_SMsgDlgError, &_SMsgDlgInformation,
  430. &_SMsgDlgConfirm, NULL };
  431. 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. void __fastcall TMessageForm::InsertPanel(TPanel * Panel)
  587. {
  588. if (DebugAlwaysTrue(MessageBrowser != NULL))
  589. {
  590. // we currently use this for updates message box only
  591. TControl * ContentsControl = static_cast<TControl *>(DebugNotNull(MessageBrowser))->Parent;
  592. Panel->Parent = ContentsPanel;
  593. Panel->Width = ContentsControl->Width;
  594. Panel->Left = ContentsControl->Left;
  595. int ContentsBottom = ContentsControl->Top + ContentsControl->Height;
  596. Panel->Top = ContentsBottom + ((ContentsPanel->Height - ContentsBottom) / 2);
  597. Height = Height + Panel->Height;
  598. ContentsPanel->Height = ContentsPanel->Height + Panel->Height;
  599. // The panel itself does not need this, as the ParentBackground (true by default)
  600. // has the same effect, but an eventual TStaticText on the panel
  601. // uses a wrong background color, if panel's ParentColor is not set.
  602. Panel->ParentColor = true;
  603. }
  604. }
  605. //---------------------------------------------------------------------------
  606. int __fastcall TMessageForm::GetContentWidth()
  607. {
  608. int Result = 0;
  609. if (DebugAlwaysTrue(MessageBrowser != NULL))
  610. {
  611. // we currently use this for updates message box only
  612. TControl * ContentsControl = static_cast<TControl *>(DebugNotNull(MessageBrowser))->Parent;
  613. Result = ContentsControl->Width;
  614. }
  615. return Result;
  616. }
  617. //---------------------------------------------------------------------------
  618. void __fastcall TMessageForm::NavigateToUrl(const UnicodeString & Url)
  619. {
  620. if (DebugAlwaysTrue(MessageBrowser != NULL))
  621. {
  622. UnicodeString FontSizeParam = FORMAT(L"fontsize=%d", (Font->Size));
  623. UnicodeString FullUrl = AppendUrlParams(Url, FontSizeParam);
  624. NavigateBrowserToUrl(MessageBrowser, FullUrl);
  625. }
  626. }
  627. //---------------------------------------------------------------------------
  628. void __fastcall AnswerNameAndCaption(
  629. unsigned int Answer, UnicodeString & Name, UnicodeString & Caption)
  630. {
  631. switch (Answer)
  632. {
  633. case qaYes:
  634. Caption = LoadStr(_SMsgDlgYes.Identifier);
  635. Name = YesButtonName;
  636. break;
  637. case qaNo:
  638. Caption = LoadStr(_SMsgDlgNo.Identifier);
  639. Name = L"No";
  640. break;
  641. case qaOK:
  642. Caption = LoadStr(_SMsgDlgOK.Identifier);
  643. Name = OKButtonName;
  644. break;
  645. case qaCancel:
  646. Caption = LoadStr(_SMsgDlgCancel.Identifier);
  647. Name = L"Cancel";
  648. break;
  649. case qaAbort:
  650. Caption = LoadStr(_SMsgDlgAbort.Identifier);
  651. Name = L"Abort";
  652. break;
  653. case qaRetry:
  654. Caption = LoadStr(_SMsgDlgRetry.Identifier);
  655. Name = L"Retry";
  656. break;
  657. case qaIgnore:
  658. Caption = LoadStr(_SMsgDlgIgnore.Identifier);
  659. Name = L"Ignore";
  660. break;
  661. // Own variant to avoid accelerator conflict with "Abort" button.
  662. // Note that as of now, ALL_BUTTON is never actually used,
  663. // because qaAll is always aliased
  664. case qaAll:
  665. Caption = LoadStr(ALL_BUTTON);
  666. Name = L"All";
  667. break;
  668. case qaNoToAll:
  669. Caption = LoadStr(_SMsgDlgNoToAll.Identifier);
  670. Name = L"NoToAll";
  671. break;
  672. // Own variant to avoid accelerator conflict with "Abort" button.
  673. case qaYesToAll:
  674. Caption = LoadStr(YES_TO_ALL_BUTTON);
  675. Name = L"YesToAll";
  676. break;
  677. case qaHelp:
  678. Caption = LoadStr(_SMsgDlgHelp.Identifier);
  679. Name = L"Help";
  680. break;
  681. case qaSkip:
  682. Caption = LoadStr(SKIP_BUTTON);
  683. Name = L"Skip";
  684. break;
  685. case qaReport:
  686. Caption = LoadStr(REPORT_BUTTON);
  687. Name = L"Report";
  688. break;
  689. default:
  690. DebugFail();
  691. throw Exception(L"Undefined answer");
  692. }
  693. }
  694. //---------------------------------------------------------------------------
  695. static int __fastcall CalculateWidthOnCanvas(UnicodeString Text, void * Arg)
  696. {
  697. TCanvas * Canvas = static_cast<TCanvas *>(Arg);
  698. return Canvas->TextWidth(Text);
  699. }
  700. //---------------------------------------------------------------------------
  701. TForm * __fastcall TMessageForm::Create(const UnicodeString & Msg,
  702. TStrings * MoreMessages, TMsgDlgType DlgType, unsigned int Answers,
  703. const TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  704. unsigned int TimeoutAnswer, TButton ** TimeoutButton, const UnicodeString & AImageName,
  705. const UnicodeString & NeverAskAgainCaption, const UnicodeString & MoreMessagesUrl,
  706. TSize MoreMessagesSize, const UnicodeString & CustomCaption)
  707. {
  708. unsigned int DefaultAnswer;
  709. if (FLAGSET(Answers, qaOK))
  710. {
  711. DefaultAnswer = qaOK;
  712. }
  713. else if (FLAGSET(Answers, qaYes))
  714. {
  715. DefaultAnswer = qaYes;
  716. }
  717. else
  718. {
  719. DefaultAnswer = qaRetry;
  720. }
  721. unsigned int CancelAnswer = ::CancelAnswer(Answers);
  722. if (TimeoutButton != NULL)
  723. {
  724. *TimeoutButton = NULL;
  725. }
  726. TColor MainInstructionColor;
  727. HFONT MainInstructionFont;
  728. HFONT InstructionFont;
  729. GetInstrutionsTheme(MainInstructionColor, MainInstructionFont, InstructionFont);
  730. TMessageForm * Result = SafeFormCreate<TMessageForm>();
  731. if (InstructionFont != 0)
  732. {
  733. Result->Font->Handle = InstructionFont;
  734. }
  735. else
  736. {
  737. Result->Font->Assign(Screen->MessageFont);
  738. }
  739. // Can be possibly nul when error occurs before configuration is created
  740. if (Configuration != NULL)
  741. {
  742. Configuration->Usage->Set(L"ThemeMessageFontSize", Result->Font->Size);
  743. }
  744. // make sure we consider sizes of the monitor,
  745. // that is set in DoFormWindowProc(CM_SHOWINGCHANGED) later.
  746. Forms::TMonitor * Monitor = FormMonitor(Result);
  747. bool HasMoreMessages = (MoreMessages != NULL) || !MoreMessagesUrl.IsEmpty();
  748. Result->BiDiMode = Application->BiDiMode;
  749. Result->BorderStyle = bsDialog;
  750. Result->Canvas->Font = Result->Font;
  751. Result->KeyPreview = true;
  752. int HorzMargin = ScaleByTextHeightRunTime(Result, mcHorzMargin);
  753. int VertMargin = ScaleByTextHeightRunTime(Result, mcVertMargin);
  754. int HorzSpacing = ScaleByTextHeightRunTime(Result, mcHorzSpacing);
  755. int ButtonVertMargin = ScaleByTextHeightRunTime(Result, mcButtonVertMargin);
  756. int ButtonWidths = 0;
  757. int ButtonHeight = -1;
  758. std::vector<TButton *> ButtonControls;
  759. TStaticText * LinkControl = NULL;
  760. TAnswerButtons AnswerButtons;
  761. for (unsigned int Answer = qaFirst; Answer <= qaLast; Answer = Answer << 1)
  762. {
  763. if (FLAGSET(Answers, Answer))
  764. {
  765. DebugAssert(Answer != mrCancel);
  766. UnicodeString Caption;
  767. UnicodeString Name;
  768. AnswerNameAndCaption(Answer, Name, Caption);
  769. TButtonSubmitEvent OnSubmit = NULL;
  770. int GroupWith = -1;
  771. TShiftState GrouppedShiftState;
  772. bool ElevationRequired = false;
  773. bool MenuButton = false;
  774. UnicodeString ActionAlias;
  775. if (Aliases != NULL)
  776. {
  777. for (unsigned int i = 0; i < AliasesCount; i++)
  778. {
  779. if (Answer == Aliases[i].Button)
  780. {
  781. if (!Aliases[i].Alias.IsEmpty())
  782. {
  783. Caption = Aliases[i].Alias;
  784. }
  785. OnSubmit = Aliases[i].OnSubmit;
  786. GroupWith = Aliases[i].GroupWith;
  787. GrouppedShiftState = Aliases[i].GrouppedShiftState;
  788. ElevationRequired = Aliases[i].ElevationRequired;
  789. MenuButton = Aliases[i].MenuButton;
  790. ActionAlias = Aliases[i].ActionAlias;
  791. DebugAssert((OnSubmit == NULL) || (GrouppedShiftState == TShiftState()));
  792. break;
  793. }
  794. }
  795. }
  796. // implemented for a one link only for now
  797. if (!ActionAlias.IsEmpty() &&
  798. DebugAlwaysTrue(LinkControl == NULL) &&
  799. DebugAlwaysTrue(OnSubmit != NULL) &&
  800. DebugAlwaysTrue(GroupWith < 0))
  801. {
  802. LinkControl = new TStaticText(Result);
  803. LinkControl->Name = Name;
  804. LinkControl->Caption = ActionAlias;
  805. LinkControl->Alignment = taRightJustify;
  806. LinkControl->Anchors = TAnchors() << akRight << akTop;
  807. LinkControl->OnClick = Result->ButtonSubmit;
  808. Result->FButtonSubmitEvents[LinkControl] = OnSubmit;
  809. }
  810. else
  811. {
  812. // we hope that all grouped-with buttons are for answer with greater
  813. // value that the answer to be grouped with
  814. if (GroupWith >= 0)
  815. {
  816. if (DebugAlwaysFalse(GroupWith >= static_cast<int>(Answer)) ||
  817. DebugAlwaysFalse(Answer == TimeoutAnswer) &&
  818. DebugAlwaysFalse(Answer == DefaultAnswer) &&
  819. DebugAlwaysFalse(Answer == CancelAnswer))
  820. {
  821. GroupWith = -1;
  822. }
  823. }
  824. bool IsTimeoutButton = (TimeoutButton != NULL) && (Answer == TimeoutAnswer);
  825. if (Answer == qaHelp)
  826. {
  827. DebugAssert(OnSubmit == NULL);
  828. OnSubmit = Result->HelpButtonSubmit;
  829. }
  830. if (Answer == qaReport)
  831. {
  832. DebugAssert(OnSubmit == NULL);
  833. OnSubmit = Result->ReportButtonSubmit;
  834. }
  835. TButton * Button = Result->CreateButton(
  836. Name, Caption, Answer,
  837. OnSubmit, IsTimeoutButton, GroupWith, GrouppedShiftState, ElevationRequired, MenuButton,
  838. AnswerButtons, HasMoreMessages, ButtonWidths);
  839. if (Button != NULL)
  840. {
  841. ButtonControls.push_back(Button);
  842. Button->Default = (Answer == DefaultAnswer);
  843. Button->Cancel = (Answer == CancelAnswer);
  844. if (ButtonHeight < 0)
  845. {
  846. ButtonHeight = Button->Height;
  847. }
  848. DebugAssert(ButtonHeight == Button->Height);
  849. AnswerButtons.insert(TAnswerButtons::value_type(Answer, Button));
  850. if (IsTimeoutButton)
  851. {
  852. *TimeoutButton = Button;
  853. }
  854. }
  855. }
  856. }
  857. }
  858. int NeverAskAgainWidth = 0;
  859. int NeverAskAgainBaseWidth = 0;
  860. if (!NeverAskAgainCaption.IsEmpty())
  861. {
  862. NeverAskAgainBaseWidth = CalculateCheckBoxWidth(Result, NeverAskAgainCaption);
  863. NeverAskAgainWidth = NeverAskAgainBaseWidth + ScaleByTextHeightRunTime(Result, 8); // even more margin
  864. }
  865. int ButtonSpacing = ScaleByTextHeightRunTime(Result, mcButtonSpacing);
  866. int ButtonGroupWidth = NeverAskAgainWidth;
  867. if (!ButtonControls.empty())
  868. {
  869. ButtonGroupWidth += ButtonWidths +
  870. ButtonSpacing * (ButtonControls.size() - 1);
  871. }
  872. DebugAssert((ButtonHeight > 0) && (ButtonWidths > 0));
  873. TPanel * Panel = CreateBlankPanel(Result);
  874. Result->ContentsPanel = Panel;
  875. Panel->Name = MessagePanelName;
  876. Panel->Parent = Result;
  877. Panel->Color = clWindow;
  878. Panel->ParentBackground = false;
  879. Panel->Anchors = TAnchors() << akLeft << akRight << akTop;
  880. Panel->Caption = L"";
  881. int IconWidth = 0;
  882. int IconHeight = 0;
  883. UnicodeString ImageName = AImageName;
  884. if (ImageName.IsEmpty() &&
  885. DebugAlwaysTrue(ImageNames[DlgType] != NULL))
  886. {
  887. ImageName = ImageNames[DlgType];
  888. }
  889. if (DebugAlwaysTrue(!ImageName.IsEmpty()))
  890. {
  891. TImage * Image = new TImage(Result);
  892. Image->Name = L"Image";
  893. Image->Parent = Panel;
  894. LoadDialogImage(Image, ImageName);
  895. Image->SetBounds(HorzMargin, VertMargin, Image->Picture->Width, Image->Picture->Height);
  896. IconWidth = Image->Width + HorzSpacing;
  897. IconHeight = Image->Height;
  898. }
  899. int MaxTextWidth = ScaleByTextHeightRunTime(Result, mcMaxDialogWidth);
  900. // If the message contains SHA-256 hex fingerprint (CERT_TEXT2 on TLS/SSL certificate verification dialog),
  901. // allow wider box to fit it
  902. if (TRegEx::IsMatch(Msg, L"([0-9a-fA-F]{2}[:\-]){31}[0-9a-fA-F]{2}"))
  903. {
  904. MaxTextWidth = MaxTextWidth * 3 / 2;
  905. }
  906. // if the dialog would be wide anyway (overwrite confirmation on Windows XP),
  907. // to fit the buttons, do not restrict the text
  908. if (MaxTextWidth < ButtonGroupWidth - IconWidth)
  909. {
  910. MaxTextWidth = ButtonGroupWidth - IconWidth;
  911. }
  912. UnicodeString BodyMsg = Msg;
  913. BodyMsg = RemoveInteractiveMsgTag(BodyMsg);
  914. UnicodeString MainMsg;
  915. if (ExtractMainInstructions(BodyMsg, MainMsg))
  916. {
  917. Result->MessageText = MainMsg + BodyMsg;
  918. BodyMsg = BodyMsg.TrimLeft();
  919. }
  920. else
  921. {
  922. Result->MessageText = BodyMsg;
  923. }
  924. ApplyTabs(Result->MessageText, L' ', NULL, NULL);
  925. // Windows XP (not sure about Vista) does not support Hair space.
  926. // For Windows XP, we still keep the existing hack by using hard-coded spaces
  927. // in resource string
  928. if (IsWin7())
  929. {
  930. // Have to be padding with spaces (the smallest space defined, hair space = 1px),
  931. // as tabs actually do not tab, just expand to 8 spaces.
  932. // Otherwise we would have to do custom drawing
  933. // (using GetTabbedTextExtent and TabbedTextOut)
  934. const wchar_t HairSpace = L'\x200A';
  935. ApplyTabs(BodyMsg, HairSpace, CalculateWidthOnCanvas, Result->Canvas);
  936. }
  937. DebugAssert(MainMsg.Pos(L"\t") == 0);
  938. int IconTextWidth = -1;
  939. int IconTextHeight = 0;
  940. int ALeft = IconWidth + HorzMargin;
  941. for (int MessageIndex = 0; MessageIndex <= 1; MessageIndex++)
  942. {
  943. UnicodeString LabelMsg;
  944. UnicodeString LabelName;
  945. TColor LabelColor = Graphics::clNone;
  946. HFONT LabelFont = 0;
  947. switch (MessageIndex)
  948. {
  949. case 0:
  950. LabelMsg = MainMsg;
  951. LabelName = MainMessageLabelName;
  952. LabelColor = MainInstructionColor;
  953. LabelFont = MainInstructionFont;
  954. break;
  955. case 1:
  956. LabelMsg = BodyMsg;
  957. LabelName = MessageLabelName;
  958. break;
  959. default:
  960. DebugFail();
  961. break;
  962. }
  963. if (!LabelMsg.IsEmpty())
  964. {
  965. TLabel * Message = new TLabel(Panel);
  966. Message->Parent = Panel;
  967. Message->Name = LabelName;
  968. Message->WordWrap = true;
  969. Message->Caption = LabelMsg;
  970. Message->BiDiMode = Result->BiDiMode;
  971. // added to show & as & for messages containing !& pattern of custom commands
  972. // (suppose that we actually never want to use & as accel in message text)
  973. Message->ShowAccelChar = false;
  974. if (LabelFont != 0)
  975. {
  976. Message->Font->Handle = LabelFont;
  977. if (DebugAlwaysTrue(LabelFont == MainInstructionFont) &&
  978. // When showing an early error message
  979. (Configuration != NULL))
  980. {
  981. Configuration->Usage->Set(L"ThemeMainInstructionFontSize", Message->Font->Size);
  982. }
  983. }
  984. if (LabelColor != Graphics::clNone)
  985. {
  986. Message->Font->Color = LabelColor;
  987. }
  988. TRect TextRect;
  989. SetRect(&TextRect, 0, 0, MaxTextWidth, 0);
  990. DrawText(Message->Canvas->Handle, LabelMsg.c_str(), LabelMsg.Length() + 1, &TextRect,
  991. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX |
  992. Result->DrawTextBiDiModeFlagsReadingOnly());
  993. int MaxWidth = Monitor->Width - HorzMargin * 2 - IconWidth - 30;
  994. // 5% buffer for potential WM_DPICHANGED, as after re-scaling the text can otherwise narrowly not fit in.
  995. // Though note that the buffer is lost on the first re-scale due to the AutoSize
  996. TextRect.right = MulDiv(TextRect.right, 105, 100);
  997. // this will truncate the text, we should implement something smarter eventually
  998. TextRect.right = Min(TextRect.right, MaxWidth);
  999. IconTextWidth = Max(IconTextWidth, IconWidth + TextRect.Right);
  1000. if (IconTextHeight > 0)
  1001. {
  1002. IconTextHeight += VertMargin;
  1003. }
  1004. Message->SetBounds(ALeft, VertMargin + IconTextHeight, TextRect.Right, TextRect.Bottom);
  1005. IconTextHeight += TextRect.Bottom;
  1006. }
  1007. }
  1008. if (LinkControl != NULL)
  1009. {
  1010. LinkControl->Parent = Panel;
  1011. LinkActionLabel(LinkControl);
  1012. LinkControl->Left = Panel->ClientWidth - HorzMargin - LinkControl->Width;
  1013. LinkControl->Top = VertMargin + IconTextHeight + VertMargin;
  1014. IconTextHeight += VertMargin + LinkControl->Height;
  1015. }
  1016. DebugAssert((IconTextWidth > 0) && (IconTextHeight > 0));
  1017. IconTextHeight = Max(IconTextHeight, IconHeight);
  1018. int MoreMessageHeight =
  1019. (HasMoreMessages ?
  1020. ScaleByTextHeightRunTime(Result, (MoreMessagesSize.Height > 0 ? MoreMessagesSize.Height : mcMoreMessageHeight)) : 0);
  1021. Panel->SetBounds(0, 0, Result->ClientWidth, VertMargin + IconTextHeight + VertMargin + MoreMessageHeight);
  1022. TControl * MoreMessagesControl = NULL;
  1023. if (HasMoreMessages)
  1024. {
  1025. if (MoreMessages != NULL)
  1026. {
  1027. DebugAssert(MoreMessagesUrl.IsEmpty());
  1028. TMemo * MessageMemo = new TMemo(Panel);
  1029. MoreMessagesControl = MessageMemo;
  1030. MessageMemo->Name = L"MessageMemo";
  1031. MessageMemo->Parent = Panel;
  1032. MessageMemo->ReadOnly = true;
  1033. MessageMemo->WantReturns = False;
  1034. MessageMemo->ScrollBars = ssVertical;
  1035. MessageMemo->Anchors = TAnchors() << akLeft << akRight << akTop;
  1036. MessageMemo->Lines->Text = MoreMessages->Text;
  1037. Result->MessageMemo = MessageMemo;
  1038. }
  1039. else if (DebugAlwaysTrue(!MoreMessagesUrl.IsEmpty()))
  1040. {
  1041. TPanel * MessageBrowserPanel = CreateBlankPanel(Panel);
  1042. MessageBrowserPanel->Parent = Panel;
  1043. MessageBrowserPanel->Anchors = TAnchors() << akLeft << akRight << akTop;
  1044. MessageBrowserPanel->BevelKind = bkTile; // flat border
  1045. Result->MessageBrowserPanel = MessageBrowserPanel;
  1046. MoreMessagesControl = Result->MessageBrowserPanel;
  1047. Result->MessageBrowserUrl = CampaignUrl(MoreMessagesUrl);
  1048. }
  1049. }
  1050. int MinClientWidth =
  1051. ScaleByTextHeightRunTime(Result,
  1052. HasMoreMessages ? (MoreMessagesSize.Width > 0 ? MoreMessagesSize.Width : mcMinDialogwithMoreMessagesWidth) : mcMinDialogWidth);
  1053. int AClientWidth =
  1054. Max(
  1055. (IconTextWidth > ButtonGroupWidth ? IconTextWidth : ButtonGroupWidth) +
  1056. HorzMargin * 2,
  1057. MinClientWidth);
  1058. Result->ClientWidth = AClientWidth;
  1059. Result->ClientHeight =
  1060. Panel->Height + ButtonVertMargin + ButtonHeight + ButtonVertMargin;
  1061. if (!CustomCaption.IsEmpty())
  1062. {
  1063. Result->Caption = CustomCaption;
  1064. }
  1065. else if (DebugAlwaysTrue(DlgType != mtCustom))
  1066. {
  1067. Result->Caption = LoadResourceString(Captions[DlgType]);
  1068. }
  1069. else
  1070. {
  1071. Result->Caption = Application->Title;
  1072. }
  1073. if (MoreMessagesControl != NULL)
  1074. {
  1075. MoreMessagesControl->SetBounds(
  1076. ALeft,
  1077. Panel->Height - MoreMessageHeight,
  1078. Result->ClientWidth - ALeft - HorzMargin,
  1079. MoreMessageHeight - VertMargin);
  1080. }
  1081. int ButtonTop = Panel->Height + ButtonVertMargin;
  1082. int X = Result->ClientWidth - ButtonGroupWidth + NeverAskAgainWidth - HorzMargin;
  1083. for (unsigned int i = 0; i < ButtonControls.size(); i++)
  1084. {
  1085. ButtonControls[i]->SetBounds(
  1086. X, ButtonTop, ButtonControls[i]->Width, ButtonControls[i]->Height);
  1087. X += ButtonControls[i]->Width + ButtonSpacing;
  1088. }
  1089. if (!NeverAskAgainCaption.IsEmpty() &&
  1090. !ButtonControls.empty())
  1091. {
  1092. Result->NeverAskAgainCheck = new TCheckBox(Result);
  1093. Result->NeverAskAgainCheck->Name = L"NeverAskAgainCheck";
  1094. Result->NeverAskAgainCheck->Parent = Result;
  1095. Result->NeverAskAgainCheck->Caption = NeverAskAgainCaption;
  1096. // Previously we set anchor to akBottom, but as we do not do that for buttons, we removed that.
  1097. // When showing window on 100% DPI monitor, with system DPI 100%, but main monitor 150%,
  1098. // the title bar seems to start on 150% DPI reducing later, leaving the form client height
  1099. // sligtly higher than needed and the checkbox being aligned differently than the button.
  1100. // Removing the akBottom aligning improves this sligtly, while the main problem still should be fixed.
  1101. TButton * FirstButton = ButtonControls[0];
  1102. int NeverAskAgainHeight = ScaleByTextHeightRunTime(Result, Result->NeverAskAgainCheck->Height);
  1103. int NeverAskAgainTop = FirstButton->Top + ((FirstButton->Height - NeverAskAgainHeight) / 2);
  1104. int NeverAskAgainLeft = HorzMargin + ScaleByTextHeightRunTime(Result, 2); // checkbox indentation
  1105. Result->NeverAskAgainCheck->SetBounds(
  1106. NeverAskAgainLeft, NeverAskAgainTop, NeverAskAgainBaseWidth, NeverAskAgainHeight);
  1107. }
  1108. return Result;
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. TForm * __fastcall CreateMoreMessageDialog(const UnicodeString & Msg,
  1112. TStrings * MoreMessages, TMsgDlgType DlgType, unsigned int Answers,
  1113. const TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  1114. unsigned int TimeoutAnswer, TButton ** TimeoutButton, const UnicodeString & ImageName,
  1115. const UnicodeString & NeverAskAgainCaption, const UnicodeString & MoreMessagesUrl,
  1116. TSize MoreMessagesSize, const UnicodeString & CustomCaption)
  1117. {
  1118. return TMessageForm::Create(Msg, MoreMessages, DlgType, Answers,
  1119. Aliases, AliasesCount, TimeoutAnswer, TimeoutButton, ImageName,
  1120. NeverAskAgainCaption, MoreMessagesUrl, MoreMessagesSize, CustomCaption);
  1121. }
  1122. //---------------------------------------------------------------------------
  1123. void __fastcall InsertPanelToMessageDialog(TCustomForm * Form, TPanel * Panel)
  1124. {
  1125. TMessageForm * MessageForm = DebugNotNull(dynamic_cast<TMessageForm *>(Form));
  1126. MessageForm->InsertPanel(Panel);
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. void __fastcall NavigateMessageDialogToUrl(TCustomForm * Form, const UnicodeString & Url)
  1130. {
  1131. TMessageForm * MessageForm = DebugNotNull(dynamic_cast<TMessageForm *>(Form));
  1132. MessageForm->NavigateToUrl(Url);
  1133. }
  1134. //---------------------------------------------------------------------------
  1135. int __fastcall GetMessageDialogContentWidth(TCustomForm * Form)
  1136. {
  1137. TMessageForm * MessageForm = DebugNotNull(dynamic_cast<TMessageForm *>(Form));
  1138. return MessageForm->GetContentWidth();
  1139. }