ToolStripSplitButton.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // ToolStripSplitButton.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 ToolStripSplitButton : ToolStripDropDownItem
  35. {
  36. private bool button_pressed;
  37. private bool button_selected;
  38. private ToolStripItem default_item;
  39. private bool drop_down_button_selected;
  40. private int drop_down_button_width;
  41. #region Public Constructors
  42. public ToolStripSplitButton()
  43. : this (string.Empty, null, null, string.Empty)
  44. {
  45. }
  46. public ToolStripSplitButton (Image image)
  47. : this (string.Empty, image, null, string.Empty)
  48. {
  49. }
  50. public ToolStripSplitButton (string text)
  51. : this (text, null, null, string.Empty)
  52. {
  53. }
  54. public ToolStripSplitButton (string text, Image image)
  55. : this (text, image, null, string.Empty)
  56. {
  57. }
  58. public ToolStripSplitButton (string text, Image image, EventHandler onClick)
  59. : this (text, image, onClick, string.Empty)
  60. {
  61. }
  62. public ToolStripSplitButton (string text, Image image, params ToolStripItem[] dropDownItems)
  63. : base (text, image, dropDownItems)
  64. {
  65. this.ResetDropDownButtonWidth ();
  66. }
  67. public ToolStripSplitButton (string text, Image image, EventHandler onClick, string name)
  68. : base (text, image, onClick, name)
  69. {
  70. this.ResetDropDownButtonWidth ();
  71. }
  72. #endregion
  73. #region Public Properties
  74. public new bool AutoToolTip
  75. {
  76. get { return base.AutoToolTip; }
  77. set { base.AutoToolTip = value; }
  78. }
  79. public Rectangle ButtonBounds {
  80. get { return new Rectangle (this.Bounds.Left, this.Bounds.Top, this.Bounds.Width - this.drop_down_button_width - 1, this.Height); }
  81. }
  82. public bool ButtonPressed {
  83. get { return this.button_pressed; }
  84. }
  85. public bool ButtonSelected {
  86. get { return base.Selected; }
  87. }
  88. public Rectangle DropDownButtonBounds {
  89. get { return new Rectangle (this.Bounds.Right - this.drop_down_button_width, this.Bounds.Top, this.drop_down_button_width, this.Bounds.Height); }
  90. }
  91. public bool DropDownButtonPressed {
  92. get { return this.drop_down_button_selected || this.DropDown.Visible; }
  93. }
  94. public bool DropDownButtonSelected {
  95. get { return base.Selected; }
  96. }
  97. [DefaultValue (11)]
  98. public int DropDownButtonWidth {
  99. get { return this.drop_down_button_width; }
  100. set {
  101. if (value < 0)
  102. throw new ArgumentOutOfRangeException ();
  103. this.drop_down_button_width = value;
  104. }
  105. }
  106. public Rectangle SplitterBounds {
  107. get { return new Rectangle (this.Bounds.Width - this.drop_down_button_width - 1, this.Bounds.Top, 1, this.Height); }
  108. }
  109. #endregion
  110. #region Protected Properties
  111. protected override bool DefaultAutoToolTip {
  112. get { return true; }
  113. }
  114. protected internal override bool DismissWhenClicked {
  115. get { return true; }
  116. }
  117. #endregion
  118. #region Public Methods
  119. public override Size GetPreferredSize (Size constrainingSize)
  120. {
  121. // base should calculate the button part for us, add the splitter
  122. // and drop down arrow part to that
  123. Size s = base.GetPreferredSize (constrainingSize);
  124. s.Width += (this.drop_down_button_width - 2);
  125. return s;
  126. }
  127. public virtual void OnButtonDoubleClick (EventArgs e)
  128. {
  129. if (ButtonDoubleClick != null) ButtonDoubleClick (this, e);
  130. }
  131. public void PerformButtonClick ()
  132. {
  133. if (this.Enabled)
  134. this.OnButtonClick (EventArgs.Empty);
  135. }
  136. public virtual void ResetDropDownButtonWidth ()
  137. {
  138. this.DropDownButtonWidth = 11;
  139. }
  140. #endregion
  141. #region Protected Methods
  142. protected override ToolStripDropDown CreateDefaultDropDown ()
  143. {
  144. return new ToolStripDropDownMenu ();
  145. }
  146. protected virtual void OnButtonClick (EventArgs e)
  147. {
  148. if (ButtonClick != null) ButtonClick (this, e);
  149. }
  150. protected virtual void OnDefaultItemChanged (EventArgs e)
  151. {
  152. if (DefaultItemChanged != null) DefaultItemChanged (this, e);
  153. }
  154. protected override void OnMouseDown (MouseEventArgs e)
  155. {
  156. if (this.ButtonBounds.Contains (e.Location))
  157. {
  158. this.button_pressed = true;
  159. this.Invalidate ();
  160. base.OnMouseDown (e);
  161. }
  162. else if (this.DropDownButtonBounds.Contains (e.Location))
  163. {
  164. if (this.DropDown.Visible)
  165. this.HideDropDown (ToolStripDropDownCloseReason.ItemClicked);
  166. else
  167. this.ShowDropDown ();
  168. this.Invalidate ();
  169. }
  170. }
  171. protected override void OnMouseLeave (EventArgs e)
  172. {
  173. this.button_selected = false;
  174. this.drop_down_button_selected = false;
  175. this.button_pressed = false;
  176. this.Invalidate ();
  177. base.OnMouseLeave (e);
  178. }
  179. protected override void OnMouseUp (MouseEventArgs e)
  180. {
  181. this.button_pressed = false;
  182. this.Invalidate ();
  183. if (this.ButtonBounds.Contains (e.Location))
  184. base.OnMouseUp (e);
  185. }
  186. protected override void OnPaint (PaintEventArgs e)
  187. {
  188. base.OnPaint (e);
  189. if (this.Owner != null) {
  190. Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
  191. Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
  192. this.Owner.Renderer.DrawSplitButton (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
  193. Rectangle text_layout_rect;
  194. Rectangle image_layout_rect;
  195. Rectangle r = this.ContentRectangle;
  196. r.Width -= (this.drop_down_button_width + 1);
  197. this.CalculateTextAndImageRectangles (r, out text_layout_rect, out image_layout_rect);
  198. if (text_layout_rect != Rectangle.Empty)
  199. this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
  200. if (image_layout_rect != Rectangle.Empty)
  201. this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
  202. this.Owner.Renderer.DrawArrow (new ToolStripArrowRenderEventArgs (e.Graphics, this, new Rectangle (this.Width - 9, 1, 6, this.Height), Color.Black, ArrowDirection.Down));
  203. return;
  204. }
  205. }
  206. #endregion
  207. #region Public Events
  208. public event EventHandler ButtonClick;
  209. public event EventHandler ButtonDoubleClick;
  210. public event EventHandler DefaultItemChanged;
  211. #endregion
  212. }
  213. }
  214. #endif