2006-04-12 Peter Dennis Bartok <pbartok@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TextBox.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26
27 // NOT COMPLETE
28
29 using System;
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
32 using System.Drawing;
33
34 namespace System.Windows.Forms {
35         public class TextBox : TextBoxBase {
36                 #region Variables
37                 private ContextMenu     menu;
38                 private MenuItem        undo;
39                 private MenuItem        cut;
40                 private MenuItem        copy;
41                 private MenuItem        paste;
42                 private MenuItem        delete;
43                 private MenuItem        select_all;
44                 #endregion      // Variables
45
46                 #region Public Constructors
47                 public TextBox() {
48
49                         scrollbars = RichTextBoxScrollBars.None;
50                         alignment = HorizontalAlignment.Left;
51                         this.LostFocus +=new EventHandler(TextBox_LostFocus);
52                         this.BackColor = ThemeEngine.Current.ColorWindow;
53                         this.ForeColor = ThemeEngine.Current.ColorWindowText;
54
55                         SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
56                         SetStyle (ControlStyles.FixedHeight, true);
57
58                         undo = new MenuItem(Locale.GetText("&Undo"));
59                         cut = new MenuItem(Locale.GetText("Cu&t"));
60                         copy = new MenuItem(Locale.GetText("&Copy"));
61                         paste = new MenuItem(Locale.GetText("&Paste"));
62                         delete = new MenuItem(Locale.GetText("&Delete"));
63                         select_all = new MenuItem(Locale.GetText("Select &All"));
64
65                         menu = new ContextMenu(new MenuItem[] { undo, new MenuItem("-"), cut, copy, paste, delete, new MenuItem("-"), select_all});
66                         ContextMenu = menu;
67
68                         menu.Popup += new EventHandler(menu_Popup);
69                         undo.Click += new EventHandler(undo_Click);
70                         cut.Click += new EventHandler(cut_Click);
71                         copy.Click += new EventHandler(copy_Click);
72                         paste.Click += new EventHandler(paste_Click);
73                         delete.Click += new EventHandler(delete_Click);
74                         select_all.Click += new EventHandler(select_all_Click);
75                 }
76                 #endregion      // Public Constructors
77
78
79                 #region Private & Internal Methods
80                 private void TextBox_LostFocus(object sender, EventArgs e) {
81                         has_focus = false;
82                         Invalidate();
83                 }
84                 #endregion      // Private & Internal Methods
85
86                 #region Public Instance Properties
87 #if NET_2_0
88                 private bool use_system_password_char = false;
89
90                 [DefaultValue(false)]
91                 public bool UseSystemPasswordChar {
92                         get {
93                                 return use_system_password_char;
94                         }
95
96                         set {
97                                 use_system_password_char = value;
98                         }
99                 }
100 #endif
101
102                 [DefaultValue(false)]
103                 public bool AcceptsReturn {
104                         get {
105                                 return accepts_return;
106                         }
107
108                         set {
109                                 if (value != accepts_return) {
110                                         accepts_return = value;
111                                 }       
112                         }
113                 }
114
115                 [DefaultValue(CharacterCasing.Normal)]
116                 public CharacterCasing CharacterCasing {
117                         get {
118                                 return character_casing;
119                         }
120
121                         set {
122                                 if (value != character_casing) {
123                                         character_casing = value;
124                                 }
125                         }
126                 }
127
128                 [Localizable(true)]
129                 [DefaultValue('\0')]
130                 public char PasswordChar {
131                         get {
132 #if NET_2_0
133                                 if (use_system_password_char) {
134                                         return '*';
135                                 }
136 #endif
137                                 return password_char;
138                         }
139
140                         set {
141                                 if (value != password_char) {
142                                         password_char = value;
143                                         if (!multiline) {
144                                                 document.PasswordChar = value.ToString();
145                                         } else {
146                                                 document.PasswordChar = "";
147                                         }
148                                 }
149                         }
150                 }
151
152                 [DefaultValue(ScrollBars.None)]
153                 [Localizable(true)]
154                 public ScrollBars ScrollBars {
155                         get {
156                                 return (ScrollBars)scrollbars;
157                         }
158
159                         set {
160                                 if (value != (ScrollBars)scrollbars) {
161                                         scrollbars = (RichTextBoxScrollBars)value;
162                                         base.CalculateScrollBars();
163                                 }
164                         }
165                 }
166
167                 [Browsable(false)]
168                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
169                 public override int SelectionLength {
170                         get {
171                                 return base.SelectionLength;
172                         }
173                         set {
174                                 base.SelectionLength = value;
175                         }
176                 }
177
178
179                 public override string Text {
180                         get {
181                                 return base.Text;
182                         }
183
184                         set {
185                                 base.Text = value;
186                         }
187                 }
188
189                 [DefaultValue(HorizontalAlignment.Left)]
190                 [Localizable(true)]
191                 public HorizontalAlignment TextAlign {
192                         get {
193                                 return alignment;
194                         }
195
196                         set {
197                                 if (value != alignment) {
198                                         alignment = value;
199
200                                         // MS word-wraps if alignment isn't left
201                                         if (multiline) {
202                                                 if (alignment != HorizontalAlignment.Left) {
203                                                         document.Wrap = true;
204                                                 } else {
205                                                         document.Wrap = word_wrap;
206                                                 }
207                                         }
208
209                                         for (int i = 1; i <= document.Lines; i++) {
210                                                 document.GetLine(i).Alignment = value;
211                                         }
212                                         document.RecalculateDocument(CreateGraphicsInternal());
213                                         OnTextAlignChanged(EventArgs.Empty);
214                                 }
215                         }
216                 }
217                 #endregion      // Public Instance Properties
218
219                 #region Protected Instance Methods
220                 protected override CreateParams CreateParams {
221                         get {
222                                 return base.CreateParams;
223                         }
224                 }
225
226                 protected override ImeMode DefaultImeMode {
227                         get {
228                                 return base.DefaultImeMode;
229                         }
230                 }
231                 #endregion      // Protected Instance Methods
232
233                 #region Protected Instance Methods
234                 protected override bool IsInputKey(Keys keyData) {
235                         return base.IsInputKey (keyData);
236                 }
237
238                 protected override void OnGotFocus(EventArgs e) {
239                         has_focus=true;
240                         Invalidate();
241                         base.OnGotFocus (e);
242                 }
243
244                 protected override void OnHandleCreated(EventArgs e) {
245                         base.OnHandleCreated (e);
246                 }
247
248                 protected override void OnMouseUp(MouseEventArgs e) {
249                         base.OnMouseUp (e);
250                 }
251
252                 protected virtual void OnTextAlignChanged(EventArgs e) {
253                         if (TextAlignChanged != null) {
254                                 TextAlignChanged(this, e);
255                         }
256                 }
257
258                 protected override void WndProc(ref Message m) {
259                         base.WndProc(ref m);
260                 }
261                 #endregion      // Protected Instance Methods
262
263                 #region Events
264                 public event EventHandler TextAlignChanged;
265                 #endregion      // Events
266
267                 #region Private Methods
268                 private void menu_Popup(object sender, EventArgs e) {
269                         if (SelectionLength == 0) {
270                                 cut.Enabled = false;
271                                 copy.Enabled = false;
272                         } else {
273                                 cut.Enabled = true;
274                                 copy.Enabled = true;
275                         }
276
277                         if (SelectionLength == TextLength) {
278                                 select_all.Enabled = false;
279                         } else {
280                                 select_all.Enabled = true;
281                         }
282
283                         if (!CanUndo) {
284                                 undo.Enabled = false;
285                         } else {
286                                 undo.Enabled = true;
287                         }
288                 }
289
290                 private void undo_Click(object sender, EventArgs e) {
291                         Undo();
292                 }
293
294                 private void cut_Click(object sender, EventArgs e) {
295                         Cut();
296                 }
297
298                 private void copy_Click(object sender, EventArgs e) {
299                         Copy();
300                 }
301
302                 private void paste_Click(object sender, EventArgs e) {
303                         Paste();
304                 }
305
306                 private void delete_Click(object sender, EventArgs e) {
307                         SelectedText = "";
308                 }
309
310                 private void select_all_Click(object sender, EventArgs e) {
311                         SelectAll();
312                 }
313                 #endregion      // Private Methods
314         }
315 }