ToolStripMenuItem.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // ToolStripMenuItem.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;
  30. using System.Drawing;
  31. using System.ComponentModel;
  32. namespace System.Windows.Forms
  33. {
  34. public class ToolStripMenuItem : ToolStripDropDownItem
  35. {
  36. private CheckState checked_state;
  37. private bool check_on_click;
  38. #region Public Constructors
  39. public ToolStripMenuItem ()
  40. : base ()
  41. {
  42. }
  43. public ToolStripMenuItem (Image image)
  44. : base (string.Empty, image, null, string.Empty)
  45. {
  46. }
  47. public ToolStripMenuItem (string text)
  48. : base (text, null, null, string.Empty)
  49. {
  50. }
  51. public ToolStripMenuItem (string text, Image image)
  52. : base (text, image, null, string.Empty)
  53. {
  54. }
  55. public ToolStripMenuItem (string text, Image image, EventHandler onClick)
  56. : base (text, image, onClick, string.Empty)
  57. {
  58. }
  59. public ToolStripMenuItem (string text, Image image, params ToolStripItem[] dropDownItems)
  60. : base (text, image, null, string.Empty)
  61. {
  62. if (dropDownItems != null)
  63. foreach (ToolStripItem tsi in dropDownItems)
  64. this.DropDownItems.Add (tsi);
  65. }
  66. public ToolStripMenuItem (string text, Image image, EventHandler onClick, Keys shortcutKeys)
  67. : base (text, image, onClick, string.Empty)
  68. {
  69. }
  70. public ToolStripMenuItem (string text, Image image, EventHandler onClick, string name)
  71. : base (text, image, onClick, name)
  72. {
  73. }
  74. #endregion
  75. #region Public Properties
  76. [Bindable (true)]
  77. [DefaultValue (false)]
  78. public bool Checked {
  79. get {
  80. switch (this.checked_state) {
  81. case CheckState.Unchecked:
  82. default:
  83. return false;
  84. case CheckState.Checked:
  85. case CheckState.Indeterminate:
  86. return true;
  87. }
  88. }
  89. set {
  90. if (this.checked_state != (value ? CheckState.Checked : CheckState.Unchecked)) {
  91. this.checked_state = value ? CheckState.Checked : CheckState.Unchecked;
  92. this.OnCheckedChanged (EventArgs.Empty);
  93. this.Invalidate ();
  94. }
  95. }
  96. }
  97. [DefaultValue (false)]
  98. public bool CheckOnClick {
  99. get { return this.check_on_click; }
  100. set { this.check_on_click = value; }
  101. }
  102. [Bindable (true)]
  103. [DefaultValue (CheckState.Unchecked)]
  104. public CheckState CheckState
  105. {
  106. get { return this.checked_state; }
  107. set
  108. {
  109. if (!Enum.IsDefined (typeof (CheckState), value))
  110. throw new InvalidEnumArgumentException (string.Format ("Enum argument value '{0}' is not valid for CheckState", value));
  111. this.checked_state = value;
  112. OnCheckStateChanged (EventArgs.Empty);
  113. this.Invalidate ();
  114. }
  115. }
  116. public override bool Enabled {
  117. get { return base.Enabled; }
  118. set { base.Enabled = value; }
  119. }
  120. #endregion
  121. #region Protected Properties
  122. protected internal override Padding DefaultMargin {
  123. get { return new Padding (0); }
  124. }
  125. protected override Padding DefaultPadding {
  126. get { return new Padding (4, 0, 4, 0); }
  127. }
  128. protected override Size DefaultSize {
  129. get { return new Size (32, 19); }
  130. }
  131. #endregion
  132. #region Protected Methods
  133. protected override ToolStripDropDown CreateDefaultDropDown ()
  134. {
  135. return base.CreateDefaultDropDown ();
  136. }
  137. protected override void Dispose (bool disposing)
  138. {
  139. base.Dispose (disposing);
  140. }
  141. protected virtual void OnCheckedChanged (EventArgs e)
  142. {
  143. if (CheckedChanged != null) CheckedChanged (this, e);
  144. }
  145. protected virtual void OnCheckStateChanged (EventArgs e)
  146. {
  147. if (CheckStateChanged != null) CheckStateChanged (this, e);
  148. }
  149. protected override void OnClick (EventArgs e)
  150. {
  151. if (!this.Enabled)
  152. return;
  153. base.OnClick (e);
  154. if (this.IsOnDropDown) {
  155. if (this.HasDropDownItems)
  156. return;
  157. this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
  158. (this.Parent as ToolStripDropDown).Close (ToolStripDropDownCloseReason.ItemClicked);
  159. Object parent = this.Parent;
  160. // Find the top level MenuStrip to inform it to close all open
  161. // dropdowns because this one was clicked
  162. while (parent != null) {
  163. if (parent is MenuStrip)
  164. (parent as MenuStrip).HideMenus (true, ToolStripDropDownCloseReason.ItemClicked);
  165. if (parent is ToolStripDropDown)
  166. parent = (parent as ToolStripDropDown).OwnerItem;
  167. else if (parent is ToolStripItem)
  168. parent = (parent as ToolStripItem).Parent;
  169. else
  170. break;
  171. }
  172. }
  173. if (this.check_on_click)
  174. this.Checked = !this.Checked;
  175. }
  176. protected override void OnDropDownHide (EventArgs e)
  177. {
  178. base.OnDropDownHide (e);
  179. }
  180. protected override void OnDropDownShow (EventArgs e)
  181. {
  182. base.OnDropDownShow (e);
  183. }
  184. protected override void OnFontChanged (EventArgs e)
  185. {
  186. base.OnFontChanged (e);
  187. }
  188. protected override void OnMouseDown (MouseEventArgs e)
  189. {
  190. base.OnMouseDown (e);
  191. if (this.HasDropDownItems)
  192. if (!this.DropDown.Visible)
  193. this.ShowDropDown ();
  194. }
  195. protected override void OnMouseEnter (EventArgs e)
  196. {
  197. base.OnMouseEnter (e);
  198. if (this.IsOnDropDown && this.HasDropDownItems)
  199. this.ShowDropDown ();
  200. }
  201. protected override void OnMouseLeave (EventArgs e)
  202. {
  203. base.OnMouseLeave (e);
  204. }
  205. protected override void OnMouseUp (MouseEventArgs e)
  206. {
  207. base.OnMouseUp (e);
  208. }
  209. protected override void OnOwnerChanged (EventArgs e)
  210. {
  211. base.OnOwnerChanged (e);
  212. }
  213. protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
  214. {
  215. base.OnPaint (e);
  216. if (this.Owner != null) {
  217. Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
  218. Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
  219. if (this.IsOnDropDown)
  220. this.Owner.Renderer.DrawMenuItemBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
  221. else
  222. this.Owner.Renderer.DrawButtonBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
  223. Rectangle text_layout_rect;
  224. Rectangle image_layout_rect;
  225. this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
  226. if (this.IsOnDropDown) {
  227. text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
  228. if (image_layout_rect != Rectangle.Empty)
  229. image_layout_rect = new Rectangle (4, 3, draw_image.Width, draw_image.Height);
  230. if (this.Checked)
  231. this.Owner.Renderer.DrawItemCheck (new ToolStripItemImageRenderEventArgs (e.Graphics, this, new Rectangle (2, 1, 19, 19)));
  232. }
  233. if (text_layout_rect != Rectangle.Empty)
  234. this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
  235. if (image_layout_rect != Rectangle.Empty)
  236. this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
  237. if (this.IsOnDropDown && this.HasDropDownItems)
  238. this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Bounds.Width - 17, 2, 10, 20), Color.Black, ArrowDirection.Right));
  239. return;
  240. }
  241. }
  242. #endregion
  243. #region Public Events
  244. public event EventHandler CheckedChanged;
  245. public event EventHandler CheckStateChanged;
  246. #endregion
  247. }
  248. }
  249. #endif