[Mono-winforms-list] Patch for MessageBox

Dasnois Benjamin Dasnois Benjamin <benjamin.dasnois@gmail.com>
Mon, 4 Oct 2004 19:37:16 +0200


------=_Part_49_27682551.1096911436591
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi,
I did work on MessageBox and finished a partial implementation, but
there are still some missing features like icons and MessageBoxOptions
handling. Anyway you can use it to display a message and retrieve a
response from the user, all buttons are working. The only really
missing thing is the icons.

See attached file :)

Bye.

-- 
DASNOIS Benjamin

------=_Part_49_27682551.1096911436591
Content-Type: text/plain; name="MessageBox.cs"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename="MessageBox.cs"

// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2004 Novell, Inc.
//
// Authors:
//=09Jordi Mas i Hernandez, jordi@ximian.com
//  Benjamin Dasnois, benjamin.dasnois@gmail.com
//
// TODO:
//=09=09- Complete the implementation when icons are available, Form.Border=
Style is available.
//=09- Add support for MessageBoxOptions and MessageBoxDefaultButton.
//
//
// $Revision: 1.1 $
// $Modtime: $
// $Log: MessageBox.cs,v $
// Revision 1.1=09 2004/07/26 11:41:35  jordi
// initial messagebox implementation
//
//

// INCOMPLETE

using System;
using System.Drawing;

