09c4a42c41cdf17562b98c8edc91544958ee48a9
[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                          
123                         var cls = Gir.factoryFqn(ename);
124                          
125                         if (cls == null || cls.nodetype != "Class") {
126                                 print("getInheritsFor:could not find cls: %s\n", ename);
127                                 return ret;
128                         }
129                         
130                         return cls.inheritsToStringArray();
131                         
132
133                 }
134         /*
135         genParams: function(sig, meth)
136         {
137             var args = ['self'];
138             var ret = "\n";
139             meth.ret_type = this.typeToName(GIRepository.callable_info_get_return_type(sig));
140             // might be a numbeR??
141             meth.params = [];
142             for(var a_i  =0; a_i   < GIRepository.callable_info_get_n_args(sig); a_i++) {
143                 var arg = GIRepository.callable_info_get_arg(sig, a_i);
144                  
145                 meth.params.push({
146                     name  : arg.get_name(),
147                     type : this.typeToName(arg.get_type(), true)
148                 });
149             }
150             
151                 
152             
153             
154         },
155         genSkel: function(sig) // should really use genParams...
156         {
157             var args = ['self'];
158             var ret = "\n";
159             var ret_type = this.typeToName(GIRepository.callable_info_get_return_type(sig));
160             // might be a numbeR??
161             if (ret_type == 'boolean') {
162                 ret = "    return false;\n";
163             }
164             for(var a_i  =0; a_i   < GIRepository.callable_info_get_n_args(sig); a_i++) {
165                 var arg = GIRepository.callable_info_get_arg(sig, a_i);
166                 
167                 args.push(arg.get_name());
168             }
169             return 'function (' + args.join(', ') + ") {\n" + ret + "}"; 
170                 
171             
172             
173         },
174         typeToName  : function (type_info, allow_iface) // find type for properties or arguments.
175         {
176            var ty = GIRepository.type_tag_to_string( GIRepository.type_info_get_tag(type_info));
177            
178             if ((ty == 'void') && GIRepository.type_info_is_pointer(type_info)) {
179                 return false;
180             }
181             if (ty == 'array') {
182                 return false; // unspported   
183             }
184             if (ty != 'interface') {
185                 return ty;
186             }
187             // we can accept enum types here..
188             var interface_info = GIRepository.type_info_get_interface(type_info);
189             var interface_type = interface_info.get_type();
190             
191             if (!allow_iface && interface_type != GIRepository.InfoType.ENUM) {
192                 return false;
193             }
194             return interface_info.get_namespace() + '.' + interface_info.get_name();
195             
196         },
197         /**
198          * merge two proprety arrays' ignoring what's overrriden.
199          * 
200          * /
201         
202         
203         overrides : function (top, bottom)
204         {
205             function inTop(b)
206             {
207                 return !top.every(function(t) {
208                     if (t.name == b.name) {
209                         return false;
210                     }
211                     return true;
212                 });
213             }
214             bottom.forEach(function(e) {
215                 if (!inTop(e)) {
216                     top.push(e);
217                 }
218             });
219             
220         },
221         
222         /**
223          * guess type..
224          * 
225          * /
226         findType : function (data, prop, value)
227         {
228             // specials??
229             if ((prop == '|xns') || (prop == 'xtype'))  {
230                 return 'string';
231             }
232             
233             var qname = this.guessName(data);
234             if (prop[0] == '|') {
235                 prop= prop.substring(1);
236             }
237             
238             var prs = this.getPropertiesFor(qname, 'props');
239             var ret = false;
240             prs.forEach(function(e) {
241                 if (ret !== false) {
242                     return; // got it.
243                 }
244                 if (e.name == prop) {
245                     ret = e;
246                 }
247                 
248             });
249             if (!ret) {
250                 return Base.prototype.findType(data, prop,value);
251             }
252              
253             // got the type
254             return ret.type;
255         },
256         
257         findOptions : function(ename)
258         {
259             
260             var es = ename.split('.');
261             if (es.length !=2) {
262                 return Base.prototype.findOptions(ename);
263             }
264             var gi = GIRepository.Repository.get_default();
265             var bi = gi.find_by_name(es[0], es[1]);
266             var etype = GIRepository.object_info_get_type(bi);
267             if (etype != GIRepository.InfoType.ENUM) {
268                 console.log("Options not handled yet!!!");
269                 return false;
270             }
271             var ret = [];
272             // got an enum.. let's return the values..
273             for(var i =0; i < bi.get_n_values(); i++) {
274                  
275                   var prop = bi.get_value(i);
276                    
277                 
278                   ret.push( ename + '.' + prop.get_name().toUpperCase() ) 
279             }
280             return ret;
281         },
282         /**
283          * determine the packing types..
284          * /
285         getDefaultPack: function(pname, cname) {
286             var list = this.getPackingList(pname,cname);
287            // console.dump(list);
288             
289             
290             if (!list.every(function(e) { return e.name != 'add'; })) {
291                 return 'add'; // add is in our list..?? what about args..?!?
292             }
293             function toRet(e) {
294                 var ret = [e.name];
295                 e.params.forEach(function(p,i) {
296                     if (ret === false) { return; } // skip broken..
297                     if (i==0) { return; } // skip first..
298                     if (p.type == 'boolean') {
299                         ret.push('false');
300                         return;
301                     }
302                     if (p.type == 'number') {
303                         ret.push('0');
304                         return;
305                     }
306                     if (p.type == 'uint') {
307                         ret.push('0');
308                         return;
309                     }
310                     ret = false; // invalid!
311                 });
312                 return ret === false ? false : ret.join(',');
313             };
314             var packret = false;
315             list.every(function(e) {
316                 
317                 packret = toRet(e);
318                 //print("DEFAULT PACK TEST : " + e.name + " : " +packret);
319                 if (packret !== false) {
320                     return false;
321                 }
322                 return true; // continue
323             });
324             //print("DEFAULT PACK: " + packret);
325             // should we do best match..?
326             return packret;
327         },
328         /**
329          * get packing list..
330          * /
331         getPackingList :function (pname, cname)
332         {
333             var funcs = this.getPropertiesFor(pname,'methods');
334             //print("getPackingList : ALL FUNCS");
335             //console.dump(funcs);
336             var ret = [];
337             var _this = this;
338             // let's assume top down...
339             var inherits = [ cname ];
340             inherits.push.apply(inherits, this.getPropertiesFor(cname,'inherits'));
341             funcs.forEach(function(m) {
342                 if (m.params.length && (typeof(m.params[0].type) == 'string') &&
343                     inherits.indexOf(m.params[0].type) > -1) {
344                     ret.push(m);
345                 }
346             });
347             return ret; 
348         }
349       */
350
351                 public override void fillPack(JsRender.Node node,JsRender.Node parent)
352                 {   
353                         
354                         string inherits =  string.joinv(" ", 
355                                       this.getInheritsFor (node.fqn())) + " ";
356                         inherits += node.fqn() + " ";
357                         //print ("fillPack:Inherits : %s\n", inherits);
358                         // parent.fqn() method ( node.fqn()
359                         var methods = this.getPropertiesFor (parent.fqn(), "methods");
360                         
361                         var res = new Gee.HashMap<string,string>();
362                         var map = methods.map_iterator();
363                         while (map.next()) {
364                                 
365                                 var n = map.get_key();
366                                 //print ("fillPack:checking method %s\n", n);
367                                 
368                                 var meth = map.get_value();
369                                 if (meth.paramset == null || meth.paramset.params.size < 1) {
370                                         print ("fillPack:c -- no params\n");
371                                 
372                                         continue;
373                                 }
374                                 var fp = meth.paramset.params.get(0);
375                                 
376                                 var type = Gir.fqtypeLookup(fp.type, meth.ns);
377                                 print ("fillPack:first param type is %s\n", type);
378
379                                 
380                                 if (!inherits.contains(" " + type + " ")) {
381                                         continue;
382                                 }
383                                 
384                                 
385                                 var pack = meth.name;
386                                 for(var i =1; i < meth.paramset.params.size; i++) {
387                                         var ty = Gir.fqtypeLookup(meth.paramset.params.get(i).type, meth.ns);
388                                         pack += "," + Gir.guessDefaultValueForType(ty);
389                                 }
390
391                                 print ("fillPack:add pack:  --          %s\n",pack );
392
393                                 res.set(meth.name, pack);
394                                 
395                                 
396
397                         }
398                         if (res.size < 1) {
399                                 return ;
400                         }
401                         if (res.has_key("pack_start")) {
402                                 node.props.set("* pack", res.get("pack_start"));
403                                 return;
404                         }
405                         if (res.has_key("add")) {
406                                 node.props.set("* pack", res.get("add"));
407                                 return;
408                         }
409                         var riter = res.map_iterator();
410                         while(riter.next()) {
411                                 node.props.set("* pack", riter.get_value());
412                                 return;
413                         }
414                         
415                         
416                 }
417  
418         
419     }
420 }
421