dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] pnetlib/System.Windows.Forms HBoxLayout.cs, NONE,


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] pnetlib/System.Windows.Forms HBoxLayout.cs, NONE, 1.1 VBoxLayout.cs, NONE, 1.1 MessageBox.cs, 1.5, 1.6
Date: Fri, 14 Nov 2003 02:36:34 +0000

Update of /cvsroot/dotgnu-pnet/pnetlib/System.Windows.Forms
In directory subversions:/tmp/cvs-serv26902/System.Windows.Forms

Modified Files:
        MessageBox.cs 
Added Files:
        HBoxLayout.cs VBoxLayout.cs 
Log Message:


Make the "MessageBox" class work in a basic fashion; add "HBoxLayout"
and "VBoxLayout" helper classes to do the hard work of automatic
dialog box layout.


Index: MessageBox.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/System.Windows.Forms/MessageBox.cs,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** MessageBox.cs       12 Oct 2003 22:39:07 -0000      1.5
--- MessageBox.cs       14 Nov 2003 02:36:32 -0000      1.6
***************
*** 220,223 ****
--- 220,226 ----
                private Button button2;
                private Button button3;
+               private VBoxLayout vbox;
+               private HBoxLayout hbox;
+               private HBoxLayout buttonBox;
                private bool hasCancel;
  
***************
*** 239,242 ****
--- 242,259 ----
                                }
  
+                               // Make the borders suitable for a dialog box.
+                               FormBorderStyle = FormBorderStyle.FixedDialog;
+ 
+                               // Create the layout areas.
+                               vbox = new VBoxLayout();
+                               hbox = new HBoxLayout();
+                               buttonBox = new HBoxLayout();
+                               vbox.Controls.Add(hbox);
+                               vbox.Controls.Add(buttonBox);
+                               vbox.StretchControl = hbox;
+                               buttonBox.UniformSize = true;
+                               vbox.Dock = DockStyle.Fill;
+                               Controls.Add(vbox);
+ 
                                // Create a control to display the message box 
icon.
                                this.icon = LoadIcon(icon);
***************
*** 245,253 ****
                                        iconControl = new Control();
                                        iconControl.ClientSize = this.icon.Size;
!                                       Controls.Add(iconControl);
                                }
  
                                // Create the label containing the message text.
                                textLabel = new Label();
                                if(text != null)
                                {
--- 262,271 ----
                                        iconControl = new Control();
                                        iconControl.ClientSize = this.icon.Size;
!                                       hbox.Controls.Add(iconControl);
                                }
  
                                // Create the label containing the message text.
                                textLabel = new Label();
+                               textLabel.TextAlign = 
ContentAlignment.MiddleLeft;
                                if(text != null)
                                {
***************
*** 258,262 ****
                                        textLabel.Text = String.Empty;
                                }
!                               Controls.Add(textLabel);
  
                                // Determine the number and names of the 
message box buttons.
--- 276,280 ----
                                        textLabel.Text = String.Empty;
                                }
!                               hbox.Controls.Add(textLabel);
  
                                // Determine the number and names of the 
message box buttons.
***************
*** 333,344 ****
  
                                // Add the buttons to the control.
!                               Controls.Add(button1);
                                if(button2 != null)
                                {
!                                       Controls.Add(button2);
                                }
                                if(button3 != null)
                                {
!                                       Controls.Add(button3);
                                }
  
--- 351,362 ----
  
                                // Add the buttons to the control.
!                               buttonBox.Controls.Add(button1);
                                if(button2 != null)
                                {
!                                       buttonBox.Controls.Add(button2);
                                }
                                if(button3 != null)
                                {
!                                       buttonBox.Controls.Add(button3);
                                }
  
***************
*** 412,416 ****
                                }
  
!                               // TODO: perform dialog box layout and sizing
                        }
  
--- 430,435 ----
                                }
  
!                               // Set the initial message box size to the 
vbox's recommended.
!                               ClientSize = vbox.RecommendedSize;
                        }
  

