using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using FastReport.Design;
using FastReport.Utils;
using FastReport.DevComponents.DotNetBar;
using FastReport.Controls;
using System.Collections.Generic;
namespace FastReport
{
///
/// The base class for the context menu item.
///
[ToolboxItem(false)]
public class ContextMenuItem : ButtonItem, IToolbarItem
{
bool qatItem = false;
///
/// Gets a collection of menu items.
///
public SubItemsCollection DropDownItems
{
get { return SubItems; }
}
///
/// Gets or sets "Check on click" property.
///
public bool CheckOnClick
{
get { return AutoCheckOnClick; }
set { AutoCheckOnClick = value; }
}
///
/// Sets bold font.
///
public void SetFontBold()
{
FontBold = true;
HotFontBold = true;
}
///
/// Gets or sets a value indicating whether the item will be added to the toolbar.
///
public bool QatItem
{
get { return qatItem; }
set
{
qatItem = value;
if (value)
{
Tooltip = Text;
}
else
{
Tooltip = "";
}
}
}
}
///
/// 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 : ContextMenuBar
{
#region Fields
private ContextMenuItem mnuContextRoot;
private ItemContainer toolbar;
private List items;
#endregion
#region Properties
///
/// The reference to the report designer.
///
public Designer Designer { get; }
///
/// Gets a collection of menu items.
///
///
/// You should add new items to this collection.
///
public new List Items => items;
///
/// Gets or sets the image list.
///
public ImageList ImageList
{
get => Images;
set
{
Images = value;
UpdateItems();
}
}
public event CancelEventHandler Opening;
#endregion
private void UpdateItems()
{
foreach (BaseItem item in Items)
{
ButtonItem b = item as ButtonItem;
if (b != null && b.ImageIndex != -1)
{
// looks like this is the only way to completely refresh the image displayed (including disabled images).
b.Image = Designer.GetImage(b.ImageIndex);
}
}
}
#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;
if (click != null)
item.Click += click;
return item;
}
#endregion
///
/// Clears menu items.
///
public void Clear()
{
mnuContextRoot.SubItems.Clear();
toolbar.SubItems.Clear();
}
///
/// Adds an item to this menu.
///
/// Menu item to add.
public void AddItem(ButtonItem menuItem) => mnuContextRoot.SubItems.Add(menuItem);
///
/// Returns true if menu is empty.
///
public bool IsEmpty => mnuContextRoot.SubItems.Count == 0;
///
/// Displays context menu.
///
/// Parent control.
/// Location.
public void Show(Control parent, Point location)
{
foreach (ButtonItem button in Items)
{
if (button is ContextMenuItem && (button as ContextMenuItem).QatItem)
{
toolbar.SubItems.Add(button);
}
else
{
mnuContextRoot.SubItems.Add(button);
}
}
if (mnuContextRoot.SubItems.Count > 2)
{
mnuContextRoot.SubItems[1].BeginGroup = toolbar.SubItems.Count > 0;
mnuContextRoot.SubItems[2].BeginGroup = toolbar.SubItems.Count > 0;
}
toolbar.Visible = toolbar.SubItems.Count > 0;
mnuContextRoot.PopupMenu(parent.PointToScreen(location));
}
///
/// Updates the menu style.
///
public void UpdateUIStyle()
{
toolbar.BackgroundStyle.BackColor = UIStyleUtils.GetControlColor(Designer.UIStyle);
Style = UIStyleUtils.GetDotNetBarStyle(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 = Designer.LogicalToDevice(DrawUtils.DefaultFont);
mnuContextRoot = new ContextMenuItem();
items = new List();
base.Items.Add(mnuContextRoot);
mnuContextRoot.PopupOpen += (s, e) =>
{
var args = new CancelEventArgs(false);
Opening?.Invoke(this, args);
e.Cancel = args.Cancel;
};
toolbar = new ItemContainer();
toolbar.LayoutOrientation = eOrientation.Horizontal;
mnuContextRoot.SubItems.Add(toolbar);
UpdateUIStyle();
}
}
}