tangxu
2024-10-14 6cd995b71dfc74d4d96347d0bc535fddf36fa9df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// *********************************
// 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;
 
namespace System.Windows.Forms
{
    /// <summary>
    /// A container for grouping buttons on the Ribbon. Prevents the buttons from being separated the owner panel is resized.
    /// An example group could be the Bold, Italic and Underline buttons in th eFont panel of Word. The buttons cannot be separated
    /// and disappear together when the panel size is reduced.
    /// </summary>
    //[Designer("System.Windows.Forms.RibbonQuickAccessToolbarDesigner")]
    [Designer(typeof(RibbonItemGroupDesigner))]
    public class RibbonItemGroup : RibbonItem,
        IContainsSelectableRibbonItems, IContainsRibbonComponents
    {
        #region Fields
 
        #endregion
 
        #region Ctor
        public RibbonItemGroup()
        {
            Items = new RibbonItemGroupItemCollection(this);
            Items.SetOwnerItem(this);
            DrawBackground = true;
        }
 
        public RibbonItemGroup(IEnumerable<RibbonItem> items)
            : this()
        {
            Items.AddRange(items);
        }
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && RibbonDesigner.Current == null)
            {
                try
                {
                    foreach (RibbonItem ri in Items)
                        ri.Dispose();
                }
                catch (InvalidOperationException)
                {
                    if (!IsOpenInVisualStudioDesigner())
                    {
                        throw;
                    }
                }
            }
 
            base.Dispose(disposing);
        }
        #endregion
 
        #region Props
 
        /// <summary>
        /// This property is not relevant for this class
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override bool Checked
        {
            get => base.Checked;
            set => base.Checked = value;
        }
 
        /// <summary>
        /// Gets or sets a value indicating if the group should
        /// </summary>
        [DefaultValue(true)]
        [Category("Appearance")]
        [Description("Background drawing should be avoided when group contains only TextBoxes and ComboBoxes")]
        public bool DrawBackground { get; set; }
 
        /// <summary>
        /// Gets the first item of the group
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public RibbonItem FirstItem
        {
            get
            {
                if (Items.Count > 0)
                {
                    return Items[0];
                }
 
                return null;
            }
        }
 
        /// <summary>
        /// Gets the last item of the group
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public RibbonItem LastItem
        {
            get
            {
                if (Items.Count > 0)
                {
                    return Items[Items.Count - 1];
                }
 
                return null;
            }
        }
 
 
        /// <summary>
        /// Gets the collection of items of this group
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RibbonItemGroupItemCollection Items { get; }
 
        #endregion
 
        #region Methods
 
        protected override bool ClosesDropDownAt(Point p)
        {
            return false;
        }
 
        public override void SetBounds(Rectangle bounds)
        {
            base.SetBounds(bounds);
 
            int curLeft = bounds.Left;
 
            foreach (RibbonItem item in Items)
            {
                item.SetBounds(new Rectangle(new Point(curLeft, bounds.Top), item.LastMeasuredSize));
 
                curLeft = item.Bounds.Right + 1;
            }
 
        }
 
        public override void OnPaint(object sender, RibbonElementPaintEventArgs e)
        {
            if (DrawBackground)
            {
                Owner.Renderer.OnRenderRibbonItem(new RibbonItemRenderEventArgs(Owner, e.Graphics, e.Clip, this));
            }
 
            foreach (RibbonItem item in Items)
            {
                if (item.Visible || Owner.IsDesignMode())
                    item.OnPaint(this, new RibbonElementPaintEventArgs(item.Bounds, e.Graphics, RibbonElementSizeMode.Compact));
            }
 
            if (DrawBackground)
            {
                Owner.Renderer.OnRenderRibbonItemBorder(new RibbonItemRenderEventArgs(Owner, e.Graphics, e.Clip, this));
            }
        }
 
        public override Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e)
        {
            if (!Visible && !Owner.IsDesignMode())
            {
                SetLastMeasuredSize(new Size(0, 0));
                return LastMeasuredSize;
            }
 
            //For RibbonItemGroup, size is always compact, and it's designed to be on an horizontal flow
            //tab panel.
            //
            int minWidth = 16;
            int widthSum = 0;
            int maxHeight = 16;
 
            foreach (RibbonItem item in Items)
            {
                Size s = item.MeasureSize(this, new RibbonElementMeasureSizeEventArgs(e.Graphics, RibbonElementSizeMode.Compact));
                widthSum += s.Width + 1;
                maxHeight = Math.Max(maxHeight, s.Height);
            }
 
            widthSum -= 1;
 
            widthSum = Math.Max(widthSum, minWidth);
 
            if (Site != null && Site.DesignMode)
            {
                widthSum += 10;
            }
 
            Size result = new Size(widthSum, maxHeight);
            SetLastMeasuredSize(result);
            return result;
        }
 
        /// <param name="ownerPanel">RibbonPanel where this item is located</param>
        internal override void SetOwnerPanel(RibbonPanel ownerPanel)
        {
            base.SetOwnerPanel(ownerPanel);
 
            Items.SetOwnerPanel(ownerPanel);
        }
 
        /// <param name="owner">Ribbon that owns this item</param>
        internal override void SetOwner(Ribbon owner)
        {
            base.SetOwner(owner);
 
            Items.SetOwner(owner);
        }
 
        /// <param name="ownerTab">RibbonTab where this item is located</param>
        internal override void SetOwnerTab(RibbonTab ownerTab)
        {
            base.SetOwnerTab(ownerTab);
 
            Items.SetOwnerTab(ownerTab);
        }
 
        /// <param name="ownerItem">RibbonItem where this item is located</param>
        internal override void SetOwnerItem(RibbonItem ownerItem)
        {
            base.SetOwnerItem(ownerItem);
        }
 
        internal override void ClearOwner()
        {
            List<RibbonItem> oldItems = new List<RibbonItem>(Items);
 
            base.ClearOwner();
 
            foreach (RibbonItem item in oldItems)
            {
                item.ClearOwner();
            }
        }
 
        internal override void SetSizeMode(RibbonElementSizeMode sizeMode)
        {
            base.SetSizeMode(sizeMode);
 
            foreach (RibbonItem item in Items)
            {
                item.SetSizeMode(RibbonElementSizeMode.Compact);
            }
        }
 
        public override string ToString()
        {
            return "Group: " + Items.Count + " item(s)";
        }
        #endregion
 
        #region IContainsRibbonItems Members
 
        public IEnumerable<RibbonItem> GetItems()
        {
            return Items;
        }
 
        public Rectangle GetContentBounds()
        {
            return Rectangle.FromLTRB(Bounds.Left + 1, Bounds.Top + 1, Bounds.Right - 1, Bounds.Bottom);
        }
 
        #endregion
 
        #region IContainsRibbonComponents Members
 
        public IEnumerable<Component> GetAllChildComponents()
        {
            return Items.ToArray();
        }
 
        #endregion
    }
}