ContextMenuBase.Mono.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. namespace FastReport
  8. {
  9. /// <summary>
  10. /// An interface each menu and toolbar item must implement.
  11. /// </summary>
  12. public interface IToolbarItem
  13. {
  14. /// <summary>
  15. /// Gets or sets a value indicating that a separator is needed before this item.
  16. /// </summary>
  17. bool BeginGroup { get; set; }
  18. }
  19. /// <summary>
  20. /// The base class for the context menu item.
  21. /// </summary>
  22. [ToolboxItem(false)]
  23. public class ContextMenuItem : ToolStripMenuItem, IToolbarItem
  24. {
  25. private bool visibleInternal = true;
  26. /// <inheritdoc/>
  27. public bool BeginGroup { get; set; }
  28. /// <summary>
  29. /// Gets or sets the internal visibility value.
  30. /// </summary>
  31. public new bool Visible
  32. {
  33. // Visible flag is always false when menu is not displayed
  34. get { return visibleInternal; }
  35. set { visibleInternal = value; base.Visible = value; }
  36. }
  37. /// <summary>
  38. /// Sets bold font.
  39. /// </summary>
  40. public void SetFontBold()
  41. {
  42. Font = new Font(Font, FontStyle.Bold);
  43. }
  44. #if !(WPF || AVALONIA)
  45. /// <summary>
  46. /// Not relevant to this class. Used in WPF/Avalonia only.
  47. /// </summary>
  48. public bool QatItem { get; set; }
  49. #endif
  50. }
  51. /// <summary>
  52. /// The base class for the context menu of the report component.
  53. /// </summary>
  54. /// <remarks>
  55. /// This class represents a context menu of the report component that is displayed when the object
  56. /// is right-clicked in the designer.
  57. /// </remarks>
  58. [ToolboxItem(false)]
  59. public class ContextMenuBase : ContextMenuStrip
  60. {
  61. #region Properties
  62. /// <summary>
  63. /// The reference to the report designer.
  64. /// </summary>
  65. public Designer Designer { get; }
  66. #endregion
  67. #region Protected Methods
  68. /// <summary>
  69. /// This method is called to reflect changes in the designer.
  70. /// </summary>
  71. protected virtual void Change()
  72. {
  73. Designer.SetModified(null, "Change");
  74. }
  75. /// <summary>
  76. /// Creates a new menu item.
  77. /// </summary>
  78. /// <param name="text">Item's text.</param>
  79. /// <returns>New item.</returns>
  80. protected ContextMenuItem CreateMenuItem(string text)
  81. {
  82. return CreateMenuItem(-1, text, null);
  83. }
  84. /// <summary>
  85. /// Creates a new menu item.
  86. /// </summary>
  87. /// <param name="text">Item's text.</param>
  88. /// <param name="click">Click handler.</param>
  89. /// <returns>New item.</returns>
  90. protected ContextMenuItem CreateMenuItem(string text, EventHandler click)
  91. {
  92. return CreateMenuItem(-1, text, click);
  93. }
  94. /// <summary>
  95. /// Creates a new menu item.
  96. /// </summary>
  97. /// <param name="imageIndex">Item's image index.</param>
  98. /// <param name="text">Item's text.</param>
  99. /// <param name="click">Click handler.</param>
  100. /// <returns>New item.</returns>
  101. protected ContextMenuItem CreateMenuItem(int imageIndex, string text, EventHandler click)
  102. {
  103. ContextMenuItem item = new ContextMenuItem();
  104. if (imageIndex != -1)
  105. item.Image = Designer.GetImage(imageIndex);
  106. item.Text = text;
  107. item.Font = DrawUtils.DefaultFont; // workaround Mono behavior
  108. if (click != null)
  109. item.Click += click;
  110. return item;
  111. }
  112. #endregion
  113. /// <summary>
  114. /// Clears menu items.
  115. /// </summary>
  116. public void Clear() => Items.Clear();
  117. /// <summary>
  118. /// Adds an item to this menu.
  119. /// </summary>
  120. /// <param name="menuItem">Menu item to add.</param>
  121. public void AddItem(ToolStripMenuItem menuItem) => Items.Add(menuItem);
  122. /// <summary>
  123. /// Returns true if menu is empty.
  124. /// </summary>
  125. public bool IsEmpty => Items.Count == 0;
  126. /// <summary>
  127. /// Displays context menu.
  128. /// </summary>
  129. /// <param name="parent">Parent control.</param>
  130. /// <param name="location">Location.</param>
  131. public new void Show(Control parent, Point location)
  132. {
  133. // convert item.BeginGroup to a separator
  134. for (int i = 0; i < Items.Count; i++)
  135. {
  136. ContextMenuItem item = Items[i] as ContextMenuItem;
  137. if (i > 0 && item != null && item.Visible && item.BeginGroup)
  138. {
  139. Items.Insert(i, new ToolStripSeparator());
  140. i++;
  141. }
  142. }
  143. base.Show(parent, location);
  144. }
  145. /// <summary>
  146. /// Updates the menu style.
  147. /// </summary>
  148. public void UpdateUIStyle()
  149. {
  150. Renderer = UIStyleUtils.GetToolStripRenderer(Designer.UIStyle);
  151. }
  152. /// <summary>
  153. /// Initializes a new instance of the <b>ComponentBaseMenu</b> class with default settings.
  154. /// </summary>
  155. /// <param name="designer">The reference to a report designer.</param>
  156. public ContextMenuBase(Designer designer)
  157. {
  158. Designer = designer;
  159. Font = DrawUtils.DefaultFont;
  160. UpdateUIStyle();
  161. }
  162. }
  163. }