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