123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- 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
- {
- /// <summary>
- /// The base class for the context menu item.
- /// </summary>
- [ToolboxItem(false)]
- public class ContextMenuItem : ButtonItem, IToolbarItem
- {
- bool qatItem = false;
- /// <summary>
- /// Gets a collection of menu items.
- /// </summary>
- public SubItemsCollection DropDownItems
- {
- get { return SubItems; }
- }
- /// <summary>
- /// Gets or sets "Check on click" property.
- /// </summary>
- public bool CheckOnClick
- {
- get { return AutoCheckOnClick; }
- set { AutoCheckOnClick = value; }
- }
- /// <summary>
- /// Sets bold font.
- /// </summary>
- public void SetFontBold()
- {
- FontBold = true;
- HotFontBold = true;
- }
- /// <summary>
- /// Gets or sets a value indicating whether the item will be added to the toolbar.
- /// </summary>
- public bool QatItem
- {
- get { return qatItem; }
- set
- {
- qatItem = value;
- if (value)
- {
- Tooltip = Text;
- }
- else
- {
- Tooltip = "";
- }
- }
- }
- }
- /// <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 : ContextMenuBar
- {
- #region Fields
- private ContextMenuItem mnuContextRoot;
- private ItemContainer toolbar;
- private List<ButtonItem> items;
- #endregion
- #region Properties
- /// <summary>
- /// The reference to the report designer.
- /// </summary>
- public Designer Designer { get; }
- /// <summary>
- /// Gets a collection of menu items.
- /// </summary>
- /// <remarks>
- /// You should add new items to this collection.
- /// </remarks>
- public new List<ButtonItem> Items => items;
- /// <summary>
- /// Gets or sets the image list.
- /// </summary>
- 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
- /// <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;
- if (click != null)
- item.Click += click;
- return item;
- }
- #endregion
- /// <summary>
- /// Clears menu items.
- /// </summary>
- public void Clear()
- {
- mnuContextRoot.SubItems.Clear();
- toolbar.SubItems.Clear();
- }
- /// <summary>
- /// Adds an item to this menu.
- /// </summary>
- /// <param name="menuItem">Menu item to add.</param>
- public void AddItem(ButtonItem menuItem) => mnuContextRoot.SubItems.Add(menuItem);
- /// <summary>
- /// Returns true if menu is empty.
- /// </summary>
- public bool IsEmpty => mnuContextRoot.SubItems.Count == 0;
- /// <summary>
- /// Displays context menu.
- /// </summary>
- /// <param name="parent">Parent control.</param>
- /// <param name="location">Location.</param>
- 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));
- }
- /// <summary>
- /// Updates the menu style.
- /// </summary>
- public void UpdateUIStyle()
- {
- toolbar.BackgroundStyle.BackColor = UIStyleUtils.GetControlColor(Designer.UIStyle);
- Style = UIStyleUtils.GetDotNetBarStyle(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 = Designer.LogicalToDevice(DrawUtils.DefaultFont);
- mnuContextRoot = new ContextMenuItem();
- items = new List<ButtonItem>();
- 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();
- }
- }
- }
|