yangyin
2024-10-14 af90d73f1740a8e15fe2c76282fb7e2ccad5df5d
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
// *********************************
// 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.ComponentModel;
using System.ComponentModel.Design;
 
namespace System.Windows.Forms
{
    internal abstract class RibbonElementWithItemCollectionDesigner
        : ComponentDesigner
    {
        #region Props
 
        /// <summary>
        /// Gets a reference to the Ribbon that owns the item
        /// </summary>
        public abstract Ribbon Ribbon { get; }
 
        /// <summary>
        /// Gets the collection of items hosted by this item
        /// </summary>
        public abstract RibbonItemCollection Collection { get; }
 
        /// <summary>
        /// Called when verbs must be retrieved
        /// </summary>
        /// <returns></returns>
        protected virtual DesignerVerbCollection OnGetVerbs()
        {
            return new DesignerVerbCollection(new[] {
               new DesignerVerb("Add Button", AddButton),
               new DesignerVerb("Add ButtonList", AddButtonList),
               new DesignerVerb("Add ItemGroup", AddItemGroup),
               new DesignerVerb("Add Separator", AddSeparator),
               new DesignerVerb("Add TextBox", AddTextBox),
               new DesignerVerb("Add ComboBox", AddComboBox),
               new DesignerVerb("Add ColorChooser", AddColorChooser),
               new DesignerVerb("Add DescriptionMenuItem", AddDescriptionMenuItem),
               new DesignerVerb("Add CheckBox", AddCheckBox),
               new DesignerVerb("Add UpDown", AddUpDown),
               new DesignerVerb("Add Label", AddLabel),
               new DesignerVerb("Add Host", AddHost)
            });
        }
 
        /// <summary>
        /// Overriden. Passes the verbs to the designer
        /// </summary>
        public override DesignerVerbCollection Verbs => OnGetVerbs();
 
        #endregion
 
        #region Methods
 
        /// <summary>
        /// Creates an item of the speciifed type
        /// </summary>
        /// <param name="t"></param>
        private void CreateItem(Type t)
        {
            CreateItem(Ribbon, Collection, t);
        }
 
        /// <summary>
        /// Creates an item of the specified type and adds it to the specified collection
        /// </summary>
        /// <param name="ribbon"></param>
        /// <param name="collection"></param>
        /// <param name="t"></param>
        protected virtual void CreateItem(Ribbon ribbon, RibbonItemCollection collection, Type t)
        {
            if (GetService(typeof(IDesignerHost)) is IDesignerHost host && collection != null && ribbon != null)
            {
                DesignerTransaction transaction = host.CreateTransaction("AddRibbonItem_" + Component.Site.Name);
 
                MemberDescriptor member = TypeDescriptor.GetProperties(Component)["Items"];
                RaiseComponentChanging(member);
 
                RibbonItem item = host.CreateComponent(t) as RibbonItem;
 
                if (!(item is RibbonSeparator))
                    if (item != null)
                        item.Text = item.Site.Name;
 
                collection.Add(item);
                ribbon.OnRegionsChanged();
 
                RaiseComponentChanged(member, null, null);
                transaction.Commit();
            }
        }
 
        protected virtual void AddButton(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonButton));
        }
 
        protected virtual void AddButtonList(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonButtonList));
        }
 
        protected virtual void AddItemGroup(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonItemGroup));
        }
 
        protected virtual void AddSeparator(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonSeparator));
        }
 
        protected virtual void AddTextBox(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonTextBox));
        }
 
        protected virtual void AddComboBox(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonComboBox));
        }
 
        protected virtual void AddColorChooser(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonColorChooser));
        }
 
        protected virtual void AddDescriptionMenuItem(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonDescriptionMenuItem));
        }
        protected virtual void AddCheckBox(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonCheckBox));
        }
        protected virtual void AddUpDown(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonUpDown));
        }
        protected virtual void AddLabel(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonLabel));
        }
        protected virtual void AddHost(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonHost));
        }
        #endregion
    }
}