MessageBox.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. // Benjamin Dasnois, [email protected]
  25. //
  26. // TODO:
  27. // - Complete the implementation when icons are available, Form.BorderStyle is available.
  28. // - Add support for MessageBoxOptions and MessageBoxDefaultButton.
  29. //
  30. //
  31. // $Log: MessageBox.cs,v $
  32. // Revision 1.4 2004/10/15 06:30:56 ravindra
  33. // - MessageBox on windows does not have min/max buttons.
  34. // This change in CreateParams fixes this on Windows. We
  35. // still need to implement this windowstyle behavior in
  36. // our X11 driver.
  37. //
  38. // Revision 1.3 2004/10/14 06:14:03 ravindra
  39. // Moved InitFormSize () out of Paint method and removed unnecessary calls to Button.Show () method.
  40. //
  41. // Revision 1.2 2004/10/05 17:10:57 pbartok
  42. // - Partial implementation by Benjamin Dasnois
  43. //
  44. // Revision 1.1 2004/07/26 11:41:35 jordi
  45. // initial messagebox implementation
  46. //
  47. //
  48. // INCOMPLETE
  49. using System;
  50. using System.Drawing;
  51. namespace System.Windows.Forms
  52. {
  53. public class MessageBox
  54. {
  55. #region Private MessageBoxForm class
  56. private class MessageBoxForm : Form
  57. {
  58. #region MessageBoxFrom Local Variables
  59. string msgbox_text;
  60. bool size_known =false;
  61. const int space_image_text= 20;
  62. Image icon_image;
  63. Point textleft_up;
  64. MessageBoxButtons msgbox_buttons;
  65. bool buttons_placed = false;
  66. int button_left;
  67. #endregion // MessageBoxFrom Local Variables
  68. #region MessageBoxForm Constructors
  69. public MessageBoxForm (IWin32Window owner, string text, string caption,
  70. MessageBoxButtons buttons, MessageBoxIcon icon)
  71. {
  72. switch (icon) {
  73. case MessageBoxIcon.None: {
  74. icon_image = null;
  75. break;
  76. }
  77. case MessageBoxIcon.Error: {
  78. break;
  79. }
  80. case MessageBoxIcon.Question: {
  81. break;
  82. }
  83. case MessageBoxIcon.Exclamation: {
  84. break;
  85. }
  86. case MessageBoxIcon.Asterisk: {
  87. break;
  88. }
  89. }
  90. msgbox_text = text;
  91. msgbox_buttons = buttons;
  92. this.Text = caption;
  93. this.Paint += new PaintEventHandler (MessageBoxForm_Paint);
  94. }
  95. [MonoTODO]
  96. public MessageBoxForm (IWin32Window owner, string text, string caption,
  97. MessageBoxButtons buttons, MessageBoxIcon icon,
  98. MessageBoxDefaultButton defaultButton, MessageBoxOptions options) : this (owner, text, caption, buttons, icon)
  99. {
  100. // Still needs to implement defaultButton and options
  101. }
  102. #endregion // MessageBoxForm Constructors
  103. #region Protected Instance Properties
  104. protected override CreateParams CreateParams {
  105. get {
  106. CreateParams create_params = base.CreateParams;
  107. create_params.Style = (int)WindowStyles.WS_OVERLAPPED;
  108. create_params.Style |= (int)WindowStyles.WS_CAPTION;
  109. create_params.Style |= (int)WindowStyles.WS_SYSMENU;
  110. create_params.Style |= (int)WindowStyles.WS_VISIBLE;
  111. return create_params;
  112. }
  113. }
  114. #endregion // Protected Instance Properties
  115. #region MessageBoxForm Methods
  116. public DialogResult RunDialog ()
  117. {
  118. this.StartPosition = FormStartPosition.CenterScreen;
  119. if (size_known == false) {
  120. InitFormsSize ();
  121. }
  122. this.ShowDialog ();
  123. return this.DialogResult;
  124. }
  125. private void MessageBoxForm_Paint (object sender, PaintEventArgs e)
  126. {
  127. e.Graphics.DrawString (msgbox_text, this.Font, new SolidBrush(Color.Black), textleft_up);
  128. }
  129. private void InitFormsSize ()
  130. {
  131. int tb_width = 0;
  132. // First we have to know the size of text + image
  133. Drawing.SizeF tsize = this.DeviceContext.MeasureString (msgbox_text, this.Font);
  134. if (!(icon_image == null)) {
  135. tsize.Width += icon_image.Width;
  136. textleft_up = new Point (icon_image.Width + space_image_text, 0);
  137. } else {
  138. textleft_up = new Point (0, 0);
  139. }
  140. // Now we want to know the width of buttons
  141. switch (msgbox_buttons) {
  142. case MessageBoxButtons.OK: {
  143. tb_width = 110 * 1;
  144. break;
  145. }
  146. case MessageBoxButtons.OKCancel: {
  147. tb_width = 110 * 2;
  148. break;
  149. }
  150. case MessageBoxButtons.AbortRetryIgnore: {
  151. tb_width = 110 * 3;
  152. break;
  153. }
  154. case MessageBoxButtons.YesNoCancel: {
  155. tb_width = 110 * 3;
  156. break;
  157. }
  158. case MessageBoxButtons.YesNo: {
  159. tb_width = 110 * 2;
  160. break;
  161. }
  162. case MessageBoxButtons.RetryCancel: {
  163. tb_width = 110 * 2;
  164. break;
  165. }
  166. }
  167. // Now we choose the good size for the form
  168. if (tsize.ToSize ().Width > tb_width) {
  169. this.Width = tsize.ToSize().Width + 10;
  170. } else {
  171. this.Width = tb_width;
  172. }
  173. this.Height = tsize.ToSize ().Height + 80;
  174. // Now we set the left of the buttons
  175. button_left = (this.Width / 2) - (tb_width / 2);
  176. AddButtons ();
  177. size_known = true;
  178. }
  179. #endregion // MessageBoxForm Methods
  180. #region Functions for Adding buttons
  181. private void AddButtons()
  182. {
  183. if (!buttons_placed) {
  184. switch (msgbox_buttons) {
  185. case MessageBoxButtons.OK: {
  186. AddOkButton (0 + button_left);
  187. break;
  188. }
  189. case MessageBoxButtons.OKCancel: {
  190. AddOkButton (0 + button_left);
  191. AddCancelButton (110 + button_left);
  192. break;
  193. }
  194. case MessageBoxButtons.AbortRetryIgnore: {
  195. AddAbortButton (0 + button_left);
  196. AddRetryButton (110 + button_left);
  197. AddIgnoreButton (220 + button_left);
  198. break;
  199. }
  200. case MessageBoxButtons.YesNoCancel: {
  201. AddYesButton (0 + button_left);
  202. AddNoButton (110 + button_left);
  203. AddCancelButton (220 + button_left);
  204. break;
  205. }
  206. case MessageBoxButtons.YesNo: {
  207. AddYesButton (0 + button_left);
  208. AddNoButton (110 + button_left);
  209. break;
  210. }
  211. case MessageBoxButtons.RetryCancel: {
  212. AddRetryButton (0 + button_left);
  213. AddCancelButton (110 + button_left);
  214. break;
  215. }
  216. }
  217. buttons_placed = true;
  218. }
  219. }
  220. private void AddOkButton (int left)
  221. {
  222. Button bok = new Button ();
  223. bok.Text = "OK";
  224. bok.Width = 100;
  225. bok.Height = 30;
  226. bok.Top = this.Height - 70;
  227. bok.Left = left;
  228. bok.Click += new EventHandler (OkClick);
  229. this.Controls.Add (bok);
  230. }
  231. private void AddCancelButton (int left)
  232. {
  233. Button bcan = new Button ();
  234. bcan.Text = "Cancel";
  235. bcan.Width = 100;
  236. bcan.Height = 30;
  237. bcan.Top = this.Height - 70;
  238. bcan.Left = left;
  239. bcan.Click += new EventHandler (CancelClick);
  240. this.Controls.Add (bcan);
  241. }
  242. private void AddAbortButton (int left)
  243. {
  244. Button babort = new Button ();
  245. babort.Text = "Abort";
  246. babort.Width = 100;
  247. babort.Height = 30;
  248. babort.Top = this.Height - 70;
  249. babort.Left = left;
  250. babort.Click += new EventHandler (AbortClick);
  251. this.Controls.Add (babort);
  252. }
  253. private void AddRetryButton(int left)
  254. {
  255. Button bretry = new Button ();
  256. bretry.Text = "Retry";
  257. bretry.Width = 100;
  258. bretry.Height = 30;
  259. bretry.Top = this.Height - 70;
  260. bretry.Left = left;
  261. bretry.Click += new EventHandler (RetryClick);
  262. this.Controls.Add (bretry);
  263. }
  264. private void AddIgnoreButton (int left)
  265. {
  266. Button bignore = new Button ();
  267. bignore.Text = "Ignore";
  268. bignore.Width = 100;
  269. bignore.Height = 30;
  270. bignore.Top = this.Height - 70;
  271. bignore.Left = left;
  272. bignore.Click += new EventHandler (IgnoreClick);
  273. this.Controls.Add (bignore);
  274. }
  275. private void AddYesButton (int left)
  276. {
  277. Button byes = new Button ();
  278. byes.Text = "Yes";
  279. byes.Width = 100;
  280. byes.Height = 30;
  281. byes.Top = this.Height - 70;
  282. byes.Left = left;
  283. byes.Click += new EventHandler (YesClick);
  284. this.Controls.Add (byes);
  285. }
  286. private void AddNoButton (int left)
  287. {
  288. Button bno = new Button ();
  289. bno.Text = "No";
  290. bno.Width = 100;
  291. bno.Height = 30;
  292. bno.Top = this.Height - 70;
  293. bno.Left = left;
  294. bno.Click += new EventHandler (NoClick);
  295. this.Controls.Add (bno);
  296. }
  297. #endregion
  298. #region Button click handlers
  299. private void OkClick (object sender, EventArgs e)
  300. {
  301. this.DialogResult = DialogResult.OK;
  302. this.Close ();
  303. }
  304. private void CancelClick (object sender, EventArgs e)
  305. {
  306. this.DialogResult = DialogResult.Cancel;
  307. this.Close ();
  308. }
  309. private void AbortClick (object sender, EventArgs e)
  310. {
  311. this.DialogResult = DialogResult.Abort;
  312. this.Close ();
  313. }
  314. private void RetryClick (object sender, EventArgs e)
  315. {
  316. this.DialogResult = DialogResult.Retry;
  317. this.Close ();
  318. }
  319. private void IgnoreClick (object sender, EventArgs e)
  320. {
  321. this.DialogResult = DialogResult.Ignore;
  322. this.Close ();
  323. }
  324. private void YesClick (object sender, EventArgs e)
  325. {
  326. this.DialogResult = DialogResult.Yes;
  327. this.Close ();
  328. }
  329. private void NoClick (object sender, EventArgs e)
  330. {
  331. this.DialogResult = DialogResult.No;
  332. this.Close ();
  333. }
  334. #endregion
  335. }
  336. #endregion // Private MessageBoxForm class
  337. #region Constructors
  338. private MessageBox ()
  339. {
  340. }
  341. #endregion // Constructors
  342. #region Public Static Methods
  343. public static DialogResult Show (string text)
  344. {
  345. MessageBoxForm form = new MessageBoxForm (null, text, string.Empty,
  346. MessageBoxButtons.OK, MessageBoxIcon.None);
  347. return form.RunDialog ();
  348. }
  349. public static DialogResult Show (IWin32Window owner, string text)
  350. {
  351. MessageBoxForm form = new MessageBoxForm (owner, text, string.Empty,
  352. MessageBoxButtons.OK, MessageBoxIcon.None);
  353. return form.RunDialog ();
  354. }
  355. public static DialogResult Show (string text, string caption)
  356. {
  357. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  358. MessageBoxButtons.OK, MessageBoxIcon.None);
  359. return form.RunDialog ();
  360. }
  361. public static DialogResult Show (string text, string caption, MessageBoxButtons buttons)
  362. {
  363. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  364. buttons, MessageBoxIcon.None);
  365. return form.RunDialog ();
  366. }
  367. public static DialogResult Show (IWin32Window owner, string text, string caption,
  368. MessageBoxButtons buttons)
  369. {
  370. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  371. buttons, MessageBoxIcon.None);
  372. return form.RunDialog ();
  373. }
  374. public static DialogResult Show (IWin32Window owner, string text, string caption,
  375. MessageBoxButtons buttons, MessageBoxIcon icon)
  376. {
  377. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  378. buttons, icon);
  379. return form.RunDialog ();
  380. }
  381. public static DialogResult Show (IWin32Window owner, string text, string caption)
  382. {
  383. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  384. MessageBoxButtons.OK, MessageBoxIcon.None);
  385. return form.RunDialog ();
  386. }
  387. public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
  388. MessageBoxIcon icon)
  389. {
  390. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  391. buttons, icon);
  392. return form.RunDialog ();
  393. }
  394. public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
  395. MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
  396. {
  397. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  398. buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
  399. return form.RunDialog ();
  400. }
  401. public static DialogResult Show (IWin32Window owner, string text, string caption,
  402. MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
  403. {
  404. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  405. buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
  406. return form.RunDialog ();
  407. }
  408. public static DialogResult
  409. Show (string text, string caption,
  410. MessageBoxButtons buttons, MessageBoxIcon icon,
  411. MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
  412. {
  413. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  414. buttons, icon, defaultButton, options);
  415. return form.RunDialog ();
  416. }
  417. public static DialogResult Show (IWin32Window owner, string text, string caption,
  418. MessageBoxButtons buttons, MessageBoxIcon icon,
  419. MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
  420. {
  421. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  422. buttons, icon, defaultButton, options);
  423. return form.RunDialog ();
  424. }
  425. #endregion // Public Static Methods
  426. }
  427. }