2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripTextBox.cs
1 //
2 // ToolStripTextBox.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.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28 #if NET_2_0
29 using System.Drawing;
30 using System.ComponentModel;
31 using System.Windows.Forms.Design;
32 using System.Runtime.InteropServices;
33
34 namespace System.Windows.Forms
35 {
36         [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
37         public class ToolStripTextBox : ToolStripControlHost
38         {
39                 private BorderStyle border_style;
40
41                 #region Public Constructors
42                 public ToolStripTextBox () : base (new ToolStripTextBoxControl ())
43                 {
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;
48                 }
49
50                 [EditorBrowsable (EditorBrowsableState.Never)]
51                 public ToolStripTextBox (Control c) : base (c)
52                 {
53                         throw new NotSupportedException ("This construtor cannot be used.");
54                 }
55
56                 public ToolStripTextBox (string name) : this ()
57                 {
58                         base.Name = name;
59                 }
60                 #endregion
61
62                 #region Public Properties
63                 [DefaultValue (false)]
64                 public bool AcceptsReturn {
65                         get { return this.TextBox.AcceptsReturn; }
66                         set { this.TextBox.AcceptsReturn = value; }
67                 }
68
69                 [DefaultValue (false)]
70                 public bool AcceptsTab {
71                         get { return this.TextBox.AcceptsTab; }
72                         set { this.TextBox.AcceptsTab = value; }
73                 }
74
75                 [MonoTODO ("AutoCompletion algorithm is currently not implemented.")]
76                 [Browsable (true)]
77                 [Localizable (true)]
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; }
84                 }
85                 
86                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
87                 [Browsable (true)]
88                 [DefaultValue (AutoCompleteMode.None)]
89                 [EditorBrowsable (EditorBrowsableState.Always)]
90                 public AutoCompleteMode AutoCompleteMode {
91                         get { return this.TextBox.AutoCompleteMode; }
92                         set { this.TextBox.AutoCompleteMode = value; }
93                 }
94
95                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
96                 [Browsable (true)]
97                 [DefaultValue (AutoCompleteSource.None)]
98                 [EditorBrowsable (EditorBrowsableState.Always)]
99                 public AutoCompleteSource AutoCompleteSource {
100                         get { return this.TextBox.AutoCompleteSource; }
101                         set { this.TextBox.AutoCompleteSource = value; }
102                 }
103
104                 [Browsable (false)]
105                 [EditorBrowsable (EditorBrowsableState.Never)]
106                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
107                 public override Image BackgroundImage {
108                         get { return base.BackgroundImage; }
109                         set { base.BackgroundImage = value; }
110                 }
111
112                 [Browsable (false)]
113                 [EditorBrowsable (EditorBrowsableState.Never)]
114                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
115                 public override ImageLayout BackgroundImageLayout {
116                         get { return base.BackgroundImageLayout; }
117                         set { base.BackgroundImageLayout = value; }
118                 }
119
120                 [DefaultValue (BorderStyle.Fixed3D)]
121                 [DispId (-504)]
122                 public BorderStyle BorderStyle {
123                         get { return this.border_style; }
124                         set { 
125                                 if (this.border_style != value) {
126                                         this.border_style = value;
127                                         (base.Control as ToolStripTextBoxControl).Border = value;
128                                         this.OnBorderStyleChanged (EventArgs.Empty);
129                                 }
130                         }
131                 }
132
133                 [Browsable (false)]
134                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
135                 public bool CanUndo {
136                         get { return this.TextBox.CanUndo; }
137                 }
138
139                 [DefaultValue (CharacterCasing.Normal)]
140                 public CharacterCasing CharacterCasing {
141                         get { return this.TextBox.CharacterCasing; }
142                         set { this.TextBox.CharacterCasing = value; }
143                 }
144
145                 [DefaultValue (true)]
146                 public bool HideSelection {
147                         get { return this.TextBox.HideSelection; }
148                         set { this.TextBox.HideSelection = value; }
149                 }
150
151                 [Localizable (true)]
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; }
157                 }
158
159                 [Localizable (true)]
160                 [DefaultValue (32767)]
161                 public int MaxLength {
162                         get { return this.TextBox.MaxLength; }
163                         set { this.TextBox.MaxLength = value; }
164                 }
165
166                 [Browsable (false)]
167                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
168                 public bool Modified {
169                         get { return this.TextBox.Modified; }
170                         set { this.TextBox.Modified = value; }
171                 }
172
173                 [Localizable (true)]
174                 [Browsable (false)]
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; }
181                 }
182
183                 [DefaultValue (false)]
184                 public bool ReadOnly {
185                         get { return this.TextBox.ReadOnly; }
186                         set { this.TextBox.ReadOnly = value; }
187                 }
188
189                 [Browsable (false)]
190                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
191                 public string SelectedText {
192                         get { return this.TextBox.SelectedText; }
193                         set { this.TextBox.SelectedText = value; }
194                 }
195
196                 [Browsable (false)]
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; }
201                 }
202
203                 [Browsable (false)]
204                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
205                 public int SelectionStart {
206                         get { return this.TextBox.SelectionStart; }
207                         set { this.TextBox.SelectionStart = value; }
208                 }
209
210                 [DefaultValue (true)]
211                 public bool ShortcutsEnabled {
212                         get { return this.TextBox.ShortcutsEnabled; }
213                         set { this.TextBox.ShortcutsEnabled = value; }
214                 }
215                 
216                 [Browsable (false)]
217                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
218                 public TextBox TextBox {
219                         get { return (TextBox)base.Control; }
220                 }
221
222                 [Localizable (true)]
223                 [DefaultValue (HorizontalAlignment.Left)]
224                 public HorizontalAlignment TextBoxTextAlign {
225                         get { return this.TextBox.TextAlign; }
226                         set { this.TextBox.TextAlign = value; }
227                 }
228
229                 [Browsable (false)]
230                 public int TextLength {
231                         get { return this.TextBox.TextLength; }
232                 }
233
234                 [Localizable (true)]
235                 [Browsable (false)]
236                 [EditorBrowsable (EditorBrowsableState.Never)]
237                 [DefaultValue (true)]
238                 public bool WordWrap {
239                         get { return this.TextBox.WordWrap; }
240                         set { this.TextBox.WordWrap = value; }
241                 }
242                 #endregion
243
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); } }
247                 #endregion
248
249                 #region Public Methods
250                 public void AppendText (string text)
251                 {
252                         this.TextBox.AppendText (text);
253                 }
254
255                 public void Clear ()
256                 {
257                         this.TextBox.Clear ();
258                 }
259
260                 public void ClearUndo ()
261                 {
262                         this.TextBox.ClearUndo ();
263                 }
264
265                 public void Copy ()
266                 {
267                         this.TextBox.Copy ();
268                 }
269
270                 public void Cut ()
271                 {
272                         this.TextBox.Cut ();
273                 }
274
275                 public void DeselectAll ()
276                 {
277                         this.TextBox.DeselectAll ();
278                 }
279                 
280                 public char GetCharFromPosition (Point pt)
281                 {
282                         return this.TextBox.GetCharFromPosition (pt);
283                 }
284                 
285                 public int GetCharIndexFromPosition (Point pt)
286                 {
287                         return this.TextBox.GetCharIndexFromPosition (pt);
288                 }
289                 
290                 public int GetFirstCharIndexFromLine (int lineNumber)
291                 {
292                         return this.TextBox.GetFirstCharIndexFromLine (lineNumber);
293                 }
294                 
295                 public int GetFirstCharIndexOfCurrentLine ()
296                 {
297                         return this.TextBox.GetFirstCharIndexOfCurrentLine ();
298                 }
299                 
300                 public int GetLineFromCharIndex (int index)
301                 {
302                         return this.TextBox.GetLineFromCharIndex (index);
303                 }
304                 
305                 public Point GetPositionFromCharIndex (int index)
306                 {
307                         return this.TextBox.GetPositionFromCharIndex (index);
308                 }
309                 
310                 public override Size GetPreferredSize (Size constrainingSize)
311                 {
312                         return base.GetPreferredSize (constrainingSize);
313                 }
314
315                 public void Paste ()
316                 {
317                         this.TextBox.Paste ();
318                 }
319
320                 public void ScrollToCaret ()
321                 {
322                         this.TextBox.ScrollToCaret ();
323                 }
324
325                 public void Select (int start, int length)
326                 {
327                         this.TextBox.Select (start, length);
328                 }
329
330                 public void SelectAll ()
331                 {
332                         this.TextBox.SelectAll ();
333                 }
334
335                 public void Undo ()
336                 {
337                         this.TextBox.Undo ();
338                 }
339                 #endregion
340
341                 #region Protected Methods
342                 protected virtual void OnAcceptsTabChanged (EventArgs e)
343                 {
344                         EventHandler eh = (EventHandler)Events [AcceptsTabChangedEvent];
345                         if (eh != null)
346                                 eh (this, e);
347                 }
348
349                 protected virtual void OnBorderStyleChanged (EventArgs e)
350                 {
351                         EventHandler eh = (EventHandler)Events [BorderStyleChangedEvent];
352                         if (eh != null)
353                                 eh (this, e);
354                 }
355
356                 protected virtual void OnHideSelectionChanged (EventArgs e)
357                 {
358                         EventHandler eh = (EventHandler)Events [HideSelectionChangedEvent];
359                         if (eh != null)
360                                 eh (this, e);
361                 }
362
363                 protected virtual void OnModifiedChanged (EventArgs e)
364                 {
365                         EventHandler eh = (EventHandler)Events [ModifiedChangedEvent];
366                         if (eh != null)
367                                 eh (this, e);
368                 }
369
370                 protected virtual void OnMultilineChanged (EventArgs e)
371                 {
372                         EventHandler eh = (EventHandler)Events [MultilineChangedEvent];
373                         if (eh != null)
374                                 eh (this, e);
375                 }
376
377                 protected virtual void OnReadOnlyChanged (EventArgs e)
378                 {
379                         EventHandler eh = (EventHandler)Events [ReadOnlyChangedEvent];
380                         if (eh != null)
381                                 eh (this, e);
382                 }
383
384                 protected override void OnSubscribeControlEvents (Control control)
385                 {
386                         base.OnSubscribeControlEvents (control);
387
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);
395                 }
396
397                 protected override void OnUnsubscribeControlEvents (Control control)
398                 {
399                         base.OnUnsubscribeControlEvents (control);
400                 }
401                 #endregion
402
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 ();
411
412                 public event EventHandler AcceptsTabChanged {
413                         add { Events.AddHandler (AcceptsTabChangedEvent, value); }
414                         remove {Events.RemoveHandler (AcceptsTabChangedEvent, value); }
415                 }
416                 public event EventHandler BorderStyleChanged {
417                         add { Events.AddHandler (BorderStyleChangedEvent, value); }
418                         remove {Events.RemoveHandler (BorderStyleChangedEvent, value); }
419                 }
420                 public event EventHandler HideSelectionChanged {
421                         add { Events.AddHandler (HideSelectionChangedEvent, value); }
422                         remove {Events.RemoveHandler (HideSelectionChangedEvent, value); }
423                 }
424                 public event EventHandler ModifiedChanged {
425                         add { Events.AddHandler (ModifiedChangedEvent, value); }
426                         remove {Events.RemoveHandler (ModifiedChangedEvent, value); }
427                 }
428                 [Browsable (false)]
429                 [EditorBrowsable (EditorBrowsableState.Never)]
430                 public event EventHandler MultilineChanged {
431                         add { Events.AddHandler (MultilineChangedEvent, value); }
432                         remove {Events.RemoveHandler (MultilineChangedEvent, value); }
433                 }
434                 public event EventHandler ReadOnlyChanged {
435                         add { Events.AddHandler (ReadOnlyChangedEvent, value); }
436                         remove {Events.RemoveHandler (ReadOnlyChangedEvent, value); }
437                 }
438                 public event EventHandler TextBoxTextAlignChanged {
439                         add { Events.AddHandler (TextBoxTextAlignChangedEvent, value); }
440                         remove {Events.RemoveHandler (TextBoxTextAlignChangedEvent, value); }
441                 }
442                 #endregion
443
444                 #region Private Methods
445                 private void HandleTextAlignChanged (object sender, EventArgs e)
446                 {
447                         EventHandler eh = (EventHandler)(Events [TextBoxTextAlignChangedEvent]);
448                         if (eh != null)
449                                 eh (this, e);
450                 }
451
452                 private void HandleReadOnlyChanged (object sender, EventArgs e)
453                 {
454                         OnReadOnlyChanged (e);
455                 }
456
457                 private void HandleMultilineChanged (object sender, EventArgs e)
458                 {
459                         OnMultilineChanged (e);
460                 }
461
462                 private void HandleModifiedChanged (object sender, EventArgs e)
463                 {
464                         OnModifiedChanged (e);
465                 }
466
467                 private void HandleHideSelectionChanged (object sender, EventArgs e)
468                 {
469                         OnHideSelectionChanged (e);
470                 }
471
472                 private void HandleAcceptsTabChanged (object sender, EventArgs e)
473                 {
474                         OnAcceptsTabChanged (e);
475                 }
476
477                 private void HandleTextChanged (object sender, EventArgs e)
478                 {
479                         OnTextChanged (e);
480                 }
481                 #endregion
482
483                 private class ToolStripTextBoxControl : TextBox
484                 {
485                         private BorderStyle border;
486                         private Timer tooltip_timer;
487                         private ToolTip tooltip_window;
488                         private ToolStripItem owner_item;
489                         
490                         public ToolStripTextBoxControl () : base ()
491                         {
492                         }
493
494                         protected override void OnLostFocus (EventArgs e)
495                         {
496                                 base.OnLostFocus (e);
497                                 Invalidate ();
498                         }
499                         
500                         protected override void OnMouseEnter (EventArgs e)
501                         {
502                                 base.OnMouseEnter (e);
503                                 Invalidate ();
504
505                                 if (ShowToolTips)
506                                         ToolTipTimer.Start ();
507
508                         }
509
510                         protected override void OnMouseLeave (EventArgs e)
511                         {
512                                 base.OnMouseLeave (e);
513                                 Invalidate ();
514
515                                 ToolTipTimer.Stop ();
516                                 ToolTipWindow.Hide (this);
517                         }
518
519                         internal override void OnPaintInternal (PaintEventArgs e)
520                         {
521                                 base.OnPaintInternal (e);
522
523                                 if ((this.Focused || this.Entered || border == BorderStyle.FixedSingle) && border != BorderStyle.None) {
524                                         ToolStripRenderer tsr = (this.Parent as ToolStrip).Renderer;
525
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));
529                                 }
530                         }
531                         
532                         internal BorderStyle Border {
533                                 set {
534                                         border = value;
535                                         Invalidate ();
536                                 }
537                         }
538
539                         internal ToolStripItem OwnerItem {
540                                 set { owner_item = value; }
541                         }
542                                         
543                         #region Stuff for ToolTips
544                         private bool ShowToolTips {
545                                 get {
546                                         if (Parent == null)
547                                                 return false;
548                                                 
549                                         return (Parent as ToolStrip).ShowItemToolTips;
550                                 }
551                         }
552
553                         private Timer ToolTipTimer {
554                                 get {
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);
560                                         }
561
562                                         return tooltip_timer;
563                                 }
564                         }
565
566                         private ToolTip ToolTipWindow {
567                                 get {
568                                         if (tooltip_window == null)
569                                                 tooltip_window = new ToolTip ();
570
571                                         return tooltip_window;
572                                 }
573                         }
574
575                         private void ToolTipTimer_Tick (object o, EventArgs args)
576                         {
577                                 string tooltip = owner_item.GetToolTip ();
578
579                                 if (!string.IsNullOrEmpty (tooltip))
580                                         ToolTipWindow.Present (this, tooltip);
581
582                                 ToolTipTimer.Stop ();
583                         }
584                         #endregion
585                 }
586         }
587 }
588 #endif