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 ToolStripItemDisplayStyle DisplayStyle {
95 get { return base.DisplayStyle; }
96 set { base.DisplayStyle = value; }
100 [EditorBrowsable (EditorBrowsableState.Never)]
101 [DefaultValue (false)]
102 public 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 ContentAlignment ImageAlign {
147 get { return this.image_align; }
148 set { this.image_align = value; }
152 [EditorBrowsable (EditorBrowsableState.Never)]
153 public ToolStripItemImageScaling ImageScaling {
154 get { return this.image_scaling; }
155 set { this.image_scaling = value; }
159 [EditorBrowsable (EditorBrowsableState.Never)]
160 public 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 ContentAlignment TextAlign {
190 get { return this.text_align; }
191 set { this.text_align = value; }
195 [EditorBrowsable (EditorBrowsableState.Never)]
196 public 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 OnBoundsChanged ()
245 base.OnBoundsChanged ();
247 if (this.Parent != null) {
248 control.Size = this.Size;
249 OnLayout (new LayoutEventArgs (null, string.Empty));
253 protected virtual void OnEnter (EventArgs e)
255 if (Enter != null) Enter (this, e);
258 protected virtual void OnGotFocus (EventArgs e)
260 if (GotFocus != null) GotFocus (this, e);
263 protected virtual void OnHostedControlResize (EventArgs e)
267 protected virtual void OnKeyDown (KeyEventArgs e)
269 if (KeyDown != null) KeyDown (this, e);
272 protected virtual void OnKeyPress (KeyPressEventArgs e)
274 if (KeyPress != null) KeyPress (this, e);
277 protected virtual void OnKeyUp (KeyEventArgs e)
279 if (KeyUp != null) KeyUp (this, e);
282 protected override void OnLayout (LayoutEventArgs e)
287 control.Bounds = AlignInRectangle (this.Bounds, control.Size, this.control_align);
290 protected virtual void OnLeave (EventArgs e)
292 if (Leave != null) Leave (this, e);
295 protected virtual void OnLostFocus (EventArgs e)
297 if (LostFocus != null) LostFocus (this, e);
300 protected override void OnPaint (PaintEventArgs e)
305 protected override void OnParentChanged (ToolStrip oldParent, ToolStrip newParent)
307 base.OnParentChanged (oldParent, newParent);
309 if (oldParent != null)
310 oldParent.Controls.Remove (control);
312 if (newParent != null)
313 newParent.Controls.Add (control);
316 protected virtual void OnSubscribeControlEvents (Control control)
318 this.control.Enter += new EventHandler (HandleEnter);
319 this.control.GotFocus += new EventHandler (HandleGotFocus);
320 this.control.KeyDown += new KeyEventHandler (HandleKeyDown);
321 this.control.KeyPress += new KeyPressEventHandler (HandleKeyPress);
322 this.control.KeyUp += new KeyEventHandler (HandleKeyUp);
323 this.control.Leave += new EventHandler (HandleLeave);
324 this.control.LostFocus += new EventHandler (HandleLostFocus);
325 this.control.Validated += new EventHandler (HandleValidated);
326 this.control.Validating += new CancelEventHandler (HandleValidating);
329 protected virtual void OnUnsubscribeControlEvents (Control control)
333 protected virtual void OnValidated (EventArgs e)
335 if (Validated != null) Validated (this, e);
338 protected virtual void OnValidating (CancelEventArgs e)
340 if (Validating != null) Validating (this, e);
344 #region Public Events
346 [EditorBrowsable (EditorBrowsableState.Never)]
347 public event EventHandler DisplayStyleChanged;
348 public event EventHandler Enter;
350 [EditorBrowsable (EditorBrowsableState.Advanced)]
351 public event EventHandler GotFocus;
352 public event KeyEventHandler KeyDown;
353 public event KeyPressEventHandler KeyPress;
354 public event KeyEventHandler KeyUp;
355 public event EventHandler Leave;
357 [EditorBrowsable (EditorBrowsableState.Advanced)]
358 public event EventHandler LostFocus;
359 public event EventHandler Validated;
360 public event CancelEventHandler Validating;
363 #region Private Methods
364 private void HandleEnter (object sender, EventArgs e)
369 private void HandleGotFocus (object sender, EventArgs e)
374 private void HandleKeyDown (object sender, KeyEventArgs e)
379 private void HandleKeyPress (object sender, KeyPressEventArgs e)
384 private void HandleKeyUp (object sender, KeyEventArgs e)
389 private void HandleLeave (object sender, EventArgs e)
394 private void HandleLostFocus (object sender, EventArgs e)
396 this.OnLostFocus (e);
399 private void HandleValidated (object sender, EventArgs e)
401 this.OnValidated (e);
404 private void HandleValidating (object sender, CancelEventArgs e)
406 this.OnValidating (e);