fixed tests
[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
40                 #region Public Constructors
41                 public ToolStripControlHost (Control c) : base ()
42                 {
43                         if (c == null)
44                                 throw new ArgumentNullException ("c");
45
46                         this.control = c;
47                         this.control_align = ContentAlignment.MiddleCenter;
48                         this.OnSubscribeControlEvents (this.control);
49                 }
50
51                 public ToolStripControlHost (Control c, string name) : this (c)
52                 {
53                         base.Name = name;
54                 }
55                 #endregion
56
57                 #region Public Properties
58                 public override Color BackColor {
59                         get { return control.BackColor; }
60                         set { control.BackColor = value;
61                         }
62                 }
63
64                 [Localizable (true)]
65                 [DefaultValue (null)]
66                 public override Image BackgroundImage {
67                         get { return base.BackgroundImage; }
68                         set { base.BackgroundImage = value; }
69                 }
70
71                 [Localizable (true)]
72                 [DefaultValue (ImageLayout.Tile)]
73                 public override ImageLayout BackgroundImageLayout {
74                         get { return base.BackgroundImageLayout; }
75                         set { base.BackgroundImageLayout = value; }
76                 }
77                 
78                 public override bool CanSelect {
79                         get { return control.CanSelect; }
80                 }
81
82                 public bool CausesValidation {
83                         get { return control.CausesValidation; }
84                         set { control.CausesValidation = value; }
85                 }
86                 
87                 [Browsable (false)]
88                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
89                 public Control Control {
90                         get { return this.control; }
91                 }
92
93                 [Browsable (false)]
94                 [DefaultValue (ContentAlignment.MiddleCenter)]
95                 public ContentAlignment ControlAlign {
96                         get { return this.control_align; }
97                         set {
98                                 if (!Enum.IsDefined (typeof (ContentAlignment), value))
99                                         throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
100
101                                 this.control_align = value;
102                         }
103                 }
104
105                 [Browsable (false)]
106                 [EditorBrowsable (EditorBrowsableState.Never)]
107                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
108                 public new ToolStripItemDisplayStyle DisplayStyle {
109                         get { return base.DisplayStyle; }
110                         set { base.DisplayStyle = value; }
111                 }
112
113                 [Browsable (false)]
114                 [EditorBrowsable (EditorBrowsableState.Never)]
115                 [DefaultValue (false)]
116                 public new bool DoubleClickEnabled {
117                         get { return this.double_click_enabled; }
118                         set { this.double_click_enabled = value; }
119                 }
120
121                 public override bool Enabled {
122                         get { return base.Enabled; }
123                         set {
124                                 base.Enabled = value;
125                                 control.Enabled = value;
126                         }
127                 }
128
129                 [Browsable (false)]
130                 [EditorBrowsable (EditorBrowsableState.Always)]
131                 public virtual bool Focused {
132                         get { return control.Focused; }
133                 }
134
135                 public override Font Font {
136                         get { return control.Font; }
137                         set { control.Font = value; }
138                 }
139
140                 public override Color ForeColor {
141                         get { return control.ForeColor; }
142                         set { control.ForeColor = value; }
143                 }
144
145                 [Browsable (false)]
146                 [EditorBrowsable (EditorBrowsableState.Never)]
147                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
148                 public override Image Image {
149                         get { return base.Image; }
150                         set { base.Image = value; }
151                 }
152
153                 [Browsable (false)]
154                 [EditorBrowsable (EditorBrowsableState.Never)]
155                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
156                 public new ContentAlignment ImageAlign {
157                         get { return base.ImageAlign; }
158                         set { base.ImageAlign = value; }
159                 }
160
161                 [Browsable (false)]
162                 [EditorBrowsable (EditorBrowsableState.Never)]
163                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
164                 public new ToolStripItemImageScaling ImageScaling {
165                         get { return base.ImageScaling; }
166                         set { base.ImageScaling = value; }
167                 }
168
169                 [Browsable (false)]
170                 [EditorBrowsable (EditorBrowsableState.Never)]
171                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
172                 public new Color ImageTransparentColor {
173                         get { return base.ImageTransparentColor; }
174                         set { base.ImageTransparentColor = value; }
175                 }
176
177                 public override bool Selected {
178                         get { return base.Selected; }
179                 }
180
181                 [EditorBrowsable (EditorBrowsableState.Advanced)]
182                 public override ISite Site {
183                         get { return control.Site; }
184                         set { control.Site = value;
185                         }
186                 }
187
188                 public override Size Size
189                 {
190                         get { return control.Size; }
191                         set { control.Size = value; base.Size = value;  if (this.Owner != null) this.Owner.PerformLayout (); }
192                 }
193                 
194                 [DefaultValue ("")]
195                 public override string Text {
196                         get { return base.Text; }
197                         set {
198                                 base.Text = value;
199                                 control.Text = value;
200                         }
201                 }
202
203                 [Browsable (false)]
204                 [EditorBrowsable (EditorBrowsableState.Never)]
205                 public new ContentAlignment TextAlign {
206                         get { return base.TextAlign; }
207                         set { base.TextAlign = value; }
208                 }
209
210                 [Browsable (false)]
211                 [EditorBrowsable (EditorBrowsableState.Never)]
212                 public new TextImageRelation TextImageRelation {
213                         get { return base.TextImageRelation; }
214                         set { base.TextImageRelation = value; }
215                 }
216                 #endregion
217
218                 #region Protected Properties
219                 protected override Size DefaultSize {
220                         get {
221                                 if (control == null)
222                                         return new Size (23, 23);
223
224                                 return control.GetPreferredSize (Size.Empty);
225                         }
226                 }
227                 #endregion
228
229                 #region Public Methods
230                 [EditorBrowsable (EditorBrowsableState.Advanced)]
231                 public void Focus ()
232                 {
233                         control.Focus ();
234                 }
235
236                 public override Size GetPreferredSize (Size constrainingSize)
237                 {
238                         return control.Size;
239                 }
240
241                 [EditorBrowsable (EditorBrowsableState.Never)]
242                 public override void ResetBackColor ()
243                 {
244                         base.ResetBackColor ();
245                 }
246
247                 [EditorBrowsable (EditorBrowsableState.Never)]
248                 public override void ResetForeColor ()
249                 {
250                         base.ResetForeColor ();
251                 }
252                 #endregion
253
254                 #region Protected Methods
255                 [EditorBrowsable (EditorBrowsableState.Advanced)]
256                 protected override AccessibleObject CreateAccessibilityInstance ()
257                 {
258                         return control.AccessibilityObject;
259                 }
260
261                 protected override void Dispose (bool disposing)
262                 {
263                         base.Dispose (disposing);
264                         
265                         if (!control.IsDisposed)
266                                 control.Dispose ();
267                 }
268                 
269                 protected override void OnBoundsChanged ()
270                 {
271                         base.OnBoundsChanged ();
272                 }
273                 
274                 protected virtual void OnEnter (EventArgs e)
275                 {
276                         EventHandler eh = (EventHandler)(Events [EnterEvent]);
277                         if (eh != null)
278                                 eh (this, e);
279                 }
280
281                 protected virtual void OnGotFocus (EventArgs e)
282                 {
283                         EventHandler eh = (EventHandler)(Events [GotFocusEvent]);
284                         if (eh != null)
285                                 eh (this, e);
286                 }
287                 
288                 protected virtual void OnHostedControlResize (EventArgs e)
289                 {
290                 }
291                 
292                 protected virtual void OnKeyDown (KeyEventArgs e)
293                 {
294                         KeyEventHandler eh = (KeyEventHandler)(Events [KeyDownEvent]);
295                         if (eh != null)
296                                 eh (this, e);
297                 }
298                 
299                 protected virtual void OnKeyPress (KeyPressEventArgs e)
300                 {
301                         KeyPressEventHandler eh = (KeyPressEventHandler)(Events [KeyPressEvent]);
302                         if (eh != null)
303                                 eh (this, e);
304                 }
305                 
306                 protected virtual void OnKeyUp (KeyEventArgs e)
307                 {
308                         KeyEventHandler eh = (KeyEventHandler)(Events [KeyUpEvent]);
309                         if (eh != null)
310                                 eh (this, e);
311                 }
312
313                 protected override void OnLayout (LayoutEventArgs e)
314                 {
315                         base.OnLayout (e);
316                         
317                         if (control != null)
318                                 control.Bounds = AlignInRectangle (this.Bounds, control.Size, this.control_align);
319                 }
320                 
321                 protected virtual void OnLeave (EventArgs e)
322                 {
323                         EventHandler eh = (EventHandler)(Events [LeaveEvent]);
324                         if (eh != null)
325                                 eh (this, e);
326                 }
327                 
328                 protected virtual void OnLostFocus (EventArgs e)
329                 {
330                         EventHandler eh = (EventHandler)(Events [LostFocusEvent]);
331                         if (eh != null)
332                                 eh (this, e);
333                 }
334
335                 protected override void OnPaint (PaintEventArgs e)
336                 {
337                         base.OnPaint (e);
338                 }
339                 
340                 protected override void OnParentChanged (ToolStrip oldParent, ToolStrip newParent)
341                 {
342                         base.OnParentChanged (oldParent, newParent);
343
344                         if (oldParent != null)
345                                 oldParent.Controls.Remove (control);
346
347                         if (newParent != null)
348                                 newParent.Controls.Add (control);
349                 }
350                 
351                 protected virtual void OnSubscribeControlEvents (Control control)
352                 {
353                         this.control.Enter += new EventHandler (HandleEnter);
354                         this.control.GotFocus += new EventHandler (HandleGotFocus);
355                         this.control.KeyDown += new KeyEventHandler (HandleKeyDown);
356                         this.control.KeyPress += new KeyPressEventHandler (HandleKeyPress);
357                         this.control.KeyUp += new KeyEventHandler (HandleKeyUp);
358                         this.control.Leave += new EventHandler (HandleLeave);
359                         this.control.LostFocus += new EventHandler (HandleLostFocus);
360                         this.control.Validated += new EventHandler (HandleValidated);
361                         this.control.Validating += new CancelEventHandler (HandleValidating);
362                 }
363                 
364                 protected virtual void OnUnsubscribeControlEvents (Control control)
365                 {
366                 }
367                 
368                 protected virtual void OnValidated (EventArgs e)
369                 {
370                         EventHandler eh = (EventHandler)(Events [ValidatedEvent]);
371                         if (eh != null)
372                                 eh (this, e);
373                 }
374                 
375                 protected virtual void OnValidating (CancelEventArgs e)
376                 {
377                         CancelEventHandler eh = (CancelEventHandler)(Events [ValidatingEvent]);
378                         if (eh != null)
379                                 eh (this, e);
380                 }
381
382                 protected override void SetVisibleCore (bool visible)
383                 {
384                         base.SetVisibleCore (visible);
385                         this.control.Visible = visible;
386                 }
387                 #endregion
388
389                 #region Public Events
390                 static object EnterEvent = new object ();
391                 static object GotFocusEvent = new object ();
392                 static object KeyDownEvent = new object ();
393                 static object KeyPressEvent = new object ();
394                 static object KeyUpEvent = new object ();
395                 static object LeaveEvent = new object ();
396                 static object LostFocusEvent = new object ();
397                 static object ValidatedEvent = new object ();
398                 static object ValidatingEvent = new object ();
399
400                 [Browsable (false)]
401                 [EditorBrowsable (EditorBrowsableState.Never)]
402                 public new event EventHandler DisplayStyleChanged {
403                         add { base.DisplayStyleChanged += value; }
404                         remove { base.DisplayStyleChanged -= value; }
405                 }
406
407                 public event EventHandler Enter {
408                         add { Events.AddHandler (EnterEvent, value); }
409                         remove { Events.RemoveHandler (EnterEvent, value); }
410                 }
411
412                 [Browsable (false)]
413                 [EditorBrowsable (EditorBrowsableState.Advanced)]
414                 public event EventHandler GotFocus {
415                         add { Events.AddHandler (GotFocusEvent, value); }
416                         remove { Events.RemoveHandler (GotFocusEvent, value); }
417                 }
418
419                 public event KeyEventHandler KeyDown {
420                         add { Events.AddHandler (KeyDownEvent, value); }
421                         remove { Events.RemoveHandler (KeyDownEvent, value); }
422                 }
423
424                 public event KeyPressEventHandler KeyPress {
425                         add { Events.AddHandler (KeyPressEvent, value); }
426                         remove { Events.RemoveHandler (KeyPressEvent, value); }
427                 }
428
429                 public event KeyEventHandler KeyUp {
430                         add { Events.AddHandler (KeyUpEvent, value); }
431                         remove { Events.RemoveHandler (KeyUpEvent, value); }
432                 }
433
434                 public event EventHandler Leave {
435                         add { Events.AddHandler (LeaveEvent, value); }
436                         remove { Events.RemoveHandler (LeaveEvent, value); }
437                 }
438
439                 [Browsable (false)]
440                 [EditorBrowsable (EditorBrowsableState.Advanced)]
441                 public event EventHandler LostFocus {
442                         add { Events.AddHandler (LostFocusEvent, value); }
443                         remove { Events.RemoveHandler (LostFocusEvent, value); }
444                 }
445
446                 public event EventHandler Validated {
447                         add { Events.AddHandler (ValidatedEvent, value); }
448                         remove { Events.RemoveHandler (ValidatedEvent, value); }
449                 }
450
451                 public event CancelEventHandler Validating {
452                         add { Events.AddHandler (ValidatingEvent, value); }
453                         remove { Events.RemoveHandler (ValidatingEvent, value); }
454                 }
455                 #endregion
456
457                 #region Private Methods
458                 private void HandleEnter (object sender, EventArgs e)
459                 {
460                         this.OnEnter (e);
461                 }
462
463                 private void HandleGotFocus (object sender, EventArgs e)
464                 {
465                         this.OnGotFocus (e);
466                 }
467
468                 private void HandleKeyDown (object sender, KeyEventArgs e)
469                 {
470                         this.OnKeyDown (e);
471                 }
472
473                 private void HandleKeyPress (object sender, KeyPressEventArgs e)
474                 {
475                         this.OnKeyPress (e);
476                 }
477
478                 private void HandleKeyUp (object sender, KeyEventArgs e)
479                 {
480                         this.OnKeyUp (e);
481                 }
482
483                 private void HandleLeave (object sender, EventArgs e)
484                 {
485                         this.OnLeave (e);
486                 }
487
488                 private void HandleLostFocus (object sender, EventArgs e)
489                 {
490                         this.OnLostFocus (e);
491                 }
492
493                 private void HandleValidated (object sender, EventArgs e)
494                 {
495                         this.OnValidated (e);
496                 }
497                 
498                 private void HandleValidating (object sender, CancelEventArgs e)
499                 {
500                         this.OnValidating (e);
501                 }
502                 #endregion
503         }
504 }
505 #endif