tangxu
2024-10-11 e0b745b416843934dacfd4268da624595661004b
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// *********************************
// 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>
    /// Represents a quick access toolbar hosted on the Ribbon
    /// </summary>
    public class RibbonQuickAccessToolbar : RibbonItem,
        IContainsSelectableRibbonItems, IContainsRibbonComponents
    {
        #region Fields
        private readonly RibbonQuickAccessToolbarItemCollection _items;
        private bool _dropDownButtonVisible;
        private RibbonMouseSensor _sensor;
        private RibbonButton _dropDownButton;
        #endregion
 
        #region Ctor
 
        internal RibbonQuickAccessToolbar(Ribbon ownerRibbon)
        {
            if (ownerRibbon == null) throw new ArgumentNullException(nameof(ownerRibbon));
 
            SetOwner(ownerRibbon);
 
            _dropDownButton = new RibbonButton();
            _dropDownButton.SetOwner(ownerRibbon);
            _dropDownButton.SmallImage = CreateDropDownButtonImage();
            _dropDownButton.Style = RibbonButtonStyle.DropDown;
 
            Margin = new Padding(9);
            Padding = new Padding(3, 0, 0, 0);
            _items = new RibbonQuickAccessToolbarItemCollection(this);
            _sensor = new RibbonMouseSensor(ownerRibbon, ownerRibbon, Items);
            _dropDownButtonVisible = true;
        }
 
        /// <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 item in _items)
                        item.Dispose();
                }
                catch (InvalidOperationException)
                {
                    if (!IsOpenInVisualStudioDesigner())
                    {
                        throw;
                    }
                }
                _dropDownButton.Dispose();
                _sensor.Dispose();
            }
            base.Dispose(disposing);
        }
 
        private Image CreateDropDownButtonImage()
        {
            Bitmap bmp = new Bitmap(7, 7);
            RibbonProfessionalRenderer renderer = Owner.Renderer as RibbonProfessionalRenderer;
 
            Color dk = Color.Navy;
            Color lt = Color.White;
 
            if (renderer != null)
            {
                dk = renderer.ColorTable.Arrow;
                lt = renderer.ColorTable.ArrowLight;
            }
 
            using (Graphics g = Graphics.FromImage(bmp))
            {
                DrawDropDownButtonArrow(g, lt, 0, 1);
                DrawDropDownButtonArrow(g, dk, 0, 0);
            }
 
 
            return bmp;
        }
 
        private void DrawDropDownButtonArrow(Graphics g, Color c, int x, int y)
        {
            using (Pen p = new Pen(c))
            {
                using (SolidBrush b = new SolidBrush(c))
                {
 
                    g.DrawLine(p, x, y, x + 4, y);
                    g.FillPolygon(b, new[] {
                            new Point(x, y + 3),
                            new Point(x + 5, y + 3),
                            new Point(x + 2, y + 6)
                        });
                }
            }
        }
 
        #endregion
 
        #region Properties
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string Name
        {
            get => base.Name;
            set => base.Name = value;
        }
 
        [Description("Shows or hides the dropdown button of the toolbar")]
        [Category("Drop Down")]
        [DefaultValue(true)]
        public bool DropDownButtonVisible
        {
            get => _dropDownButtonVisible;
            set { _dropDownButtonVisible = value; Owner.OnRegionsChanged(); }
        }
 
 
        /// <summary>
        /// Gets the bounds of the toolbar including the graphic adornments
        /// </summary>
        [Browsable(false)]
        internal Rectangle SuperBounds => Rectangle.FromLTRB(Bounds.Left - Padding.Horizontal, Bounds.Top, DropDownButton.Bounds.Right, Bounds.Bottom);
 
        /// <summary>
        /// Gets the dropdown button
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public RibbonButton DropDownButton { get { return _dropDownButton; } }
 
        [Description("The drop down items of the dropdown button of the toolbar")]
        [Category("Drop Down")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RibbonItemCollection DropDownButtonItems => DropDownButton.DropDownItems;
 
        /// <summary>
        /// Gets or sets the padding (internal) of the toolbar
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Padding Padding { get; }
 
        /// <summary>
        /// Gets or sets the margin (external) of the toolbar
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Padding Margin { get; }
 
        /// <summary>
        /// Gets or sets a value indicating if the button that shows the menu of the 
        /// QuickAccess toolbar should be visible
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool MenuButtonVisible { get; set; }
 
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public RibbonMouseSensor Sensor { get { return _sensor; } }
 
        /// <summary>
        /// Gets the Items of the QuickAccess toolbar.
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public RibbonQuickAccessToolbarItemCollection Items
        {
            get
            {
                if (DropDownButtonVisible)
                {
                    if (!_items.Contains(DropDownButton))
                    {
                        _items.Add(DropDownButton);
                    }
                }
                else
                {
                    if (_items.Contains(DropDownButton))
                    {
                        _items.Remove(DropDownButton);
                    }
                }
                return _items;
            }
        }
 
        #endregion
 
        #region Methods
 
        public override void OnPaint(object sender, RibbonElementPaintEventArgs e)
        {
            if (Visible && Owner.CaptionBarVisible)
            {
                if (Owner.OrbStyle == RibbonOrbStyle.Office_2007)
                    Owner.Renderer.OnRenderRibbonQuickAccessToolbarBackground(new RibbonRenderEventArgs(Owner, e.Graphics, e.Clip));
 
                foreach (RibbonItem item in Items)
                {
                    if (item.Visible)
                        item.OnPaint(this, new RibbonElementPaintEventArgs(item.Bounds, e.Graphics, RibbonElementSizeMode.Compact));
                }
            }
        }
 
        public override Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e)
        {
            //For RibbonItemGroup, size is always compact, and it's designed to be on an horizontal flow
            //tab panel.
            //
            if (!Visible || !Owner.CaptionBarVisible)
            {
                SetLastMeasuredSize(new Size(0, 0));
                return LastMeasuredSize;
            }
 
            int widthSum = Padding.Horizontal;
            int maxHeight = 16;
 
            foreach (RibbonItem item in Items)
            {
                if (item.Equals(DropDownButton)) continue;
                item.SetSizeMode(RibbonElementSizeMode.Compact);
                Size s = item.MeasureSize(this, new RibbonElementMeasureSizeEventArgs(e.Graphics, RibbonElementSizeMode.Compact));
                widthSum += s.Width + 1;
                maxHeight = Math.Max(maxHeight, s.Height);
            }
 
            widthSum -= 1;
 
            if (Site != null && Site.DesignMode) widthSum += 16;
 
            Size result = new Size(widthSum, maxHeight);
            SetLastMeasuredSize(result);
            return result;
        }
 
        public override void SetBounds(Rectangle bounds)
        {
            base.SetBounds(bounds);
 
            if (Owner.RightToLeft == RightToLeft.No)
            {
                int curLeft = bounds.Left + Padding.Left;
 
                foreach (RibbonItem item in Items)
                {
                    item.SetBounds(new Rectangle(new Point(curLeft, bounds.Top), item.LastMeasuredSize));
 
                    curLeft = item.Bounds.Right + 1;
                }
                if (DropDownButtonVisible)
                    DropDownButton.SetBounds(new Rectangle(bounds.Right + bounds.Height / 2 + 2, bounds.Top, 12, bounds.Height));
            }
            else
            {
                int curLeft = bounds.Left + Padding.Left;
 
                for (int i = Items.Count - 1; i >= 0; i--)
                {
                    Items[i].SetBounds(new Rectangle(new Point(curLeft, bounds.Top), Items[i].LastMeasuredSize));
 
                    curLeft = Items[i].Bounds.Right + 1;
                }
                if (DropDownButtonVisible)
                    DropDownButton.SetBounds(new Rectangle(bounds.Left - bounds.Height / 2 - 14, bounds.Top, 12, bounds.Height));
            }
        }
 
        #endregion
 
        #region IContainsRibbonComponents Members
 
        public IEnumerable<Component> GetAllChildComponents()
        {
            return Items.ToArray();
        }
 
        #endregion
 
        #region IContainsSelectableRibbonItems Members
 
        public IEnumerable<RibbonItem> GetItems()
        {
            return Items;
        }
 
        public Rectangle GetContentBounds()
        {
            return Bounds;
        }
 
        #endregion
    }
}