--- NEW FILE: HBoxLayout.cs ---
/*
 * HBoxLayout.cs - Implementation of the
 *                      "System.Windows.Forms.HBoxLayout" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Windows.Forms
{

using System.Drawing;

// This is a special-purpose control that lays out its children horizontally.
// It is intended for use inside dialog box controls like "MessageBox".

internal class HBoxLayout : Control
{
        // Internal state.
        private bool uniformSize;
        private Control stretchControl;
        private int margin, spacing;

        // Constructor.
        public HBoxLayout()
                        {
                                uniformSize = false;
                                stretchControl = null;
                                margin = 4;
                                spacing = 4;
                        }

        // Get or set whether all controls will have a uniform size.
        public bool UniformSize
                        {
                                get
                                {
                                        return uniformSize;
                                }
                                set
                                {
                                        uniformSize = value;
                                }
                        }

        // Get or set the control to be stretched in non-uniform layouts.
        // If this is not set, then the last control will be stretched.
        public Control StretchControl
                        {
                                get
                                {
                                        return stretchControl;
                                }
                                set
                                {
                                        stretchControl = value;
                                }
                        }

        // Get or set the margin.
        public int Margin
                        {
                                get
                                {
                                        return margin;
                                }
                                set
                                {
                                        margin = value;
                                }
                        }

        // Get or set the spacing between items.
        public int Spacing
                        {
                                get
                                {
                                        return spacing;
                                }
                                set
                                {
                                        spacing = value;
                                }
                        }

        // Get the recommended size for a control.
        internal static Size GetRecommendedSize(Control control)
                        {
                                if(control is HBoxLayout)
                                {
                                        return 
((HBoxLayout)control).RecommendedSize;
                                }
                                else if(control is VBoxLayout)
                                {
                                        return 
((VBoxLayout)control).RecommendedSize;
                                }
                                else if(control is Label)
                                {
                                        return new 
Size(((Label)control).PreferredWidth,
                                                                        
((Label)control).PreferredHeight);
                                }
                                else
                                {
                                        return control.Size;
                                }
                        }

        // Get the recommended client size for this control.
        public Size RecommendedSize
                        {
                                get
                                {
                                        int width = 0;
                                        int maxWidth = 0;
                                        int maxHeight = 0;
                                        int numVisible = 0;
                                        int xextra, yextra;
                                        Size childSize;
                                        foreach(Control child in Controls)
                                        {
                                                if(child.visible)
                                                {
                                                        childSize = 
GetRecommendedSize(child);
                                                        width += 
childSize.Width;
                                                        if(childSize.Width > 
maxWidth)
                                                        {
                                                                maxWidth = 
childSize.Width;
                                                        }
                                                        if(childSize.Height > 
maxHeight)
                                                        {
                                                                maxHeight = 
childSize.Height;
                                                        }
                                                        ++numVisible;
                                                }
                                        }
                                        xextra = margin * 2;
                                        if(numVisible >= 2)
                                        {
                                                xextra += (numVisible - 1) * 
spacing;
                                        }
                                        yextra = margin * 2;
                                        if(uniformSize)
                                        {
                                                return new Size
                                                        (maxWidth * numVisible 
+ xextra,
                                                         maxHeight + yextra);
                                        }
                                        else
                                        {
                                                return new Size
                                                        (width + xextra, 
maxHeight + yextra);
                                        }
                                }
                        }

        // Lay out the children in this control uniformly.
        private void UniformLayout()
                        {
                                int numVisible = 0;
                                int index;
                                int width, height;

                                // Count the number of visible child controls.
                                foreach(Control child1 in Controls)
                                {
                                        if(child1.visible)
                                        {
                                                ++numVisible;
                                        }
                                }
                                if(numVisible == 0)
                                {
                                        return;
                                }

                                // Determine the optimal width and height.
                                width = ClientSize.Width - margin * 2;
                                width -= (numVisible - 1) * spacing;
                                width = width / numVisible;
                                height = ClientSize.Height - margin * 2;

                                // Lay out the visible controls.
                                index = 0;
                                foreach(Control child in Controls)
                                {
                                        if(!(child.visible))
                                        {
                                                continue;
                                        }
                                        if(index == (numVisible - 1) && margin 
== 0)
                                        {
                                                // Lay out the final control to 
the full width,
                                                // to undo the effect of slight 
rounding errors.
                                                child.SetBounds
                                                        (index * (width + 
spacing), 0,
                                                         ClientSize.Width - 
index * width, height);
                                        }
                                        else
                                        {
                                                // Lay out some control other 
than the last.
                                                child.SetBounds
                                                        (margin + index * 
(width + spacing), margin,
                                                         width, height);
                                        }
                                        ++index;
                                }
                        }

        // Lay out the children in this control non-uniformly.
        private void NonUniformLayout()
                        {
                                ControlCollection controls = Controls;
                                int count, index;
                                Control stretch;
                                Control child;
                                int posn, posn2;
                                Size clientSize;
                                Size childSize;

                                // Find the control to be stretched.
                                if(stretchControl != null && 
stretchControl.visible)
                                {
                                        stretch = stretchControl;
                                }
                                else
                                {
                                        stretch = null;
                                        foreach(Control child1 in controls)
                                        {
                                                if(child1.visible)
                                                {
                                                        stretch = child1;
                                                }
                                        }
                                        if(stretch == null)
                                        {
                                                // Abort layout - none of the 
children are visible.
                                                return;
                                        }
                                }

                                // Lay out the children before the stretched 
control.
                                count = controls.Count;
                                index = 0;
                                posn = margin;
                                clientSize = ClientSize;
                                while(index < count)
                                {
                                        child = controls[index];
                                        if(child == stretch)
                                        {
                                                break;
                                        }
                                        if(child.visible)
                                        {
                                                childSize = 
GetRecommendedSize(child);
                                                child.SetBounds
                                                        (posn, margin, 
childSize.Width,
                                                         clientSize.Height - 2 
* margin);
                                                posn += childSize.Width + 
spacing;
                                        }
                                        ++index;
                                }

                                // Lay out the children after the stretched 
control.
                                posn2 = clientSize.Width - margin;
                                index = count - 1;
                                while(index > 0)
                                {
                                        child = controls[index];
                                        if(child == stretch)
                                        {
                                                break;
                                        }
                                        if(child.visible)
                                        {
                                                childSize = 
GetRecommendedSize(child);
                                                posn2 -= childSize.Width;
                                                child.SetBounds
                                                        (posn2, margin, 
childSize.Width,
                                                         clientSize.Height - 2 
* margin);
                                                posn2 -= spacing;
                                        }
                                        --index;
                                }

                                // Lay out the stretched control.
                                if(posn2 < posn)
                                {
                                        posn2 = posn;
                                }
                                stretch.SetBounds
                                        (posn, margin, posn2 - posn,
                                         clientSize.Height - 2 * margin);
                        }

        // Lay out the children in this control.
        protected override void OnLayout(LayoutEventArgs e)
                        {
                                if(uniformSize)
                                {
                                        UniformLayout();
                                }
                                else
                                {
                                        NonUniformLayout();
                                }
                        }

}; // class HBoxLayout

}; // namespace System.Windows.Forms

--- NEW FILE: VBoxLayout.cs ---
/*
 * VBoxLayout.cs - Implementation of the
 *                      "System.Windows.Forms.VBoxLayout" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Windows.Forms
{

using System.Drawing;

// This is a special-purpose control that lays out its children vertically.
// It is intended for use inside dialog box controls like "MessageBox".

internal class VBoxLayout : Control
{
        // Internal state.
        private bool uniformSize;
        private Control stretchControl;
        private int margin, spacing;

        // Constructor.
        public VBoxLayout()
                        {
                                uniformSize = false;
                                stretchControl = null;
                                margin = 4;
                                spacing = 4;
                        }

        // Get or set whether all controls will have a uniform size.
        public bool UniformSize
                        {
                                get
                                {
                                        return uniformSize;
                                }
                                set
                                {
                                        uniformSize = value;
                                }
                        }

        // Get or set the control to be stretched in non-uniform layouts.
        // If this is not set, then the last control will be stretched.
        public Control StretchControl
                        {
                                get
                                {
                                        return stretchControl;
                                }
                                set
                                {
                                        stretchControl = value;
                                }
                        }

        // Get or set the margin.
        public int Margin
                        {
                                get
                                {
                                        return margin;
                                }
                                set
                                {
                                        margin = value;
                                }
                        }

        // Get or set the spacing between items.
        public int Spacing
                        {
                                get
                                {
                                        return spacing;
                                }
                                set
                                {
                                        spacing = value;
                                }
                        }

        // Get the recommended client size for this control.
        public Size RecommendedSize
                        {
                                get
                                {
                                        int height = 0;
                                        int maxWidth = 0;
                                        int maxHeight = 0;
                                        int numVisible = 0;
                                        int xextra, yextra;
                                        Size childSize;
                                        foreach(Control child in Controls)
                                        {
                                                if(child.visible)
                                                {
                                                        childSize = 
HBoxLayout.GetRecommendedSize(child);
                                                        height += 
childSize.Height;
                                                        if(childSize.Width > 
maxWidth)
                                                        {
                                                                maxWidth = 
childSize.Width;
                                                        }
                                                        if(childSize.Height > 
maxHeight)
                                                        {
                                                                maxHeight = 
childSize.Height;
                                                        }
                                                        ++numVisible;
                                                }
                                        }
                                        xextra = margin * 2;
                                        yextra = margin * 2;
                                        if(numVisible >= 2)
                                        {
                                                yextra += (numVisible - 1) * 
spacing;
                                        }
                                        if(uniformSize)
                                        {
                                                return new Size
                                                        (maxWidth + xextra,
                                                         maxHeight * numVisible 
+ yextra);
                                        }
                                        else
                                        {
                                                return new Size
                                                        (maxWidth + xextra, 
height + yextra);
                                        }
                                }
                        }

        // Lay out the children in this control uniformly.
        private void UniformLayout()
                        {
                                int numVisible = 0;
                                int index;
                                int width, height;

                                // Count the number of visible child controls.
                                foreach(Control child1 in Controls)
                                {
                                        if(child1.visible)
                                        {
                                                ++numVisible;
                                        }
                                }
                                if(numVisible == 0)
                                {
                                        return;
                                }

                                // Determine the optimal width and height.
                                width = ClientSize.Width - margin * 2;
                                height = ClientSize.Height - margin * 2;
                                height -= (numVisible - 1) * spacing;
                                height = height / numVisible;

                                // Lay out the visible controls.
                                index = 0;
                                foreach(Control child in Controls)
                                {
                                        if(!(child.visible))
                                        {
                                                continue;
                                        }
                                        if(index == (numVisible - 1) && margin 
== 0)
                                        {
                                                // Lay out the final control to 
the full width,
                                                // to undo the effect of slight 
rounding errors.
                                                child.SetBounds
                                                        (0, index * (height + 
spacing), width,
                                                         ClientSize.Height - 
index * (height + spacing));
                                        }
                                        else
                                        {
                                                // Lay out some control other 
than the last.
                                                child.SetBounds
                                                        (margin, margin + index 
* (height + spacing),
                                                         width, height);
                                        }
                                        ++index;
                                }
                        }

        // Lay out the children in this control non-uniformly.
        private void NonUniformLayout()
                        {
                                ControlCollection controls = Controls;
                                int count, index;
                                Control stretch;
                                Control child;
                                int posn, posn2;
                                Size clientSize;
                                Size childSize;

                                // Find the control to be stretched.
                                if(stretchControl != null && 
stretchControl.visible)
                                {
                                        stretch = stretchControl;
                                }
                                else
                                {
                                        stretch = null;
                                        foreach(Control child1 in controls)
                                        {
                                                if(child1.visible)
                                                {
                                                        stretch = child1;
                                                }
                                        }
                                        if(stretch == null)
                                        {
                                                // Abort layout - none of the 
children are visible.
                                                return;
                                        }
                                }

                                // Lay out the children before the stretched 
control.
                                count = controls.Count;
                                index = 0;
                                posn = margin;
                                clientSize = ClientSize;
                                while(index < count)
                                {
                                        child = controls[index];
                                        if(child == stretch)
                                        {
                                                break;
                                        }
                                        if(child.visible)
                                        {
                                                childSize = 
HBoxLayout.GetRecommendedSize(child);
                                                child.SetBounds
                                                        (margin, posn, 
childSize.Width - 2 * margin,
                                                         childSize.Height);
                                                posn += childSize.Height + 
spacing;
                                        }
                                        ++index;
                                }

                                // Lay out the children after the stretched 
control.
                                posn2 = clientSize.Height - margin;
                                index = count - 1;
                                while(index > 0)
                                {
                                        child = controls[index];
                                        if(child == stretch)
                                        {
                                                break;
                                        }
                                        if(child.visible)
                                        {
                                                childSize = 
HBoxLayout.GetRecommendedSize(child);
                                                posn2 -= childSize.Height;
                                                child.SetBounds
                                                        (margin, posn2, 
clientSize.Width - 2 * margin,
                                                         childSize.Height);
                                                posn2 -= spacing;
                                        }
                                        --index;
                                }

                                // Lay out the stretched control.
                                if(posn2 < posn)
                                {
                                        posn2 = posn;
                                }
                                stretch.SetBounds
                                        (margin, posn, clientSize.Width - 2 * 
margin,
                                         posn2 - posn);
                        }

        // Lay out the children in this control.
        protected override void OnLayout(LayoutEventArgs e)
                        {
                                if(uniformSize)
                                {
                                        UniformLayout();
                                }
                                else
                                {
                                        NonUniformLayout();
                                }
                        }

}; // class VBoxLayout

}; // namespace System.Windows.Forms





reply via email to

[Prev in Thread] Current Thread [Next in Thread]