* TextBoxBase.cs: Take HideSelection into account when
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripControlHost.cs
blob2e0072617bc3c88d41efbb55ef08bd5c7b334bef
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.
23 // Copyright (c) 2006 Jonathan Pobst
25 // Authors:
26 // Jonathan Pobst (monkey@jpobst.com)
28 #if NET_2_0
29 using System.Drawing;
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 ()
48 if (c == null)
49 throw new ArgumentNullException ("c");
51 this.control = 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;
60 #endregion
62 #region Public Properties
63 public override Color BackColor {
64 get { return base.BackColor; }
65 set {
66 base.BackColor = value;
67 control.BackColor = value;
71 public override bool CanSelect {
72 get { return control.CanSelect; }
75 [Browsable (false)]
76 public Control Control {
77 get { return this.control; }
80 [Browsable (false)]
81 [DefaultValue (ContentAlignment.MiddleCenter)]
82 public ContentAlignment ControlAlign {
83 get { return this.control_align; }
84 set {
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;
92 [Browsable (false)]
93 [EditorBrowsable (EditorBrowsableState.Never)]
94 public new ToolStripItemDisplayStyle DisplayStyle {
95 get { return base.DisplayStyle; }
96 set { base.DisplayStyle = value; }
99 [Browsable (false)]
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; }
107 [Browsable (false)]
108 [EditorBrowsable (EditorBrowsableState.Never)]
109 public override bool Enabled {
110 get { return base.Enabled; }
111 set {
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; }
123 set {
124 base.Font = value;
125 control.Font = value;
129 public override Color ForeColor {
130 get { return base.ForeColor; }
131 set {
132 base.ForeColor = value;
133 control.ForeColor = value;
137 [Browsable (false)]
138 [EditorBrowsable (EditorBrowsableState.Never)]
139 public override Image Image {
140 get { return base.Image; }
141 set { base.Image = value; }
144 [Browsable (false)]
145 [EditorBrowsable (EditorBrowsableState.Never)]
146 public new ContentAlignment ImageAlign {
147 get { return this.image_align; }
148 set { this.image_align = value; }
151 [Browsable (false)]
152 [EditorBrowsable (EditorBrowsableState.Never)]
153 public new ToolStripItemImageScaling ImageScaling {
154 get { return this.image_scaling; }
155 set { this.image_scaling = value; }
158 [Browsable (false)]
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; }
172 set {
173 base.Site = value;
174 control.Site = value;
178 [DefaultValue ("")]
179 public override string Text {
180 get { return base.Text; }
181 set {
182 base.Text = value;
183 control.Text = value;
187 [Browsable (false)]
188 [EditorBrowsable (EditorBrowsableState.Never)]
189 public new ContentAlignment TextAlign {
190 get { return this.text_align; }
191 set { this.text_align = value; }
194 [Browsable (false)]
195 [EditorBrowsable (EditorBrowsableState.Never)]
196 public new TextImageRelation TextImageRelation {
197 get { return this.text_image_relation; }
198 set { this.text_image_relation = value; }
200 #endregion
202 #region Protected Properties
203 protected override Size DefaultSize {
204 get {
205 if (control == null)
206 return new Size (23, 23);
208 return control.GetPreferredSize (Size.Empty);
211 #endregion
213 #region Public Methods
214 public void Focus ()
216 control.Focus ();
219 public override Size GetPreferredSize (Size constrainingSize)
221 return this.Size;
224 [EditorBrowsable (EditorBrowsableState.Never)]
225 public override void ResetBackColor ()
227 base.ResetBackColor ();
230 [EditorBrowsable (EditorBrowsableState.Never)]
231 public override void ResetForeColor ()
233 base.ResetForeColor ();
235 #endregion
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)
248 control.Dispose ();
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]);
264 if (eh != null)
265 eh (this, e);
268 protected virtual void OnGotFocus (EventArgs e)
270 EventHandler eh = (EventHandler)(Events [GotFocusEvent]);
271 if (eh != null)
272 eh (this, e);
275 protected virtual void OnHostedControlResize (EventArgs e)
279 protected virtual void OnKeyDown (KeyEventArgs e)
281 KeyEventHandler eh = (KeyEventHandler)(Events [KeyDownEvent]);
282 if (eh != null)
283 eh (this, e);
286 protected virtual void OnKeyPress (KeyPressEventArgs e)
288 KeyPressEventHandler eh = (KeyPressEventHandler)(Events [KeyPressEvent]);
289 if (eh != null)
290 eh (this, e);
293 protected virtual void OnKeyUp (KeyEventArgs e)
295 KeyEventHandler eh = (KeyEventHandler)(Events [KeyUpEvent]);
296 if (eh != null)
297 eh (this, e);
300 protected override void OnLayout (LayoutEventArgs e)
302 base.OnLayout (e);
304 if (control != null)
305 control.Bounds = AlignInRectangle (this.Bounds, control.Size, this.control_align);
308 protected virtual void OnLeave (EventArgs e)
310 EventHandler eh = (EventHandler)(Events [LeaveEvent]);
311 if (eh != null)
312 eh (this, e);
315 protected virtual void OnLostFocus (EventArgs e)
317 EventHandler eh = (EventHandler)(Events [LostFocusEvent]);
318 if (eh != null)
319 eh (this, e);
322 protected override void OnPaint (PaintEventArgs e)
324 base.OnPaint (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]);
358 if (eh != null)
359 eh (this, e);
362 protected virtual void OnValidating (CancelEventArgs e)
364 CancelEventHandler eh = (CancelEventHandler)(Events [ValidatingEvent]);
365 if (eh != null)
366 eh (this, e);
368 #endregion
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 ();
381 [Browsable (false)]
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); }
393 [Browsable (false)]
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); }
420 [Browsable (false)]
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); }
436 #endregion
438 #region Private Methods
439 private void HandleEnter (object sender, EventArgs e)
441 this.OnEnter (e);
444 private void HandleGotFocus (object sender, EventArgs e)
446 this.OnGotFocus (e);
449 private void HandleKeyDown (object sender, KeyEventArgs e)
451 this.OnKeyDown (e);
454 private void HandleKeyPress (object sender, KeyPressEventArgs e)
456 this.OnKeyPress (e);
459 private void HandleKeyUp (object sender, KeyEventArgs e)
461 this.OnKeyUp (e);
464 private void HandleLeave (object sender, EventArgs e)
466 this.OnLeave (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);
483 #endregion
486 #endif