~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.GtkCore/libstetic/GeneratorContext.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.CodeDom;
 
3
using System.Collections;
 
4
 
 
5
namespace Stetic
 
6
{
 
7
        public class GeneratorContext
 
8
        {
 
9
                CodeNamespace cns;
 
10
                int n;
 
11
                string idPrefix;
 
12
                Hashtable vars = new Hashtable ();
 
13
                ArrayList generatedWrappers = new ArrayList ();
 
14
                WidgetMap map;
 
15
                CodeStatementCollection statements;
 
16
                GenerationOptions options;
 
17
                ArrayList warnings = new ArrayList ();
 
18
                CodeExpression rootObject;
 
19
                
 
20
                public GeneratorContext (CodeNamespace cns, string idPrefix, CodeStatementCollection statements, GenerationOptions options)
 
21
                {
 
22
                        this.cns = cns;
 
23
                        this.idPrefix = idPrefix;
 
24
                        this.statements = statements;
 
25
                        this.options = options;
 
26
                        map = new WidgetMap (vars);
 
27
                }
 
28
                
 
29
                public CodeNamespace GlobalCodeNamespace {
 
30
                        get { return cns; }
 
31
                }
 
32
                
 
33
                public CodeStatementCollection Statements {
 
34
                        get { return statements; }
 
35
                }
 
36
                
 
37
                public GenerationOptions Options {
 
38
                        get { return options; }
 
39
                }
 
40
                
 
41
                public string[] Warnings {
 
42
                        get { return (string[]) warnings.ToArray (typeof(string)); }
 
43
                }
 
44
                
 
45
                public void ReportWarning (string s)
 
46
                {
 
47
                        warnings.Add (s);
 
48
                }
 
49
                
 
50
                public string NewId ()
 
51
                {
 
52
                        return idPrefix + (++n);
 
53
                }
 
54
                
 
55
                public CodeExpression GenerateNewInstanceCode (Wrapper.Widget widget)
 
56
                {
 
57
                        CodeExpression exp = widget.GenerateObjectCreation (this);
 
58
                        CodeExpression var = GenerateInstanceExpression (widget, exp);
 
59
                        GenerateBuildCode (widget, var);
 
60
                        return var;
 
61
                }
 
62
                
 
63
                public virtual CodeExpression GenerateInstanceExpression (ObjectWrapper wrapper, CodeExpression newObject)
 
64
                {
 
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);
 
70
                }
 
71
                
 
72
                public virtual void GenerateCreationCode (ObjectWrapper wrapper, CodeExpression varExp)
 
73
                {
 
74
                        rootObject = varExp;
 
75
                        wrapper.GenerateInitCode (this, varExp);
 
76
                        GenerateBuildCode (wrapper, varExp);
 
77
                }
 
78
                
 
79
                public virtual void GenerateBuildCode (ObjectWrapper wrapper, CodeExpression var)
 
80
                {
 
81
                        vars [wrapper] = var;
 
82
                        wrapper.GenerateBuildCode (this, var);
 
83
                        generatedWrappers.Add (wrapper);
 
84
                }
 
85
                
 
86
                public virtual void GenerateCreationCode (Wrapper.ActionGroup agroup, CodeExpression var)
 
87
                {
 
88
                        rootObject = var;
 
89
                        vars [agroup] = var;
 
90
                        agroup.GenerateBuildCode (this, var);
 
91
                }
 
92
                
 
93
                public CodeExpression GenerateValue (object value, Type type)
 
94
                {
 
95
                        return GenerateValue (value, type, false);
 
96
                }
 
97
                
 
98
                public CodeExpression GenerateValue (object value, Type type, bool translatable)
 
99
                {
 
100
                        if (value == null)
 
101
                                return new CodePrimitiveExpression (value);
 
102
                                
 
103
                        if (value.GetType ().IsEnum) {
 
104
                                if (!type.IsEnum) {
 
105
                                        object ival = Convert.ChangeType (value, type);
 
106
                                        return new CodePrimitiveExpression (ival);
 
107
                                } else {
 
108
                                        long ival = (long) Convert.ChangeType (value, typeof(long));
 
109
                                        return new CodeCastExpression (
 
110
                                                new CodeTypeReference (value.GetType ()), 
 
111
                                                new CodePrimitiveExpression (ival)
 
112
                                        );
 
113
                                }
 
114
                        }
 
115
                        
 
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));
 
