ToolBarButton.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. // - DropDownMenu
  30. // - Adding a button to two toolbars
  31. //
  32. // $Revision: 1.9 $
  33. // $Modtime: $
  34. // $Log: ToolBarButton.cs,v $
  35. // Revision 1.9 2004/10/14 11:10:47 ravindra
  36. // - Added an internal member 'inside' to handle mouse move
  37. // with mouse pressed ie mouse drag event.
  38. // - Changed 'Pressed' property to return true only when
  39. // 'inside' and 'pressed' are both true.
  40. // - Some coding style love.
  41. //
  42. // Revision 1.8 2004/10/05 08:42:20 ravindra
  43. // Added an internal member dd_pressed to handle clicks on DropDown arrow.
  44. //
  45. // Revision 1.7 2004/09/09 11:23:05 ravindra
  46. // Changes in ToolBarButton need to make it's parent redraw.
  47. //
  48. // Revision 1.6 2004/08/27 22:12:56 ravindra
  49. // Added TypeConverter attribute.
  50. //
  51. // Revision 1.5 2004/08/25 20:04:40 ravindra
  52. // Added the missing divider code and grip for ToolBar Control.
  53. //
  54. // Revision 1.4 2004/08/22 00:03:20 ravindra
  55. // Fixed toolbar control signatures.
  56. //
  57. // Revision 1.3 2004/08/21 01:52:08 ravindra
  58. // Improvments in mouse event handling in the ToolBar control.
  59. //
  60. // Revision 1.2 2004/08/17 02:00:54 ravindra
  61. // Added attributes.
  62. //
  63. // Revision 1.1 2004/08/15 23:13:15 ravindra
  64. // First Implementation of ToolBar control.
  65. //
  66. //
  67. // NOT COMPLETE
  68. using System.ComponentModel;
  69. using System.ComponentModel.Design;
  70. using System.Drawing;
  71. using System.Drawing.Imaging;
  72. namespace System.Windows.Forms
  73. {
  74. [DefaultProperty ("Text")]
  75. [Designer ("System.Windows.Forms.Design.ToolBarButtonDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  76. [DesignTimeVisible (false)]
  77. [ToolboxItem (false)]
  78. public class ToolBarButton : Component
  79. {
  80. #region instance variable
  81. //private ContextMenu menu; //NotImplemented
  82. private bool enabled = true;
  83. private int image_index = -1;
  84. private ToolBar parent;
  85. private bool partial_push = false;
  86. private bool pushed = false;
  87. private ToolBarButtonStyle style = ToolBarButtonStyle.PushButton;
  88. private object tag;
  89. private string text = "";
  90. private string tooltip = "";
  91. private bool visible = true;
  92. private Point location = new Point (ThemeEngine.Current.ToolBarGripWidth,
  93. ThemeEngine.Current.ToolBarGripWidth);
  94. internal bool dd_pressed = false; // to check for a mouse down on dropdown rect
  95. internal bool hilight = false; // to hilight buttons in flat style
  96. internal bool inside = false; // to handle the mouse move event with mouse pressed
  97. internal bool wrapper = false; // to mark a wrapping button
  98. internal bool pressed = false; // this is to check for mouse down on a button
  99. #endregion
  100. #region constructors
  101. public ToolBarButton () { }
  102. public ToolBarButton (string text)
  103. {
  104. this.text = text;
  105. }
  106. #endregion
  107. #region internal properties
  108. internal bool Hilight {
  109. get { return hilight; }
  110. set {
  111. if (! pushed)
  112. hilight = value;
  113. else
  114. hilight = false;
  115. }
  116. }
  117. internal Point Location {
  118. get { return location; }
  119. set { location = value; }
  120. }
  121. internal bool Pressed {
  122. get {
  123. if (pressed && inside)
  124. return true;
  125. else
  126. return false;
  127. }
  128. set { pressed = value; }
  129. }
  130. internal bool Wrapper {
  131. get { return wrapper; }
  132. set { wrapper = value; }
  133. }
  134. #endregion internal properties
  135. #region properties
  136. /*
  137. [DefaultValue (null)]
  138. [TypeConverter (typeof (ReferenceConverter))]
  139. public Menu DropDownMenu {
  140. get { return menu; }
  141. set {
  142. if (value is ContextMenu)
  143. menu = value;
  144. else
  145. throw new ArgumentException ("DropDownMenu must be of type ContextMenu.");
  146. }
  147. }
  148. */
  149. [DefaultValue (true)]
  150. [Localizable (true)]
  151. public bool Enabled {
  152. get { return enabled; }
  153. set {
  154. if (value == enabled)
  155. return;
  156. enabled = value;
  157. if (parent != null)
  158. parent.Redraw (false);
  159. }
  160. }
  161. [DefaultValue (-1)]
  162. [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  163. [Localizable (true)]
  164. [TypeConverter (typeof (ImageIndexConverter))]
  165. public int ImageIndex {
  166. get { return image_index; }
  167. set {
  168. if (value < -1)
  169. throw new ArgumentException ("ImageIndex value must be above or equal to -1.");
  170. if (value == image_index)
  171. return;
  172. image_index = value;
  173. if (parent != null)
  174. parent.Redraw (true);
  175. }
  176. }
  177. [Browsable (false)]
  178. public ToolBar Parent {
  179. get { return parent; }
  180. }
  181. [DefaultValue (false)]
  182. public bool PartialPush {
  183. get { return partial_push; }
  184. set {
  185. if (value == partial_push)
  186. return;
  187. partial_push = value;
  188. if (parent != null)
  189. parent.Redraw (false);
  190. }
  191. }
  192. [DefaultValue (false)]
  193. public bool Pushed {
  194. get { return pushed; }
  195. set {
  196. if (value == pushed)
  197. return;
  198. pushed = value;
  199. if (pushed)
  200. hilight = false;
  201. if (parent != null)
  202. parent.Redraw (false);
  203. }
  204. }
  205. public Rectangle Rectangle {
  206. get {
  207. if (parent == null)
  208. return Rectangle.Empty;
  209. else if (visible && parent.Visible)
  210. return parent.GetChildBounds (this);
  211. else
  212. return Rectangle.Empty;
  213. }
  214. }
  215. [DefaultValue (ToolBarButtonStyle.PushButton)]
  216. [RefreshProperties (RefreshProperties.Repaint)]
  217. public ToolBarButtonStyle Style {
  218. get { return style; }
  219. set {
  220. if (value == style)
  221. return;
  222. style = value;
  223. if (parent != null)
  224. parent.Redraw (true);
  225. }
  226. }
  227. [Bindable (true)]
  228. [DefaultValue (null)]
  229. [Localizable (false)]
  230. [TypeConverter (typeof (StringConverter))]
  231. public object Tag {
  232. get { return tag; }
  233. set { tag = value; }
  234. }
  235. [DefaultValue (null)]
  236. [Localizable (true)]
  237. public string Text {
  238. get { return text; }
  239. set {
  240. if (value == text)
  241. return;
  242. text = value;
  243. if (parent != null)
  244. parent.Redraw (true);
  245. }
  246. }
  247. [DefaultValue (null)]
  248. [Localizable (true)]
  249. public string ToolTipText {
  250. get { return tooltip; }
  251. set { tooltip = value; }
  252. }
  253. [DefaultValue (true)]
  254. [Localizable (true)]
  255. public bool Visible {
  256. get { return visible; }
  257. set {
  258. if (value == visible)
  259. return;
  260. visible = value;
  261. if (parent != null)
  262. parent.Redraw (true);
  263. }
  264. }
  265. #endregion
  266. #region internal methods
  267. internal void SetParent (ToolBar parent)
  268. {
  269. this.parent = parent;
  270. }
  271. internal void Dump ()
  272. {
  273. Console.WriteLine ("TBButton: style: " + this.Style);
  274. Console.WriteLine ("TBButton: wrapper: " + this.Wrapper);
  275. Console.WriteLine ("TBButton: hilight: " + this.Hilight);
  276. Console.WriteLine ("TBButton: loc: " + this.Location);
  277. Console.WriteLine ("TBButton: rect: " + this.Rectangle);
  278. Console.WriteLine ("TBButton: txt: " + this.Text);
  279. Console.WriteLine ("TBButton: visible " + this.Visible);
  280. Console.WriteLine ("TBButton: enabled: " + this.Enabled);
  281. Console.WriteLine ("TBButton: image index: " + this.ImageIndex);
  282. Console.WriteLine ("TBButton: pushed: " + this.Pushed);
  283. Console.WriteLine ("TBButton: partial push: " + this.PartialPush);
  284. }
  285. #endregion
  286. #region methods
  287. protected override void Dispose (bool disposing)
  288. {
  289. base.Dispose (disposing);
  290. }
  291. public override string ToString ()
  292. {
  293. return string.Format ("ToolBarButton: {0}, Style: {1}", text, style);
  294. }
  295. #endregion
  296. }
  297. }