TextBox.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 [email protected]
  24. // Daniel Nauck (dna(at)mono-project(dot)de)
  25. //
  26. // NOT COMPLETE
  27. using System;
  28. using System.ComponentModel;
  29. using System.ComponentModel.Design;
  30. using System.Drawing;
  31. #if NET_2_0
  32. using System.Runtime.InteropServices;
  33. #endif
  34. namespace System.Windows.Forms {
  35. #if NET_2_0
  36. [ComVisible(true)]
  37. [ClassInterface (ClassInterfaceType.AutoDispatch)]
  38. [Designer ("System.Windows.Forms.Design.TextBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. #endif
  40. public class TextBox : TextBoxBase {
  41. #region Variables
  42. private ContextMenu menu;
  43. private MenuItem undo;
  44. private MenuItem cut;
  45. private MenuItem copy;
  46. private MenuItem paste;
  47. private MenuItem delete;
  48. private MenuItem select_all;
  49. #if NET_2_0
  50. private bool use_system_password_char = false;
  51. private AutoCompleteStringCollection auto_complete_custom_source = null;
  52. private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
  53. private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
  54. #endif
  55. #endregion // Variables
  56. #region Public Constructors
  57. public TextBox() {
  58. scrollbars = RichTextBoxScrollBars.None;
  59. alignment = HorizontalAlignment.Left;
  60. this.LostFocus +=new EventHandler(TextBox_LostFocus);
  61. BackColor = SystemColors.Window;
  62. ForeColor = SystemColors.WindowText;
  63. backcolor_set = false;
  64. SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
  65. SetStyle (ControlStyles.FixedHeight, true);
  66. undo = new MenuItem(Locale.GetText("&Undo"));
  67. cut = new MenuItem(Locale.GetText("Cu&t"));
  68. copy = new MenuItem(Locale.GetText("&Copy"));
  69. paste = new MenuItem(Locale.GetText("&Paste"));
  70. delete = new MenuItem(Locale.GetText("&Delete"));
  71. select_all = new MenuItem(Locale.GetText("Select &All"));
  72. menu = new ContextMenu(new MenuItem[] { undo, new MenuItem("-"), cut, copy, paste, delete, new MenuItem("-"), select_all});
  73. ContextMenu = menu;
  74. menu.Popup += new EventHandler(menu_Popup);
  75. undo.Click += new EventHandler(undo_Click);
  76. cut.Click += new EventHandler(cut_Click);
  77. copy.Click += new EventHandler(copy_Click);
  78. paste.Click += new EventHandler(paste_Click);
  79. delete.Click += new EventHandler(delete_Click);
  80. select_all.Click += new EventHandler(select_all_Click);
  81. document.multiline = false;
  82. }
  83. #endregion // Public Constructors
  84. #region Private & Internal Methods
  85. private void TextBox_LostFocus(object sender, EventArgs e) {
  86. if (hide_selection)
  87. document.InvalidateSelectionArea ();
  88. }
  89. internal override Color ChangeBackColor (Color backColor)
  90. {
  91. if (backColor == Color.Empty) {
  92. #if NET_2_0
  93. if (!ReadOnly)
  94. backColor = SystemColors.Window;
  95. #else
  96. backColor = SystemColors.Window;
  97. #endif
  98. backcolor_set = false;
  99. }
  100. return backColor;
  101. }
  102. #if NET_2_0
  103. void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
  104. if(auto_complete_source == AutoCompleteSource.CustomSource) {
  105. //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
  106. }
  107. }
  108. #endif
  109. #endregion // Private & Internal Methods
  110. #region Public Instance Properties
  111. #if NET_2_0
  112. [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
  113. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  114. [Browsable (true)]
  115. [EditorBrowsable (EditorBrowsableState.Always)]
  116. [Localizable (true)]
  117. [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
  118. "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  119. public AutoCompleteStringCollection AutoCompleteCustomSource {
  120. get {
  121. if(auto_complete_custom_source == null) {
  122. auto_complete_custom_source = new AutoCompleteStringCollection ();
  123. auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
  124. }
  125. return auto_complete_custom_source;
  126. }
  127. set {
  128. if(auto_complete_custom_source == value)
  129. return;
  130. if(auto_complete_custom_source != null) //remove eventhandler from old collection
  131. auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
  132. auto_complete_custom_source = value;
  133. if(auto_complete_custom_source != null)
  134. auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
  135. }
  136. }
  137. [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
  138. [Browsable (true)]
  139. [EditorBrowsable (EditorBrowsableState.Always)]
  140. [DefaultValue (AutoCompleteMode.None)]
  141. public AutoCompleteMode AutoCompleteMode {
  142. get { return auto_complete_mode; }
  143. set {
  144. if(auto_complete_mode == value)
  145. return;
  146. if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
  147. throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
  148. auto_complete_mode = value;
  149. }
  150. }
  151. [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
  152. [Browsable (true)]
  153. [EditorBrowsable (EditorBrowsableState.Always)]
  154. [DefaultValue (AutoCompleteSource.None)]
  155. [TypeConverter (typeof (TextBoxAutoCompleteSourceConverter))]
  156. public AutoCompleteSource AutoCompleteSource {
  157. get { return auto_complete_source; }
  158. set {
  159. if(auto_complete_source == value)
  160. return;
  161. if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
  162. throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
  163. auto_complete_source = value;
  164. }
  165. }
  166. [DefaultValue(false)]
  167. [RefreshProperties (RefreshProperties.Repaint)]
  168. public bool UseSystemPasswordChar {
  169. get {
  170. return use_system_password_char;
  171. }
  172. set {
  173. use_system_password_char = value;
  174. }
  175. }
  176. #endif
  177. [DefaultValue(false)]
  178. [MWFCategory("Behavior")]
  179. public bool AcceptsReturn {
  180. get {
  181. return accepts_return;
  182. }
  183. set {
  184. if (value != accepts_return) {
  185. accepts_return = value;
  186. }
  187. }
  188. }
  189. [DefaultValue(CharacterCasing.Normal)]
  190. [MWFCategory("Behavior")]
  191. public CharacterCasing CharacterCasing {
  192. get {
  193. return character_casing;
  194. }
  195. set {
  196. if (value != character_casing) {
  197. character_casing = value;
  198. }
  199. }
  200. }
  201. [Localizable(true)]
  202. [DefaultValue('\0')]
  203. [MWFCategory("Behavior")]
  204. [RefreshProperties (RefreshProperties.Repaint)]
  205. public char PasswordChar {
  206. get {
  207. #if NET_2_0
  208. if (use_system_password_char) {
  209. return '*';
  210. }
  211. #endif
  212. return password_char;
  213. }
  214. set {
  215. if (value != password_char) {
  216. password_char = value;
  217. if (!Multiline) {
  218. document.PasswordChar = value.ToString();
  219. } else {
  220. document.PasswordChar = "";
  221. }
  222. this.CalculateDocument();
  223. }
  224. }
  225. }
  226. [DefaultValue(ScrollBars.None)]
  227. [Localizable(true)]
  228. [MWFCategory("Appearance")]
  229. public ScrollBars ScrollBars {
  230. get {
  231. return (ScrollBars)scrollbars;
  232. }
  233. set {
  234. if (!Enum.IsDefined (typeof (ScrollBars), value))
  235. throw new InvalidEnumArgumentException ("value", (int) value,
  236. typeof (ScrollBars));
  237. if (value != (ScrollBars)scrollbars) {
  238. scrollbars = (RichTextBoxScrollBars)value;
  239. base.CalculateScrollBars();
  240. }
  241. }
  242. }
  243. #if ONLY_1_1
  244. [Browsable(false)]
  245. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  246. public override int SelectionLength {
  247. get {
  248. return base.SelectionLength;
  249. }
  250. set {
  251. base.SelectionLength = value;
  252. }
  253. }
  254. #endif
  255. public override string Text {
  256. get {
  257. return base.Text;
  258. }
  259. set {
  260. base.Text = value;
  261. }
  262. }
  263. [DefaultValue(HorizontalAlignment.Left)]
  264. [Localizable(true)]
  265. [MWFCategory("Appearance")]
  266. public HorizontalAlignment TextAlign {
  267. get {
  268. return alignment;
  269. }
  270. set {
  271. if (value != alignment) {
  272. alignment = value;
  273. document.alignment = value;
  274. // MS word-wraps if alignment isn't left
  275. if (Multiline) {
  276. if (alignment != HorizontalAlignment.Left) {
  277. document.Wrap = true;
  278. } else {
  279. document.Wrap = word_wrap;
  280. }
  281. }
  282. for (int i = 1; i <= document.Lines; i++) {
  283. document.GetLine(i).Alignment = value;
  284. }
  285. document.RecalculateDocument(CreateGraphicsInternal());
  286. OnTextAlignChanged(EventArgs.Empty);
  287. }
  288. }
  289. }
  290. #endregion // Public Instance Properties
  291. public void Paste (string text)
  292. {
  293. document.ReplaceSelection (CaseAdjust (text), false);
  294. ScrollToCaret();
  295. OnTextChanged(EventArgs.Empty);
  296. }
  297. #region Protected Instance Methods
  298. protected override CreateParams CreateParams {
  299. get {
  300. return base.CreateParams;
  301. }
  302. }
  303. #if ONLY_1_1
  304. protected override ImeMode DefaultImeMode {
  305. get {
  306. return base.DefaultImeMode;
  307. }
  308. }
  309. #endif
  310. #if NET_2_0
  311. protected override void Dispose (bool disposing)
  312. {
  313. base.Dispose (disposing);
  314. }
  315. #endif
  316. protected override bool IsInputKey(Keys keyData) {
  317. return base.IsInputKey (keyData);
  318. }
  319. protected override void OnGotFocus(EventArgs e) {
  320. base.OnGotFocus (e);
  321. if (selection_length == -1 && !has_been_focused)
  322. SelectAllNoScroll ();
  323. has_been_focused = true;
  324. }
  325. protected override void OnHandleCreated(EventArgs e) {
  326. base.OnHandleCreated (e);
  327. }
  328. #if ONLY_1_1
  329. protected override void OnMouseUp(MouseEventArgs e) {
  330. base.OnMouseUp (e);
  331. }
  332. #endif
  333. protected virtual void OnTextAlignChanged(EventArgs e) {
  334. EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
  335. if (eh != null)
  336. eh (this, e);
  337. }
  338. protected override void WndProc(ref Message m) {
  339. switch ((Msg)m.Msg) {
  340. case Msg.WM_LBUTTONDOWN:
  341. // When the textbox gets focus by LBUTTON (but not by middle or right)
  342. // it does not do the select all / scroll thing.
  343. has_been_focused = true;
  344. FocusInternal (true);
  345. break;
  346. }
  347. base.WndProc(ref m);
  348. }
  349. #endregion // Protected Instance Methods
  350. #region Events
  351. static object TextAlignChangedEvent = new object ();
  352. public event EventHandler TextAlignChanged {
  353. add { Events.AddHandler (TextAlignChangedEvent, value); }
  354. remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
  355. }
  356. #endregion // Events
  357. #region Private Methods
  358. internal override ContextMenu ContextMenuInternal {
  359. get {
  360. ContextMenu res = base.ContextMenuInternal;
  361. if (res == menu)
  362. return null;
  363. return res;
  364. }
  365. set {
  366. base.ContextMenuInternal = value;
  367. }
  368. }
  369. private void menu_Popup(object sender, EventArgs e) {
  370. if (SelectionLength == 0) {
  371. cut.Enabled = false;
  372. copy.Enabled = false;
  373. } else {
  374. cut.Enabled = true;
  375. copy.Enabled = true;
  376. }
  377. if (SelectionLength == TextLength) {
  378. select_all.Enabled = false;
  379. } else {
  380. select_all.Enabled = true;
  381. }
  382. if (!CanUndo) {
  383. undo.Enabled = false;
  384. } else {
  385. undo.Enabled = true;
  386. }
  387. if (ReadOnly) {
  388. undo.Enabled = cut.Enabled = paste.Enabled = delete.Enabled = false;
  389. }
  390. }
  391. private void undo_Click(object sender, EventArgs e) {
  392. Undo();
  393. }
  394. private void cut_Click(object sender, EventArgs e) {
  395. Cut();
  396. }
  397. private void copy_Click(object sender, EventArgs e) {
  398. Copy();
  399. }
  400. private void paste_Click(object sender, EventArgs e) {
  401. Paste();
  402. }
  403. private void delete_Click(object sender, EventArgs e) {
  404. SelectedText = "";
  405. }
  406. private void select_all_Click(object sender, EventArgs e) {
  407. SelectAll();
  408. }
  409. #endregion // Private Methods
  410. #if NET_2_0
  411. public override bool Multiline {
  412. get {
  413. return base.Multiline;
  414. }
  415. set {
  416. base.Multiline = value;
  417. }
  418. }
  419. protected override void OnBackColorChanged (EventArgs e)
  420. {
  421. base.OnBackColorChanged (e);
  422. }
  423. protected override void OnFontChanged (EventArgs e)
  424. {
  425. base.OnFontChanged (e);
  426. }
  427. protected override void OnHandleDestroyed (EventArgs e)
  428. {
  429. base.OnHandleDestroyed (e);
  430. }
  431. #endif
  432. }
  433. #if NET_2_0
  434. internal class TextBoxAutoCompleteSourceConverter : TypeConverter
  435. {
  436. }
  437. #endif
  438. }