1
0

MessageDlg.cpp 42 KB

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