namespace System.Windows.Forms
{

=09public class MessageBox
=09{
=09=09private class MessageBoxForm : Form
=09=09{
=09=09=09string msgbox_text;
=09=09=09bool size_known=3Dfalse;
=09=09=09const int space_image_text =3D 20;
=09=09=09Image icon_image;
=09=09=09Point textleft_up;
=09=09=09MessageBoxButtons msgbox_buttons;
=09=09=09bool buttons_placed =3D false;
=09=09=09int button_left;

=09=09=09public MessageBoxForm (IWin32Window owner, string text, string cap=
tion,
=09=09=09=09=09MessageBoxButtons buttons, MessageBoxIcon icon)=20
=09=09=09{
=09=09=09=09switch (icon) {
=09=09=09=09case MessageBoxIcon.None:
=09=09=09=09=09icon_image =3D null;
=09=09=09=09=09break;
=09=09=09=09case MessageBoxIcon.Error:
=09=09=09=09=09break;
=09=09=09=09case MessageBoxIcon.Question:
=09=09=09=09=09break;
=09=09=09=09case MessageBoxIcon.Exclamation:
=09=09=09=09=09break;
=09=09=09=09case MessageBoxIcon.Asterisk:
=09=09=09=09=09break;
=09=09=09=09}

=09=09=09=09msgbox_text =3D text;
=09=09=09=09msgbox_buttons =3D buttons;
=09=09=09=09this.Text =3D caption;
=09=09=09=09this.Paint +=3D new PaintEventHandler (MessageBoxForm_Paint);
=09=09=09}

=09=09=09[MonoTODO]
=09=09=09public MessageBoxForm (IWin32Window owner, string text, string cap=
tion,
=09=09=09=09=09MessageBoxButtons buttons, MessageBoxIcon icon,
=09=09=09=09=09MessageBoxDefaultButton defaultButton, MessageBoxOptions opt=
ions) : this (owner, text, caption, buttons, icon)
=09=09=09{
=09=09=09=09// Still needs to implement defaultButton and options

=09=09=09}
=09=09=09
=09=09=09public DialogResult RunDialog ()
=09=09=09{
=09=09=09=09this.StartPosition =3D FormStartPosition.CenterScreen;
=09=09=09=09this.ShowDialog ();

=09=09=09=09return this.DialogResult;

=09=09=09}

=09=09=09private void MessageBoxForm_Paint(object sender, PaintEventArgs e)
=09=09=09{
=09=09=09=09if (size_known =3D=3D false) {
=09=09=09=09=09InitFormsSize (e);
=09=09=09=09}
=09=09=09      =20
=09=09=09=09e.Graphics.DrawString (msgbox_text, this.Font, new SolidBrush(C=
olor.Black), textleft_up);
=09=09=09}

=09=09=09private void InitFormsSize(PaintEventArgs e)
=09=09=09{
=09=09=09=09int tb_width =3D 0;
=09=09
=09=09=09=09// First we have to know the size of text + image
=09=09=09=09Drawing.SizeF tsize =3D e.Graphics.MeasureString (msgbox_text, =
this.Font);

=09=09=09=09if (!(icon_image =3D=3D null)) {
=09=09=09=09=09tsize.Width +=3D icon_image.Width;
=09=09=09=09=09textleft_up =3D new Point (icon_image.Width + space_image_te=
xt, 0);
=09=09=09=09} else {
=09=09=09=09=09textleft_up =3D new Point (0, 0);
=09=09=09=09}

=09=09=09=09// Now we want to know the width of buttons
=09=09
=09=09=09=09switch (msgbox_buttons) {
=09=09=09=09case MessageBoxButtons.OK:
=09=09=09=09=09tb_width =3D 110 * 1;
=09=09=09=09=09break;
=09=09=09=09case MessageBoxButtons.OKCancel:
=09=09=09=09=09tb_width =3D 110 * 2;
=09=09=09=09=09break;
=09=09=09=09case MessageBoxButtons.AbortRetryIgnore:
=09=09=09=09=09tb_width =3D 110 * 3;
=09=09=09=09=09break;
=09=09=09=09case MessageBoxButtons.YesNoCancel:
=09=09=09=09=09tb_width =3D 110 * 3;
=09=09=09=09=09break;
=09=09=09=09case MessageBoxButtons.YesNo:
=09=09=09=09=09tb_width =3D 110 * 2;
=09=09=09=09=09break;
=09=09=09=09case MessageBoxButtons.RetryCancel:
=09=09=09=09=09tb_width =3D 110 * 2;
=09=09=09=09=09break;
=09=09
=09=09=09=09}

=09=09=09=09// Now we choose the good size for the form
=09=09=09=09if (tsize.ToSize ().Width > tb_width) {
=09=09=09=09=09this.Width =3D tsize.ToSize().Width + 10;
=09=09=09=09} else {
=09=09=09=09=09this.Width =3D tb_width;
=09=09=09=09}
=09=09=09=09this.Height =3D tsize.ToSize ().Height + 80;

=09=09=09=09// Now we set the left of the buttons
=09=09=09=09button_left =3D (this.Width / 2) - (tb_width / 2);
=09=09=09=09AddButtons ();
=09=09=09=09size_known =3D true;
=09=09=09=09this.Refresh ();
=09=09=09}

=09=09=09private void AddButtons()
=09=09=09{
=09=09=09=09if (!buttons_placed) {
=09=09=09=09=09switch (msgbox_buttons) {
=09=09=09=09=09case MessageBoxButtons.OK:
=09=09=09=09=09=09AddOkButton (0 + button_left);
=09=09=09=09=09=09break;
=09=09=09=09=09case MessageBoxButtons.OKCancel:
=09=09=09=09=09=09AddOkButton (0 + button_left);
=09=09=09=09=09=09AddCancelButton (110 + button_left);
=09=09=09=09=09=09break;
=09=09=09=09=09case MessageBoxButtons.AbortRetryIgnore:
=09=09=09=09=09=09AddAbortButton (0 + button_left);
=09=09=09=09=09=09AddRetryButton (110 + button_left);
=09=09=09=09=09=09AddIgnoreButton (220 + button_left);
=09=09=09=09=09=09break;
=09=09=09=09=09case MessageBoxButtons.YesNoCancel:
=09=09=09=09=09=09AddYesButton (0 + button_left);
=09=09=09=09=09=09AddNoButton (110 + button_left);
=09=09=09=09=09=09AddCancelButton (220 + button_left);
=09=09=09=09=09=09break;
=09=09=09=09=09case MessageBoxButtons.YesNo:
=09=09=09=09=09=09AddYesButton (0 + button_left);
=09=09=09=09=09=09AddNoButton (110 + button_left);
=09=09=09=09=09=09break;
=09=09=09=09=09case MessageBoxButtons.RetryCancel:
=09=09=09=09=09=09AddRetryButton (0 + button_left);
=09=09=09=09=09=09AddCancelButton (110 + button_left);
=09=09=09=09=09=09break;

=09=09=09=09=09}
=09=09=09=09=09buttons_placed =3D true;
=09=09=09=09}
=09=09=09}

#region Functions for Adding buttons

=09=09=09private void AddOkButton (int left)
=09=09=09{
=09=09=09=09Button bok =3D new Button ();
=09=09=09=09bok.Text =3D "OK";
=09=09=09=09this.Controls.Add (bok);
=09=09=09=09bok.Width =3D 100;
=09=09=09=09bok.Height =3D 30;
=09=09=09=09bok.Top =3D this.Height - 70;
=09=09=09=09bok.Left =3D left;
=09=09=09=09bok.Show ();
=09=09=09=09bok.Click +=3D new EventHandler (OkClick);

=09=09=09}

=09=09=09private void AddCancelButton (int left)
=09=09=09{
=09=09=09=09Button bcan =3D new Button ();
=09=09=09=09bcan.Text =3D "Cancel";
=09=09=09=09this.Controls.Add (bcan);
=09=09=09=09bcan.Width =3D 100;
=09=09=09=09bcan.Height =3D 30;
=09=09=09=09bcan.Top =3D this.Height - 70;
=09=09=09=09bcan.Left =3D left;
=09=09=09=09bcan.Show ();
=09=09=09=09bcan.Click +=3D new EventHandler (CancelClick);
=09=09=09}

=09=09=09private void AddAbortButton (int left)
=09=09=09{
=09=09=09=09Button babort =3D new Button ();
=09=09=09=09babort.Text =3D "Abort";
=09=09=09=09this.Controls.Add (babort);
=09=09=09=09babort.Width =3D 100;
=09=09=09=09babort.Height =3D 30;
=09=09=09=09babort.Top =3D this.Height - 70;
=09=09=09=09babort.Left =3D left;
=09=09=09=09babort.Show ();
=09=09=09=09babort.Click +=3D new EventHandler (AbortClick);

=09=09=09}

=09=09=09private void AddRetryButton(int left)
=09=09=09{
=09=09=09=09Button bretry =3D new Button ();
=09=09=09=09bretry.Text =3D "Retry";
=09=09=09=09this.Controls.Add (bretry);
=09=09=09=09bretry.Width =3D 100;
=09=09=09=09bretry.Height =3D 30;
=09=09=09=09bretry.Top =3D this.Height - 70;
=09=09=09=09bretry.Left =3D left;
=09=09=09=09bretry.Show ();
=09=09=09=09bretry.Click +=3D new EventHandler (RetryClick);

=09=09=09}

=09=09=09private void AddIgnoreButton (int left)
=09=09=09{
=09=09=09=09Button bignore =3D new Button ();
=09=09=09=09bignore.Text =3D "Ignore";
=09=09=09=09this.Controls.Add (bignore);
=09=09=09=09bignore.Width =3D 100;
=09=09=09=09bignore.Height =3D 30;
=09=09=09=09bignore.Top =3D this.Height - 70;
=09=09=09=09bignore.Left =3D left;
=09=09=09=09bignore.Show ();
=09=09=09=09bignore.Click +=3D new EventHandler (IgnoreClick);

=09=09=09}

=09=09=09private void AddYesButton (int left)
=09=09=09{
=09=09=09=09Button byes =3D new Button ();
=09=09=09=09byes.Text =3D "Yes";
=09=09=09=09this.Controls.Add (byes);
=09=09=09=09byes.Width =3D 100;
=09=09=09=09byes.Height =3D 30;
=09=09=09=09byes.Top =3D this.Height - 70;
=09=09=09=09byes.Left =3D left;
=09=09=09=09byes.Show ();
=09=09=09=09byes.Click +=3D new EventHandler (YesClick);

=09=09=09}

=09=09=09private void AddNoButton (int left)
=09=09=09{
=09=09=09=09Button bno =3D new Button ();
=09=09=09=09bno.Text =3D "No";
=09=09=09=09this.Controls.Add (bno);
=09=09=09=09bno.Width =3D 100;
=09=09=09=09bno.Height =3D 30;
=09=09=09=09bno.Top =3D this.Height - 70;
=09=09=09=09bno.Left =3D left;
=09=09=09=09bno.Show ();
=09=09=09=09bno.Click +=3D new EventHandler (NoClick);

=09=09=09}

#endregion

#region Button click handlers

=09=09=09private void OkClick (object sender, EventArgs e)
=09=09=09{
=09=09=09=09this.DialogResult =3D DialogResult.OK;
=09=09=09=09this.Close ();
=09=09=09}

=09=09=09private void CancelClick (object sender, EventArgs e)
=09=09=09{
=09=09=09=09this.DialogResult =3D DialogResult.Cancel;
=09=09=09=09this.Close ();
=09=09=09}

=09=09=09private void AbortClick (object sender, EventArgs e)
=09=09=09{
=09=09=09=09this.DialogResult =3D DialogResult.Abort;
=09=09=09=09this.Close ();
=09=09=09}

=09=09=09private void RetryClick (object sender, EventArgs e)
=09=09=09{
=09=09=09=09this.DialogResult =3D DialogResult.Retry;
=09=09=09=09this.Close ();
=09=09=09}

=09=09=09private void IgnoreClick (object sender, EventArgs e)
=09=09=09{
=09=09=09=09this.DialogResult =3D DialogResult.Ignore;
=09=09=09=09this.Close ();
=09=09=09}

=09=09=09private void YesClick (object sender, EventArgs e)
=09=09=09{
=09=09=09=09this.DialogResult =3D DialogResult.Yes;
=09=09=09=09this.Close ();
=09=09=09}

=09=09=09private void NoClick (object sender, EventArgs e)
=09=09=09{
=09=09=09=09this.DialogResult =3D DialogResult.No;
=09=09=09=09this.Close ();
=09=09=09}

#endregion
=09=09}


=09=09private MessageBox ()
=09=09{

=09=09}

=09=09public static DialogResult Show (string text)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (null, text, string.Emp=
ty,
=09=09=09=09=09MessageBoxButtons.OK, MessageBoxIcon.None);

=09=09=09return form.RunDialog ();

=09=09}

=09=09public static DialogResult Show (IWin32Window owner, string text)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (owner, text, string.Em=
pty,
=09=09=09=09=09MessageBoxButtons.OK, MessageBoxIcon.None);
=09=09=09=09
=09=09=09return form.RunDialog ();

=09=09}

