TextBox.cs 11 KB

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