tools/build_gtk_tree.js
[app.Builder.js] / tools / build_gtk_tree.js
1 //<script type="text/javascript">
2
3
4 /**
5  * usage:
6  * 
7  * this.data = new BuildLists();
8  * 
9  * 
10  * 
11  * 
12  * 
13  */
14 // see if we can build the insertion tree for gtk - using introspection
15
16 // it should build the tree of feasible insertions, then we will have to manually prune it..
17
18 // it needs to know
19 // a) what the inherited types are
20 // b) what methods are available for each type, that include a reference to another type..
21
22 // let's start with types.. 
23 GIRepository = imports.gi.GIRepository;
24 GLib        = imports.gi.GLib;
25
26 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
27 //GIRepository.Repository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.1');
28
29
30 imports.searchPath.push('../../gnome.introspection-doc-generator');
31
32 XObject     = imports.XObject.XObject;
33 File        = imports.File.File; 
34  
35 // Introspecion specific..
36 NameSpace   = imports.Introspect.NameSpace.NameSpace; 
37 Link        = imports.Introspect.Link.Link; 
38
39
40 Array.prototype.pushUnique = function(v) {
41     if (this.indexOf(v) < 0) {
42         this.push(v);
43     }
44 }
45
46 function BuildLists () {
47  
48     
49  
50
51     var ns_list = [ 'Gtk' , 'Gdk', 'Pango', 'GtkSource', 'WebKit', 'Vte', 'GtkClutter'] ; //NameSpace.namespaces();
52      
53     ns_list = ns_list.sort();
54     // let's try and load them, so we find out early what will fail.
55     print("loading library to make sure it works.");
56
57     var classes = {};
58
59     ns_list.forEach(function(ns_name) {   
60         var  core = imports.gi[ns_name];
61         var ns = NameSpace.ns(ns_name); // fetch all the elements in namespace...
62         ns['objects'].forEach( function(n) {
63             var odata = NameSpace.factory('Class', ns_name, n);
64             classes[odata.alias] = odata;
65             
66         });
67         ns['interfaces'].forEach( function(n) {
68              var odata =NameSpace.factory('Interface', ns_name, n);
69             classes[odata.alias] = odata;
70         });
71     });
72     //print(JSON.stringify(classes['Gtk.CellRenderer'] , null, 4));
73     //print(JSON.stringify(classes['Gtk.CellRenderer'].titleType, null, 4));
74     //print(JSON.stringify(classes['Gtk.CellRenderer'].childClasses, null, 4));
75     //print(JSON.stringify(classes['Gtk.CellRenderer'].implementedBy, null, 4));
76           
77     
78
79     print("Looping throught namespaces");
80     var ns_idx = [];
81     var implementations = {};
82     
83     var methods = {};
84     var allmethods = [];  
85     
86     for (cls in classes) {
87         var odata = classes[cls];
88     
89         methods[cls] = {
90             //titleType : odata.titleType,
91             extendsClasses : [],
92             can_contain : [],
93             can_contain_using: [],
94           //  can_be_added_to : [],
95             //using_methods : { },
96             can_be_added_to_as : {}
97         };
98         odata.extendsClasses.forEach(function(child) {
99             methods[cls].extendsClasses.push(child.alias);
100         });
101         
102         implementations[odata.alias] = odata.titleType == 'Class' ? odata.childClasses :  odata.implementedBy;
103         
104     }
105     for (cls in classes) {
106         var odata = classes[cls];
107         
108        
109         
110         //print(cls);
111         //print(JSON.stringify(odata.methods,null,4));
112         odata.methods.forEach(function(m) {
113             
114             
115             
116             
117              if (!m.name.match(/^(add|pack)/)) {
118                 return;
119             }
120             //print(JSON.stringify(m,null,4));
121             m.params.forEach(function(p) {
122                  
123                 if (!p.type || typeof(classes[p.type]) == 'undefined') {
124                     return;
125                 }
126                 // now add it..
127                 //print(JSON.stringify(p));Seed.exit();
128                 var addable_type = p.type;
129                 if (addable_type.indexOf('.') < 0) {
130                     addable_type = p.memberOf + '.' + p.type;
131                 }
132                 
133                 if (m.memberOf == 'Gtk.Buildable') {
134                     return;
135                 }
136                                 //"Gtk.Widget:add_accelerator",
137                                 //"Gtk.Widget:add_device_events"
138
139                 if (m.name.match(/^(add_mnemonic_label|add_accelerator|add_device_events)$/)) {
140                     return;
141                 }
142         
143                 // in theory you can not add a window to anything.. ???
144                 //if ('Gtk.Window' == addable_type || methods[addable_type].extendsClasses.indexOf('Gtk.Window') > -1) {
145                 //    return;
146                 //}
147         // 
148         
149                 //print(full_method_name );
150                 
151                 //if (allmethods.indexOf(full_method_name) < 0) {
152                 //    allmethods.push(full_method_name);
153                 //}
154                 
155                 methods[cls].can_contain.pushUnique(addable_type);
156                 var add = m.memberOf +':'+ m.name;
157  
158                 methods[cls].can_contain_using.pushUnique(add);
159                 //methods[cls].using_methods[m.name] = m.params;
160                 
161                 //if (methods[addable_type].can_be_added_to.indexOf(cls) < 0) { 
162                 //    methods[addable_type].can_be_added_to.push(cls);
163                 //}
164                 
165                 
166                 
167                 if (typeof(methods[addable_type].can_be_added_to_as[cls]) == 'undefined') {
168                     methods[addable_type].can_be_added_to_as[cls]=[];
169                 }
170                 methods[addable_type].can_be_added_to_as[cls].pushUnique( add );
171                 implementations[cls].forEach(function(imp) {
172                     
173                     
174                     if (typeof(methods[addable_type ].can_be_added_to_as[imp]) == 'undefined') {
175                         methods[addable_type].can_be_added_to_as[imp] = [];
176                     }
177                     
178                     methods[addable_type].can_be_added_to_as[imp].pushUnique(add);
179                      
180                     
181                 });
182                 // also needs to add
183                 //print(addable_type);
184                 //print(JSON.stringify(implementations[addable_type], null,4));
185                  
186                 
187                 implementations[addable_type].forEach(function(addable_child) {
188                     
189                     //if (addable_child == 'Gtk.AboutDialog') {
190                     //    print(JSON.stringify(methods[addable_child].extendsClasses,null,4));Seed.exit();
191                         
192                     //}
193                     
194                     if (addable_child == 'Gtk.Window' ||
195                             methods[addable_child].extendsClasses.indexOf('Gtk.Window') > -1) {
196                         return;
197                     }
198                     
199                     if (typeof(methods[addable_child].can_be_added_to_as[cls]) == 'undefined') {
200                         methods[addable_child].can_be_added_to_as[cls]=[];
201                     }
202                     methods[addable_child].can_be_added_to_as[cls].pushUnique( add );
203                     implementations[cls].forEach(function(imp) {
204                         if (typeof(methods[addable_child ].can_be_added_to_as[imp]) == 'undefined') {
205                             methods[addable_child].can_be_added_to_as[imp] = [];
206                         }
207                         
208                         methods[addable_child].can_be_added_to_as[imp].pushUnique(add);
209                          
210                         
211                     });
212                 
213                   
214                     
215                 });
216                 
217                  
218                 
219                 
220                 
221                 
222                 
223                 
224                 
225                 return;
226             /*
227                     methods[cls].using_methods[m.name] = {};
228                 }
229                 
230                 
231                 if (typeof(methods[cls][full_method_name]) == 'undefined') {
232                     methods[cls][full_method_name] = [];
233                 }
234                 if (methods[cls][full_method_name].indexOf(m.name) > -1) {
235                     return;
236                 }
237                 methods[cls][full_method_name].push(m.name);
238             */  
239             });
240             
241         });
242         //for(method in odata.methods) {
243         //    print(method.name);
244         //}
245         
246         
247     }
248     /*
249     // fill in the added to list..
250     for(var p in methods ) {
251         var odata = methods[p];
252         
253         methods[p].can_be_added_to.forEach(function(c) {
254             methods[p].can_be_added_to_as[c]=c;
255             implementations[c].forEach(function(imp) {
256                 methods[p].can_be_added_to_as[imp]=c;
257             });
258         });
259         
260         
261     }
262     */
263     // now do the reverese 'can be added to'
264     
265     
266     
267     this.methods = methods;
268     this.allmethods = methods;
269     this.implementations = implementations;
270     //print(JSON.stringify(methods,null,4)); Seed.exit();
271     // dump out a usage file..
272     
273     function verifyUsageMethod(parent,child,method)
274     {
275         // currently only verifies add on container.
276         if (method !='Gtk.Container:add') {
277             return true;
278         }
279         var ar =  parent.split('.')
280         var cls =ar[1];
281         var ns =ar[0];
282          
283         /*
284         if (parent == 'Gtk.Bin' || methods['Gtk.Bin'].extendsClasses.indexOf(parent) > -1) {
285             return false;
286         }
287         if (['GtkSource.CompletionInfo',
288              'Gtk.MenuShell',
289              'GtkSource.View', // ??? nothing can be added to it?
290              'WebKit.WebView', // ??? nothing can be added to it?
291              'GtkClutter.Embed'
292              ].indexOf(parent) > -1) {
293              return false;
294         }
295         */
296         
297         
298         try {
299             var x = new imports.gi[ns][cls]();
300         } catch(e) {
301             print("TRY ctor:  " + parent );
302             print(e.toString());
303             return false;
304         }
305         
306         //print("TRY child type:  " + parent);
307         var ct = x.child_type();
308         print(parent + " : says children are of type : " + ct);
309         // get the Gtype for the child?
310         var GObject = imports.gi.GObject;
311         var match = GObject.type_from_name(ar.join(''));
312         //print([match, ct]);
313         //print ("matching?" + (GObject.type_is_a(match,  ct ) ? "YES" : "NO"));
314         
315         
316         return GObject.type_is_a(match,  ct )  ? true : false;
317         
318     }
319     
320     function is_a(cls, instance_of) {
321         return methods[cls].extendsClasses.indexOf(instance_of) > -1; 
322     }
323     
324     function verifyUsage(parent,child)
325     {
326         // find all the methods that child can be added to parent.
327         var mts = methods[parent].can_contain_using;
328         for(var i =0;i<mts.length;i++) {
329             var m = mts[i].split(':');
330             if (!is_a(child,m[0])) {
331                 continue;
332                 
333             }
334             if (verifyUsageMethod(parent,child,mts[i])) {
335                 return true;
336             }
337         }
338         return false;
339         
340         
341     }
342     
343     
344     
345     
346     
347     
348     // basically anything that is a Gtk.Container, should be available at the top.
349     /*
350 left:
351  *top
352 right:
353         Gtk.Window 
354         Gtk.Dialog
355         Gtk.MessageDialog
356         Gtk.AboutDialog
357         Gtk.Dialog
358         Gtk.FontSelectionDialog 
359         Gtk.InputDialog 
360         Gtk.ColorSelectionDialog 
361         Gtk.FileChooserDialog
362         Gtk.Menu
363         GtkClutter.Window
364         Gtk.OffScreenWindow
365     */
366 // these should really be based on heirachy..
367     usage = {};
368     tops = {}
369     usage['*top'] = implementations['Gtk.Container'];
370     usage['*top'].forEach(function(ch) {
371         tops[ch] = [ '*top' ];
372     });
373     for(var cls in methods) {
374         if (cls =='Gtk.Builder') {
375             continue;
376         }
377         for (var par in methods[cls].can_be_added_to_as) {
378             
379             if (!verifyUsage(par,cls)) {
380                 continue;
381             }
382             
383             if (typeof(usage[par]) == 'undefined') {
384                 usage[par] = [];
385             }
386             
387            
388             
389             usage[par].pushUnique(cls);
390             if (typeof(tops[cls]) == 'undefined') {
391                 tops[cls] = [];
392             }
393             tops[cls].pushUnique(par);
394         }
395     }
396     
397     
398     function canTop(cls, rec) {
399         
400         rec = rec || 0;
401         //print('CANTOP: ' + cls + ' =' + rec);
402         if (rec > 5) {
403        //     print('SKIP : ' + cls);
404         }
405         if (typeof(tops[cls]) == 'undefined') {
406             return false;
407         }
408         for (var i =0; i < tops[cls].length; i++) {
409             if (tops[cls][i] == '*top') {
410                 return true;
411             }
412             if (cls == tops[cls][i]) {
413                 continue;
414             }
415             if (canTop(tops[cls][i], rec+1)) {
416                 return true;
417             }
418         }
419         return false;
420     }
421     var lefts = {};
422     for(var par in usage) {
423         var left = usange[par].join(',');
424         if (typeof(lefts[left]) == 'undefined') {
425             lefts[left] = [];
426         }
427         lefts.push(par);
428     }
429     print (JSON.stringify(lefts,null,4));
430         
431         
432         
433         
434     print (JSON.stringify(usage,null,4));
435     var nusage = {};
436         var usage_left = {};
437     for(var par in usage) {
438         // see if the parent can be added to something.
439         if (!canTop(par)) {
440             continue;
441         }
442                 var duped = false;
443                 for(var dupe in usage) {
444                         if (par != dupe
445                                 && typeof(nusage[dupe]) != 'undefined'
446                                 && usage[par].join(',') == usage[dupe].join(',')) {
447                                 duped = true;
448                                 
449                                 if (typeof(usage_left[dupe]) == 'undefined') {
450                                         usage_left[dupe] = []; 
451                                 }
452                                 //print(par+ ' is a dupe of ' + dupe);
453                                 usage_left[dupe].pushUnique(par);
454                                 break;
455                         }
456                 }
457                  if (duped) {
458                         continue;
459                  }
460         nusage[par] = usage[par];
461         
462     }
463     usage = nusage;
464     //print(JSON.stringify(nusage,null,4));  Seed.exit();
465     print(JSON.stringify(methods['Gtk.TextView'],null,4));
466
467     var str = [];
468         for(var par in usage) {
469                 str.push('left');
470                 str.push('   ' + par);
471                 if (typeof(usage_left[par]) != 'undefined') {
472                         usage_left[par].forEach(function(d) { str.push('    ' + d);});
473                 }
474                 str.push('right');
475                 usage[par].forEach(function(d) { str.push('    ' + d);});
476                 str.push('');
477         }
478         print(str.join("\n"));
479     //print(JSON.stringify(implementations ,null,4));
480     /*
481       methods is
482         [a class]
483             [has  methods that use this object]
484                 [list of methods of the top class..]
485      
486      
487       So let's pick one..
488         TOP        ARRAY  2ND
489         Gtk.Button.add(Gtk.Widget) <<
490         
491         
492         What we need:
493         
494         A) what can this dragged element be dropped onto.
495         eg. list of parents.
496         - can_be_added_to_as (left)
497         
498         
499         
500         B) what method is available when this object is dropped onto this one.
501         
502         - get the right hand side?
503         
504         
505      
506      
507     */
508     
509     
510     //print(JSON.stringify(implementations,null,4));
511     
512 }
513 imports.gi.Gtk.init(Seed.argv);
514
515 imports.gi.GtkClutter.init(Seed.argv);
516 BuildLists();
517
518 // we now have a list of classes / methods that can be used..
519 // we now need a ui to flag stuff as "don't bother with"
520
521