src/JsRender/NodeToGtk.vala
[app.Builder.js] / src / JsRender / NodeToGtk.vala
1 /*
2
3  
4 */
5 public class JsRender.NodeToGtk : Object {
6
7         Node node;
8         Object wrapped_object; 
9         NodeToGtk parentObj;
10         
11         Gee.ArrayList<NodeToGtk> children;
12         
13         Gee.ArrayList<string> els;
14         //Gee.ArrayList<string> skip;
15         Gee.HashMap<string,string> ar_props;
16         public static int vcnt = 0; 
17
18         public NodeToGtk( Node node) 
19         {
20                 this.node = node;
21                 this.els = new Gee.ArrayList<string>(); 
22                 this.children = new Gee.ArrayList<NodeToGtk>(); 
23                 //this.skip = new Gee.ArrayList<string>();
24                 this.ar_props = new Gee.HashMap<string,string>();
25                 this.parentObj = null;
26         }
27         
28         public Object? munge ( )
29         {
30                 var ret = this.mungeNode();
31                 if (ret == null) {
32                         return null;
33                 }
34                         
35                 return ret.wrapped_object;
36                       
37         }
38         public NodeToGtk? mungeChild(  Node cnode)
39         {
40                 var x = new  NodeToGtk(cnode);
41                 x.parentObj = this;
42                 return x.mungeNode();
43                 
44         }
45         
46         public NodeToGtk? mungeNode()
47         {
48
49                 var parent = this.parentObj != null ? this.parentObj.wrapped_object : null;
50                 var cls = this.node.fqn().replace(".", "");
51                 var ns = this.node.fqn().split(".")[0];
52                 var gtkbuilder = new global::Gtk.Builder();
53
54                 var cls_gtype = gtkbuilder.get_type_from_name(cls);
55                 print("Type: %s ?= %s\n", this.node.fqn(), cls_gtype.name());
56
57                 if (cls_gtype == GLib.Type.INVALID) {
58                         print("SKIP - gtype is invalid\n");
59                         return null;
60                 }
61                 // if it's a window... 
62
63                 if (cls_gtype.is_a(typeof(global::Gtk.Window))) {
64                         // what if it has none...
65                         if (this.node.items.size < 1) {
66                                 return null;
67                         }
68                         return this.mungeChild(this.node.items.get(0));
69                 }
70
71                 var ret = Object.new(cls_gtype);
72                 ret.ref(); //??? problematic?
73                 this.wrapped_object = ret;
74                 
75                  
76                 switch(cls) {
77                         // fixme
78                         //case "GtkTreeStore": // top level.. - named and referenced
79                         case "GtkListStore": // top level.. - named and referenced
80                         //case "GtkTreeViewColumn": // part of liststore?!?!
81                         //case "GtkMenu": // top level..
82                         //case "GtkCellRendererText":
83                         case "GtkSourceBuffer":                         
84                         case "GtkClutterActor"://fixme..
85                         case "GtkClutterEmbed"://fixme.. -- we can not nest embedded.. need to solve..
86                                         
87                                 return null;
88                 }
89
90                 this.packParent();
91                 
92
93                 // pack paramenters
94
95                 
96                 if (parent != null && parent.get_type().is_a(typeof(global::Gtk.Container))) {
97                         this.packContainerParams();
98                 }
99                 
100                 var cls_gir =Palete.Gir.factoryFqn(this.node.fqn()); 
101                 if (cls_gir == null) {
102                         return null;
103                 }
104                 //var id = this.node.uid();
105                 //var ret = @"$pad<object class=\"$cls\" id=\"$id\">\n";
106                 // properties..
107                 var props = cls_gir.props;
108                 
109               
110                 var pviter = props.map_iterator();
111                 while (pviter.next()) {
112                         
113                                 // print("Check: " +cls + "::(" + pviter.get_value().propertyof + ")" + pviter.get_key() + " " );
114                         var k = pviter.get_key();
115                         // skip items we have already handled..
116                         if  (!this.node.has(k)) {
117                                 continue;
118                         }
119                         // find out the type of the property...
120                         var type = pviter.get_value().type;
121                         type = Palete.Gir.fqtypeLookup(type, ns);
122
123                         var val = this.toValue(this.node.get(k).strip(), type);
124                         if (val == null) {
125                                 print("skip (failed to transform value %s type = %s from %s\n", 
126                                         cls + "." + k, type,  this.node.get(k).strip());
127                                 continue;
128                         }
129                         print ("set_property ( %s , %s / %s)\n", k, this.node.get(k).strip(), val.strdup_contents());
130                         
131                         
132                         ret.set_property(k, val);  
133                         
134
135                 }
136                 // packing???
137                 // for now... - just try the builder style packing
138                 
139                 
140                  
141                 if (this.node.items.size < 1) {
142                         return this;
143                 }
144                 
145                 for (var i = 0; i < this.node.items.size; i++ ) {
146
147                          this.mungeChild(this.node.items.get(i));
148                          
149                 }
150                 
151                 this.afterChildren();
152                 
153                 return this;
154                 
155
156                  
157
158         }
159         
160         function afterChildren()
161         {
162                 // things like GtkNotebook - we have to pack children after they have been created..
163                 
164                 
165                 
166                 
167         }
168         
169         /**
170          * called after the this.object  has been created
171          * and it needs to be packed onto parent.
172          */
173         public void packParent() 
174         {
175                 var cls = this.node.fqn().replace(".", "");
176                 
177                 var gtkbuilder = new global::Gtk.Builder();
178                 var cls_gtype = gtkbuilder.get_type_from_name(cls);
179
180                 if (this.parentObj == null) {
181                         return;
182                 }
183                                 
184                     
185                 var parent = this.parentObj.wrapped_object;
186                 
187                 var do_pack =true;
188
189                 if (parent == null) { // no parent.. can not pack.
190                         return; /// 
191                 }
192                 // -------------  handle various special parents .. -----------
193                 
194                 var par_type = this.parentObj.node.fqn().replace(".", "");
195                 
196                 if (par_type == "GtkNotebook") {
197                         // do not pack - it's done afterwards...
198                         return;
199                 }
200                 
201                 // -------------  handle various child types.. -----------
202                 // our overrides
203                 if (cls == "GtkMenu") {
204                         this.packMenu();
205                         return;
206                 }
207
208                 if (cls == "GtkTreeStore") { // other stores?
209                         // tree store is buildable??? --- 
210                         this.packTreeStore();
211                         return;
212                 }
213                 if (cls =="GtkTreeViewColumn") { // other stores?
214                         //?? treeview column is actually buildable -- but we do not use the builder???
215                         this.packTreeViewColumn();
216                         return;
217                 }
218                 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
219                         this.packCellRenderer();
220                         return;
221                 }
222
223
224                 
225                 // -- handle buildable add_child..
226                 if (    cls_gtype.is_a(typeof(global::Gtk.Buildable))
227                      && 
228                         parent.get_type().is_a(typeof(global::Gtk.Buildable))
229                 )
230                 {
231                         ((global::Gtk.Buildable)parent).add_child(gtkbuilder, 
232                                                   this.wrapped_object, null);
233                         return;
234                 }
235                 // other packing?
236
237                 
238
239         }
240
241         public void packMenu()
242         {
243
244
245                 var parent = this.parentObj.wrapped_object;
246                 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
247                         print("skip menu pack - parent is not a widget");
248                         return;
249                 }
250                 
251                 var p = (global::Gtk.Menu)this.wrapped_object;
252                 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => { 
253                         p.set_screen(Gdk.Screen.get_default());
254                         p.show_all();
255                         p.popup(null, null, null, ev.button, ev.time);
256                         return true;
257                 });
258         }
259
260         public void packTreeStore()
261         {
262                 var parent = this.parentObj.wrapped_object;
263                 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
264                         print("skip treestore pack - parent is not a treeview");
265                         return;
266                 }
267                 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
268                 
269         }
270         public void packTreeViewColumn()
271         {
272                 var parent = this.parentObj.wrapped_object;
273                 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
274                         print("skip packGtkViewColumn pack - parent is not a treeview");
275                         return;
276                 }
277                 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
278                 // init contains the add_attribute for what to render...
279                 
280         }       
281
282
283         public void packCellRenderer()
284         {
285                 var parent = this.parentObj.wrapped_object;
286                 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
287                         print("skip packGtkViewColumn pack - parent is not a treeview");
288                         return;
289                 }
290                 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
291                 // init contains the add_attribute for what to render...
292                 
293         }       
294
295
296         public void packContainerParams()
297         {
298          
299                 if (this.parentObj == null) {
300                         return;
301                 }
302                 // child must be a widget..
303                 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
304                         return;
305                 }
306                 
307                 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
308
309                 var parent = this.parentObj.wrapped_object;
310                 
311                 if (parent_gir == null) {
312                         return;
313                 }
314                 
315                 // let's test just setting expand to false...
316                 var cls_methods = parent_gir.methods;
317                 if (cls_methods == null) {
318                         return;
319                 }
320         
321                 if (!this.node.props.has_key("* pack")) {
322                         return;
323                 }
324                 
325                 var ns = this.parentObj.node.fqn().split(".")[0];
326                  
327                 var pack = this.node.props.get("* pack").split(",");
328
329         
330                 if (cls_methods.has_key(pack[0])) {
331                         var mparams = cls_methods.get(pack[0]).paramset.params;
332                         for (var i = 1; i < mparams.size; i++ ) {
333                                 if (i > (pack.length -1)) {
334                                         continue;
335                                 }
336                         
337                                 var k = mparams.get(i).name;
338
339                                 Value cur_val;
340                                  
341                                 var type = mparams.get(i).type;
342                                 type = Palete.Gir.fqtypeLookup(type, ns);
343
344                                 var val = this.toValue(pack[i].strip(), type);
345                                 if (val == null) {
346                                         print("skip (failed to transform value %s type = %s from %s\n", 
347                                                 this.parentObj.node.fqn()  + "." + k, type, pack[i].strip());
348                                         continue;
349                                 }
350                                 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
351         
352                                 ((global::Gtk.Container)parent).child_set_property(
353                                         (global::Gtk.Widget)this.wrapped_object , k, val);
354                                  
355                         }
356                 
357                 }
358         
359
360
361                         
362         }
363                    
364
365         public GLib.Value? toValue(string val, string type) {
366
367                 var gtkbuilder = new global::Gtk.Builder();
368
369                 if (type == "utf8") {
370                         var qret = GLib.Value(typeof(string));
371                         qret.set_string(val);
372                         return qret;
373                 }
374                 
375                 var prop_gtype = gtkbuilder.get_type_from_name(type);
376                 
377
378                 if (prop_gtype == GLib.Type.INVALID) {
379                          
380                         return null;
381                 }
382                 
383                 
384                 var ret = GLib.Value(prop_gtype);
385
386
387                 switch(type) {
388                         case "gboolean":
389                                 ret.set_boolean(val.down() == "false" ? false : true);
390                                 return ret;
391                         case "guint":
392                                 ret.set_uint(int.parse(val));
393                                 return ret;
394                                 
395                         case "gint":
396                                 ret.set_int(int.parse(val));
397                                 return ret;
398
399                         case "gfloat":
400                                 ret.set_float(long.parse(val));
401                                 return ret;
402                                 
403                         case "utf8":
404                                 ret.set_string(val);
405                                 return ret;
406
407                         default:
408
409                                 var sval =  GLib.Value(typeof(string));
410                                 sval.set_string(val);
411                         
412                                 if (!sval.transform(ref ret)) {
413                                 
414                                         return null;
415                                 }
416                                 return ret;
417                 }
418         }
419         
420          
421           
422                 
423 }