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
// *********************************
// 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;
using System.Windows.Forms.Design.Behavior;
 
namespace System.Windows.Forms
{
    public class RibbonTabDesigner
        : ComponentDesigner
    {
        private Adorner _panelAdorner;
 
        public override DesignerVerbCollection Verbs => new DesignerVerbCollection(new[] {
          new DesignerVerb("Add Panel", AddPanel)
      });
 
        public RibbonTab Tab => Component as RibbonTab;
 
        public void AddPanel(object sender, EventArgs e)
        {
            if (GetService(typeof(IDesignerHost)) is IDesignerHost host && Tab != null)
            {
 
 
                DesignerTransaction transaction = host.CreateTransaction("AddPanel" + Component.Site.Name);
                MemberDescriptor member = TypeDescriptor.GetProperties(Component)["Panels"];
                RaiseComponentChanging(member);
 
                if (host.CreateComponent(typeof(RibbonPanel)) is RibbonPanel panel)
                {
                    panel.Text = panel.Site.Name;
 
                    //Michael Spradlin 07/05/2013 Added Panel Index code so we can tell where a panel is at on the ribbon.
                    panel.Index = Tab.Panels.Count;
 
                    if (panel.Index == 0)
                    {
                        panel.IsFirstPanel = true;
                    }
                    else
                    {
                        foreach (RibbonPanel pnl in Tab.Panels)
                        {
                            pnl.IsLastPanel = false;
                        }
 
                        panel.IsLastPanel = true;
                    }
 
                    Tab.Panels.Add(panel);
                    Tab.Owner.OnRegionsChanged();
                }
 
                RaiseComponentChanged(member, null, null);
                transaction.Commit();
            }
        }
 
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
 
            _panelAdorner = new Adorner();
 
            //Kevin Carbis - another point where exception is thrown by the designer when current is null
            if (RibbonDesigner.Current != null)
            {
                BehaviorService bs = RibbonDesigner.Current.GetBehaviorService();
 
                if (bs == null) return;
 
                bs.Adorners.AddRange(new[] { _panelAdorner });
 
                _panelAdorner.Glyphs.Add(new RibbonPanelGlyph(bs, this, Tab));
            }
        }
    }
}