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;
31 using System.Windows.Forms.Design;
32 using System.Runtime.InteropServices;
34 namespace System.Windows.Forms
36 [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
37 public class ToolStripTextBox : ToolStripControlHost
39 private BorderStyle border_style;
41 #region Public Constructors
42 public ToolStripTextBox () : base (new ToolStripTextBoxControl ())
44 base.Control.border_style = BorderStyle.None;
45 this.border_style = BorderStyle.Fixed3D;
48 [EditorBrowsable (EditorBrowsableState.Never)]
49 public ToolStripTextBox (Control c) : base (c)
51 throw new NotSupportedException ("This construtor cannot be used.");
54 public ToolStripTextBox (string name) : this ()
60 #region Public Properties
61 [DefaultValue (false)]
62 public bool AcceptsReturn {
63 get { return this.TextBox.AcceptsReturn; }
64 set { this.TextBox.AcceptsReturn = value; }
67 [DefaultValue (false)]
68 public bool AcceptsTab {
69 get { return this.TextBox.AcceptsTab; }
70 set { this.TextBox.AcceptsTab = value; }
74 [EditorBrowsable (EditorBrowsableState.Never)]
75 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
76 public override Image BackgroundImage {
77 get { return base.BackgroundImage; }
78 set { base.BackgroundImage = value; }
82 [EditorBrowsable (EditorBrowsableState.Never)]
83 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
84 public override ImageLayout BackgroundImageLayout {
85 get { return base.BackgroundImageLayout; }
86 set { base.BackgroundImageLayout = value; }
89 [DefaultValue (BorderStyle.Fixed3D)]
91 public BorderStyle BorderStyle {
92 get { return this.border_style; }
94 if (this.border_style != value) {
95 this.border_style = value;
96 this.OnBorderStyleChanged (EventArgs.Empty);
102 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
103 public bool CanUndo {
104 get { return this.TextBox.CanUndo; }
107 [DefaultValue (CharacterCasing.Normal)]
108 public CharacterCasing CharacterCasing {
109 get { return this.TextBox.CharacterCasing; }
110 set { this.TextBox.CharacterCasing = value; }
113 [DefaultValue (true)]
114 public bool HideSelection {
115 get { return this.TextBox.HideSelection; }
116 set { this.TextBox.HideSelection = value; }
120 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
121 [Editor ("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
122 public string[] Lines {
123 get { return this.TextBox.Lines; }
124 set { this.TextBox.Lines = value; }
128 [DefaultValue (32767)]
129 public int MaxLength {
130 get { return this.TextBox.MaxLength; }
131 set { this.TextBox.MaxLength = value; }
135 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
136 public bool Modified {
137 get { return this.TextBox.Modified; }
138 set { this.TextBox.Modified = value; }
143 [EditorBrowsable (EditorBrowsableState.Never)]
144 [DefaultValue (false)]
145 [RefreshProperties (RefreshProperties.All)]
146 public bool Multiline {
147 get { return this.TextBox.Multiline; }
148 set { this.TextBox.Multiline = value; }
151 [DefaultValue (false)]
152 public bool ReadOnly {
153 get { return this.TextBox.ReadOnly; }
154 set { this.TextBox.ReadOnly = value; }
158 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
159 public string SelectedText {
160 get { return this.TextBox.SelectedText; }
161 set { this.TextBox.SelectedText = value; }
165 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
166 public int SelectionLength {
167 get { return this.TextBox.SelectionLength == -1 ? 0 : this.TextBox.SelectionLength; }
168 set { this.TextBox.SelectionLength = value; }
172 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
173 public int SelectionStart {
174 get { return this.TextBox.SelectionStart; }
175 set { this.TextBox.SelectionStart = value; }
179 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
180 public TextBox TextBox {
181 get { return (TextBox)base.Control; }
185 [DefaultValue (HorizontalAlignment.Left)]
186 public HorizontalAlignment TextBoxTextAlign {
187 get { return this.TextBox.TextAlign; }
188 set { this.TextBox.TextAlign = value; }
192 public int TextLength {
193 get { return this.TextBox.TextLength; }
198 [EditorBrowsable (EditorBrowsableState.Never)]
199 [DefaultValue (true)]
200 public bool WordWrap {
201 get { return this.TextBox.WordWrap; }
202 set { this.TextBox.WordWrap = value; }
206 #region Protected Properties
207 protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
208 protected override Size DefaultSize { get { return new Size (100, 22); } }
211 #region Public Methods
212 public void AppendText (string text)
214 this.TextBox.AppendText (text);
219 this.TextBox.Clear ();
222 public void ClearUndo ()
224 this.TextBox.ClearUndo ();
229 this.TextBox.Copy ();
237 public override Size GetPreferredSize (Size constrainingSize)
239 return this.DefaultSize;
244 this.TextBox.Paste ();
247 public void ScrollToCaret ()
249 this.TextBox.ScrollToCaret ();
252 public void Select (int start, int length)
254 this.TextBox.Select (start, length);
257 public void SelectAll ()
259 this.TextBox.SelectAll ();
264 this.TextBox.Undo ();
268 #region Protected Methods
269 protected virtual void OnAcceptsTabChanged (EventArgs e)
271 EventHandler eh = (EventHandler)Events [AcceptsTabChangedEvent];
276 protected virtual void OnBorderStyleChanged (EventArgs e)
278 EventHandler eh = (EventHandler)Events [BorderStyleChangedEvent];
283 protected virtual void OnHideSelectionChanged (EventArgs e)
285 EventHandler eh = (EventHandler)Events [HideSelectionChangedEvent];
290 protected virtual void OnModifiedChanged (EventArgs e)
292 EventHandler eh = (EventHandler)Events [ModifiedChangedEvent];
297 protected virtual void OnMultilineChanged (EventArgs e)
299 EventHandler eh = (EventHandler)Events [MultilineChangedEvent];
304 protected virtual void OnReadOnlyChanged (EventArgs e)
306 EventHandler eh = (EventHandler)Events [ReadOnlyChangedEvent];
311 protected override void OnSubscribeControlEvents (Control control)
313 base.OnSubscribeControlEvents (control);
315 this.TextBox.AcceptsTabChanged += new EventHandler (HandleAcceptsTabChanged);
316 this.TextBox.HideSelectionChanged += new EventHandler (HandleHideSelectionChanged);
317 this.TextBox.ModifiedChanged += new EventHandler (HandleModifiedChanged);
318 this.TextBox.MultilineChanged += new EventHandler (HandleMultilineChanged);
319 this.TextBox.ReadOnlyChanged += new EventHandler (HandleReadOnlyChanged);
320 this.TextBox.TextAlignChanged += new EventHandler (HandleTextAlignChanged);
323 protected override void OnUnsubscribeControlEvents (Control control)
325 base.OnUnsubscribeControlEvents (control);
329 #region Public Events
330 static object AcceptsTabChangedEvent = new object ();
331 static object BorderStyleChangedEvent = new object ();
332 static object HideSelectionChangedEvent = new object ();
333 static object ModifiedChangedEvent = new object ();
334 static object MultilineChangedEvent = new object ();
335 static object ReadOnlyChangedEvent = new object ();
336 static object TextBoxTextAlignChangedEvent = new object ();
338 public event EventHandler AcceptsTabChanged {
339 add { Events.AddHandler (AcceptsTabChangedEvent, value); }
340 remove {Events.RemoveHandler (AcceptsTabChangedEvent, value); }
342 public event EventHandler BorderStyleChanged {
343 add { Events.AddHandler (BorderStyleChangedEvent, value); }
344 remove {Events.RemoveHandler (BorderStyleChangedEvent, value); }
346 public event EventHandler HideSelectionChanged {
347 add { Events.AddHandler (HideSelectionChangedEvent, value); }
348 remove {Events.RemoveHandler (HideSelectionChangedEvent, value); }
350 public event EventHandler ModifiedChanged {
351 add { Events.AddHandler (ModifiedChangedEvent, value); }
352 remove {Events.RemoveHandler (ModifiedChangedEvent, value); }
355 [EditorBrowsable (EditorBrowsableState.Never)]
356 public event EventHandler MultilineChanged {
357 add { Events.AddHandler (MultilineChangedEvent, value); }
358 remove {Events.RemoveHandler (MultilineChangedEvent, value); }
360 public event EventHandler ReadOnlyChanged {
361 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
362 remove {Events.RemoveHandler (ReadOnlyChangedEvent, value); }
364 public event EventHandler TextBoxTextAlignChanged {
365 add { Events.AddHandler (TextBoxTextAlignChangedEvent, value); }
366 remove {Events.RemoveHandler (TextBoxTextAlignChangedEvent, value); }
370 #region Private Methods
371 private void HandleTextAlignChanged (object sender, EventArgs e)
373 EventHandler eh = (EventHandler)(Events [TextBoxTextAlignChangedEvent]);
378 private void HandleReadOnlyChanged (object sender, EventArgs e)
380 OnReadOnlyChanged (e);
383 private void HandleMultilineChanged (object sender, EventArgs e)
385 OnMultilineChanged (e);
388 private void HandleModifiedChanged (object sender, EventArgs e)
390 OnModifiedChanged (e);
393 private void HandleHideSelectionChanged (object sender, EventArgs e)
395 OnHideSelectionChanged (e);
398 private void HandleAcceptsTabChanged (object sender, EventArgs e)
400 OnAcceptsTabChanged (e);
404 private class ToolStripTextBoxControl : TextBox
406 public ToolStripTextBoxControl () : base ()
410 protected override void OnMouseEnter (EventArgs e)
412 base.OnMouseEnter (e);
416 protected override void OnMouseLeave (EventArgs e)
418 base.OnMouseLeave (e);
422 protected override void OnPaint (PaintEventArgs e)
425 if (this.Focused || is_entered || this.border_style == BorderStyle.FixedSingle) {
426 ToolStripRenderer tsr = (this.Parent as ToolStrip).Renderer;
428 if (tsr is ToolStripProfessionalRenderer)
429 using (Pen p = new Pen ((tsr as ToolStripProfessionalRenderer).ColorTable.ButtonSelectedBorder))
430 e.Graphics.DrawRectangle (p, new Rectangle (0, 0, this.Width - 1, this.Height - 1));