f0d1dd22f22b75c83c357b32614886eebd08c19b
[roobuilder] / src / Project / Gtk.vala
1 //<Script type="text/javascript">
2 /**
3  * Gtk projects - normally vala based now..
4  * 
5  * should have a few extra features..
6  * 
7  * like:
8  *   compile flags etc..
9  *   different versions (eg. different files can compile different versions - eg. for testing.
10  *   
11  * If we model this like adjuta - then we would need a 'project' file that is actually in 
12  * the directory somewhere... - and is revision controlled etc..
13  * 
14  * builder.config ??
15  * 
16  * 
17  * 
18  * 
19  */
20  
21
22 namespace Project 
23 {
24  
25         
26
27         public class Gtk : Project
28         {
29                 /**
30                 * Gir cache - it's local as we might want clear it if we modify the packages...
31                 *
32                 */
33                 // public Gee.HashMap<string,Palete.GirObject> gir_cache = null; ?? 
34                 
35                 public bool gir_cache_loaded = false;  /// set this to false to force a relaod of vapi's?
36                 
37                 // these are loaded / created by the palete.. but are project specific.
38                 public Gee.HashMap<string,Gee.ArrayList<string>>? dropList = null;  
39             public Gee.HashMap<string,Gee.ArrayList<string>> child_list_cache;   // what child can on on what node
40                 public Gee.HashMap<string,Gee.ArrayList<string>> child_list_cache_props; // what child can go on what node (with properties included)
41                 
42              public string compile_flags = ""; // generic to all.       
43                 public Gee.ArrayList<string> packages; // list of vapi's that are used by this project. 
44                  
45                 //pblic Gee.ArrayList<string> hidden; // list of dirs to be hidden from display...
46                 
47                 public GtkValaSettings? active_cg = null;
48                 public Gee.HashMap<string,GtkValaSettings> compilegroups;
49                 
50                 
51                 public Palete.Gtk gpalete {
52                         get {
53                                 return (Palete.Gtk) this.palete;
54                         }
55                         set {}
56                 }
57                  
58                 
59                 public Gtk(string path) {
60                   
61                   
62                         base(path);
63                         
64                         this.initChildCache();
65                    
66                         this.palete = new Palete.Gtk(this);
67                         
68                         this.gir_cache = new Gee.HashMap<string,Palete.GirObject>();
69                         this.xtype = "Gtk";
70                         //var gid = "project-gtk-%d".printf(gtk_id++);
71                         //this.id = gid;
72                         this.packages = new Gee.ArrayList<string>();
73                         //this.hidden = new Gee.ArrayList<string>();
74                         this.compilegroups = new  Gee.HashMap<string,GtkValaSettings>();
75                 
76                 }
77                 
78                 public  void initChildCache()
79                 {
80                         this.child_list_cache = new Gee.HashMap<string,Gee.ArrayList<string>>();
81                         this.child_list_cache_props = new Gee.HashMap<string,Gee.ArrayList<string>>();
82                         this.dropList = null;
83                 }
84                 
85                 
86                 
87                 public override void loadJson(Json.Object obj)  
88                 {
89                         // load a builder.config JSON file.
90                         // 
91                         this.compilegroups = new  Gee.HashMap<string,GtkValaSettings>();
92                         
93                         if (obj.has_member("packages")) {
94                                 this.packages = this.readArray(obj.get_array_member("packages"));
95                         }
96                         if (obj.has_member("compiler_flags")) {
97                                 this.compile_flags = obj.get_string_member("compile_flags");
98                         }
99                         
100                          if (!obj.has_member("compilegroups") || obj.get_member("compilegroups").get_node_type () != Json.NodeType.ARRAY) {
101                                 // make _default_ ?
102                                  return;
103                          }
104                         
105                         //this.hidden = this.readArray(obj.get_array_member("hidden"));
106                         var ar = obj.get_array_member("compilegroups");
107                         for(var i= 0;i<ar.get_length();i++) {
108                                 var el = ar.get_object_element(i);
109                                 var vs = new GtkValaSettings.from_json(this,el);
110                 if (vs == null) {
111                     print("problem loading json file");
112                     continue;
113                 }
114                                  
115                                 this.compilegroups.set(vs.name,vs);
116                         }
117                                                 
118                          
119                         //GLib.debug("%s\n",this.configToString ());
120                         
121                 }
122                 public override void saveJson(Json.Object obj)
123                 {
124                         var ar = new Json.Array();
125                         foreach(var cg in this.compilegroups.values) {
126                                  ar.add_object_element(cg.toJson());
127                         }
128                         obj.set_array_member("compilegroups", ar);
129                         
130                         obj.set_string_member("compile_flags", this.compile_flags);
131                         var par = new Json.Array();
132                         foreach(var p in this.packages) {
133                                 par.add_string_element(p);
134                         }
135                         obj.set_array_member("packages", par);
136                         //var hi = new Json.Array();
137                         //foreach(var p in this.hidden) {
138                         //      hi.add_string_element(p);
139                         //}
140                         //obj.set_array_member("hidden", hi);
141                         
142                         this.gir_cache_loaded = false; // force reload of the cache if we change the packages.
143                         this.gpalete.loaded = false;
144                         this.initChildCache();
145                         this.gir_cache = null;
146                 }
147                 
148          
149                 /**
150                  *  perhaps we should select the default in the window somewhere...
151                  */ 
152                 public string firstBuildModule()
153                 {
154                         
155                         foreach(var cg in this.compilegroups.values) {
156                                 return cg.name;
157                                 
158                         }
159                         return "";
160                          
161                 }
162                 public string firstBuildModuleWith(JsRender.JsRender file)
163                 {
164                         foreach(var cg in this.compilegroups.values) {
165                                  if (cg.has_file(file)) {
166                                         return cg.name;
167                                  }
168                                  
169                                  
170                         }
171                         return this.firstBuildModule();
172                          
173                 }
174                 
175                 public void loadVapiIntoStore(GLib.ListStore ls) 
176                 {
177                         ls.remove_all();
178     
179                          
180                         var pal = (Palete.Gtk) this.palete;
181                         var pkgs = pal.packages(this);
182                         foreach (var p in pkgs) {
183                                 ls.append(new VapiSelection(this.packages, p));
184                         }
185                         
186                 }
187                 
188                 public void loadTargetsIntoStore(GLib.ListStore ls) 
189                 {
190                         ls.remove_all();
191                         foreach(var cg in this.compilegroups.values) {
192                                 ls.append(cg);
193                         }
194                 }
195                 
196                  
197                 
198                  
199                 
200                 public string[] vapidirs()
201                 {
202                         return this.pathsMatching("vapi", false);
203                 }
204                  
205                 public override void initialize()
206                 {
207                         string[] dirs = {
208                                 "src",
209                                 "src/ui"
210                                 // ?? docs ?
211                                 //   
212                         };
213                         
214                         
215                         string[] vapis = {
216                                 "gtk4",
217                                 "gee-0.8",
218                                 "gio-2.0",
219                                  
220                                 "glib-2.0",
221                                 "gobject-2.0",
222                                  
223                                 // "json-glib-1.0",
224                                  
225                                 //"libadwaita-1",
226                                 //"libxml-2.0",
227                                 "posix"
228                                  
229  
230                         };
231                         for(var i = 0;  i < dirs.length; i++) {
232                                 this.makeProjectSubdir( dirs[i]);
233                         }
234                         for(var i = 0;  i < vapis.length; i++) {
235                                 this.packages.add(vapis[i]);
236                         
237                         }
238                         // create/// some dummy files?
239                         // application
240                         
241                         this.makeMain();
242                         this.makeApplication();
243                         this.makeWindow();
244                         
245
246                         var cg =  new GtkValaSettings(this, this.name);
247                         this.compilegroups.set(this.name, cg);
248                         cg.sources.add("src/Main.vala");
249                         cg.sources.add("src/%sApplication.vala".printf(this.name));
250                         cg.sources.add("src/ui/ui.Window.bjs");
251                         // rescan... not needed as it get's selected after initialization.
252                         this.load();
253                         var fn = this.getByPath(this.path + "/src/ui/ui.Window.bjs");
254                         try {
255                                 fn.loadItems();
256                         } catch (GLib.Error e) { } // do nothing?
257                         
258                         fn.save();
259                         
260                         
261                         
262                 }
263                 
264                 
265                 void makeTemplatedFile(string name, string[] str, string replace) 
266                 {
267                         var o = "";
268                         for(var i=0;i< str.length;i++) {
269                                 o += str[i].replace("%s", replace) + "\n";
270                         }
271                         this.writeFile(name, o);
272                 }
273                 public void writeFile(string name, string o) 
274                 {
275                         var f = GLib.File.new_for_path(this.path + "/" + name);
276                         try {
277                                 var data_out = new GLib.DataOutputStream( f.replace(null, false, GLib.FileCreateFlags.NONE, null) );
278                                 data_out.put_string(o, null);
279                                 data_out.close(null);
280                         } catch (GLib.Error e) {
281                                 GLib.debug("Error writing file %s", e.message);
282                         }
283                         
284                 } 
285                 
286                 void makeMain()
287                 {
288                         string[] str = {
289                                 "int main (string[] args)",
290                                 "{",
291                                 "       var app = new  %sApplication(  args);",
292                                 "       Gtk.init ();",
293                                 "       GLib.Log.set_always_fatal(LogLevelFlags.LEVEL_ERROR ); ",
294                                 "       app.activate.connect(() => {",
295                                 "               var w = new ui.Window();",   // ?? main window as UI window?
296                                 "               w.el.application  = app;",
297                                 "               w.ref();",
298                                 "               w.show();",
299                                 "       });",
300                                 "       var ret = app.run(args);",
301                                 "       return ret; ",
302                                 "}"
303                         };
304                         this.makeTemplatedFile("src/Main.vala", str, this.name); // fixme name needs to be code friendly!
305                 }
306                 void makeApplication()
307                 {
308                         string[] str = {
309                         
310                         
311                                 "public class %sApplication : Gtk.Application",
312                                 "{",
313                                 "       public %sApplication (string[] args) ",
314                                 "       {",
315                                 "               Object(",
316                                 "                       application_id: \"org.roojs.%s\",",
317                                 "                       flags: ApplicationFlags.FLAGS_NONE",
318                                 "               );",
319                                 "       }",
320                                 "}"
321                         };
322                                 
323                         
324                         this.makeTemplatedFile("src/%sApplication.vala".printf(this.name), str, this.name); // fixme name needs to be code friendly!
325                 }
326
327                 
328                 void makeWindow()
329                 {
330                         this.writeFile("src/ui/ui.Window.bjs", """{
331  "build_module" : "",
332  "items" : [
333   {
334    "$ xns" : "Gtk",
335    "| void show" : "() { this.el.show(); }",
336    "items" : [
337     {
338      "$ xns" : "Gtk",
339      "* prop" : "child",
340      "Gtk.Orientation orientation" : "Gtk.Orientation.HORIZONTAL",
341      "int spacing" : 0,
342      "items" : [
343       {
344        "$ xns" : "Gtk",
345        "string label" : "Hello World",
346        "xtype" : "Label"
347       }
348      ],
349      "xtype" : "Box"
350     }
351    ],
352    "xtype" : "Window"
353   }
354  ],
355  "name" : "ui.Window",
356  "gen_extended" : false
357 }
358 """);
359         }
360         
361  
362                         
363                 
364  public override void   initDatabase()
365     {
366          // nOOP
367     }
368         }
369          
370    
371 }