ToolBarItem.cs
Upload User: nnpulika
Upload Date: 2013-02-15
Package Size: 597k
Code Size: 8k
Category:

StatusBar

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Diagnostics;
  5. using System.ComponentModel;
  6. using UtilityLibrary.Win32;
  7. using UtilityLibrary.Menus;
  8. using UtilityLibrary.Collections;
  9. namespace UtilityLibrary.CommandBars
  10. {
  11. #region Enumerations
  12. public enum ToolBarItemStyle
  13. {
  14. DropDownButton = 0,
  15. PushButton = 1,
  16. Separator = 2,
  17. ComboBox = 3
  18. }
  19. #endregion
  20. #region ToolBarItem
  21. /// <summary>
  22. /// Summary description for ToolBarItem.
  23. /// </summary>
  24. [ToolboxItem(false)]
  25. public class ToolBarItem : Component
  26. {
  27. #region Events
  28. public event EventHandler Changed;
  29. public event EventHandler Click;
  30. public event EventHandler DropDown;
  31. #endregion
  32. #region Class Variables
  33. Image image = null;
  34. string text = string.Empty;
  35. bool enabled = true;
  36. bool check = false;
  37. bool visible = true;
  38. bool dropped = false;
  39. bool raiseEvents = true;
  40. Keys sCut = Keys.None;
  41. ToolBarItemStyle style = ToolBarItemStyle.PushButton;
  42. string toolTip = null;
  43. Object tag = null;
  44. EventHandler clickHandler = null;
  45. ComboBox comboBox = null;
  46. // ToolBar index
  47. int index = -1;
  48. // ImageList index
  49. int imageListIndex = -1;
  50. ToolBarEx parentToolBar = null;
  51. MenuItemExCollection menuItems = null;
  52. CommandBarMenu commandBarMenu = null;
  53. #endregion
  54. #region Constructors
  55. public ToolBarItem(ToolBarItemStyle style, ComboBox comboBox)
  56. {
  57. Debug.Assert(comboBox != null);
  58. Debug.Assert(style == ToolBarItemStyle.ComboBox);
  59.             this.style = style;
  60. this.comboBox = comboBox;
  61. }
  62. public ToolBarItem()
  63. {
  64. Initialize(null, null, null, Keys.None, null, -1);
  65. }
  66. public ToolBarItem(string text)
  67. {
  68. Initialize(null, text, null, Keys.None, null, -1);
  69. }
  70. public ToolBarItem(string text, EventHandler clickHandler)
  71. {
  72. Initialize(null, text, clickHandler, Keys.None, null, -1);
  73. }
  74. public ToolBarItem(string text, EventHandler clickHandler, int imageListIndex)
  75. {
  76. Initialize(null, text, clickHandler, Keys.None, null, imageListIndex);
  77. }
  78. public ToolBarItem(Image image, EventHandler clickHandler)
  79. {
  80. Initialize(image, null, clickHandler, Keys.None, null, -1);
  81. }
  82. public ToolBarItem(Image image, string text, EventHandler clickHandler, Keys shortcut)
  83. {
  84. Initialize(image, text, clickHandler, shortcut, null, -1);
  85. }
  86. public ToolBarItem(string text, EventHandler clickHandler, Keys shortcut, int imageListIndex)
  87. {
  88. Initialize(null, text, clickHandler, shortcut, null, imageListIndex);
  89. }
  90. public ToolBarItem(Image image, EventHandler clickHandler, Keys shortCut)
  91. {
  92. Initialize(image, null, clickHandler, shortCut, null, -1);
  93. }
  94. public ToolBarItem(Image image, EventHandler clickHandler, Keys shortCut, string toolTip)
  95. {
  96. Initialize(image, null, clickHandler, shortCut, toolTip, -1);
  97. }
  98. public ToolBarItem(string text, EventHandler clickHandler, Keys shortCut, string toolTip, int imageListIndex)
  99. {
  100. Initialize(null, text, clickHandler, shortCut, toolTip, imageListIndex);
  101. }
  102. public ToolBarItem(Image image, string text, EventHandler clickHandler, Keys shortCut, string toolTip)
  103. {
  104. Initialize(image, text, clickHandler, shortCut, toolTip, -1);
  105. }
  106. private void Initialize(Image image, string text, EventHandler clickHandler, Keys shortCut , string toolTip, int imageListIndex)
  107. {
  108. this.image = image;
  109. this.text = text;
  110. if ( clickHandler != null )
  111. {
  112. this.Click += clickHandler;
  113. this.clickHandler = clickHandler;
  114. }
  115. this.sCut = shortCut;
  116. this.toolTip = toolTip;
  117. menuItems = new MenuItemExCollection();
  118. commandBarMenu = new CommandBarMenu(menuItems);
  119. this.imageListIndex = imageListIndex;
  120. }
  121. #endregion
  122. #region Properties
  123.         internal bool RaiseEvents
  124. {
  125. set { raiseEvents = value;}
  126. get { return raiseEvents; }
  127. }
  128. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  129. [Editor(typeof(UtilityLibrary.Designers.ToolBarItemMenuItemsEditor), typeof(System.Drawing.Design.UITypeEditor))]
  130. public MenuItemExCollection MenuItems
  131. {
  132. set {
  133. if ( menuItems != value)
  134. {
  135.     menuItems = value; 
  136. commandBarMenu = new CommandBarMenu(menuItems);
  137. RaiseChanged();
  138. }
  139. }
  140. get 
  141. return menuItems; 
  142. }
  143. }
  144. public CommandBarMenu ToolBarItemMenu
  145. {
  146. set { commandBarMenu = value; }
  147. get 
  148. {
  149.                 // Make sure to return one that
  150. // it is actually initialize with
  151. // the item in the MenuItemExCollection
  152. // -- In case more MenuItemEx items were added after
  153. // the construction of the ToolBarItem
  154. commandBarMenu = new CommandBarMenu(menuItems);
  155. return commandBarMenu; 
  156. }
  157. }
  158. public ComboBox ComboBox
  159. {
  160. set { comboBox = value; }
  161. get { return comboBox; }
  162. }
  163. public bool Visible
  164. {
  165. set { if ( visible != value ) { visible = value; RaiseChanged();} }
  166. get { return visible; }
  167. }
  168. public object Tag
  169. {
  170. // No need to raised a changed event since this does not alter the 
  171. // state of the ToolbarItem but it is just a convinience for the user
  172. // to associate an object with the ToolbarItem
  173. set { tag = value;  }
  174. get { return tag; }
  175. }
  176. public Image Image
  177. {
  178. set { if ( image != value) { image = value; RaiseChanged();}  }
  179. get { return image; }
  180. }
  181. public string Text
  182. {
  183. set { if (text != value) { text = value; RaiseChanged(); } }
  184. get { return text; }
  185. }
  186. public bool Enabled
  187. {
  188. set 
  189. {
  190. if (enabled != value) 
  191. enabled = value; 
  192. if ( ComboBox != null)
  193. ComboBox.Enabled = value;
  194. RaiseChanged(); 
  195. } }
  196. get { return enabled; }
  197. }
  198. public bool Checked
  199. {
  200. set { if (check != value) { check = value; RaiseChanged(); } }
  201. get { return check; }
  202. }
  203. public Keys Shortcut
  204. {
  205. set { if (sCut != value) { sCut = value; RaiseChanged(); } }
  206. get { return sCut; }
  207. }
  208. public ToolBarItemStyle Style
  209. {
  210. set { if (style != value) { style = value; RaiseChanged(); } }
  211. get { return style; }
  212. }
  213. public string ToolTip 
  214. {
  215. set { if (toolTip != value) { toolTip = value; RaiseChanged(); } }
  216. get { return toolTip; }
  217. }
  218. public int Index
  219. {
  220. set { index = value; }
  221. get { return index; }
  222. }
  223. public bool Dropped
  224. {
  225. set { dropped = value; }
  226. get { return dropped; }
  227. }
  228. public int ImageListIndex
  229. {
  230. set { imageListIndex = value; }
  231. get { return imageListIndex; }
  232. }
  233. public Rectangle ItemRectangle
  234. {
  235. get
  236. {
  237. // toolBar object must have been setup right before
  238. // rendering the toolbar by the toolbar itself and for all items
  239. if ( parentToolBar != null)
  240. {
  241. RECT rect = new RECT();
  242. WindowsAPI.SendMessage(parentToolBar.Handle, (int)ToolBarMessages.TB_GETRECT, index, ref rect);
  243. return new Rectangle(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top);
  244. }
  245. else return Rectangle.Empty;
  246. }
  247. }
  248. public ToolBarEx ToolBar
  249. {
  250. set { 
  251. parentToolBar = value; 
  252. }
  253. get
  254. {
  255. return parentToolBar;
  256. }
  257. }
  258. public EventHandler ClickHandler 
  259. {
  260. set 
  261. { if ( Click != value) 
  262.               { 
  263.   Click = value;
  264.   clickHandler = value; 
  265.   RaiseChanged(); 
  266.   } 
  267. }
  268. get { return clickHandler; }
  269. }
  270. #endregion
  271. #region Implementation
  272. internal void RaiseChanged()
  273. {
  274. if (Changed != null && raiseEvents ) Changed(this, EventArgs.Empty);
  275. }
  276. internal void RaiseClick()
  277. {
  278. if (Click != null && raiseEvents ) Click(this, EventArgs.Empty);
  279. }
  280. internal void RaiseDropDown()
  281. {
  282. if (DropDown != null && raiseEvents ) DropDown(this, EventArgs.Empty);
  283. }
  284. #endregion
  285. }
  286. #endregion
  287. }