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