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