* X11Keyboard.cs: Detect and use the num lock mask.
[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 Novell, Inc.
21 //
22 // Authors:
23 //      Jordi Mas i Hernandez, jordi@ximian.com
24 //      Benjamin Dasnois, benjamin.dasnois@gmail.com
25 //
26 // TODO:
27 //      - Complete the implementation when icons are available, Form.BorderStyle is available.
28 //      - Add support for MessageBoxOptions and MessageBoxDefaultButton.
29 //
30 //
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.
37 //
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.
40 //
41 // Revision 1.2  2004/10/05 17:10:57  pbartok
42 // - Partial implementation by Benjamin Dasnois
43 //
44 // Revision 1.1  2004/07/26 11:41:35  jordi
45 // initial messagebox implementation
46 //
47 //
48
49 // INCOMPLETE
50
51 using System;
52 using System.Drawing;
53
54 namespace System.Windows.Forms
55 {
56         public class MessageBox
57         {
58                 #region Private MessageBoxForm class
59                 private class MessageBoxForm : Form
60                 {
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
72                         #region MessageBoxForm Constructors
73                         public MessageBoxForm (IWin32Window owner, string text, string caption,
74                                         MessageBoxButtons buttons, MessageBoxIcon icon) 
75                         {
76                                 switch (icon) {
77                                         case MessageBoxIcon.None: {
78                                                 icon_image = null;
79                                                 break;
80                                         }
81
82                                         case MessageBoxIcon.Error: {
83                                                 break;
84                                         }
85
86                                         case MessageBoxIcon.Question: {
87                                                 break;
88                                         }
89
90                                         case MessageBoxIcon.Exclamation: {
91                                                 break;
92                                         }
93
94                                         case MessageBoxIcon.Asterisk: {
95                                                 break;
96                                         }
97                                 }
98
99                                 msgbox_text = text;
100                                 msgbox_buttons = buttons;
101                                 this.Text = caption;
102                                 this.Paint += new PaintEventHandler (MessageBoxForm_Paint);
103                         }
104
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)
109                         {
110                                 // Still needs to implement defaultButton and options
111
112                         }
113                         #endregion      // MessageBoxForm Constructors
114
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;
123
124                                         return create_params;
125                                 }
126                         }
127                         #endregion      // Protected Instance Properties
128
129                         #region MessageBoxForm Methods
130                         public DialogResult RunDialog ()
131                         {
132                                 this.StartPosition = FormStartPosition.CenterScreen;
133
134                                 if (size_known == false) {
135                                         InitFormsSize ();
136                                 }
137
138                                 this.ShowDialog ();
139
140                                 return this.DialogResult;
141
142                         }
143
144                         private void MessageBoxForm_Paint (object sender, PaintEventArgs e)
145                         {
146                                 e.Graphics.DrawString (msgbox_text, this.Font, new SolidBrush(Color.Black), textleft_up);
147                         }
148
149                         private void InitFormsSize ()
150                         {
151                                 int tb_width = 0;
152                 
153                                 // First we have to know the size of text + image
154                                 Drawing.SizeF tsize = this.DeviceContext.MeasureString (msgbox_text, this.Font);
155
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);
161                                 }
162
163                                 // Now we want to know the width of buttons
164                 
165                                 switch (msgbox_buttons) {
166                                         case MessageBoxButtons.OK: {
167                                                 tb_width = 110 * 1;
168                                                 break;
169                                         }
170
171                                         case MessageBoxButtons.OKCancel: {
172                                                 tb_width = 110 * 2;
173                                                 break;
174                                         }
175
176                                         case MessageBoxButtons.AbortRetryIgnore: {
177                                                 tb_width = 110 * 3;
178                                                 break;
179                                         }
180
181                                         case MessageBoxButtons.YesNoCancel: {
182                                                 tb_width = 110 * 3;
183                                                 break;
184                                         }
185
186                                         case MessageBoxButtons.YesNo: {
187                                                 tb_width = 110 * 2;
188                                                 break;
189                                         }
190
191                                         case MessageBoxButtons.RetryCancel: {
192                                                 tb_width = 110 * 2;
193                                                 break;
194                                         }
195                                 }
196
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;
202                                 }
203                                 this.Height = tsize.ToSize ().Height + 80;
204
205                                 // Now we set the left of the buttons
206                                 button_left = (this.Width / 2) - (tb_width / 2);
207                                 AddButtons ();
208                                 size_known = true;
209                         }
210                         #endregion      // MessageBoxForm Methods
211
212                         #region Functions for Adding buttons
213                         private void AddButtons()
214                         {
215                                 if (!buttons_placed) {
216                                         switch (msgbox_buttons) {
217                                                 case MessageBoxButtons.OK: {
218                                                         AddOkButton (0 + button_left);
219                                                         break;
220                                                 }
221
222                                                 case MessageBoxButtons.OKCancel: {
223                                                         AddOkButton (0 + button_left);
224                                                         AddCancelButton (110 + button_left);
225                                                         break;
226                                                 }
227
228                                                 case MessageBoxButtons.AbortRetryIgnore: {
229                                                         AddAbortButton (0 + button_left);
230                                                         AddRetryButton (110 + button_left);
231                                                         AddIgnoreButton (220 + button_left);
232                                                         break;
233                                                 }
234
235                                                 case MessageBoxButtons.YesNoCancel: {
236                                                         AddYesButton (0 + button_left);
237                                                         AddNoButton (110 + button_left);
238                                                         AddCancelButton (220 + button_left);
239                                                         break;
240                                                 }
241
242                                                 case MessageBoxButtons.YesNo: {
243                                                         AddYesButton (0 + button_left);
244                                                         AddNoButton (110 + button_left);
245                                                         break;
246                                                 }
247
248                                                 case MessageBoxButtons.RetryCancel: {
249                                                         AddRetryButton (0 + button_left);
250                                                         AddCancelButton (110 + button_left);
251                                                         break;
252                                                 }
253                                         }
254                                         buttons_placed = true;
255                                 }
256                         }
257
258                         private void AddOkButton (int left)
259                         {
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);
268                         }
269
270                         private void AddCancelButton (int left)
271                         {
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);
280                         }
281
282                         private void AddAbortButton (int left)
283                         {
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);
292                         }
293
294                         private void AddRetryButton(int left)
295                         {
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);
304                         }
305
306                         private void AddIgnoreButton (int left)
307                         {
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);
316                         }
317
318                         private void AddYesButton (int left)
319                         {
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);
328                         }
329
330                         private void AddNoButton (int left)
331                         {
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);
340                         }
341                         #endregion
342
343                         #region Button click handlers
344                         private void OkClick (object sender, EventArgs e)
345                         {
346                                 this.DialogResult = DialogResult.OK;
347                                 this.Close ();
348                         }
349
350                         private void CancelClick (object sender, EventArgs e)
351                         {
352                                 this.DialogResult = DialogResult.Cancel;
353                                 this.Close ();
354                         }
355
356                         private void AbortClick (object sender, EventArgs e)
357                         {
358                                 this.DialogResult = DialogResult.Abort;
359                                 this.Close ();
360                         }
361
362                         private void RetryClick (object sender, EventArgs e)
363                         {
364                                 this.DialogResult = DialogResult.Retry;
365                                 this.Close ();
366                         }
367
368                         private void IgnoreClick (object sender, EventArgs e)
369                         {
370                                 this.DialogResult = DialogResult.Ignore;
371                                 this.Close ();
372                         }
373
374                         private void YesClick (object sender, EventArgs e)
375                         {
376                                 this.DialogResult = DialogResult.Yes;
377                                 this.Close ();
378                         }
379
380                         private void NoClick (object sender, EventArgs e)
381                         {
382                                 this.DialogResult = DialogResult.No;
383                                 this.Close ();
384                         }
385                         #endregion
386                 }
387                 #endregion      // Private MessageBoxForm class
388
389
390                 #region Constructors
391                 private MessageBox ()
392                 {
393
394                 }
395                 #endregion      // Constructors
396
397                 #region Public Static Methods
398                 public static DialogResult Show (string text)
399                 {
400                         MessageBoxForm form = new MessageBoxForm (null, text, string.Empty,
401                                         MessageBoxButtons.OK, MessageBoxIcon.None);
402
403                         return form.RunDialog ();
404
405                 }
406
407                 public static DialogResult Show (IWin32Window owner, string text)
408                 {
409                         MessageBoxForm form = new MessageBoxForm (owner, text, string.Empty,
410                                         MessageBoxButtons.OK, MessageBoxIcon.None);
411                                 
412                         return form.RunDialog ();
413
414                 }
415
416                 public static DialogResult Show (string text, string caption)
417                 {
418                         MessageBoxForm form = new MessageBoxForm (null, text, caption,
419                                         MessageBoxButtons.OK, MessageBoxIcon.None);
420
421                         return form.RunDialog ();
422                 }
423
424                 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons)
425                 {
426                         MessageBoxForm form = new MessageBoxForm (null, text, caption,
427                                         buttons, MessageBoxIcon.None);
428                                 
429                         return form.RunDialog ();
430                 }
431
432                 public static DialogResult Show (IWin32Window owner, string text,  string caption,
433                                 MessageBoxButtons buttons)
434                 {
435                         MessageBoxForm form = new MessageBoxForm (owner, text, caption,
436                                         buttons, MessageBoxIcon.None);
437                                 
438                         return form.RunDialog ();
439                 }
440
441                 public static DialogResult Show (IWin32Window owner, string text, string caption,
442                                 MessageBoxButtons buttons, MessageBoxIcon icon)
443                 {
444                         MessageBoxForm form = new MessageBoxForm (owner, text, caption,
445                                         buttons, icon);
446                                 
447                         return form.RunDialog ();
448                 }
449
450
451                 public static DialogResult Show (IWin32Window owner, string text, string caption)
452                 {
453                         MessageBoxForm form = new MessageBoxForm (owner, text, caption,
454                                         MessageBoxButtons.OK, MessageBoxIcon.None);
455                                 
456                         return form.RunDialog ();
457                 }
458
459
460                 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
461                                 MessageBoxIcon icon)
462                 {
463                         MessageBoxForm form = new MessageBoxForm (null, text, caption,
464                                         buttons, icon);
465                                 
466                         return form.RunDialog ();
467                 }
468
469                 public static DialogResult Show (string text, string caption, MessageBoxButtons buttons,
470                                 MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
471                 {
472
473                         MessageBoxForm form = new MessageBoxForm (null, text, caption,
474                                         buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
475                                 
476                         return form.RunDialog ();
477
478                 }
479
480                 public static DialogResult Show (IWin32Window owner, string text, string caption,
481                                 MessageBoxButtons buttons, MessageBoxIcon icon,  MessageBoxDefaultButton defaultButton)
482                 {
483                         MessageBoxForm form = new MessageBoxForm (owner, text, caption,
484                                         buttons, icon, defaultButton, MessageBoxOptions.DefaultDesktopOnly);
485                                 
486                         return form.RunDialog ();
487                 }
488
489                 public static DialogResult
490                                 Show (string text, string caption,
491                                                 MessageBoxButtons buttons, MessageBoxIcon icon,
492                                                 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
493                 {
494                         MessageBoxForm form = new MessageBoxForm (null, text, caption,
495                                         buttons, icon, defaultButton, options);
496                                 
497                         return form.RunDialog ();
498                 }
499
500                 public static DialogResult Show (IWin32Window owner, string text, string caption,
501                                 MessageBoxButtons buttons, MessageBoxIcon icon,
502                                 MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
503                 {
504                         MessageBoxForm form = new MessageBoxForm (owner, text, caption,
505                                         buttons, icon, defaultButton, options);
506                                 
507                         return form.RunDialog ();
508                 }
509                 #endregion      // Public Static Methods
510         }
511 }
512