MessageDlg.cpp 41 KB

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