This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
CallbacksBuilder.cs
351 lines (303 loc) · 9.86 KB
/
CallbacksBuilder.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System;
using System.Xml;
using System.Text;
using System.Diagnostics;
namespace CustomUIEditor
{
/// <summary>
/// Code to generate callbacks
/// </summary>
public class CallbacksBuilder
{
private CallbacksBuilder()
{
}
/// <summary>
/// Generates callbacks for given custom UI Xml.
/// </summary>
/// <param name="customUIXml">The custom UI Xml to generate callbacks for.</param>
/// <returns>List of callbacks.</returns>
public static System.Text.StringBuilder GenerateCallback(XmlDocument customUIXml)
{
StringBuilder result = new StringBuilder();
result.Append(XmlColorizer.rtfString);
if (attributeList == null)
{
attributeList = new System.Collections.Hashtable();
}
attributeList.Clear();
GenerateCallback(customUIXml.DocumentElement, result);
return ColorizingCallbacks(result);
}
private static void GenerateCallback(System.Xml.XmlNode node, System.Text.StringBuilder result)
{
if (node.Attributes != null)
{
foreach (XmlAttribute attribute in node.Attributes)
{
string callback = GenerateCallback(attribute);
if (callback == null || callback.Length == 0)
{
continue;
}
string controlID = GetControlID(node);
result.Append("<green>'Callback for ");
if (controlID == null || controlID.Length == 0)
{
result.Append(node.Name + "." + attribute.Name);
}
else
{
result.Append(controlID + " " + attribute.Name);
}
result.Append("\n");
result.Append(callback);
result.Append("\n\n");
}
}
if (node.HasChildNodes)
{
foreach (XmlNode child in node.ChildNodes)
{
GenerateCallback(child, result);
}
}
return;
}
private static StringBuilder ColorizingCallbacks(StringBuilder callbacks)
{
callbacks.Replace("\n", "\\par ");
callbacks.Replace("<green>", XmlColorizer.rtfComment);
callbacks.Replace("<blue>", XmlColorizer.rtfAttributeValue);
callbacks.Replace("<black>", XmlColorizer.rtfAttributeQuote);
//callbacks.Replace("'", XmlColorizer.rtfComment + "'");
//callbacks.Replace(SUB, XmlColorizer.rtfAttributeValue + SUB + XmlColorizer.rtfAttributeQuote);
//callbacks.Replace("End Sub", XmlColorizer.rtfAttributeValue + "End Sub" + XmlColorizer.rtfAttributeQuote);
//callbacks.Replace("As ", XmlColorizer.rtfAttributeValue + "As " + XmlColorizer.rtfAttributeQuote);
//callbacks.Replace("ByRef", XmlColorizer.rtfAttributeValue + "ByRef" + XmlColorizer.rtfAttributeQuote);
//callbacks.Replace(" Object", XmlColorizer.rtfAttributeValue + " Object" + XmlColorizer.rtfAttributeQuote);
//callbacks.Replace(" Boolean", XmlColorizer.rtfAttributeValue + " Boolean" + XmlColorizer.rtfAttributeQuote);
//callbacks.Replace(" Integer", XmlColorizer.rtfAttributeValue + " Integer" + XmlColorizer.rtfAttributeQuote);
//callbacks.Replace(" String", XmlColorizer.rtfAttributeValue + " String" + XmlColorizer.rtfAttributeQuote);
return callbacks;
}
private static string GetControlID(XmlNode node)
{
if (node.NodeType != XmlNodeType.Element)
{
return null;
}
try
{
foreach (XmlAttribute attribute in node.Attributes)
{
if (attribute.Name == "id" || attribute.Name == "idMso" || attribute.Name == "idQ")
{
return attribute.Value.Substring(attribute.Value.LastIndexOf(':') + 1);
}
}
}
catch (XmlException ex)
{
System.Diagnostics.Debug.Assert(false, ex.Message);
}
return null;
}
private static string GenerateCallback(System.Xml.XmlAttribute callback)
{
if (callback.Value == null || callback.Value.Length == 0)
{
return String.Empty;
}
string callbackValue = callback.Value.Substring(callback.Value.LastIndexOf('.') + 1);
Debug.Assert(attributeList != null);
if (attributeList.ContainsKey(callbackValue))
{
return String.Empty;
}
attributeList.Add(callbackValue, callbackValue);
BaseCallbackType callbackType = MapToBase(callback);
switch (callbackType)
{
case BaseCallbackType.buttonOnAction:
return GenerateVoidCallback(callbackValue);
case BaseCallbackType.commandOnAction:
return GenerateVoidCommand(callbackValue);
case BaseCallbackType.toggleButtonOnAction:
return GenerateToggleButtonOnActionCallback(callbackValue);
case BaseCallbackType.galleryOnAction:
return GenerateItemVoidCallback(callbackValue);
case BaseCallbackType.comboBoxOnChange:
return GenerateVoidOnChangeCallback(callbackValue);
case BaseCallbackType.getBoolean:
case BaseCallbackType.getString:
case BaseCallbackType.getInt:
case BaseCallbackType.getImage:
case BaseCallbackType.getSize:
return GenerateReturnCallback(callbackValue);
case BaseCallbackType.getItemString:
case BaseCallbackType.getItemImage:
return GenerateItemReturnCallback(callbackValue);
case BaseCallbackType.onLoad:
return GenerateOnLoadCallback(callbackValue);
case BaseCallbackType.onShow:
return GenerateOnShowCallback(callbackValue);
case BaseCallbackType.loadImage:
return GenerateLoadImageCallback(callbackValue);
case BaseCallbackType.getStyle:
return GenerateGetSlabStyleCallback(callbackValue);
case BaseCallbackType.unKnown:
Debug.Assert(false, "Unkown callback " + callback.OwnerDocument.Name + "." + callback.Name);
return String.Empty;
case BaseCallbackType.notCallback:
default:
return String.Empty;
}
}
private const string CONTROL_STRING = "control <blue>As<black> IRibbonControl";
private const string SUB = "<blue>Sub<black> ";
private const string ENDSUB = "\n<blue>End Sub<black>";
private static string GenerateOnLoadCallback(string callback)
{
return SUB + callback + "(ribbon <blue>As<black> IRibbonUI)" + ENDSUB;
}
private static string GenerateOnShowCallback(string callback)
{
return SUB + callback + "(contextObject <blue>As Object<black>)" + ENDSUB;
}
private static string GenerateLoadImageCallback(string callback)
{
return SUB + callback + "(imageID <blue>As String<black>, <blue>ByRef<black> returnedVal)" + ENDSUB;
}
private static string GenerateVoidCommand(string callback)
{
return SUB + callback + "(" + CONTROL_STRING + ", <blue>ByRef<black> cancelDefault)" + ENDSUB;
}
private static string GenerateVoidCallback(string callback)
{
return SUB + callback + "(" + CONTROL_STRING + ")" + ENDSUB;
}
private static string GenerateVoidOnChangeCallback(string callback)
{
return SUB + callback + "(" + CONTROL_STRING + ", text <blue>As String<black>)" + ENDSUB;
}
private static string GenerateToggleButtonOnActionCallback(string callback)
{
return SUB + callback + "(" + CONTROL_STRING + ", pressed <blue>As Boolean<black>)" + ENDSUB;
}
private static string GenerateReturnCallback(string callback)
{
return SUB + callback + "(" + CONTROL_STRING + ", <blue>ByRef<black> returnedVal)" + ENDSUB;
}
private static string GenerateItemVoidCallback(string callback)
{
return SUB + callback + "(" + CONTROL_STRING + ", id <blue>As String<black>, index <blue>As Integer<black>)" + ENDSUB;
}
private static string GenerateItemReturnCallback(string callback)
{
return SUB + callback + "(" + CONTROL_STRING + ", index <blue>As Integer<black>, <blue>ByRef<black> returnedVal)" + ENDSUB;
}
private static string GenerateGetSlabStyleCallback(string callback)
{
return SUB + callback + "(" + CONTROL_STRING + ", <blue>ByRef<black> returnedVal)" + "\n\treturnedVal = BackstageGroupStyle.BackstageGroupStyleWarning" + ENDSUB;
}
private static BaseCallbackType MapToBase(System.Xml.XmlAttribute callback)
{
switch (callback.Name)
{
case "onLoad":
return BaseCallbackType.onLoad;
case "onShow":
case "onHide":
return BaseCallbackType.onShow;
case "loadImage":
return BaseCallbackType.loadImage;
case "onAction":
switch (callback.OwnerElement.Name)
{
case "dropDown":
case "gallery":
return BaseCallbackType.galleryOnAction;
case "command":
return BaseCallbackType.commandOnAction;
case "toggleButton":
case "checkBox":
return BaseCallbackType.toggleButtonOnAction;
default:
return BaseCallbackType.buttonOnAction;
}
case "onChange":
return BaseCallbackType.comboBoxOnChange;
case "getEnabled":
case "getVisible":
case "getPressed":
case "getShowLabel":
case "getShowImage":
return BaseCallbackType.getBoolean;
case "getLabel":
case "getScreentip":
case "getSupertip":
case "getDescription":
case "getKeytip":
case "getSelectedItemId":
case "getImageMso":
case "getContent":
case "getText":
case "getTitle":
case "getTarget":
case "getHelperText":
return BaseCallbackType.getString;
case "getItemLabel":
case "getItemTooltip":
case "getItemId":
return BaseCallbackType.getItemString;
case "getImage":
return BaseCallbackType.getImage;
case "getItemImage":
return BaseCallbackType.getItemImage;
case "getItemCount":
case "getItemIndex":
case "getItemHeight":
case "getItemWidth":
case "getSelectedItemIndex":
return BaseCallbackType.getInt;
case "getSize":
case "getItemSize":
return BaseCallbackType.getSize;
case "getStyle":
return BaseCallbackType.getStyle;
default:
if (callback.Name.StartsWith("on") || callback.Name.StartsWith("get"))
{
return BaseCallbackType.unKnown;
}
else
{
return BaseCallbackType.notCallback;
}
}
}
private static System.Collections.Hashtable attributeList;
private enum BaseCallbackType
{
buttonOnAction,
toggleButtonOnAction,
commandOnAction,
galleryOnAction,
comboBoxOnChange,
getBoolean,
getString,
getItemString,
getImage,
getItemImage,
getInt,
getSize,
getStyle,
onShow,
onLoad,
loadImage,
unKnown,
notCallback, //Always last
}
}
}