TextBox.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. //
  25. //
  26. // NOT COMPLETE
  27. using System;
  28. using System.ComponentModel;
  29. using System.ComponentModel.Design;
  30. using System.Drawing;
  31. namespace System.Windows.Forms {
  32. public class TextBox : TextBoxBase {
  33. #region Variables
  34. private ContextMenu menu;
  35. private MenuItem undo;
  36. private MenuItem cut;
  37. private MenuItem copy;
  38. private MenuItem paste;
  39. private MenuItem delete;
  40. private MenuItem select_all;
  41. #endregion // Variables
  42. #region Public Constructors
  43. public TextBox() {
  44. scrollbars = RichTextBoxScrollBars.None;
  45. alignment = HorizontalAlignment.Left;
  46. this.LostFocus +=new EventHandler(TextBox_LostFocus);
  47. this.BackColor = ThemeEngine.Current.ColorWindow;
  48. this.ForeColor = ThemeEngine.Current.ColorWindowText;
  49. SetStyle (ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
  50. SetStyle (ControlStyles.FixedHeight, true);
  51. undo = new MenuItem(Locale.GetText("&Undo"));
  52. cut = new MenuItem(Locale.GetText("Cu&t"));
  53. copy = new MenuItem(Locale.GetText("&Copy"));
  54. paste = new MenuItem(Locale.GetText("&Paste"));
  55. delete = new MenuItem(Locale.GetText("&Delete"));
  56. select_all = new MenuItem(Locale.GetText("Select &All"));
  57. menu = new ContextMenu(new MenuItem[] { undo, new MenuItem("-"), cut, copy, paste, delete, new MenuItem("-"), select_all});
  58. ContextMenu = menu;
  59. menu.Popup += new EventHandler(menu_Popup);
  60. undo.Click += new EventHandler(undo_Click);
  61. cut.Click += new EventHandler(cut_Click);
  62. copy.Click += new EventHandler(copy_Click);
  63. paste.Click += new EventHandler(paste_Click);
  64. delete.Click += new EventHandler(delete_Click);
  65. select_all.Click += new EventHandler(select_all_Click);
  66. }
  67. #endregion // Public Constructors
  68. #region Private & Internal Methods
  69. private void TextBox_LostFocus(object sender, EventArgs e) {
  70. has_focus = false;
  71. Invalidate();
  72. }
  73. #endregion // Private & Internal Methods
  74. #region Public Instance Properties
  75. #if NET_2_0
  76. private bool use_system_password_char = false;
  77. [DefaultValue(false)]
  78. public bool UseSystemPasswordChar {
  79. get {
  80. return use_system_password_char;
  81. }
  82. set {
  83. use_system_password_char = value;
  84. }
  85. }
  86. #endif
  87. [DefaultValue(false)]
  88. public bool AcceptsReturn {
  89. get {
  90. return accepts_return;
  91. }
  92. set {
  93. if (value != accepts_return) {
  94. accepts_return = value;
  95. }
  96. }
  97. }
  98. [DefaultValue(CharacterCasing.Normal)]
  99. public CharacterCasing CharacterCasing {
  100. get {
  101. return character_casing;
  102. }
  103. set {
  104. if (value != character_casing) {
  105. character_casing = value;
  106. }
  107. }
  108. }
  109. [Localizable(true)]
  110. [DefaultValue('\0')]
  111. public char PasswordChar {
  112. get {
  113. #if NET_2_0
  114. if (use_system_password_char) {
  115. return '*';
  116. }
  117. #endif
  118. return password_char;
  119. }
  120. set {
  121. if (value != password_char) {
  122. password_char = value;
  123. if (!multiline) {
  124. document.PasswordChar = value.ToString();
  125. } else {
  126. document.PasswordChar = "";
  127. }
  128. }
  129. }
  130. }
  131. [DefaultValue(ScrollBars.None)]
  132. [Localizable(true)]
  133. public ScrollBars ScrollBars {
  134. get {
  135. return (ScrollBars)scrollbars;
  136. }
  137. set {
  138. if (value != (ScrollBars)scrollbars) {
  139. scrollbars = (RichTextBoxScrollBars)value;
  140. base.CalculateScrollBars();
  141. }
  142. }
  143. }
  144. [Browsable(false)]
  145. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  146. public override int SelectionLength {
  147. get {
  148. return base.SelectionLength;
  149. }
  150. set {
  151. base.SelectionLength = value;
  152. }
  153. }
  154. public override string Text {
  155. get {
  156. return base.Text;
  157. }
  158. set {
  159. base.Text = value;
  160. }
  161. }
  162. [DefaultValue(HorizontalAlignment.Left)]
  163. [Localizable(true)]
  164. public HorizontalAlignment TextAlign {
  165. get {
  166. return alignment;
  167. }
  168. set {
  169. if (value != alignment) {
  170. alignment = value;
  171. // MS word-wraps if alignment isn't left
  172. if (multiline) {
  173. if (alignment != HorizontalAlignment.Left) {
  174. document.Wrap = true;
  175. } else {
  176. document.Wrap = word_wrap;
  177. }
  178. }
  179. for (int i = 1; i <= document.Lines; i++) {
  180. document.GetLine(i).Alignment = value;
  181. }
  182. document.RecalculateDocument(CreateGraphicsInternal());
  183. OnTextAlignChanged(EventArgs.Empty);
  184. }
  185. }
  186. }
  187. #endregion // Public Instance Properties
  188. #region Protected Instance Methods
  189. protected override CreateParams CreateParams {
  190. get {
  191. return base.CreateParams;
  192. }
  193. }
  194. protected override ImeMode DefaultImeMode {
  195. get {
  196. return base.DefaultImeMode;
  197. }
  198. }
  199. #endregion // Protected Instance Methods
  200. #region Protected Instance Methods
  201. protected override bool IsInputKey(Keys keyData) {
  202. return base.IsInputKey (keyData);
  203. }
  204. protected override void OnGotFocus(EventArgs e) {
  205. has_focus=true;
  206. Invalidate();
  207. base.OnGotFocus (e);
  208. }
  209. protected override void OnHandleCreated(EventArgs e) {
  210. base.OnHandleCreated (e);
  211. }
  212. protected override void OnMouseUp(MouseEventArgs e) {
  213. base.OnMouseUp (e);
  214. }
  215. protected virtual void OnTextAlignChanged(EventArgs e) {
  216. if (TextAlignChanged != null) {
  217. TextAlignChanged(this, e);
  218. }
  219. }
  220. protected override void WndProc(ref Message m) {
  221. base.WndProc(ref m);
  222. }
  223. #endregion // Protected Instance Methods
  224. #region Events
  225. public event EventHandler TextAlignChanged;
  226. #endregion // Events
  227. #region Private Methods
  228. private void menu_Popup(object sender, EventArgs e) {
  229. if (SelectionLength == 0) {
  230. cut.Enabled = false;
  231. copy.Enabled = false;
  232. } else {
  233. cut.Enabled = true;
  234. copy.Enabled = true;
  235. }
  236. if (SelectionLength == TextLength) {
  237. select_all.Enabled = false;
  238. } else {
  239. select_all.Enabled = true;
  240. }
  241. if (!CanUndo) {
  242. undo.Enabled = false;
  243. } else {
  244. undo.Enabled = true;
  245. }
  246. }
  247. private void undo_Click(object sender, EventArgs e) {
  248. Undo();
  249. }
  250. private void cut_Click(object sender, EventArgs e) {
  251. Cut();
  252. }
  253. private void copy_Click(object sender, EventArgs e) {
  254. Copy();
  255. }
  256. private void paste_Click(object sender, EventArgs e) {
  257. Paste();
  258. }
  259. private void delete_Click(object sender, EventArgs e) {
  260. SelectedText = "";
  261. }
  262. private void select_all_Click(object sender, EventArgs e) {
  263. SelectAll();
  264. }
  265. #endregion // Private Methods
  266. }
  267. }