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