TextBox.cs 11 KB

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