src/JsRender/Gtk.vala
[app.Builder.js] / src / JsRender / Gtk.vala
1 /**
2  * 
3  *  this is the code represents a File when using the Gtk view..
4  *   
5  *  It ues NodeToGtk
6  * 
7  * 
8  */
9
10 namespace JsRender {
11
12
13  
14     int gid = 1;
15
16   
17     public  class Gtk : JsRender
18     {
19        
20
21         public Gtk(Project.Project project, string path) {
22         
23             base( project, path);
24             this.xtype = "Gtk";
25             this.language = "vala";
26             
27             
28             //this.items = false;
29             //if (cfg.json) {
30             //    var jstr =  JSON.parse(cfg.json);
31             //    this.items = [ jstr ];
32             //    //console.log(cfg.items.length);
33             //    delete cfg.json; // not needed!
34             // }
35              
36             
37             
38             // super?!?!
39             this.id = "file-gtk-%d".printf(gid++);
40             //console.dump(this);
41             // various loader methods..
42
43             // Class = list of arguments ... and which property to use as a value.
44        
45
46             
47             
48         }
49           
50
51         /*
52         setNSID : function(id)
53         {
54             
55             this.items[0]['*class'] = id;
56             
57             
58         },
59         getType: function() {
60             return 'Gtk';
61         },
62         */
63
64                 public   override void   removeFiles() {
65                         var js = GLib.Path.get_dirname(this.path) +"/" +  name + ".js";
66                         if (FileUtils.test(js, FileTest.EXISTS)) {
67                                 GLib.FileUtils.remove(js);
68                         }
69                         var vala = GLib.Path.get_dirname(this.path) +"/" + name + ".vala";
70                         if (FileUtils.test(vala, FileTest.EXISTS)) {
71                                 GLib.FileUtils.remove(vala);
72                         }
73                 }
74         
75                 public   override void  loadItems() throws GLib.Error // : function(cb, sync) == original was async.
76                 {
77                   
78                         print("load Items!");
79                         if (this.tree != null) {
80                                 return;
81                         }
82                         
83                         print("load: " + this.path);
84
85
86                         var pa = new Json.Parser();
87                         pa.load_from_file(this.path);
88                         var node = pa.get_root();
89                         
90                         if (node.get_node_type () != Json.NodeType.OBJECT) {
91                                 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
92                         }
93                         var obj = node.get_object ();
94                         
95                         this.name = obj.get_string_member("name");
96                         this.parent = obj.get_string_member("parent");
97                         this.title = obj.get_string_member("title");
98                         
99                         if (obj.has_member("build_module")) { // should check type really..
100                                 this.build_module = obj.get_string_member("build_module");
101                         }
102                          
103                         // load items[0] ??? into tree...
104                         var bjs_version_str = this.jsonHasOrEmpty(obj, "bjs-version");
105                         bjs_version_str = bjs_version_str == "" ? "1" : bjs_version_str;
106
107                         if (obj.has_member("items") 
108                                 && 
109                                 obj.get_member("items").get_node_type() == Json.NodeType.ARRAY
110                                 &&
111                                 obj.get_array_member("items").get_length() > 0
112                         ) {
113                                 var ar = obj.get_array_member("items");
114                                 var tree_base = ar.get_object_element(0);
115                                 this.tree = new Node();
116                                 this.tree.loadFromJson(tree_base, int.parse(bjs_version_str));
117
118                         }
119                                 
120                         
121                 }
122          
123         
124                 
125         public override string toSourcePreview()
126         {
127                         return "";
128                 }
129       
130         public override string toSource()
131         {
132         
133             
134             if (this.tree == null) {
135                 return "";
136             }
137             
138             // var data = JSON.parse(JSON.stringify(this.items[0]));
139             // we should base this on the objects in the tree really..
140             string[]  inc = { "Gtk", "Gdk", "Pango", "GLib", "Gio", "GObject", 
141                 "GtkSource", "WebKit", "Vte" }; //, "GtkClutter" , "Gdl"];
142             var src = "";
143                          
144             for (var i=0; i< inc.length; i++) {
145                                 var e = inc[i];
146                 src += e+" = imports.gi." + e +";\n";
147             }
148             
149             src += "console = imports.console;\n"; // path?!!?
150             src += "XObject = imports.XObject.XObject;\n"; // path?!!?
151             
152             
153             src += this.name + "=new XObject("+ this.mungeToString("    ") + ");\n";
154             src += this.name + ".init();\n";
155             // register it in the cache
156             src += "XObject.cache['/" + this.name + "'] = " + this.name + ";\n";
157             
158             
159             return src;
160             
161             
162         }
163                 
164         public override void save() {
165             this.saveBJS();
166             // this.saveJS(); - disabled at present.. project settings will probably enable this later..
167                 
168             this.saveVala();
169         }
170             // ignore these calls.
171         public override void saveHTML ( string html ) {}
172             
173                     
174         /** 
175          *  saveJS
176          * 
177          * save as a javascript file.
178          * why is this not save...???
179          * 
180          */ 
181           
182         void saveJS()
183         {
184              
185             var fn = GLib.Path.get_dirname(this.path) + "/" + this.name + ".js";
186             print("WRITE :%s\n " , fn);
187             this.writeFile(fn, this.toSource());
188             
189         }
190         
191        void  saveVala()
192         {
193                 if (this.tree == null) {
194                         return;
195                 }
196                 var fn = GLib.Path.get_dirname(this.path) + "/" + this.name + ".vala";
197                 print("WRITE :%s\n " , fn);
198                 this.writeFile(fn,  NodeToVala.mungeFile(this));
199             
200             
201         }
202                 /*
203         valaCompileCmd : function()
204         {
205             
206             var fn = '/tmp/' + this.name + '.vala';
207             print("WRITE : " + fn);
208             File.write(fn, this.toVala(true));
209             
210             
211             
212             return ["valac",
213                    "--pkg",  "gio-2.0",
214                    "--pkg" , "posix" ,
215                    "--pkg" , "gtk+-3.0",
216                    "--pkg",  "libnotify",
217                    "--pkg",  "gtksourceview-3.0",
218                    "--pkg", "libwnck-3.0",
219                    fn ,   "-o", "/tmp/" + this.name];
220             
221            
222              
223             
224         },
225         */
226         
227    
228         string getHelpUrl(string cls)
229         {
230             return "http://devel.akbkhome.com/seed/" + cls + ".html";
231         }
232         public override void  findTransStrings(Node? node )
233                 {
234                         // not yet..
235                 }
236         
237         
238           
239
240                 
241
242         }
243 }
244
245
246