MessageDlg.cpp 42 KB

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