MessageBox.cs 13 KB

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