MessageDlg.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. //---------------------------------------------------------------------------
  13. class TMessageForm : public TForm
  14. {
  15. public:
  16. static TForm * __fastcall Create(const AnsiString & Msg, TStrings * MoreMessages,
  17. TMsgDlgType DlgType, TMsgDlgButtons Buttons,
  18. TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  19. TMsgDlgBtn TimeoutResult, TButton ** TimeoutButton);
  20. protected:
  21. __fastcall TMessageForm(TComponent * AOwner);
  22. DYNAMIC void __fastcall KeyDown(Word & Key, TShiftState Shift);
  23. AnsiString __fastcall GetFormText();
  24. virtual void __fastcall CreateParams(TCreateParams & Params);
  25. DYNAMIC void __fastcall DoShow();
  26. private:
  27. TLabel * Message;
  28. TMemo * MessageMemo;
  29. void __fastcall HelpButtonClick(TObject * Sender);
  30. };
  31. //---------------------------------------------------------------------------
  32. __fastcall TMessageForm::TMessageForm(TComponent * AOwner) : TForm(AOwner, 0)
  33. {
  34. Message = NULL;
  35. MessageMemo = NULL;
  36. TNonClientMetrics NonClientMetrics;
  37. NonClientMetrics.cbSize = sizeof(NonClientMetrics);
  38. if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &NonClientMetrics, 0))
  39. {
  40. Font->Handle = CreateFontIndirect(&NonClientMetrics.lfMessageFont);
  41. }
  42. Position = poMainFormCenter;
  43. UseSystemSettingsPre(this);
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TMessageForm::HelpButtonClick(TObject * /*Sender*/)
  47. {
  48. FormHelp(this);
  49. }
  50. //---------------------------------------------------------------------------
  51. void __fastcall TMessageForm::KeyDown(Word & Key, TShiftState Shift)
  52. {
  53. if (Shift.Contains(ssCtrl) && (Key == 'C'))
  54. {
  55. CopyToClipboard(GetFormText());
  56. }
  57. }
  58. //---------------------------------------------------------------------------
  59. AnsiString __fastcall TMessageForm::GetFormText()
  60. {
  61. AnsiString DividerLine, ButtonCaptions;
  62. DividerLine = AnsiString::StringOfChar('-', 27) + sLineBreak;
  63. for (int i = 0; i < ComponentCount - 1; i++)
  64. {
  65. if (dynamic_cast<TButton*>(Components[i]) != NULL)
  66. {
  67. ButtonCaptions += dynamic_cast<TButton*>(Components[i])->Caption +
  68. AnsiString::StringOfChar(' ', 3);
  69. }
  70. }
  71. ButtonCaptions = StringReplace(ButtonCaptions, "&", "",
  72. TReplaceFlags() << rfReplaceAll);
  73. AnsiString MoreMessages;
  74. if (MessageMemo != NULL)
  75. {
  76. MoreMessages = MessageMemo->Text + DividerLine;
  77. }
  78. AnsiString MessageCaption;
  79. MessageCaption = StringReplace(Message->Caption, "\r", "", TReplaceFlags() << rfReplaceAll);
  80. MessageCaption = StringReplace(MessageCaption, "\n", "\r\n", TReplaceFlags() << rfReplaceAll);
  81. AnsiString Result = FORMAT("%s%s%s%s%s%s%s%s%s%s%s", (DividerLine, Caption, sLineBreak,
  82. DividerLine, MessageCaption, sLineBreak, DividerLine, MoreMessages,
  83. ButtonCaptions, sLineBreak, DividerLine));
  84. return Result;
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TMessageForm::CreateParams(TCreateParams & Params)
  88. {
  89. TForm::CreateParams(Params);
  90. if ((Screen != NULL) && (Screen->ActiveForm != NULL) &&
  91. Screen->ActiveForm->HandleAllocated())
  92. {
  93. Params.WndParent = Screen->ActiveForm->Handle;
  94. }
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TMessageForm::DoShow()
  98. {
  99. UseSystemSettingsPost(this);
  100. TForm::DoShow();
  101. }
  102. //---------------------------------------------------------------------------
  103. const ResourceString * Captions[] = { &_SMsgDlgWarning, &_SMsgDlgError, &_SMsgDlgInformation,
  104. &_SMsgDlgConfirm, NULL };
  105. const char * IconIDs[] = { IDI_EXCLAMATION, IDI_HAND, IDI_ASTERISK,
  106. IDI_QUESTION, NULL };
  107. const int ButtonCount = 11;
  108. const AnsiString ButtonNames[ButtonCount] = {
  109. "Yes", "No", "OK", "Cancel", "Abort", "Retry", "Ignore", "All", "NoToAll",
  110. "YesToAll", "Help" };
  111. const ResourceString * ButtonCaptions[ButtonCount] = {
  112. &_SMsgDlgYes, &_SMsgDlgNo, &_SMsgDlgOK, &_SMsgDlgCancel, &_SMsgDlgAbort,
  113. &_SMsgDlgRetry, &_SMsgDlgIgnore, &_SMsgDlgAll, &_SMsgDlgNoToAll, &_SMsgDlgYesToAll,
  114. &_SMsgDlgHelp };
  115. extern const int ModalResults[ButtonCount] = {
  116. mrYes, mrNo, mrOk, mrCancel, mrAbort, mrRetry, mrIgnore, mrAll, mrNoToAll,
  117. mrYesToAll, 0 };
  118. const int mcHorzMargin = 8;
  119. const int mcVertMargin = 8;
  120. const int mcHorzSpacing = 10;
  121. const int mcVertSpacing = 10;
  122. const int mcButtonWidth = 50;
  123. const int mcButtonHeight = 14;
  124. const int mcButtonSpacing = 4;
  125. const int mcMoreMessageWidth = 320;
  126. const int mcMoreMessageHeight = 80;
  127. //---------------------------------------------------------------------------
  128. TForm * __fastcall TMessageForm::Create(const AnsiString & Msg,
  129. TStrings * MoreMessages, TMsgDlgType DlgType, TMsgDlgButtons Buttons,
  130. TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  131. TMsgDlgBtn TimeoutResult, TButton ** TimeoutButton)
  132. {
  133. TRect TextRect;
  134. TMsgDlgBtn DefaultButton, CancelButton;
  135. if (Buttons.Contains(mbOK))
  136. {
  137. DefaultButton = mbOK;
  138. }
  139. else if (Buttons.Contains(mbYes))
  140. {
  141. DefaultButton = mbYes;
  142. }
  143. else
  144. {
  145. DefaultButton = mbRetry;
  146. }
  147. if (Buttons.Contains(mbCancel))
  148. {
  149. CancelButton = mbCancel;
  150. }
  151. else if (Buttons.Contains(mbNo))
  152. {
  153. CancelButton = mbNo;
  154. }
  155. else if (Buttons.Contains(mbAbort))
  156. {
  157. CancelButton = mbAbort;
  158. }
  159. else
  160. {
  161. CancelButton = mbOK;
  162. }
  163. if (TimeoutButton != NULL)
  164. {
  165. *TimeoutButton = NULL;
  166. }
  167. TMessageForm * Result = SafeFormCreate<TMessageForm>();
  168. Result->BiDiMode = Application->BiDiMode;
  169. Result->BorderStyle = bsDialog;
  170. Result->Canvas->Font = Result->Font;
  171. Result->KeyPreview = true;
  172. TPoint DialogUnits = GetAveCharSize(Result->Canvas);
  173. int HorzMargin = MulDiv(mcHorzMargin, DialogUnits.x, 4);
  174. int VertMargin = MulDiv(mcVertMargin, DialogUnits.y, 8);
  175. int HorzSpacing = MulDiv(mcHorzSpacing, DialogUnits.x, 4);
  176. int VertSpacing = MulDiv(mcVertSpacing, DialogUnits.y, 8);
  177. int ButtonWidth = MulDiv(mcButtonWidth, DialogUnits.x, 4);
  178. TButton * ButtonControls[ButtonCount + 1];
  179. int ButtonControlsCount = 0;
  180. for (unsigned int B = mbYes; B <= mbHelp; B++)
  181. {
  182. assert(B < ButtonCount);
  183. if (Buttons.Contains(TMsgDlgBtn(B)))
  184. {
  185. TextRect = Rect(0,0,0,0);
  186. AnsiString Caption = LoadResourceString(ButtonCaptions[B]);
  187. // temporary fix of accelerators (&Abort vs. &All/Yes to &All)
  188. // must be removed
  189. if (Caption == "&All")
  190. {
  191. Caption = "A&ll";
  192. }
  193. else if (Caption == "Yes to &All")
  194. {
  195. Caption = "Yes to A&ll";
  196. }
  197. TNotifyEvent OnClick = NULL;
  198. if (Aliases != NULL)
  199. {
  200. for (unsigned int i = 0; i < AliasesCount; i++)
  201. {
  202. if (B == Aliases[i].Button)
  203. {
  204. Caption = Aliases[i].Alias;
  205. OnClick = Aliases[i].OnClick;
  206. break;
  207. }
  208. }
  209. }
  210. TButton * Button = new TButton(Result);
  211. AnsiString MeasureCaption = Caption;
  212. if ((TimeoutButton != NULL) && (B == TimeoutResult))
  213. {
  214. MeasureCaption = FMTLOAD(TIMEOUT_BUTTON, (MeasureCaption, 99));
  215. *TimeoutButton = Button;
  216. }
  217. DrawText(Result->Canvas->Handle,
  218. MeasureCaption.c_str(), -1,
  219. &TextRect, DT_CALCRECT | DT_LEFT | DT_SINGLELINE |
  220. Result->DrawTextBiDiModeFlagsReadingOnly());
  221. int CurButtonWidth = TextRect.Right - TextRect.Left + 8;
  222. if (CurButtonWidth > ButtonWidth)
  223. {
  224. ButtonWidth = CurButtonWidth;
  225. }
  226. Button->Name = ButtonNames[TMsgDlgBtn(B)];
  227. Button->Parent = Result;
  228. Button->Caption = Caption;
  229. if (OnClick != NULL)
  230. {
  231. Button->OnClick = OnClick;
  232. }
  233. else
  234. {
  235. Button->ModalResult = ModalResults[B];
  236. Button->Default = (B == DefaultButton);
  237. Button->Cancel = (B == CancelButton);
  238. }
  239. if (MoreMessages != NULL)
  240. {
  241. Button->Anchors = TAnchors() << akBottom << akLeft;
  242. }
  243. if (B == mbHelp)
  244. {
  245. Button->OnClick = Result->HelpButtonClick;
  246. }
  247. ButtonControls[ButtonControlsCount] = Button;
  248. ButtonControlsCount++;
  249. }
  250. }
  251. int ButtonHeight = MulDiv(mcButtonHeight, DialogUnits.y, 8);
  252. int ButtonSpacing = MulDiv(mcButtonSpacing, DialogUnits.x, 4);
  253. SetRect(&TextRect, 0, 0, Screen->Width / 2, 0);
  254. DrawText(Result->Canvas->Handle, Msg.c_str(), Msg.Length() + 1, &TextRect,
  255. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK |
  256. Result->DrawTextBiDiModeFlagsReadingOnly());
  257. const char * IconID = IconIDs[DlgType];
  258. int IconTextWidth = TextRect.Right;
  259. int IconTextHeight = TextRect.Bottom;
  260. if (IconID != NULL)
  261. {
  262. IconTextWidth += 32 + HorzSpacing;
  263. if (IconTextHeight < 32)
  264. {
  265. IconTextHeight = 32;
  266. }
  267. }
  268. if (MoreMessages != NULL)
  269. {
  270. TMemo * MessageMemo = new TMemo(Result);
  271. MessageMemo->Parent = Result;
  272. MessageMemo->ReadOnly = true;
  273. MessageMemo->WantReturns = False;
  274. MessageMemo->ScrollBars = ssVertical;
  275. MessageMemo->Anchors = TAnchors() << akLeft << akRight << akTop; //akBottom;
  276. MessageMemo->Color = clBtnFace;
  277. MessageMemo->Lines->Text = MoreMessages->Text;
  278. Result->MessageMemo = MessageMemo;
  279. }
  280. int ButtonGroupWidth = 0;
  281. if (ButtonControlsCount > 0)
  282. {
  283. ButtonGroupWidth = ButtonWidth * ButtonControlsCount +
  284. ButtonSpacing * (ButtonControlsCount - 1);
  285. }
  286. int MoreMessageWidth = (MoreMessages != NULL ?
  287. MulDiv(mcMoreMessageWidth, DialogUnits.x, 4) : 0);
  288. int MoreMessageHeight = (MoreMessages != NULL ?
  289. MulDiv(mcMoreMessageHeight, DialogUnits.y, 12) : 0);
  290. int AClientWidth =
  291. (IconTextWidth > ButtonGroupWidth ? IconTextWidth : ButtonGroupWidth) +
  292. HorzMargin * 2;
  293. Result->ClientWidth = (AClientWidth > MoreMessageWidth ?
  294. AClientWidth : MoreMessageWidth);
  295. Result->ClientHeight = IconTextHeight + ButtonHeight + VertSpacing +
  296. VertMargin * 2 + MoreMessageHeight;
  297. Result->Left = (Screen->Width / 2) - (Result->Width / 2);
  298. Result->Top = (Screen->Height / 2) - (Result->Height / 2);
  299. if (DlgType != mtCustom)
  300. {
  301. Result->Caption = LoadResourceString(Captions[DlgType]);
  302. }
  303. else
  304. {
  305. Result->Caption = Application->Title;
  306. }
  307. if (IconID != NULL)
  308. {
  309. TImage * Image = new TImage(Result);
  310. Image->Name = "Image";
  311. Image->Parent = Result;
  312. Image->Picture->Icon->Handle = LoadIcon(0, IconID);
  313. Image->SetBounds(HorzMargin, VertMargin, 32, 32);
  314. }
  315. TLabel * Message = new TLabel(Result);
  316. Result->Message = Message;
  317. Message->Name = "Message";
  318. Message->Parent = Result;
  319. Message->WordWrap = true;
  320. Message->Caption = Msg;
  321. Message->BoundsRect = TextRect;
  322. Message->BiDiMode = Result->BiDiMode;
  323. // added to show & as & for messages containing !& pattern of custom commands
  324. // (suppose that we actually never want to use & as accel in message text)
  325. Message->ShowAccelChar = false;
  326. int ALeft = IconTextWidth - TextRect.Right + HorzMargin;
  327. Message->SetBounds(ALeft, VertMargin, TextRect.Right, TextRect.Bottom);
  328. int ButtonTop = IconTextHeight + VertMargin + VertSpacing + MoreMessageHeight;
  329. if (Result->MessageMemo != NULL)
  330. {
  331. Result->MessageMemo->BoundsRect = TRect(Message->Left,
  332. Message->Top + Message->Height + VertSpacing,
  333. Result->ClientWidth - HorzMargin,
  334. Message->Top + Message->Height + VertSpacing + MoreMessageHeight);
  335. // rather hack, whole control positioning is wrong
  336. if (Result->MessageMemo->Top + Result->MessageMemo->Height > ButtonTop - VertSpacing)
  337. {
  338. Result->MessageMemo->Height =
  339. (ButtonTop - VertSpacing) - Result->MessageMemo->Top;
  340. }
  341. }
  342. int X = (Result->ClientWidth - ButtonGroupWidth) / 2;
  343. for (int i = 0; i < ButtonControlsCount; i++)
  344. {
  345. ButtonControls[i]->SetBounds(X,
  346. ButtonTop, ButtonWidth, ButtonHeight);
  347. X += ButtonWidth + ButtonSpacing;
  348. }
  349. return Result;
  350. }
  351. //---------------------------------------------------------------------------
  352. TForm * __fastcall CreateMoreMessageDialog(const AnsiString & Msg,
  353. TStrings * MoreMessages, TMsgDlgType DlgType, TMsgDlgButtons Buttons,
  354. TQueryButtonAlias * Aliases, unsigned int AliasesCount,
  355. TMsgDlgBtn TimeoutResult, TButton ** TimeoutButton)
  356. {
  357. return TMessageForm::Create(Msg, MoreMessages, DlgType, Buttons,
  358. Aliases, AliasesCount, TimeoutResult, TimeoutButton);
  359. }