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; }
73 [MonoTODO ("AutoCompletion algorithm is currently not implemented.")]
76 [EditorBrowsable (EditorBrowsableState.Always)]
77 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
78 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
79 public AutoCompleteStringCollection AutoCompleteCustomSource {
80 get { return this.TextBox.AutoCompleteCustomSource; }
81 set { this.TextBox.AutoCompleteCustomSource = value; }
84 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
86 [DefaultValue (AutoCompleteMode.None)]
87 [EditorBrowsable (EditorBrowsableState.Always)]
88 public AutoCompleteMode AutoCompleteMode {
89 get { return this.TextBox.AutoCompleteMode; }
90 set { this.TextBox.AutoCompleteMode = value; }
93 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
95 [DefaultValue (AutoCompleteSource.None)]
96 [EditorBrowsable (EditorBrowsableState.Always)]
97 public AutoCompleteSource AutoCompleteSource {
98 get { return this.TextBox.AutoCompleteSource; }
99 set { this.TextBox.AutoCompleteSource = value; }
103 [EditorBrowsable (EditorBrowsableState.Never)]
104 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
105 public override Image BackgroundImage {
106 get { return base.BackgroundImage; }
107 set { base.BackgroundImage = value; }
111 [EditorBrowsable (EditorBrowsableState.Never)]
112 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
113 public override ImageLayout BackgroundImageLayout {
114 get { return base.BackgroundImageLayout; }
115 set { base.BackgroundImageLayout = value; }
118 [DefaultValue (BorderStyle.Fixed3D)]
120 public BorderStyle BorderStyle {
121 get { return this.border_style; }
123 if (this.border_style != value) {
124 this.border_style = value;
125 this.OnBorderStyleChanged (EventArgs.Empty);
131 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
132 public bool CanUndo {
133 get { return this.TextBox.CanUndo; }
136 [DefaultValue (CharacterCasing.Normal)]
137 public CharacterCasing CharacterCasing {
138 get { return this.TextBox.CharacterCasing; }
139 set { this.TextBox.CharacterCasing = value; }
142 [DefaultValue (true)]
143 public bool HideSelection {
144 get { return this.TextBox.HideSelection; }
145 set { this.TextBox.HideSelection = value; }
149 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
150 [Editor ("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
151 public string[] Lines {
152 get { return this.TextBox.Lines; }
153 set { this.TextBox.Lines = value; }
157 [DefaultValue (32767)]
158 public int MaxLength {
159 get { return this.TextBox.MaxLength; }
160 set { this.TextBox.MaxLength = value; }
164 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
165 public bool Modified {
166 get { return this.TextBox.Modified; }
167 set { this.TextBox.Modified = value; }
172 [EditorBrowsable (EditorBrowsableState.Never)]
173 [DefaultValue (false)]
174 [RefreshProperties (RefreshProperties.All)]
175 public bool Multiline {
176 get { return this.TextBox.Multiline; }
177 set { this.TextBox.Multiline = value; }
180 [DefaultValue (false)]
181 public bool ReadOnly {
182 get { return this.TextBox.ReadOnly; }
183 set { this.TextBox.ReadOnly = value; }
187 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
188 public string SelectedText {
189 get { return this.TextBox.SelectedText; }
190 set { this.TextBox.SelectedText = value; }
194 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
195 public int SelectionLength {
196 get { return this.TextBox.SelectionLength == -1 ? 0 : this.TextBox.SelectionLength; }
197 set { this.TextBox.SelectionLength = value; }
201 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
202 public int SelectionStart {
203 get { return this.TextBox.SelectionStart; }
204 set { this.TextBox.SelectionStart = value; }
207 [DefaultValue (true)]
208 public bool ShortcutsEnabled {
209 get { return this.TextBox.ShortcutsEnabled; }
210 set { this.TextBox.ShortcutsEnabled = value; }
214 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
215 public TextBox TextBox {
216 get { return (TextBox)base.Control; }
220 [DefaultValue (HorizontalAlignment.Left)]
221 public HorizontalAlignment TextBoxTextAlign {
222 get { return this.TextBox.TextAlign; }
223 set { this.TextBox.TextAlign = value; }
227 public int TextLength {
228 get { return this.TextBox.TextLength; }
233 [EditorBrowsable (EditorBrowsableState.Never)]
234 [DefaultValue (true)]
235 public bool WordWrap {
236 get { return this.TextBox.WordWrap; }
237 set { this.TextBox.WordWrap = value; }
241 #region Protected Properties
242 protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
243 protected override Size DefaultSize { get { return new Size (100, 22); } }
246 #region Public Methods
247 public void AppendText (string text)
249 this.TextBox.AppendText (text);
254 this.TextBox.Clear ();
257 public void ClearUndo ()
259 this.TextBox.ClearUndo ();
264 this.TextBox.Copy ();
272 public void DeselectAll ()
274 this.TextBox.DeselectAll ();
277 public char GetCharFromPosition (Point pt)
279 return this.TextBox.GetCharFromPosition (pt);
282 public int GetCharIndexFromPosition (Point pt)
284 return this.TextBox.GetCharIndexFromPosition (pt);
287 public int GetFirstCharIndexFromLine (int lineNumber)
289 return this.TextBox.GetFirstCharIndexFromLine (lineNumber);
292 public int GetFirstCharIndexOfCurrentLine ()
294 return this.TextBox.GetFirstCharIndexOfCurrentLine ();
297 public int GetLineFromCharIndex (int index)
299 return this.TextBox.GetLineFromCharIndex (index);
302 public Point GetPositionFromCharIndex (int index)
304 return this.TextBox.GetPositionFromCharIndex (index);
307 public override Size GetPreferredSize (Size constrainingSize)
309 return this.DefaultSize;
314 this.TextBox.Paste ();
317 public void ScrollToCaret ()
319 this.TextBox.ScrollToCaret ();
322 public void Select (int start, int length)
324 this.TextBox.Select (start, length);
327 public void SelectAll ()
329 this.TextBox.SelectAll ();
334 this.TextBox.Undo ();
338 #region Protected Methods
339 protected virtual void OnAcceptsTabChanged (EventArgs e)
341 EventHandler eh = (EventHandler)Events [AcceptsTabChangedEvent];
346 protected virtual void OnBorderStyleChanged (EventArgs e)
348 EventHandler eh = (EventHandler)Events [BorderStyleChangedEvent];
353 protected virtual void OnHideSelectionChanged (EventArgs e)
355 EventHandler eh = (EventHandler)Events [HideSelectionChangedEvent];
360 protected virtual void OnModifiedChanged (EventArgs e)
362 EventHandler eh = (EventHandler)Events [ModifiedChangedEvent];
367 protected virtual void OnMultilineChanged (EventArgs e)
369 EventHandler eh = (EventHandler)Events [MultilineChangedEvent];
374 protected virtual void OnReadOnlyChanged (EventArgs e)
376 EventHandler eh = (EventHandler)Events [ReadOnlyChangedEvent];
381 protected override void OnSubscribeControlEvents (Control control)
383 base.OnSubscribeControlEvents (control);
385 this.TextBox.AcceptsTabChanged += new EventHandler (HandleAcceptsTabChanged);
386 this.TextBox.HideSelectionChanged += new EventHandler (HandleHideSelectionChanged);
387 this.TextBox.ModifiedChanged += new EventHandler (HandleModifiedChanged);
388 this.TextBox.MultilineChanged += new EventHandler (HandleMultilineChanged);
389 this.TextBox.ReadOnlyChanged += new EventHandler (HandleReadOnlyChanged);
390 this.TextBox.TextAlignChanged += new EventHandler (HandleTextAlignChanged);
393 protected override void OnUnsubscribeControlEvents (Control control)
395 base.OnUnsubscribeControlEvents (control);
399 #region Public Events
400 static object AcceptsTabChangedEvent = new object ();
401 static object BorderStyleChangedEvent = new object ();
402 static object HideSelectionChangedEvent = new object ();
403 static object ModifiedChangedEvent = new object ();
404 static object MultilineChangedEvent = new object ();
405 static object ReadOnlyChangedEvent = new object ();
406 static object TextBoxTextAlignChangedEvent = new object ();
408 public event EventHandler AcceptsTabChanged {
409 add { Events.AddHandler (AcceptsTabChangedEvent, value); }
410 remove {Events.RemoveHandler (AcceptsTabChangedEvent, value); }
412 public event EventHandler BorderStyleChanged {
413 add { Events.AddHandler (BorderStyleChangedEvent, value); }
414 remove {Events.RemoveHandler (BorderStyleChangedEvent, value); }
416 public event EventHandler HideSelectionChanged {
417 add { Events.AddHandler (HideSelectionChangedEvent, value); }
418 remove {Events.RemoveHandler (HideSelectionChangedEvent, value); }
420 public event EventHandler ModifiedChanged {
421 add { Events.AddHandler (ModifiedChangedEvent, value); }
422 remove {Events.RemoveHandler (ModifiedChangedEvent, value); }
425 [EditorBrowsable (EditorBrowsableState.Never)]
426 public event EventHandler MultilineChanged {
427 add { Events.AddHandler (MultilineChangedEvent, value); }
428 remove {Events.RemoveHandler (MultilineChangedEvent, value); }
430 public event EventHandler ReadOnlyChanged {
431 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
432 remove {Events.RemoveHandler (ReadOnlyChangedEvent, value); }
434 public event EventHandler TextBoxTextAlignChanged {
435 add { Events.AddHandler (TextBoxTextAlignChangedEvent, value); }
436 remove {Events.RemoveHandler (TextBoxTextAlignChangedEvent, value); }
440 #region Private Methods
441 private void HandleTextAlignChanged (object sender, EventArgs e)
443 EventHandler eh = (EventHandler)(Events [TextBoxTextAlignChangedEvent]);
448 private void HandleReadOnlyChanged (object sender, EventArgs e)
450 OnReadOnlyChanged (e);
453 private void HandleMultilineChanged (object sender, EventArgs e)
455 OnMultilineChanged (e);
458 private void HandleModifiedChanged (object sender, EventArgs e)
460 OnModifiedChanged (e);
463 private void HandleHideSelectionChanged (object sender, EventArgs e)
465 OnHideSelectionChanged (e);
468 private void HandleAcceptsTabChanged (object sender, EventArgs e)
470 OnAcceptsTabChanged (e);
474 private class ToolStripTextBoxControl : TextBox
476 public ToolStripTextBoxControl () : base ()
480 protected override void OnMouseEnter (EventArgs e)
482 base.OnMouseEnter (e);
486 protected override void OnMouseLeave (EventArgs e)
488 base.OnMouseLeave (e);
492 protected override void OnPaint (PaintEventArgs e)
495 if (this.Focused || this.Entered || this.border_style == BorderStyle.FixedSingle) {
496 ToolStripRenderer tsr = (this.Parent as ToolStrip).Renderer;
498 if (tsr is ToolStripProfessionalRenderer)
499 using (Pen p = new Pen ((tsr as ToolStripProfessionalRenderer).ColorTable.ButtonSelectedBorder))
500 e.Graphics.DrawRectangle (p, new Rectangle (0, 0, this.Width - 1, this.Height - 1));