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