126
                        }
 
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))));
 
131
                        }
 
132
                        if (value is ulong) {
 
133
                                return new CodeMethodInvokeExpression (
 
134
                                        new CodeTypeReferenceExpression (value.GetType ()),
 
135
                                        "Parse",
 
136
                                        new CodePrimitiveExpression (value.ToString ()));
 
137
                        }
 
138
                        
 
139
                        if (value is ImageInfo && typeof(Gdk.Pixbuf).IsAssignableFrom (type))
 
140
                                return ((ImageInfo)value).ToCodeExpression (this);
 
141
                        
 
142
                        if (value is Wrapper.ActionGroup) {
 
143
                                return new CodeMethodInvokeExpression (
 
144
                                        new CodeMethodReferenceExpression (
 
145
                                                new CodeTypeReferenceExpression (GlobalCodeNamespace.Name + ".ActionGroups"),
 
146
                                                "GetActionGroup"
 
147
                                        ),
 
148
                                        new CodePrimitiveExpression (((Wrapper.ActionGroup)value).Name)
 
149
                                );
 
150
                        }
 
151
                        
 
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)));
 
157
                        }
 
158
                        
 
159
                        if (value is DateTime) {
 
160
                                return new CodeObjectCreateExpression (
 
161
                                        typeof(DateTime),
 
162
                                        new CodePrimitiveExpression (((DateTime)value).Ticks)
 
163
                                );
 
164
                        }
 
165
                        
 
166
                        if (value is TimeSpan) {
 
167
                                return new CodeObjectCreateExpression (
 
168
                                        typeof(TimeSpan),
 
169
                                        new CodePrimitiveExpression (((TimeSpan)value).Ticks)
 
170
                                );
 
171
                        }
 
172
                        
 
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),
 
177
                                        "GetString",
 
178
                                        new CodePrimitiveExpression (str)
 
179
                                );
 
180
                        }
 
181
                        
 
182
                        return new CodePrimitiveExpression (value);
 
183
                }
 
184
                
 
185
                public WidgetMap WidgetMap {
 
186
                        get { return map; }
 
187
                }
 
188
 
 
189
                public System.CodeDom.CodeExpression RootObject {
 
190
                        get {
 
191
                                return rootObject;
 
192
                        }
 
193
                        set {
 
194
                                rootObject = value;
 
195
                        }
 
196
                }
 
197
                
 
198
                public void EndGeneration ()
 
199
                {
 
200
                        foreach (ObjectWrapper w in generatedWrappers) {
 
201
                                CodeExpression var = (CodeExpression) vars [w];
 
202
                                w.GeneratePostBuildCode (this, var);
 
203
                        }
 
204
                }
 
205
                
 
206
                public void Reset ()
 
207
                {
 
208
                        vars.Clear ();
 
209
                        generatedWrappers.Clear ();
 
210
                        map = new WidgetMap (vars);
 
211
                        n = 0;
 
212
                }
 
213
                
 
214
                public CodeExpression GenerateLoadPixbuf (string name, Gtk.IconSize size)
 
215
                {
 
216
                        bool found = false;
 
217
                        foreach (CodeTypeDeclaration t in cns.Types) {
 
218
                                if (t.Name == "IconLoader") {
 
219
                                        found = true;
 
220
                                        break;
 
221
                                }
 
222
                        }
 
223
                        
 
224
                        if (!found)
 
225
                        {
 
226
                                CodeTypeDeclaration cls = new CodeTypeDeclaration ("IconLoader");
 
227
                                cls.Attributes = MemberAttributes.Private;
 
228
                                cls.TypeAttributes = System.Reflection.TypeAttributes.NestedAssembly;
 
229
                                cns.Types.Add (cls);
 
230
                                
 
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));
 
