2007-08-28 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / UserControl.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25
26 using System;
27 using System.ComponentModel;
28 using System.ComponentModel.Design;
29 using System.Drawing;
30 using System.Runtime.InteropServices;
31
32 namespace System.Windows.Forms {
33         [DefaultEvent("Load")]
34         [DesignerCategory("UserControl")]
35         [Designer("System.Windows.Forms.Design.ControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
36         [Designer("System.Windows.Forms.Design.UserControlDocumentDesigner, " + Consts.AssemblySystem_Design, typeof(IRootDesigner))]
37 #if NET_2_0
38         [ClassInterface (ClassInterfaceType.AutoDispatch)]
39         [ComVisible (true)]
40 #endif
41         public class UserControl : ContainerControl {
42                 #region Public Constructors
43                 public UserControl() {
44 #if NET_2_0
45                         SetStyle (ControlStyles.SupportsTransparentBackColor, true);
46 #endif
47                 }
48                 #endregion      // Public Constructors
49
50                 #region Public Instance Properties
51 #if NET_2_0
52                 [Browsable (true)]
53                 [EditorBrowsable (EditorBrowsableState.Always)]
54                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
55                 public override bool AutoSize {
56                         get { return base.AutoSize; }
57                         set { base.AutoSize = value; }
58                 }
59                 
60                 [Browsable (true)]
61                 [LocalizableAttribute(true)] 
62                 [DefaultValue (AutoSizeMode.GrowOnly)]
63                 public AutoSizeMode AutoSizeMode {
64                         get { return base.GetAutoSizeMode (); } 
65                         set {
66                                 if (base.GetAutoSizeMode () != value) {
67                                         base.SetAutoSizeMode (value);
68                                 }
69                         } 
70                 }
71 #endif
72                 protected override Size DefaultSize {
73                         get {
74                                 return new Size(150, 150);
75                         }
76                 }
77
78
79                 [Bindable(false)]
80                 [Browsable(false)]
81                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
82                 [EditorBrowsable(EditorBrowsableState.Never)]
83                 public override string Text {
84                         get {
85                                 return base.Text;
86                         }
87
88                         set {
89                                 base.Text = value;
90                         }
91                 }
92                 #endregion      // Public Instance Properties
93
94                 #region Protected Instance Methods
95                 [EditorBrowsable(EditorBrowsableState.Advanced)]
96                 protected override void OnCreateControl() {
97                         base.OnCreateControl();
98
99                         // The OnCreateControl isn't neccessarily raised *before* it
100                         // becomes first visible, but that's the best we've got
101                         OnLoad(EventArgs.Empty);
102                 }
103
104                 [EditorBrowsable(EditorBrowsableState.Advanced)]
105                 protected virtual void OnLoad(EventArgs e) {
106                         EventHandler eh = (EventHandler)(Events [LoadEvent]);
107                         if (eh != null)
108                                 eh (this, e);
109                 }
110
111                 [EditorBrowsable(EditorBrowsableState.Advanced)]
112                 protected override void OnMouseDown(MouseEventArgs e) {
113                         base.OnMouseDown(e);
114                 }
115
116                 [EditorBrowsable(EditorBrowsableState.Advanced)]
117                 protected override void WndProc(ref Message m) {
118                         base.WndProc(ref m);
119                 }
120                 #endregion      // Protected Instance Methods
121
122                 #region Protected Properties
123 #if NET_2_0
124                 protected override CreateParams CreateParams {
125                         get { 
126                                 CreateParams cp = base.CreateParams;
127                                 cp.Style |= (int)WindowStyles.WS_TABSTOP;
128                                 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTROLPARENT;
129                                 return cp;
130                         }
131                 }
132 #endif
133                 #endregion
134                 #region Events
135                 static object LoadEvent = new object ();
136
137 #if NET_2_0
138                 [Browsable (true)]
139                 [EditorBrowsable (EditorBrowsableState.Always)]
140                 public new event EventHandler AutoSizeChanged {
141                         add { base.AutoSizeChanged += value; }
142                         remove { base.AutoSizeChanged -= value; }
143                 }
144 #endif
145
146                 public event EventHandler Load {
147                         add { Events.AddHandler (LoadEvent, value); }
148                         remove { Events.RemoveHandler (LoadEvent, value); }
149                 }
150
151                 [Browsable(false)]
152                 [EditorBrowsable(EditorBrowsableState.Never)]
153                 public new event EventHandler TextChanged {
154                         add { base.TextChanged += value; }
155                         remove { base.TextChanged -= value; }
156                 }
157
158                 #endregion      // Events
159
160 #if NET_2_0
161                 protected override void OnResize (EventArgs e)
162                 {
163                         base.OnResize (e);
164                 }
165
166                 [Browsable (true)]
167                 [DefaultValue (BorderStyle.None)]
168                 [EditorBrowsable (EditorBrowsableState.Always)]
169                 public BorderStyle BorderStyle {
170                         get { return InternalBorderStyle; }
171                         set { InternalBorderStyle = value; }
172                 }
173
174                 internal override Size GetPreferredSizeCore (Size proposedSize)
175                 {
176                         Size retsize = Size.Empty;
177
178                         foreach (Control child in Controls) {
179                                 if (child.Dock == DockStyle.Fill) {
180                                         if (child.Bounds.Right > retsize.Width)
181                                                 retsize.Width = child.Bounds.Right;
182                                 } else if (child.Dock != DockStyle.Top && child.Dock != DockStyle.Bottom && (child.Anchor & AnchorStyles.Right) == 0 && (child.Bounds.Right + child.Margin.Right) > retsize.Width)
183                                         retsize.Width = child.Bounds.Right + child.Margin.Right;
184
185                                 if (child.Dock == DockStyle.Fill) {
186                                         if (child.Bounds.Bottom > retsize.Height)
187                                                 retsize.Height = child.Bounds.Bottom;
188                                 } else if (child.Dock != DockStyle.Left && child.Dock != DockStyle.Right && (child.Anchor & AnchorStyles.Bottom) == 0 && (child.Bounds.Bottom + child.Margin.Bottom) > retsize.Height)
189                                         retsize.Height = child.Bounds.Bottom + child.Margin.Bottom;
190                         }
191
192                         return retsize;
193                 }
194 #endif
195         }
196 }