MessageBox.cs 13 KB

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