TextBox.cs 11 KB

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