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);
72 protected override Size DefaultSize {
74 return new Size(150, 150);
81 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
82 [EditorBrowsable(EditorBrowsableState.Never)]
83 public override string Text {
92 #endregion // Public Instance Properties
94 #region Protected Instance Methods
95 [EditorBrowsable(EditorBrowsableState.Advanced)]
96 protected override void OnCreateControl() {
97 base.OnCreateControl();
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);
104 [EditorBrowsable(EditorBrowsableState.Advanced)]
105 protected virtual void OnLoad(EventArgs e) {
106 EventHandler eh = (EventHandler)(Events [LoadEvent]);
111 [EditorBrowsable(EditorBrowsableState.Advanced)]
112 protected override void OnMouseDown(MouseEventArgs e) {
116 [EditorBrowsable(EditorBrowsableState.Advanced)]
117 protected override void WndProc(ref Message m) {
120 #endregion // Protected Instance Methods
122 #region Protected Properties
124 protected override CreateParams CreateParams {
126 CreateParams cp = base.CreateParams;
127 cp.Style |= (int)WindowStyles.WS_TABSTOP;
128 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTROLPARENT;
135 static object LoadEvent = new object ();
139 [EditorBrowsable (EditorBrowsableState.Always)]
140 public new event EventHandler AutoSizeChanged {
141 add { base.AutoSizeChanged += value; }
142 remove { base.AutoSizeChanged -= value; }
146 public event EventHandler Load {
147 add { Events.AddHandler (LoadEvent, value); }
148 remove { Events.RemoveHandler (LoadEvent, value); }
152 [EditorBrowsable(EditorBrowsableState.Never)]
153 public new event EventHandler TextChanged {
154 add { base.TextChanged += value; }
155 remove { base.TextChanged -= value; }
161 protected override void OnResize (EventArgs e)
167 [DefaultValue (BorderStyle.None)]
168 [EditorBrowsable (EditorBrowsableState.Always)]
169 public BorderStyle BorderStyle {
170 get { return InternalBorderStyle; }
171 set { InternalBorderStyle = value; }
174 internal override Size GetPreferredSizeCore (Size proposedSize)
176 Size retsize = Size.Empty;
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;
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;