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:
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-2006 Novell, Inc.
23 // Jordi Mas i Hernandez (jordi@ximian.com)
24 // Benjamin Dasnois (benjamin.dasnois@gmail.com)
25 // Robert Thompson (rmt@corporatism.org)
26 // Peter Bartok (pbartok@novell.com)
29 // - Add support for MessageBoxOptions and MessageBoxDefaultButton.
37 using System.Globalization;
38 using System.Resources;
40 namespace System.Windows.Forms
42 public class MessageBox
44 #region Private MessageBoxForm class
45 #if NET_2_0 // UIA Framework
46 internal class MessageBoxForm : Form
48 private class MessageBoxForm : Form
51 #region MessageBoxFrom Local Variables
52 const int space_border = 10;
53 const int button_width = 86;
54 const int button_height = 23;
55 const int button_space = 5;
56 const int space_image_text= 10;
59 bool size_known = false;
62 MessageBoxButtons msgbox_buttons;
63 MessageBoxDefaultButton msgbox_default;
64 bool buttons_placed = false;
66 Button[] buttons = new Button[4];
68 string help_file_path;
70 HelpNavigator help_navigator;
73 #endregion // MessageBoxFrom Local Variables
75 #region MessageBoxForm Constructors
76 public MessageBoxForm (IWin32Window owner, string text, string caption,
77 MessageBoxButtons buttons, MessageBoxIcon icon,
78 bool displayHelpButton)
80 show_help = displayHelpButton;
83 case MessageBoxIcon.None: {
85 alert_type = AlertType.Default;
89 case MessageBoxIcon.Error: { // Same as MessageBoxIcon.Hand and MessageBoxIcon.Stop
90 icon_image = SystemIcons.Error;
91 alert_type = AlertType.Error;
95 case MessageBoxIcon.Question: {
96 icon_image = SystemIcons.Question;
97 alert_type = AlertType.Question;
101 case MessageBoxIcon.Asterisk: { // Same as MessageBoxIcon.Information
102 icon_image = SystemIcons.Information;
103 alert_type = AlertType.Information;
107 case MessageBoxIcon.Warning: { // Same as MessageBoxIcon.Exclamation:
108 icon_image = SystemIcons.Warning;
109 alert_type = AlertType.Warning;
115 msgbox_buttons = buttons;
116 msgbox_default = MessageBoxDefaultButton.Button1;
119 Owner = Control.FromHandle(owner.Handle).FindForm();
121 if (Application.MWFThread.Current.Context != null) {
122 Owner = Application.MWFThread.Current.Context.MainForm;
126 this.ControlBox = true;
127 this.MinimizeBox = false;
128 this.MaximizeBox = false;
129 this.ShowInTaskbar = (Owner == null);
130 this.FormBorderStyle = FormBorderStyle.FixedDialog;
133 public MessageBoxForm (IWin32Window owner, string text, string caption,
134 MessageBoxButtons buttons, MessageBoxIcon icon,
135 MessageBoxDefaultButton defaultButton, MessageBoxOptions options, bool displayHelpButton)
136 : this (owner, text, caption, buttons, icon, displayHelpButton)
138 msgbox_default = defaultButton;
141 public MessageBoxForm (IWin32Window owner, string text, string caption,
142 MessageBoxButtons buttons, MessageBoxIcon icon)
143 : this (owner, text, caption, buttons, icon, false)
146 #endregion // MessageBoxForm Constructors
148 #region Protected Instance Properties
149 protected override CreateParams CreateParams {
151 CreateParams cp = base.CreateParams;;
153 cp.Style |= (int)(WindowStyles.WS_DLGFRAME | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CAPTION);
156 cp.Style |= (int)(WindowStyles.WS_DISABLED);
161 #endregion // Protected Instance Properties
163 #region MessageBoxForm Methods
164 public void SetHelpData (string file_path, string keyword, HelpNavigator navigator, object param)
166 help_file_path = file_path;
167 help_keyword = keyword;
168 help_navigator = navigator;
172 internal string HelpFilePath {
173 get { return help_file_path; }
176 internal string HelpKeyword {
177 get { return help_keyword; }
180 internal HelpNavigator HelpNavigator {
181 get { return help_navigator; }
184 internal object HelpParam {
185 get { return help_param; }
188 public DialogResult RunDialog ()
190 this.StartPosition = FormStartPosition.CenterScreen;
192 if (size_known == false) {
197 TopMost = Owner.TopMost;
199 XplatUI.AudibleAlert (alert_type);
202 return this.DialogResult;
205 internal override void OnPaintInternal (PaintEventArgs e)
207 e.Graphics.DrawString (msgbox_text, this.Font, ThemeEngine.Current.ResPool.GetSolidBrush (Color.Black), text_rect);
208 if (icon_image != null) {
209 e.Graphics.DrawIcon(icon_image, space_border, space_border);
213 private void InitFormsSize ()
217 // Max width of messagebox must be 60% of screen width
218 int max_width = (int) (Screen.GetWorkingArea (this).Width * 0.6);
220 // First we have to know the size of text + image
221 Drawing.SizeF tsize = TextRenderer.MeasureString (msgbox_text, this.Font, max_width);
222 text_rect.Size = tsize;
224 if (icon_image != null) {
225 tsize.Width += icon_image.Width + 10;
226 if(icon_image.Height > tsize.Height) {
227 // Place text middle-right
228 text_rect.Location = new Point (icon_image.Width + space_image_text + space_border, (int)((icon_image.Height/2)-(tsize.Height/2)) + space_border);
230 text_rect.Location = new Point (icon_image.Width + space_image_text + space_border, 2 + space_border);
232 if (tsize.Height < icon_image.Height)
233 tsize.Height = icon_image.Height;
235 text_rect.Location = new Point (space_border + button_space, space_border);
237 tsize.Height += space_border * 2;
239 // Now we want to know the amount of buttons
241 switch (msgbox_buttons) {
242 case MessageBoxButtons.OK:
246 case MessageBoxButtons.OKCancel:
250 case MessageBoxButtons.AbortRetryIgnore:
254 case MessageBoxButtons.YesNoCancel:
258 case MessageBoxButtons.YesNo:
262 case MessageBoxButtons.RetryCancel:
274 // Calculate the width based on amount of buttons
275 tb_width = (button_width + button_space) * buttoncount;
277 // The form caption can also make us bigger
278 SizeF caption = TextRenderer.MeasureString (Text, new Font (DefaultFont, FontStyle.Bold));
280 // Use the bigger of the caption size (plus some arbitrary borders/close button)
281 // or the text size, up to 60% of the screen (max_size)
282 Size new_size = new SizeF (Math.Min (Math.Max (caption.Width + 40, tsize.Width), max_width), tsize.Height).ToSize ();
284 // Now we choose the good size for the form
285 if (new_size.Width > tb_width)
286 this.ClientSize = new Size (new_size.Width + (space_border * 2), Height = new_size.Height + (space_border * 4));
288 this.ClientSize = new Size (tb_width + (space_border * 2), Height = new_size.Height + (space_border * 4));
290 // Now we set the left of the buttons
291 button_left = (this.ClientSize.Width / 2) - (tb_width / 2) + 5;
295 // Still needs to implement defaultButton and options
296 switch(msgbox_default) {
297 case MessageBoxDefaultButton.Button2: {
298 if (this.buttons[1] != null) {
299 ActiveControl = this.buttons[1];
304 case MessageBoxDefaultButton.Button3: {
305 if (this.buttons[2] != null) {
306 ActiveControl = this.buttons[2];
313 protected override bool ProcessDialogKey(Keys keyData) {
314 if (keyData == Keys.Escape) {
315 this.CancelClick(this, null);
319 if (((keyData & Keys.Modifiers) == Keys.Control) &&
320 (((keyData & Keys.KeyCode) == Keys.C) ||
321 ((keyData & Keys.KeyCode) == Keys.Insert))) {
325 return base.ProcessDialogKey (keyData);
328 protected override bool ProcessDialogChar (char charCode)
330 // Shortcut keys, kinda like mnemonics, except you don't have to press Alt
331 if ((charCode == 'N' || charCode == 'n') && (CancelButton != null && (CancelButton as Button).Text == "No"))
332 CancelButton.PerformClick ();
333 else if ((charCode == 'Y' || charCode == 'y') && (AcceptButton as Button).Text == "Yes")
334 AcceptButton.PerformClick ();
335 else if ((charCode == 'A' || charCode == 'a') && (CancelButton != null && (CancelButton as Button).Text == "Abort"))
336 CancelButton.PerformClick ();
337 else if ((charCode == 'R' || charCode == 'r') && (AcceptButton as Button).Text == "Retry")
338 AcceptButton.PerformClick ();
339 else if ((charCode == 'I' || charCode == 'i') && buttons.Length >= 3 && buttons[2].Text == "Ignore")
340 buttons[2].PerformClick ();
342 return base.ProcessDialogChar (charCode);
347 string separator = "---------------------------" + Environment.NewLine;
349 System.Text.StringBuilder contents = new System.Text.StringBuilder ();
351 contents.Append (separator);
352 contents.Append (this.Text).Append (Environment.NewLine);
353 contents.Append (separator);
354 contents.Append (msgbox_text).Append (Environment.NewLine);
355 contents.Append (separator);
357 foreach (Button btn in buttons) {
360 contents.Append (btn.Text).Append (" ");;
363 contents.Append (Environment.NewLine);
364 contents.Append (separator);
366 DataObject obj = new DataObject(DataFormats.Text, contents.ToString());
367 Clipboard.SetDataObject (obj);
370 #endregion // MessageBoxForm Methods
372 #region Functions for Adding buttons
373 private void AddButtons()
375 if (!buttons_placed) {
376 switch (msgbox_buttons) {
377 case MessageBoxButtons.OK: {
378 buttons[0] = AddOkButton (0);
382 case MessageBoxButtons.OKCancel: {
383 buttons[0] = AddOkButton (0);
384 buttons[1] = AddCancelButton (1);
388 case MessageBoxButtons.AbortRetryIgnore: {
389 buttons[0] = AddAbortButton (0);
390 buttons[1] = AddRetryButton (1);
391 buttons[2] = AddIgnoreButton (2);
395 case MessageBoxButtons.YesNoCancel: {
396 buttons[0] = AddYesButton (0);
397 buttons[1] = AddNoButton (1);
398 buttons[2] = AddCancelButton (2);
402 case MessageBoxButtons.YesNo: {
403 buttons[0] = AddYesButton (0);
404 buttons[1] = AddNoButton (1);
408 case MessageBoxButtons.RetryCancel: {
409 buttons[0] = AddRetryButton (0);
410 buttons[1] = AddCancelButton (1);
417 for (int i = 0; i <= 3; i++) {
418 if (buttons [i] == null) {
425 buttons_placed = true;
429 private Button AddButton (string text, int left, EventHandler click_event)
431 Button button = new Button ();
432 button.Text = Locale.GetText(text);
433 button.Width = button_width;
434 button.Height = button_height;
435 button.Top = this.ClientSize.Height - button.Height - space_border;
436 button.Left = ((button_width + button_space) * left) + button_left;
438 if (click_event != null)
439 button.Click += click_event;
441 if ((text == "OK") || (text == "Retry") || (text == "Yes"))
442 AcceptButton = button;
443 else if ((text == "Cancel") || (text == "Abort") || (text == "No"))
444 CancelButton = button;
446 this.Controls.Add (button);
451 private Button AddOkButton (int left)
453 return AddButton ("OK", left, new EventHandler (OkClick));
456 private Button AddCancelButton (int left)
458 return AddButton ("Cancel", left, new EventHandler (CancelClick));
461 private Button AddAbortButton (int left)
463 return AddButton ("Abort", left, new EventHandler (AbortClick));
466 private Button AddRetryButton(int left)
468 return AddButton ("Retry", left, new EventHandler (RetryClick));
471 private Button AddIgnoreButton (int left)
473 return AddButton ("Ignore", left, new EventHandler (IgnoreClick));
476 private Button AddYesButton (int left)
478 return AddButton ("Yes", left, new EventHandler (YesClick));
481 private Button AddNoButton (int left)
483 return AddButton ("No", left, new EventHandler (NoClick));
487 private Button AddHelpButton (int left)
489 Button button = AddButton ("Help", left, null);
490 button.Click += delegate { Owner.RaiseHelpRequested (new HelpEventArgs (Owner.Location)); };
496 #region Button click handlers
497 private void OkClick (object sender, EventArgs e)
499 this.DialogResult = DialogResult.OK;
503 private void CancelClick (object sender, EventArgs e)
505 this.DialogResult = DialogResult.Cancel;
509 private void AbortClick (object sender, EventArgs e)
511 this.DialogResult = DialogResult.Abort;
515 private void RetryClick (object sender, EventArgs e)
517 this.DialogResult = DialogResult.Retry;
521 private void IgnoreClick (object sender, EventArgs e)
523 this.DialogResult = DialogResult.Ignore;
527 private void YesClick (object sender, EventArgs e)
529 this.DialogResult = DialogResult.Yes;
533 private void NoClick (object sender, EventArgs e)
535 this.DialogResult = DialogResult.No;
542 #region UIA Framework: Methods, Properties and Events
544 internal string UIAMessage {
545 get { return msgbox_text; }
548 internal Rectangle UIAMessageRectangle {
550 return new Rectangle ((int) text_rect.X,
552 (int) text_rect.Width,
553 (int) text_rect.Height);
557 internal Rectangle UIAIconRectangle {
559 return new Rectangle (space_border,
561 icon_image == null ? -1 : icon_image.Width,
562 icon_image == null ? -1 : icon_image.Height);
570 #endregion // Private MessageBoxForm class
574 private MessageBox ()
577 #endregion // Constructors
579 #region Public Static Methods
580 public static DialogResult Show (string text)
582 MessageBoxForm form = new MessageBoxForm (null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None);
584 return form.RunDialog ();
587 public static DialogResult Show (IWin32Window owner, string text)
589 MessageBoxForm form = new MessageBoxForm (owner, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None);
591 return form.RunDialog ();
594 public static DialogResult Show (string text, string caption)
596 MessageBoxForm form = new MessageBoxForm (null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None);
598 return form.RunDialog ();
601 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons)
603 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, MessageBoxIcon.None);
605 return form.RunDialog ();
608 public static DialogResult Show (IWin32Window owner, string text, string caption,
609 MessageBoxButtons buttons)
611 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons, MessageBoxIcon.None);
613 return form.RunDialog ();
616 public static DialogResult Show (IWin32Window owner, string text, string caption,
617 MessageBoxButtons buttons, MessageBoxIcon icon)
619 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons, icon);
621 return form.RunDialog ();
625 public static DialogResult Show (IWin32Window owner, string text, string caption)
627 MessageBoxForm form = new MessageBoxForm (owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None);
629 return form.RunDialog ();
633 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
636 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, icon);
638 return form.RunDialog ();
641 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
642 MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
645 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons,
646 icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly, false);
648 return form.RunDialog ();
651 public static DialogResult Show (IWin32Window owner, string text, string caption,
652 MessageBoxButtons buttons, MessageBoxIcon icon,
653 MessageBoxDefaultButton defaultButton)
655 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons,
656 icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly, false);
658 return form.RunDialog ();
661 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
662 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
664 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons,
665 icon, defaultButton, options, false);
667 return form.RunDialog ();
670 public static DialogResult Show (IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
671 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
673 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons,
674 icon, defaultButton, options, false);
676 return form.RunDialog ();
678 #endregion // Public Static Methods
681 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
682 MessageBoxDefaultButton defaultButton, MessageBoxOptions options,
683 bool displayHelpButton)
685 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons,
686 icon, defaultButton, options, displayHelpButton);
687 return form.RunDialog ();
690 [MonoTODO ("Help is not implemented")]
691 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
692 MessageBoxDefaultButton defaultButton, MessageBoxOptions options,
695 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons,
696 icon, defaultButton, options, true);
697 form.SetHelpData (helpFilePath, null, HelpNavigator.TableOfContents, null);
698 return form.RunDialog ();
701 [MonoTODO ("Help is not implemented")]
702 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
703 MessageBoxDefaultButton defaultButton, MessageBoxOptions options,
704 string helpFilePath, string keyword)
706 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons,
707 icon, defaultButton, options, true);
708 form.SetHelpData (helpFilePath, keyword, HelpNavigator.TableOfContents, null);
709 return form.RunDialog ();
712 [MonoTODO ("Help is not implemented")]
713 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
714 MessageBoxDefaultButton defaultButton, MessageBoxOptions options,
715 string helpFilePath, HelpNavigator navigator)
717 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons,
718 icon, defaultButton, options, true);
719 form.SetHelpData (helpFilePath, null, navigator, null);
720 return form.RunDialog ();
723 [MonoTODO ("Help is not implemented")]
724 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
725 MessageBoxDefaultButton defaultButton, MessageBoxOptions options,
726 string helpFilePath, HelpNavigator navigator, object param)
728 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons,
729 icon, defaultButton, options, true);
730 form.SetHelpData (helpFilePath, null, navigator, param);
731 return form.RunDialog ();
734 [MonoTODO ("Help is not implemented")]
735 public static DialogResult Show (IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
736 MessageBoxDefaultButton defaultButton, MessageBoxOptions options,
739 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons,
740 icon, defaultButton, options, true);
741 form.SetHelpData (helpFilePath, null, HelpNavigator.TableOfContents, null);
742 return form.RunDialog ();
745 [MonoTODO ("Help is not implemented")]
746 public static DialogResult Show (IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
747 MessageBoxDefaultButton defaultButton, MessageBoxOptions options,
748 string helpFilePath, string keyword)
750 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons,
751 icon, defaultButton, options, true);
752 form.SetHelpData (helpFilePath, keyword, HelpNavigator.TableOfContents, null);
753 return form.RunDialog ();
756 [MonoTODO ("Help is not implemented")]
757 public static DialogResult Show (IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
758 MessageBoxDefaultButton defaultButton, MessageBoxOptions options,
759 string helpFilePath, HelpNavigator navigator)
761 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons,
762 icon, defaultButton, options, true);
763 form.SetHelpData (helpFilePath, null, navigator, null);
764 return form.RunDialog ();
767 [MonoTODO ("Help is not implemented")]
768 public static DialogResult Show (IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
769 MessageBoxDefaultButton defaultButton, MessageBoxOptions options,
770 string helpFilePath, HelpNavigator navigator, object param)
772 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons,
773 icon, defaultButton, options, true);
774 form.SetHelpData (helpFilePath, null, navigator, param);
775 return form.RunDialog ();