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                          var ch = this.mungeChild(this.node.items.get(i));
148                          if (ch != null) {
149                                  this.children.add(ch);
150                          }
151                          
152                 }
153                 
154                 this.afterChildren();
155                 
156                 return this;
157                 
158
159                  
160
161         }
162         
163         public void  afterChildren()
164         {
165                 // things like GtkNotebook - we have to pack children after they have been created..
166                 var cls = this.node.fqn().replace(".", "");
167                 
168                 if (cls == "GtkNotebook") {
169                         this.afterChildrenGtkNotebook();
170                 }
171                 
172                 
173                 
174         }
175         
176         public void  afterChildrenGtkNotebook()
177         {
178                 
179                 
180         }
181         
182         
183         /**
184          * called after the this.object  has been created
185          * and it needs to be packed onto parent.
186          */
187         public void packParent() 
188         {
189                 var cls = this.node.fqn().replace(".", "");
190                 
191                 var gtkbuilder = new global::Gtk.Builder();
192                 var cls_gtype = gtkbuilder.get_type_from_name(cls);
193
194                 if (this.parentObj == null) {
195                         return;
196                 }
197                                 
198                     
199                 var parent = this.parentObj.wrapped_object;
200                 
201                 var do_pack =true;
202
203                 if (parent == null) { // no parent.. can not pack.
204                         return; /// 
205                 }
206                 // -------------  handle various special parents .. -----------
207                 
208                 var par_type = this.parentObj.node.fqn().replace(".", "");
209                 
210                 if (par_type == "GtkNotebook") {
211                         // do not pack - it's done afterwards...
212                         return;
213                 }
214                 
215                 // -------------  handle various child types.. -----------
216                 // our overrides
217                 if (cls == "GtkMenu") {
218                         this.packMenu();
219                         return;
220                 }
221
222                 if (cls == "GtkTreeStore") { // other stores?
223                         // tree store is buildable??? --- 
224                         this.packTreeStore();
225                         return;
226                 }
227                 if (cls =="GtkTreeViewColumn") { // other stores?
228                         //?? treeview column is actually buildable -- but we do not use the builder???
229                         this.packTreeViewColumn();
230                         return;
231                 }
232                 if (cls_gtype.is_a(typeof(global::Gtk.CellRenderer))) { // other stores?
233                         this.packCellRenderer();
234                         return;
235                 }
236
237
238                 
239                 // -- handle buildable add_child..
240                 if (    cls_gtype.is_a(typeof(global::Gtk.Buildable))
241                      && 
242                         parent.get_type().is_a(typeof(global::Gtk.Buildable))
243                 )
244                 {
245                         ((global::Gtk.Buildable)parent).add_child(gtkbuilder, 
246                                                   this.wrapped_object, null);
247                         return;
248                 }
249                 // other packing?
250
251                 
252
253         }
254
255         public void packMenu()
256         {
257
258
259                 var parent = this.parentObj.wrapped_object;
260                 if (!parent.get_type().is_a(typeof(global::Gtk.Widget))) {
261                         print("skip menu pack - parent is not a widget");
262                         return;
263                 }
264                 
265                 var p = (global::Gtk.Menu)this.wrapped_object;
266                 ((global::Gtk.Widget)parent).button_press_event.connect((s, ev) => { 
267                         p.set_screen(Gdk.Screen.get_default());
268                         p.show_all();
269                         p.popup(null, null, null, ev.button, ev.time);
270                         return true;
271                 });
272         }
273
274         public void packTreeStore()
275         {
276                 var parent = this.parentObj.wrapped_object;
277                 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
278                         print("skip treestore pack - parent is not a treeview");
279                         return;
280                 }
281                 ((global::Gtk.TreeView)parent).set_model((global::Gtk.TreeModel)this.wrapped_object);
282                 
283         }
284         public void packTreeViewColumn()
285         {
286                 var parent = this.parentObj.wrapped_object;
287                 if (!parent.get_type().is_a(typeof(global::Gtk.TreeView))) {
288                         print("skip packGtkViewColumn pack - parent is not a treeview");
289                         return;
290                 }
291                 ((global::Gtk.TreeView)parent).append_column((global::Gtk.TreeViewColumn)this.wrapped_object);
292                 // init contains the add_attribute for what to render...
293                 
294         }       
295
296
297         public void packCellRenderer()
298         {
299                 var parent = this.parentObj.wrapped_object;
300                 if (!parent.get_type().is_a(typeof(global::Gtk.TreeViewColumn))) {
301                         print("skip packGtkViewColumn pack - parent is not a treeview");
302                         return;
303                 }
304                 ((global::Gtk.TreeViewColumn)parent).pack_start((global::Gtk.CellRenderer)this.wrapped_object, false);
305                 // init contains the add_attribute for what to render...
306                 
307         }       
308
309
310         public void packContainerParams()
311         {
312          
313                 if (this.parentObj == null) {
314                         return;
315                 }
316                 // child must be a widget..
317                 if (!this.wrapped_object.get_type().is_a(typeof(global::Gtk.Widget))) {
318                         return;
319                 }
320                 
321                 var parent_gir = Palete.Gir.factoryFqn(this.parentObj.node.fqn());
322
323                 var parent = this.parentObj.wrapped_object;
324                 
325                 if (parent_gir == null) {
326                         return;
327                 }
328                 
329                 // let's test just setting expand to false...
330                 var cls_methods = parent_gir.methods;
331                 if (cls_methods == null) {
332                         return;
333                 }
334         
335                 if (!this.node.props.has_key("* pack")) {
336                         return;
337                 }
338                 
339                 var ns = this.parentObj.node.fqn().split(".")[0];
340                  
341                 var pack = this.node.props.get("* pack").split(",");
342
343         
344                 if (cls_methods.has_key(pack[0])) {
345                         var mparams = cls_methods.get(pack[0]).paramset.params;
346                         for (var i = 1; i < mparams.size; i++ ) {
347                                 if (i > (pack.length -1)) {
348                                         continue;
349                                 }
350                         
351                                 var k = mparams.get(i).name;
352
353                                 Value cur_val;
354                                  
355                                 var type = mparams.get(i).type;
356                                 type = Palete.Gir.fqtypeLookup(type, ns);
357
358                                 var val = this.toValue(pack[i].strip(), type);
359                                 if (val == null) {
360                                         print("skip (failed to transform value %s type = %s from %s\n", 
361                                                 this.parentObj.node.fqn()  + "." + k, type, pack[i].strip());
362                                         continue;
363                                 }
364                                 print ("pack:set_property ( %s , %s / %s)\n", k, pack[i].strip(), val.strdup_contents());
365         
366                                 ((global::Gtk.Container)parent).child_set_property(
367                                         (global::Gtk.Widget)this.wrapped_object , k, val);
368                                  
369                         }
370                 
371                 }
372         
373
374
375                         
376         }
377                    
378
379         public GLib.Value? toValue(string val, string type) {
380
381                 var gtkbuilder = new global::Gtk.Builder();
382
383                 if (type == "utf8") {
384                         var qret = GLib.Value(typeof(string));
385                         qret.set_string(val);
386                         return qret;
387                 }
388                 
389                 var prop_gtype = gtkbuilder.get_type_from_name(type);
390                 
391
392                 if (prop_gtype == GLib.Type.INVALID) {
393                          
394                         return null;
395                 }
396                 
397                 
398                 var ret = GLib.Value(prop_gtype);
399
400
401                 switch(type) {
402                         case "gboolean":
403                                 ret.set_boolean(val.down() == "false" ? false : true);
404                                 return ret;
405                         case "guint":
406                                 ret.set_uint(int.parse(val));
407                                 return ret;
408                                 
409                         case "gint":
410                                 ret.set_int(int.parse(val));
411                                 return ret;
412
413                         case "gfloat":
414                                 ret.set_float(long.parse(val));
415                                 return ret;
416                                 
417                         case "utf8":
418                                 ret.set_string(val);
419                                 return ret;
420
421                         default:
422
423                                 var sval =  GLib.Value(typeof(string));
424                                 sval.set_string(val);
425                         
426                                 if (!sval.transform(ref ret)) {
427                                 
428                                         return null;
429                                 }
430                                 return ret;
431                 }
432         }
433         
434          
435           
436                 
437 }