240
                                
 
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");
 
251
                                
 
252
                                met.Statements.Add (
 
253
                                        new CodeVariableDeclarationStatement (typeof(Gdk.Pixbuf), "res",
 
254
                                                new CodeMethodInvokeExpression (
 
255
                                                        widgetExp,
 
256
                                                        "RenderIcon",
 
257
                                                        nameExp,
 
258
                                                        sizeExp,
 
259
                                                        new CodePrimitiveExpression (null)
 
260
                                                )
 
261
                                        )
 
262
                                );
 
263
                                
 
264
                                CodeConditionStatement nullcheck = new CodeConditionStatement ();
 
265
                                met.Statements.Add (nullcheck);
 
266
                                nullcheck.Condition = new CodeBinaryOperatorExpression (
 
267
                                        resExp,
 
268
                                        CodeBinaryOperatorType.IdentityInequality,
 
269
                                        new CodePrimitiveExpression (null)
 
270
                                );
 
271
                                nullcheck.TrueStatements.Add (new CodeMethodReturnStatement (resExp));
 
272
                                
 
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)),
 
280
                                                                "Default"
 
281
                                                        ),
 
282
                                                        "LoadIcon",
 
283
                                                        nameExp,
 
284
                                                        szExp,
 
285
                                                        zeroExp
 
286
                                                )
 
287
                                        )
 
288
                                );
 
289
                                
 
290
                                CodeCatchClause ccatch = new CodeCatchClause ();
 
291
                                trycatch.CatchClauses.Add (ccatch);                             
 
292
                                
 
293
                                CodeConditionStatement cond = new CodeConditionStatement ();
 
294
                                ccatch.Statements.Add (cond);
 
295
                                
 
296
                                cond.Condition = new CodeBinaryOperatorExpression (
 
297
                                        nameExp,
 
298
                                        CodeBinaryOperatorType.IdentityInequality,
 
299
                                        new CodePrimitiveExpression ("gtk-missing-image")
 
300
                                );
 
301
                                
 
302
                                cond.TrueStatements.Add (
 
303
                                        new CodeMethodReturnStatement (
 
304
                                                new CodeMethodInvokeExpression (
 
305
                                                        new CodeTypeReferenceExpression (cns.Name + "." + cls.Name),
 
306
                                                        "LoadIcon",
 
307
                                                        widgetExp,
 
308
                                                        new CodePrimitiveExpression ("gtk-missing-image"),
 
309
                                                        sizeExp,
 
310
                                                        szExp
 
311
                                                )
 
312
                                        )
 
313
                                );
 
314
                                
 
315
                                CodeStatementCollection stms = cond.FalseStatements;
 
316
                                
 
317
                                stms.Add (
 
318
                                        new CodeVariableDeclarationStatement (typeof(Gdk.Pixmap), "pmap", 
 
319
                                                new CodeObjectCreateExpression (
 
320
                                                        typeof(Gdk.Pixmap),
 
321
                                                        new CodePropertyReferenceExpression (
 
322
                                                                new CodePropertyReferenceExpression (
 
323
                                                                        new CodeTypeReferenceExpression (typeof(Gdk.Screen)),
 
324
                                                                        "Default"
 
325
                                                                ),
 
326
                                                                "RootWindow"
 
327
                                                        ),
 
328
                                                        szExp,
 
329
                                                        szExp
 
330
                                                )
 
331
                                        )
 
332
                                );
 
333
                                stms.Add (
 
334
                                        new CodeVariableDeclarationStatement (typeof(Gdk.GC), "gc", 
 
335
                                                new CodeObjectCreateExpression (typeof(Gdk.GC), pmapExp)
 
336
                                        )
 
337
                                );
 
