JsRender/NodeToGtk.vala
[app.Builder.js] / JsRender / NodeToGtk.vala
1 /*
2
3  
4 */
5 public class JsRender.NodeToGtk : Object {
6
7         Node node;
8         Object wrapped_object; 
9         Gee.ArrayList<string> els;
10         //Gee.ArrayList<string> skip;
11         Gee.HashMap<string,string> ar_props;
12         public static int vcnt = 0; 
13
14         public NodeToGtk( Node node) 
15         {
16                 this.node = node;
17                 this.els = new Gee.ArrayList<string>(); 
18                 //this.skip = new Gee.ArrayList<string>();
19                 this.ar_props = new Gee.HashMap<string,string>();
20
21         }
22         
23         public Object? munge ( )
24         {
25
26                  return this.mungeNode (null);
27                  
28                      
29         }
30         public Object? mungeChild(  Node cnode)
31         {
32                 var x = new  NodeToGtk(cnode);
33                 return x.mungeNode(this.wrapped_object);
34         }
35         
36         public Object? mungeNode(Object? parent)
37         {
38                  
39                 var cls = this.node.fqn().replace(".", "");
40                 var ns = this.node.fqn().split(".")[0];
41                 var gtkbuilder = new global::Gtk.Builder();
42
43                 var cls_gtype = gtkbuilder.get_type_from_name(cls);
44                 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
45
46                 if (cls_gtype == GLib.Type.INVALID) {
47                         print("SKIP - gtype is invalid\n");
48                         return null;
49                 }
50                 // if it's a window... 
51
52                 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
53                         // what if it has none...
54                         if (this.node.items.size < 1) {
55                                 return null;
56                         }
57                         return this.mungeChild(this.node.items.get(0));
58                 }
59
60                 var ret = Object.new(cls_gtype);
61                 ret.ref(); //??? problematic?
62                 this.wrapped_object = ret;
63                 
64                  
65                 switch(cls) {
66                         // fixme
67                         case "GtkTreeStore": // top level.. - named and referenced
68                         case "GtkListStore": // top level.. - named and referenced
69                         case "GtkTreeViewColumn": // part of liststore?!?!
70                         case "GtkMenu": // top level..
71                         case "GtkCellRendererText":
72                         case "GtkSourceBuffer":                         
73                         case "GtkClutterActor"://fixme..
74                         ///case "GtkClutterEmbed"://fixme..
75                                 return null;
76                 }
77
78                 
79                 //var id = this.node.uid();
80                 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
81                 // properties..
82
83                 var props = Palete.Gir.factoryFqn(this.node.fqn()).props;
84                 
85               
86                 var pviter = props.map_iterator();
87                 while (pviter.next()) {
88                         
89                                 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
90                         var k = pviter.get_key();
91                         // skip items we have already handled..
92                         if  (!this.node.has(k)) {
93                                 continue;
94                         }
95                         // find out the type of the property...
96                         var type = pviter.get_value().type;
97                         type = Palete.Gir.fqtypeLookup(type, ns);
98
99                         var val = this.toValue(this.node.get(k).strip(), type);
100                         if (val == null) {
101                                 print("skip (failed to transform value %s type = %s from %s\n", 
102                                         cls + "." + k, type,  this.node.get(k).strip());
103                                 continue;
104                         }
105                         print ("set_property ( %s , %s)\n", k, this.node.get(k).strip());
106                         
107                         
108                         ret.set_property(k, val);  
109                         
110
111                 }
112                 // packing???
113                 // for now... - just try the builder style packing
114                 var do_pack =true;
115                 
116                 if (!cls_gtype.is_a(typeof(global::Gtk.Buildable))) {
117                         print("skipping pack  %s is not a buildable..\n", cls);
118                         do_pack = false;
119                 }
120                 if (parent == null) {
121                         //print("skipping pack  %s is not a buildable..\n", cls);
122                         do_pack = false;
123                 } else  if (!parent.get_type().is_a(typeof(global::Gtk.Buildable))) {
124                         print("skipping pack parent:%s is not a buildable..\n", parent.get_type().name());
125                         do_pack = false;
126                 }
127                 
128                 if (do_pack) {
129                         ((global::Gtk.Buildable)parent).add_child(gtkbuilder, ret, null);
130                 }
131                     
132
133                  
134                 if (this.node.items.size < 1) {
135                         return ret;
136                 }
137                 
138                 for (var i = 0; i < this.node.items.size; i++ ) {
139
140                          this.mungeChild(this.node.items.get(i));
141                          
142                 }
143                 
144                 return ret;
145                 
146
147                  
148
149         }
150
151         public GLib.Value? toValue(string val, string type) {
152
153                 var gtkbuilder = new global::Gtk.Builder();
154
155                 if (type == "utf8") {
156                         var qret = new GLib.Value(typeof(string));
157                         qret.set_string(val);
158                         return qret;
159                 }
160                 
161                 var prop_gtype = gtkbuilder.get_type_from_name(type);
162                 
163
164                 if (prop_gtype == GLib.Type.INVALID) {
165                          
166                         return null;
167                 }
168                 
169                 
170                 var ret = new GLib.Value(prop_gtype);
171
172
173                 switch(type) {
174                         case "gboolean":
175                                 ret.set_boolean(val.down() == "false" ? false : true);
176                                 return ret;
177                         case "guint":
178                                 ret.set_uint(int.parse(val));
179                                 return ret;
180                                 
181                         case "gint":
182                                 ret.set_int(int.parse(val));
183                                 return ret;
184
185                         case "gfloat":
186                                 ret.set_float(long.parse(val));
187                                 return ret;
188                                 
189                         case "utf8":
190                                 ret.set_string(val);
191                                 return ret;
192
193                         default:
194
195                                 var sval =new GLib.Value(typeof(string));
196                                 sval.set_string(val);
197                         
198                                 if (!sval.transform(ref ret)) {
199                                 
200                                         return null;
201                                 }
202                                 return ret;
203                 }
204         }
205         
206          
207           
208                 
209 }