* TabControl.cs: Show the tooltip depending on the value
[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
72                 [Browsable (true)]
73                 [EditorBrowsable (EditorBrowsableState.Always)]
74                 public override AutoValidate AutoValidate {
75                         get { return base.AutoValidate; }
76                         set { base.AutoValidate = value; }
77                 }
78 #endif
79                 protected override Size DefaultSize {
80                         get {
81                                 return new Size(150, 150);
82                         }
83                 }
84
85
86                 [Bindable(false)]
87                 [Browsable(false)]
88                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
89                 [EditorBrowsable(EditorBrowsableState.Never)]
90                 public override string Text {
91                         get {
92                                 return base.Text;
93                         }
94
95                         set {
96                                 base.Text = value;
97                         }
98                 }
99                 #endregion      // Public Instance Properties
100
101                 #region Public Instance Methods
102 #if NET_2_0
103                 [Browsable (true)]
104                 [EditorBrowsable (EditorBrowsableState.Always)]
105                 public override bool ValidateChildren ()
106                 {
107                         return base.ValidateChildren ();
108                 }
109
110                 [Browsable (true)]
111                 [EditorBrowsable (EditorBrowsableState.Always)]
112                 public override bool ValidateChildren (ValidationConstraints validationConstraints)
113                 {
114                         return base.ValidateChildren (validationConstraints);
115                 }
116 #endif
117                 #endregion
118                 
119                 #region Protected Instance Methods
120                 [EditorBrowsable(EditorBrowsableState.Advanced)]
121                 protected override void OnCreateControl() {
122                         base.OnCreateControl();
123
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);
127                 }
128
129                 [EditorBrowsable(EditorBrowsableState.Advanced)]
130                 protected virtual void OnLoad(EventArgs e) {
131                         EventHandler eh = (EventHandler)(Events [LoadEvent]);
132                         if (eh != null)
133                                 eh (this, e);
134                 }
135
136                 [EditorBrowsable(EditorBrowsableState.Advanced)]
137                 protected override void OnMouseDown(MouseEventArgs e) {
138                         base.OnMouseDown(e);
139                 }
140
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);
148                                         break;
149                                 default:
150                                         base.WndProc (ref m);
151                                         break;
152                         }
153                 }
154                 #endregion      // Protected Instance Methods
155
156                 #region Protected Properties
157 #if NET_2_0
158                 protected override CreateParams CreateParams {
159                         get { 
160                                 CreateParams cp = base.CreateParams;
161                                 cp.Style |= (int)WindowStyles.WS_TABSTOP;
162                                 cp.ExStyle |= (int)WindowExStyles.WS_EX_CONTROLPARENT;
163                                 return cp;
164                         }
165                 }
166 #endif
167                 #endregion
168                 #region Events
169                 static object LoadEvent = new object ();
170
171 #if NET_2_0
172                 [Browsable (true)]
173                 [EditorBrowsable (EditorBrowsableState.Always)]
174                 public new event EventHandler AutoSizeChanged {
175                         add { base.AutoSizeChanged += value; }
176                         remove { base.AutoSizeChanged -= value; }
177                 }
178
179                 [Browsable (true)]
180                 [EditorBrowsable (EditorBrowsableState.Always)]
181                 public new event EventHandler AutoValidateChanged {
182                         add { base.AutoValidateChanged += value; }
183                         remove { base.AutoValidateChanged -= value; }
184                 }
185 #endif
186
187                 public event EventHandler Load {
188                         add { Events.AddHandler (LoadEvent, value); }
189                         remove { Events.RemoveHandler (LoadEvent, value); }
190                 }
191
192                 [Browsable(false)]
193                 [EditorBrowsable(EditorBrowsableState.Never)]
194                 public new event EventHandler TextChanged {
195                         add { base.TextChanged += value; }
196                         remove { base.TextChanged -= value; }
197                 }
198
199                 #endregion      // Events
200
201 #if NET_2_0
202                 protected override void OnResize (EventArgs e)
203                 {
204                         base.OnResize (e);
205                 }
206
207                 [Browsable (true)]
208                 [DefaultValue (BorderStyle.None)]
209                 [EditorBrowsable (EditorBrowsableState.Always)]
210                 public BorderStyle BorderStyle {
211                         get { return InternalBorderStyle; }
212                         set { InternalBorderStyle = value; }
213                 }
214
215                 internal override Size GetPreferredSizeCore (Size proposedSize)
216                 {
217                         Size retsize = Size.Empty;
218
219                         // Add up the requested sizes for Docked controls
220                         foreach (Control child in Controls) {
221                                 if (!child.is_visible)
222                                         continue;
223                                         
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;   
228                         }
229                         
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)
233                                         continue;
234
235                                 if (child.Dock != DockStyle.None)
236                                         continue;
237                                         
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)
240                                         continue;
241                                         
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);
244                         }
245
246                         return retsize;
247                 }
248 #endif
249         }
250 }