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))]
37 [ClassInterface (ClassInterfaceType.AutoDispatch)]
39 public class UserControl : ContainerControl {
40 #region Public Constructors
41 public UserControl() {
42 SetStyle (ControlStyles.SupportsTransparentBackColor, true);
44 #endregion // Public Constructors
46 #region Public Instance Properties
48 [EditorBrowsable (EditorBrowsableState.Always)]
49 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
50 public override bool AutoSize {
51 get { return base.AutoSize; }
52 set { base.AutoSize = value; }
56 [LocalizableAttribute(true)]
57 [DefaultValue (AutoSizeMode.GrowOnly)]
58 public AutoSizeMode AutoSizeMode {
59 get { return base.GetAutoSizeMode (); }
61 if (base.GetAutoSizeMode () != value) {
62 base.SetAutoSizeMode (value);
68 [EditorBrowsable (EditorBrowsableState.Always)]
69 public override AutoValidate AutoValidate {
70 get { return base.AutoValidate; }
71 set { base.AutoValidate = value; }
74 protected override Size DefaultSize {
76 return new Size(150, 150);
82 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 public override string Text {
93 #endregion // Public Instance Properties
95 #region Public Instance Methods
97 [EditorBrowsable (EditorBrowsableState.Always)]
98 public override bool ValidateChildren ()
100 return base.ValidateChildren ();
104 [EditorBrowsable (EditorBrowsableState.Always)]
105 public override bool ValidateChildren (ValidationConstraints validationConstraints)
107 return base.ValidateChildren (validationConstraints);
111 #region Protected Instance Methods
112 [EditorBrowsable(EditorBrowsableState.Advanced)]
113 protected override void OnCreateControl() {
114 base.OnCreateControl();
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);
121 [EditorBrowsable(EditorBrowsableState.Advanced)]
122 protected virtual void OnLoad(EventArgs e) {
123 EventHandler eh = (EventHandler)(Events [LoadEvent]);
128 [EditorBrowsable(EditorBrowsableState.Advanced)]
129 protected override void OnMouseDown(MouseEventArgs e) {
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);
142 base.WndProc (ref m);
146 #endregion // Protected Instance Methods
148 #region Protected Properties
149 protected override CreateParams CreateParams {
151 CreateParams cp = base.CreateParams;
152 cp.Style |= (int)WindowStyles.WS_TABSTOP;
153 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTROLPARENT;
160 static object LoadEvent = new object ();
163 [EditorBrowsable (EditorBrowsableState.Always)]
164 public new event EventHandler AutoSizeChanged {
165 add { base.AutoSizeChanged += value; }
166 remove { base.AutoSizeChanged -= value; }
170 [EditorBrowsable (EditorBrowsableState.Always)]
171 public new event EventHandler AutoValidateChanged {
172 add { base.AutoValidateChanged += value; }
173 remove { base.AutoValidateChanged -= value; }
176 public event EventHandler Load {
177 add { Events.AddHandler (LoadEvent, value); }
178 remove { Events.RemoveHandler (LoadEvent, value); }
182 [EditorBrowsable(EditorBrowsableState.Never)]
183 public new event EventHandler TextChanged {
184 add { base.TextChanged += value; }
185 remove { base.TextChanged -= value; }
189 protected override void OnResize (EventArgs e)
195 [DefaultValue (BorderStyle.None)]
196 [EditorBrowsable (EditorBrowsableState.Always)]
197 public BorderStyle BorderStyle {
198 get { return InternalBorderStyle; }
199 set { InternalBorderStyle = value; }
202 internal override Size GetPreferredSizeCore (Size proposedSize)
204 Size retsize = Size.Empty;
206 // Add up the requested sizes for Docked controls
207 foreach (Control child in Controls) {
208 if (!child.is_visible)
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;
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)
222 if (child.Dock != DockStyle.None)
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)
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);