ToolStripTextBox.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // ToolStripTextBox.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Jonathan Pobst
  24. //
  25. // Authors:
  26. // Jonathan Pobst ([email protected])
  27. //
  28. #if NET_2_0
  29. using System.Drawing;
  30. using System.ComponentModel;
  31. namespace System.Windows.Forms
  32. {
  33. public class ToolStripTextBox : ToolStripControlHost
  34. {
  35. private BorderStyle border_style;
  36. #region Public Constructors
  37. public ToolStripTextBox () : base (new ToolStripTextBoxControl ())
  38. {
  39. base.Control.border_style = BorderStyle.None;
  40. this.border_style = BorderStyle.Fixed3D;
  41. }
  42. public ToolStripTextBox (Control c) : base (c)
  43. {
  44. }
  45. public ToolStripTextBox (string name) : this ()
  46. {
  47. base.Control.Name = name;
  48. }
  49. #endregion
  50. #region Public Properties
  51. [DefaultValue (false)]
  52. public bool AcceptsReturn {
  53. get { return this.TextBox.AcceptsReturn; }
  54. set { this.TextBox.AcceptsReturn = value; }
  55. }
  56. [DefaultValue (false)]
  57. public bool AcceptsTab {
  58. get { return this.TextBox.AcceptsTab; }
  59. set { this.TextBox.AcceptsTab = value; }
  60. }
  61. [DefaultValue (BorderStyle.Fixed3D)]
  62. public BorderStyle BorderStyle {
  63. get { return this.border_style; }
  64. set { this.border_style = value; }
  65. }
  66. [Browsable (false)]
  67. public bool CanUndo {
  68. get { return this.TextBox.CanUndo; }
  69. }
  70. [DefaultValue (CharacterCasing.Normal)]
  71. public CharacterCasing CharacterCasing {
  72. get { return this.TextBox.CharacterCasing; }
  73. set { this.TextBox.CharacterCasing = value; }
  74. }
  75. [DefaultValue (true)]
  76. public bool HideSelection {
  77. get { return this.TextBox.HideSelection; }
  78. set { this.TextBox.HideSelection = value; }
  79. }
  80. [Localizable (true)]
  81. public string[] Lines {
  82. get { return this.TextBox.Lines; }
  83. set { this.TextBox.Lines = value; }
  84. }
  85. [Localizable (true)]
  86. [DefaultValue (32767)]
  87. public int MaxLength {
  88. get { return this.TextBox.MaxLength; }
  89. set { this.TextBox.MaxLength = value; }
  90. }
  91. [Browsable (false)]
  92. public bool Modified {
  93. get { return this.TextBox.Modified; }
  94. set { this.TextBox.Modified = value; }
  95. }
  96. [Localizable (true)]
  97. [Browsable (false)]
  98. [EditorBrowsable (EditorBrowsableState.Never)]
  99. [DefaultValue (false)]
  100. public bool Multiline {
  101. get { return this.TextBox.Multiline; }
  102. set { this.TextBox.Multiline = value; }
  103. }
  104. [DefaultValue (false)]
  105. public bool ReadOnly {
  106. get { return this.TextBox.ReadOnly; }
  107. set { this.TextBox.ReadOnly = value; }
  108. }
  109. [Browsable (false)]
  110. public string SelectedText {
  111. get { return this.TextBox.SelectedText; }
  112. set { this.TextBox.SelectedText = value; }
  113. }
  114. [Browsable (false)]
  115. public int SelectionLength {
  116. get { return this.TextBox.SelectionLength; }
  117. set { this.TextBox.SelectionLength = value; }
  118. }
  119. [Browsable (false)]
  120. public int SelectionStart {
  121. get { return this.TextBox.SelectionStart; }
  122. set { this.TextBox.SelectionStart = value; }
  123. }
  124. [Browsable (false)]
  125. public TextBox TextBox {
  126. get { return (TextBox)base.Control; }
  127. }
  128. [Localizable (true)]
  129. [DefaultValue (HorizontalAlignment.Left)]
  130. public HorizontalAlignment TextBoxTextAlign {
  131. get { return this.TextBox.TextAlign; }
  132. set { this.TextBox.TextAlign = value; }
  133. }
  134. [Browsable (false)]
  135. public int TextLength {
  136. get { return this.TextBox.TextLength; }
  137. }
  138. [Localizable (true)]
  139. [Browsable (false)]
  140. [EditorBrowsable (EditorBrowsableState.Never)]
  141. [DefaultValue (true)]
  142. public bool WordWrap {
  143. get { return this.TextBox.WordWrap; }
  144. set { this.TextBox.WordWrap = value; }
  145. }
  146. #endregion
  147. #region Protected Properties
  148. protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
  149. protected override Size DefaultSize { get { return new Size (100, 22); } }
  150. #endregion
  151. #region Public Methods
  152. public void AppendText (string text)
  153. {
  154. this.TextBox.AppendText (text);
  155. }
  156. public void Clear ()
  157. {
  158. this.TextBox.Clear ();
  159. }
  160. public void ClearUndo ()
  161. {
  162. this.TextBox.ClearUndo ();
  163. }
  164. public void Copy ()
  165. {
  166. this.TextBox.Copy ();
  167. }
  168. public void Cut ()
  169. {
  170. this.TextBox.Cut ();
  171. }
  172. public override Size GetPreferredSize (Size constrainingSize)
  173. {
  174. return this.DefaultSize;
  175. }
  176. public void Paste ()
  177. {
  178. this.TextBox.Paste ();
  179. }
  180. public void ScrollToCaret ()
  181. {
  182. this.TextBox.ScrollToCaret ();
  183. }
  184. public void Select (int start, int length)
  185. {
  186. this.TextBox.Select (start, length);
  187. }
  188. public void SelectAll ()
  189. {
  190. this.TextBox.SelectAll ();
  191. }
  192. public void Undo ()
  193. {
  194. this.TextBox.Undo ();
  195. }
  196. #endregion
  197. #region Protected Methods
  198. protected virtual void OnAcceptsTabChanged (EventArgs e)
  199. {
  200. if (AcceptsTabChanged != null) AcceptsTabChanged (this, e);
  201. }
  202. protected virtual void OnBorderStyleChanged (EventArgs e)
  203. {
  204. if (BorderStyleChanged != null) BorderStyleChanged (this, e);
  205. }
  206. protected virtual void OnHideSelectionChanged (EventArgs e)
  207. {
  208. if (HideSelectionChanged != null) HideSelectionChanged (this, e);
  209. }
  210. protected virtual void OnModifiedChanged (EventArgs e)
  211. {
  212. if (ModifiedChanged != null) ModifiedChanged (this, e);
  213. }
  214. protected virtual void OnMultilineChanged (EventArgs e)
  215. {
  216. if (MultilineChanged != null) MultilineChanged (this, e);
  217. }
  218. protected virtual void OnReadOnlyChanged (EventArgs e)
  219. {
  220. if (ReadOnlyChanged != null) ReadOnlyChanged (this, e);
  221. }
  222. protected override void OnSubscribeControlEvents (Control control)
  223. {
  224. base.OnSubscribeControlEvents (control);
  225. this.TextBox.AcceptsTabChanged += new EventHandler (HandleAcceptsTabChanged);
  226. this.TextBox.BorderStyleChanged += new EventHandler (HandleBorderStyleChanged);
  227. this.TextBox.HideSelectionChanged += new EventHandler (HandleHideSelectionChanged);
  228. this.TextBox.ModifiedChanged += new EventHandler (HandleModifiedChanged);
  229. this.TextBox.MultilineChanged += new EventHandler (HandleMultilineChanged);
  230. this.TextBox.ReadOnlyChanged += new EventHandler (HandleReadOnlyChanged);
  231. this.TextBox.TextAlignChanged += new EventHandler (HandleTextAlignChanged);
  232. }
  233. protected override void OnUnsubscribeControlEvents (Control control)
  234. {
  235. base.OnUnsubscribeControlEvents (control);
  236. }
  237. #endregion
  238. #region Public Events
  239. public event EventHandler AcceptsTabChanged;
  240. public event EventHandler BorderStyleChanged;
  241. public event EventHandler HideSelectionChanged;
  242. public event EventHandler ModifiedChanged;
  243. [Browsable (false)]
  244. [EditorBrowsable (EditorBrowsableState.Never)]
  245. public event EventHandler MultilineChanged;
  246. public event EventHandler ReadOnlyChanged;
  247. public event EventHandler TextBoxTextAlignChanged;
  248. #endregion
  249. #region Private Methods
  250. private void HandleTextAlignChanged (object sender, EventArgs e)
  251. {
  252. if (TextBoxTextAlignChanged != null) TextBoxTextAlignChanged (this, e);
  253. }
  254. private void HandleReadOnlyChanged (object sender, EventArgs e)
  255. {
  256. OnReadOnlyChanged (e);
  257. }
  258. private void HandleMultilineChanged (object sender, EventArgs e)
  259. {
  260. OnMultilineChanged (e);
  261. }
  262. private void HandleModifiedChanged (object sender, EventArgs e)
  263. {
  264. OnModifiedChanged (e);
  265. }
  266. private void HandleHideSelectionChanged (object sender, EventArgs e)
  267. {
  268. OnHideSelectionChanged (e);
  269. }
  270. private void HandleBorderStyleChanged (object sender, EventArgs e)
  271. {
  272. OnBorderStyleChanged (e);
  273. }
  274. private void HandleAcceptsTabChanged (object sender, EventArgs e)
  275. {
  276. OnAcceptsTabChanged (e);
  277. }
  278. #endregion
  279. private class ToolStripTextBoxControl : TextBox
  280. {
  281. public ToolStripTextBoxControl () : base ()
  282. {
  283. }
  284. protected override void OnMouseEnter (EventArgs e)
  285. {
  286. base.OnMouseEnter (e);
  287. Invalidate ();
  288. }
  289. protected override void OnMouseLeave (EventArgs e)
  290. {
  291. base.OnMouseLeave (e);
  292. Invalidate ();
  293. }
  294. protected override void OnPaint (PaintEventArgs e)
  295. {
  296. base.OnPaint (e);
  297. if (this.Focused || is_entered || this.border_style == BorderStyle.FixedSingle) {
  298. ToolStripRenderer tsr = (this.parent as ToolStrip).Renderer;
  299. if (tsr is ToolStripProfessionalRenderer)
  300. using (Pen p = new Pen ((tsr as ToolStripProfessionalRenderer).ColorTable.ButtonSelectedBorder))
  301. e.Graphics.DrawRectangle (p, new Rectangle (0, 0, this.Width - 1, this.Height - 1));
  302. }
  303. }
  304. }
  305. }
  306. }
  307. #endif