// ********************************* // Message from Original Author: // // 2008 Jose Menendez Poo // Please give me credit if you use this code. It's all I ask. // Contact me for more info: menendezpoo@gmail.com // ********************************* // // Original project from http://ribbon.codeplex.com/ // Continue to support and maintain by http://officeribbon.codeplex.com/ using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Design; using System.Windows.Forms.Classes.Collections; namespace System.Windows.Forms { [Editor(typeof(RibbonItemCollectionEditor), typeof(UITypeEditor))] public class RibbonItemCollection : RibbonCollectionBase { #region Fields #endregion #region Ctor /// /// Creates a new ribbon item collection /// internal RibbonItemCollection() : base(null) { } #endregion #region Properties /// /// Gets the RibbonPanel where this item is located /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public RibbonPanel OwnerPanel { get; private set; } /// /// Gets the RibbonTab that contains this item /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public RibbonTab OwnerTab { get; private set; } /// /// Gets the RibbonItem that contains this item /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public RibbonItem OwnerItem { get; private set; } #endregion /// /// Sets the owner Tab of the collection /// /// internal void SetOwnerTab(RibbonTab tab) { OwnerTab = tab; foreach (RibbonItem item in this) { item.SetOwnerTab(tab); } } /// /// Sets the owner panel of the collection /// /// internal void SetOwnerPanel(RibbonPanel panel) { OwnerPanel = panel; foreach (RibbonItem item in this) { item.SetOwnerPanel(panel); } } /// /// Sets the owner item of the collection /// /// internal void SetOwnerItem(RibbonItem item) { OwnerItem = item; foreach (RibbonItem subitem in this) { subitem.SetOwnerItem(item); } } #region Overrides /// /// /// Sets the owner Ribbon of the collection /// /// internal override void SetOwner(Ribbon owner) { base.SetOwner(owner); foreach (RibbonItem item in this) { item.SetOwner(owner); } } internal override void SetOwner(RibbonItem item) { item.SetOwner(Owner); item.SetOwnerPanel(OwnerPanel); item.SetOwnerTab(OwnerTab); item.SetOwnerItem(OwnerItem); } /// /// Clears the owner of the given . /// /// the RibbonItem to clear the owner internal override void ClearOwner(RibbonItem item) { item.ClearOwner(); } /// /// Notifies the and about changes in the . /// internal override void UpdateRegions() { try { OwnerTab?.UpdatePanelsRegions(); if (Owner != null && Owner.IsDisposed == false) { Owner.UpdateRegions(); Owner.Invalidate(); } } catch { // ignored } } #endregion #region Methods /// /// Gets the left of items as a group of shapes /// /// internal int GetItemsLeft(IEnumerable items) { if (Count == 0) return 0; int min = int.MaxValue; foreach (RibbonItem item in items) { if (item.Bounds.X < min) { min = item.Bounds.X; } } return min; } /// /// Gets the right of items as a group of shapes /// /// internal int GetItemsRight(IEnumerable items) { if (Count == 0) return 0; int max = int.MinValue; ; foreach (RibbonItem item in items) { if (item.Bounds.Right > max) { max = item.Bounds.Right; } } return max; } /// /// Gets the top of items as a group of shapes /// /// internal int GetItemsTop(IEnumerable items) { if (Count == 0) return 0; int min = int.MaxValue; foreach (RibbonItem item in items) { if (item.Bounds.Y < min) { min = item.Bounds.Y; } } return min; } /// /// Gets the bottom of items as a group of shapes /// /// internal int GetItemsBottom(IEnumerable items) { if (Count == 0) return 0; int max = int.MinValue; foreach (RibbonItem item in items) { if (item.Bounds.Bottom > max) { max = item.Bounds.Bottom; } } return max; } /// /// Gets the width of items as a group of shapes /// /// internal int GetItemsWidth(IEnumerable items) => GetItemsRight(items) - GetItemsLeft(items); /// /// Gets the height of items as a group of shapes /// /// internal int GetItemsHeight(IEnumerable items) => GetItemsBottom(items) - GetItemsTop(items); /// /// Gets the bounds of items as a group of shapes /// /// internal Rectangle GetItemsBounds(IEnumerable items) => Rectangle.FromLTRB(GetItemsLeft(items), GetItemsTop(items), GetItemsRight(items), GetItemsBottom(items)); /// /// Gets the left of items as a group of shapes /// /// internal int GetItemsLeft() { if (Count == 0) return 0; int min = int.MaxValue; foreach (RibbonItem item in this) { if (item.Bounds.X < min) { min = item.Bounds.X; } } return min; } /// /// Gets the right of items as a group of shapes /// /// internal int GetItemsRight() { if (Count == 0) return 0; int max = int.MinValue; ; foreach (RibbonItem item in this) { if (item.Visible && item.Bounds.Right > max) { max = item.Bounds.Right; } } if (max == int.MinValue) { max = 0; } return max; } /// /// Gets the top of items as a group of shapes /// /// internal int GetItemsTop() { if (Count == 0) return 0; int min = int.MaxValue; foreach (RibbonItem item in this) { if (item.Bounds.Y < min) { min = item.Bounds.Y; } } return min; } /// /// Gets the bottom of items as a group of shapes /// /// internal int GetItemsBottom() { if (Count == 0) return 0; int max = int.MinValue; foreach (RibbonItem item in this) { if (item.Visible && item.Bounds.Bottom > max) { max = item.Bounds.Bottom; } } if (max == int.MinValue) { max = 0; } return max; } /// /// Gets the width of items as a group of shapes /// /// internal int GetItemsWidth() { return GetItemsRight() - GetItemsLeft(); } /// /// Gets the height of items as a group of shapes /// /// internal int GetItemsHeight() { return GetItemsBottom() - GetItemsTop(); } /// /// Gets the bounds of items as a group of shapes /// /// internal Rectangle GetItemsBounds() { return Rectangle.FromLTRB(GetItemsLeft(), GetItemsTop(), GetItemsRight(), GetItemsBottom()); } /// /// Moves the bounds of items as a group of shapes /// /// internal void MoveTo(Point p) { MoveTo(this, p); } /// /// Moves the bounds of items as a group of shapes /// /// /// internal void MoveTo(IEnumerable items, Point p) { Rectangle oldBounds = GetItemsBounds(items); foreach (RibbonItem item in items) { int dx = item.Bounds.X - oldBounds.Left; int dy = item.Bounds.Y - oldBounds.Top; item.SetBounds(new Rectangle(new Point(p.X + dx, p.Y + dy), item.Bounds.Size)); } } /// /// Centers the items on the specified rectangle /// /// internal void CenterItemsInto(Rectangle rectangle) { CenterItemsInto(this, rectangle); } /// /// Centers the items vertically on the specified rectangle /// /// internal void CenterItemsVerticallyInto(Rectangle rectangle) { CenterItemsVerticallyInto(this, rectangle); } /// /// Centers the items horizontally on the specified rectangle /// /// internal void CenterItemsHorizontallyInto(Rectangle rectangle) { CenterItemsHorizontallyInto(this, rectangle); } /// /// Centers the items on the specified rectangle /// /// /// internal void CenterItemsInto(IEnumerable items, Rectangle rectangle) { int x = rectangle.Left + (rectangle.Width - GetItemsWidth()) / 2; int y = rectangle.Top + (rectangle.Height - GetItemsHeight()) / 2; MoveTo(items, new Point(x, y)); } /// /// Centers the items vertically on the specified rectangle /// /// /// internal void CenterItemsVerticallyInto(IEnumerable items, Rectangle rectangle) { int x = GetItemsLeft(items); int y = rectangle.Top + (rectangle.Height - GetItemsHeight(items)) / 2; MoveTo(items, new Point(x, y)); } /// /// Centers the items horizontally on the specified rectangle /// /// /// internal void CenterItemsHorizontallyInto(IEnumerable items, Rectangle rectangle) { int x = rectangle.Left + (rectangle.Width - GetItemsWidth(items)) / 2; int y = GetItemsTop(items); MoveTo(items, new Point(x, y)); } /// /// Checks for the restrictions that buttons should have on the RibbonButton List /// /// private void CheckRestrictions(RibbonItem item) { if (OwnerItem != null) { // A DropDown menu //if (item is RibbonDescriptionMenuItem) //{ // throw new ArgumentException("The only style supported by the RibbonButtonList is Normal", nameof(item)); //} } else { // A Panel if (item is RibbonDescriptionMenuItem) { throw new ArgumentException("The RibbonDescriptionMenuItem item is not supported on a panel", nameof(item)); } } } /// /// Adds the specified item to the collection /// public override void Add(RibbonItem item) { CheckRestrictions(item); item.SetOwner(Owner); item.SetOwnerPanel(OwnerPanel); item.SetOwnerTab(OwnerTab); item.SetOwnerItem(OwnerItem); base.Add(item); } /// /// Adds the specified range of items /// /// Items to add public override void AddRange(IEnumerable items) { foreach (RibbonItem item in items) { CheckRestrictions(item); item.SetOwner(Owner); item.SetOwnerPanel(OwnerPanel); item.SetOwnerTab(OwnerTab); item.SetOwnerItem(OwnerItem); } base.AddRange(items); } /// /// /// Inserts the specified item at the desired index /// /// Desired index of the item /// Item to insert public override void Insert(int index, RibbonItem item) { CheckRestrictions(item); item.SetOwner(Owner); item.SetOwnerPanel(OwnerPanel); item.SetOwnerTab(OwnerTab); item.SetOwnerItem(OwnerItem); base.Insert(index, item); } #endregion } }