MessageBox.cs 13 KB

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