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
// *********************************
// 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 context on the Ribbon
    /// </summary>
    /// <remarks>Contexts are useful when some tabs are volatile, depending on some selection. A RibbonTabContext can be added to the ribbon by calling Ribbon.Contexts.Add</remarks>
    [ToolboxItem(false)]
    [DesignTimeVisible(false)]
    public class RibbonContext
        : Component, IRibbonElement
    {
        #region Fields
 
        private string _text;
        private Color _glowColor;
 
        private bool _visible;
        //private ContextualTabCollection _tabs;
        public event EventHandler OwnerChanged;
 
        #endregion
 
        #region Ctor
 
        #endregion
 
        #region Events
 
        #endregion
 
        #region Props
 
        /// <summary>
        /// Gets or sets the text of the Context
        /// </summary>
        [Category("Appearance")]
        public string Text
        {
            get => _text;
            set
            {
                _text = value;
                if (Owner != null) Owner.OnRegionsChanged();
            }
        }
 
        /// <summary>
        /// Gets or sets the color of the glow that indicates a context
        /// </summary>
        [Category("Appearance")]
        public Color GlowColor
        {
            get => _glowColor;
            set
            {
                _glowColor = value;
                if (Owner != null) Owner.OnRegionsChanged();
            }
        }
 
        /// <summary>
        /// Gets or sets the visibility of this tab
        /// </summary>
        /// <remarks>Tabs on a context are highlighted with a special glow color</remarks>
        [Category("Behavior")]
        [DefaultValue(false)]
        public bool Visible
        {
            get
            {
                if (Owner != null && !Owner.IsDesignMode() && !Owner.Visible)
                    return false;
                return _visible;
            }
            set
            {
                _visible = value;
 
                if (Owner != null)
                {
                    if (_visible)
                    {
                        if (ContextualTabsCount > 0)
                            Owner.ActiveTab = ContextualTabs[0];
                    }
                    else
                    {
                        Owner.ActiveTab = Owner.Tabs[0];
                    }
                }
 
                if (Owner != null) Owner.OnRegionsChanged();
            }
        }
 
        /// <summary>
        /// Gets the <see cref="Bounds"/> property value
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Rectangle Bounds { get; private set; }
 
        /// <summary>
        /// Gets the bounds of the context header (the portion above the tab).
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Rectangle HeaderBounds { get; private set; }
 
        /// <summary>
        /// An enumerable list of RibbonTabs assigned to the context.
        /// </summary>
        /// <returns></returns>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<RibbonTab> ContextualTabs
        {
            get
            {
                List<RibbonTab> myList = new List<RibbonTab>();
                foreach (RibbonTab tab in Owner.Tabs)
                {
                    if (tab.Context == this)
                        myList.Add(tab);
                }
 
                return myList;
            }
        }
 
 
        /// <summary>
        /// Context tab count.
        /// </summary>
        /// <returns></returns>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int ContextualTabsCount
        {
            get
            {
                int total = 0;
                foreach (RibbonTab tab in Owner.Tabs)
                {
                    if (tab.Context == this)
                        total++;
                }
                return total;
            }
        }
 
        /// <summary>
        /// Gets the Ribbon that owns this context
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Ribbon Owner { get; private set; }
 
        #endregion
 
        #region Methods
 
        /// <summary>
        /// This method is not relevant for this class
        /// </summary>
        /// <exception cref="NotSupportedException">Always</exception>
        public void SetBounds(Rectangle bounds)
        {
            Bounds = bounds;
 
            //OnBoundsChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// This method is not relevant for this class
        /// </summary>
        /// <exception cref="NotSupportedException">Always</exception>
        public void SetHeaderBounds(Rectangle bounds)
        {
            HeaderBounds = bounds;
 
            //OnBoundsChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Sets the value of the Owner Property
        /// </summary>
        internal void SetOwner(Ribbon owner)
        {
            Owner = owner;
            //_tabs.SetOwner(owner);
        }
 
        /// <summary>
        /// When an item is removed from the RibbonItemCollection remove all its references.
        /// </summary>
        internal virtual void ClearOwner()
        {
            Owner = null;
            OnOwnerChanged(EventArgs.Empty);
        }
 
        /// <summary>
        /// Measures the size of the context.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public Size MeasureSize(object sender, RibbonElementMeasureSizeEventArgs e)
        {
            if (!Visible && !Owner.IsDesignMode()) return new Size(0, 0);
 
            return Size.Ceiling(e.Graphics.MeasureString(string.IsNullOrEmpty(Text) ? "   " : Text, Owner.Font));
        }
 
        public void OnPaint(object sender, RibbonElementPaintEventArgs e)
        {
            if (Owner == null) return;
 
            if (ContextualTabsCount > 0 || Owner.IsDesignMode())
            {
                Owner.Renderer.OnRenderRibbonContext(new RibbonContextRenderEventArgs(Owner, e.Graphics, e.Clip, this));
                Owner.Renderer.OnRenderRibbonContextText(new RibbonContextRenderEventArgs(Owner, e.Graphics, e.Clip, this));
            }
        }
 
        public void OnOwnerChanged(EventArgs e)
        {
            if (OwnerChanged != null)
            {
                OwnerChanged(this, e);
            }
        }
 
        #endregion
 
    }
}