2 // ToolStripControlHost.cs
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
30 using System
.ComponentModel
;
32 namespace System
.Windows
.Forms
34 public class ToolStripControlHost
: ToolStripItem
36 private Control control
;
37 private ContentAlignment control_align
;
38 private bool double_click_enabled
;
39 private ContentAlignment image_align
;
40 private ToolStripItemImageScaling image_scaling
;
41 private Color image_transparent_color
;
42 private ContentAlignment text_align
;
43 private TextImageRelation text_image_relation
;
45 #region Public Constructors
46 public ToolStripControlHost (Control c
) : base ()
49 throw new ArgumentNullException ("c");
52 this.control_align
= ContentAlignment
.MiddleCenter
;
53 this.OnSubscribeControlEvents (this.control
);
56 public ToolStripControlHost (Control c
, string name
) : this (c
)
58 this.control
.Name
= name
;
62 #region Public Properties
63 public override Color BackColor
{
64 get { return base.BackColor; }
66 base.BackColor
= value;
67 control
.BackColor
= value;
71 public override bool CanSelect
{
72 get { return control.CanSelect; }
76 public Control Control
{
77 get { return this.control; }
81 [DefaultValue (ContentAlignment
.MiddleCenter
)]
82 public ContentAlignment ControlAlign
{
83 get { return this.control_align; }
85 if (!Enum
.IsDefined (typeof (ContentAlignment
), value))
86 throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
88 this.control_align
= value;
93 [EditorBrowsable (EditorBrowsableState
.Never
)]
94 public new ToolStripItemDisplayStyle DisplayStyle
{
95 get { return base.DisplayStyle; }
96 set { base.DisplayStyle = value; }
100 [EditorBrowsable (EditorBrowsableState
.Never
)]
101 [DefaultValue (false)]
102 public new bool DoubleClickEnabled
{
103 get { return this.double_click_enabled; }
104 set { this.double_click_enabled = value; }
108 [EditorBrowsable (EditorBrowsableState
.Never
)]
109 public override bool Enabled
{
110 get { return base.Enabled; }
112 base.Enabled
= value;
113 control
.Enabled
= value;
117 public virtual bool Focused
{
118 get { return control.Focused; }
121 public override Font Font
{
122 get { return base.Font; }
125 control
.Font
= value;
129 public override Color ForeColor
{
130 get { return base.ForeColor; }
132 base.ForeColor
= value;
133 control
.ForeColor
= value;
138 [EditorBrowsable (EditorBrowsableState
.Never
)]
139 public override Image Image
{
140 get { return base.Image; }
141 set { base.Image = value; }
145 [EditorBrowsable (EditorBrowsableState
.Never
)]
146 public new ContentAlignment ImageAlign
{
147 get { return this.image_align; }
148 set { this.image_align = value; }
152 [EditorBrowsable (EditorBrowsableState
.Never
)]
153 public new ToolStripItemImageScaling ImageScaling
{
154 get { return this.image_scaling; }
155 set { this.image_scaling = value; }
159 [EditorBrowsable (EditorBrowsableState
.Never
)]
160 public new Color ImageTransparentColor
{
161 get { return this.image_transparent_color; }
162 set { this.image_transparent_color = value; }
165 public override bool Selected
{
166 get { return base.Selected; }
169 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
170 public override ISite Site
{
171 get { return base.Site; }
174 control
.Site
= value;
179 public override string Text
{
180 get { return base.Text; }
183 control
.Text
= value;
188 [EditorBrowsable (EditorBrowsableState
.Never
)]
189 public new ContentAlignment TextAlign
{
190 get { return this.text_align; }
191 set { this.text_align = value; }
195 [EditorBrowsable (EditorBrowsableState
.Never
)]
196 public new TextImageRelation TextImageRelation
{
197 get { return this.text_image_relation; }
198 set { this.text_image_relation = value; }
202 #region Protected Properties
203 protected override Size DefaultSize
{
206 return new Size (23, 23);
208 return control
.GetPreferredSize (Size
.Empty
);
213 #region Public Methods
219 public override Size
GetPreferredSize (Size constrainingSize
)
224 [EditorBrowsable (EditorBrowsableState
.Never
)]
225 public override void ResetBackColor ()
227 base.ResetBackColor ();
230 [EditorBrowsable (EditorBrowsableState
.Never
)]
231 public override void ResetForeColor ()
233 base.ResetForeColor ();
237 #region Protected Methods
238 protected override AccessibleObject
CreateAccessibilityInstance ()
240 return this.Control
.AccessibilityObject
;
243 protected override void Dispose (bool disposing
)
245 base.Dispose (disposing
);
247 if (!control
.IsDisposed
)
251 protected override void OnBoundsChanged ()
253 base.OnBoundsChanged ();
255 if (this.Parent
!= null) {
256 control
.Size
= this.Size
;
257 OnLayout (new LayoutEventArgs (null, string.Empty
));
261 protected virtual void OnEnter (EventArgs e
)
263 EventHandler eh
= (EventHandler
)(Events
[EnterEvent
]);
268 protected virtual void OnGotFocus (EventArgs e
)
270 EventHandler eh
= (EventHandler
)(Events
[GotFocusEvent
]);
275 protected virtual void OnHostedControlResize (EventArgs e
)
279 protected virtual void OnKeyDown (KeyEventArgs e
)
281 KeyEventHandler eh
= (KeyEventHandler
)(Events
[KeyDownEvent
]);
286 protected virtual void OnKeyPress (KeyPressEventArgs e
)
288 KeyPressEventHandler eh
= (KeyPressEventHandler
)(Events
[KeyPressEvent
]);
293 protected virtual void OnKeyUp (KeyEventArgs e
)
295 KeyEventHandler eh
= (KeyEventHandler
)(Events
[KeyUpEvent
]);
300 protected override void OnLayout (LayoutEventArgs e
)
305 control
.Bounds
= AlignInRectangle (this.Bounds
, control
.Size
, this.control_align
);
308 protected virtual void OnLeave (EventArgs e
)
310 EventHandler eh
= (EventHandler
)(Events
[LeaveEvent
]);
315 protected virtual void OnLostFocus (EventArgs e
)
317 EventHandler eh
= (EventHandler
)(Events
[LostFocusEvent
]);
322 protected override void OnPaint (PaintEventArgs e
)
327 protected override void OnParentChanged (ToolStrip oldParent
, ToolStrip newParent
)
329 base.OnParentChanged (oldParent
, newParent
);
331 if (oldParent
!= null)
332 oldParent
.Controls
.Remove (control
);
334 if (newParent
!= null)
335 newParent
.Controls
.Add (control
);
338 protected virtual void OnSubscribeControlEvents (Control control
)
340 this.control
.Enter
+= new EventHandler (HandleEnter
);
341 this.control
.GotFocus
+= new EventHandler (HandleGotFocus
);
342 this.control
.KeyDown
+= new KeyEventHandler (HandleKeyDown
);
343 this.control
.KeyPress
+= new KeyPressEventHandler (HandleKeyPress
);
344 this.control
.KeyUp
+= new KeyEventHandler (HandleKeyUp
);
345 this.control
.Leave
+= new EventHandler (HandleLeave
);
346 this.control
.LostFocus
+= new EventHandler (HandleLostFocus
);
347 this.control
.Validated
+= new EventHandler (HandleValidated
);
348 this.control
.Validating
+= new CancelEventHandler (HandleValidating
);
351 protected virtual void OnUnsubscribeControlEvents (Control control
)
355 protected virtual void OnValidated (EventArgs e
)
357 EventHandler eh
= (EventHandler
)(Events
[ValidatedEvent
]);
362 protected virtual void OnValidating (CancelEventArgs e
)
364 CancelEventHandler eh
= (CancelEventHandler
)(Events
[ValidatingEvent
]);
370 #region Public Events
371 static object EnterEvent
= new object ();
372 static object GotFocusEvent
= new object ();
373 static object KeyDownEvent
= new object ();
374 static object KeyPressEvent
= new object ();
375 static object KeyUpEvent
= new object ();
376 static object LeaveEvent
= new object ();
377 static object LostFocusEvent
= new object ();
378 static object ValidatedEvent
= new object ();
379 static object ValidatingEvent
= new object ();
382 [EditorBrowsable (EditorBrowsableState
.Never
)]
383 public new event EventHandler DisplayStyleChanged
{
384 add { base.DisplayStyleChanged += value; }
385 remove { base.DisplayStyleChanged -= value; }
388 public event EventHandler Enter
{
389 add { Events.AddHandler (EnterEvent, value); }
390 remove { Events.RemoveHandler (EnterEvent, value); }
394 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
395 public event EventHandler GotFocus
{
396 add { Events.AddHandler (GotFocusEvent, value); }
397 remove { Events.RemoveHandler (GotFocusEvent, value); }
400 public event KeyEventHandler KeyDown
{
401 add { Events.AddHandler (KeyDownEvent, value); }
402 remove { Events.RemoveHandler (KeyDownEvent, value); }
405 public event KeyPressEventHandler KeyPress
{
406 add { Events.AddHandler (KeyPressEvent, value); }
407 remove { Events.RemoveHandler (KeyPressEvent, value); }
410 public event KeyEventHandler KeyUp
{
411 add { Events.AddHandler (KeyUpEvent, value); }
412 remove { Events.RemoveHandler (KeyUpEvent, value); }
415 public event EventHandler Leave
{
416 add { Events.AddHandler (LeaveEvent, value); }
417 remove { Events.RemoveHandler (LeaveEvent, value); }
421 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
422 public event EventHandler LostFocus
{
423 add { Events.AddHandler (LostFocusEvent, value); }
424 remove { Events.RemoveHandler (LostFocusEvent, value); }
427 public event EventHandler Validated
{
428 add { Events.AddHandler (ValidatedEvent, value); }
429 remove { Events.RemoveHandler (ValidatedEvent, value); }
432 public event CancelEventHandler Validating
{
433 add { Events.AddHandler (ValidatingEvent, value); }
434 remove { Events.RemoveHandler (ValidatingEvent, value); }
438 #region Private Methods
439 private void HandleEnter (object sender
, EventArgs e
)
444 private void HandleGotFocus (object sender
, EventArgs e
)
449 private void HandleKeyDown (object sender
, KeyEventArgs e
)
454 private void HandleKeyPress (object sender
, KeyPressEventArgs e
)
459 private void HandleKeyUp (object sender
, KeyEventArgs e
)
464 private void HandleLeave (object sender
, EventArgs e
)
469 private void HandleLostFocus (object sender
, EventArgs e
)
471 this.OnLostFocus (e
);
474 private void HandleValidated (object sender
, EventArgs e
)
476 this.OnValidated (e
);
479 private void HandleValidating (object sender
, CancelEventArgs e
)
481 this.OnValidating (e
);