ContextMenuBase.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #if !MONO
  8. using FastReport.DevComponents.DotNetBar;
  9. #endif
  10. namespace FastReport
  11. {
  12. /// <summary>
  13. /// The base class for the context menu item.
  14. /// </summary>
  15. [ToolboxItem(false)]
  16. public class ContextMenuItem
  17. #if !MONO
  18. : ButtonItem
  19. {
  20. /// <summary>
  21. /// Gets a collection of menu items.
  22. /// </summary>
  23. public SubItemsCollection DropDownItems
  24. {
  25. get { return SubItems; }
  26. }
  27. /// <summary>
  28. /// Gets or sets "Check on click" property.
  29. /// </summary>
  30. public bool CheckOnClick
  31. {
  32. get { return AutoCheckOnClick; }
  33. set { AutoCheckOnClick = value; }
  34. }
  35. /// <summary>
  36. /// Sets bold font.
  37. /// </summary>
  38. public void SetFontBold()
  39. {
  40. FontBold = true;
  41. HotFontBold = true;
  42. }
  43. }
  44. #else
  45. : ToolStripMenuItem
  46. {
  47. private bool beginGroup;
  48. private bool visibleInternal = true;
  49. /// <summary>
  50. /// Gets or sets a value indicating that a separator is needed before this menu item.
  51. /// </summary>
  52. public bool BeginGroup
  53. {
  54. get { return beginGroup; }
  55. set { beginGroup = value; }
  56. }
  57. /// <summary>
  58. /// Gets or sets the internal visibility value.
  59. /// </summary>
  60. public new bool Visible
  61. {
  62. // Visible flag is always false when menu is not displayed
  63. get { return visibleInternal; }
  64. set { visibleInternal = value; base.Visible = value; }
  65. }
  66. /// <summary>
  67. /// Sets bold font.
  68. /// </summary>
  69. public void SetFontBold()
  70. {
  71. Font = new Font(Font, FontStyle.Bold);
  72. }
  73. }
  74. #endif
  75. /// <summary>
  76. /// The base class for the context menu of the report component.
  77. /// </summary>
  78. /// <remarks>
  79. /// This class represents a context menu of the report component that is displayed when the object
  80. /// is right-clicked in the designer.
  81. /// </remarks>
  82. [ToolboxItem(false)]
  83. public class ContextMenuBase
  84. #if !MONO
  85. : ContextMenuBar
  86. #else
  87. : ContextMenuStrip
  88. #endif
  89. {
  90. #region Fields
  91. private Designer designer;
  92. #if !MONO
  93. private ContextMenuItem mnuContextRoot;
  94. #endif
  95. #endregion
  96. #region Properties
  97. /// <summary>
  98. /// The reference to the report designer.
  99. /// </summary>
  100. public Designer Designer
  101. {
  102. get { return designer; }
  103. }
  104. #if !MONO
  105. /// <summary>
  106. /// Gets a collection of menu items.
  107. /// </summary>
  108. /// <remarks>
  109. /// You should add new items to this collection.
  110. /// </remarks>
  111. public new SubItemsCollection Items
  112. {
  113. get { return mnuContextRoot.SubItems; }
  114. }
  115. #endif
  116. #endregion
  117. #region Private Methods
  118. #endregion
  119. #region Protected Methods
  120. /// <summary>
  121. /// This method is called to reflect changes in the designer.
  122. /// </summary>
  123. protected virtual void Change()
  124. {
  125. Designer.SetModified(null, "Change");
  126. }
  127. /// <summary>
  128. /// Creates a new menu item.
  129. /// </summary>
  130. /// <param name="text">Item's text.</param>
  131. /// <returns>New item.</returns>
  132. protected ContextMenuItem CreateMenuItem(string text)
  133. {
  134. return CreateMenuItem(-1, text, null);
  135. }
  136. /// <summary>
  137. /// Creates a new menu item.
  138. /// </summary>
  139. /// <param name="text">Item's text.</param>
  140. /// <param name="click">Click handler.</param>
  141. /// <returns>New item.</returns>
  142. protected ContextMenuItem CreateMenuItem(string text, EventHandler click)
  143. {
  144. return CreateMenuItem(-1, text, click);
  145. }
  146. /// <summary>
  147. /// Creates a new menu item.
  148. /// </summary>
  149. /// <param name="imageIndex">Item's image index.</param>
  150. /// <param name="text">Item's text.</param>
  151. /// <param name="click">Click handler.</param>
  152. /// <returns>New item.</returns>
  153. protected ContextMenuItem CreateMenuItem(int imageIndex, string text, EventHandler click)
  154. {
  155. ContextMenuItem item = new ContextMenuItem();
  156. if (imageIndex != -1)
  157. item.Image = Designer.GetImage(imageIndex);
  158. item.Text = text;
  159. #if MONO
  160. item.Font = DrawUtils.DefaultFont; // workaround Mono behavior
  161. #endif
  162. if (click != null)
  163. item.Click += click;
  164. return item;
  165. }
  166. #endregion
  167. #if !MONO
  168. /// <summary>
  169. /// Displays context menu.
  170. /// </summary>
  171. /// <param name="parent">Parent control.</param>
  172. /// <param name="location">Location.</param>
  173. public void Show(Control parent, Point location)
  174. {
  175. mnuContextRoot.PopupMenu(parent.PointToScreen(location));
  176. }
  177. #else
  178. /// <summary>
  179. /// Displays context menu.
  180. /// </summary>
  181. /// <param name="parent">Parent control.</param>
  182. /// <param name="location">Location.</param>
  183. public new void Show(Control parent, Point location)
  184. {
  185. // convert item.BeginGroup to a separator
  186. for (int i = 0; i < Items.Count; i++)
  187. {
  188. ContextMenuItem item = Items[i] as ContextMenuItem;
  189. if (i > 0 && item != null && item.Visible && item.BeginGroup)
  190. {
  191. Items.Insert(i, new ToolStripSeparator());
  192. i++;
  193. }
  194. }
  195. base.Show(parent, location);
  196. }
  197. #endif
  198. /// <summary>
  199. /// Initializes a new instance of the <b>ComponentBaseMenu</b> class with default settings.
  200. /// </summary>
  201. /// <param name="designer">The reference to a report designer.</param>
  202. public ContextMenuBase(Designer designer)
  203. : base()
  204. {
  205. this.designer = designer;
  206. #if !MONO
  207. Style = UIStyleUtils.GetDotNetBarStyle(Designer.UIStyle);
  208. Font = Designer.LogicalToDevice(DrawUtils.DefaultFont);
  209. mnuContextRoot = new ContextMenuItem();
  210. base.Items.Add(mnuContextRoot);
  211. #else
  212. Font = DrawUtils.DefaultFont;
  213. Renderer = UIStyleUtils.GetToolStripRenderer(Designer.UIStyle);
  214. #endif
  215. }
  216. }
  217. }