=09=09public static DialogResult Show (string text, string caption)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (null, text, caption,
=09=09=09=09=09MessageBoxButtons.OK, MessageBoxIcon.None);

=09=09=09return form.RunDialog ();
=09=09}

=09=09public static DialogResult Show (string text, string caption, Message=
BoxButtons buttons)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (null, text, caption,
=09=09=09=09=09buttons, MessageBoxIcon.None);
=09=09=09=09
=09=09=09return form.RunDialog ();
=09=09}

=09=09public static DialogResult Show (IWin32Window owner, string text,  st=
ring caption,
=09=09=09=09MessageBoxButtons buttons)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (owner, text, caption,
=09=09=09=09=09buttons, MessageBoxIcon.None);
=09=09=09=09
=09=09=09return form.RunDialog ();
=09=09}

=09=09public static DialogResult Show (IWin32Window owner, string text, str=
ing caption,
=09=09=09=09MessageBoxButtons buttons, MessageBoxIcon icon)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (owner, text, caption,
=09=09=09=09=09buttons, icon);
=09=09=09=09
=09=09=09return form.RunDialog ();
=09=09}


=09=09public static DialogResult Show (IWin32Window owner, string text, str=
ing caption)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (owner, text, caption,
=09=09=09=09=09MessageBoxButtons.OK, MessageBoxIcon.None);
=09=09=09=09
=09=09=09return form.RunDialog ();
=09=09}


