123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.Windows.Forms;
- using System.Drawing;
- using System.ComponentModel;
- using FastReport.Design;
- using FastReport.Utils;
- namespace FastReport
- {
- /// <summary>
- /// An interface each menu and toolbar item must implement.
- /// </summary>
- public interface IToolbarItem
- {
- /// <summary>
- /// Gets or sets a value indicating that a separator is needed before this item.
- /// </summary>
- bool BeginGroup { get; set; }
- }
- /// <summary>
- /// The base class for the context menu item.
- /// </summary>
- [ToolboxItem(false)]
- public class ContextMenuItem : ToolStripMenuItem, IToolbarItem
- {
- private bool visibleInternal = true;
- /// <inheritdoc/>
- public bool BeginGroup { get; set; }
- /// <summary>
- /// Gets or sets the internal visibility value.
- /// </summary>
- public new bool Visible
- {
- // Visible flag is always false when menu is not displayed
- get { return visibleInternal; }
- set { visibleInternal = value; base.Visible = value; }
- }
- /// <summary>
- /// Sets bold font.
- /// </summary>
- public void SetFontBold()
- {
- Font = new Font(Font, FontStyle.Bold);
- }
- #if !(WPF || AVALONIA)
- /// <summary>
- /// Not relevant to this class. Used in WPF/Avalonia only.
- /// </summary>
- public bool QatItem { get; set; }
- #endif
- }
- /// <summary>
- /// The base class for the context menu of the report component.
- /// </summary>
- /// <remarks>
- /// This class represents a context menu of the report component that is displayed when the object
- /// is right-clicked in the designer.
- /// </remarks>
- [ToolboxItem(false)]
- public class ContextMenuBase : ContextMenuStrip
- {
- #region Properties
- /// <summary>
- /// The reference to the report designer.
- /// </summary>
- public Designer Designer { get; }
- #endregion
- #region Protected Methods
- /// <summary>
- /// This method is called to reflect changes in the designer.
- /// </summary>
- protected virtual void Change()
- {
- Designer.SetModified(null, "Change");
- }
- /// <summary>
- /// Creates a new menu item.
- /// </summary>
- /// <param name="text">Item's text.</param>
- /// <returns>New item.</returns>
- protected ContextMenuItem CreateMenuItem(string text)
- {
- return CreateMenuItem(-1, text, null);
- }
- /// <summary>
- /// Creates a new menu item.
- /// </summary>
- /// <param name="text">Item's text.</param>
- /// <param name="click">Click handler.</param>
- /// <returns>New item.</returns>
- protected ContextMenuItem CreateMenuItem(string text, EventHandler click)
- {
- return CreateMenuItem(-1, text, click);
- }
- /// <summary>
- /// Creates a new menu item.
- /// </summary>
- /// <param name="imageIndex">Item's image index.</param>
- /// <param name="text">Item's text.</param>
- /// <param name="click">Click handler.</param>
- /// <returns>New item.</returns>
- protected ContextMenuItem CreateMenuItem(int imageIndex, string text, EventHandler click)
- {
- ContextMenuItem item = new ContextMenuItem();
- if (imageIndex != -1)
- item.Image = Designer.GetImage(imageIndex);
- item.Text = text;
- item.Font = DrawUtils.DefaultFont; // workaround Mono behavior
- if (click != null)
- item.Click += click;
- return item;
- }
- #endregion
- /// <summary>
- /// Clears menu items.
- /// </summary>
- public void Clear() => Items.Clear();
- /// <summary>
- /// Adds an item to this menu.
- /// </summary>
- /// <param name="menuItem">Menu item to add.</param>
- public void AddItem(ToolStripMenuItem menuItem) => Items.Add(menuItem);
- /// <summary>
- /// Returns true if menu is empty.
- /// </summary>
- public bool IsEmpty => Items.Count == 0;
- /// <summary>
- /// Displays context menu.
- /// </summary>
- /// <param name="parent">Parent control.</param>
- /// <param name="location">Location.</param>
- public new void Show(Control parent, Point location)
- {
- // convert item.BeginGroup to a separator
- for (int i = 0; i < Items.Count; i++)
- {
- ContextMenuItem item = Items[i] as ContextMenuItem;
- if (i > 0 && item != null && item.Visible && item.BeginGroup)
- {
- Items.Insert(i, new ToolStripSeparator());
- i++;
- }
- }
- base.Show(parent, location);
- }
- /// <summary>
- /// Updates the menu style.
- /// </summary>
- public void UpdateUIStyle()
- {
- Renderer = UIStyleUtils.GetToolStripRenderer(Designer.UIStyle);
- }
- /// <summary>
- /// Initializes a new instance of the <b>ComponentBaseMenu</b> class with default settings.
- /// </summary>
- /// <param name="designer">The reference to a report designer.</param>
- public ContextMenuBase(Designer designer)
- {
- Designer = designer;
- Font = DrawUtils.DefaultFont;
- UpdateUIStyle();
- }
- }
- }
|