ContextMenuBase.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.ComponentModel;
  5. using FastReport.Design;
  6. using FastReport.Utils;
  7. using FastReport.DevComponents.DotNetBar;
  8. using FastReport.Controls;
  9. using System.Collections.Generic;
  10. namespace FastReport
  11. {
  12. /// <summary>
  13. /// The base class for the context menu item.
  14. /// </summary>
  15. [ToolboxItem(false)]
  16. public class ContextMenuItem : ButtonItem, IToolbarItem
  17. {
  18. bool qatItem = false;
  19. /// <summary>
  20. /// Gets a collection of menu items.
  21. /// </summary>
  22. public SubItemsCollection DropDownItems
  23. {
  24. get { return SubItems; }
  25. }
  26. /// <summary>
  27. /// Gets or sets "Check on click" property.
  28. /// </summary>
  29. public bool CheckOnClick
  30. {
  31. get { return AutoCheckOnClick; }
  32. set { AutoCheckOnClick = value; }
  33. }
  34. /// <summary>
  35. /// Sets bold font.
  36. /// </summary>
  37. public void SetFontBold()
  38. {
  39. FontBold = true;
  40. HotFontBold = true;
  41. }
  42. /// <summary>
  43. /// Gets or sets a value indicating whether the item will be added to the toolbar.
  44. /// </summary>
  45. public bool QatItem
  46. {
  47. get { return qatItem; }
  48. set
  49. {
  50. qatItem = value;
  51. if (value)
  52. {
  53. Tooltip = Text;
  54. }
  55. else
  56. {
  57. Tooltip = "";
  58. }
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// The base class for the context menu of the report component.
  64. /// </summary>
  65. /// <remarks>
  66. /// This class represents a context menu of the report component that is displayed when the object
  67. /// is right-clicked in the designer.
  68. /// </remarks>
  69. [ToolboxItem(false)]
  70. public class ContextMenuBase : ContextMenuBar
  71. {
  72. #region Fields
  73. private ContextMenuItem mnuContextRoot;
  74. private ItemContainer toolbar;
  75. private List<ButtonItem> items;
  76. #endregion
  77. #region Properties
  78. /// <summary>
  79. /// The reference to the report designer.
  80. /// </summary>
  81. public Designer Designer { get; }
  82. /// <summary>
  83. /// Gets a collection of menu items.
  84. /// </summary>
  85. /// <remarks>
  86. /// You should add new items to this collection.
  87. /// </remarks>
  88. public new List<ButtonItem> Items => items;
  89. /// <summary>
  90. /// Gets or sets the image list.
  91. /// </summary>
  92. public ImageList ImageList
  93. {
  94. get => Images;
  95. set
  96. {
  97. Images = value;
  98. UpdateItems();
  99. }
  100. }
  101. public event CancelEventHandler Opening;
  102. #endregion
  103. private void UpdateItems()
  104. {
  105. foreach (BaseItem item in Items)
  106. {
  107. ButtonItem b = item as ButtonItem;
  108. if (b != null && b.ImageIndex != -1)
  109. {
  110. // looks like this is the only way to completely refresh the image displayed (including disabled images).
  111. b.Image = Designer.GetImage(b.ImageIndex);
  112. }
  113. }
  114. }
  115. #region Protected Methods
  116. /// <summary>
  117. /// This method is called to reflect changes in the designer.
  118. /// </summary>
  119. protected virtual void Change()
  120. {
  121. Designer.SetModified(null, "Change");
  122. }
  123. /// <summary>
  124. /// Creates a new menu item.
  125. /// </summary>
  126. /// <param name="text">Item's text.</param>
  127. /// <returns>New item.</returns>
  128. protected ContextMenuItem CreateMenuItem(string text)
  129. {
  130. return CreateMenuItem(-1, text, null);
  131. }
  132. /// <summary>
  133. /// Creates a new menu item.
  134. /// </summary>
  135. /// <param name="text">Item's text.</param>
  136. /// <param name="click">Click handler.</param>
  137. /// <returns>New item.</returns>
  138. protected ContextMenuItem CreateMenuItem(string text, EventHandler click)
  139. {
  140. return CreateMenuItem(-1, text, click);
  141. }
  142. /// <summary>
  143. /// Creates a new menu item.
  144. /// </summary>
  145. /// <param name="imageIndex">Item's image index.</param>
  146. /// <param name="text">Item's text.</param>
  147. /// <param name="click">Click handler.</param>
  148. /// <returns>New item.</returns>
  149. protected ContextMenuItem CreateMenuItem(int imageIndex, string text, EventHandler click)
  150. {
  151. ContextMenuItem item = new ContextMenuItem();
  152. if (imageIndex != -1)
  153. item.Image = Designer.GetImage(imageIndex);
  154. item.Text = text;
  155. if (click != null)
  156. item.Click += click;
  157. return item;
  158. }
  159. #endregion
  160. /// <summary>
  161. /// Clears menu items.
  162. /// </summary>
  163. public void Clear()
  164. {
  165. mnuContextRoot.SubItems.Clear();
  166. toolbar.SubItems.Clear();
  167. }
  168. /// <summary>
  169. /// Adds an item to this menu.
  170. /// </summary>
  171. /// <param name="menuItem">Menu item to add.</param>
  172. public void AddItem(ButtonItem menuItem) => mnuContextRoot.SubItems.Add(menuItem);
  173. /// <summary>
  174. /// Returns true if menu is empty.
  175. /// </summary>
  176. public bool IsEmpty => mnuContextRoot.SubItems.Count == 0;
  177. /// <summary>
  178. /// Displays context menu.
  179. /// </summary>
  180. /// <param name="parent">Parent control.</param>
  181. /// <param name="location">Location.</param>
  182. public void Show(Control parent, Point location)
  183. {
  184. foreach (ButtonItem button in Items)
  185. {
  186. if (button is ContextMenuItem && (button as ContextMenuItem).QatItem)
  187. {
  188. toolbar.SubItems.Add(button);
  189. }
  190. else
  191. {
  192. mnuContextRoot.SubItems.Add(button);
  193. }
  194. }
  195. if (mnuContextRoot.SubItems.Count > 2)
  196. {
  197. mnuContextRoot.SubItems[1].BeginGroup = toolbar.SubItems.Count > 0;
  198. mnuContextRoot.SubItems[2].BeginGroup = toolbar.SubItems.Count > 0;
  199. }
  200. toolbar.Visible = toolbar.SubItems.Count > 0;
  201. mnuContextRoot.PopupMenu(parent.PointToScreen(location));
  202. }
  203. /// <summary>
  204. /// Updates the menu style.
  205. /// </summary>
  206. public void UpdateUIStyle()
  207. {
  208. toolbar.BackgroundStyle.BackColor = UIStyleUtils.GetControlColor(Designer.UIStyle);
  209. Style = UIStyleUtils.GetDotNetBarStyle(Designer.UIStyle);
  210. }
  211. /// <summary>
  212. /// Initializes a new instance of the <b>ComponentBaseMenu</b> class with default settings.
  213. /// </summary>
  214. /// <param name="designer">The reference to a report designer.</param>
  215. public ContextMenuBase(Designer designer)
  216. {
  217. Designer = designer;
  218. Font = Designer.LogicalToDevice(DrawUtils.DefaultFont);
  219. mnuContextRoot = new ContextMenuItem();
  220. items = new List<ButtonItem>();
  221. base.Items.Add(mnuContextRoot);
  222. mnuContextRoot.PopupOpen += (s, e) =>
  223. {
  224. var args = new CancelEventArgs(false);
  225. Opening?.Invoke(this, args);
  226. e.Cancel = args.Cancel;
  227. };
  228. toolbar = new ItemContainer();
  229. toolbar.LayoutOrientation = eOrientation.Horizontal;
  230. mnuContextRoot.SubItems.Add(toolbar);
  231. UpdateUIStyle();
  232. }
  233. }
  234. }