=09=09public static DialogResult Show (string text, string caption, Message=
BoxButtons buttons,
=09=09=09=09MessageBoxIcon icon)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (null, text, caption,
=09=09=09=09=09buttons, icon);
=09=09=09=09
=09=09=09return form.RunDialog ();
=09=09}

=09=09public static DialogResult Show (string text, string caption, Message=
BoxButtons buttons,
=09=09=09=09MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
=09=09{

=09=09=09MessageBoxForm form =3D new MessageBoxForm (null, text, caption,
=09=09=09=09=09buttons, icon, defaultButton, MessageBoxOptions.DefaultDeskt=
opOnly);
=09=09=09=09
=09=09=09return form.RunDialog ();

=09=09}

=09=09public static DialogResult Show (IWin32Window owner, string text, str=
ing caption,
=09=09=09=09MessageBoxButtons buttons, MessageBoxIcon icon,=09 MessageBoxDe=
faultButton defaultButton)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (owner, text, caption,
=09=09=09=09=09buttons, icon, defaultButton, MessageBoxOptions.DefaultDeskt=
opOnly);
=09=09=09=09
=09=09=09return form.RunDialog ();
=09=09}

=09=09public static DialogResult
=09=09=09=09Show (string text, string caption,
=09=09=09=09=09=09MessageBoxButtons buttons, MessageBoxIcon icon,
=09=09=09=09=09=09MessageBoxDefaultButton defaultButton, MessageBoxOptions =
options)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (null, text, caption,
=09=09=09=09=09buttons, icon, defaultButton, options);
=09=09=09=09
=09=09=09return form.RunDialog ();
=09=09}

=09=09public static DialogResult Show (IWin32Window owner, string text, str=
ing caption,
=09=09=09=09MessageBoxButtons buttons, MessageBoxIcon icon,
=09=09=09=09MessageBoxDefaultButton defaultButton, MessageBoxOptions option=
s)
=09=09{
=09=09=09MessageBoxForm form =3D new MessageBoxForm (owner, text, caption,
=09=09=09=09=09buttons, icon, defaultButton, options);
=09=09=09=09
=09=09=09return form.RunDialog ();
=09=09}
=09}
}


------=_Part_49_27682551.1096911436591--