New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripControlHost.cs
1 //
2 // ToolStripControlHost.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28 #if NET_2_0
29 using System.Drawing;
30 using System.ComponentModel;
31
32 namespace System.Windows.Forms
33 {
34         public class ToolStripControlHost : ToolStripItem
35         {
36                 private Control control;
37                 private ContentAlignment control_align;
38                 private bool double_click_enabled;
39                 private ContentAlignment image_align;
40                 private ToolStripItemImageScaling image_scaling;
41                 private Color image_transparent_color;
42                 private ContentAlignment text_align;
43                 private TextImageRelation text_image_relation;
44
45                 #region Public Constructors
46                 public ToolStripControlHost (Control c) : base ()
47                 {
48                         if (c == null)
49                                 throw new ArgumentNullException ("c");
50
51                         this.control = c;
52                         this.control_align = ContentAlignment.MiddleCenter;
53                         this.OnSubscribeControlEvents (this.control);
54                 }
55
56                 public ToolStripControlHost (Control c, string name) : this (c)
57                 {
58                         this.control.name = name;
59                 }
60                 #endregion
61
62                 #region Public Properties
63                 public override Color BackColor {
64                         get { return base.BackColor; }
65                         set { 
66                                 base.BackColor = value;
67                                 control.BackColor = value;
68                         }
69                 }
70
71                 public override bool CanSelect {
72                         get { return control.CanSelect; }
73                 }
74
75                 [Browsable (false)]
76                 public Control Control {
77                         get { return this.control; }
78                 }
79
80                 [Browsable (false)]
81                 [DefaultValue (ContentAlignment.MiddleCenter)]
82                 public ContentAlignment ControlAlign {
83                         get { return this.control_align; }
84                         set {
85                                 if (!Enum.IsDefined (typeof (ContentAlignment), value))
86                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
87
88                                 this.control_align = value;
89                         }
90                 }
91
92                 [Browsable (false)]
93                 [EditorBrowsable (EditorBrowsableState.Never)]
94                 public ToolStripItemDisplayStyle DisplayStyle {
95                         get { return base.DisplayStyle; }
96                         set { base.DisplayStyle = value; }
97                 }
98
99                 [Browsable (false)]
100                 [EditorBrowsable (EditorBrowsableState.Never)]
101                 [DefaultValue (false)]
102                 public bool DoubleClickEnabled {
103                         get { return this.double_click_enabled; }
104                         set { this.double_click_enabled = value; }
105                 }
106
107                 [Browsable (false)]
108                 [EditorBrowsable (EditorBrowsableState.Never)]
109                 public override bool Enabled {
110                         get { return base.Enabled; }
111                         set {
112                                 base.Enabled = value;
113                                 control.Enabled = value;
114                         }
115                 }
116
117                 public virtual bool Focused {
118                         get { return control.Focused; }
119                 }
120
121                 public override Font Font {
122                         get { return base.Font; }
123                         set {
124                                 base.Font = value;
125                                 control.Font = value;
126                         }
127                 }
128
129                 public override Color ForeColor {
130                         get { return base.ForeColor; }
131                         set { 
132                                 base.ForeColor = value;
133                                 control.ForeColor = value;
134                         }
135                 }
136
137                 [Browsable (false)]
138                 [EditorBrowsable (EditorBrowsableState.Never)]
139                 public override Image Image {
140                         get { return base.Image; }
141                         set { base.Image = value; }
142                 }
143
144                 [Browsable (false)]
145                 [EditorBrowsable (EditorBrowsableState.Never)]
146                 public ContentAlignment ImageAlign {
147                         get { return this.image_align; }
148                         set { this.image_align = value; }
149                 }
150
151                 [Browsable (false)]
152                 [EditorBrowsable (EditorBrowsableState.Never)]
153                 public ToolStripItemImageScaling ImageScaling {
154                         get { return this.image_scaling; }
155                         set { this.image_scaling = value; }
156                 }
157
158                 [Browsable (false)]
159                 [EditorBrowsable (EditorBrowsableState.Never)]
160                 public Color ImageTransparentColor {
161                         get { return this.image_transparent_color; }
162                         set { this.image_transparent_color = value; }
163                 }
164
165                 public override bool Selected {
166                         get { return base.Selected; }
167                 }
168
169                 [EditorBrowsable (EditorBrowsableState.Advanced)]
170                 public override ISite Site {
171                         get { return base.Site; }
172                         set { 
173                                 base.Site = value;
174                                 control.Site = value;
175                         }
176                 }
177
178                 [DefaultValue ("")]
179                 public override string Text {
180                         get { return base.Text; }
181                         set {
182                                 base.Text = value;
183                                 control.Text = value;
184                         }
185                 }
186
187                 [Browsable (false)]
188                 [EditorBrowsable (EditorBrowsableState.Never)]
189                 public ContentAlignment TextAlign {
190                         get { return this.text_align; }
191                         set { this.text_align = value; }
192                 }
193
194                 [Browsable (false)]
195                 [EditorBrowsable (EditorBrowsableState.Never)]
196                 public TextImageRelation TextImageRelation {
197                         get { return this.text_image_relation; }
198                         set { this.text_image_relation = value; }
199                 }
200                 #endregion
201
202                 #region Protected Properties
203                 protected override Size DefaultSize {
204                         get {
205                                 if (control == null)
206                                         return new Size (23, 23);
207
208                                 return control.GetPreferredSize (Size.Empty);
209                         }
210                 }
211                 #endregion
212
213                 #region Public Methods
214                 public void Focus ()
215                 {
216                         control.Focus ();
217                 }
218
219                 public override Size GetPreferredSize (Size constrainingSize)
220                 {
221                         return this.Size;
222                 }
223
224                 [EditorBrowsable (EditorBrowsableState.Never)]
225                 public override void ResetBackColor ()
226                 {
227                         base.ResetBackColor ();
228                 }
229
230                 [EditorBrowsable (EditorBrowsableState.Never)]
231                 public override void ResetForeColor ()
232                 {
233                         base.ResetForeColor ();
234                 }
235                 #endregion
236
237                 #region Protected Methods
238                 protected override AccessibleObject CreateAccessibilityInstance ()
239                 {
240                         return this.Control.AccessibilityObject;
241                 }
242                 
243                 protected override void OnBoundsChanged ()
244                 {
245                         base.OnBoundsChanged ();
246
247                         if (this.Parent != null) {
248                                 control.Size = this.Size;
249                                 OnLayout (new LayoutEventArgs (null, string.Empty));
250                         }
251                 }
252                 
253                 protected virtual void OnEnter (EventArgs e)
254                 {
255                         if (Enter != null) Enter (this, e);
256                 }
257
258                 protected virtual void OnGotFocus (EventArgs e)
259                 {
260                         if (GotFocus != null) GotFocus (this, e);
261                 }
262                 
263                 protected virtual void OnHostedControlResize (EventArgs e)
264                 {
265                 }
266                 
267                 protected virtual void OnKeyDown (KeyEventArgs e)
268                 {
269                         if (KeyDown != null) KeyDown (this, e);
270                 }
271                 
272                 protected virtual void OnKeyPress (KeyPressEventArgs e)
273                 {
274                         if (KeyPress != null) KeyPress (this, e);
275                 }
276                 
277                 protected virtual void OnKeyUp (KeyEventArgs e)
278                 {
279                         if (KeyUp != null) KeyUp (this, e);
280                 }
281
282                 protected override void OnLayout (LayoutEventArgs e)
283                 {
284                         base.OnLayout (e);
285                         
286                         if (control != null)
287                                 control.Bounds = AlignInRectangle (this.Bounds, control.Size, this.control_align);
288                 }
289                 
290                 protected virtual void OnLeave (EventArgs e)
291                 {
292                         if (Leave != null) Leave (this, e);
293                 }
294                 
295                 protected virtual void OnLostFocus (EventArgs e)
296                 {
297                         if (LostFocus != null) LostFocus (this, e);
298                 }
299
300                 protected override void OnPaint (PaintEventArgs e)
301                 {
302                         base.OnPaint (e);
303                 }
304                 
305                 protected override void OnParentChanged (ToolStrip oldParent, ToolStrip newParent)
306                 {
307                         base.OnParentChanged (oldParent, newParent);
308
309                         if (oldParent != null)
310                                 oldParent.Controls.Remove (control);
311
312                         if (newParent != null)
313                                 newParent.Controls.Add (control);
314                 }
315                 
316                 protected virtual void OnSubscribeControlEvents (Control control)
317                 {
318                         this.control.Enter += new EventHandler (HandleEnter);
319                         this.control.GotFocus += new EventHandler (HandleGotFocus);
320                         this.control.KeyDown += new KeyEventHandler (HandleKeyDown);
321                         this.control.KeyPress += new KeyPressEventHandler (HandleKeyPress);
322                         this.control.KeyUp += new KeyEventHandler (HandleKeyUp);
323                         this.control.Leave += new EventHandler (HandleLeave);
324                         this.control.LostFocus += new EventHandler (HandleLostFocus);
325                         this.control.Validated += new EventHandler (HandleValidated);
326                         this.control.Validating += new CancelEventHandler (HandleValidating);
327                 }
328                 
329                 protected virtual void OnUnsubscribeControlEvents (Control control)
330                 {
331                 }
332                 
333                 protected virtual void OnValidated (EventArgs e)
334                 {
335                         if (Validated != null) Validated (this, e);
336                 }
337                 
338                 protected virtual void OnValidating (CancelEventArgs e)
339                 {
340                         if (Validating != null) Validating (this, e);
341                 }
342                 #endregion
343
344                 #region Public Events
345                 [Browsable (false)]
346                 [EditorBrowsable (EditorBrowsableState.Never)]
347                 public event EventHandler DisplayStyleChanged;
348                 public event EventHandler Enter;
349                 [Browsable (false)]
350                 [EditorBrowsable (EditorBrowsableState.Advanced)]
351                 public event EventHandler GotFocus;
352                 public event KeyEventHandler KeyDown;
353                 public event KeyPressEventHandler KeyPress;
354                 public event KeyEventHandler KeyUp;
355                 public event EventHandler Leave;
356                 [Browsable (false)]
357                 [EditorBrowsable (EditorBrowsableState.Advanced)]
358                 public event EventHandler LostFocus;
359                 public event EventHandler Validated;
360                 public event CancelEventHandler Validating;
361                 #endregion
362
363                 #region Private Methods
364                 private void HandleEnter (object sender, EventArgs e)
365                 {
366                         this.OnEnter (e);
367                 }
368
369                 private void HandleGotFocus (object sender, EventArgs e)
370                 {
371                         this.OnGotFocus (e);
372                 }
373
374                 private void HandleKeyDown (object sender, KeyEventArgs e)
375                 {
376                         this.OnKeyDown (e);
377                 }
378
379                 private void HandleKeyPress (object sender, KeyPressEventArgs e)
380                 {
381                         this.OnKeyPress (e);
382                 }
383
384                 private void HandleKeyUp (object sender, KeyEventArgs e)
385                 {
386                         this.OnKeyUp (e);
387                 }
388
389                 private void HandleLeave (object sender, EventArgs e)
390                 {
391                         this.OnLeave (e);
392                 }
393
394                 private void HandleLostFocus (object sender, EventArgs e)
395                 {
396                         this.OnLostFocus (e);
397                 }
398
399                 private void HandleValidated (object sender, EventArgs e)
400                 {
401                         this.OnValidated (e);
402                 }
403                 
404                 private void HandleValidating (object sender, CancelEventArgs e)
405                 {
406                         this.OnValidating (e);
407                 }
408                 #endregion
409         }
410 }
411 #endif