3
using System.Collections;
7
public class GeneratorContext
12
Hashtable vars = new Hashtable ();
13
ArrayList generatedWrappers = new ArrayList ();
15
CodeStatementCollection statements;
16
GenerationOptions options;
17
ArrayList warnings = new ArrayList ();
18
CodeExpression rootObject;
20
public GeneratorContext (CodeNamespace cns, string idPrefix, CodeStatementCollection statements, GenerationOptions options)
23
this.idPrefix = idPrefix;
24
this.statements = statements;
25
this.options = options;
26
map = new WidgetMap (vars);
29
public CodeNamespace GlobalCodeNamespace {
33
public CodeStatementCollection Statements {
34
get { return statements; }
37
public GenerationOptions Options {
38
get { return options; }
41
public string[] Warnings {
42
get { return (string[]) warnings.ToArray (typeof(string)); }
45
public void ReportWarning (string s)
50
public string NewId ()
52
return idPrefix + (++n);
55
public CodeExpression GenerateNewInstanceCode (Wrapper.Widget widget)
57
CodeExpression exp = widget.GenerateObjectCreation (this);
58
CodeExpression var = GenerateInstanceExpression (widget, exp);
59
GenerateBuildCode (widget, var);
63
public virtual CodeExpression GenerateInstanceExpression (ObjectWrapper wrapper, CodeExpression newObject)
65
string varName = NewId ();
66
CodeVariableDeclarationStatement varDec = new CodeVariableDeclarationStatement (wrapper.WrappedTypeName, varName);
67
varDec.InitExpression = newObject;
68
statements.Add (varDec);
69
return new CodeVariableReferenceExpression (varName);
72
public virtual void GenerateCreationCode (ObjectWrapper wrapper, CodeExpression varExp)
75
wrapper.GenerateInitCode (this, varExp);
76
GenerateBuildCode (wrapper, varExp);
79
public virtual void GenerateBuildCode (ObjectWrapper wrapper, CodeExpression var)
82
wrapper.GenerateBuildCode (this, var);
83
generatedWrappers.Add (wrapper);
86
public virtual void GenerateCreationCode (Wrapper.ActionGroup agroup, CodeExpression var)
90
agroup.GenerateBuildCode (this, var);
93
public CodeExpression GenerateValue (object value, Type type)
95
return GenerateValue (value, type, false);
98
public CodeExpression GenerateValue (object value, Type type, bool translatable)
101
return new CodePrimitiveExpression (value);
103
if (value.GetType ().IsEnum) {
105
object ival = Convert.ChangeType (value, type);
106
return new CodePrimitiveExpression (ival);
108
long ival = (long) Convert.ChangeType (value, typeof(long));
109
return new CodeCastExpression (
110
new CodeTypeReference (value.GetType ()),
111
new CodePrimitiveExpression (ival)
116
if (value is Gtk.Adjustment) {
117
Gtk.Adjustment adj = value as Gtk.Adjustment;
118
return new CodeObjectCreateExpression (
119
typeof(Gtk.Adjustment),
120
new CodePrimitiveExpression (adj.Value),
121
new CodePrimitiveExpression (adj.Lower),
122
new CodePrimitiveExpression (adj.Upper),
123
new CodePrimitiveExpression (adj.StepIncrement),
124
new CodePrimitiveExpression (adj.PageIncrement),
125
new CodePrimitiveExpression (adj.PageSize));
127
if (value is ushort || value is uint) {
128
return new CodeCastExpression (
129
new CodeTypeReference (value.GetType ()),
130
new CodePrimitiveExpression (Convert.ChangeType (value, typeof(long))));
132
if (value is ulong) {
133
return new CodeMethodInvokeExpression (
134
new CodeTypeReferenceExpression (value.GetType ()),
136
new CodePrimitiveExpression (value.ToString ()));
139
if (value is ImageInfo && typeof(Gdk.Pixbuf).IsAssignableFrom (type))
140
return ((ImageInfo)value).ToCodeExpression (this);
142
if (value is Wrapper.ActionGroup) {
143
return new CodeMethodInvokeExpression (
144
new CodeMethodReferenceExpression (
145
new CodeTypeReferenceExpression (GlobalCodeNamespace.Name + ".ActionGroups"),
148
new CodePrimitiveExpression (((Wrapper.ActionGroup)value).Name)
152
if (value is Array) {
153
ArrayList list = new ArrayList ();
154
foreach (object val in (Array)value)
155
list.Add (GenerateValue (val, val != null ? val.GetType() : null, translatable));
156
return new CodeArrayCreateExpression (value.GetType().GetElementType(), (CodeExpression[]) list.ToArray(typeof(CodeExpression)));
159
if (value is DateTime) {
160
return new CodeObjectCreateExpression (
162
new CodePrimitiveExpression (((DateTime)value).Ticks)
166
if (value is TimeSpan) {
167
return new CodeObjectCreateExpression (
169
new CodePrimitiveExpression (((TimeSpan)value).Ticks)
173
string str = value as string;
174
if (translatable && str != null && str.Length > 0 && options.UseGettext) {
175
return new CodeMethodInvokeExpression (
176
new CodeTypeReferenceExpression (options.GettextClass),
178
new CodePrimitiveExpression (str)
182
return new CodePrimitiveExpression (value);
185
public WidgetMap WidgetMap {
189
public System.CodeDom.CodeExpression RootObject {
198
public void EndGeneration ()
200
foreach (ObjectWrapper w in generatedWrappers) {
201
CodeExpression var = (CodeExpression) vars [w];
202
w.GeneratePostBuildCode (this, var);
209
generatedWrappers.Clear ();
210
map = new WidgetMap (vars);
214
public CodeExpression GenerateLoadPixbuf (string name, Gtk.IconSize size)
217
foreach (CodeTypeDeclaration t in cns.Types) {
218
if (t.Name == "IconLoader") {
226
CodeTypeDeclaration cls = new CodeTypeDeclaration ("IconLoader");
227
cls.Attributes = MemberAttributes.Private;
228
cls.TypeAttributes = System.Reflection.TypeAttributes.NestedAssembly;
231
CodeMemberMethod met = new CodeMemberMethod ();
232
cls.Members.Add (met);
233
met.Attributes = MemberAttributes.Public | MemberAttributes.Static;
234
met.Name = "LoadIcon";
235
met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(Gtk.Widget), "widget"));
236
met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(string), "name"));
237
met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(Gtk.IconSize), "size"));
238
met.Parameters.Add (new CodeParameterDeclarationExpression (typeof(int), "sz"));
239
met.ReturnType = new CodeTypeReference (typeof(Gdk.Pixbuf));
241
CodeExpression widgetExp = new CodeVariableReferenceExpression ("widget");
242
CodeExpression nameExp = new CodeVariableReferenceExpression ("name");
243
CodeExpression sizeExp = new CodeVariableReferenceExpression ("size");
244
CodeExpression szExp = new CodeVariableReferenceExpression ("sz");
245
CodeExpression mgExp = new CodeBinaryOperatorExpression (szExp, CodeBinaryOperatorType.Divide, new CodePrimitiveExpression (4));
246
CodeExpression pmapExp = new CodeVariableReferenceExpression ("pmap");
247
CodeExpression gcExp = new CodeVariableReferenceExpression ("gc");
248
CodeExpression szM1Exp = new CodeBinaryOperatorExpression (szExp, CodeBinaryOperatorType.Subtract, new CodePrimitiveExpression (1));
249
CodeExpression zeroExp = new CodePrimitiveExpression (0);
250
CodeExpression resExp = new CodeVariableReferenceExpression ("res");
253
new CodeVariableDeclarationStatement (typeof(Gdk.Pixbuf), "res",
254
new CodeMethodInvokeExpression (
259
new CodePrimitiveExpression (null)
264
CodeConditionStatement nullcheck = new CodeConditionStatement ();
265
met.Statements.Add (nullcheck);
266
nullcheck.Condition = new CodeBinaryOperatorExpression (
268
CodeBinaryOperatorType.IdentityInequality,
269
new CodePrimitiveExpression (null)
271
nullcheck.TrueStatements.Add (new CodeMethodReturnStatement (resExp));
273
CodeTryCatchFinallyStatement trycatch = new CodeTryCatchFinallyStatement ();
274
nullcheck.FalseStatements.Add (trycatch);
275
trycatch.TryStatements.Add (
276
new CodeMethodReturnStatement (
277
new CodeMethodInvokeExpression (
278
new CodePropertyReferenceExpression (
279
new CodeTypeReferenceExpression (typeof(Gtk.IconTheme)),
290
CodeCatchClause ccatch = new CodeCatchClause ();
291
trycatch.CatchClauses.Add (ccatch);
293
CodeConditionStatement cond = new CodeConditionStatement ();
294
ccatch.Statements.Add (cond);
296
cond.Condition = new CodeBinaryOperatorExpression (
298
CodeBinaryOperatorType.IdentityInequality,
299
new CodePrimitiveExpression ("gtk-missing-image")
302
cond.TrueStatements.Add (
303
new CodeMethodReturnStatement (
304
new CodeMethodInvokeExpression (
305
new CodeTypeReferenceExpression (cns.Name + "." + cls.Name),
308
new CodePrimitiveExpression ("gtk-missing-image"),
315
CodeStatementCollection stms = cond.FalseStatements;
318
new CodeVariableDeclarationStatement (typeof(Gdk.Pixmap), "pmap",
319
new CodeObjectCreateExpression (
321
new CodePropertyReferenceExpression (
322
new CodePropertyReferenceExpression (
323
new CodeTypeReferenceExpression (typeof(Gdk.Screen)),
334
new CodeVariableDeclarationStatement (typeof(Gdk.GC), "gc",
335
new CodeObjectCreateExpression (typeof(Gdk.GC), pmapExp)
339
new CodeAssignStatement (
340
new CodePropertyReferenceExpression (
344
new CodeObjectCreateExpression (
346
new CodePrimitiveExpression (255),
347
new CodePrimitiveExpression (255),
348
new CodePrimitiveExpression (255)
353
new CodeMethodInvokeExpression (
357
new CodePrimitiveExpression (true),
365
new CodeAssignStatement (
366
new CodePropertyReferenceExpression (
370
new CodeObjectCreateExpression (
372
zeroExp, zeroExp, zeroExp
377
new CodeMethodInvokeExpression (
381
new CodePrimitiveExpression (false),
389
new CodeMethodInvokeExpression (
392
new CodePrimitiveExpression (3),
393
new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (typeof(Gdk.LineStyle)), "Solid"),
394
new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (typeof(Gdk.CapStyle)), "Round"),
395
new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (typeof(Gdk.JoinStyle)), "Round")
399
new CodeAssignStatement (
400
new CodePropertyReferenceExpression (
404
new CodeObjectCreateExpression (
406
new CodePrimitiveExpression (255),
413
new CodeMethodInvokeExpression (
419
new CodeBinaryOperatorExpression (szM1Exp, CodeBinaryOperatorType.Subtract, mgExp),
420
new CodeBinaryOperatorExpression (szM1Exp, CodeBinaryOperatorType.Subtract, mgExp)
424
new CodeMethodInvokeExpression (
428
new CodeBinaryOperatorExpression (szM1Exp, CodeBinaryOperatorType.Subtract, mgExp),
431
new CodeBinaryOperatorExpression (szM1Exp, CodeBinaryOperatorType.Subtract, mgExp)
435
new CodeMethodReturnStatement (
436
new CodeMethodInvokeExpression (
437
new CodeTypeReferenceExpression (typeof(Gdk.Pixbuf)),
440
new CodePropertyReferenceExpression (pmapExp, "Colormap"),
441
zeroExp, zeroExp, zeroExp, zeroExp, szExp, szExp
448
Gtk.Icon.SizeLookup (size, out sz, out h);
450
return new CodeMethodInvokeExpression (
451
new CodeTypeReferenceExpression (cns.Name + ".IconLoader"),
454
new CodePrimitiveExpression (name),
455
new CodeFieldReferenceExpression (
456
new CodeTypeReferenceExpression (typeof(Gtk.IconSize)),
459
new CodePrimitiveExpression (sz)
464
public class WidgetMap
468
internal WidgetMap (Hashtable vars)
473
public CodeExpression GetWidgetExp (ObjectWrapper wrapper)
475
return (CodeExpression) vars [wrapper];
478
public CodeExpression GetWidgetExp (object wrapped)
480
ObjectWrapper w = ObjectWrapper.Lookup (wrapped);
482
return GetWidgetExp (w);
489
public class GenerationOptions
493
bool generateEmptyBuildMethod;
494
bool generateSingleFile = true;
495
bool failForUnknownWidgets = false;
497
string globalNamespace = "Stetic";
500
public bool UseGettext {
501
get { return useGettext; }
502
set { useGettext = value; }
505
public string GettextClass {
507
if (gettextClass == null || gettextClass.Length == 0)
508
return "Mono.Unix.Catalog";
512
set { gettextClass = value; }
515
public bool UsePartialClasses {
516
get { return partialClasses; }
517
set { partialClasses = value; }
522
set { path = value; }
525
public bool GenerateEmptyBuildMethod {
526
get { return generateEmptyBuildMethod; }
527
set { generateEmptyBuildMethod = value; }
530
public bool GenerateSingleFile {
531
get { return generateSingleFile; }
532
set { generateSingleFile = value; }
535
public string GlobalNamespace {
536
get { return globalNamespace; }
537
set { globalNamespace = value; }
540
public bool FailForUnknownWidgets {
541
get { return failForUnknownWidgets; }
542
set { failForUnknownWidgets = value; }