* Test/System.Windows.Forms/DataGridViewCellTest.cs: Added
[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                         base.Control.border_style = BorderStyle.None;
45                         this.border_style = BorderStyle.Fixed3D;
46                 }
47
48                 [EditorBrowsable (EditorBrowsableState.Never)]
49                 public ToolStripTextBox (Control c) : base (c)
50                 {
51                         throw new NotSupportedException ("This construtor cannot be used.");
52                 }
53
54                 public ToolStripTextBox (string name) : this ()
55                 {
56                         base.Name = name;
57                 }
58                 #endregion
59
60                 #region Public Properties
61                 [DefaultValue (false)]
62                 public bool AcceptsReturn {
63                         get { return this.TextBox.AcceptsReturn; }
64                         set { this.TextBox.AcceptsReturn = value; }
65                 }
66
67                 [DefaultValue (false)]
68                 public bool AcceptsTab {
69                         get { return this.TextBox.AcceptsTab; }
70                         set { this.TextBox.AcceptsTab = value; }
71                 }
72
73                 [MonoTODO ("AutoCompletion algorithm is currently not implemented.")]
74                 [Browsable (true)]
75                 [Localizable (true)]
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; }
82                 }
83                 
84                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
85                 [Browsable (true)]
86                 [DefaultValue (AutoCompleteMode.None)]
87                 [EditorBrowsable (EditorBrowsableState.Always)]
88                 public AutoCompleteMode AutoCompleteMode {
89                         get { return this.TextBox.AutoCompleteMode; }
90                         set { this.TextBox.AutoCompleteMode = value; }
91                 }
92
93                 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
94                 [Browsable (true)]
95                 [DefaultValue (AutoCompleteSource.None)]
96                 [EditorBrowsable (EditorBrowsableState.Always)]
97                 public AutoCompleteSource AutoCompleteSource {
98                         get { return this.TextBox.AutoCompleteSource; }
99                         set { this.TextBox.AutoCompleteSource = value; }
100                 }
101
102                 [Browsable (false)]
103                 [EditorBrowsable (EditorBrowsableState.Never)]
104                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
105                 public override Image BackgroundImage {
106                         get { return base.BackgroundImage; }
107                         set { base.BackgroundImage = value; }
108                 }
109
110                 [Browsable (false)]
111                 [EditorBrowsable (EditorBrowsableState.Never)]
112                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
113                 public override ImageLayout BackgroundImageLayout {
114                         get { return base.BackgroundImageLayout; }
115                         set { base.BackgroundImageLayout = value; }
116                 }
117
118                 [DefaultValue (BorderStyle.Fixed3D)]
119                 [DispId (-504)]
120                 public BorderStyle BorderStyle {
121                         get { return this.border_style; }
122                         set { 
123                                 if (this.border_style != value) {
124                                         this.border_style = value;
125                                         this.OnBorderStyleChanged (EventArgs.Empty);
126                                 }
127                         }
128                 }
129
130                 [Browsable (false)]
131                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
132                 public bool CanUndo {
133                         get { return this.TextBox.CanUndo; }
134                 }
135
136                 [DefaultValue (CharacterCasing.Normal)]
137                 public CharacterCasing CharacterCasing {
138                         get { return this.TextBox.CharacterCasing; }
139                         set { this.TextBox.CharacterCasing = value; }
140                 }
141
142                 [DefaultValue (true)]
143                 public bool HideSelection {
144                         get { return this.TextBox.HideSelection; }
145                         set { this.TextBox.HideSelection = value; }
146                 }
147
148                 [Localizable (true)]
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; }
154                 }
155
156                 [Localizable (true)]
157                 [DefaultValue (32767)]
158                 public int MaxLength {
159                         get { return this.TextBox.MaxLength; }
160                         set { this.TextBox.MaxLength = value; }
161                 }
162
163                 [Browsable (false)]
164                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
165                 public bool Modified {
166                         get { return this.TextBox.Modified; }
167                         set { this.TextBox.Modified = value; }
168                 }
169
170                 [Localizable (true)]
171                 [Browsable (false)]
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; }
178                 }
179
180                 [DefaultValue (false)]
181                 public bool ReadOnly {
182                         get { return this.TextBox.ReadOnly; }
183                         set { this.TextBox.ReadOnly = value; }
184                 }
185
186                 [Browsable (false)]
187                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
188                 public string SelectedText {
189                         get { return this.TextBox.SelectedText; }
190                         set { this.TextBox.SelectedText = value; }
191                 }
192
193                 [Browsable (false)]
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; }
198                 }
199
200                 [Browsable (false)]
201                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
202                 public int SelectionStart {
203                         get { return this.TextBox.SelectionStart; }
204                         set { this.TextBox.SelectionStart = value; }
205                 }
206
207                 [DefaultValue (true)]
208                 public bool ShortcutsEnabled {
209                         get { return this.TextBox.ShortcutsEnabled; }
210                         set { this.TextBox.ShortcutsEnabled = value; }
211                 }
212                 
213                 [Browsable (false)]
214                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
215                 public TextBox TextBox {
216                         get { return (TextBox)base.Control; }
217                 }
218
219                 [Localizable (true)]
220                 [DefaultValue (HorizontalAlignment.Left)]
221                 public HorizontalAlignment TextBoxTextAlign {
222                         get { return this.TextBox.TextAlign; }
223                         set { this.TextBox.TextAlign = value; }
224                 }
225
226                 [Browsable (false)]
227                 public int TextLength {
228                         get { return this.TextBox.TextLength; }
229                 }
230
231                 [Localizable (true)]
232                 [Browsable (false)]
233                 [EditorBrowsable (EditorBrowsableState.Never)]
234                 [DefaultValue (true)]
235                 public bool WordWrap {
236                         get { return this.TextBox.WordWrap; }
237                         set { this.TextBox.WordWrap = value; }
238                 }
239                 #endregion
240
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); } }
244                 #endregion
245
246                 #region Public Methods
247                 public void AppendText (string text)
248                 {
249                         this.TextBox.AppendText (text);
250                 }
251
252                 public void Clear ()
253                 {
254                         this.TextBox.Clear ();
255                 }
256
257                 public void ClearUndo ()
258                 {
259                         this.TextBox.ClearUndo ();
260                 }
261
262                 public void Copy ()
263                 {
264                         this.TextBox.Copy ();
265                 }
266
267                 public void Cut ()
268                 {
269                         this.TextBox.Cut ();
270                 }
271
272                 public void DeselectAll ()
273                 {
274                         this.TextBox.DeselectAll ();
275                 }
276                 
277                 public char GetCharFromPosition (Point pt)
278                 {
279                         return this.TextBox.GetCharFromPosition (pt);
280                 }
281                 
282                 public int GetCharIndexFromPosition (Point pt)
283                 {
284                         return this.TextBox.GetCharIndexFromPosition (pt);
285                 }
286                 
287                 public int GetFirstCharIndexFromLine (int lineNumber)
288                 {
289                         return this.TextBox.GetFirstCharIndexFromLine (lineNumber);
290                 }
291                 
292                 public int GetFirstCharIndexOfCurrentLine ()
293                 {
294                         return this.TextBox.GetFirstCharIndexOfCurrentLine ();
295                 }
296                 
297                 public int GetLineFromCharIndex (int index)
298                 {
299                         return this.TextBox.GetLineFromCharIndex (index);
300                 }
301                 
302                 public Point GetPositionFromCharIndex (int index)
303                 {
304                         return this.TextBox.GetPositionFromCharIndex (index);
305                 }
306                 
307                 public override Size GetPreferredSize (Size constrainingSize)
308                 {
309                         return this.DefaultSize;
310                 }
311
312                 public void Paste ()
313                 {
314                         this.TextBox.Paste ();
315                 }
316
317                 public void ScrollToCaret ()
318                 {
319                         this.TextBox.ScrollToCaret ();
320                 }
321
322                 public void Select (int start, int length)
323                 {
324                         this.TextBox.Select (start, length);
325                 }
326
327                 public void SelectAll ()
328                 {
329                         this.TextBox.SelectAll ();
330                 }
331
332                 public void Undo ()
333                 {
334                         this.TextBox.Undo ();
335                 }
336                 #endregion
337
338                 #region Protected Methods
339                 protected virtual void OnAcceptsTabChanged (EventArgs e)
340                 {
341                         EventHandler eh = (EventHandler)Events [AcceptsTabChangedEvent];
342                         if (eh != null)
343                                 eh (this, e);
344                 }
345
346                 protected virtual void OnBorderStyleChanged (EventArgs e)
347                 {
348                         EventHandler eh = (EventHandler)Events [BorderStyleChangedEvent];
349                         if (eh != null)
350                                 eh (this, e);
351                 }
352
353                 protected virtual void OnHideSelectionChanged (EventArgs e)
354                 {
355                         EventHandler eh = (EventHandler)Events [HideSelectionChangedEvent];
356                         if (eh != null)
357                                 eh (this, e);
358                 }
359
360                 protected virtual void OnModifiedChanged (EventArgs e)
361                 {
362                         EventHandler eh = (EventHandler)Events [ModifiedChangedEvent];
363                         if (eh != null)
364                                 eh (this, e);
365                 }
366
367                 protected virtual void OnMultilineChanged (EventArgs e)
368                 {
369                         EventHandler eh = (EventHandler)Events [MultilineChangedEvent];
370                         if (eh != null)
371                                 eh (this, e);
372                 }
373
374                 protected virtual void OnReadOnlyChanged (EventArgs e)
375                 {
376                         EventHandler eh = (EventHandler)Events [ReadOnlyChangedEvent];
377                         if (eh != null)
378                                 eh (this, e);
379                 }
380
381                 protected override void OnSubscribeControlEvents (Control control)
382                 {
383                         base.OnSubscribeControlEvents (control);
384
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);
391                 }
392
393                 protected override void OnUnsubscribeControlEvents (Control control)
394                 {
395                         base.OnUnsubscribeControlEvents (control);
396                 }
397                 #endregion
398
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 ();
407
408                 public event EventHandler AcceptsTabChanged {
409                         add { Events.AddHandler (AcceptsTabChangedEvent, value); }
410                         remove {Events.RemoveHandler (AcceptsTabChangedEvent, value); }
411                 }
412                 public event EventHandler BorderStyleChanged {
413                         add { Events.AddHandler (BorderStyleChangedEvent, value); }
414                         remove {Events.RemoveHandler (BorderStyleChangedEvent, value); }
415                 }
416                 public event EventHandler HideSelectionChanged {
417                         add { Events.AddHandler (HideSelectionChangedEvent, value); }
418                         remove {Events.RemoveHandler (HideSelectionChangedEvent, value); }
419                 }
420                 public event EventHandler ModifiedChanged {
421                         add { Events.AddHandler (ModifiedChangedEvent, value); }
422                         remove {Events.RemoveHandler (ModifiedChangedEvent, value); }
423                 }
424                 [Browsable (false)]
425                 [EditorBrowsable (EditorBrowsableState.Never)]
426                 public event EventHandler MultilineChanged {
427                         add { Events.AddHandler (MultilineChangedEvent, value); }
428                         remove {Events.RemoveHandler (MultilineChangedEvent, value); }
429                 }
430                 public event EventHandler ReadOnlyChanged {
431                         add { Events.AddHandler (ReadOnlyChangedEvent, value); }
432                         remove {Events.RemoveHandler (ReadOnlyChangedEvent, value); }
433                 }
434                 public event EventHandler TextBoxTextAlignChanged {
435                         add { Events.AddHandler (TextBoxTextAlignChangedEvent, value); }
436                         remove {Events.RemoveHandler (TextBoxTextAlignChangedEvent, value); }
437                 }
438                 #endregion
439
440                 #region Private Methods
441                 private void HandleTextAlignChanged (object sender, EventArgs e)
442                 {
443                         EventHandler eh = (EventHandler)(Events [TextBoxTextAlignChangedEvent]);
444                         if (eh != null)
445                                 eh (this, e);
446                 }
447
448                 private void HandleReadOnlyChanged (object sender, EventArgs e)
449                 {
450                         OnReadOnlyChanged (e);
451                 }
452
453                 private void HandleMultilineChanged (object sender, EventArgs e)
454                 {
455                         OnMultilineChanged (e);
456                 }
457
458                 private void HandleModifiedChanged (object sender, EventArgs e)
459                 {
460                         OnModifiedChanged (e);
461                 }
462
463                 private void HandleHideSelectionChanged (object sender, EventArgs e)
464                 {
465                         OnHideSelectionChanged (e);
466                 }
467
468                 private void HandleAcceptsTabChanged (object sender, EventArgs e)
469                 {
470                         OnAcceptsTabChanged (e);
471                 }
472                 #endregion
473
474                 private class ToolStripTextBoxControl : TextBox
475                 {
476                         public ToolStripTextBoxControl () : base ()
477                         {
478                         }
479
480                         protected override void OnMouseEnter (EventArgs e)
481                         {
482                                 base.OnMouseEnter (e);
483                                 Invalidate ();
484                         }
485
486                         protected override void OnMouseLeave (EventArgs e)
487                         {
488                                 base.OnMouseLeave (e);
489                                 Invalidate ();
490                         }
491
492                         protected override void OnPaint (PaintEventArgs e)
493                         {
494                                 base.OnPaint (e);
495                                 if (this.Focused || this.Entered || this.border_style == BorderStyle.FixedSingle) {
496                                         ToolStripRenderer tsr = (this.Parent as ToolStrip).Renderer;
497
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));
501                                 }
502                         }
503                 }
504         }
505 }
506 #endif