338
                                stms.Add (
 
339
                                        new CodeAssignStatement (
 
340
                                                new CodePropertyReferenceExpression (
 
341
                                                        gcExp,
 
342
                                                        "RgbFgColor"
 
343
                                                ),
 
344
                                                new CodeObjectCreateExpression (
 
345
                                                        typeof(Gdk.Color),
 
346
                                                        new CodePrimitiveExpression (255),
 
347
                                                        new CodePrimitiveExpression (255),
 
348
                                                        new CodePrimitiveExpression (255)
 
349
                                                )
 
350
                                        )
 
351
                                );
 
352
                                stms.Add (
 
353
                                        new CodeMethodInvokeExpression (
 
354
                                                pmapExp,
 
355
                                                "DrawRectangle",
 
356
                                                gcExp,
 
357
                                                new CodePrimitiveExpression (true),
 
358
                                                zeroExp,
 
359
                                                zeroExp,
 
360
                                                szExp,
 
361
                                                szExp
 
362
                                        )
 
363
                                );
 
364
                                stms.Add (
 
365
                                        new CodeAssignStatement (
 
366
                                                new CodePropertyReferenceExpression (
 
367
                                                        gcExp,
 
368
                                                        "RgbFgColor"
 
369
                                                ),
 
370
                                                new CodeObjectCreateExpression (
 
371
                                                        typeof(Gdk.Color),
 
372
                                                        zeroExp, zeroExp, zeroExp
 
373
                                                )
 
374
                                        )
 
375
                                );
 
376
                                stms.Add (
 
377
                                        new CodeMethodInvokeExpression (
 
378
                                                pmapExp,
 
379
                                                "DrawRectangle",
 
380
                                                gcExp,
 
381
                                                new CodePrimitiveExpression (false),
 
382
                                                zeroExp,
 
383
                                                zeroExp,
 
384
                                                szM1Exp,
 
385
                                                szM1Exp
 
386
                                        )
 
387
                                );
 
388
                                stms.Add (
 
389
                                        new CodeMethodInvokeExpression (
 
390
                                                gcExp,
 
391
                                                "SetLineAttributes",
 
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")
 
396
                                        )
 
397
                                );
 
398
                                stms.Add (
 
399
                                        new CodeAssignStatement (
 
400
                                                new CodePropertyReferenceExpression (
 
401
                                                        gcExp,
 
402
                                                        "RgbFgColor"
 
403
                                                ),
 
404
                                                new CodeObjectCreateExpression (
 
405
                                                        typeof(Gdk.Color),
 
406
                                                        new CodePrimitiveExpression (255),
 
407
                                                        zeroExp,
 
408
                                                        zeroExp
 
409
                                                )
 
410
                                        )
 
411
                                );
 
412
                                stms.Add (
 
413
                                        new CodeMethodInvokeExpression (
 
414
                                                pmapExp,
 
415
                                                "DrawLine",
 
416
                                                gcExp,
 
417
                                                mgExp,
 
418
                                                mgExp,
 
419
                                                new CodeBinaryOperatorExpression (szM1Exp, CodeBinaryOperatorType.Subtract, mgExp),
 
420
                                                new CodeBinaryOperatorExpression (szM1Exp, CodeBinaryOperatorType.Subtract, mgExp)
 
421
                                        )
 
422
                                );
 
423
                                stms.Add (
 
424
                                        new CodeMethodInvokeExpression (
 
425
                                                pmapExp,
 
426
                                                "DrawLine",
 
427
                                                gcExp,
 
428
                                                new CodeBinaryOperatorExpression (szM1Exp, CodeBinaryOperatorType.Subtract, mgExp),
 
429
                                                mgExp,
 
430
                                                mgExp,
 
431
                                                new CodeBinaryOperatorExpression (szM1Exp, CodeBinaryOperatorType.Subtract, mgExp)
 
432
                                        )
 
433
                                );
 
