Merge pull request #3018 from ludovic-henry/coop-marshal-attach
[mono.git] / mcs / class / System.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         [ClassInterface (ClassInterfaceType.AutoDispatch)]
38         [ComVisible (true)]
39         public class UserControl : ContainerControl {
40                 #region Public Constructors
41                 public UserControl() {
42                         SetStyle (ControlStyles.SupportsTransparentBackColor, true);
43                 }
44                 #endregion      // Public Constructors
45
46                 #region Public Instance Properties
47                 [Browsable (true)]
48                 [EditorBrowsable (EditorBrowsableState.Always)]
49                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
50                 public override bool AutoSize {
51                         get { return base.AutoSize; }
52                         set { base.AutoSize = value; }
53                 }
54                 
55                 [Browsable (true)]
56                 [LocalizableAttribute(true)] 
57                 [DefaultValue (AutoSizeMode.GrowOnly)]
58                 public AutoSizeMode AutoSizeMode {
59                         get { return base.GetAutoSizeMode (); } 
60                         set {
61                                 if (base.GetAutoSizeMode () != value) {
62                                         base.SetAutoSizeMode (value);
63                                 }
64                         } 
65                 }
66
67                 [Browsable (true)]
68                 [EditorBrowsable (EditorBrowsableState.Always)]
69                 public override AutoValidate AutoValidate {
70                         get { return base.AutoValidate; }
71                         set { base.AutoValidate = value; }
72                 }
73
74                 protected override Size DefaultSize {
75                         get {
76                                 return new Size(150, 150);
77                         }
78                 }
79
80                 [Bindable(false)]
81                 [Browsable(false)]
82                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
83                 [EditorBrowsable(EditorBrowsableState.Never)]
84                 public override string Text {
85                         get {
86                                 return base.Text;
87                         }
88
89                         set {
90                                 base.Text = value;
91                         }
92                 }
93                 #endregion      // Public Instance Properties
94
95                 #region Public Instance Methods
96                 [Browsable (true)]
97                 [EditorBrowsable (EditorBrowsableState.Always)]
98                 public override bool ValidateChildren ()
99                 {
100                         return base.ValidateChildren ();
101                 }
102
103                 [Browsable (true)]
104                 [EditorBrowsable (EditorBrowsableState.Always)]
105                 public override bool ValidateChildren (ValidationConstraints validationConstraints)
106                 {
107                         return base.ValidateChildren (validationConstraints);
108                 }
109                 #endregion
110                 
111                 #region Protected Instance Methods
112                 [EditorBrowsable(EditorBrowsableState.Advanced)]
113                 protected override void OnCreateControl() {
114                         base.OnCreateControl();
115
116                         // The OnCreateControl isn't neccessarily raised *before* it
117                         // becomes first visible, but that's the best we've got
118                         OnLoad(EventArgs.Empty);
119                 }
120
121                 [EditorBrowsable(EditorBrowsableState.Advanced)]
122                 protected virtual void OnLoad(EventArgs e) {
123                         EventHandler eh = (EventHandler)(Events [LoadEvent]);
124                         if (eh != null)
125                                 eh (this, e);
126                 }
127
128                 [EditorBrowsable(EditorBrowsableState.Advanced)]
129                 protected override void OnMouseDown(MouseEventArgs e) {
130                         base.OnMouseDown(e);
131                 }
132
133                 [EditorBrowsable(EditorBrowsableState.Advanced)]
134                 protected override void WndProc(ref Message m) {
135                         switch ((Msg) m.Msg) {
136                                 case Msg.WM_SETFOCUS:
137                                         if (ActiveControl == null)
138                                                 SelectNextControl (null, true, true, true, false);
139                                         base.WndProc (ref m);
140                                         break;
141                                 default:
142                                         base.WndProc (ref m);
143                                         break;
144                         }
145                 }
146                 #endregion      // Protected Instance Methods
147
148                 #region Protected Properties
149                 protected override CreateParams CreateParams {
150                         get { 
151                                 CreateParams cp = base.CreateParams;
152                                 cp.Style |= (int)WindowStyles.WS_TABSTOP;
153                                 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTROLPARENT;
154                                 return cp;
155                         }
156                 }
157                 #endregion
158
159                 #region Events
160                 static object LoadEvent = new object ();
161
162                 [Browsable (true)]
163                 [EditorBrowsable (EditorBrowsableState.Always)]
164                 public new event EventHandler AutoSizeChanged {
165                         add { base.AutoSizeChanged += value; }
166                         remove { base.AutoSizeChanged -= value; }
167                 }
168
169                 [Browsable (true)]
170                 [EditorBrowsable (EditorBrowsableState.Always)]
171                 public new event EventHandler AutoValidateChanged {
172                         add { base.AutoValidateChanged += value; }
173                         remove { base.AutoValidateChanged -= value; }
174                 }
175
176                 public event EventHandler Load {
177                         add { Events.AddHandler (LoadEvent, value); }
178                         remove { Events.RemoveHandler (LoadEvent, value); }
179                 }
180
181                 [Browsable(false)]
182                 [EditorBrowsable(EditorBrowsableState.Never)]
183                 public new event EventHandler TextChanged {
184                         add { base.TextChanged += value; }
185                         remove { base.TextChanged -= value; }
186                 }
187                 #endregion      // Events
188
189                 protected override void OnResize (EventArgs e)
190                 {
191                         base.OnResize (e);
192                 }
193
194                 [Browsable (true)]
195                 [DefaultValue (BorderStyle.None)]
196                 [EditorBrowsable (EditorBrowsableState.Always)]
197                 public BorderStyle BorderStyle {
198                         get { return InternalBorderStyle; }
199                         set { InternalBorderStyle = value; }
200                 }
201
202                 internal override Size GetPreferredSizeCore (Size proposedSize)
203                 {
204                         Size retsize = Size.Empty;
205
206                         // Add up the requested sizes for Docked controls
207                         foreach (Control child in Controls) {
208                                 if (!child.is_visible)
209                                         continue;
210                                         
211                                 if (child.Dock == DockStyle.Left || child.Dock == DockStyle.Right)
212                                         retsize.Width += child.PreferredSize.Width;
213                                 else if (child.Dock == DockStyle.Top || child.Dock == DockStyle.Bottom)
214                                         retsize.Height += child.PreferredSize.Height;   
215                         }
216                         
217                         // See if any non-Docked control is positioned lower or more right than our size
218                         foreach (Control child in Controls) {
219                                 if (!child.is_visible)
220                                         continue;
221
222                                 if (child.Dock != DockStyle.None)
223                                         continue;
224                                         
225                                 // If its anchored to the bottom or right, that doesn't really count
226                                 if ((child.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom || (child.Anchor & AnchorStyles.Right) == AnchorStyles.Right)
227                                         continue;
228                                         
229                                 retsize.Width = Math.Max (retsize.Width, child.Bounds.Right + child.Margin.Right);
230                                 retsize.Height = Math.Max (retsize.Height, child.Bounds.Bottom + child.Margin.Bottom);
231                         }
232
233                         return retsize;
234                 }
235         }
236 }