-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRibbon.cs
More file actions
295 lines (262 loc) · 9.45 KB
/
Ribbon.cs
File metadata and controls
295 lines (262 loc) · 9.45 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace PPTProgressMaker
{
public partial class Ribbon
{
private void Ribbon_Load(object sender, RibbonUIEventArgs e)
{
setEnabled(false);
Globals.ThisAddIn.Application.ColorSchemeChanged += Application_ColorSchemeChanged;
Globals.ThisAddIn.Application.PresentationOpen += Application_PresentationOpen;
Globals.ThisAddIn.Application.PresentationClose += Application_PresentationClose;
Globals.ThisAddIn.Application.PresentationBeforeSave += Application_PresentationBeforeSave;
}
private void Application_PresentationBeforeSave(Microsoft.Office.Interop.PowerPoint.Presentation Pres, ref bool Cancel)
{
}
private void Application_PresentationOpen(Microsoft.Office.Interop.PowerPoint.Presentation Pres)
{
setEnabled(true);
Application_ColorSchemeChanged(null);
loadStyle();
}
private void Application_PresentationClose(Microsoft.Office.Interop.PowerPoint.Presentation Pres)
{
setEnabled(false);
}
private void Application_ColorSchemeChanged(Microsoft.Office.Interop.PowerPoint.SlideRange SldRange)
{
var presentation = Globals.ThisAddIn.Application.ActivePresentation;
if (presentation != null)
{
ColorSchemeChanged(presentation);
}
}
private void setEnabled(bool enabled)
{
foreach (var t in this.Tabs)
{
foreach (var g in t.Groups)
{
foreach (var c in g.Items)
{
c.Enabled = enabled;
}
}
}
}
private void ColorSchemeChanged(Microsoft.Office.Interop.PowerPoint.Presentation presentation)
{
var colors = presentation.Designs[1].SlideMaster.Theme.ThemeColorScheme;
var coloridx = new int[]
{
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeAccent1).RGB,
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeAccent2).RGB,
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeAccent3).RGB,
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeAccent4).RGB,
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeAccent5).RGB,
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeAccent6).RGB,
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeDark1).RGB,
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeDark2).RGB,
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeLight1).RGB,
colors.Colors(Microsoft.Office.Core.MsoThemeColorSchemeIndex.msoThemeLight2).RGB,
};
initNormalColors(coloridx, 0, glNormal);
initNormalColors(coloridx, 5, glActive);
}
private void initNormalColors(int[] coloridx, int active, RibbonGallery gallery)
{
var items = gallery.Items;
items.Clear();
for (int i = 0; i < coloridx.Length; ++i)
{
items.Add(Factory.CreateRibbonDropDownItem());
items[i].Label = String.Format("Accent {0}", i);
items[i].Image = get16pxImage(coloridx[i]);
items[i].Tag = coloridx[i];
}
setGalleryColor(active, gallery);
}
private static void setGalleryColor(int active, RibbonGallery gallery)
{
var items = gallery.Items;
gallery.Image = items[active].Image;
gallery.Tag = items[active].Tag;
}
private static Image get16pxImage(int rgb)
{
int size = 16;
var bmp = new Bitmap(size, size);
using (Graphics gfx = Graphics.FromImage(bmp))
{
using (SolidBrush brush = new SolidBrush(ColorTranslator.FromOle(rgb)))
{
gfx.FillRectangle(brush, 0, 0, size, size);
}
}
return bmp;
}
private void btnHorizTOC_Click(object sender, RibbonControlEventArgs e)
{
addToC(ToCStyle.Types.Horizontal);
}
private void btnVertToc_Click(object sender, RibbonControlEventArgs e)
{
addToC(ToCStyle.Types.Vertical);
}
private void addToC(ToCStyle.Types type)
{
try
{
var style = new ToCStyle() { Type = type, Style = Style, RTL = RTL, FirstSlide = FirstSlide, SlideNumbers = SlideNumbers, IgnoreLastSection = IgnoreLastSection, NormalColor = NormalColor, ActiveColor = ActiveColor};
storeStyle(style);
ThisAddIn.instance.addToC(style);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private ToCStyle.Styles Style
{
get
{
if (cbGradient.Checked)
return ToCStyle.Styles.Gradient;
else
return ToCStyle.Styles.Solid;
}
}
private bool RTL
{
get { return cbRTL.Checked; }
}
private bool FirstSlide
{
get
{
return cbFirstSlide.Checked;
}
}
public bool IgnoreLastSection { get { return cbIgnoreLastSec.Checked; } }
private bool SlideNumbers
{
get
{
return cbSlideNumbers.Checked;
}
}
private void glActive_Click(object sender, RibbonControlEventArgs e)
{
setGalleryColor(glActive.SelectedItemIndex, glActive);
}
private void glNormal_Click(object sender, RibbonControlEventArgs e)
{
setGalleryColor(glNormal.SelectedItemIndex, glNormal);
}
private void setColor(RibbonGallery gl, int value)
{
for(int i = 0; i < gl.Items.Count; ++ i)
{
if((int)gl.Items[i].Tag == value)
{
gl.SelectedItemIndex = i;
setGalleryColor(i, gl);
return;
}
}
int pos = gl.Items.Count;
gl.Items.Add(Factory.CreateRibbonDropDownItem());
gl.Items[pos].Label = "Custom";
gl.Items[pos].Image = get16pxImage(value);
gl.Items[pos].Tag = value;
setGalleryColor(pos, gl);
}
public int NormalColor
{
get {
return (int)glNormal.Tag;
}
set
{
setColor(glNormal, value);
}
}
public int ActiveColor
{
get {
return (int)glActive.Tag;
}
set
{
setColor(glActive, value);
}
}
private void setPresTag(Microsoft.Office.Interop.PowerPoint.Presentation Pres, string tag, string value)
{
try
{
Pres.Tags.Delete(tag);
}
catch
{
}
Pres.Tags.Add(tag, value);
}
private string getPresTag(Microsoft.Office.Interop.PowerPoint.Presentation Pres, string tag)
{
try
{
return Pres.Tags[tag];
}
catch
{
return null;
}
}
private void loadStyle()
{
try
{
var state = Convert.FromBase64String(getPresTag(Globals.ThisAddIn.Application.ActivePresentation, "ae_state"));
using (var stream = new MemoryStream(state))
{
var bf = new BinaryFormatter();
var style = (ToCStyle)bf.Deserialize(stream);
applyUIStyle(style);
}
}
catch { }
}
private void applyUIStyle(ToCStyle style)
{
cbGradient.Checked = style.Style == ToCStyle.Styles.Gradient;
cbRTL.Checked = style.RTL;
cbFirstSlide.Checked = style.FirstSlide;
cbSlideNumbers.Checked = style.SlideNumbers;
cbIgnoreLastSec.Checked = style.IgnoreLastSection;
ActiveColor = style.ActiveColor;
NormalColor = style.NormalColor;
}
private void storeStyle(ToCStyle style)
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
bf.Serialize(stream, style);
byte[] data = stream.ToArray();
string state = Convert.ToBase64String(data);
setPresTag(Globals.ThisAddIn.Application.ActivePresentation, "ae_state", state);
}
}
}
}