-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThisAddIn.cs
305 lines (257 loc) · 10.9 KB
/
ThisAddIn.cs
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
296
297
298
299
300
301
302
303
304
305
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Diagnostics;
using System.Drawing;
namespace PPTProgressMaker
{
public partial class ThisAddIn
{
public const string HORIZ_SMART_ART_OBJECT_NAME = "Closed Chevron Process";
public const string VERT_SMART_ART_OBJECT_NAME = "Vertical Curved List";
public const string SHAPE_TAG = "AEContents";
public static ThisAddIn instance;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
instance = this;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
public void addToC(ToCStyle tocstyle)
{
applyStyleDelegate styler = getStyler(tocstyle);
string[] secnames;
double[] pcnt;
deleteOldToC();
buildToCData(out secnames, out pcnt);
switch (tocstyle.Type)
{
case ToCStyle.Types.Horizontal:
addHorizontalToC(secnames, pcnt, styler, tocstyle);
break;
case ToCStyle.Types.Vertical:
addVerticalToC(secnames, pcnt, styler, tocstyle);
break;
}
}
private applyStyleDelegate getStyler(ToCStyle tocstyle)
{
applyStyleDelegate styler;
switch (tocstyle.Style)
{
case ToCStyle.Styles.Solid: styler = getSolidStyle(tocstyle); break;
case ToCStyle.Styles.Gradient: styler = getGradientStyle(tocstyle); break;
default:
throw new Exception("Unknown color style");
}
return styler;
}
delegate void applyStyleDelegate(Microsoft.Office.Core.ShapeRange shapes, int shapeidx, int sldidx, double sldpcnt);
private applyStyleDelegate getSolidStyle(ToCStyle style)
{
var normal = style.NormalColor;
var active = style.ActiveColor;
return (shapes, shapeidx, sldidx, sldpcnt) =>
{
var col = (sldidx == shapeidx) ? active : normal;
shapes.Fill.TwoColorGradient(Office.MsoGradientStyle.msoGradientVertical, 1);
var gs = shapes.Fill.GradientStops;
gs.Insert(col, 0f, 0);
gs.Insert(col, 1f);
gs.Delete(1);
gs.Delete(1);
};
}
private applyStyleDelegate getGradientStyle(ToCStyle style)
{
var active = style.ActiveColor;
var normal = style.NormalColor;
return (shapes, shapeidx, sldidx, sldpcnt) =>
{
var col1 = active;
var col2 = active;
var col3 = active;
if (sldidx == shapeidx)
{
col3 = normal;
}
else if (sldidx < shapeidx)
{
col1 = col2 = col3 = normal;
}
shapes.Fill.TwoColorGradient(style.Type == ToCStyle.Types.Vertical ? Office.MsoGradientStyle.msoGradientHorizontal : Office.MsoGradientStyle.msoGradientVertical, 1);
var gs = shapes.Fill.GradientStops;
gs.Insert(col1, 0f, 0);
gs.Insert(col2, (float)(sldpcnt));
if(sldpcnt < 0.6)
gs.Insert(col3, 0.6f);
else if (sldpcnt < 0.8)
gs.Insert(col3, 0.8f);
gs.Insert(col3, 1f);
gs.Delete(1);
gs.Delete(1);
};
}
private void buildToCData(out string[] secnames, out double[] pcnt)
{
var p = Application.ActivePresentation;
int num_slides = p.Slides.Count;
pcnt = new double[num_slides];
// per section
var sections = p.SectionProperties;
secnames = new string[sections.Count];
for (int i = 1; i <= sections.Count; ++i)
{
string name = sections.Name(i);
secnames[i - 1] = name;
int first_slide = sections.FirstSlide(i);
int last_slide = (i + 1 <= sections.Count) ? sections.FirstSlide(i + 1) : num_slides;
int hidden = 0;
for (int j = first_slide; j <= last_slide && j >= 1; ++j)
if (p.Slides[j].SlideShowTransition.Hidden == Office.MsoTriState.msoTrue)
hidden += 1;
double count = last_slide - first_slide + 1.0 - hidden;
int hidden_pos = 0;
for (int j = first_slide; j <= last_slide && j >= 1; ++j)
{
if (p.Slides[j].SlideShowTransition.Hidden == Office.MsoTriState.msoTrue)
hidden_pos += 1;
pcnt[j - 1] = (j - hidden_pos - first_slide + 1.0) / count;
}
}
}
private void deleteOldToC()
{
var p = Application.ActivePresentation;
var slides = p.Slides;
int num_slides = slides.Count;
for (int i = 0; i < num_slides; ++i)
{
var slide = slides[i + 1];
var old = getShapeSafe(slide, SHAPE_TAG);
if (old != null)
old.Delete();
}
}
private void addHorizontalToC(string[] secnames, double[] pcnt, applyStyleDelegate styler, ToCStyle style)
{
var p = Application.ActivePresentation;
var slides = p.Slides;
int num_slides = slides.Count;
int id = getSmartArtObjectByName(HORIZ_SMART_ART_OBJECT_NAME);
var layout = Application.SmartArtLayouts[id];
float height = p.PageSetup.SlideHeight / 20;
float width = p.PageSetup.SlideWidth;
float left = 0;
float top = p.PageSetup.SlideHeight - height;
for (int i = getFirstSlide(style); i < num_slides; ++i)
{
var slide = slides[i + 1];
var shape = slide.Shapes.AddSmartArt(layout, left, top, width, height);
shape.Name = SHAPE_TAG;
if (style.RTL)
shape.SmartArt.Reverse = Microsoft.Office.Core.MsoTriState.msoTrue;
formatToCShape(shape, i, slide.SectionNumber, secnames, pcnt, styler, style);
}
}
private void addVerticalToC(string[] secnames, double[] pcnt, applyStyleDelegate styler, ToCStyle style)
{
var p = Application.ActivePresentation;
var slides = p.Slides;
int num_slides = slides.Count;
int id = getSmartArtObjectByName(VERT_SMART_ART_OBJECT_NAME);
var layout = Application.SmartArtLayouts[id];
float height = p.PageSetup.SlideHeight;
float width = p.PageSetup.SlideWidth / 6;
float left = style.RTL ? (p.PageSetup.SlideWidth - width) : 0;
float top = 0;
for (int i = getFirstSlide(style); i < num_slides; ++i)
{
var slide = slides[i + 1];
var shape = slide.Shapes.AddSmartArt(layout, left, top, width, height);
shape.Name = SHAPE_TAG;
if(style.RTL)
shape.SmartArt.Reverse = Microsoft.Office.Core.MsoTriState.msoTrue;
formatToCShape(shape, i, slide.SectionNumber, secnames, pcnt, styler, style);
}
}
private int getFirstSlide(ToCStyle style)
{
return style.FirstSlide ? 0 : 1;
}
private void formatToCShape(PowerPoint.Shape shape, int sldindex, int secindex, string[] secnames, double[] pcnt, applyStyleDelegate styler, ToCStyle style)
{
bool sldnumbers = style.Type == ToCStyle.Types.Horizontal && style.SlideNumbers;
int fsofs = sldnumbers ? 1 : 0;
int seccount = secnames.Length;
if (style.IgnoreLastSection)
seccount -= 1;
for (int i = shape.SmartArt.Nodes.Count + 1; i<= seccount + fsofs; ++ i)
shape.SmartArt.Nodes.Add();
if(sldnumbers)
{
var node = shape.SmartArt.Nodes[1];
node.TextFrame2.TextRange.Text = sldindex.ToString();
float height = node.Shapes.Height;
while (node.Shapes.Height == height)
node.Smaller();
node.Larger();
node.TextFrame2.TextRange.ParagraphFormat.Alignment = style.RTL ? Office.MsoParagraphAlignment.msoAlignRight: Office.MsoParagraphAlignment.msoAlignLeft;
styler(node.Shapes, 1, 1, 1);
}
for (int i = 0; i < seccount; ++i)
{
var node = shape.SmartArt.Nodes[i + 1 + fsofs];
node.TextFrame2.TextRange.Text = secnames[i];
styler(node.Shapes, i, secindex, pcnt[sldindex]);
}
}
private PowerPoint.Shape getShapeSafe(PowerPoint.Slide slide, string name)
{
try
{
return slide.Shapes[name];
}
catch
{
return null;
}
}
private int getSmartArtObjectByName(string name)
{
for(int i = 1; i <= Application.SmartArtLayouts.Count; ++i)
{
Debug.WriteLine(Application.SmartArtLayouts[i].Name);
if (Application.SmartArtLayouts[i].Name.Equals(name))
return i;
}
throw new Exception("Could not find smart art object");
}
private PowerPoint.Shape findShapeByTag(PowerPoint.Slide slide, string name)
{
for (int i = 1; i < slide.Shapes.Count; ++i)
{
var s = slide.Shapes[i];
if (s.Tags[name] != null)
return s;
}
return null;
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}