* Test/System.Windows.Forms/DataGridViewCellTest.cs: Added
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / GroupBox.cs
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 #if NET_2_0
43         [ClassInterface (ClassInterfaceType.AutoDispatch)]
44         [ComVisible (true)]
45 #endif
46         public class GroupBox : Control
47         {
48                 private FlatStyle flat_style;
49                 private Rectangle display_rectangle = new Rectangle ();
50
51                 #region Events
52 #if NET_2_0
53                 [Browsable (true)]
54                 [EditorBrowsable (EditorBrowsableState.Always)]
55                 public new event EventHandler AutoSizeChanged {
56                         add { base.AutoSizeChanged += value; }
57                         remove { base.AutoSizeChanged -= value; }
58                 }
59 #endif
60
61                 [Browsable (false)]
62                 [EditorBrowsable(EditorBrowsableState.Advanced)]
63                 public new event EventHandler Click {
64                         add { base.Click += value; }
65                         remove { base.Click -= value; }
66                 }
67
68                 [Browsable(false)]
69                 [EditorBrowsable(EditorBrowsableState.Advanced)]
70                 public new event EventHandler DoubleClick {
71                         add { base.DoubleClick += value; }
72                         remove { base.DoubleClick -= value; }
73                 }
74
75                 [Browsable(false)]
76                 [EditorBrowsable(EditorBrowsableState.Advanced)]
77                 public new event KeyEventHandler KeyDown {
78                         add { base.KeyDown += value; }
79                         remove { base.KeyDown -= value; }
80                 }
81
82                 [Browsable(false)]
83                 [EditorBrowsable(EditorBrowsableState.Advanced)]
84                 public new event KeyPressEventHandler KeyPress {
85                         add { base.KeyPress += value; }
86                         remove { base.KeyPress -= value; }
87                 }
88
89                 [Browsable(false)]
90                 [EditorBrowsable(EditorBrowsableState.Advanced)]
91                 public new event KeyEventHandler KeyUp {
92                         add { base.KeyUp += value; }
93                         remove { base.KeyUp -= value; }
94                 }
95
96 #if NET_2_0
97                 [Browsable (false)]
98                 [EditorBrowsable (EditorBrowsableState.Advanced)]
99                 public new event MouseEventHandler MouseClick {
100                         add { base.MouseClick += value; }
101                         remove { base.MouseClick -= value; }
102                 }
103
104                 [Browsable (false)]
105                 [EditorBrowsable (EditorBrowsableState.Advanced)]
106                 public new event MouseEventHandler MouseDoubleClick {
107                         add { base.MouseDoubleClick += value; }
108                         remove { base.MouseDoubleClick -= value; }
109                 }
110 #endif
111
112                 [Browsable(false)]
113                 [EditorBrowsable(EditorBrowsableState.Advanced)]
114                 public new event MouseEventHandler MouseDown {
115                         add { base.MouseDown += value; }
116                         remove { base.MouseDown -= value; }
117                 }
118
119                 [Browsable(false)]
120                 [EditorBrowsable(EditorBrowsableState.Advanced)]
121                 public new event EventHandler MouseEnter {
122                         add { base.MouseEnter += value; }
123                         remove { base.MouseEnter -= value; }
124                 }
125
126                 [Browsable(false)]
127                 [EditorBrowsable(EditorBrowsableState.Advanced)]
128                 public new event EventHandler MouseLeave {
129                         add { base.MouseLeave += value; }
130                         remove { base.MouseLeave -= value; }
131                 }
132
133                 [Browsable(false)]
134                 [EditorBrowsable(EditorBrowsableState.Advanced)]
135                 public new event MouseEventHandler MouseMove {
136                         add { base.MouseMove += value; }
137                         remove { base.MouseMove -= value; }
138                 }
139
140                 [Browsable(false)]
141                 [EditorBrowsable(EditorBrowsableState.Advanced)]
142                 public new event MouseEventHandler MouseUp {
143                         add { base.MouseUp += value; }
144                         remove { base.MouseUp -= value; }
145                 }
146
147                 [Browsable(false)]
148                 [EditorBrowsable(EditorBrowsableState.Advanced)]
149                 public new event EventHandler TabStopChanged {
150                         add { base.TabStopChanged += value; }
151                         remove { base.TabStopChanged -= value; }
152                 }
153                 #endregion Events
154
155                 public GroupBox ()
156                 {
157                         TabStop = false;
158                         flat_style = FlatStyle.Standard;
159
160                         SetStyle(ControlStyles.ContainerControl | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
161                         SetStyle(ControlStyles.Selectable, false);
162                 }
163
164                 #region Public Properties
165                 [Browsable(false)]
166                 [EditorBrowsable(EditorBrowsableState.Advanced)]
167                 public override bool AllowDrop {
168                         get { return base.AllowDrop;  }
169                         set { base.AllowDrop = value; }
170                 }
171
172 #if NET_2_0
173                 [Browsable (true)]
174                 [EditorBrowsable (EditorBrowsableState.Always)]
175                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
176                 public override bool AutoSize {
177                         get { return base.AutoSize; }
178                         set { base.AutoSize = value; }
179                 }
180
181                 [Browsable (true)]
182                 [DefaultValue (AutoSizeMode.GrowOnly)]
183                 [Localizable (true)]
184                 public AutoSizeMode AutoSizeMode {
185                         get { return base.GetAutoSizeMode (); }
186                         set { base.SetAutoSizeMode (value); }
187                 }
188 #endif
189
190                 protected override CreateParams CreateParams {
191                         get { return base.CreateParams; }
192                 }
193
194                 protected override Size DefaultSize {
195                         get { return ThemeEngine.Current.GroupBoxDefaultSize;}
196                 }
197
198                 public override Rectangle DisplayRectangle {
199                         get {
200 #if NET_2_0
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;
206 #else
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;
212 #endif
213                         }
214                 }
215
216                 [DefaultValue(FlatStyle.Standard)]
217                 public FlatStyle FlatStyle {
218                         get { return flat_style; }
219                         set {
220                                 if (!Enum.IsDefined (typeof (FlatStyle), value))
221                                          new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
222
223                                 if (flat_style == value)
224                                         return;
225                                         
226                                 flat_style = value;
227                                 Refresh ();
228                         }
229                 }
230
231                 [Browsable(false)]
232                 [EditorBrowsable(EditorBrowsableState.Advanced)]
233                 public new bool TabStop {
234                         get { return base.TabStop;  }
235                         set { base.TabStop = value; }
236                 }
237
238                 [Localizable(true)]
239                 public override string Text {
240                         get { return base.Text; }
241                         set {
242                                 if (base.Text == value)
243                                         return;
244
245                                 base.Text = value;
246                                 Refresh ();
247                         }
248                 }
249
250                 #endregion //Public Properties
251
252                 #region Public Methods
253 #if NET_2_0
254                 protected override AccessibleObject CreateAccessibilityInstance ()
255                 {
256                         return new GroupBoxAccessibleObject (this);
257                 }
258 #endif
259                 
260                 protected override void OnFontChanged (EventArgs e)
261                 {
262                         base.OnFontChanged (e);
263                         Refresh ();
264                 }
265
266                 protected override void OnPaint (PaintEventArgs pevent)
267                 {
268                         ThemeEngine.Current.DrawGroupBox (pevent.Graphics, ClientRectangle, this);
269                         base.OnPaint(pevent);
270                 }
271
272                 protected override bool ProcessMnemonic (char charCode)
273                 {
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);
278                                 }
279                                 return true;
280                         }
281                         
282                         return base.ProcessMnemonic (charCode);
283                 }
284
285 #if NET_2_0
286                 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
287                 {
288                         base.ScaleControl (factor, specified);
289                 }
290 #endif
291
292                 public override string ToString()
293                 {
294                         return GetType ().FullName.ToString () + ", Text: " + Text;
295                 }
296
297                 protected override void WndProc(ref Message m) {
298                         base.WndProc (ref m);
299                 }
300                                 
301                 #endregion Public Methods
302 #if NET_2_0
303                 [DefaultValue (false)]
304                 public bool UseCompatibleTextRendering {
305                         get {
306                                 return use_compatible_text_rendering;
307                         }
308
309                         set {
310                                 use_compatible_text_rendering = value;
311                         }
312                 }
313 #endif
314
315                 #region Protected Properties
316 #if NET_2_0
317                 protected override Padding DefaultPadding {
318                         get { return new Padding (3); }
319                 }
320 #endif
321                 #endregion
322
323                 #region Internal Methods
324 #if NET_2_0
325                 internal override Size GetPreferredSizeCore (Size proposedSize)
326                 {
327                         Size retsize = new Size (Padding.Left, Padding.Top);
328
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;
335
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;
341                         }
342
343                         retsize.Width += Padding.Right;
344                         retsize.Height += Padding.Bottom;
345                         
346                         retsize.Height += this.Font.Height;
347                         
348                         return retsize;
349                 }
350 #endif
351                 #endregion
352
353                 #region Private Classes
354                 private class GroupBoxAccessibleObject : Control.ControlAccessibleObject
355                 {
356                         public GroupBoxAccessibleObject (Control owner) : base (owner)
357                         {
358                         }
359                 }
360                 #endregion
361         }
362 }