ToolBarButton.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // System.Windows.Forms.ToolBarButton.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) 2004 Novell, Inc. (http://www.novell.com)
  24. //
  25. // Authors:
  26. // Ravindra ([email protected])
  27. //
  28. // TODO:
  29. // - Adding a button to two toolbars
  30. //
  31. // NOT COMPLETE
  32. using System.ComponentModel;
  33. using System.ComponentModel.Design;
  34. using System.Drawing;
  35. using System.Drawing.Imaging;
  36. namespace System.Windows.Forms
  37. {
  38. [DefaultProperty ("Text")]
  39. [Designer ("System.Windows.Forms.Design.ToolBarButtonDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  40. [DesignTimeVisible (false)]
  41. [ToolboxItem (false)]
  42. public class ToolBarButton : Component
  43. {
  44. #region instance variable
  45. private bool enabled = true;
  46. private int image_index = -1;
  47. private ContextMenu menu;
  48. private ToolBar parent;
  49. private bool partial_push = false;
  50. private bool pushed = false;
  51. private ToolBarButtonStyle style = ToolBarButtonStyle.PushButton;
  52. private object tag;
  53. private string text = "";
  54. private string tooltip = "";
  55. private bool visible = true;
  56. private Point location = new Point (ThemeEngine.Current.ToolBarGripWidth,
  57. ThemeEngine.Current.ToolBarGripWidth);
  58. internal bool dd_pressed = false; // to check for a mouse down on dropdown rect
  59. internal bool hilight = false; // to hilight buttons in flat style
  60. internal bool inside = false; // to handle the mouse move event with mouse pressed
  61. internal bool wrapper = false; // to mark a wrapping button
  62. internal bool pressed = false; // this is to check for mouse down on a button
  63. #endregion
  64. #region constructors
  65. public ToolBarButton () { }
  66. public ToolBarButton (string text)
  67. {
  68. this.text = text;
  69. }
  70. #endregion
  71. #region internal properties
  72. internal bool Hilight {
  73. get { return hilight; }
  74. set {
  75. if (! pushed)
  76. hilight = value;
  77. else
  78. hilight = false;
  79. }
  80. }
  81. internal Point Location {
  82. get { return location; }
  83. set { location = value; }
  84. }
  85. internal bool Pressed {
  86. get {
  87. if (pressed && inside)
  88. return true;
  89. else
  90. return false;
  91. }
  92. set { pressed = value; }
  93. }
  94. internal bool Wrapper {
  95. get { return wrapper; }
  96. set { wrapper = value; }
  97. }
  98. #endregion internal properties
  99. #region properties
  100. [DefaultValue (null)]
  101. [TypeConverter (typeof (ReferenceConverter))]
  102. public Menu DropDownMenu {
  103. get { return menu; }
  104. set {
  105. if (value is ContextMenu)
  106. menu = (ContextMenu) value;
  107. else
  108. throw new ArgumentException ("DropDownMenu must be of type ContextMenu.");
  109. }
  110. }
  111. [DefaultValue (true)]
  112. [Localizable (true)]
  113. public bool Enabled {
  114. get { return enabled; }
  115. set {
  116. if (value == enabled)
  117. return;
  118. enabled = value;
  119. if (parent != null)
  120. parent.Redraw (false);
  121. }
  122. }
  123. [DefaultValue (-1)]
  124. [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  125. [Localizable (true)]
  126. [TypeConverter (typeof (ImageIndexConverter))]
  127. public int ImageIndex {
  128. get { return image_index; }
  129. set {
  130. if (value < -1)
  131. throw new ArgumentException ("ImageIndex value must be above or equal to -1.");
  132. if (value == image_index)
  133. return;
  134. image_index = value;
  135. if (parent != null)
  136. parent.Redraw (true);
  137. }
  138. }
  139. [Browsable (false)]
  140. public ToolBar Parent {
  141. get { return parent; }
  142. }
  143. [DefaultValue (false)]
  144. public bool PartialPush {
  145. get { return partial_push; }
  146. set {
  147. if (value == partial_push)
  148. return;
  149. partial_push = value;
  150. if (parent != null)
  151. parent.Redraw (false);
  152. }
  153. }
  154. [DefaultValue (false)]
  155. public bool Pushed {
  156. get { return pushed; }
  157. set {
  158. if (value == pushed)
  159. return;
  160. pushed = value;
  161. if (pushed)
  162. hilight = false;
  163. if (parent != null)
  164. parent.Redraw (false);
  165. }
  166. }
  167. public Rectangle Rectangle {
  168. get {
  169. if (parent == null)
  170. return Rectangle.Empty;
  171. else if (visible && parent.Visible)
  172. return parent.GetChildBounds (this);
  173. else
  174. return Rectangle.Empty;
  175. }
  176. }
  177. [DefaultValue (ToolBarButtonStyle.PushButton)]
  178. [RefreshProperties (RefreshProperties.Repaint)]
  179. public ToolBarButtonStyle Style {
  180. get { return style; }
  181. set {
  182. if (value == style)
  183. return;
  184. style = value;
  185. if (parent != null)
  186. parent.Redraw (true);
  187. }
  188. }
  189. [Bindable (true)]
  190. [DefaultValue (null)]
  191. [Localizable (false)]
  192. [TypeConverter (typeof (StringConverter))]
  193. public object Tag {
  194. get { return tag; }
  195. set { tag = value; }
  196. }
  197. [DefaultValue (null)]
  198. [Localizable (true)]
  199. public string Text {
  200. get { return text; }
  201. set {
  202. if (value == text)
  203. return;
  204. text = value;
  205. if (parent != null)
  206. parent.Redraw (true);
  207. }
  208. }
  209. [DefaultValue (null)]
  210. [Localizable (true)]
  211. public string ToolTipText {
  212. get { return tooltip; }
  213. set { tooltip = value; }
  214. }
  215. [DefaultValue (true)]
  216. [Localizable (true)]
  217. public bool Visible {
  218. get { return visible; }
  219. set {
  220. if (value == visible)
  221. return;
  222. visible = value;
  223. if (parent != null)
  224. parent.Redraw (true);
  225. }
  226. }
  227. #endregion
  228. #region internal methods
  229. internal void SetParent (ToolBar parent)
  230. {
  231. this.parent = parent;
  232. }
  233. internal void Dump ()
  234. {
  235. Console.WriteLine ("TBButton: style: " + this.Style);
  236. Console.WriteLine ("TBButton: wrapper: " + this.Wrapper);
  237. Console.WriteLine ("TBButton: hilight: " + this.Hilight);
  238. Console.WriteLine ("TBButton: loc: " + this.Location);
  239. Console.WriteLine ("TBButton: rect: " + this.Rectangle);
  240. Console.WriteLine ("TBButton: txt: " + this.Text);
  241. Console.WriteLine ("TBButton: visible " + this.Visible);
  242. Console.WriteLine ("TBButton: enabled: " + this.Enabled);
  243. Console.WriteLine ("TBButton: image index: " + this.ImageIndex);
  244. Console.WriteLine ("TBButton: pushed: " + this.Pushed);
  245. Console.WriteLine ("TBButton: partial push: " + this.PartialPush);
  246. }
  247. #endregion
  248. #region methods
  249. protected override void Dispose (bool disposing)
  250. {
  251. base.Dispose (disposing);
  252. }
  253. public override string ToString ()
  254. {
  255. return string.Format ("ToolBarButton: {0}, Style: {1}", text, style);
  256. }
  257. #endregion
  258. }
  259. }