MessageDlg.cpp 42 KB

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