* TreeView.cs: Don't draw the selected node when we lose
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / MessageBox.cs
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-2005 Novell, Inc.
21 //
22 // Authors:
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)
27 //
28 // TODO:
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
32 //
33
34
35 // NOT COMPLETE
36
37 using System;
38 using System.Drawing;
39 using System.Globalization;
40 using System.Resources;
41
42 namespace System.Windows.Forms
43 {
44         public class MessageBox
45         {
46                 #region Private MessageBoxForm class
47                 private class MessageBoxForm : Form
48                 {
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;
56                         string                  msgbox_text;
57                         bool                    size_known      = false;
58                         const int               space_image_text= 10;
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
66                         #region MessageBoxForm Constructors
67                         public MessageBoxForm (IWin32Window owner, string text, string caption,
68                                         MessageBoxButtons buttons, MessageBoxIcon icon) 
69                         {
70                                 switch (icon) {
71                                         case MessageBoxIcon.None: {
72                                                 icon_image = null;
73                                                 break;
74                                         }
75
76                                         case MessageBoxIcon.Error: {            // Same as MessageBoxIcon.Hand and MessageBoxIcon.Stop
77                                                 icon_image = error_icon;
78                                                 break;
79                                         }
80
81                                         case MessageBoxIcon.Question: {
82                                                 icon_image = question_icon;
83                                                 break;
84                                         }
85
86                                         case MessageBoxIcon.Asterisk: {         // Same as MessageBoxIcon.Information
87                                                 icon_image = info_icon;
88                                                 break;
89                                         }
90
91                                         case MessageBoxIcon.Warning: {          // Same as MessageBoxIcon.Exclamation:
92                                                 icon_image = warning_icon;
93                                                 break;
94                                         }
95                                 }
96
97                                 msgbox_text = text;
98                                 msgbox_buttons = buttons;
99                                 this.Text = caption;
100                                 this.Paint += new PaintEventHandler (MessageBoxForm_Paint);
101                         }
102
103                         [MonoTODO]
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)
107                         {
108                                 // Still needs to implement defaultButton and options
109
110                         }
111                         #endregion      // MessageBoxForm Constructors
112
113                         #region Protected Instance Properties
114                         protected override CreateParams CreateParams {
115                                 get {
116                                         CreateParams    cp;
117
118                                         ControlBox = true;
119                                         MinimizeBox = false;
120                                         MaximizeBox = false;
121
122                                         cp = base.CreateParams;
123
124                                         cp.Style = (int)(WindowStyles.WS_DLGFRAME | WindowStyles.WS_POPUP | WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CAPTION);
125
126                                         return cp;
127                                 }
128                         }
129                         #endregion      // Protected Instance Properties
130
131                         #region MessageBoxForm Methods
132                         public DialogResult RunDialog ()
133                         {
134                                 this.StartPosition = FormStartPosition.CenterScreen;
135
136                                 if (size_known == false) {
137                                         InitFormsSize ();
138                                 }
139
140                                 this.ShowDialog ();
141
142                                 return this.DialogResult;
143
144                         }
145
146                         private void MessageBoxForm_Paint (object sender, PaintEventArgs e)
147                         {
148                                 e.Graphics.DrawString (msgbox_text, this.Font, ThemeEngine.Current.ResPool.GetSolidBrush (Color.Black), textleft_up);
149                                 if (icon_image != null) {
150                                         e.Graphics.DrawImage(icon_image, new Point(space_border, space_border));
151                                 }
152                         }
153
154                         private void InitFormsSize ()
155                         {
156                                 int tb_width = 0;
157                 
158                                 // First we have to know the size of text + image
159                                 Drawing.SizeF tsize = DeviceContext.MeasureString (msgbox_text, this.Font);
160
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);
166                                         } else {
167                                                 textleft_up = new Point (icon_image.Width + space_image_text + space_border, 2 + space_border);
168                                         }
169                                         tsize.Height = icon_image.Height;
170                                 } else {
171                                         tsize.Width += space_border * 2;
172                                         textleft_up = new Point (space_border + 12, space_border + 12);
173                                         tsize.Height += space_border * 2;
174                                 }
175
176                                 // Now we want to know the width of buttons
177                 
178                                 switch (msgbox_buttons) {
179                                         case MessageBoxButtons.OK: {
180                                                 tb_width = 110 * 1;
181                                                 break;
182                                         }
183
184                                         case MessageBoxButtons.OKCancel: {
185                                                 tb_width = 110 * 2;
186                                                 break;
187                                         }
188
189                                         case MessageBoxButtons.AbortRetryIgnore: {
190                                                 tb_width = 110 * 3;
191                                                 break;
192                                         }
193
194                                         case MessageBoxButtons.YesNoCancel: {
195                                                 tb_width = 110 * 3;
196                                                 break;
197                                         }
198
199                                         case MessageBoxButtons.YesNo: {
200                                                 tb_width = 110 * 2;
201                                                 break;
202                                         }
203
204                                         case MessageBoxButtons.RetryCancel: {
205                                                 tb_width = 110 * 2;
206                                                 break;
207                                         }
208                                 }
209
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));
214                                 } else {
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));
217                                 }
218
219                                 // Now we set the left of the buttons
220                                 button_left = (this.ClientSize.Width / 2) - (tb_width / 2) + 5;
221                                 AddButtons ();
222                                 size_known = true;
223                         }
224                         #endregion      // MessageBoxForm Methods
225
226                         #region Functions for Adding buttons
227                         private void AddButtons()
228                         {
229                                 if (!buttons_placed) {
230                                         switch (msgbox_buttons) {
231                                                 case MessageBoxButtons.OK: {
232                                                         AddOkButton (0 + button_left);
233                                                         break;
234                                                 }
235
236                                                 case MessageBoxButtons.OKCancel: {
237                                                         AddOkButton (0 + button_left);
238                                                         AddCancelButton (110 + button_left);
239                                                         break;
240                                                 }
241
242                                                 case MessageBoxButtons.AbortRetryIgnore: {
243                                                         AddAbortButton (0 + button_left);
244                                                         AddRetryButton (110 + button_left);
245                                                         AddIgnoreButton (220 + button_left);
246                                                         break;
247                                                 }
248
249                                                 case MessageBoxButtons.YesNoCancel: {
250                                                         AddYesButton (0 + button_left);
251                                                         AddNoButton (110 + button_left);
252                                                         AddCancelButton (220 + button_left);
253                                                         break;
254                                                 }
255
256                                                 case MessageBoxButtons.YesNo: {
257                                                         AddYesButton (0 + button_left);
258                                                         AddNoButton (110 + button_left);
259                                                         break;
260                                                 }
261
262                                                 case MessageBoxButtons.RetryCancel: {
263                                                         AddRetryButton (0 + button_left);
264                                                         AddCancelButton (110 + button_left);
265                                                         break;
266                                                 }
267                                         }
268                                         buttons_placed = true;
269                                 }
270                         }
271
272                         private void AddOkButton (int left)
273                         {
274                                 Button bok = new Button ();
275                                 bok.Text = Locale.GetText("OK");
276                                 bok.Width = 100;
277                                 bok.Height = 30;
278                                 bok.Top = this.ClientSize.Height - 35 - space_border;
279                                 bok.Left = left;
280                                 bok.Click += new EventHandler (OkClick);
281                                 AcceptButton = bok;
282                                 this.Controls.Add (bok);
283                         }
284
285                         private void AddCancelButton (int left)
286                         {
287                                 Button bcan = new Button ();
288                                 bcan.Text = Locale.GetText("Cancel");
289                                 bcan.Width = 100;
290                                 bcan.Height = 30;
291                                 bcan.Top = this.ClientSize.Height - 35 - space_border;
292                                 bcan.Left = left;
293                                 bcan.Click += new EventHandler (CancelClick);
294                                 CancelButton = bcan;
295                                 this.Controls.Add (bcan);
296                         }
297
298                         private void AddAbortButton (int left)
299                         {
300                                 Button babort = new Button ();
301                                 babort.Text = Locale.GetText("Abort");
302                                 babort.Width = 100;
303                                 babort.Height = 30;
304                                 babort.Top = this.ClientSize.Height - 35 - space_border;
305                                 babort.Left = left;
306                                 babort.Click += new EventHandler (AbortClick);
307                                 CancelButton = babort;
308                                 this.Controls.Add (babort);
309                         }
310
311                         private void AddRetryButton(int left)
312                         {
313                                 Button bretry = new Button ();
314                                 bretry.Text = Locale.GetText("Retry");
315                                 bretry.Width = 100;
316                                 bretry.Height = 30;
317                                 bretry.Top = this.ClientSize.Height - 35 - space_border;
318                                 bretry.Left = left;
319                                 bretry.Click += new EventHandler (RetryClick);
320                                 AcceptButton = bretry;
321                                 this.Controls.Add (bretry);
322                         }
323
324                         private void AddIgnoreButton (int left)
325                         {
326                                 Button bignore = new Button ();
327                                 bignore.Text = Locale.GetText("Ignore");
328                                 bignore.Width = 100;
329                                 bignore.Height = 30;
330                                 bignore.Top = this.ClientSize.Height - 35 - space_border;
331                                 bignore.Left = left;
332                                 bignore.Click += new EventHandler (IgnoreClick);
333                                 this.Controls.Add (bignore);
334                         }
335
336                         private void AddYesButton (int left)
337                         {
338                                 Button byes = new Button ();
339                                 byes.Text = Locale.GetText("Yes");
340                                 byes.Width = 100;
341                                 byes.Height = 30;
342                                 byes.Top = this.ClientSize.Height - 35 - space_border;
343                                 byes.Left = left;
344                                 byes.Click += new EventHandler (YesClick);
345                                 AcceptButton = byes;
346                                 this.Controls.Add (byes);
347                         }
348
349                         private void AddNoButton (int left)
350                         {
351                                 Button bno = new Button ();
352                                 bno.Text = Locale.GetText("No");
353                                 bno.Width = 100;
354                                 bno.Height = 30;
355                                 bno.Top = this.ClientSize.Height - 35 - space_border;
356                                 bno.Left = left;
357                                 bno.Click += new EventHandler (NoClick);
358                                 CancelButton = bno;
359                                 this.Controls.Add (bno);
360                         }
361                         #endregion
362
363                         #region Button click handlers
364                         private void OkClick (object sender, EventArgs e)
365                         {
366                                 this.DialogResult = DialogResult.OK;
367                                 this.Close ();
368                         }
369
370                         private void CancelClick (object sender, EventArgs e)
371                         {
372                                 this.DialogResult = DialogResult.Cancel;
373                                 this.Close ();
374                         }
375
376                         private void AbortClick (object sender, EventArgs e)
377                         {
378                                 this.DialogResult = DialogResult.Abort;
379                                 this.Close ();
380                         }
381
382                         private void RetryClick (object sender, EventArgs e)
383                         {
384                                 this.DialogResult = DialogResult.Retry;
385                                 this.Close ();
386                         }
387
388                         private void IgnoreClick (object sender, EventArgs e)
389                         {
390                                 this.DialogResult = DialogResult.Ignore;
391                                 this.Close ();
392                         }
393
394                         private void YesClick (object sender, EventArgs e)
395                         {
396                                 this.DialogResult = DialogResult.Yes;
397                                 this.Close ();
398                         }
399
400                         private void NoClick (object sender, EventArgs e)
401                         {
402                                 this.DialogResult = DialogResult.No;
403                                 this.Close ();
404                         }
405                         #endregion
406                 }
407                 #endregion      // Private MessageBoxForm class
408
409
410                 #region Constructors
411                 private MessageBox ()
412                 {
413                 }
414                 #endregion      // Constructors
415
416                 #region Public Static Methods
417                 public static DialogResult Show (string text)
418                 {
419                         MessageBoxForm form = new MessageBoxForm (null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None);
420
421                         return form.RunDialog ();
422
423                 }
424
425                 public static DialogResult Show (IWin32Window owner, string text)
426                 {
427                         MessageBoxForm form = new MessageBoxForm (owner, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None);
428                                 
429                         return form.RunDialog ();
430
431                 }
432
433                 public static DialogResult Show (string text, string caption)
434                 {
435                         MessageBoxForm form = new MessageBoxForm (null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None);
436
437                         return form.RunDialog ();
438                 }
439
440                 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons)
441                 {
442                         MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, MessageBoxIcon.None);
443                                 
444                         return form.RunDialog ();
445                 }
446
447                 public static DialogResult Show (IWin32Window owner, string text, string caption,
448                                 MessageBoxButtons buttons)
449                 {
450                         MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons, MessageBoxIcon.None);
451                                 
452                         return form.RunDialog ();
453                 }
454
455                 public static DialogResult Show (IWin32Window owner, string text, string caption,
456                                 MessageBoxButtons buttons, MessageBoxIcon icon)
457                 {
458                         MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons, icon);
459                                 
460                         return form.RunDialog ();
461                 }
462
463
464                 public static DialogResult Show (IWin32Window owner, string text, string caption)
465                 {
466                         MessageBoxForm form = new MessageBoxForm (owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None);
467                                 
468                         return form.RunDialog ();
469                 }
470
471
472                 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
473                                 MessageBoxIcon icon)
474                 {
475                         MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, icon);
476                                 
477                         return form.RunDialog ();
478                 }
479
480                 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
481                                 MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
482                 {
483
484                         MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
485                                 
486                         return form.RunDialog ();
487
488                 }
489
490                 public static DialogResult Show (IWin32Window owner, string text, string caption,
491                                 MessageBoxButtons buttons, MessageBoxIcon icon,  MessageBoxDefaultButton defaultButton)
492                 {
493                         MessageBoxForm form = new MessageBoxForm (owner, text, caption, buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
494                                 
495                         return form.RunDialog ();
496                 }
497
498                 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
499                                                 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
500                 {
501                         MessageBoxForm form = new MessageBoxForm (null, text, caption, buttons, icon, defaultButton, options);
502                                 
503                         return form.RunDialog ();
504                 }
505
506                 public static DialogResult Show (IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon,
507                                                 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
508                 {
509                         MessageBoxForm form = new MessageBoxForm (owner, text, caption,
510                                         buttons, icon, defaultButton, options);
511                                 
512                         return form.RunDialog ();
513                 }
514                 #endregion      // Public Static Methods
515         }
516 }
517