TextBox.cs 12 KB

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