MessageDlg.cpp 33 KB

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