b4e5671c135c10e7e15a9207a73006d9d816d9b3
[app.Builder.js] / Palete / Gtk.vala
1 namespace Palete {
2
3         
4         
5         
6         
7         public class Introspect.El : Object
8         {
9                 public enum eltype { 
10                             NS,
11                             CLASS,
12                             METHOD,
13                             PROP
14                 }
15                 
16             
17                 public eltype type;
18         }
19
20
21         public class Gtk : Palete {
22                 
23                 
24                 public Gtk()
25                 {
26
27
28                     
29                     base();
30                     this.name = "Gtk";
31                                  
32                                 //this.load();
33                     // various loader methods..
34                       //this.map = [];
35                     //this.load();
36                     //this.proplist = {};
37                     //this.comments = { }; 
38                     // no parent...
39                 }
40               
41                 public override void  load () {
42
43                         this.loadUsageFile(Builder4.Application.configDirectory() + "/resources/GtkUsage.txt");
44          
45                      
46                 }
47                 
48                 public string doc(string what) {
49                         var ns = what.split(".")[0];
50                         var gir =  Gir.factory(ns);
51                         return   gir.doc(what);
52                         
53                     //return typeof(this.comments[ns][what]) == 'undefined' ?  '' : this.comments[ns][what];
54                 }
55
56                         // does not handle implements...
57                 public override GirObject? getClass(string ename)
58                 {
59
60                         var es = ename.split(".");
61                         var gir = Gir.factory(es[0]);
62                 
63                         return gir.classes.get(es[1]);
64                 
65                 }
66
67                 public override Gee.HashMap<string,GirObject> getPropertiesFor(string ename, string type)
68                 {
69                         //print("Loading for " + ename);
70                     
71
72
73                                 // if (typeof(this.proplist[ename]) != 'undefined') {
74                         //print("using cache");
75                         //   return this.proplist[ename][type];
76                         //}
77                         // use introspection to get lists..
78          
79                         var es = ename.split(".");
80                         var gir = Gir.factory(es[0]);
81                 
82                         var cls = gir.classes.get(es[1]);
83                         if (cls == null) {
84                                 var ret = new Gee.HashMap<string,GirObject>();
85                                 return ret;
86                                 //throw new Error.INVALID_VALUE( "Could not find class: " + ename);
87                         
88                         }
89
90                         //cls.parseProps();
91                         //cls.parseSignals(); // ?? needed for add handler..
92                         //cls.parseMethods(); // ?? needed for ??..
93                         //cls.parseConstructors(); // ?? needed for ??..
94
95                         cls.overlayParent();
96
97                         switch  (type) {
98                                 case "props":
99                                         return cls.props;
100                                 case "signals":
101                                         return cls.signals;
102                                 case "methods":
103                                         return cls.methods;
104                                 case "ctors":
105                                         return cls.ctors;
106                                 default:
107                                         throw new Error.INVALID_VALUE( "getPropertiesFor called with: " + type);
108                                         //var ret = new Gee.HashMap<string,GirObject>();
109                                         //return ret;
110                                 
111                         }
112                                 
113                         
114                         //cls.overlayInterfaces(gir);
115                     
116                     
117                      
118                 }
119                 public string[] getInheritsFor(string ename)
120                 {
121                         string[] ret = {};
122                         var es = ename.split(".");
123                         var gir = Gir.factory(es[0]);
124                         
125                         var cls = gir.classes.get(es[1]);
126                         if (cls == null) {
127                                 return ret;
128                         }
129                         return cls.inheritsToStringArray();
130                         
131
132                 }
133         /*
134         genParams: function(sig, meth)
135         {
136             var args = ['self'];
137             var ret = "\n";
138             meth.ret_type = this.typeToName(GIRepository.callable_info_get_return_type(sig));
139             // might be a numbeR??
140             meth.params = [];
141             for(var a_i  =0; a_i   < GIRepository.callable_info_get_n_args(sig); a_i++) {
142                 var arg = GIRepository.callable_info_get_arg(sig, a_i);
143                  
144                 meth.params.push({
145                     name  : arg.get_name(),
146                     type : this.typeToName(arg.get_type(), true)
147                 });
148             }
149             
150                 
151             
152             
153         },
154         genSkel: function(sig) // should really use genParams...
155         {
156             var args = ['self'];
157             var ret = "\n";
158             var ret_type = this.typeToName(GIRepository.callable_info_get_return_type(sig));
159             // might be a numbeR??
160             if (ret_type == 'boolean') {
161                 ret = "    return false;\n";
162             }
163             for(var a_i  =0; a_i   < GIRepository.callable_info_get_n_args(sig); a_i++) {
164                 var arg = GIRepository.callable_info_get_arg(sig, a_i);
165                 
166                 args.push(arg.get_name());
167             }
168             return 'function (' + args.join(', ') + ") {\n" + ret + "}"; 
169                 
170             
171             
172         },
173         typeToName  : function (type_info, allow_iface) // find type for properties or arguments.
174         {
175            var ty = GIRepository.type_tag_to_string( GIRepository.type_info_get_tag(type_info));
176            
177             if ((ty == 'void') && GIRepository.type_info_is_pointer(type_info)) {
178                 return false;
179             }
180             if (ty == 'array') {
181                 return false; // unspported   
182             }
183             if (ty != 'interface') {
184                 return ty;
185             }
186             // we can accept enum types here..
187             var interface_info = GIRepository.type_info_get_interface(type_info);
188             var interface_type = interface_info.get_type();
189             
190             if (!allow_iface && interface_type != GIRepository.InfoType.ENUM) {
191                 return false;
192             }
193             return interface_info.get_namespace() + '.' + interface_info.get_name();
194             
195         },
196         /**
197          * merge two proprety arrays' ignoring what's overrriden.
198          * 
199          * /
200         
201         
202         overrides : function (top, bottom)
203         {
204             function inTop(b)
205             {
206                 return !top.every(function(t) {
207                     if (t.name == b.name) {
208                         return false;
209                     }
210                     return true;
211                 });
212             }
213             bottom.forEach(function(e) {
214                 if (!inTop(e)) {
215                     top.push(e);
216                 }
217             });
218             
219         },
220         
221         /**
222          * guess type..
223          * 
224          * /
225         findType : function (data, prop, value)
226         {
227             // specials??
228             if ((prop == '|xns') || (prop == 'xtype'))  {
229                 return 'string';
230             }
231             
232             var qname = this.guessName(data);
233             if (prop[0] == '|') {
234                 prop= prop.substring(1);
235             }
236             
237             var prs = this.getPropertiesFor(qname, 'props');
238             var ret = false;
239             prs.forEach(function(e) {
240                 if (ret !== false) {
241                     return; // got it.
242                 }
243                 if (e.name == prop) {
244                     ret = e;
245                 }
246                 
247             });
248             if (!ret) {
249                 return Base.prototype.findType(data, prop,value);
250             }
251              
252             // got the type
253             return ret.type;
254         },
255         
256         findOptions : function(ename)
257         {
258             
259             var es = ename.split('.');
260             if (es.length !=2) {
261                 return Base.prototype.findOptions(ename);
262             }
263             var gi = GIRepository.Repository.get_default();
264             var bi = gi.find_by_name(es[0], es[1]);
265             var etype = GIRepository.object_info_get_type(bi);
266             if (etype != GIRepository.InfoType.ENUM) {
267                 console.log("Options not handled yet!!!");
268                 return false;
269             }
270             var ret = [];
271             // got an enum.. let's return the values..
272             for(var i =0; i < bi.get_n_values(); i++) {
273                  
274                   var prop = bi.get_value(i);
275                    
276                 
277                   ret.push( ename + '.' + prop.get_name().toUpperCase() ) 
278             }
279             return ret;
280         },
281         /**
282          * determine the packing types..
283          * /
284         getDefaultPack: function(pname, cname) {
285             var list = this.getPackingList(pname,cname);
286            // console.dump(list);
287             
288             
289             if (!list.every(function(e) { return e.name != 'add'; })) {
290                 return 'add'; // add is in our list..?? what about args..?!?
291             }
292             function toRet(e) {
293                 var ret = [e.name];
294                 e.params.forEach(function(p,i) {
295                     if (ret === false) { return; } // skip broken..
296                     if (i==0) { return; } // skip first..
297                     if (p.type == 'boolean') {
298                         ret.push('false');
299                         return;
300                     }
301                     if (p.type == 'number') {
302                         ret.push('0');
303                         return;
304                     }
305                     if (p.type == 'uint') {
306                         ret.push('0');
307                         return;
308                     }
309                     ret = false; // invalid!
310                 });
311                 return ret === false ? false : ret.join(',');
312             };
313             var packret = false;
314             list.every(function(e) {
315                 
316                 packret = toRet(e);
317                 //print("DEFAULT PACK TEST : " + e.name + " : " +packret);
318                 if (packret !== false) {
319                     return false;
320                 }
321                 return true; // continue
322             });
323             //print("DEFAULT PACK: " + packret);
324             // should we do best match..?
325             return packret;
326         },
327         /**
328          * get packing list..
329          * /
330         getPackingList :function (pname, cname)
331         {
332             var funcs = this.getPropertiesFor(pname,'methods');
333             //print("getPackingList : ALL FUNCS");
334             //console.dump(funcs);
335             var ret = [];
336             var _this = this;
337             // let's assume top down...
338             var inherits = [ cname ];
339             inherits.push.apply(inherits, this.getPropertiesFor(cname,'inherits'));
340             funcs.forEach(function(m) {
341                 if (m.params.length && (typeof(m.params[0].type) == 'string') &&
342                     inherits.indexOf(m.params[0].type) > -1) {
343                     ret.push(m);
344                 }
345             });
346             return ret; 
347         }
348       */
349
350                 public override void fillPack(JsRender.Node node,JsRender.Node parent)
351                 {   
352                         
353                         string inherits = node.fqn() + " " + string.joinv(" ", this.getInheritsFor (node.fqn())) + " ";
354                         print ("Inherits
355                         // parent.fqn() method ( node.fqn()
356                         var methods = this.getPropertiesFor (parent.fqn(), "methods");
357                         
358                         
359                         var map = methods.map_iterator();
360                         while (map.next()) {
361                                 var n = map.get_key();
362                                 
363                                 
364                                 var meth = map.get_value();
365                                 if (meth.paramset == null || meth.paramset.params.size < 1) {
366                                         continue;
367                                 }
368                                 var fp = meth.paramset.params.get(0);
369                                 
370                                 var type = Gir.fqtypeLookup(fp.type, meth.ns);
371                                 if (!inherits.contains(" " + type + " ")) {
372                                         continue;
373                                 }
374                                 var pack = meth.name;
375                                 for(var i =1; i < meth.paramset.params.size; i++) {
376                                         var ty = Gir.fqtypeLookup(meth.paramset.params.get(i).type, meth.ns);
377                                         pack += "," + Gir.guessDefaultValueForType(ty);
378                                 }
379                                 node.set("pack", pack);
380                                 return;
381
382                         }
383
384                         
385                 }
386  
387         
388     }
389 }
390