2 // System.Windows.Forms.GroupBox.cs
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 // Jordi Mas i Hernandez, jordi@ximian.com
28 // Copyright (C) Novell Inc., 2004-2005
33 using System.ComponentModel;
34 using System.ComponentModel.Design;
35 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms
39 [DefaultProperty("Text")]
40 [DefaultEvent("Enter")]
41 [Designer ("System.Windows.Forms.Design.GroupBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [ClassInterface (ClassInterfaceType.AutoDispatch)]
46 public class GroupBox : Control
48 private FlatStyle flat_style;
49 private Rectangle display_rectangle = new Rectangle ();
54 [EditorBrowsable (EditorBrowsableState.Always)]
55 public new event EventHandler AutoSizeChanged {
56 add { base.AutoSizeChanged += value; }
57 remove { base.AutoSizeChanged -= value; }
62 [EditorBrowsable(EditorBrowsableState.Advanced)]
63 public new event EventHandler Click {
64 add { base.Click += value; }
65 remove { base.Click -= value; }
69 [EditorBrowsable(EditorBrowsableState.Advanced)]
70 public new event EventHandler DoubleClick {
71 add { base.DoubleClick += value; }
72 remove { base.DoubleClick -= value; }
76 [EditorBrowsable(EditorBrowsableState.Advanced)]
77 public new event KeyEventHandler KeyDown {
78 add { base.KeyDown += value; }
79 remove { base.KeyDown -= value; }
83 [EditorBrowsable(EditorBrowsableState.Advanced)]
84 public new event KeyPressEventHandler KeyPress {
85 add { base.KeyPress += value; }
86 remove { base.KeyPress -= value; }
90 [EditorBrowsable(EditorBrowsableState.Advanced)]
91 public new event KeyEventHandler KeyUp {
92 add { base.KeyUp += value; }
93 remove { base.KeyUp -= value; }
98 [EditorBrowsable (EditorBrowsableState.Advanced)]
99 public new event MouseEventHandler MouseClick {
100 add { base.MouseClick += value; }
101 remove { base.MouseClick -= value; }
105 [EditorBrowsable (EditorBrowsableState.Advanced)]
106 public new event MouseEventHandler MouseDoubleClick {
107 add { base.MouseDoubleClick += value; }
108 remove { base.MouseDoubleClick -= value; }
113 [EditorBrowsable(EditorBrowsableState.Advanced)]
114 public new event MouseEventHandler MouseDown {
115 add { base.MouseDown += value; }
116 remove { base.MouseDown -= value; }
120 [EditorBrowsable(EditorBrowsableState.Advanced)]
121 public new event EventHandler MouseEnter {
122 add { base.MouseEnter += value; }
123 remove { base.MouseEnter -= value; }
127 [EditorBrowsable(EditorBrowsableState.Advanced)]
128 public new event EventHandler MouseLeave {
129 add { base.MouseLeave += value; }
130 remove { base.MouseLeave -= value; }
134 [EditorBrowsable(EditorBrowsableState.Advanced)]
135 public new event MouseEventHandler MouseMove {
136 add { base.MouseMove += value; }
137 remove { base.MouseMove -= value; }
141 [EditorBrowsable(EditorBrowsableState.Advanced)]
142 public new event MouseEventHandler MouseUp {
143 add { base.MouseUp += value; }
144 remove { base.MouseUp -= value; }
148 [EditorBrowsable(EditorBrowsableState.Advanced)]
149 public new event EventHandler TabStopChanged {
150 add { base.TabStopChanged += value; }
151 remove { base.TabStopChanged -= value; }
158 flat_style = FlatStyle.Standard;
160 SetStyle(ControlStyles.ContainerControl | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
161 SetStyle(ControlStyles.Selectable, false);
164 #region Public Properties
166 [EditorBrowsable(EditorBrowsableState.Advanced)]
167 public override bool AllowDrop {
168 get { return base.AllowDrop; }
169 set { base.AllowDrop = value; }
174 [EditorBrowsable (EditorBrowsableState.Always)]
175 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
176 public override bool AutoSize {
177 get { return base.AutoSize; }
178 set { base.AutoSize = value; }
182 [DefaultValue (AutoSizeMode.GrowOnly)]
184 public AutoSizeMode AutoSizeMode {
185 get { return base.GetAutoSizeMode (); }
186 set { base.SetAutoSizeMode (value); }
190 protected override CreateParams CreateParams {
191 get { return base.CreateParams; }
194 protected override Size DefaultSize {
195 get { return ThemeEngine.Current.GroupBoxDefaultSize;}
198 public override Rectangle DisplayRectangle {
201 display_rectangle.X = Padding.Left;
202 display_rectangle.Y = Font.Height + Padding.Top;
203 display_rectangle.Width = Width - Padding.Horizontal;
204 display_rectangle.Height = Height - Font.Height - Padding.Vertical;
205 return display_rectangle;
207 display_rectangle.X = 3;
208 display_rectangle.Y = Font.Height + 3;
209 display_rectangle.Width = Width - 6;
210 display_rectangle.Height = Height - Font.Height - 6;
211 return display_rectangle;
216 [DefaultValue(FlatStyle.Standard)]
217 public FlatStyle FlatStyle {
218 get { return flat_style; }
220 if (!Enum.IsDefined (typeof (FlatStyle), value))
221 new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
223 if (flat_style == value)
232 [EditorBrowsable(EditorBrowsableState.Advanced)]
233 public new bool TabStop {
234 get { return base.TabStop; }
235 set { base.TabStop = value; }
239 public override string Text {
240 get { return base.Text; }
242 if (base.Text == value)
250 #endregion //Public Properties
252 #region Public Methods
254 protected override AccessibleObject CreateAccessibilityInstance ()
256 return new GroupBoxAccessibleObject (this);
260 protected override void OnFontChanged (EventArgs e)
262 base.OnFontChanged (e);
266 protected override void OnPaint (PaintEventArgs pevent)
268 ThemeEngine.Current.DrawGroupBox (pevent.Graphics, ClientRectangle, this);
269 base.OnPaint(pevent);
272 protected override bool ProcessMnemonic (char charCode)
274 if (IsMnemonic(charCode, Text) == true) {
275 // Select item next in line in tab order
276 if (this.Parent != null) {
277 Parent.SelectNextControl(this, true, false, true, false);
282 return base.ProcessMnemonic (charCode);
286 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
288 base.ScaleControl (factor, specified);
292 public override string ToString()
294 return GetType ().FullName.ToString () + ", Text: " + Text;
297 protected override void WndProc(ref Message m) {
298 base.WndProc (ref m);
301 #endregion Public Methods
303 [DefaultValue (false)]
304 public bool UseCompatibleTextRendering {
306 return use_compatible_text_rendering;
310 use_compatible_text_rendering = value;
315 #region Protected Properties
317 protected override Padding DefaultPadding {
318 get { return new Padding (3); }
323 #region Internal Methods
325 internal override Size GetPreferredSizeCore (Size proposedSize)
327 Size retsize = new Size (Padding.Left, Padding.Top);
329 foreach (Control child in Controls) {
330 if (child.Dock == DockStyle.Fill) {
331 if (child.Bounds.Right > retsize.Width)
332 retsize.Width = child.Bounds.Right;
333 } else if (child.Dock != DockStyle.Top && child.Dock != DockStyle.Bottom && (child.Anchor & AnchorStyles.Right) == 0 && (child.Bounds.Right + child.Margin.Right) > retsize.Width)
334 retsize.Width = child.Bounds.Right + child.Margin.Right;
336 if (child.Dock == DockStyle.Fill) {
337 if (child.Bounds.Bottom > retsize.Height)
338 retsize.Height = child.Bounds.Bottom;
339 } else if (child.Dock != DockStyle.Left && child.Dock != DockStyle.Right && (child.Anchor & AnchorStyles.Bottom) == 0 && (child.Bounds.Bottom + child.Margin.Bottom) > retsize.Height)
340 retsize.Height = child.Bounds.Bottom + child.Margin.Bottom;
343 retsize.Width += Padding.Right;
344 retsize.Height += Padding.Bottom;
346 retsize.Height += this.Font.Height;
353 #region Private Classes
354 private class GroupBoxAccessibleObject : Control.ControlAccessibleObject
356 public GroupBoxAccessibleObject (Control owner) : base (owner)