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 ToolStripTextBoxControl text_box = TextBox as ToolStripTextBoxControl;
45 text_box.OwnerItem = this;
46 text_box.border_style = BorderStyle.None;
47 text_box.TopMargin = 3; // need to explicitly set the margin
48 text_box.Border = BorderStyle.Fixed3D; // ToolStripTextBoxControl impl, not TextBox
49 this.border_style = BorderStyle.Fixed3D;
52 [EditorBrowsable (EditorBrowsableState.Never)]
53 public ToolStripTextBox (Control c) : base (c)
55 throw new NotSupportedException ("This construtor cannot be used.");
58 public ToolStripTextBox (string name) : this ()
64 #region Public Properties
65 [DefaultValue (false)]
66 public bool AcceptsReturn {
67 get { return this.TextBox.AcceptsReturn; }
68 set { this.TextBox.AcceptsReturn = value; }
71 [DefaultValue (false)]
72 public bool AcceptsTab {
73 get { return this.TextBox.AcceptsTab; }
74 set { this.TextBox.AcceptsTab = value; }
77 [MonoTODO ("AutoCompletion algorithm is currently not implemented.")]
80 [EditorBrowsable (EditorBrowsableState.Always)]
81 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
82 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
83 public AutoCompleteStringCollection AutoCompleteCustomSource {
84 get { return this.TextBox.AutoCompleteCustomSource; }
85 set { this.TextBox.AutoCompleteCustomSource = value; }
88 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
90 [DefaultValue (AutoCompleteMode.None)]
91 [EditorBrowsable (EditorBrowsableState.Always)]
92 public AutoCompleteMode AutoCompleteMode {
93 get { return this.TextBox.AutoCompleteMode; }
94 set { this.TextBox.AutoCompleteMode = value; }
97 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
99 [DefaultValue (AutoCompleteSource.None)]
100 [EditorBrowsable (EditorBrowsableState.Always)]
101 public AutoCompleteSource AutoCompleteSource {
102 get { return this.TextBox.AutoCompleteSource; }
103 set { this.TextBox.AutoCompleteSource = value; }
107 [EditorBrowsable (EditorBrowsableState.Never)]
108 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
109 public override Image BackgroundImage {
110 get { return base.BackgroundImage; }
111 set { base.BackgroundImage = value; }
115 [EditorBrowsable (EditorBrowsableState.Never)]
116 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
117 public override ImageLayout BackgroundImageLayout {
118 get { return base.BackgroundImageLayout; }
119 set { base.BackgroundImageLayout = value; }
122 [DefaultValue (BorderStyle.Fixed3D)]
124 public BorderStyle BorderStyle {
125 get { return this.border_style; }
127 if (this.border_style != value) {
128 this.border_style = value;
129 (base.Control as ToolStripTextBoxControl).Border = value;
130 this.OnBorderStyleChanged (EventArgs.Empty);
136 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
137 public bool CanUndo {
138 get { return this.TextBox.CanUndo; }
141 [DefaultValue (CharacterCasing.Normal)]
142 public CharacterCasing CharacterCasing {
143 get { return this.TextBox.CharacterCasing; }
144 set { this.TextBox.CharacterCasing = value; }
147 [DefaultValue (true)]
148 public bool HideSelection {
149 get { return this.TextBox.HideSelection; }
150 set { this.TextBox.HideSelection = value; }
154 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
155 [Editor ("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
156 public string[] Lines {
157 get { return this.TextBox.Lines; }
158 set { this.TextBox.Lines = value; }
162 [DefaultValue (32767)]
163 public int MaxLength {
164 get { return this.TextBox.MaxLength; }
165 set { this.TextBox.MaxLength = value; }
169 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
170 public bool Modified {
171 get { return this.TextBox.Modified; }
172 set { this.TextBox.Modified = value; }
177 [EditorBrowsable (EditorBrowsableState.Never)]
178 [DefaultValue (false)]
179 [RefreshProperties (RefreshProperties.All)]
180 public bool Multiline {
181 get { return this.TextBox.Multiline; }
182 set { this.TextBox.Multiline = value; }
185 [DefaultValue (false)]
186 public bool ReadOnly {
187 get { return this.TextBox.ReadOnly; }
188 set { this.TextBox.ReadOnly = value; }
192 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
193 public string SelectedText {
194 get { return this.TextBox.SelectedText; }
195 set { this.TextBox.SelectedText = value; }
199 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
200 public int SelectionLength {
201 get { return this.TextBox.SelectionLength == -1 ? 0 : this.TextBox.SelectionLength; }
202 set { this.TextBox.SelectionLength = value; }
206 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
207 public int SelectionStart {
208 get { return this.TextBox.SelectionStart; }
209 set { this.TextBox.SelectionStart = value; }
212 [DefaultValue (true)]
213 public bool ShortcutsEnabled {
214 get { return this.TextBox.ShortcutsEnabled; }
215 set { this.TextBox.ShortcutsEnabled = value; }
219 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
220 public TextBox TextBox {
221 get { return (TextBox)base.Control; }
225 [DefaultValue (HorizontalAlignment.Left)]
226 public HorizontalAlignment TextBoxTextAlign {
227 get { return this.TextBox.TextAlign; }
228 set { this.TextBox.TextAlign = value; }
232 public int TextLength {
233 get { return this.TextBox.TextLength; }
238 [EditorBrowsable (EditorBrowsableState.Never)]
239 [DefaultValue (true)]
240 public bool WordWrap {
241 get { return this.TextBox.WordWrap; }
242 set { this.TextBox.WordWrap = value; }
246 #region Protected Properties
247 protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
248 protected override Size DefaultSize { get { return new Size (100, 22); } }
251 #region Public Methods
252 public void AppendText (string text)
254 this.TextBox.AppendText (text);
259 this.TextBox.Clear ();
262 public void ClearUndo ()
264 this.TextBox.ClearUndo ();
269 this.TextBox.Copy ();
277 public void DeselectAll ()
279 this.TextBox.DeselectAll ();
282 public char GetCharFromPosition (Point pt)
284 return this.TextBox.GetCharFromPosition (pt);
287 public int GetCharIndexFromPosition (Point pt)
289 return this.TextBox.GetCharIndexFromPosition (pt);
292 public int GetFirstCharIndexFromLine (int lineNumber)
294 return this.TextBox.GetFirstCharIndexFromLine (lineNumber);
297 public int GetFirstCharIndexOfCurrentLine ()
299 return this.TextBox.GetFirstCharIndexOfCurrentLine ();
302 public int GetLineFromCharIndex (int index)
304 return this.TextBox.GetLineFromCharIndex (index);
307 public Point GetPositionFromCharIndex (int index)
309 return this.TextBox.GetPositionFromCharIndex (index);
312 public override Size GetPreferredSize (Size constrainingSize)
314 return base.GetPreferredSize (constrainingSize);
319 this.TextBox.Paste ();
322 public void ScrollToCaret ()
324 this.TextBox.ScrollToCaret ();
327 public void Select (int start, int length)
329 this.TextBox.Select (start, length);
332 public void SelectAll ()
334 this.TextBox.SelectAll ();
339 this.TextBox.Undo ();
343 #region Protected Methods
344 protected virtual void OnAcceptsTabChanged (EventArgs e)
346 EventHandler eh = (EventHandler)Events [AcceptsTabChangedEvent];
351 protected virtual void OnBorderStyleChanged (EventArgs e)
353 EventHandler eh = (EventHandler)Events [BorderStyleChangedEvent];
358 protected virtual void OnHideSelectionChanged (EventArgs e)
360 EventHandler eh = (EventHandler)Events [HideSelectionChangedEvent];
365 protected virtual void OnModifiedChanged (EventArgs e)
367 EventHandler eh = (EventHandler)Events [ModifiedChangedEvent];
372 protected virtual void OnMultilineChanged (EventArgs e)
374 EventHandler eh = (EventHandler)Events [MultilineChangedEvent];
379 protected virtual void OnReadOnlyChanged (EventArgs e)
381 EventHandler eh = (EventHandler)Events [ReadOnlyChangedEvent];
386 protected override void OnSubscribeControlEvents (Control control)
388 base.OnSubscribeControlEvents (control);
390 this.TextBox.AcceptsTabChanged += new EventHandler (HandleAcceptsTabChanged);
391 this.TextBox.HideSelectionChanged += new EventHandler (HandleHideSelectionChanged);
392 this.TextBox.ModifiedChanged += new EventHandler (HandleModifiedChanged);
393 this.TextBox.MultilineChanged += new EventHandler (HandleMultilineChanged);
394 this.TextBox.ReadOnlyChanged += new EventHandler (HandleReadOnlyChanged);
395 this.TextBox.TextAlignChanged += new EventHandler (HandleTextAlignChanged);
396 this.TextBox.TextChanged += new EventHandler (HandleTextChanged);
399 protected override void OnUnsubscribeControlEvents (Control control)
401 base.OnUnsubscribeControlEvents (control);
405 #region Public Events
406 static object AcceptsTabChangedEvent = new object ();
407 static object BorderStyleChangedEvent = new object ();
408 static object HideSelectionChangedEvent = new object ();
409 static object ModifiedChangedEvent = new object ();
410 static object MultilineChangedEvent = new object ();
411 static object ReadOnlyChangedEvent = new object ();
412 static object TextBoxTextAlignChangedEvent = new object ();
414 public event EventHandler AcceptsTabChanged {
415 add { Events.AddHandler (AcceptsTabChangedEvent, value); }
416 remove {Events.RemoveHandler (AcceptsTabChangedEvent, value); }
418 public event EventHandler BorderStyleChanged {
419 add { Events.AddHandler (BorderStyleChangedEvent, value); }
420 remove {Events.RemoveHandler (BorderStyleChangedEvent, value); }
422 public event EventHandler HideSelectionChanged {
423 add { Events.AddHandler (HideSelectionChangedEvent, value); }
424 remove {Events.RemoveHandler (HideSelectionChangedEvent, value); }
426 public event EventHandler ModifiedChanged {
427 add { Events.AddHandler (ModifiedChangedEvent, value); }
428 remove {Events.RemoveHandler (ModifiedChangedEvent, value); }
431 [EditorBrowsable (EditorBrowsableState.Never)]
432 public event EventHandler MultilineChanged {
433 add { Events.AddHandler (MultilineChangedEvent, value); }
434 remove {Events.RemoveHandler (MultilineChangedEvent, value); }
436 public event EventHandler ReadOnlyChanged {
437 add { Events.AddHandler (ReadOnlyChangedEvent, value); }
438 remove {Events.RemoveHandler (ReadOnlyChangedEvent, value); }
440 public event EventHandler TextBoxTextAlignChanged {
441 add { Events.AddHandler (TextBoxTextAlignChangedEvent, value); }
442 remove {Events.RemoveHandler (TextBoxTextAlignChangedEvent, value); }
446 #region Private Methods
447 private void HandleTextAlignChanged (object sender, EventArgs e)
449 EventHandler eh = (EventHandler)(Events [TextBoxTextAlignChangedEvent]);
454 private void HandleReadOnlyChanged (object sender, EventArgs e)
456 OnReadOnlyChanged (e);
459 private void HandleMultilineChanged (object sender, EventArgs e)
461 OnMultilineChanged (e);
464 private void HandleModifiedChanged (object sender, EventArgs e)
466 OnModifiedChanged (e);
469 private void HandleHideSelectionChanged (object sender, EventArgs e)
471 OnHideSelectionChanged (e);
474 private void HandleAcceptsTabChanged (object sender, EventArgs e)
476 OnAcceptsTabChanged (e);
479 private void HandleTextChanged (object sender, EventArgs e)
485 private class ToolStripTextBoxControl : TextBox
487 private BorderStyle border;
488 private Timer tooltip_timer;
489 private ToolTip tooltip_window;
490 private ToolStripItem owner_item;
492 public ToolStripTextBoxControl () : base ()
496 protected override void OnLostFocus (EventArgs e)
498 base.OnLostFocus (e);
502 protected override void OnMouseEnter (EventArgs e)
504 base.OnMouseEnter (e);
508 ToolTipTimer.Start ();
512 protected override void OnMouseLeave (EventArgs e)
514 base.OnMouseLeave (e);
517 ToolTipTimer.Stop ();
518 ToolTipWindow.Hide (this);
521 internal override void OnPaintInternal (PaintEventArgs e)
523 base.OnPaintInternal (e);
525 if ((this.Focused || this.Entered || border == BorderStyle.FixedSingle) && border != BorderStyle.None) {
526 ToolStripRenderer tsr = (this.Parent as ToolStrip).Renderer;
528 if (tsr is ToolStripProfessionalRenderer)
529 using (Pen p = new Pen ((tsr as ToolStripProfessionalRenderer).ColorTable.ButtonSelectedBorder))
530 e.Graphics.DrawRectangle (p, new Rectangle (0, 0, this.Width - 1, this.Height - 1));
534 internal BorderStyle Border {
541 internal ToolStripItem OwnerItem {
542 set { owner_item = value; }
545 #region Stuff for ToolTips
546 private bool ShowToolTips {
551 return (Parent as ToolStrip).ShowItemToolTips;
555 private Timer ToolTipTimer {
557 if (tooltip_timer == null) {
558 tooltip_timer = new Timer ();
559 tooltip_timer.Enabled = false;
560 tooltip_timer.Interval = 500;
561 tooltip_timer.Tick += new EventHandler (ToolTipTimer_Tick);
564 return tooltip_timer;
568 private ToolTip ToolTipWindow {
570 if (tooltip_window == null)
571 tooltip_window = new ToolTip ();
573 return tooltip_window;
577 private void ToolTipTimer_Tick (object o, EventArgs args)
579 string tooltip = owner_item.GetToolTip ();
581 if (!string.IsNullOrEmpty (tooltip))
582 ToolTipWindow.Present (this, tooltip);
584 ToolTipTimer.Stop ();