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 (this.TextBox as ToolStripTextBoxControl).OwnerItem = this;
45 base.Control.border_style = BorderStyle.None;
46 (base.Control as ToolStripTextBoxControl).Border = BorderStyle.Fixed3D;
47 this.border_style = BorderStyle.Fixed3D;
50 [EditorBrowsable (EditorBrowsableState.Never)]
51 public ToolStripTextBox (Control c) : base (c)
53 throw new NotSupportedException ("This construtor cannot be used.");
56 public ToolStripTextBox (string name) : this ()
62 #region Public Properties
63 [DefaultValue (false)]
64 public bool AcceptsReturn {
65 get { return this.TextBox.AcceptsReturn; }
66 set { this.TextBox.AcceptsReturn = value; }
69 [DefaultValue (false)]
70 public bool AcceptsTab {
71 get { return this.TextBox.AcceptsTab; }
72 set { this.TextBox.AcceptsTab = value; }
75 [MonoTODO ("AutoCompletion algorithm is currently not implemented.")]
78 [EditorBrowsable (EditorBrowsableState.Always)]
79 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
80 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
81 public AutoCompleteStringCollection AutoCompleteCustomSource {
82 get { return this.TextBox.AutoCompleteCustomSource; }
83 set { this.TextBox.AutoCompleteCustomSource = value; }
86 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
88 [DefaultValue (AutoCompleteMode.None)]
89 [EditorBrowsable (EditorBrowsableState.Always)]
90 public AutoCompleteMode AutoCompleteMode {
91 get { return this.TextBox.AutoCompleteMode; }
92 set { this.TextBox.AutoCompleteMode = value; }
95 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
97 [DefaultValue (AutoCompleteSource.None)]
98 [EditorBrowsable (EditorBrowsableState.Always)]
99 public AutoCompleteSource AutoCompleteSource {
100 get { return this.TextBox.AutoCompleteSource; }
101 set { this.TextBox.AutoCompleteSource = value; }
105 [EditorBrowsable (EditorBrowsableState.Never)]
106 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
107 public override Image BackgroundImage {
108 get { return base.BackgroundImage; }
109 set { base.BackgroundImage = value; }
113 [EditorBrowsable (EditorBrowsableState.Never)]
114 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
115 public override ImageLayout BackgroundImageLayout {
116 get { return base.BackgroundImageLayout; }
117 set { base.BackgroundImageLayout = value; }
120 [DefaultValue (BorderStyle.Fixed3D)]
122 public BorderStyle BorderStyle {
123 get { return this.border_style; }
125 if (this.border_style != value) {
126 this.border_style = value;
127 (base.Control as ToolStripTextBoxControl).Border = value;
128 this.OnBorderStyleChanged (EventArgs.Empty);
134 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
135 public bool CanUndo {
136 get { return this.TextBox.CanUndo; }
139 [DefaultValue (CharacterCasing.Normal)]
140 public CharacterCasing CharacterCasing {
141 get { return this.TextBox.CharacterCasing; }
142 set { this.TextBox.CharacterCasing = value; }
145 [DefaultValue (true)]
146 public bool HideSelection {
147 get { return this.TextBox.HideSelection; }
148 set { this.TextBox.HideSelection = value; }
152 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
153 [Editor ("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
154 public string[] Lines {
155 get { return this.TextBox.Lines; }
156 set { this.TextBox.Lines = value; }
160 [DefaultValue (32767)]
161 public int MaxLength {
162 get { return this.TextBox.MaxLength; }
163 set { this.TextBox.MaxLength = value; }
167 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
168 public bool Modified {
169 get { return this.TextBox.Modified; }
170 set { this.TextBox.Modified = value; }
175 [EditorBrowsable (EditorBrowsableState.Never)]
176 [DefaultValue (false)]
177 [RefreshProperties (RefreshProperties.All)]
178 public bool Multiline {
179 get { return this.TextBox.Multiline; }
180 set { this.TextBox.Multiline = value; }
183 [DefaultValue (false)]
184 public bool ReadOnly {
185 get { return this.TextBox.ReadOnly; }
186 set { this.TextBox.ReadOnly = value; }
190 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
191 public string SelectedText {
192 get { return this.TextBox.SelectedText; }
193 set { this.TextBox.SelectedText = value; }
197 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
198 public int SelectionLength {
199 get { return this.TextBox.SelectionLength == -1 ? 0 : this.TextBox.SelectionLength; }
200 set { this.TextBox.SelectionLength = value; }
204 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
205 public int SelectionStart {
206 get { return this.TextBox.SelectionStart; }
207 set { this.TextBox.SelectionStart = value; }
210 [DefaultValue (true)]
211 public bool ShortcutsEnabled {
212 get { return this.TextBox.ShortcutsEnabled; }
213 set { this.TextBox.ShortcutsEnabled = value; }
217 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
218 public TextBox TextBox {
219 get { return (TextBox)base.Control; }
223 [DefaultValue (HorizontalAlignment.Left)]
224 public HorizontalAlignment TextBoxTextAlign {
225 get { return this.TextBox.TextAlign; }
226 set { this.TextBox.TextAlign = value; }
230 public int TextLength {
231 get { return this.TextBox.TextLength; }
236 [EditorBrowsable (EditorBrowsableState.Never)]
237 [DefaultValue (true)]
238 public bool WordWrap {
239 get { return this.TextBox.WordWrap; }
240 set { this.TextBox.WordWrap = value; }
244 #region Protected Properties
245 protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
246 protected override Size DefaultSize { get { return new Size (100, 22); } }
249 #region Public Methods
250 public void AppendText (string text)
252 this.TextBox.AppendText (text);
257 this.TextBox.Clear ();
260 public void ClearUndo ()
262 this.TextBox.ClearUndo ();
267 this.TextBox.Copy ();
275 public void DeselectAll ()
277 this.TextBox.DeselectAll ();
280 public char GetCharFromPosition (Point pt)
282 return this.TextBox.GetCharFromPosition (pt);
285 public int GetCharIndexFromPosition (Point pt)
287 return this.TextBox.GetCharIndexFromPosition (pt);
290 public int GetFirstCharIndexFromLine (int lineNumber)
292 return this.TextBox.GetFirstCharIndexFromLine (lineNumber);
295 public int GetFirstCharIndexOfCurrentLine ()
297 return this.TextBox.GetFirstCharIndexOfCurrentLine ();
300 public int GetLineFromCharIndex (int index)
302 return this.TextBox.GetLineFromCharIndex (index);
305 public Point GetPositionFromCharIndex (int index)
307 return this.TextBox.GetPositionFromCharIndex (index);
310 public override Size GetPreferredSize (Size constrainingSize)
312 return base.GetPreferredSize (constrainingSize);
317 this.TextBox.Paste ();
320 public void ScrollToCaret ()
322 this.TextBox.ScrollToCaret ();
325 public void Select (int start, int length)
327 this.TextBox.Select (start, length);
330 public void SelectAll ()
332 this.TextBox.SelectAll ();
337 this.TextBox.Undo ();
341 #region Protected Methods
342 protected virtual void OnAcceptsTabChanged (EventArgs e)
344 EventHandler eh = (EventHandler)Events [AcceptsTabChangedEvent];
349 protected virtual void OnBorderStyleChanged (EventArgs e)
351 EventHandler eh = (EventHandler)Events [BorderStyleChangedEvent];
356 protected virtual void OnHideSelectionChanged (EventArgs e)
358 EventHandler eh = (EventHandler)Events [HideSelectionChangedEvent];
363 protected virtual void OnModifiedChanged (EventArgs e)
365 EventHandler eh = (EventHandler)Events [ModifiedChangedEvent];
370 protected virtual void OnMultilineChanged (EventArgs e)
372 EventHandler eh = (EventHandler)Events [MultilineChangedEvent];
377 protected virtual void OnReadOnlyChanged (EventArgs e)
379 EventHandler eh = (EventHandler)Events [ReadOnlyChangedEvent];
384 protected override void OnSubscribeControlEvents (Control control)
386 base.OnSubscribeControlEvents (control);
388 this.TextBox.AcceptsTabChanged += new EventHandler (HandleAcceptsTabChanged);
389 this.TextBox.HideSelectionChanged += new EventHandler (HandleHideSelectionChanged);
390 this.TextBox.ModifiedChanged += new EventHandler (HandleModifiedChanged);
391 this.TextBox.MultilineChanged += new EventHandler (HandleMultilineChanged);
392 this.TextBox.ReadOnlyChanged += new EventHandler (HandleReadOnlyChanged);
393 this.TextBox.TextAlignChanged += new EventHandler (HandleTextAlignChanged);
394 this.TextBox.TextChanged += new EventHandler (HandleTextChanged);
397 protected override void OnUnsubscribeControlEvents (Control control)
399 base.OnUnsubscribeControlEvents (control);
403 #region Public Events
404 static object AcceptsTabChangedEvent = new object ();
405 static object BorderStyleChangedEvent = new object ();
406 static object HideSelectionChangedEvent = new object ();
407 static object ModifiedChangedEvent = new object ();
408 static object MultilineChangedEvent = new object ();
409 static object ReadOnlyChangedEvent = new object ();
410 static object TextBoxTextAlignChangedEvent = new object ();
412 public event EventHandler AcceptsTabChanged {
413 add { Events.AddHandler (AcceptsTabChangedEvent, value); }
414 remove {Events.RemoveHandler (AcceptsTabChangedEvent, value); }
416 public event EventHandler BorderStyleChanged {
417 add { Events.AddHandler (BorderStyleChangedEvent, value); }
418 remove {Events.RemoveHandler (BorderStyleChangedEvent, value); }
420 public event EventHandler HideSelectionChanged {
421 add { Events.AddHandler (HideSelectionChangedEvent, value); }
422 remove {Events.RemoveHandler (HideSelectionChangedEvent, value); }
424 public event EventHandler ModifiedChanged {
425 add { Events.AddHandler (ModifiedChangedEvent, value); }
426 remove {Events.RemoveHandler (ModifiedChangedEvent, value); }
429 [EditorBrowsable (EditorBrowsableState.Never)]
430 public event EventHandler MultilineChanged {
431 add { Events.AddHandler (MultilineChangedEvent, value); }
432 remove {Events.RemoveHandler (MultilineChangedEvent, value); }
434 public event EventHandler ReadOnlyChanged {
435 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
436 remove {Events.RemoveHandler (ReadOnlyChangedEvent, value); }
438 public event EventHandler TextBoxTextAlignChanged {
439 add { Events.AddHandler (TextBoxTextAlignChangedEvent, value); }
440 remove {Events.RemoveHandler (TextBoxTextAlignChangedEvent, value); }
444 #region Private Methods
445 private void HandleTextAlignChanged (object sender, EventArgs e)
447 EventHandler eh = (EventHandler)(Events [TextBoxTextAlignChangedEvent]);
452 private void HandleReadOnlyChanged (object sender, EventArgs e)
454 OnReadOnlyChanged (e);
457 private void HandleMultilineChanged (object sender, EventArgs e)
459 OnMultilineChanged (e);
462 private void HandleModifiedChanged (object sender, EventArgs e)
464 OnModifiedChanged (e);
467 private void HandleHideSelectionChanged (object sender, EventArgs e)
469 OnHideSelectionChanged (e);
472 private void HandleAcceptsTabChanged (object sender, EventArgs e)
474 OnAcceptsTabChanged (e);
477 private void HandleTextChanged (object sender, EventArgs e)
483 private class ToolStripTextBoxControl : TextBox
485 private BorderStyle border;
486 private Timer tooltip_timer;
487 private ToolTip tooltip_window;
488 private ToolStripItem owner_item;
490 public ToolStripTextBoxControl () : base ()
494 protected override void OnLostFocus (EventArgs e)
496 base.OnLostFocus (e);
500 protected override void OnMouseEnter (EventArgs e)
502 base.OnMouseEnter (e);
506 ToolTipTimer.Start ();
510 protected override void OnMouseLeave (EventArgs e)
512 base.OnMouseLeave (e);
515 ToolTipTimer.Stop ();
516 ToolTipWindow.Hide (this);
519 internal override void OnPaintInternal (PaintEventArgs e)
521 base.OnPaintInternal (e);
523 if ((this.Focused || this.Entered || border == BorderStyle.FixedSingle) && border != BorderStyle.None) {
524 ToolStripRenderer tsr = (this.Parent as ToolStrip).Renderer;
526 if (tsr is ToolStripProfessionalRenderer)
527 using (Pen p = new Pen ((tsr as ToolStripProfessionalRenderer).ColorTable.ButtonSelectedBorder))
528 e.Graphics.DrawRectangle (p, new Rectangle (0, 0, this.Width - 1, this.Height - 1));
532 internal BorderStyle Border {
539 internal ToolStripItem OwnerItem {
540 set { owner_item = value; }
543 #region Stuff for ToolTips
544 private bool ShowToolTips {
549 return (Parent as ToolStrip).ShowItemToolTips;
553 private Timer ToolTipTimer {
555 if (tooltip_timer == null) {
556 tooltip_timer = new Timer ();
557 tooltip_timer.Enabled = false;
558 tooltip_timer.Interval = 500;
559 tooltip_timer.Tick += new EventHandler (ToolTipTimer_Tick);
562 return tooltip_timer;
566 private ToolTip ToolTipWindow {
568 if (tooltip_window == null)
569 tooltip_window = new ToolTip ();
571 return tooltip_window;
575 private void ToolTipTimer_Tick (object o, EventArgs args)
577 string tooltip = owner_item.GetToolTip ();
579 if (!string.IsNullOrEmpty (tooltip))
580 ToolTipWindow.Present (this, tooltip);
582 ToolTipTimer.Stop ();