**** Merged from MCS ****
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / MessageBox.cs
blob3dc6dadec220296aee8e9ffdfa0d47c6c71408fd
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.
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.
20 // Copyright (c) 2004 Novell, Inc.
22 // Authors:
23 // Jordi Mas i Hernandez, jordi@ximian.com
24 // Benjamin Dasnois, benjamin.dasnois@gmail.com
26 // TODO:
27 // - Complete the implementation when icons are available, Form.BorderStyle is available.
28 // - Add support for MessageBoxOptions and MessageBoxDefaultButton.
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.
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.
41 // Revision 1.2 2004/10/05 17:10:57 pbartok
42 // - Partial implementation by Benjamin Dasnois
44 // Revision 1.1 2004/07/26 11:41:35 jordi
45 // initial messagebox implementation
49 // INCOMPLETE
51 using System;
52 using System.Drawing;
54 namespace System.Windows.Forms
56 public class MessageBox
58 #region Private MessageBoxForm class
59 private class MessageBoxForm : Form
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
72 #region MessageBoxForm Constructors
73 public MessageBoxForm (IWin32Window owner, string text, string caption,
74 MessageBoxButtons buttons, MessageBoxIcon icon)
76 switch (icon) {
77 case MessageBoxIcon.None: {
78 icon_image = null;
79 break;
82 case MessageBoxIcon.Error: {
83 break;
86 case MessageBoxIcon.Question: {
87 break;
90 case MessageBoxIcon.Exclamation: {
91 break;
94 case MessageBoxIcon.Asterisk: {
95 break;
99 msgbox_text = text;
100 msgbox_buttons = buttons;
101 this.Text = caption;
102 this.Paint += new PaintEventHandler (MessageBoxForm_Paint);
105 [MonoTODO]
106 public MessageBoxForm (IWin32Window owner, string text, string caption,
107 MessageBoxButtons buttons, MessageBoxIcon icon,
108 MessageBoxDefaultButton defaultButton, MessageBoxOptions options) : this (owner, text, caption, buttons, icon)
110 // Still needs to implement defaultButton and options
113 #endregion // MessageBoxForm Constructors
115 #region Protected Instance Properties
116 protected override CreateParams CreateParams {
117 get {
118 CreateParams create_params = base.CreateParams;
119 create_params.Style = (int)WindowStyles.WS_OVERLAPPED;
120 create_params.Style |= (int)WindowStyles.WS_CAPTION;
121 create_params.Style |= (int)WindowStyles.WS_SYSMENU;
122 create_params.Style |= (int)WindowStyles.WS_VISIBLE;
124 return create_params;
127 #endregion // Protected Instance Properties
129 #region MessageBoxForm Methods
130 public DialogResult RunDialog ()
132 this.StartPosition = FormStartPosition.CenterScreen;
134 if (size_known == false) {
135 InitFormsSize ();
138 this.ShowDialog ();
140 return this.DialogResult;
144 private void MessageBoxForm_Paint (object sender, PaintEventArgs e)
146 e.Graphics.DrawString (msgbox_text, this.Font, new SolidBrush(Color.Black), textleft_up);
149 private void InitFormsSize ()
151 int tb_width = 0;
153 // First we have to know the size of text + image
154 Drawing.SizeF tsize = this.DeviceContext.MeasureString (msgbox_text, this.Font);
156 if (!(icon_image == null)) {
157 tsize.Width += icon_image.Width;
158 textleft_up = new Point (icon_image.Width + space_image_text, 0);
159 } else {
160 textleft_up = new Point (0, 0);
163 // Now we want to know the width of buttons
165 switch (msgbox_buttons) {
166 case MessageBoxButtons.OK: {
167 tb_width = 110 * 1;
168 break;
171 case MessageBoxButtons.OKCancel: {
172 tb_width = 110 * 2;
173 break;
176 case MessageBoxButtons.AbortRetryIgnore: {
177 tb_width = 110 * 3;
178 break;
181 case MessageBoxButtons.YesNoCancel: {
182 tb_width = 110 * 3;
183 break;
186 case MessageBoxButtons.YesNo: {
187 tb_width = 110 * 2;
188 break;
191 case MessageBoxButtons.RetryCancel: {
192 tb_width = 110 * 2;
193 break;
197 // Now we choose the good size for the form
198 if (tsize.ToSize ().Width > tb_width) {
199 this.Width = tsize.ToSize().Width + 10;
200 } else {
201 this.Width = tb_width;
203 this.Height = tsize.ToSize ().Height + 80;
205 // Now we set the left of the buttons
206 button_left = (this.Width / 2) - (tb_width / 2);
207 AddButtons ();
208 size_known = true;
210 #endregion // MessageBoxForm Methods
212 #region Functions for Adding buttons
213 private void AddButtons()
215 if (!buttons_placed) {
216 switch (msgbox_buttons) {
217 case MessageBoxButtons.OK: {
218 AddOkButton (0 + button_left);
219 break;
222 case MessageBoxButtons.OKCancel: {
223 AddOkButton (0 + button_left);
224 AddCancelButton (110 + button_left);
225 break;
228 case MessageBoxButtons.AbortRetryIgnore: {
229 AddAbortButton (0 + button_left);
230 AddRetryButton (110 + button_left);
231 AddIgnoreButton (220 + button_left);
232 break;
235 case MessageBoxButtons.YesNoCancel: {
236 AddYesButton (0 + button_left);
237 AddNoButton (110 + button_left);
238 AddCancelButton (220 + button_left);
239 break;
242 case MessageBoxButtons.YesNo: {
243 AddYesButton (0 + button_left);
244 AddNoButton (110 + button_left);
245 break;
248 case MessageBoxButtons.RetryCancel: {
249 AddRetryButton (0 + button_left);
250 AddCancelButton (110 + button_left);
251 break;
254 buttons_placed = true;
258 private void AddOkButton (int left)
260 Button bok = new Button ();
261 bok.Text = "OK";
262 bok.Width = 100;
263 bok.Height = 30;
264 bok.Top = this.Height - 70;
265 bok.Left = left;
266 bok.Click += new EventHandler (OkClick);
267 this.Controls.Add (bok);
270 private void AddCancelButton (int left)
272 Button bcan = new Button ();
273 bcan.Text = "Cancel";
274 bcan.Width = 100;
275 bcan.Height = 30;
276 bcan.Top = this.Height - 70;
277 bcan.Left = left;
278 bcan.Click += new EventHandler (CancelClick);
279 this.Controls.Add (bcan);
282 private void AddAbortButton (int left)
284 Button babort = new Button ();
285 babort.Text = "Abort";
286 babort.Width = 100;
287 babort.Height = 30;
288 babort.Top = this.Height - 70;
289 babort.Left = left;
290 babort.Click += new EventHandler (AbortClick);
291 this.Controls.Add (babort);
294 private void AddRetryButton(int left)
296 Button bretry = new Button ();
297 bretry.Text = "Retry";
298 bretry.Width = 100;
299 bretry.Height = 30;
300 bretry.Top = this.Height - 70;
301 bretry.Left = left;
302 bretry.Click += new EventHandler (RetryClick);
303 this.Controls.Add (bretry);
306 private void AddIgnoreButton (int left)
308 Button bignore = new Button ();
309 bignore.Text = "Ignore";
310 bignore.Width = 100;
311 bignore.Height = 30;
312 bignore.Top = this.Height - 70;
313 bignore.Left = left;
314 bignore.Click += new EventHandler (IgnoreClick);
315 this.Controls.Add (bignore);
318 private void AddYesButton (int left)
320 Button byes = new Button ();
321 byes.Text = "Yes";
322 byes.Width = 100;
323 byes.Height = 30;
324 byes.Top = this.Height - 70;
325 byes.Left = left;
326 byes.Click += new EventHandler (YesClick);
327 this.Controls.Add (byes);
330 private void AddNoButton (int left)
332 Button bno = new Button ();
333 bno.Text = "No";
334 bno.Width = 100;
335 bno.Height = 30;
336 bno.Top = this.Height - 70;
337 bno.Left = left;
338 bno.Click += new EventHandler (NoClick);
339 this.Controls.Add (bno);
341 #endregion
343 #region Button click handlers
344 private void OkClick (object sender, EventArgs e)
346 this.DialogResult = DialogResult.OK;
347 this.Close ();
350 private void CancelClick (object sender, EventArgs e)
352 this.DialogResult = DialogResult.Cancel;
353 this.Close ();
356 private void AbortClick (object sender, EventArgs e)
358 this.DialogResult = DialogResult.Abort;
359 this.Close ();
362 private void RetryClick (object sender, EventArgs e)
364 this.DialogResult = DialogResult.Retry;
365 this.Close ();
368 private void IgnoreClick (object sender, EventArgs e)
370 this.DialogResult = DialogResult.Ignore;
371 this.Close ();
374 private void YesClick (object sender, EventArgs e)
376 this.DialogResult = DialogResult.Yes;
377 this.Close ();
380 private void NoClick (object sender, EventArgs e)
382 this.DialogResult = DialogResult.No;
383 this.Close ();
385 #endregion
387 #endregion // Private MessageBoxForm class
390 #region Constructors
391 private MessageBox ()
395 #endregion // Constructors
397 #region Public Static Methods
398 public static DialogResult Show (string text)
400 MessageBoxForm form = new MessageBoxForm (null, text, string.Empty,
401 MessageBoxButtons.OK, MessageBoxIcon.None);
403 return form.RunDialog ();
407 public static DialogResult Show (IWin32Window owner, string text)
409 MessageBoxForm form = new MessageBoxForm (owner, text, string.Empty,
410 MessageBoxButtons.OK, MessageBoxIcon.None);
412 return form.RunDialog ();
416 public static DialogResult Show (string text, string caption)
418 MessageBoxForm form = new MessageBoxForm (null, text, caption,
419 MessageBoxButtons.OK, MessageBoxIcon.None);
421 return form.RunDialog ();
424 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons)
426 MessageBoxForm form = new MessageBoxForm (null, text, caption,
427 buttons, MessageBoxIcon.None);
429 return form.RunDialog ();
432 public static DialogResult Show (IWin32Window owner, string text, string caption,
433 MessageBoxButtons buttons)
435 MessageBoxForm form = new MessageBoxForm (owner, text, caption,
436 buttons, MessageBoxIcon.None);
438 return form.RunDialog ();
441 public static DialogResult Show (IWin32Window owner, string text, string caption,
442 MessageBoxButtons buttons, MessageBoxIcon icon)
444 MessageBoxForm form = new MessageBoxForm (owner, text, caption,
445 buttons, icon);
447 return form.RunDialog ();
451 public static DialogResult Show (IWin32Window owner, string text, string caption)
453 MessageBoxForm form = new MessageBoxForm (owner, text, caption,
454 MessageBoxButtons.OK, MessageBoxIcon.None);
456 return form.RunDialog ();
460 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
461 MessageBoxIcon icon)
463 MessageBoxForm form = new MessageBoxForm (null, text, caption,
464 buttons, icon);
466 return form.RunDialog ();
469 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
470 MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
473 MessageBoxForm form = new MessageBoxForm (null, text, caption,
474 buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
476 return form.RunDialog ();
480 public static DialogResult Show (IWin32Window owner, string text, string caption,
481 MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
483 MessageBoxForm form = new MessageBoxForm (owner, text, caption,
484 buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
486 return form.RunDialog ();
489 public static DialogResult
490 Show (string text, string caption,
491 MessageBoxButtons buttons, MessageBoxIcon icon,
492 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
494 MessageBoxForm form = new MessageBoxForm (null, text, caption,
495 buttons, icon, defaultButton, options);
497 return form.RunDialog ();
500 public static DialogResult Show (IWin32Window owner, string text, string caption,
501 MessageBoxButtons buttons, MessageBoxIcon icon,
502 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
504 MessageBoxForm form = new MessageBoxForm (owner, text, caption,
505 buttons, icon, defaultButton, options);
507 return form.RunDialog ();
509 #endregion // Public Static Methods