MessageDlg.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 <WinInterface.h>
  9. #include <TextsWin.h>
  10. #include <Vcl.Imaging.pngimage.hpp>
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. //---------------------------------------------------------------------------
  14. class TMessageForm : public TForm
  15. {
  16. public:
  17. static TForm * __fastcall Create(const UnicodeString & Msg, TStrings * MoreMessages,
  18. TMsgDlgType DlgType, TMsgDlgButtons Buttons,
  19. TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  20. TMsgDlgBtn TimeoutResult, TButton ** TimeoutButton, const UnicodeString & ImageName);
  21. protected:
  22. __fastcall TMessageForm(TComponent * AOwner);
  23. virtual __fastcall ~TMessageForm();
  24. DYNAMIC void __fastcall KeyDown(Word & Key, TShiftState Shift);
  25. DYNAMIC void __fastcall KeyUp(Word & Key, TShiftState Shift);
  26. UnicodeString __fastcall GetFormText();
  27. virtual void __fastcall CreateParams(TCreateParams & Params);
  28. DYNAMIC void __fastcall DoShow();
  29. virtual void __fastcall Dispatch(void * Message);
  30. void __fastcall MenuItemClick(TObject * Sender);
  31. void __fastcall ButtonDropDownClick(TObject * Sender);
  32. void __fastcall UpdateForShiftStateTimer(TObject * Sender);
  33. private:
  34. TLabel * Message;
  35. TMemo * MessageMemo;
  36. TShiftState FShiftState;
  37. TTimer * FUpdateForShiftStateTimer;
  38. void __fastcall HelpButtonClick(TObject * Sender);
  39. void __fastcall CMDialogKey(TWMKeyDown & Message);
  40. void __fastcall UpdateForShiftState();
  41. };
  42. //---------------------------------------------------------------------------
  43. __fastcall TMessageForm::TMessageForm(TComponent * AOwner) : TForm(AOwner, 0)
  44. {
  45. Message = NULL;
  46. MessageMemo = NULL;
  47. FUpdateForShiftStateTimer = NULL;
  48. TNonClientMetrics NonClientMetrics;
  49. NonClientMetrics.cbSize = sizeof(NonClientMetrics);
  50. if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &NonClientMetrics, 0))
  51. {
  52. Font->Handle = CreateFontIndirect(&NonClientMetrics.lfMessageFont);
  53. }
  54. Position = poOwnerFormCenter;
  55. UseSystemSettingsPre(this);
  56. }
  57. //---------------------------------------------------------------------------
  58. __fastcall TMessageForm::~TMessageForm()
  59. {
  60. SAFE_DESTROY(FUpdateForShiftStateTimer);
  61. }
  62. //---------------------------------------------------------------------------
  63. void __fastcall TMessageForm::HelpButtonClick(TObject * /*Sender*/)
  64. {
  65. if (HelpKeyword != HELP_NONE)
  66. {
  67. FormHelp(this);
  68. }
  69. else
  70. {
  71. UnicodeString Text = Message->Caption;
  72. if (MessageMemo != NULL)
  73. {
  74. Text += L"\n" + MessageMemo->Text;
  75. }
  76. MessageWithNoHelp(Text);
  77. }
  78. }
  79. //---------------------------------------------------------------------------
  80. void __fastcall TMessageForm::UpdateForShiftState()
  81. {
  82. TShiftState ShiftState =
  83. KeyboardStateToShiftState() *
  84. (TShiftState() << ssShift << ssCtrl << ssAlt);
  85. if (FShiftState != ShiftState)
  86. {
  87. FShiftState = ShiftState;
  88. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  89. {
  90. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  91. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  92. {
  93. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  94. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  95. {
  96. TMenuItem * Item = MenuItems->Items[ItemIndex];
  97. TShiftState GrouppedShiftState(Item->Tag >> 16);
  98. if (Item->Enabled &&
  99. ((ShiftState.Empty() && Item->Default) ||
  100. (!ShiftState.Empty() && (ShiftState == GrouppedShiftState))))
  101. {
  102. int From = 1;
  103. Button->Caption = CopyToChars(Item->Caption, From, L"\t", false);
  104. Button->ModalResult = Item->Tag & 0xFFFF;
  105. assert(Button->OnClick == NULL);
  106. assert(Item->OnClick == MenuItemClick);
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. //---------------------------------------------------------------------------
  115. void __fastcall TMessageForm::KeyUp(Word & Key, TShiftState Shift)
  116. {
  117. UpdateForShiftState();
  118. TForm::KeyUp(Key, Shift);
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TMessageForm::KeyDown(Word & Key, TShiftState Shift)
  122. {
  123. if (Shift.Contains(ssCtrl) && (Key == L'C'))
  124. {
  125. CopyToClipboard(GetFormText());
  126. }
  127. else
  128. {
  129. if (!Shift.Contains(ssCtrl))
  130. {
  131. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  132. {
  133. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  134. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  135. {
  136. TMenuItem * MenuItems = Button->DropDownMenu->Items;
  137. for (int ItemIndex = 0; ItemIndex < MenuItems->Count; ItemIndex++)
  138. {
  139. TMenuItem * Item = MenuItems->Items[ItemIndex];
  140. if (IsAccel(Key, MenuItems->Items[ItemIndex]->Caption))
  141. {
  142. Item->OnClick(Item);
  143. Key = 0;
  144. break;
  145. }
  146. }
  147. }
  148. if (Key == 0)
  149. {
  150. break;
  151. }
  152. }
  153. }
  154. UpdateForShiftState();
  155. TForm::KeyDown(Key, Shift);
  156. }
  157. }
  158. //---------------------------------------------------------------------------
  159. UnicodeString __fastcall TMessageForm::GetFormText()
  160. {
  161. UnicodeString DividerLine, ButtonCaptions;
  162. DividerLine = UnicodeString::StringOfChar(L'-', 27) + sLineBreak;
  163. for (int i = 0; i < ComponentCount - 1; i++)
  164. {
  165. if (dynamic_cast<TButton*>(Components[i]) != NULL)
  166. {
  167. ButtonCaptions += dynamic_cast<TButton*>(Components[i])->Caption +
  168. UnicodeString::StringOfChar(L' ', 3);
  169. }
  170. }
  171. ButtonCaptions = StringReplace(ButtonCaptions, L"&", L"",
  172. TReplaceFlags() << rfReplaceAll);
  173. UnicodeString MoreMessages;
  174. if (MessageMemo != NULL)
  175. {
  176. MoreMessages = MessageMemo->Text + DividerLine;
  177. }
  178. UnicodeString MessageCaption;
  179. MessageCaption = StringReplace(Message->Caption, L"\r", L"", TReplaceFlags() << rfReplaceAll);
  180. MessageCaption = StringReplace(MessageCaption, L"\n", L"\r\n", TReplaceFlags() << rfReplaceAll);
  181. UnicodeString Result = FORMAT(L"%s%s%s%s%s%s%s%s%s%s%s", (DividerLine, Caption, sLineBreak,
  182. DividerLine, MessageCaption, sLineBreak, DividerLine, MoreMessages,
  183. ButtonCaptions, sLineBreak, DividerLine));
  184. return Result;
  185. }
  186. //---------------------------------------------------------------------------
  187. void __fastcall TMessageForm::CMDialogKey(TWMKeyDown & Message)
  188. {
  189. // this gets used in WinInterface.cpp SetTimeoutEvents
  190. if (OnKeyDown != NULL)
  191. {
  192. OnKeyDown(this, Message.CharCode, KeyDataToShiftState(Message.KeyData));
  193. }
  194. if (Message.CharCode == VK_MENU)
  195. {
  196. bool AnyButtonWithGrouppedCommandsWithShiftState = false;
  197. for (int ComponentIndex = 0; ComponentIndex < ComponentCount - 1; ComponentIndex++)
  198. {
  199. TButton * Button = dynamic_cast<TButton*>(Components[ComponentIndex]);
  200. if ((Button != NULL) && (Button->DropDownMenu != NULL))
  201. {
  202. // we should check if there are any commands with shift state,
  203. // but it's bit overkill
  204. AnyButtonWithGrouppedCommandsWithShiftState = true;
  205. break;
  206. }
  207. }
  208. // this is to make Alt only alter button meaning (if there is any
  209. // alternable button) and not popup system menu
  210. if (AnyButtonWithGrouppedCommandsWithShiftState)
  211. {
  212. Message.Result = 1;
  213. UpdateForShiftState();
  214. }
  215. else
  216. {
  217. TForm::Dispatch(&Message);
  218. }
  219. }
  220. else
  221. {
  222. TForm::Dispatch(&Message);
  223. }
  224. }
  225. //---------------------------------------------------------------------------
  226. void __fastcall TMessageForm::Dispatch(void * Message)
  227. {
  228. TMessage * M = reinterpret_cast<TMessage*>(Message);
  229. if (M->Msg == CM_DIALOGKEY)
  230. {
  231. CMDialogKey(*((TWMKeyDown *)Message));
  232. }
  233. else
  234. {
  235. TForm::Dispatch(Message);
  236. }
  237. }
  238. //---------------------------------------------------------------------------
  239. void __fastcall TMessageForm::CreateParams(TCreateParams & Params)
  240. {
  241. TForm::CreateParams(Params);
  242. if ((Screen != NULL) && (Screen->ActiveForm != NULL) &&
  243. Screen->ActiveForm->HandleAllocated())
  244. {
  245. Params.WndParent = Screen->ActiveForm->Handle;
  246. }
  247. }
  248. //---------------------------------------------------------------------------
  249. void __fastcall TMessageForm::DoShow()
  250. {
  251. UseSystemSettingsPost(this);
  252. TForm::DoShow();
  253. }
  254. //---------------------------------------------------------------------------
  255. void __fastcall TMessageForm::MenuItemClick(TObject * Sender)
  256. {
  257. TMenuItem * Item = NOT_NULL(dynamic_cast<TMenuItem *>(Sender));
  258. ModalResult = (Item->Tag & 0xFFFF);
  259. }
  260. //---------------------------------------------------------------------------
  261. void __fastcall TMessageForm::UpdateForShiftStateTimer(TObject * /*Sender*/)
  262. {
  263. // this is needed to reflect shift state, even when we do not have a keyboard
  264. // focus, what happens when drop down menu is popped up
  265. UpdateForShiftState();
  266. }
  267. //---------------------------------------------------------------------------
  268. void __fastcall TMessageForm::ButtonDropDownClick(TObject * /*Sender*/)
  269. {
  270. // as optimization, do not waste time running timer, unless
  271. // user pops up drop down menu. we do not have a way to stop timer, once
  272. // it closes, but functionaly is does not matter
  273. if (FUpdateForShiftStateTimer == NULL)
  274. {
  275. FUpdateForShiftStateTimer = new TTimer(this);
  276. FUpdateForShiftStateTimer->Interval = 50;
  277. FUpdateForShiftStateTimer->OnTimer = UpdateForShiftStateTimer;
  278. }
  279. }
  280. //---------------------------------------------------------------------------
  281. const ResourceString * Captions[] = { &_SMsgDlgWarning, &_SMsgDlgError, &_SMsgDlgInformation,
  282. &_SMsgDlgConfirm, NULL };
  283. const wchar_t * IconIDs[] = { IDI_EXCLAMATION, IDI_HAND, IDI_ASTERISK,
  284. IDI_QUESTION, NULL };
  285. const int ButtonCount = 11;
  286. const UnicodeString ButtonNames[ButtonCount] = {
  287. L"Yes", L"No", L"OK", L"Cancel", L"Abort", L"Retry", L"Ignore", L"All", L"NoToAll",
  288. L"YesToAll", L"Help" };
  289. // Own variant to avoid accelerator conflict with "Abort" button.
  290. // Note that as of now, ALL_BUTTON is never actually used, because it's always aliased
  291. ResourceString MsgDlgAll = { NULL, ALL_BUTTON };
  292. ResourceString MsgDlgYesToAll = { NULL, YES_TO_ALL_BUTTON };
  293. const ResourceString * ButtonCaptions[ButtonCount] = {
  294. &_SMsgDlgYes, &_SMsgDlgNo, &_SMsgDlgOK, &_SMsgDlgCancel, &_SMsgDlgAbort,
  295. &_SMsgDlgRetry, &_SMsgDlgIgnore, &MsgDlgAll, &_SMsgDlgNoToAll, &MsgDlgYesToAll,
  296. &_SMsgDlgHelp };
  297. extern const int ModalResults[ButtonCount] = {
  298. mrYes, mrNo, mrOk, mrCancel, mrAbort, mrRetry, mrIgnore, mrAll, mrNoToAll,
  299. mrYesToAll, 0 };
  300. const int mcHorzMargin = 8;
  301. const int mcVertMargin = 8;
  302. const int mcHorzSpacing = 10;
  303. const int mcVertSpacing = 10;
  304. const int mcButtonWidth = 50;
  305. const int mcButtonHeight = 14;
  306. const int mcButtonSpacing = 4;
  307. const int mcMoreMessageWidth = 320;
  308. const int mcMoreMessageHeight = 80;
  309. //---------------------------------------------------------------------------
  310. static UnicodeString __fastcall GetKeyNameStr(int Key)
  311. {
  312. wchar_t Buf[MAX_PATH];
  313. LONG VirtualKey = MapVirtualKey(Key, MAPVK_VK_TO_VSC);
  314. VirtualKey <<= 16;
  315. if (GetKeyNameText(VirtualKey, Buf, LENOF(Buf)) > 0)
  316. {
  317. Buf[LENOF(Buf) - 1] = L'\0';
  318. }
  319. else
  320. {
  321. Buf[0] = L'\0';
  322. }
  323. return Buf;
  324. }
  325. //---------------------------------------------------------------------------
  326. TForm * __fastcall TMessageForm::Create(const UnicodeString & Msg,
  327. TStrings * MoreMessages, TMsgDlgType DlgType, TMsgDlgButtons Buttons,
  328. TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  329. TMsgDlgBtn TimeoutResult, TButton ** TimeoutButton, const UnicodeString & ImageName)
  330. {
  331. TRect TextRect;
  332. TMsgDlgBtn DefaultButton, CancelButton;
  333. if (Buttons.Contains(mbOK))
  334. {
  335. DefaultButton = mbOK;
  336. }
  337. else if (Buttons.Contains(mbYes))
  338. {
  339. DefaultButton = mbYes;
  340. }
  341. else
  342. {
  343. DefaultButton = mbRetry;
  344. }
  345. if (Buttons.Contains(mbCancel))
  346. {
  347. CancelButton = mbCancel;
  348. }
  349. else if (Buttons.Contains(mbNo))
  350. {
  351. CancelButton = mbNo;
  352. }
  353. else if (Buttons.Contains(mbAbort))
  354. {
  355. CancelButton = mbAbort;
  356. }
  357. else
  358. {
  359. CancelButton = mbOK;
  360. }
  361. if (TimeoutButton != NULL)
  362. {
  363. *TimeoutButton = NULL;
  364. }
  365. TMessageForm * Result = SafeFormCreate<TMessageForm>();
  366. Result->BiDiMode = Application->BiDiMode;
  367. Result->BorderStyle = bsDialog;
  368. Result->Canvas->Font = Result->Font;
  369. Result->KeyPreview = true;
  370. TPoint DialogUnits = GetAveCharSize(Result->Canvas);
  371. int HorzMargin = MulDiv(mcHorzMargin, DialogUnits.x, 4);
  372. int VertMargin = MulDiv(mcVertMargin, DialogUnits.y, 8);
  373. int HorzSpacing = MulDiv(mcHorzSpacing, DialogUnits.x, 4);
  374. int VertSpacing = MulDiv(mcVertSpacing, DialogUnits.y, 8);
  375. int ButtonWidth = MulDiv(mcButtonWidth, DialogUnits.x, 4);
  376. TButton * ButtonControls[ButtonCount + 1];
  377. int ButtonControlsCount = 0;
  378. typedef std::map<unsigned int, TButton *> TAnswerButtons;
  379. TAnswerButtons AnswerButtons;
  380. for (unsigned int B = mbYes; B <= mbHelp; B++)
  381. {
  382. assert(B < ButtonCount);
  383. if (Buttons.Contains(TMsgDlgBtn(B)))
  384. {
  385. TextRect = Rect(0,0,0,0);
  386. const ResourceString * CaptionResource = ButtonCaptions[B];
  387. UnicodeString Caption = LoadStr(CaptionResource->Identifier);
  388. TNotifyEvent OnClick = NULL;
  389. int GroupWith = -1;
  390. TShiftState GrouppedShiftState;
  391. if (Aliases != NULL)
  392. {
  393. for (unsigned int i = 0; i < AliasesCount; i++)
  394. {
  395. if (B == Aliases[i].Button)
  396. {
  397. if (!Aliases[i].Alias.IsEmpty())
  398. {
  399. Caption = Aliases[i].Alias;
  400. }
  401. OnClick = Aliases[i].OnClick;
  402. GroupWith = Aliases[i].GroupWith;
  403. GrouppedShiftState = Aliases[i].GrouppedShiftState;
  404. assert((OnClick == NULL) || (GrouppedShiftState == TShiftState()));
  405. break;
  406. }
  407. }
  408. }
  409. UnicodeString MeasureCaption = Caption;
  410. bool IsTimeoutButton = (TimeoutButton != NULL) && (B == static_cast<unsigned int>(TimeoutResult));
  411. if (IsTimeoutButton)
  412. {
  413. MeasureCaption = FMTLOAD(TIMEOUT_BUTTON, (MeasureCaption, 99));
  414. }
  415. DrawText(Result->Canvas->Handle,
  416. UnicodeString(MeasureCaption).c_str(), -1,
  417. &TextRect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE |
  418. Result->DrawTextBiDiModeFlagsReadingOnly());
  419. int CurButtonWidth = TextRect.Right - TextRect.Left + 8;
  420. int ModalResult = ModalResults[B];
  421. // we hope that all grouped-with buttons are for asnwer with greater
  422. // value that the answer to be grouped with
  423. if (SupportsSplitButton() &&
  424. (GroupWith >= 0) && ALWAYS_TRUE(GroupWith < static_cast<int>(B)) &&
  425. ALWAYS_TRUE(AnswerButtons.find(GroupWith) != AnswerButtons.end()) &&
  426. ALWAYS_TRUE(B != static_cast<unsigned int>(TimeoutResult)) &&
  427. ALWAYS_TRUE(B != static_cast<unsigned int>(DefaultButton)) &&
  428. ALWAYS_TRUE(B != static_cast<unsigned int>(CancelButton)))
  429. {
  430. TButton * GroupWithButton = AnswerButtons[GroupWith];
  431. if (GroupWithButton->DropDownMenu == NULL)
  432. {
  433. GroupWithButton->Style = TCustomButton::bsSplitButton;
  434. GroupWithButton->DropDownMenu = new TPopupMenu(Result);
  435. // cannot handle subitems with shift state,
  436. // if the button has its own handler
  437. // (though it may not be the case still here)
  438. assert(GroupWithButton->OnClick == NULL);
  439. TMenuItem * Item = new TMenuItem(GroupWithButton->DropDownMenu);
  440. GroupWithButton->DropDownMenu->Items->Add(Item);
  441. GroupWithButton->OnDropDownClick = Result->ButtonDropDownClick;
  442. Item->Caption = GroupWithButton->Caption;
  443. Item->OnClick = Result->MenuItemClick;
  444. assert(GroupWithButton->ModalResult <= 0xFFFF);
  445. Item->Tag = GroupWithButton->ModalResult;
  446. Item->Default = true;
  447. }
  448. TMenuItem * Item = new TMenuItem(GroupWithButton->DropDownMenu);
  449. GroupWithButton->DropDownMenu->Items->Add(Item);
  450. // See ShortCutToText in Vcl.Menus.pas
  451. if (GrouppedShiftState == (TShiftState() << ssAlt))
  452. {
  453. Caption = Caption + L"\t" + GetKeyNameStr(VK_MENU);
  454. }
  455. else if (GrouppedShiftState == (TShiftState() << ssCtrl))
  456. {
  457. Caption = Caption + L"\t" + GetKeyNameStr(VK_CONTROL);
  458. }
  459. else if (GrouppedShiftState == (TShiftState() << ssShift))
  460. {
  461. Caption = Caption + L"\t" + GetKeyNameStr(VK_SHIFT);
  462. }
  463. else
  464. {
  465. // do not support combined shift states yet
  466. assert(GrouppedShiftState == TShiftState());
  467. }
  468. Item->Caption = Caption;
  469. if (OnClick != NULL)
  470. {
  471. Item->OnClick = OnClick;
  472. }
  473. else
  474. {
  475. Item->OnClick = Result->MenuItemClick;
  476. assert((ModalResult <= 0xFFFF) && (GrouppedShiftState.ToInt() <= 0xFFFF));
  477. Item->Tag = ModalResult + (GrouppedShiftState.ToInt() << 16);
  478. }
  479. // Hard-coded drop down button width (do not know how to ask for system width).
  480. // Also we do not update the max button width for the default groupped
  481. // button caption. We just blindly hope that captions of advanced commands
  482. // are always longer than the caption of simple default command
  483. CurButtonWidth += 15;
  484. }
  485. else
  486. {
  487. TButton * Button = new TButton(Result);
  488. if (IsTimeoutButton)
  489. {
  490. *TimeoutButton = Button;
  491. }
  492. Button->Name = ButtonNames[TMsgDlgBtn(B)];
  493. Button->Parent = Result;
  494. Button->Caption = Caption;
  495. if (OnClick != NULL)
  496. {
  497. Button->OnClick = OnClick;
  498. }
  499. else
  500. {
  501. Button->ModalResult = ModalResult;
  502. Button->Default = (B == static_cast<unsigned int>(DefaultButton));
  503. Button->Cancel = (B == static_cast<unsigned int>(CancelButton));
  504. }
  505. if (MoreMessages != NULL)
  506. {
  507. Button->Anchors = TAnchors() << akBottom << akLeft;
  508. }
  509. if (B == mbHelp)
  510. {
  511. Button->OnClick = Result->HelpButtonClick;
  512. }
  513. ButtonControls[ButtonControlsCount] = Button;
  514. ButtonControlsCount++;
  515. AnswerButtons.insert(TAnswerButtons::value_type(B, Button));
  516. }
  517. if (CurButtonWidth > ButtonWidth)
  518. {
  519. ButtonWidth = CurButtonWidth;
  520. }
  521. }
  522. }
  523. int ButtonHeight = MulDiv(mcButtonHeight, DialogUnits.y, 8);
  524. int ButtonSpacing = MulDiv(mcButtonSpacing, DialogUnits.x, 4);
  525. SetRect(&TextRect, 0, 0, Screen->Width / 2, 0);
  526. DrawText(Result->Canvas->Handle, Msg.c_str(), Msg.Length() + 1, &TextRect,
  527. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK |
  528. Result->DrawTextBiDiModeFlagsReadingOnly());
  529. int MaxWidth = Screen->Width - HorzMargin * 2 - 32 - HorzSpacing - 30;
  530. if (TextRect.right > MaxWidth)
  531. {
  532. // this will truncate the text, we should implement something smarter eventually
  533. TextRect.right = MaxWidth;
  534. }
  535. const wchar_t * IconID = IconIDs[DlgType];
  536. int IconTextWidth = TextRect.Right;
  537. int IconTextHeight = TextRect.Bottom;
  538. if ((IconID != NULL) || !ImageName.IsEmpty())
  539. {
  540. IconTextWidth += 32 + HorzSpacing;
  541. if (IconTextHeight < 32)
  542. {
  543. IconTextHeight = 32;
  544. }
  545. }
  546. if (MoreMessages != NULL)
  547. {
  548. TMemo * MessageMemo = new TMemo(Result);
  549. MessageMemo->Parent = Result;
  550. MessageMemo->ReadOnly = true;
  551. MessageMemo->WantReturns = False;
  552. MessageMemo->ScrollBars = ssVertical;
  553. MessageMemo->Anchors = TAnchors() << akLeft << akRight << akTop; //akBottom;
  554. MessageMemo->Color = clBtnFace;
  555. MessageMemo->Lines->Text = MoreMessages->Text;
  556. Result->MessageMemo = MessageMemo;
  557. }
  558. int ButtonGroupWidth = 0;
  559. if (ButtonControlsCount > 0)
  560. {
  561. ButtonGroupWidth = ButtonWidth * ButtonControlsCount +
  562. ButtonSpacing * (ButtonControlsCount - 1);
  563. }
  564. int MoreMessageWidth = (MoreMessages != NULL ?
  565. MulDiv(mcMoreMessageWidth, DialogUnits.x, 4) : 0);
  566. int MoreMessageHeight = (MoreMessages != NULL ?
  567. MulDiv(mcMoreMessageHeight, DialogUnits.y, 12) : 0);
  568. int AClientWidth =
  569. (IconTextWidth > ButtonGroupWidth ? IconTextWidth : ButtonGroupWidth) +
  570. HorzMargin * 2;
  571. Result->ClientWidth = (AClientWidth > MoreMessageWidth ?
  572. AClientWidth : MoreMessageWidth);
  573. Result->ClientHeight = IconTextHeight + ButtonHeight + VertSpacing +
  574. VertMargin * 2 + MoreMessageHeight;
  575. Result->Left = (Screen->Width / 2) - (Result->Width / 2);
  576. Result->Top = (Screen->Height / 2) - (Result->Height / 2);
  577. if (DlgType != mtCustom)
  578. {
  579. Result->Caption = LoadResourceString(Captions[DlgType]);
  580. }
  581. else
  582. {
  583. Result->Caption = Application->Title;
  584. }
  585. if ((IconID != NULL) || !ImageName.IsEmpty())
  586. {
  587. TImage * Image = new TImage(Result);
  588. Image->Name = L"Image";
  589. Image->Parent = Result;
  590. if (!ImageName.IsEmpty())
  591. {
  592. std::auto_ptr<TPngImage> Png(new TPngImage());
  593. Png->LoadFromResourceName(0, ImageName);
  594. Image->Picture->Assign(Png.get());
  595. }
  596. else
  597. {
  598. Image->Picture->Icon->Handle = LoadIcon(0, IconID);
  599. }
  600. Image->SetBounds(HorzMargin, VertMargin, 32, 32);
  601. }
  602. TLabel * Message = new TLabel(Result);
  603. Result->Message = Message;
  604. Message->Name = L"Message";
  605. Message->Parent = Result;
  606. Message->WordWrap = true;
  607. Message->Caption = Msg;
  608. Message->BoundsRect = TextRect;
  609. Message->BiDiMode = Result->BiDiMode;
  610. // added to show & as & for messages containing !& pattern of custom commands
  611. // (suppose that we actually never want to use & as accel in message text)
  612. Message->ShowAccelChar = false;
  613. int ALeft = IconTextWidth - TextRect.Right + HorzMargin;
  614. Message->SetBounds(ALeft, VertMargin, TextRect.Right, TextRect.Bottom);
  615. int ButtonTop = IconTextHeight + VertMargin + VertSpacing + MoreMessageHeight;
  616. if (Result->MessageMemo != NULL)
  617. {
  618. Result->MessageMemo->BoundsRect = TRect(Message->Left,
  619. Message->Top + Message->Height + VertSpacing,
  620. Result->ClientWidth - HorzMargin,
  621. Message->Top + Message->Height + VertSpacing + MoreMessageHeight);
  622. // rather hack, whole control positioning is wrong
  623. if (Result->MessageMemo->Top + Result->MessageMemo->Height > ButtonTop - VertSpacing)
  624. {
  625. Result->MessageMemo->Height =
  626. (ButtonTop - VertSpacing) - Result->MessageMemo->Top;
  627. }
  628. }
  629. int X = (Result->ClientWidth - ButtonGroupWidth) / 2;
  630. for (int i = 0; i < ButtonControlsCount; i++)
  631. {
  632. ButtonControls[i]->SetBounds(X,
  633. ButtonTop, ButtonWidth, ButtonHeight);
  634. X += ButtonWidth + ButtonSpacing;
  635. }
  636. return Result;
  637. }
  638. //---------------------------------------------------------------------------
  639. TForm * __fastcall CreateMoreMessageDialog(const UnicodeString & Msg,
  640. TStrings * MoreMessages, TMsgDlgType DlgType, TMsgDlgButtons Buttons,
  641. TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  642. TMsgDlgBtn TimeoutResult, TButton ** TimeoutButton, const UnicodeString & ImageName)
  643. {
  644. return TMessageForm::Create(Msg, MoreMessages, DlgType, Buttons,
  645. Aliases, AliasesCount, TimeoutResult, TimeoutButton, ImageName);
  646. }