434
                                stms.Add (
 
435
                                        new CodeMethodReturnStatement (
 
436
                                                new CodeMethodInvokeExpression (
 
437
                                                        new CodeTypeReferenceExpression (typeof(Gdk.Pixbuf)),
 
438
                                                        "FromDrawable",
 
439
                                                        pmapExp,
 
440
                                                        new CodePropertyReferenceExpression (pmapExp, "Colormap"),
 
441
                                                        zeroExp, zeroExp, zeroExp, zeroExp, szExp, szExp
 
442
                                                )
 
443
                                        )
 
444
                                );
 
445
                        }
 
446
                        
 
447
                        int sz, h;
 
448
                        Gtk.Icon.SizeLookup (size, out sz, out h);
 
449
                        
 
450
                        return new CodeMethodInvokeExpression (
 
451
                                new CodeTypeReferenceExpression (cns.Name + ".IconLoader"),
 
452
                                "LoadIcon",
 
453
                                rootObject,
 
454
                                new CodePrimitiveExpression (name),
 
455
                            new CodeFieldReferenceExpression (
 
456
                                        new CodeTypeReferenceExpression (typeof(Gtk.IconSize)),
 
457
                                        size.ToString ()
 
458
                                ),
 
459
                                new CodePrimitiveExpression (sz)
 
460
                        );
 
461
                }
 
462
        }
 
463
 
 
464
        public class WidgetMap
 
465
        {
 
466
                Hashtable vars;
 
467
                
 
468
                internal WidgetMap (Hashtable vars)
 
469
                {
 
470
                        this.vars = vars;
 
471
                }
 
472
                
 
473
                public CodeExpression GetWidgetExp (ObjectWrapper wrapper)
 
474
                {
 
475
                        return (CodeExpression) vars [wrapper];
 
476
                }
 
477
                
 
478
                public CodeExpression GetWidgetExp (object wrapped)
 
479
                {
 
480
                        ObjectWrapper w = ObjectWrapper.Lookup (wrapped);
 
481
                        if (w != null)
 
482
                                return GetWidgetExp (w);
 
483
                        else
 
484
                                return null;
 
485
                }
 
486
        }
 
487
        
 
488
        [Serializable]
 
489
        public class GenerationOptions
 
490
        {
 
491
                bool useGettext;
 
492
                bool partialClasses;
 
493
                bool generateEmptyBuildMethod;
 
494
                bool generateSingleFile = true;
 
495
                bool failForUnknownWidgets = false;
 
496
                string path;
 
497
                string globalNamespace = "Stetic";
 
498
                string gettextClass;
 
499
                
 
500
                public bool UseGettext {
 
501
                        get { return useGettext; }
 
502
                        set { useGettext = value; }
 
503
                }
 
504
                
 
505
                public string GettextClass {
 
506
                        get {
 
507
                                if (gettextClass == null || gettextClass.Length == 0)
 
508
                                        return "Mono.Unix.Catalog";
 
509
                                else
 
510
                                        return gettextClass;
 
511
                        }
 
512
                        set { gettextClass = value; }
 
513
                }
 
514
                
 
515
                public bool UsePartialClasses {
 
516
                        get { return partialClasses; }
 
517
                        set { partialClasses = value; }
 
518
                }
 
519
                
 
520
                public string Path {
 
521
                        get { return path; }
 
522
                        set { path = value; }
 
523
                }
 
524
                
 
525
                public bool GenerateEmptyBuildMethod {
 
526
                        get { return generateEmptyBuildMethod; }
 
527
                        set { generateEmptyBuildMethod = value; }
 
528
                }
 
529
                
 
530
                public bool GenerateSingleFile {
 
531
                        get { return generateSingleFile; }
 
532
                        set { generateSingleFile = value; }
 
533
                }
 
534
                
 
535
                public string GlobalNamespace {
 
536
                        get { return globalNamespace; }
 
537
                        set { globalNamespace = value; }
 
538
                }
 
539
                
 
540
                public bool FailForUnknownWidgets {
 
541
                        get { return failForUnknownWidgets; }
 
542
                        set { failForUnknownWidgets = value; }
 
543
                }
 
544
        }
 
545
}
 
546