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