MessageDlg.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. //---------------------------------------------------------------------------
  19. #pragma package(smart_init)
  20. //---------------------------------------------------------------------------
  21. class TMessageButton : public TButton
  22. {
  23. public:
  24. __fastcall TMessageButton(TComponent * Owner);
  25. protected:
  26. virtual void __fastcall Dispatch(void * Message);
  27. private:
  28. void __fastcall WMGetDlgCode(TWMGetDlgCode & Message);
  29. };
  30. //---------------------------------------------------------------------------
  31. __fastcall TMessageButton::TMessageButton(TComponent * Owner) :
  32. TButton(Owner)
  33. {
  34. }
  35. //---------------------------------------------------------------------------
  36. void __fastcall TMessageButton::Dispatch(void * Message)
  37. {
  38. TMessage * M = reinterpret_cast<TMessage*>(Message);
  39. if (M->Msg == WM_GETDLGCODE)
  40. {
  41. WMGetDlgCode(*((TWMGetDlgCode *)Message));
  42. }
  43. else
  44. {
  45. TButton::Dispatch(Message);
  46. }
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TMessageButton::WMGetDlgCode(TWMGetDlgCode & Message)
  50. {
  51. TButton::Dispatch(&Message);
  52. // WORKAROUND
  53. // Windows default handler returns DLGC_WANTARROWS for split buttons,
  54. // what prevent left/right keys from being used for focusing next/previous buttons/controls.
  55. // Overrwide that. Though note that we need to pass the up/down keys back to button
  56. // to allow drop down, see TMessageForm::CMDialogKey
  57. Message.Result = Message.Result & ~DLGC_WANTARROWS;
  58. }
  59. //---------------------------------------------------------------------------
  60. class TMessageForm : public TForm
  61. {
  62. public:
  63. static TForm * __fastcall Create(const UnicodeString & Msg, TStrings * MoreMessages,
  64. TMsgDlgType DlgType, unsigned int Answers,
  65. const TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  66. unsigned int TimeoutAnswer, TButton ** TimeoutButton, const UnicodeString & ImageName,
  67. const UnicodeString & NeverAskAgainCaption);
  68. protected:
  69. __fastcall TMessageForm(TComponent * AOwner);
  70. virtual __fastcall ~TMessageForm();
  71. DYNAMIC void __fastcall KeyDown(Word & Key, TShiftState Shift);
  72. DYNAMIC void __fastcall KeyUp(Word & Key, TShiftState Shift);
  73. UnicodeString __fastcall GetFormText();
  74. UnicodeString __fastcall GetReportText();
  75. UnicodeString __fastcall NormalizeNewLines(UnicodeString Text);
  76. virtual void __fastcall CreateParams(TCreateParams & Params);
  77. DYNAMIC void __fastcall DoShow();
  78. virtual void __fastcall Dispatch(void * Message);
  79. void __fastcall MenuItemClick(TObject * Sender);
  80. void __fastcall ButtonDropDownClick(TObject * Sender);
  81. void __fastcall UpdateForShiftStateTimer(TObject * Sender);
  82. private:
  83. typedef std::map<unsigned int, TButton *> TAnswerButtons;
  84. UnicodeString MessageText;
  85. TMemo * MessageMemo;
  86. TShiftState FShiftState;
  87. TTimer * FUpdateForShiftStateTimer;
  88. TForm * FDummyForm;
  89. void __fastcall HelpButtonClick(TObject * Sender);
  90. void __fastcall ReportButtonClick(TObject * Sender);
  91. void __fastcall CMDialogKey(TWMKeyDown & Message);
  92. void __fastcall UpdateForShiftState();
  93. TButton * __fastcall CreateButton(
  94. UnicodeString Name, UnicodeString Caption, unsigned int Answer,
  95. TNotifyEvent OnClick, bool IsTimeoutButton,
  96. int GroupWith, TShiftState GrouppedShiftState,
  97. TAnswerButtons & AnswerButtons, bool HasMoreMessages, int & ButtonWidth);
  98. };
  99. //---------------------------------------------------------------------------
  100. __fastcall TMessageForm::TMessageForm(TComponent * AOwner) : TForm(AOwner, 0)
  101. {
  102. MessageMemo = NULL;
  103. FUpdateForShiftStateTimer = NULL;
  104. Position = poOwnerFormCenter;
  105. UseSystemSettingsPre(this);
  106. FDummyForm = new TForm(this);
  107. UseSystemSettings(FDummyForm);
  108. }
  109. //---------------------------------------------------------------------------
  110. __fastcall TMessageForm::~TMessageForm()
  111. {
  112. SAFE_DESTROY(FDummyForm);
  113. SAFE_DESTROY(FUpdateForShiftStateTimer);
  114. }
  115. //---------------------------------------------------------------------------
  116. void __fastcall TMessageForm::HelpButtonClick(TObject * /*Sender*/)
  117. {
  118. if (HelpKeyword != HELP_NONE)
  119. {
  120. FormHelp(this);
  121. }
  122. else
  123. {
  124. MessageWithNoHelp(GetReportText());
  125. }
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TMessageForm::ReportButtonClick(TObject * /*Sender*/)
  129. {
  130. UnicodeString Url =
  131. FMTLOAD(ERROR_REPORT_URL,
  132. (EncodeUrlString(GetReportText()), Configuration->ProductVersion,
  133. IntToHex(__int64(GUIConfiguration->Locale), 4)));
  134. OpenBrowser(Url);
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TMessageForm::UpdateForShiftState()
  138. {
  139. TShiftState ShiftState =
  140. KeyboardStateToShiftState() *
  141. (TShiftState() << ssShift << ssCtrl << ssAlt);
  142. if (FShiftState != ShiftState)
  143. {
  144. FShiftState = ShiftState;
  145. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  146. {
  147. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  148. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  149. {
  150. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  151. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  152. {
  153. TMenuItem * Item = MenuItems->Items[ItemIndex];
  154. TShiftState GrouppedShiftState(Item->Tag >> 16);
  155. if (Item->Enabled &&
  156. ((ShiftState.Empty() && Item->Default) ||
  157. (!ShiftState.Empty() && (ShiftState == GrouppedShiftState))))
  158. {
  159. int From = 1;
  160. Button->Caption = CopyToChars(Item->Caption, From, L"\t", false);
  161. Button->ModalResult = Item->Tag & 0xFFFF;
  162. assert(Button->OnClick == NULL);
  163. assert(Item->OnClick == MenuItemClick);
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. //---------------------------------------------------------------------------
  172. void __fastcall TMessageForm::KeyUp(Word & Key, TShiftState Shift)
  173. {
  174. UpdateForShiftState();
  175. TForm::KeyUp(Key, Shift);
  176. }
  177. //---------------------------------------------------------------------------
  178. void __fastcall TMessageForm::KeyDown(Word & Key, TShiftState Shift)
  179. {
  180. if (Shift.Contains(ssCtrl) && (Key == L'C'))
  181. {
  182. CopyToClipboard(GetFormText());
  183. }
  184. else
  185. {
  186. if (!Shift.Contains(ssCtrl))
  187. {
  188. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  189. {
  190. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  191. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  192. {
  193. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  194. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  195. {
  196. TMenuItem * Item = MenuItems->Items[ItemIndex];
  197. if (IsAccel(Key, MenuItems->Items[ItemIndex]->Caption))
  198. {
  199. Item->OnClick(Item);
  200. Key = 0;
  201. break;
  202. }
  203. }
  204. }
  205. if (Key == 0)
  206. {
  207. break;
  208. }
  209. }
  210. }
  211. UpdateForShiftState();
  212. TForm::KeyDown(Key, Shift);
  213. }
  214. }
  215. //---------------------------------------------------------------------------
  216. UnicodeString __fastcall TMessageForm::NormalizeNewLines(UnicodeString Text)
  217. {
  218. Text = ReplaceStr(Text, L"\r", L"");
  219. Text = ReplaceStr(Text, L"\n", L"\r\n");
  220. return Text;
  221. }
  222. //---------------------------------------------------------------------------
  223. UnicodeString __fastcall TMessageForm::GetFormText()
  224. {
  225. UnicodeString DividerLine, ButtonCaptions;
  226. DividerLine = UnicodeString::StringOfChar(L'-', 27) + sLineBreak;
  227. for (int i = 0; i < ComponentCount - 1; i++)
  228. {
  229. if (dynamic_cast<TButton*>(Components[i]) != NULL)
  230. {
  231. ButtonCaptions += dynamic_cast<TButton*>(Components[i])->Caption +
  232. UnicodeString::StringOfChar(L' ', 3);
  233. }
  234. }
  235. ButtonCaptions = ReplaceStr(ButtonCaptions, L"&", L"");
  236. UnicodeString MoreMessages;
  237. if (MessageMemo != NULL)
  238. {
  239. MoreMessages = MessageMemo->Text + DividerLine;
  240. }
  241. UnicodeString MessageCaption = NormalizeNewLines(MessageText);
  242. UnicodeString Result = FORMAT(L"%s%s%s%s%s%s%s%s%s%s%s", (DividerLine, Caption, sLineBreak,
  243. DividerLine, MessageCaption, sLineBreak, DividerLine, MoreMessages,
  244. ButtonCaptions, sLineBreak, DividerLine));
  245. return Result;
  246. }
  247. //---------------------------------------------------------------------------
  248. UnicodeString __fastcall TMessageForm::GetReportText()
  249. {
  250. UnicodeString Text = MessageText;
  251. if (MessageMemo != NULL)
  252. {
  253. Text += L"\n" + MessageMemo->Text;
  254. }
  255. Text = NormalizeNewLines(Text);
  256. UnicodeString ReportErrorText = NormalizeNewLines(FMTLOAD(REPORT_ERROR, (L"")));
  257. Text = ReplaceStr(Text, ReportErrorText, L"");
  258. Text = Trim(Text);
  259. return Text;
  260. }
  261. //---------------------------------------------------------------------------
  262. void __fastcall TMessageForm::CMDialogKey(TWMKeyDown & Message)
  263. {
  264. // this gets used in WinInterface.cpp SetTimeoutEvents
  265. if (OnKeyDown != NULL)
  266. {
  267. OnKeyDown(this, Message.CharCode, KeyDataToShiftState(Message.KeyData));
  268. }
  269. if (Message.CharCode == VK_MENU)
  270. {
  271. bool AnyButtonWithGrouppedCommandsWithShiftState = false;
  272. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  273. {
  274. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  275. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  276. {
  277. // we should check if there are any commands with shift state,
  278. // but it's bit overkill
  279. AnyButtonWithGrouppedCommandsWithShiftState = true;
  280. break;
  281. }
  282. }
  283. // this is to make Alt only alter button meaning (if there is any
  284. // alternable button) and not popup system menu
  285. if (AnyButtonWithGrouppedCommandsWithShiftState)
  286. {
  287. Message.Result = 1;
  288. UpdateForShiftState();
  289. }
  290. else
  291. {
  292. TForm::Dispatch(&Message);
  293. }
  294. }
  295. else if ((Message.CharCode == VK_UP) || (Message.CharCode == VK_DOWN))
  296. {
  297. // WORKAROUND
  298. // noop to make up/down be passed back to button to allow drop down,
  299. // see TMessageButton::WMGetDlgCode
  300. }
  301. else
  302. {
  303. TForm::Dispatch(&Message);
  304. }
  305. }
  306. //---------------------------------------------------------------------------
  307. void __fastcall TMessageForm::Dispatch(void * Message)
  308. {
  309. TMessage * M = reinterpret_cast<TMessage*>(Message);
  310. if (M->Msg == CM_DIALOGKEY)
  311. {
  312. CMDialogKey(*((TWMKeyDown *)Message));
  313. }
  314. else
  315. {
  316. TForm::Dispatch(Message);
  317. }
  318. }
  319. //---------------------------------------------------------------------------
  320. void __fastcall TMessageForm::CreateParams(TCreateParams & Params)
  321. {
  322. TForm::CreateParams(Params);
  323. if ((Screen != NULL) && (Screen->ActiveForm != NULL) &&
  324. Screen->ActiveForm->HandleAllocated())
  325. {
  326. Params.WndParent = Screen->ActiveForm->Handle;
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TMessageForm::DoShow()
  331. {
  332. UseSystemSettingsPost(this);
  333. TForm::DoShow();
  334. }
  335. //---------------------------------------------------------------------------
  336. void __fastcall TMessageForm::MenuItemClick(TObject * Sender)
  337. {
  338. TMenuItem * Item = NOT_NULL(dynamic_cast<TMenuItem *>(Sender));
  339. ModalResult = (Item->Tag & 0xFFFF);
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TMessageForm::UpdateForShiftStateTimer(TObject * /*Sender*/)
  343. {
  344. // this is needed to reflect shift state, even when we do not have a keyboard
  345. // focus, what happens when drop down menu is popped up
  346. UpdateForShiftState();
  347. }
  348. //---------------------------------------------------------------------------
  349. void __fastcall TMessageForm::ButtonDropDownClick(TObject * /*Sender*/)
  350. {
  351. // as optimization, do not waste time running timer, unless
  352. // user pops up drop down menu. we do not have a way to stop timer, once
  353. // it closes, but functionaly is does not matter
  354. if (FUpdateForShiftStateTimer == NULL)
  355. {
  356. FUpdateForShiftStateTimer = new TTimer(this);
  357. FUpdateForShiftStateTimer->Interval = 50;
  358. FUpdateForShiftStateTimer->OnTimer = UpdateForShiftStateTimer;
  359. }
  360. }
  361. //---------------------------------------------------------------------------
  362. const ResourceString * Captions[] = { &_SMsgDlgWarning, &_SMsgDlgError, &_SMsgDlgInformation,
  363. &_SMsgDlgConfirm, NULL };
  364. const wchar_t * IconIDs[] = { IDI_EXCLAMATION, IDI_HAND, IDI_ASTERISK,
  365. IDI_QUESTION, NULL };
  366. const int mcHorzMargin = 10;
  367. const int mcVertMargin = 13;
  368. const int mcHorzSpacing = 12;
  369. const int mcButtonVertMargin = 7;
  370. const int mcButtonSpacing = 5;
  371. // includes mcVertMargin
  372. const int mcMoreMessageHeight = 86;
  373. // approximately what Windows Vista task dialogs use,
  374. // actually they probably has fixed width
  375. const int mcMaxDialogWidth = 340;
  376. const int mcMinDialogWidth = 310;
  377. const int mcMinDialogwithMoreMessagesWidth = 400;
  378. //---------------------------------------------------------------------------
  379. static UnicodeString __fastcall GetKeyNameStr(int Key)
  380. {
  381. wchar_t Buf[MAX_PATH];
  382. LONG VirtualKey = MapVirtualKey(Key, MAPVK_VK_TO_VSC);
  383. VirtualKey <<= 16;
  384. if (GetKeyNameText(VirtualKey, Buf, LENOF(Buf)) > 0)
  385. {
  386. NULL_TERMINATE(Buf);
  387. }
  388. else
  389. {
  390. Buf[0] = L'\0';
  391. }
  392. return Buf;
  393. }
  394. //---------------------------------------------------------------------------
  395. TButton * __fastcall TMessageForm::CreateButton(
  396. UnicodeString Name, UnicodeString Caption, unsigned int Answer,
  397. TNotifyEvent OnClick, bool IsTimeoutButton,
  398. int GroupWith, TShiftState GrouppedShiftState,
  399. TAnswerButtons & AnswerButtons, bool HasMoreMessages, int & ButtonWidth)
  400. {
  401. UnicodeString MeasureCaption = Caption;
  402. if (IsTimeoutButton)
  403. {
  404. MeasureCaption = FMTLOAD(TIMEOUT_BUTTON, (MeasureCaption, 99));
  405. }
  406. TRect TextRect;
  407. DrawText(Canvas->Handle,
  408. UnicodeString(MeasureCaption).c_str(), -1,
  409. &TextRect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE |
  410. DrawTextBiDiModeFlagsReadingOnly());
  411. int CurButtonWidth = TextRect.Right - TextRect.Left + ScaleByTextHeightRunTime(this, 16);
  412. TButton * Button = NULL;
  413. if (SupportsSplitButton() &&
  414. (GroupWith >= 0) &&
  415. ALWAYS_TRUE(AnswerButtons.find(GroupWith) != AnswerButtons.end()))
  416. {
  417. TButton * GroupWithButton = AnswerButtons[GroupWith];
  418. if (GroupWithButton->DropDownMenu == NULL)
  419. {
  420. GroupWithButton->Style = TCustomButton::bsSplitButton;
  421. GroupWithButton->DropDownMenu = new TPopupMenu(this);
  422. // cannot handle subitems with shift state,
  423. // if the button has its own handler
  424. // (though it may not be the case still here)
  425. assert(GroupWithButton->OnClick == NULL);
  426. TMenuItem * Item = new TMenuItem(GroupWithButton->DropDownMenu);
  427. GroupWithButton->DropDownMenu->Items->Add(Item);
  428. GroupWithButton->OnDropDownClick = ButtonDropDownClick;
  429. Item->Caption = GroupWithButton->Caption;
  430. Item->OnClick = MenuItemClick;
  431. assert(GroupWithButton->ModalResult <= 0xFFFF);
  432. Item->Tag = GroupWithButton->ModalResult;
  433. Item->Default = true;
  434. }
  435. TMenuItem * Item = new TMenuItem(GroupWithButton->DropDownMenu);
  436. GroupWithButton->DropDownMenu->Items->Add(Item);
  437. // See ShortCutToText in Vcl.Menus.pas
  438. if (GrouppedShiftState == (TShiftState() << ssAlt))
  439. {
  440. Caption = Caption + L"\t" + GetKeyNameStr(VK_MENU);
  441. }
  442. else if (GrouppedShiftState == (TShiftState() << ssCtrl))
  443. {
  444. Caption = Caption + L"\t" + GetKeyNameStr(VK_CONTROL);
  445. }
  446. else if (GrouppedShiftState == (TShiftState() << ssShift))
  447. {
  448. Caption = Caption + L"\t" + GetKeyNameStr(VK_SHIFT);
  449. }
  450. else
  451. {
  452. // do not support combined shift states yet
  453. assert(GrouppedShiftState == TShiftState());
  454. }
  455. Item->Caption = Caption;
  456. if (OnClick != NULL)
  457. {
  458. Item->OnClick = OnClick;
  459. }
  460. else
  461. {
  462. Item->OnClick = MenuItemClick;
  463. assert((Answer <= 0xFFFF) && (GrouppedShiftState.ToInt() <= 0xFFFF));
  464. Item->Tag = Answer + (GrouppedShiftState.ToInt() << 16);
  465. }
  466. // Hard-coded drop down button width (do not know how to ask for system width).
  467. // Also we do not update the max button width for the default groupped
  468. // button caption. We just blindly hope that captions of advanced commands
  469. // are always longer than the caption of simple default command
  470. CurButtonWidth += ScaleByTextHeightRunTime(this, 15);
  471. }
  472. else
  473. {
  474. Button = new TMessageButton(this);
  475. Button->Name = Name;
  476. Button->Parent = this;
  477. Button->Caption = Caption;
  478. // Scale buttons using regular font, so that they are as large as buttons
  479. // on other dialogs (note that they are still higher than Windows Task dialog
  480. // buttons)
  481. Button->Height = ScaleByTextHeightRunTime(FDummyForm, Button->Height);
  482. Button->Width = ScaleByTextHeightRunTime(FDummyForm, Button->Width);
  483. if (OnClick != NULL)
  484. {
  485. Button->OnClick = OnClick;
  486. }
  487. else
  488. {
  489. Button->ModalResult = Answer;
  490. }
  491. if (HasMoreMessages)
  492. {
  493. Button->Anchors = TAnchors() << akBottom << akLeft;
  494. }
  495. // never shrink buttons below their default width
  496. if (Button->Width > CurButtonWidth)
  497. {
  498. CurButtonWidth = Button->Width;
  499. }
  500. }
  501. if (CurButtonWidth > ButtonWidth)
  502. {
  503. ButtonWidth = CurButtonWidth;
  504. }
  505. return Button;
  506. }
  507. //---------------------------------------------------------------------------
  508. void __fastcall AnswerNameAndCaption(
  509. unsigned int Answer, UnicodeString & Name, UnicodeString & Caption)
  510. {
  511. switch (Answer)
  512. {
  513. case qaYes:
  514. Caption = LoadStr(_SMsgDlgYes.Identifier);
  515. Name = L"Yes";
  516. break;
  517. case qaNo:
  518. Caption = LoadStr(_SMsgDlgNo.Identifier);
  519. Name = L"No";
  520. break;
  521. case qaOK:
  522. Caption = LoadStr(_SMsgDlgOK.Identifier);
  523. Name = L"OK";
  524. break;
  525. case qaCancel:
  526. Caption = LoadStr(_SMsgDlgCancel.Identifier);
  527. Name = L"Cancel";
  528. break;
  529. case qaAbort:
  530. Caption = LoadStr(_SMsgDlgAbort.Identifier);
  531. Name = L"Abort";
  532. break;
  533. case qaRetry:
  534. Caption = LoadStr(_SMsgDlgRetry.Identifier);
  535. Name = L"Retry";
  536. break;
  537. case qaIgnore:
  538. Caption = LoadStr(_SMsgDlgIgnore.Identifier);
  539. Name = L"Ignore";
  540. break;
  541. // Own variant to avoid accelerator conflict with "Abort" button.
  542. // Note that as of now, ALL_BUTTON is never actually used,
  543. // because qaAll is always aliased
  544. case qaAll:
  545. Caption = LoadStr(ALL_BUTTON);
  546. Name = L"All";
  547. break;
  548. case qaNoToAll:
  549. Caption = LoadStr(_SMsgDlgNoToAll.Identifier);
  550. Name = L"NoToAll";
  551. break;
  552. // Own variant to avoid accelerator conflict with "Abort" button.
  553. case qaYesToAll:
  554. Caption = LoadStr(YES_TO_ALL_BUTTON);
  555. Name = L"YesToAll";
  556. break;
  557. case qaHelp:
  558. Caption = LoadStr(_SMsgDlgHelp.Identifier);
  559. Name = L"Help";
  560. break;
  561. case qaSkip:
  562. Caption = LoadStr(SKIP_BUTTON);
  563. Name = L"Skip";
  564. break;
  565. case qaReport:
  566. Caption = LoadStr(REPORT_BUTTON);
  567. Name = L"Report";
  568. break;
  569. default:
  570. FAIL;
  571. throw Exception(L"Undefined answer");
  572. }
  573. }
  574. //---------------------------------------------------------------------------
  575. static int __fastcall CalculateWidthOnCanvas(UnicodeString Text, void * Arg)
  576. {
  577. TCanvas * Canvas = static_cast<TCanvas *>(Arg);
  578. return Canvas->TextWidth(Text);
  579. }
  580. //---------------------------------------------------------------------------
  581. TForm * __fastcall TMessageForm::Create(const UnicodeString & Msg,
  582. TStrings * MoreMessages, TMsgDlgType DlgType, unsigned int Answers,
  583. const TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  584. unsigned int TimeoutAnswer, TButton ** TimeoutButton, const UnicodeString & ImageName,
  585. const UnicodeString & NeverAskAgainCaption)
  586. {
  587. unsigned int DefaultAnswer;
  588. if (FLAGSET(Answers, qaOK))
  589. {
  590. DefaultAnswer = qaOK;
  591. }
  592. else if (FLAGSET(Answers, qaYes))
  593. {
  594. DefaultAnswer = qaYes;
  595. }
  596. else
  597. {
  598. DefaultAnswer = qaRetry;
  599. }
  600. unsigned int CancelAnswer = ::CancelAnswer(Answers);
  601. if (TimeoutButton != NULL)
  602. {
  603. *TimeoutButton = NULL;
  604. }
  605. TMessageForm * Result = SafeFormCreate<TMessageForm>();
  606. UseDesktopFont(Result);
  607. // make sure we consider sizes of the monitor,
  608. // that is set in DoFormWindowProc(CM_SHOWINGCHANGED) later.
  609. Forms::TMonitor * Monitor = FormMonitor(Result);
  610. Result->BiDiMode = Application->BiDiMode;
  611. Result->BorderStyle = bsDialog;
  612. Result->Canvas->Font = Result->Font;
  613. Result->KeyPreview = true;
  614. int HorzMargin = ScaleByTextHeightRunTime(Result, mcHorzMargin);
  615. int VertMargin = ScaleByTextHeightRunTime(Result, mcVertMargin);
  616. int HorzSpacing = ScaleByTextHeightRunTime(Result, mcHorzSpacing);
  617. int ButtonVertMargin = ScaleByTextHeightRunTime(Result, mcButtonVertMargin);
  618. int ButtonWidth = -1;
  619. int ButtonHeight = -1;
  620. std::vector<TButton *> ButtonControls;
  621. TAnswerButtons AnswerButtons;
  622. for (unsigned int Answer = qaFirst; Answer <= qaLast; Answer = Answer << 1)
  623. {
  624. if (FLAGSET(Answers, Answer))
  625. {
  626. assert(Answer != mrCancel);
  627. UnicodeString Caption;
  628. UnicodeString Name;
  629. AnswerNameAndCaption(Answer, Name, Caption);
  630. TNotifyEvent OnClick = NULL;
  631. int GroupWith = -1;
  632. TShiftState GrouppedShiftState;
  633. if (Aliases != NULL)
  634. {
  635. for (unsigned int i = 0; i < AliasesCount; i++)
  636. {
  637. if (Answer == Aliases[i].Button)
  638. {
  639. if (!Aliases[i].Alias.IsEmpty())
  640. {
  641. Caption = Aliases[i].Alias;
  642. }
  643. OnClick = Aliases[i].OnClick;
  644. GroupWith = Aliases[i].GroupWith;
  645. GrouppedShiftState = Aliases[i].GrouppedShiftState;
  646. assert((OnClick == NULL) || (GrouppedShiftState == TShiftState()));
  647. break;
  648. }
  649. }
  650. }
  651. // we hope that all grouped-with buttons are for answer with greater
  652. // value that the answer to be grouped with
  653. if (GroupWith >= 0)
  654. {
  655. if (ALWAYS_FALSE(GroupWith >= static_cast<int>(Answer)) ||
  656. ALWAYS_FALSE(Answer == TimeoutAnswer) &&
  657. ALWAYS_FALSE(Answer == DefaultAnswer) &&
  658. ALWAYS_FALSE(Answer == CancelAnswer))
  659. {
  660. GroupWith = -1;
  661. }
  662. }
  663. bool IsTimeoutButton = (TimeoutButton != NULL) && (Answer == TimeoutAnswer);
  664. if (Answer == qaHelp)
  665. {
  666. assert(OnClick == NULL);
  667. OnClick = Result->HelpButtonClick;
  668. }
  669. if (Answer == qaReport)
  670. {
  671. assert(OnClick == NULL);
  672. OnClick = Result->ReportButtonClick;
  673. }
  674. TButton * Button = Result->CreateButton(
  675. Name, Caption, Answer,
  676. OnClick, IsTimeoutButton, GroupWith, GrouppedShiftState,
  677. AnswerButtons, (MoreMessages != NULL), ButtonWidth);
  678. if (Button != NULL)
  679. {
  680. ButtonControls.push_back(Button);
  681. Button->Default = (Answer == DefaultAnswer);
  682. Button->Cancel = (Answer == CancelAnswer);
  683. if (ButtonHeight < 0)
  684. {
  685. ButtonHeight = Button->Height;
  686. }
  687. assert(ButtonHeight == Button->Height);
  688. AnswerButtons.insert(TAnswerButtons::value_type(Answer, Button));
  689. if (IsTimeoutButton)
  690. {
  691. *TimeoutButton = Button;
  692. }
  693. }
  694. }
  695. }
  696. int NeverAskAgainWidth = 0;
  697. if (!NeverAskAgainCaption.IsEmpty())
  698. {
  699. NeverAskAgainWidth =
  700. ScaleByTextHeightRunTime(Result, 16) + // checkbox
  701. Result->Canvas->TextWidth(NeverAskAgainCaption) +
  702. ScaleByTextHeightRunTime(Result, 16); // margin
  703. }
  704. int ButtonSpacing = ScaleByTextHeightRunTime(Result, mcButtonSpacing);
  705. int ButtonGroupWidth = NeverAskAgainWidth;
  706. if (!ButtonControls.empty())
  707. {
  708. ButtonGroupWidth += ButtonWidth * ButtonControls.size() +
  709. ButtonSpacing * (ButtonControls.size() - 1);
  710. }
  711. int IconWidth = 0;
  712. const wchar_t * IconID = IconIDs[DlgType];
  713. bool HasIcon = (IconID != NULL) || !ImageName.IsEmpty();
  714. if (HasIcon)
  715. {
  716. IconWidth = 32 + HorzSpacing;
  717. }
  718. assert((ButtonHeight > 0) && (ButtonWidth > 0));
  719. int MaxTextWidth = ScaleByTextHeightRunTime(Result, mcMaxDialogWidth);
  720. // if the dialog would be wide anyway (overwrite confirmation on Windows XP),
  721. // to fit the buttons, do not restrict the text
  722. if (MaxTextWidth < ButtonGroupWidth - IconWidth)
  723. {
  724. MaxTextWidth = ButtonGroupWidth - IconWidth;
  725. }
  726. TPanel * Panel = new TPanel(Result);
  727. Panel->Name = L"Panel";
  728. Panel->Parent = Result;
  729. Panel->Color = clWindow;
  730. Panel->ParentBackground = false;
  731. Panel->Anchors = TAnchors() << akLeft << akRight << akTop;
  732. Panel->BevelOuter = bvNone;
  733. Panel->BevelKind = bkNone;
  734. Panel->Caption = L"";
  735. UnicodeString BodyMsg = Msg;
  736. UnicodeString MainMsg;
  737. if (ExtractMainInstructions(BodyMsg, MainMsg))
  738. {
  739. Result->MessageText = MainMsg + BodyMsg;
  740. BodyMsg = BodyMsg.TrimLeft();
  741. }
  742. else
  743. {
  744. Result->MessageText = BodyMsg;
  745. }
  746. ApplyTabs(Result->MessageText, L' ', NULL, NULL);
  747. // Windows XP (not sure about Vista) does not support Hair space.
  748. // For Windows XP, we still keep the existing hack by using hard-coded spaces
  749. // in resource string
  750. if (CheckWin32Version(6, 1))
  751. {
  752. // Have to be padding with spaces (the smallest space defined, hair space = 1px),
  753. // as tabs actually do not tab, just expand to 8 spaces.
  754. // Otherwise we would have to do custom drawing
  755. // (using GetTabbedTextExtent and TabbedTextOut)
  756. const wchar_t HairSpace = L'\x200A';
  757. ApplyTabs(BodyMsg, HairSpace, CalculateWidthOnCanvas, Result->Canvas);
  758. }
  759. assert(MainMsg.Pos(L"\t") == 0);
  760. TColor MainInstructionColor = Graphics::clNone;
  761. HFONT MainInstructionFont = 0;
  762. HTHEME Theme = OpenThemeData(0, L"TEXTSTYLE");
  763. if (Theme != NULL)
  764. {
  765. LOGFONT AMainInstructionFont;
  766. memset(&AMainInstructionFont, sizeof(AMainInstructionFont), 0);
  767. if (GetThemeFont(Theme, NULL, TEXT_MAININSTRUCTION, 0, TMT_FONT, &AMainInstructionFont) == S_OK)
  768. {
  769. MainInstructionFont = CreateFontIndirect(&AMainInstructionFont);
  770. }
  771. COLORREF AMainInstructionColor;
  772. if (GetThemeColor(Theme, TEXT_MAININSTRUCTION, 0, TMT_TEXTCOLOR, &AMainInstructionColor) == S_OK)
  773. {
  774. MainInstructionColor = (TColor)AMainInstructionColor;
  775. }
  776. CloseThemeData(Theme);
  777. }
  778. int IconTextWidth = -1;
  779. int IconTextHeight = 0;
  780. int ALeft = IconWidth + HorzMargin;
  781. for (int MessageIndex = 0; MessageIndex <= 1; MessageIndex++)
  782. {
  783. UnicodeString LabelMsg;
  784. UnicodeString LabelName;
  785. TColor LabelColor = Graphics::clNone;
  786. HFONT LabelFont = 0;
  787. switch (MessageIndex)
  788. {
  789. case 0:
  790. LabelMsg = MainMsg;
  791. LabelName = L"MainMessage";
  792. LabelColor = MainInstructionColor;
  793. LabelFont = MainInstructionFont;
  794. break;
  795. case 1:
  796. LabelMsg = BodyMsg;
  797. LabelName = L"Message";
  798. break;
  799. default:
  800. FAIL;
  801. break;
  802. }
  803. if (!LabelMsg.IsEmpty())
  804. {
  805. TLabel * Message = new TLabel(Panel);
  806. Message->Name = LabelName;
  807. Message->Parent = Panel;
  808. Message->WordWrap = true;
  809. Message->Caption = LabelMsg;
  810. Message->BiDiMode = Result->BiDiMode;
  811. // added to show & as & for messages containing !& pattern of custom commands
  812. // (suppose that we actually never want to use & as accel in message text)
  813. Message->ShowAccelChar = false;
  814. if (LabelFont != 0)
  815. {
  816. Message->Font->Handle = LabelFont;
  817. }
  818. if (LabelColor != Graphics::clNone)
  819. {
  820. Message->Font->Color = LabelColor;
  821. }
  822. TRect TextRect;
  823. SetRect(&TextRect, 0, 0, MaxTextWidth, 0);
  824. DrawText(Message->Canvas->Handle, LabelMsg.c_str(), LabelMsg.Length() + 1, &TextRect,
  825. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK |
  826. Result->DrawTextBiDiModeFlagsReadingOnly());
  827. int MaxWidth = Monitor->Width - HorzMargin * 2 - IconWidth - 30;
  828. if (TextRect.right > MaxWidth)
  829. {
  830. // this will truncate the text, we should implement something smarter eventually
  831. TextRect.right = MaxWidth;
  832. }
  833. IconTextWidth = Max(IconTextWidth, IconWidth + TextRect.Right);
  834. if (IconTextHeight > 0)
  835. {
  836. IconTextHeight += VertMargin;
  837. }
  838. Message->SetBounds(ALeft, VertMargin + IconTextHeight, TextRect.Right, TextRect.Bottom);
  839. IconTextHeight += TextRect.Bottom;
  840. }
  841. }
  842. assert((IconTextWidth > 0) && (IconTextHeight > 0));
  843. if (HasIcon && (IconTextHeight < 32))
  844. {
  845. IconTextHeight = 32;
  846. }
  847. int MoreMessageHeight = (MoreMessages != NULL ?
  848. ScaleByTextHeightRunTime(Result, mcMoreMessageHeight) : 0);
  849. Panel->SetBounds(0, 0, Result->ClientWidth, VertMargin + IconTextHeight + VertMargin + MoreMessageHeight);
  850. if (MoreMessages != NULL)
  851. {
  852. TMemo * MessageMemo = new TMemo(Panel);
  853. MessageMemo->Name = L"MessageMemo";
  854. MessageMemo->Parent = Panel;
  855. MessageMemo->ReadOnly = true;
  856. MessageMemo->WantReturns = False;
  857. MessageMemo->ScrollBars = ssVertical;
  858. MessageMemo->Anchors = TAnchors() << akLeft << akRight << akTop;
  859. MessageMemo->Lines->Text = MoreMessages->Text;
  860. Result->MessageMemo = MessageMemo;
  861. }
  862. int MinClientWidth =
  863. ScaleByTextHeightRunTime(Result,
  864. (MoreMessages != NULL) ? mcMinDialogwithMoreMessagesWidth : mcMinDialogWidth);
  865. int AClientWidth =
  866. Max(
  867. (IconTextWidth > ButtonGroupWidth ? IconTextWidth : ButtonGroupWidth) +
  868. HorzMargin * 2,
  869. MinClientWidth);
  870. Result->ClientWidth = AClientWidth;
  871. Result->ClientHeight =
  872. Panel->Height + ButtonVertMargin + ButtonHeight + ButtonVertMargin;
  873. Result->Left = (Monitor->Width / 2) - (Result->Width / 2);
  874. Result->Top = (Monitor->Height / 2) - (Result->Height / 2);
  875. if (DlgType != mtCustom)
  876. {
  877. Result->Caption = LoadResourceString(Captions[DlgType]);
  878. }
  879. else
  880. {
  881. Result->Caption = Application->Title;
  882. }
  883. if ((IconID != NULL) || !ImageName.IsEmpty())
  884. {
  885. TImage * Image = new TImage(Panel);
  886. Image->Name = L"Image";
  887. Image->Parent = Panel;
  888. if (!ImageName.IsEmpty())
  889. {
  890. LoadResourceImage(Image, ImageName);
  891. }
  892. else
  893. {
  894. Image->Picture->Icon->Handle = LoadIcon(0, IconID);
  895. }
  896. Image->SetBounds(HorzMargin, VertMargin, 32, 32);
  897. }
  898. if (Result->MessageMemo != NULL)
  899. {
  900. Result->MessageMemo->SetBounds(
  901. ALeft,
  902. Panel->Height - MoreMessageHeight,
  903. Result->ClientWidth - ALeft - HorzMargin,
  904. MoreMessageHeight - VertMargin);
  905. }
  906. int ButtonTop = Panel->Height + ButtonVertMargin;
  907. int X = Result->ClientWidth - ButtonGroupWidth + NeverAskAgainWidth - HorzMargin;
  908. for (unsigned int i = 0; i < ButtonControls.size(); i++)
  909. {
  910. ButtonControls[i]->SetBounds(
  911. X, ButtonTop, ButtonWidth, ButtonControls[i]->Height);
  912. X += ButtonWidth + ButtonSpacing;
  913. }
  914. if (!NeverAskAgainCaption.IsEmpty() &&
  915. !ButtonControls.empty())
  916. {
  917. TCheckBox * NeverAskAgainCheck = new TCheckBox(Result);
  918. NeverAskAgainCheck->Name = L"NeverAskAgainCheck";
  919. NeverAskAgainCheck->Parent = Result;
  920. NeverAskAgainCheck->Caption = NeverAskAgainCaption;
  921. NeverAskAgainCheck->Anchors = TAnchors() << akBottom << akLeft;
  922. TButton * FirstButton = ButtonControls[0];
  923. int NeverAskAgainHeight = ScaleByTextHeightRunTime(Result, NeverAskAgainCheck->Height);
  924. int NeverAskAgainTop = FirstButton->Top + ((FirstButton->Height - NeverAskAgainHeight) / 2);
  925. int NeverAskAgainLeft = HorzMargin;
  926. NeverAskAgainCheck->SetBounds(
  927. NeverAskAgainLeft, NeverAskAgainTop, NeverAskAgainWidth, NeverAskAgainHeight);
  928. }
  929. return Result;
  930. }
  931. //---------------------------------------------------------------------------
  932. TForm * __fastcall CreateMoreMessageDialog(const UnicodeString & Msg,
  933. TStrings * MoreMessages, TMsgDlgType DlgType, unsigned int Answers,
  934. const TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  935. unsigned int TimeoutAnswer, TButton ** TimeoutButton, const UnicodeString & ImageName,
  936. const UnicodeString & NeverAskAgainCaption)
  937. {
  938. return TMessageForm::Create(Msg, MoreMessages, DlgType, Answers,
  939. Aliases, AliasesCount, TimeoutAnswer, TimeoutButton, ImageName,
  940. NeverAskAgainCaption);
  941. }