fixed tests
[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                 [Browsable (false)]
74                 [EditorBrowsable (EditorBrowsableState.Never)]
75                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
76                 public override Image BackgroundImage {
77                         get { return base.BackgroundImage; }
78                         set { base.BackgroundImage = value; }
79                 }
80
81                 [Browsable (false)]
82                 [EditorBrowsable (EditorBrowsableState.Never)]
83                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
84                 public override ImageLayout BackgroundImageLayout {
85                         get { return base.BackgroundImageLayout; }
86                         set { base.BackgroundImageLayout = value; }
87                 }
88
89                 [DefaultValue (BorderStyle.Fixed3D)]
90                 [DispId (-504)]
91                 public BorderStyle BorderStyle {
92                         get { return this.border_style; }
93                         set { 
94                                 if (this.border_style != value) {
95                                         this.border_style = value;
96                                         this.OnBorderStyleChanged (EventArgs.Empty);
97                                 }
98                         }
99                 }
100
101                 [Browsable (false)]
102                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
103                 public bool CanUndo {
104                         get { return this.TextBox.CanUndo; }
105                 }
106
107                 [DefaultValue (CharacterCasing.Normal)]
108                 public CharacterCasing CharacterCasing {
109                         get { return this.TextBox.CharacterCasing; }
110                         set { this.TextBox.CharacterCasing = value; }
111                 }
112
113                 [DefaultValue (true)]
114                 public bool HideSelection {
115                         get { return this.TextBox.HideSelection; }
116                         set { this.TextBox.HideSelection = value; }
117                 }
118
119                 [Localizable (true)]
120                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
121                 [Editor ("System.Windows.Forms.Design.StringArrayEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
122                 public string[] Lines {
123                         get { return this.TextBox.Lines; }
124                         set { this.TextBox.Lines = value; }
125                 }
126
127                 [Localizable (true)]
128                 [DefaultValue (32767)]
129                 public int MaxLength {
130                         get { return this.TextBox.MaxLength; }
131                         set { this.TextBox.MaxLength = value; }
132                 }
133
134                 [Browsable (false)]
135                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
136                 public bool Modified {
137                         get { return this.TextBox.Modified; }
138                         set { this.TextBox.Modified = value; }
139                 }
140
141                 [Localizable (true)]
142                 [Browsable (false)]
143                 [EditorBrowsable (EditorBrowsableState.Never)]
144                 [DefaultValue (false)]
145                 [RefreshProperties (RefreshProperties.All)]
146                 public bool Multiline {
147                         get { return this.TextBox.Multiline; }
148                         set { this.TextBox.Multiline = value; }
149                 }
150
151                 [DefaultValue (false)]
152                 public bool ReadOnly {
153                         get { return this.TextBox.ReadOnly; }
154                         set { this.TextBox.ReadOnly = value; }
155                 }
156
157                 [Browsable (false)]
158                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
159                 public string SelectedText {
160                         get { return this.TextBox.SelectedText; }
161                         set { this.TextBox.SelectedText = value; }
162                 }
163
164                 [Browsable (false)]
165                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
166                 public int SelectionLength {
167                         get { return this.TextBox.SelectionLength == -1 ? 0 : this.TextBox.SelectionLength; }
168                         set { this.TextBox.SelectionLength = value; }
169                 }
170
171                 [Browsable (false)]
172                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
173                 public int SelectionStart {
174                         get { return this.TextBox.SelectionStart; }
175                         set { this.TextBox.SelectionStart = value; }
176                 }
177
178                 [Browsable (false)]
179                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
180                 public TextBox TextBox {
181                         get { return (TextBox)base.Control; }
182                 }
183
184                 [Localizable (true)]
185                 [DefaultValue (HorizontalAlignment.Left)]
186                 public HorizontalAlignment TextBoxTextAlign {
187                         get { return this.TextBox.TextAlign; }
188                         set { this.TextBox.TextAlign = value; }
189                 }
190
191                 [Browsable (false)]
192                 public int TextLength {
193                         get { return this.TextBox.TextLength; }
194                 }
195
196                 [Localizable (true)]
197                 [Browsable (false)]
198                 [EditorBrowsable (EditorBrowsableState.Never)]
199                 [DefaultValue (true)]
200                 public bool WordWrap {
201                         get { return this.TextBox.WordWrap; }
202                         set { this.TextBox.WordWrap = value; }
203                 }
204                 #endregion
205
206                 #region Protected Properties
207                 protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
208                 protected override Size DefaultSize { get { return new Size (100, 22); } }
209                 #endregion
210
211                 #region Public Methods
212                 public void AppendText (string text)
213                 {
214                         this.TextBox.AppendText (text);
215                 }
216
217                 public void Clear ()
218                 {
219                         this.TextBox.Clear ();
220                 }
221
222                 public void ClearUndo ()
223                 {
224                         this.TextBox.ClearUndo ();
225                 }
226
227                 public void Copy ()
228                 {
229                         this.TextBox.Copy ();
230                 }
231
232                 public void Cut ()
233                 {
234                         this.TextBox.Cut ();
235                 }
236
237                 public override Size GetPreferredSize (Size constrainingSize)
238                 {
239                         return this.DefaultSize;
240                 }
241
242                 public void Paste ()
243                 {
244                         this.TextBox.Paste ();
245                 }
246
247                 public void ScrollToCaret ()
248                 {
249                         this.TextBox.ScrollToCaret ();
250                 }
251
252                 public void Select (int start, int length)
253                 {
254                         this.TextBox.Select (start, length);
255                 }
256
257                 public void SelectAll ()
258                 {
259                         this.TextBox.SelectAll ();
260                 }
261
262                 public void Undo ()
263                 {
264                         this.TextBox.Undo ();
265                 }
266                 #endregion
267
268                 #region Protected Methods
269                 protected virtual void OnAcceptsTabChanged (EventArgs e)
270                 {
271                         EventHandler eh = (EventHandler)Events [AcceptsTabChangedEvent];
272                         if (eh != null)
273                                 eh (this, e);
274                 }
275
276                 protected virtual void OnBorderStyleChanged (EventArgs e)
277                 {
278                         EventHandler eh = (EventHandler)Events [BorderStyleChangedEvent];
279                         if (eh != null)
280                                 eh (this, e);
281                 }
282
283                 protected virtual void OnHideSelectionChanged (EventArgs e)
284                 {
285                         EventHandler eh = (EventHandler)Events [HideSelectionChangedEvent];
286                         if (eh != null)
287                                 eh (this, e);
288                 }
289
290                 protected virtual void OnModifiedChanged (EventArgs e)
291                 {
292                         EventHandler eh = (EventHandler)Events [ModifiedChangedEvent];
293                         if (eh != null)
294                                 eh (this, e);
295                 }
296
297                 protected virtual void OnMultilineChanged (EventArgs e)
298                 {
299                         EventHandler eh = (EventHandler)Events [MultilineChangedEvent];
300                         if (eh != null)
301                                 eh (this, e);
302                 }
303
304                 protected virtual void OnReadOnlyChanged (EventArgs e)
305                 {
306                         EventHandler eh = (EventHandler)Events [ReadOnlyChangedEvent];
307                         if (eh != null)
308                                 eh (this, e);
309                 }
310
311                 protected override void OnSubscribeControlEvents (Control control)
312                 {
313                         base.OnSubscribeControlEvents (control);
314
315                         this.TextBox.AcceptsTabChanged += new EventHandler (HandleAcceptsTabChanged);
316                         this.TextBox.HideSelectionChanged += new EventHandler (HandleHideSelectionChanged);
317                         this.TextBox.ModifiedChanged += new EventHandler (HandleModifiedChanged);
318                         this.TextBox.MultilineChanged += new EventHandler (HandleMultilineChanged);
319                         this.TextBox.ReadOnlyChanged += new EventHandler (HandleReadOnlyChanged);
320                         this.TextBox.TextAlignChanged += new EventHandler (HandleTextAlignChanged);
321                 }
322
323                 protected override void OnUnsubscribeControlEvents (Control control)
324                 {
325                         base.OnUnsubscribeControlEvents (control);
326                 }
327                 #endregion
328
329                 #region Public Events
330                 static object AcceptsTabChangedEvent = new object ();
331                 static object BorderStyleChangedEvent = new object ();
332                 static object HideSelectionChangedEvent = new object ();
333                 static object ModifiedChangedEvent = new object ();
334                 static object MultilineChangedEvent = new object ();
335                 static object ReadOnlyChangedEvent = new object ();
336                 static object TextBoxTextAlignChangedEvent = new object ();
337
338                 public event EventHandler AcceptsTabChanged {
339                         add { Events.AddHandler (AcceptsTabChangedEvent, value); }
340                         remove {Events.RemoveHandler (AcceptsTabChangedEvent, value); }
341                 }
342                 public event EventHandler BorderStyleChanged {
343                         add { Events.AddHandler (BorderStyleChangedEvent, value); }
344                         remove {Events.RemoveHandler (BorderStyleChangedEvent, value); }
345                 }
346                 public event EventHandler HideSelectionChanged {
347                         add { Events.AddHandler (HideSelectionChangedEvent, value); }
348                         remove {Events.RemoveHandler (HideSelectionChangedEvent, value); }
349                 }
350                 public event EventHandler ModifiedChanged {
351                         add { Events.AddHandler (ModifiedChangedEvent, value); }
352                         remove {Events.RemoveHandler (ModifiedChangedEvent, value); }
353                 }
354                 [Browsable (false)]
355                 [EditorBrowsable (EditorBrowsableState.Never)]
356                 public event EventHandler MultilineChanged {
357                         add { Events.AddHandler (MultilineChangedEvent, value); }
358                         remove {Events.RemoveHandler (MultilineChangedEvent, value); }
359                 }
360                 public event EventHandler ReadOnlyChanged {
361                         add { Events.AddHandler (ReadOnlyChangedEvent, value); }
362                         remove {Events.RemoveHandler (ReadOnlyChangedEvent, value); }
363                 }
364                 public event EventHandler TextBoxTextAlignChanged {
365                         add { Events.AddHandler (TextBoxTextAlignChangedEvent, value); }
366                         remove {Events.RemoveHandler (TextBoxTextAlignChangedEvent, value); }
367                 }
368                 #endregion
369
370                 #region Private Methods
371                 private void HandleTextAlignChanged (object sender, EventArgs e)
372                 {
373                         EventHandler eh = (EventHandler)(Events [TextBoxTextAlignChangedEvent]);
374                         if (eh != null)
375                                 eh (this, e);
376                 }
377
378                 private void HandleReadOnlyChanged (object sender, EventArgs e)
379                 {
380                         OnReadOnlyChanged (e);
381                 }
382
383                 private void HandleMultilineChanged (object sender, EventArgs e)
384                 {
385                         OnMultilineChanged (e);
386                 }
387
388                 private void HandleModifiedChanged (object sender, EventArgs e)
389                 {
390                         OnModifiedChanged (e);
391                 }
392
393                 private void HandleHideSelectionChanged (object sender, EventArgs e)
394                 {
395                         OnHideSelectionChanged (e);
396                 }
397
398                 private void HandleAcceptsTabChanged (object sender, EventArgs e)
399                 {
400                         OnAcceptsTabChanged (e);
401                 }
402                 #endregion
403
404                 private class ToolStripTextBoxControl : TextBox
405                 {
406                         public ToolStripTextBoxControl () : base ()
407                         {
408                         }
409
410                         protected override void OnMouseEnter (EventArgs e)
411                         {
412                                 base.OnMouseEnter (e);
413                                 Invalidate ();
414                         }
415
416                         protected override void OnMouseLeave (EventArgs e)
417                         {
418                                 base.OnMouseLeave (e);
419                                 Invalidate ();
420                         }
421
422                         protected override void OnPaint (PaintEventArgs e)
423                         {
424                                 base.OnPaint (e);
425                                 if (this.Focused || is_entered || this.border_style == BorderStyle.FixedSingle) {
426                                         ToolStripRenderer tsr = (this.Parent as ToolStrip).Renderer;
427
428                                         if (tsr is ToolStripProfessionalRenderer)
429                                                 using (Pen p = new Pen ((tsr as ToolStripProfessionalRenderer).ColorTable.ButtonSelectedBorder))
430                                                         e.Graphics.DrawRectangle (p, new Rectangle (0, 0, this.Width - 1, this.Height - 1));
431                                 }
432                         }
433                 }
434         }
435 }
436 #endif