TextBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. // MS word-wraps if alignment isn't left
  248. if (Multiline) {
  249. if (alignment != HorizontalAlignment.Left) {
  250. document.Wrap = true;
  251. } else {
  252. document.Wrap = word_wrap;
  253. }
  254. }
  255. for (int i = 1; i <= document.Lines; i++) {
  256. document.GetLine(i).Alignment = value;
  257. }
  258. document.RecalculateDocument(CreateGraphicsInternal());
  259. OnTextAlignChanged(EventArgs.Empty);
  260. }
  261. }
  262. }
  263. #endregion // Public Instance Properties
  264. #region Protected Instance Methods
  265. protected override CreateParams CreateParams {
  266. get {
  267. return base.CreateParams;
  268. }
  269. }
  270. protected override ImeMode DefaultImeMode {
  271. get {
  272. return base.DefaultImeMode;
  273. }
  274. }
  275. #endregion // Protected Instance Methods
  276. #region Protected Instance Methods
  277. #if NET_2_0
  278. protected override void Dispose (bool disposing)
  279. {
  280. base.Dispose (disposing);
  281. }
  282. #endif
  283. protected override bool IsInputKey(Keys keyData) {
  284. return base.IsInputKey (keyData);
  285. }
  286. protected override void OnGotFocus(EventArgs e) {
  287. base.OnGotFocus (e);
  288. }
  289. protected override void OnHandleCreated(EventArgs e) {
  290. base.OnHandleCreated (e);
  291. SelectAll ();
  292. }
  293. protected override void OnMouseUp(MouseEventArgs e) {
  294. base.OnMouseUp (e);
  295. }
  296. protected virtual void OnTextAlignChanged(EventArgs e) {
  297. EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
  298. if (eh != null)
  299. eh (this, e);
  300. }
  301. protected override void WndProc(ref Message m) {
  302. switch ((Msg)m.Msg) {
  303. case Msg.WM_LBUTTONDOWN:
  304. FocusInternal ();
  305. break;
  306. }
  307. base.WndProc(ref m);
  308. }
  309. #endregion // Protected Instance Methods
  310. #region Events
  311. static object TextAlignChangedEvent = new object ();
  312. public event EventHandler TextAlignChanged {
  313. add { Events.AddHandler (TextAlignChangedEvent, value); }
  314. remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
  315. }
  316. #endregion // Events
  317. #region Private Methods
  318. internal override ContextMenu GetContextMenuInternal ()
  319. {
  320. ContextMenu res = base.GetContextMenuInternal ();
  321. if (res == menu)
  322. return null;
  323. return res;
  324. }
  325. private void menu_Popup(object sender, EventArgs e) {
  326. if (SelectionLength == 0) {
  327. cut.Enabled = false;
  328. copy.Enabled = false;
  329. } else {
  330. cut.Enabled = true;
  331. copy.Enabled = true;
  332. }
  333. if (SelectionLength == TextLength) {
  334. select_all.Enabled = false;
  335. } else {
  336. select_all.Enabled = true;
  337. }
  338. if (!CanUndo) {
  339. undo.Enabled = false;
  340. } else {
  341. undo.Enabled = true;
  342. }
  343. }
  344. private void undo_Click(object sender, EventArgs e) {
  345. Undo();
  346. }
  347. private void cut_Click(object sender, EventArgs e) {
  348. Cut();
  349. }
  350. private void copy_Click(object sender, EventArgs e) {
  351. Copy();
  352. }
  353. private void paste_Click(object sender, EventArgs e) {
  354. Paste();
  355. }
  356. private void delete_Click(object sender, EventArgs e) {
  357. SelectedText = "";
  358. }
  359. private void select_all_Click(object sender, EventArgs e) {
  360. SelectAll();
  361. }
  362. #endregion // Private Methods
  363. #if NET_2_0
  364. public override bool Multiline {
  365. get {
  366. return base.Multiline;
  367. }
  368. set {
  369. base.Multiline = value;
  370. }
  371. }
  372. protected override void OnFontChanged (EventArgs e)
  373. {
  374. base.OnFontChanged (e);
  375. }
  376. #endif
  377. }
  378. }