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-2005 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 // - Complete the implementation when Form.BorderStyle is available.
30 // - Add support for MessageBoxOptions and MessageBoxDefaultButton.
31 // - Button size calculations assume fixed height for buttons, that could be bad
39 using System.Globalization;
40 using System.Resources;
42 namespace System.Windows.Forms
44 public class MessageBox
46 #region Private MessageBoxForm class
47 private class MessageBoxForm : Form
49 #region MessageBoxFrom Local Variables
50 internal static Image error_icon = (Image)Locale.GetResource("mbox_error.png");
51 internal static Image question_icon = (Image)Locale.GetResource("mbox_question.png");
52 internal static Image warning_icon = (Image)Locale.GetResource("mbox_warn.png");
53 internal static Image info_icon = (Image)Locale.GetResource("mbox_info.png");
54 internal static string Yes;
55 internal const int space_border = 10;
57 bool size_known = false;
58 const int space_image_text= 10;
61 MessageBoxButtons msgbox_buttons;
62 bool buttons_placed = false;
64 #endregion // MessageBoxFrom Local Variables
66 #region MessageBoxForm Constructors
67 public MessageBoxForm (IWin32Window owner, string text, string caption,
68 MessageBoxButtons buttons, MessageBoxIcon icon)
71 case MessageBoxIcon.None: {
76 case MessageBoxIcon.Error: { // Same as MessageBoxIcon.Hand and MessageBoxIcon.Stop
77 icon_image = error_icon;
81 case MessageBoxIcon.Question: {
82 icon_image = question_icon;
86 case MessageBoxIcon.Asterisk: { // Same as MessageBoxIcon.Information
87 icon_image = info_icon;
91 case MessageBoxIcon.Warning: { // Same as MessageBoxIcon.Exclamation:
92 icon_image = warning_icon;
98 msgbox_buttons = buttons;
100 this.Paint += new PaintEventHandler (MessageBoxForm_Paint);
104 public MessageBoxForm (IWin32Window owner, string text, string caption,
105 MessageBoxButtons buttons, MessageBoxIcon icon,
106 MessageBoxDefaultButton defaultButton, MessageBoxOptions options) : this (owner, text, caption, buttons, icon)
108 // Still needs to implement defaultButton and options
111 #endregion // MessageBoxForm Constructors
113 #region Protected Instance Properties
114 protected override CreateParams CreateParams {
122 cp = base.CreateParams;
124 cp.Style = (int)(WindowStyles.WS_DLGFRAME | WindowStyles.WS_POPUP | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CAPTION);
129 #endregion // Protected Instance Properties
131 #region MessageBoxForm Methods
132 public DialogResult RunDialog ()
134 this.StartPosition = FormStartPosition.CenterScreen;
136 if (size_known == false) {
142 return this.DialogResult;
146 private void MessageBoxForm_Paint (object sender, PaintEventArgs e)
148 e.Graphics.DrawString (msgbox_text, this.Font, new SolidBrush(Color.Black), textleft_up);
149 if (icon_image != null) {
150 e.Graphics.DrawImage(icon_image, new Point(space_border, space_border));
154 private void InitFormsSize ()
158 // First we have to know the size of text + image
159 Drawing.SizeF tsize = this.DeviceContext.MeasureString (msgbox_text, this.Font);
161 if (icon_image != null) {
162 tsize.Width += icon_image.Width + 10;
163 if(icon_image.Height > tsize.Height) {
164 // Place text middle-right
165 textleft_up = new Point (icon_image.Width + space_image_text + space_border, (int)((icon_image.Height/2)-(tsize.Height/2)) + space_border);
167 textleft_up = new Point (icon_image.Width + space_image_text + space_border, 2 + space_border);
169 tsize.Height = icon_image.Height;
171 tsize.Width += space_border * 2;
172 textleft_up = new Point (space_border + 12, space_border + 12);
173 tsize.Height += space_border * 2;
176 // Now we want to know the width of buttons
178 switch (msgbox_buttons) {
179 case MessageBoxButtons.OK: {
184 case MessageBoxButtons.OKCancel: {
189 case MessageBoxButtons.AbortRetryIgnore: {
194 case MessageBoxButtons.YesNoCancel: {
199 case MessageBoxButtons.YesNo: {
204 case MessageBoxButtons.RetryCancel: {
210 // Now we choose the good size for the form
211 if (tsize.ToSize ().Width > tb_width) {
212 //this.Width = tsize.ToSize().Width + 10;
213 this.ClientSize = new Size(tsize.ToSize().Width + 10 + (space_border * 2), Height = tsize.ToSize ().Height + 40 + (space_border * 2));
215 //this.Width = tb_width + 10;
216 this.ClientSize = new Size(tb_width + 10 + (space_border * 2), Height = tsize.ToSize ().Height + 40 + (space_border * 2));
219 // Now we set the left of the buttons
220 button_left = (this.ClientSize.Width / 2) - (tb_width / 2) + 5;
224 #endregion // MessageBoxForm Methods
226 #region Functions for Adding buttons
227 private void AddButtons()
229 if (!buttons_placed) {
230 switch (msgbox_buttons) {
231 case MessageBoxButtons.OK: {
232 AddOkButton (0 + button_left);
236 case MessageBoxButtons.OKCancel: {
237 AddOkButton (0 + button_left);
238 AddCancelButton (110 + button_left);
242 case MessageBoxButtons.AbortRetryIgnore: {
243 AddAbortButton (0 + button_left);
244 AddRetryButton (110 + button_left);
245 AddIgnoreButton (220 + button_left);
249 case MessageBoxButtons.YesNoCancel: {
250 AddYesButton (0 + button_left);
251 AddNoButton (110 + button_left);
252 AddCancelButton (220 + button_left);
256 case MessageBoxButtons.YesNo: {
257 AddYesButton (0 + button_left);
258 AddNoButton (110 + button_left);
262 case MessageBoxButtons.RetryCancel: {
263 AddRetryButton (0 + button_left);
264 AddCancelButton (110 + button_left);
268 buttons_placed = true;
272 private void AddOkButton (int left)
274 Button bok = new Button ();
275 bok.Text = Locale.GetText("OK");
278 bok.Top = this.ClientSize.Height - 35 - space_border;
280 bok.Click += new EventHandler (OkClick);
282 this.Controls.Add (bok);
285 private void AddCancelButton (int left)
287 Button bcan = new Button ();
288 bcan.Text = Locale.GetText("Cancel");
291 bcan.Top = this.ClientSize.Height - 35 - space_border;
293 bcan.Click += new EventHandler (CancelClick);
295 this.Controls.Add (bcan);
298 private void AddAbortButton (int left)
300 Button babort = new Button ();
301 babort.Text = Locale.GetText("Abort");
304 babort.Top = this.ClientSize.Height - 35 - space_border;
306 babort.Click += new EventHandler (AbortClick);
307 CancelButton = babort;
308 this.Controls.Add (babort);
311 private void AddRetryButton(int left)
313 Button bretry = new Button ();
314 bretry.Text = Locale.GetText("Retry");
317 bretry.Top = this.ClientSize.Height - 35 - space_border;
319 bretry.Click += new EventHandler (RetryClick);
320 AcceptButton = bretry;
321 this.Controls.Add (bretry);
324 private void AddIgnoreButton (int left)
326 Button bignore = new Button ();
327 bignore.Text = Locale.GetText("Ignore");
330 bignore.Top = this.ClientSize.Height - 35 - space_border;
332 bignore.Click += new EventHandler (IgnoreClick);
333 this.Controls.Add (bignore);
336 private void AddYesButton (int left)
338 Button byes = new Button ();
339 byes.Text = Locale.GetText("Yes");
342 byes.Top = this.ClientSize.Height - 35 - space_border;
344 byes.Click += new EventHandler (YesClick);
346 this.Controls.Add (byes);
349 private void AddNoButton (int left)
351 Button bno = new Button ();
352 bno.Text = Locale.GetText("No");
355 bno.Top = this.ClientSize.Height - 35 - space_border;
357 bno.Click += new EventHandler (NoClick);
359 this.Controls.Add (bno);
363 #region Button click handlers
364 private void OkClick (object sender, EventArgs e)
366 this.DialogResult = DialogResult.OK;
370 private void CancelClick (object sender, EventArgs e)
372 this.DialogResult = DialogResult.Cancel;
376 private void AbortClick (object sender, EventArgs e)
378 this.DialogResult = DialogResult.Abort;
382 private void RetryClick (object sender, EventArgs e)
384 this.DialogResult = DialogResult.Retry;
388 private void IgnoreClick (object sender, EventArgs e)
390 this.DialogResult = DialogResult.Ignore;
394 private void YesClick (object sender, EventArgs e)
396 this.DialogResult = DialogResult.Yes;
400 private void NoClick (object sender, EventArgs e)
402 this.DialogResult = DialogResult.No;
407 #endregion // Private MessageBoxForm class
411 private MessageBox ()
414 #endregion // Constructors
416 #region Public Static Methods
417 public static DialogResult Show (string text)
419 MessageBoxForm form = new MessageBoxForm (null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None);
421 return form.RunDialog ();
425 public static DialogResult Show (IWin32Window owner, string text)
427 MessageBoxForm form = new MessageBoxForm (owner, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None);
429 return form.RunDialog ();
433 public static DialogResult Show (string text, string caption)
435 MessageBoxForm form = new MessageBoxForm (null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None);
437 return form.RunDialog ();
440 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons)
442 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, MessageBoxIcon.None);
444 return form.RunDialog ();
447 public static DialogResult Show (IWin32Window owner, string text, string caption,
448 MessageBoxButtons buttons)
450 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons, MessageBoxIcon.None);
452 return form.RunDialog ();
455 public static DialogResult Show (IWin32Window owner, string text, string caption,
456 MessageBoxButtons buttons, MessageBoxIcon icon)
458 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons, icon);
460 return form.RunDialog ();
464 public static DialogResult Show (IWin32Window owner, string text, string caption)
466 MessageBoxForm form = new MessageBoxForm (owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None);
468 return form.RunDialog ();
472 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
475 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, icon);
477 return form.RunDialog ();
480 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
481 MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
484 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
486 return form.RunDialog ();
490 public static DialogResult Show (IWin32Window owner, string text, string caption,
491 MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
493 MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
495 return form.RunDialog ();
498 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
499 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
501 MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, icon, defaultButton, options);
503 return form.RunDialog ();
506 public static DialogResult Show (IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
507 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
509 MessageBoxForm form = new MessageBoxForm (owner, text, caption,
510 buttons, icon, defaultButton, options);
512 return form.RunDialog ();
514 #endregion // Public Static Methods