ToolStripControlHost.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // ToolStripControlHost.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 ToolStripControlHost : ToolStripItem
  34. {
  35. private Control control;
  36. private ContentAlignment control_align;
  37. private bool double_click_enabled;
  38. private ContentAlignment image_align;
  39. private ToolStripItemImageScaling image_scaling;
  40. private Color image_transparent_color;
  41. private ContentAlignment text_align;
  42. private TextImageRelation text_image_relation;
  43. #region Public Constructors
  44. public ToolStripControlHost (Control c) : base ()
  45. {
  46. if (c == null)
  47. throw new ArgumentNullException ("c");
  48. this.control = c;
  49. this.control_align = ContentAlignment.MiddleCenter;
  50. this.OnSubscribeControlEvents (this.control);
  51. }
  52. public ToolStripControlHost (Control c, string name) : this (c)
  53. {
  54. this.control.name = name;
  55. }
  56. #endregion
  57. #region Public Properties
  58. public override Color BackColor {
  59. get { return base.BackColor; }
  60. set {
  61. base.BackColor = value;
  62. control.BackColor = value;
  63. }
  64. }
  65. public override bool CanSelect {
  66. get { return control.CanSelect; }
  67. }
  68. [Browsable (false)]
  69. public Control Control {
  70. get { return this.control; }
  71. }
  72. [Browsable (false)]
  73. [DefaultValue (ContentAlignment.MiddleCenter)]
  74. public ContentAlignment ControlAlign {
  75. get { return this.control_align; }
  76. set {
  77. if (!Enum.IsDefined (typeof (ContentAlignment), value))
  78. throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for ContentAlignment", value));
  79. this.control_align = value;
  80. }
  81. }
  82. [Browsable (false)]
  83. [EditorBrowsable (EditorBrowsableState.Never)]
  84. public ToolStripItemDisplayStyle DisplayStyle {
  85. get { return base.DisplayStyle; }
  86. set { base.DisplayStyle = value; }
  87. }
  88. [Browsable (false)]
  89. [EditorBrowsable (EditorBrowsableState.Never)]
  90. [DefaultValue (false)]
  91. public bool DoubleClickEnabled {
  92. get { return this.double_click_enabled; }
  93. set { this.double_click_enabled = value; }
  94. }
  95. [Browsable (false)]
  96. [EditorBrowsable (EditorBrowsableState.Never)]
  97. public override bool Enabled {
  98. get { return base.Enabled; }
  99. set {
  100. base.Enabled = value;
  101. control.Enabled = value;
  102. }
  103. }
  104. public virtual bool Focused {
  105. get { return control.Focused; }
  106. }
  107. public override Font Font {
  108. get { return base.Font; }
  109. set {
  110. base.Font = value;
  111. control.Font = value;
  112. }
  113. }
  114. public override Color ForeColor {
  115. get { return base.ForeColor; }
  116. set {
  117. base.ForeColor = value;
  118. control.ForeColor = value;
  119. }
  120. }
  121. [Browsable (false)]
  122. [EditorBrowsable (EditorBrowsableState.Never)]
  123. public override Image Image {
  124. get { return base.Image; }
  125. set { base.Image = value; }
  126. }
  127. [Browsable (false)]
  128. [EditorBrowsable (EditorBrowsableState.Never)]
  129. public ContentAlignment ImageAlign {
  130. get { return this.image_align; }
  131. set { this.image_align = value; }
  132. }
  133. [Browsable (false)]
  134. [EditorBrowsable (EditorBrowsableState.Never)]
  135. public ToolStripItemImageScaling ImageScaling {
  136. get { return this.image_scaling; }
  137. set { this.image_scaling = value; }
  138. }
  139. [Browsable (false)]
  140. [EditorBrowsable (EditorBrowsableState.Never)]
  141. public Color ImageTransparentColor {
  142. get { return this.image_transparent_color; }
  143. set { this.image_transparent_color = value; }
  144. }
  145. public override bool Selected {
  146. get { return base.Selected; }
  147. }
  148. [EditorBrowsable (EditorBrowsableState.Advanced)]
  149. public override ISite Site {
  150. get { return base.Site; }
  151. set {
  152. base.Site = value;
  153. control.Site = value;
  154. }
  155. }
  156. [DefaultValue ("")]
  157. public override string Text {
  158. get { return base.Text; }
  159. set {
  160. base.Text = value;
  161. control.Text = value;
  162. }
  163. }
  164. [Browsable (false)]
  165. [EditorBrowsable (EditorBrowsableState.Never)]
  166. public ContentAlignment TextAlign {
  167. get { return this.text_align; }
  168. set { this.text_align = value; }
  169. }
  170. [Browsable (false)]
  171. [EditorBrowsable (EditorBrowsableState.Never)]
  172. public TextImageRelation TextImageRelation {
  173. get { return this.text_image_relation; }
  174. set { this.text_image_relation = value; }
  175. }
  176. #endregion
  177. #region Protected Properties
  178. protected override Size DefaultSize {
  179. get {
  180. if (control == null)
  181. return new Size (23, 23);
  182. return control.GetPreferredSize (Size.Empty);
  183. }
  184. }
  185. #endregion
  186. #region Public Methods
  187. public void Focus ()
  188. {
  189. control.Focus ();
  190. }
  191. public override Size GetPreferredSize (Size constrainingSize)
  192. {
  193. return this.Size;
  194. }
  195. [EditorBrowsable (EditorBrowsableState.Never)]
  196. public override void ResetBackColor ()
  197. {
  198. base.ResetBackColor ();
  199. }
  200. [EditorBrowsable (EditorBrowsableState.Never)]
  201. public override void ResetForeColor ()
  202. {
  203. base.ResetForeColor ();
  204. }
  205. #endregion
  206. #region Protected Methods
  207. protected override AccessibleObject CreateAccessibilityInstance ()
  208. {
  209. return this.Control.AccessibilityObject;
  210. }
  211. protected override void OnBoundsChanged ()
  212. {
  213. base.OnBoundsChanged ();
  214. if (this.Parent != null) {
  215. control.Size = this.Size;
  216. OnLayout (new LayoutEventArgs (null, string.Empty));
  217. }
  218. }
  219. protected virtual void OnEnter (EventArgs e)
  220. {
  221. if (Enter != null) Enter (this, e);
  222. }
  223. protected virtual void OnGotFocus (EventArgs e)
  224. {
  225. if (GotFocus != null) GotFocus (this, e);
  226. }
  227. protected virtual void OnHostedControlResize (EventArgs e)
  228. {
  229. }
  230. protected virtual void OnKeyDown (KeyEventArgs e)
  231. {
  232. if (KeyDown != null) KeyDown (this, e);
  233. }
  234. protected virtual void OnKeyPress (KeyPressEventArgs e)
  235. {
  236. if (KeyPress != null) KeyPress (this, e);
  237. }
  238. protected virtual void OnKeyUp (KeyEventArgs e)
  239. {
  240. if (KeyUp != null) KeyUp (this, e);
  241. }
  242. protected override void OnLayout (LayoutEventArgs e)
  243. {
  244. base.OnLayout (e);
  245. if (control != null)
  246. control.Bounds = AlignInRectangle (this.Bounds, control.Size, this.control_align);
  247. }
  248. protected virtual void OnLeave (EventArgs e)
  249. {
  250. if (Leave != null) Leave (this, e);
  251. }
  252. protected virtual void OnLostFocus (EventArgs e)
  253. {
  254. if (LostFocus != null) LostFocus (this, e);
  255. }
  256. protected override void OnPaint (PaintEventArgs e)
  257. {
  258. base.OnPaint (e);
  259. }
  260. protected override void OnParentChanged (ToolStrip oldParent, ToolStrip newParent)
  261. {
  262. base.OnParentChanged (oldParent, newParent);
  263. if (oldParent != null)
  264. oldParent.Controls.Remove (control);
  265. if (newParent != null)
  266. newParent.Controls.Add (control);
  267. }
  268. protected virtual void OnSubscribeControlEvents (Control control)
  269. {
  270. this.control.Enter += new EventHandler (HandleEnter);
  271. this.control.GotFocus += new EventHandler (HandleGotFocus);
  272. this.control.KeyDown += new KeyEventHandler (HandleKeyDown);
  273. this.control.KeyPress += new KeyPressEventHandler (HandleKeyPress);
  274. this.control.KeyUp += new KeyEventHandler (HandleKeyUp);
  275. this.control.Leave += new EventHandler (HandleLeave);
  276. this.control.LostFocus += new EventHandler (HandleLostFocus);
  277. this.control.Validated += new EventHandler (HandleValidated);
  278. this.control.Validating += new CancelEventHandler (HandleValidating);
  279. }
  280. protected virtual void OnUnsubscribeControlEvents (Control control)
  281. {
  282. }
  283. protected virtual void OnValidated (EventArgs e)
  284. {
  285. if (Validated != null) Validated (this, e);
  286. }
  287. protected virtual void OnValidating (CancelEventArgs e)
  288. {
  289. if (Validating != null) Validating (this, e);
  290. }
  291. #endregion
  292. #region Public Events
  293. [Browsable (false)]
  294. [EditorBrowsable (EditorBrowsableState.Never)]
  295. public event EventHandler DisplayStyleChanged;
  296. public event EventHandler Enter;
  297. [Browsable (false)]
  298. [EditorBrowsable (EditorBrowsableState.Advanced)]
  299. public event EventHandler GotFocus;
  300. public event KeyEventHandler KeyDown;
  301. public event KeyPressEventHandler KeyPress;
  302. public event KeyEventHandler KeyUp;
  303. public event EventHandler Leave;
  304. [Browsable (false)]
  305. [EditorBrowsable (EditorBrowsableState.Advanced)]
  306. public event EventHandler LostFocus;
  307. public event EventHandler Validated;
  308. public event CancelEventHandler Validating;
  309. #endregion
  310. #region Private Methods
  311. private void HandleEnter (object sender, EventArgs e)
  312. {
  313. this.OnEnter (e);
  314. }
  315. private void HandleGotFocus (object sender, EventArgs e)
  316. {
  317. this.OnGotFocus (e);
  318. }
  319. private void HandleKeyDown (object sender, KeyEventArgs e)
  320. {
  321. this.OnKeyDown (e);
  322. }
  323. private void HandleKeyPress (object sender, KeyPressEventArgs e)
  324. {
  325. this.OnKeyPress (e);
  326. }
  327. private void HandleKeyUp (object sender, KeyEventArgs e)
  328. {
  329. this.OnKeyUp (e);
  330. }
  331. private void HandleLeave (object sender, EventArgs e)
  332. {
  333. this.OnLeave (e);
  334. }
  335. private void HandleLostFocus (object sender, EventArgs e)
  336. {
  337. this.OnLostFocus (e);
  338. }
  339. private void HandleValidated (object sender, EventArgs e)
  340. {
  341. this.OnValidated (e);
  342. }
  343. private void HandleValidating (object sender, CancelEventArgs e)
  344. {
  345. this.OnValidating (e);
  346. }
  347. #endregion
  348. }
  349. }
  350. #endif