TabPageSwitcherDesigner.cs
7.1 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
namespace RibbonStyle {
class TabPageSwitcherDesigner : ParentControlDesigner {
private DesignerVerbCollection verbs; // a collection of actions to perform (appear as links in propgrid, on designer action panel)
private ISelectionService selectionService; // service which lets you know when the selection changes in the designer.
/// <summary>
/// The TabPageSwitcher we're designing - strongly typed wrapper around Component property.
/// </summary>
public TabPageSwitcher ControlSwitcher {
get { return Component as TabPageSwitcher; }
}
/// <summary>
/// Fetches the selection service from the service provider - from this we can tell what's selected and when selection changes
/// </summary>
internal ISelectionService SelectionService {
get {
if (selectionService == null) {
selectionService = (ISelectionService)GetService(typeof(ISelectionService));
Debug.Assert(selectionService != null, "Failed to get Selection Service!");
}
return selectionService;
}
}
/// <summary>
/// List of "verbs" or actions to be used in the designer. These typically appear on the Context Menu,
/// links on the property grid, and as links on the designer action panel.
/// </summary>
public override System.ComponentModel.Design.DesignerVerbCollection Verbs {
get {
if (verbs == null) {
verbs = new DesignerVerbCollection();
verbs.Add(new DesignerVerb(TabStripControlLibrary.Properties.Resources.AddTab, new EventHandler(this.OnAdd)));
}
return verbs;
}
}
/// <summary>
/// when the designer disposes, we need to be careful about
/// unhooking from service events we've subscribed to.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing) {
SelectionService.SelectionChanged -= new EventHandler(SelectionService_SelectionChanged);
}
}
/// <summary>
/// This is called when the designer is first loaded.
/// Usually a good time to hook up to events. If you want to
/// set property defaults, InitializeNewComponent is what you
/// want to override
/// </summary>
/// <param name="component"></param>
public override void Initialize(IComponent component) {
base.Initialize(component);
SelectionService.SelectionChanged += new EventHandler(SelectionService_SelectionChanged);
}
public override bool CanParent(Control control) {
return control is TabStripPage;
}
/// <summary>
/// Method implementation for our "Add TabStripPage verb".
/// </summary>
/// <param name="sender"></param>
/// <param name="eevent"></param>
private void OnAdd(object sender, EventArgs eevent) {
// fetch our designer host
IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
if (host != null) {
// Create a transaction so we're friendly to undo/redo and serialization
DesignerTransaction t = null;
try {
try {
t = host.CreateTransaction(TabStripControlLibrary.Properties.Resources.AddTab + Component.Site.Name);
}
catch (CheckoutException ex) {
if (ex == CheckoutException.Canceled) {
return;
}
throw ex;
}
// Add a TabStripPage to the controls collection of the TabPageSwitcher
// Notify the TabPageSwitcher that it's control collection is changing.
MemberDescriptor member = TypeDescriptor.GetProperties(ControlSwitcher)["Controls"];
TabStripPage page = host.CreateComponent(typeof(TabStripPage)) as TabStripPage;
RaiseComponentChanging(member);
// add the page to the controls collection.
ControlSwitcher.Controls.Add(page);
// set the SelectedTabStripPage to the current page so that it opens correctly
SetProperty("SelectedTabStripPage", page);
// Raise event that we're done changing the controls property.
RaiseComponentChanged(member, null, null);
// if we have an associated TabStrip,
// add a matching Tab to the TabStrip.
if (ControlSwitcher.TabStrip != null) {
// add a tab to the toolstrip designer
MemberDescriptor itemsProp = TypeDescriptor.GetProperties(ControlSwitcher.TabStrip)["Items"];
Tab tab = host.CreateComponent(typeof(Tab)) as Tab;
RaiseComponentChanging(itemsProp);
ControlSwitcher.TabStrip.Items.Add(tab);
RaiseComponentChanged(itemsProp, null, null);
SetProperty(tab, "DisplayStyle", ToolStripItemDisplayStyle.ImageAndText);
SetProperty(tab, "Text", tab.Name);
SetProperty(tab, "TabStripPage", page);
SetProperty(ControlSwitcher.TabStrip, "SelectedTab", tab);
}
}
finally {
if (t != null)
t.Commit();
}
}
}
void SelectionService_SelectionChanged(object sender, EventArgs e) {
IList selectedComponents = (IList)SelectionService.GetSelectedComponents();
if (selectedComponents.Count == 1) {
Tab tab = selectedComponents[0] as Tab;
if (tab != null) {
SetProperty("SelectedTabStripPage", tab.TabStripPage);
SetProperty(tab, "Checked", true);
}
}
}
private void SetProperty(object target, string propname, object value) {
PropertyDescriptor propDescriptor = TypeDescriptor.GetProperties(target)[propname];
if (propDescriptor != null) {
propDescriptor.SetValue(target, value);
}
}
private void SetProperty(string propname, object value) {
SetProperty(ControlSwitcher, propname, value);
}
}
}