MessageBox.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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.2 2004/10/05 17:10:57 pbartok
  33. // - Partial implementation by Benjamin Dasnois
  34. //
  35. // Revision 1.1 2004/07/26 11:41:35 jordi
  36. // initial messagebox implementation
  37. //
  38. //
  39. // INCOMPLETE
  40. using System;
  41. using System.Drawing;
  42. namespace System.Windows.Forms
  43. {
  44. public class MessageBox
  45. {
  46. #region Private MessageBoxForm class
  47. private class MessageBoxForm : Form
  48. {
  49. #region MessageBoxFrom Local Variables
  50. string msgbox_text;
  51. bool size_known =false;
  52. const int space_image_text= 20;
  53. Image icon_image;
  54. Point textleft_up;
  55. MessageBoxButtons msgbox_buttons;
  56. bool buttons_placed = false;
  57. int button_left;
  58. #endregion // MessageBoxFrom Local Variables
  59. #region MessageBoxForm Constructors
  60. public MessageBoxForm (IWin32Window owner, string text, string caption,
  61. MessageBoxButtons buttons, MessageBoxIcon icon)
  62. {
  63. switch (icon) {
  64. case MessageBoxIcon.None: {
  65. icon_image = null;
  66. break;
  67. }
  68. case MessageBoxIcon.Error: {
  69. break;
  70. }
  71. case MessageBoxIcon.Question: {
  72. break;
  73. }
  74. case MessageBoxIcon.Exclamation: {
  75. break;
  76. }
  77. case MessageBoxIcon.Asterisk: {
  78. break;
  79. }
  80. }
  81. msgbox_text = text;
  82. msgbox_buttons = buttons;
  83. this.Text = caption;
  84. this.Paint += new PaintEventHandler (MessageBoxForm_Paint);
  85. }
  86. [MonoTODO]
  87. public MessageBoxForm (IWin32Window owner, string text, string caption,
  88. MessageBoxButtons buttons, MessageBoxIcon icon,
  89. MessageBoxDefaultButton defaultButton, MessageBoxOptions options) : this (owner, text, caption, buttons, icon)
  90. {
  91. // Still needs to implement defaultButton and options
  92. }
  93. #endregion // MessageBoxForm Constructors
  94. #region MessageBoxForm Methods
  95. public DialogResult RunDialog ()
  96. {
  97. this.StartPosition = FormStartPosition.CenterScreen;
  98. this.ShowDialog ();
  99. return this.DialogResult;
  100. }
  101. private void MessageBoxForm_Paint(object sender, PaintEventArgs e)
  102. {
  103. if (size_known == false) {
  104. InitFormsSize (e);
  105. }
  106. e.Graphics.DrawString (msgbox_text, this.Font, new SolidBrush(Color.Black), textleft_up);
  107. }
  108. private void InitFormsSize(PaintEventArgs e)
  109. {
  110. int tb_width = 0;
  111. // First we have to know the size of text + image
  112. Drawing.SizeF tsize = e.Graphics.MeasureString (msgbox_text, this.Font);
  113. if (!(icon_image == null)) {
  114. tsize.Width += icon_image.Width;
  115. textleft_up = new Point (icon_image.Width + space_image_text, 0);
  116. } else {
  117. textleft_up = new Point (0, 0);
  118. }
  119. // Now we want to know the width of buttons
  120. switch (msgbox_buttons) {
  121. case MessageBoxButtons.OK: {
  122. tb_width = 110 * 1;
  123. break;
  124. }
  125. case MessageBoxButtons.OKCancel: {
  126. tb_width = 110 * 2;
  127. break;
  128. }
  129. case MessageBoxButtons.AbortRetryIgnore: {
  130. tb_width = 110 * 3;
  131. break;
  132. }
  133. case MessageBoxButtons.YesNoCancel: {
  134. tb_width = 110 * 3;
  135. break;
  136. }
  137. case MessageBoxButtons.YesNo: {
  138. tb_width = 110 * 2;
  139. break;
  140. }
  141. case MessageBoxButtons.RetryCancel: {
  142. tb_width = 110 * 2;
  143. break;
  144. }
  145. }
  146. // Now we choose the good size for the form
  147. if (tsize.ToSize ().Width > tb_width) {
  148. this.Width = tsize.ToSize().Width + 10;
  149. } else {
  150. this.Width = tb_width;
  151. }
  152. this.Height = tsize.ToSize ().Height + 80;
  153. // Now we set the left of the buttons
  154. button_left = (this.Width / 2) - (tb_width / 2);
  155. AddButtons ();
  156. size_known = true;
  157. this.Refresh ();
  158. }
  159. #endregion // MessageBoxForm Methods
  160. #region Functions for Adding buttons
  161. private void AddButtons()
  162. {
  163. if (!buttons_placed) {
  164. switch (msgbox_buttons) {
  165. case MessageBoxButtons.OK: {
  166. AddOkButton (0 + button_left);
  167. break;
  168. }
  169. case MessageBoxButtons.OKCancel: {
  170. AddOkButton (0 + button_left);
  171. AddCancelButton (110 + button_left);
  172. break;
  173. }
  174. case MessageBoxButtons.AbortRetryIgnore: {
  175. AddAbortButton (0 + button_left);
  176. AddRetryButton (110 + button_left);
  177. AddIgnoreButton (220 + button_left);
  178. break;
  179. }
  180. case MessageBoxButtons.YesNoCancel: {
  181. AddYesButton (0 + button_left);
  182. AddNoButton (110 + button_left);
  183. AddCancelButton (220 + button_left);
  184. break;
  185. }
  186. case MessageBoxButtons.YesNo: {
  187. AddYesButton (0 + button_left);
  188. AddNoButton (110 + button_left);
  189. break;
  190. }
  191. case MessageBoxButtons.RetryCancel: {
  192. AddRetryButton (0 + button_left);
  193. AddCancelButton (110 + button_left);
  194. break;
  195. }
  196. }
  197. buttons_placed = true;
  198. }
  199. }
  200. private void AddOkButton (int left)
  201. {
  202. Button bok = new Button ();
  203. bok.Text = "OK";
  204. this.Controls.Add (bok);
  205. bok.Width = 100;
  206. bok.Height = 30;
  207. bok.Top = this.Height - 70;
  208. bok.Left = left;
  209. bok.Show ();
  210. bok.Click += new EventHandler (OkClick);
  211. }
  212. private void AddCancelButton (int left)
  213. {
  214. Button bcan = new Button ();
  215. bcan.Text = "Cancel";
  216. this.Controls.Add (bcan);
  217. bcan.Width = 100;
  218. bcan.Height = 30;
  219. bcan.Top = this.Height - 70;
  220. bcan.Left = left;
  221. bcan.Show ();
  222. bcan.Click += new EventHandler (CancelClick);
  223. }
  224. private void AddAbortButton (int left)
  225. {
  226. Button babort = new Button ();
  227. babort.Text = "Abort";
  228. this.Controls.Add (babort);
  229. babort.Width = 100;
  230. babort.Height = 30;
  231. babort.Top = this.Height - 70;
  232. babort.Left = left;
  233. babort.Show ();
  234. babort.Click += new EventHandler (AbortClick);
  235. }
  236. private void AddRetryButton(int left)
  237. {
  238. Button bretry = new Button ();
  239. bretry.Text = "Retry";
  240. this.Controls.Add (bretry);
  241. bretry.Width = 100;
  242. bretry.Height = 30;
  243. bretry.Top = this.Height - 70;
  244. bretry.Left = left;
  245. bretry.Show ();
  246. bretry.Click += new EventHandler (RetryClick);
  247. }
  248. private void AddIgnoreButton (int left)
  249. {
  250. Button bignore = new Button ();
  251. bignore.Text = "Ignore";
  252. this.Controls.Add (bignore);
  253. bignore.Width = 100;
  254. bignore.Height = 30;
  255. bignore.Top = this.Height - 70;
  256. bignore.Left = left;
  257. bignore.Show ();
  258. bignore.Click += new EventHandler (IgnoreClick);
  259. }
  260. private void AddYesButton (int left)
  261. {
  262. Button byes = new Button ();
  263. byes.Text = "Yes";
  264. this.Controls.Add (byes);
  265. byes.Width = 100;
  266. byes.Height = 30;
  267. byes.Top = this.Height - 70;
  268. byes.Left = left;
  269. byes.Show ();
  270. byes.Click += new EventHandler (YesClick);
  271. }
  272. private void AddNoButton (int left)
  273. {
  274. Button bno = new Button ();
  275. bno.Text = "No";
  276. this.Controls.Add (bno);
  277. bno.Width = 100;
  278. bno.Height = 30;
  279. bno.Top = this.Height - 70;
  280. bno.Left = left;
  281. bno.Show ();
  282. bno.Click += new EventHandler (NoClick);
  283. }
  284. #endregion
  285. #region Button click handlers
  286. private void OkClick (object sender, EventArgs e)
  287. {
  288. this.DialogResult = DialogResult.OK;
  289. this.Close ();
  290. }
  291. private void CancelClick (object sender, EventArgs e)
  292. {
  293. this.DialogResult = DialogResult.Cancel;
  294. this.Close ();
  295. }
  296. private void AbortClick (object sender, EventArgs e)
  297. {
  298. this.DialogResult = DialogResult.Abort;
  299. this.Close ();
  300. }
  301. private void RetryClick (object sender, EventArgs e)
  302. {
  303. this.DialogResult = DialogResult.Retry;
  304. this.Close ();
  305. }
  306. private void IgnoreClick (object sender, EventArgs e)
  307. {
  308. this.DialogResult = DialogResult.Ignore;
  309. this.Close ();
  310. }
  311. private void YesClick (object sender, EventArgs e)
  312. {
  313. this.DialogResult = DialogResult.Yes;
  314. this.Close ();
  315. }
  316. private void NoClick (object sender, EventArgs e)
  317. {
  318. this.DialogResult = DialogResult.No;
  319. this.Close ();
  320. }
  321. #endregion
  322. }
  323. #endregion // Private MessageBoxForm class
  324. #region Constructors
  325. private MessageBox ()
  326. {
  327. }
  328. #endregion // Constructors
  329. #region Public Static Methods
  330. public static DialogResult Show (string text)
  331. {
  332. MessageBoxForm form = new MessageBoxForm (null, text, string.Empty,
  333. MessageBoxButtons.OK, MessageBoxIcon.None);
  334. return form.RunDialog ();
  335. }
  336. public static DialogResult Show (IWin32Window owner, string text)
  337. {
  338. MessageBoxForm form = new MessageBoxForm (owner, text, string.Empty,
  339. MessageBoxButtons.OK, MessageBoxIcon.None);
  340. return form.RunDialog ();
  341. }
  342. public static DialogResult Show (string text, string caption)
  343. {
  344. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  345. MessageBoxButtons.OK, MessageBoxIcon.None);
  346. return form.RunDialog ();
  347. }
  348. public static DialogResult Show (string text, string caption, MessageBoxButtons buttons)
  349. {
  350. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  351. buttons, MessageBoxIcon.None);
  352. return form.RunDialog ();
  353. }
  354. public static DialogResult Show (IWin32Window owner, string text, string caption,
  355. MessageBoxButtons buttons)
  356. {
  357. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  358. buttons, MessageBoxIcon.None);
  359. return form.RunDialog ();
  360. }
  361. public static DialogResult Show (IWin32Window owner, string text, string caption,
  362. MessageBoxButtons buttons, MessageBoxIcon icon)
  363. {
  364. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  365. buttons, icon);
  366. return form.RunDialog ();
  367. }
  368. public static DialogResult Show (IWin32Window owner, string text, string caption)
  369. {
  370. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  371. MessageBoxButtons.OK, MessageBoxIcon.None);
  372. return form.RunDialog ();
  373. }
  374. public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
  375. MessageBoxIcon icon)
  376. {
  377. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  378. buttons, icon);
  379. return form.RunDialog ();
  380. }
  381. public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
  382. MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
  383. {
  384. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  385. buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
  386. return form.RunDialog ();
  387. }
  388. public static DialogResult Show (IWin32Window owner, string text, string caption,
  389. MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
  390. {
  391. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  392. buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
  393. return form.RunDialog ();
  394. }
  395. public static DialogResult
  396. Show (string text, string caption,
  397. MessageBoxButtons buttons, MessageBoxIcon icon,
  398. MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
  399. {
  400. MessageBoxForm form = new MessageBoxForm (null, text, caption,
  401. buttons, icon, defaultButton, options);
  402. return form.RunDialog ();
  403. }
  404. public static DialogResult Show (IWin32Window owner, string text, string caption,
  405. MessageBoxButtons buttons, MessageBoxIcon icon,
  406. MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
  407. {
  408. MessageBoxForm form = new MessageBoxForm (owner, text, caption,
  409. buttons, icon, defaultButton, options);
  410. return form.RunDialog ();
  411. }
  412. #endregion // Public Static Methods
  413. }
  414. }