1 //
2 // System.Windows.Forms.GroupBox.cs
3 //
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:
11 //
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 //
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.
22 //
23 // Authors:
24 //		Jordi Mas i Hernandez, jordi@ximian.com
25 //
26 // TODO:
27 //
28 // Copyright (C) Novell Inc., 2004-2005
29 //
30 //
31 
32 using System.Drawing;
33 using System.ComponentModel;
34 using System.ComponentModel.Design;
35 using System.Runtime.InteropServices;
36 
37 namespace System.Windows.Forms
38 {
39 	[DefaultProperty("Text")]
40 	[DefaultEvent("Enter")]
41 	[Designer ("System.Windows.Forms.Design.GroupBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
42 	[ClassInterface (ClassInterfaceType.AutoDispatch)]
43 	[ComVisible (true)]
44 	public class GroupBox : Control
45 	{
46 		private FlatStyle flat_style;
47 		private Rectangle display_rectangle = new Rectangle ();
48 
49 		#region Events
50 		[Browsable (true)]
51 		[EditorBrowsable (EditorBrowsableState.Always)]
52 		public new event EventHandler AutoSizeChanged {
53 			add { base.AutoSizeChanged += value; }
54 			remove { base.AutoSizeChanged -= value; }
55 		}
56 
57 		[Browsable (false)]
58 		[EditorBrowsable(EditorBrowsableState.Advanced)]
59 		public new event EventHandler Click {
60 			add { base.Click += value; }
61 			remove { base.Click -= value; }
62 		}
63 
64 		[Browsable(false)]
65 		[EditorBrowsable(EditorBrowsableState.Advanced)]
66 		public new event EventHandler DoubleClick {
67 			add { base.DoubleClick += value; }
68 			remove { base.DoubleClick -= value; }
69 		}
70 
71 		[Browsable(false)]
72 		[EditorBrowsable(EditorBrowsableState.Advanced)]
73 		public new event KeyEventHandler KeyDown {
74 			add { base.KeyDown += value; }
75 			remove { base.KeyDown -= value; }
76 		}
77 
78 		[Browsable(false)]
79 		[EditorBrowsable(EditorBrowsableState.Advanced)]
80 		public new event KeyPressEventHandler KeyPress {
81 			add { base.KeyPress += value; }
82 			remove { base.KeyPress -= value; }
83 		}
84 
85 		[Browsable(false)]
86 		[EditorBrowsable(EditorBrowsableState.Advanced)]
87 		public new event KeyEventHandler KeyUp {
88 			add { base.KeyUp += value; }
89 			remove { base.KeyUp -= value; }
90 		}
91 
92 		[Browsable (false)]
93 		[EditorBrowsable (EditorBrowsableState.Advanced)]
94 		public new event MouseEventHandler MouseClick {
95 			add { base.MouseClick += value; }
96 			remove { base.MouseClick -= value; }
97 		}
98 
99 		[Browsable (false)]
100 		[EditorBrowsable (EditorBrowsableState.Advanced)]
101 		public new event MouseEventHandler MouseDoubleClick {
102 			add { base.MouseDoubleClick += value; }
103 			remove { base.MouseDoubleClick -= value; }
104 		}
105 
106 		[Browsable(false)]
107 		[EditorBrowsable(EditorBrowsableState.Advanced)]
108 		public new event MouseEventHandler MouseDown {
109 			add { base.MouseDown += value; }
110 			remove { base.MouseDown -= value; }
111 		}
112 
113 		[Browsable(false)]
114 		[EditorBrowsable(EditorBrowsableState.Advanced)]
115 		public new event EventHandler MouseEnter {
116 			add { base.MouseEnter += value; }
117 			remove { base.MouseEnter -= value; }
118 		}
119 
120 		[Browsable(false)]
121 		[EditorBrowsable(EditorBrowsableState.Advanced)]
122 		public new event EventHandler MouseLeave {
123 			add { base.MouseLeave += value; }
124 			remove { base.MouseLeave -= value; }
125 		}
126 
127 		[Browsable(false)]
128 		[EditorBrowsable(EditorBrowsableState.Advanced)]
129 		public new event MouseEventHandler MouseMove {
130 			add { base.MouseMove += value; }
131 			remove { base.MouseMove -= value; }
132 		}
133 
134 		[Browsable(false)]
135 		[EditorBrowsable(EditorBrowsableState.Advanced)]
136 		public new event MouseEventHandler MouseUp {
137 			add { base.MouseUp += value; }
138 			remove { base.MouseUp -= value; }
139 		}
140 
141 		[Browsable(false)]
142 		[EditorBrowsable(EditorBrowsableState.Advanced)]
143 		public new event EventHandler TabStopChanged {
144 			add { base.TabStopChanged += value; }
145 			remove { base.TabStopChanged -= value; }
146 		}
147 		#endregion Events
148 
GroupBox()149 		public GroupBox ()
150 		{
151 			TabStop = false;
152 			flat_style = FlatStyle.Standard;
153 
154 			SetStyle(ControlStyles.ContainerControl | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
155 			SetStyle(ControlStyles.Selectable, false);
156 		}
157 
158 		#region Public Properties
159 		[Browsable(false)]
160 		[EditorBrowsable(EditorBrowsableState.Advanced)]
161 		public override bool AllowDrop {
162 			get { return base.AllowDrop;  }
163 			set { base.AllowDrop = value; }
164 		}
165 
166 		[Browsable (true)]
167 		[EditorBrowsable (EditorBrowsableState.Always)]
168 		[DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
169 		public override bool AutoSize {
170 			get { return base.AutoSize; }
171 			set { base.AutoSize = value; }
172 		}
173 
174 		[Browsable (true)]
175 		[DefaultValue (AutoSizeMode.GrowOnly)]
176 		[Localizable (true)]
177 		public AutoSizeMode AutoSizeMode {
178 			get { return base.GetAutoSizeMode (); }
179 			set { base.SetAutoSizeMode (value); }
180 		}
181 
182 		protected override CreateParams CreateParams {
183 			get { return base.CreateParams; }
184 		}
185 
186 		protected override Size DefaultSize {
187 			get { return ThemeEngine.Current.GroupBoxDefaultSize;}
188 		}
189 
190 		public override Rectangle DisplayRectangle {
191 			get {
192 				display_rectangle.X = Padding.Left;
193 				display_rectangle.Y = Font.Height + Padding.Top;
194 				display_rectangle.Width = Width - Padding.Horizontal;
195 				display_rectangle.Height = Height - Font.Height - Padding.Vertical;
196 				return display_rectangle;
197 			}
198 		}
199 
200 		[DefaultValue(FlatStyle.Standard)]
201 		public FlatStyle FlatStyle {
202 			get { return flat_style; }
203 			set {
204 				if (!Enum.IsDefined (typeof (FlatStyle), value))
205 					 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
206 
207 				if (flat_style == value)
208 					return;
209 
210 				flat_style = value;
211 				Refresh ();
212 			}
213 		}
214 
215 		[Browsable(false)]
216 		[EditorBrowsable(EditorBrowsableState.Advanced)]
217 		public new bool TabStop {
218 			get { return base.TabStop;  }
219 			set { base.TabStop = value; }
220 		}
221 
222 		[Localizable(true)]
223 		public override string Text {
224 			get { return base.Text; }
225 			set {
226 				if (base.Text == value)
227 					return;
228 
229 				base.Text = value;
230 				Refresh ();
231 			}
232 		}
233 
234 		#endregion //Public Properties
235 
236 		#region Public Methods
CreateAccessibilityInstance()237 		protected override AccessibleObject CreateAccessibilityInstance ()
238 		{
239 			return new GroupBoxAccessibleObject (this);
240 		}
241 
OnFontChanged(EventArgs e)242 		protected override void OnFontChanged (EventArgs e)
243 		{
244 			base.OnFontChanged (e);
245 			Refresh ();
246 		}
247 
OnPaint(PaintEventArgs e)248 		protected override void OnPaint (PaintEventArgs e)
249 		{
250 			ThemeEngine.Current.DrawGroupBox (e.Graphics, ClientRectangle, this);
251 			base.OnPaint(e);
252 		}
253 
ProcessMnemonic(char charCode)254 		protected override bool ProcessMnemonic (char charCode)
255 		{
256 			if (IsMnemonic(charCode, Text) == true) {
257 				// Select item next in line in tab order
258 				if (this.Parent != null) {
259 					Parent.SelectNextControl(this, true, false, true, false);
260 				}
261 				return true;
262 			}
263 
264 			return base.ProcessMnemonic (charCode);
265 		}
266 
ScaleControl(SizeF factor, BoundsSpecified specified)267 		protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
268 		{
269 			base.ScaleControl (factor, specified);
270 		}
271 
ToString()272 		public override string ToString()
273 		{
274 			return GetType ().FullName + ", Text: " + Text;
275 		}
276 
WndProc(ref Message m)277 		protected override void WndProc(ref Message m) {
278 			base.WndProc (ref m);
279 		}
280 
281 		#endregion Public Methods
282 
283 		[DefaultValue (false)]
284 		public bool UseCompatibleTextRendering {
285 			get {
286 				return use_compatible_text_rendering;
287 			}
288 
289 			set {
290 				if (use_compatible_text_rendering != value) {
291 					use_compatible_text_rendering = value;
292 					if (Parent != null)
293 						Parent.PerformLayout (this, "UseCompatibleTextRendering");
294 					Invalidate ();
295 				}
296 			}
297 		}
298 
299 		#region Protected Properties
300 		protected override Padding DefaultPadding {
301 			get { return new Padding (3); }
302 		}
303 		#endregion
304 
305 		#region Internal Methods
GetPreferredSizeCore(Size proposedSize)306 		internal override Size GetPreferredSizeCore (Size proposedSize)
307 		{
308 			Size retsize = new Size (Padding.Left, Padding.Top);
309 
310 			foreach (Control child in Controls) {
311 				if (child.Dock == DockStyle.Fill) {
312 					if (child.Bounds.Right > retsize.Width)
313 						retsize.Width = child.Bounds.Right;
314 				} else if (child.Dock != DockStyle.Top && child.Dock != DockStyle.Bottom && (child.Bounds.Right + child.Margin.Right) > retsize.Width)
315 					retsize.Width = child.Bounds.Right + child.Margin.Right;
316 
317 				if (child.Dock == DockStyle.Fill) {
318 					if (child.Bounds.Bottom > retsize.Height)
319 						retsize.Height = child.Bounds.Bottom;
320 				} else if (child.Dock != DockStyle.Left && child.Dock != DockStyle.Right && (child.Bounds.Bottom + child.Margin.Bottom) > retsize.Height)
321 					retsize.Height = child.Bounds.Bottom + child.Margin.Bottom;
322 			}
323 
324 			retsize.Width += Padding.Right;
325 			retsize.Height += Padding.Bottom;
326 
327 			retsize.Height += this.Font.Height;
328 
329 			return retsize;
330 		}
331 		#endregion
332 
333 		#region Private Classes
334 		private class GroupBoxAccessibleObject : Control.ControlAccessibleObject
335 		{
GroupBoxAccessibleObject(Control owner)336 			public GroupBoxAccessibleObject (Control owner) : base (owner)
337 			{
338 			}
339 		}
340 		#endregion
341 	}
342 }
343