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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
27 using System.ComponentModel;
28 using System.ComponentModel.Design;
30 using System.Runtime.InteropServices;
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))]
38 [ClassInterface (ClassInterfaceType.AutoDispatch)]
41 public class UserControl : ContainerControl {
42 #region Public Constructors
43 public UserControl() {
45 SetStyle (ControlStyles.SupportsTransparentBackColor, true);
48 #endregion // Public Constructors
50 #region Public Instance Properties
53 [EditorBrowsable (EditorBrowsableState.Always)]
54 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
55 public override bool AutoSize {
56 get { return base.AutoSize; }
57 set { base.AutoSize = value; }
61 [LocalizableAttribute(true)]
62 [DefaultValue (AutoSizeMode.GrowOnly)]
63 public AutoSizeMode AutoSizeMode {
64 get { return base.GetAutoSizeMode (); }
66 if (base.GetAutoSizeMode () != value) {
67 base.SetAutoSizeMode (value);
73 [EditorBrowsable (EditorBrowsableState.Always)]
74 public override AutoValidate AutoValidate {
75 get { return base.AutoValidate; }
76 set { base.AutoValidate = value; }
79 protected override Size DefaultSize {
81 return new Size(150, 150);
88 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
89 [EditorBrowsable(EditorBrowsableState.Never)]
90 public override string Text {
99 #endregion // Public Instance Properties
101 #region Public Instance Methods
104 [EditorBrowsable (EditorBrowsableState.Always)]
105 public override bool ValidateChildren ()
107 return base.ValidateChildren ();
111 [EditorBrowsable (EditorBrowsableState.Always)]
112 public override bool ValidateChildren (ValidationConstraints validationConstraints)
114 return base.ValidateChildren (validationConstraints);
119 #region Protected Instance Methods
120 [EditorBrowsable(EditorBrowsableState.Advanced)]
121 protected override void OnCreateControl() {
122 base.OnCreateControl();
124 // The OnCreateControl isn't neccessarily raised *before* it
125 // becomes first visible, but that's the best we've got
126 OnLoad(EventArgs.Empty);
129 [EditorBrowsable(EditorBrowsableState.Advanced)]
130 protected virtual void OnLoad(EventArgs e) {
131 EventHandler eh = (EventHandler)(Events [LoadEvent]);
136 [EditorBrowsable(EditorBrowsableState.Advanced)]
137 protected override void OnMouseDown(MouseEventArgs e) {
141 [EditorBrowsable(EditorBrowsableState.Advanced)]
142 protected override void WndProc(ref Message m) {
143 switch ((Msg) m.Msg) {
144 case Msg.WM_SETFOCUS:
145 if (ActiveControl == null)
146 SelectNextControl (null, true, true, true, false);
147 base.WndProc (ref m);
150 base.WndProc (ref m);
154 #endregion // Protected Instance Methods
156 #region Protected Properties
158 protected override CreateParams CreateParams {
160 CreateParams cp = base.CreateParams;
161 cp.Style |= (int)WindowStyles.WS_TABSTOP;
162 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTROLPARENT;
169 static object LoadEvent = new object ();
173 [EditorBrowsable (EditorBrowsableState.Always)]
174 public new event EventHandler AutoSizeChanged {
175 add { base.AutoSizeChanged += value; }
176 remove { base.AutoSizeChanged -= value; }
180 [EditorBrowsable (EditorBrowsableState.Always)]
181 public new event EventHandler AutoValidateChanged {
182 add { base.AutoValidateChanged += value; }
183 remove { base.AutoValidateChanged -= value; }
187 public event EventHandler Load {
188 add { Events.AddHandler (LoadEvent, value); }
189 remove { Events.RemoveHandler (LoadEvent, value); }
193 [EditorBrowsable(EditorBrowsableState.Never)]
194 public new event EventHandler TextChanged {
195 add { base.TextChanged += value; }
196 remove { base.TextChanged -= value; }
202 protected override void OnResize (EventArgs e)
208 [DefaultValue (BorderStyle.None)]
209 [EditorBrowsable (EditorBrowsableState.Always)]
210 public BorderStyle BorderStyle {
211 get { return InternalBorderStyle; }
212 set { InternalBorderStyle = value; }
215 internal override Size GetPreferredSizeCore (Size proposedSize)
217 Size retsize = Size.Empty;
219 // Add up the requested sizes for Docked controls
220 foreach (Control child in Controls) {
221 if (!child.is_visible)
224 if (child.Dock == DockStyle.Left || child.Dock == DockStyle.Right)
225 retsize.Width += child.PreferredSize.Width;
226 else if (child.Dock == DockStyle.Top || child.Dock == DockStyle.Bottom)
227 retsize.Height += child.PreferredSize.Height;
230 // See if any non-Docked control is positioned lower or more right than our size
231 foreach (Control child in Controls) {
232 if (!child.is_visible)
235 if (child.Dock != DockStyle.None)
238 // If its anchored to the bottom or right, that doesn't really count
239 if ((child.Anchor & AnchorStyles.Bottom) == AnchorStyles.Bottom || (child.Anchor & AnchorStyles.Right) == AnchorStyles.Right)
242 retsize.Width = Math.Max (retsize.Width, child.Bounds.Right + child.Margin.Right);
243 retsize.Height = Math.Max (retsize.Height, child.Bounds.Bottom + child.Margin.Bottom);