fix line numbering issues with vala generator - hopefully fixes completion in node...
[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                 public Meson meson;
50                 
51                 
52                 public Palete.Gtk gpalete {
53                         get {
54                                 return (Palete.Gtk) this.palete;
55                         }
56                         set {}
57                 }
58                  
59                 
60                 public Gtk(string path) {
61                   
62                   
63                         base(path);
64                         
65                         this.initChildCache();
66                    
67                         this.palete = new Palete.Gtk(this);
68                         
69                         this.gir_cache = new Gee.HashMap<string,Palete.GirObject>();
70                         this.xtype = "Gtk";
71                         //var gid = "project-gtk-%d".printf(gtk_id++);
72                         //this.id = gid;
73                         this.packages = new Gee.ArrayList<string>();
74                         //this.hidden = new Gee.ArrayList<string>();
75                         this.compilegroups = new  Gee.HashMap<string,GtkValaSettings>();
76                         this.meson = new Meson(this);
77                 
78                 }
79                 
80                 public  void initChildCache()
81                 {
82                         this.child_list_cache = new Gee.HashMap<string,Gee.ArrayList<string>>();
83                         this.child_list_cache_props = new Gee.HashMap<string,Gee.ArrayList<string>>();
84                         this.dropList = null;
85                 }
86                 
87                 
88                 
89                 public override void loadJson(Json.Object obj)  
90                 {
91                         // load a builder.config JSON file.
92                         // 
93                         this.compilegroups = new  Gee.HashMap<string,GtkValaSettings>();
94                         
95                         if (obj.has_member("packages")) {
96                                 this.packages = this.readArray(obj.get_array_member("packages"));
97                         }
98                         if (obj.has_member("compiler_flags")) {
99                                 this.compile_flags = obj.get_string_member("compile_flags");
100                         }
101                         
102                          if (!obj.has_member("compilegroups") || obj.get_member("compilegroups").get_node_type () != Json.NodeType.ARRAY) {
103                                 // make _default_ ?
104                                  return;
105                          }
106                         
107                         //this.hidden = this.readArray(obj.get_array_member("hidden"));
108                         var ar = obj.get_array_member("compilegroups");
109                         for(var i= 0;i<ar.get_length();i++) {
110                                 var el = ar.get_object_element(i);
111                                 var vs = new GtkValaSettings.from_json(this,el);
112                 if (vs == null) {
113                     print("problem loading json file");
114                     continue;
115                 }
116                                  
117                                 this.compilegroups.set(vs.name,vs);
118                         }
119                                                 
120                          
121                         //GLib.debug("%s\n",this.configToString ());
122                         
123                 }
124                 public override void saveJson(Json.Object obj)
125                 {
126                         var ar = new Json.Array();
127                         foreach(var cg in this.compilegroups.values) {
128                                  ar.add_object_element(cg.toJson());
129                         }
130                         obj.set_array_member("compilegroups", ar);
131                         
132                         obj.set_string_member("compile_flags", this.compile_flags);
133                         var par = new Json.Array();
134                         foreach(var p in this.packages) {
135                                 par.add_string_element(p);
136                         }
137                         obj.set_array_member("packages", par);
138                         //var hi = new Json.Array();
139                         //foreach(var p in this.hidden) {
140                         //      hi.add_string_element(p);
141                         //}
142                         //obj.set_array_member("hidden", hi);
143                         
144                         this.gir_cache_loaded = false; // force reload of the cache if we change the packages.
145                         this.gpalete.loaded = false;
146                         this.initChildCache();
147                         this.gir_cache = null;
148                 }
149                 
150                 public override void onSave()
151                 {
152                         this.meson.save();
153                 }
154          
155                 /**
156                  *  perhaps we should select the default in the window somewhere...
157                  */ 
158                 public string firstBuildModule()
159                 {
160                         
161                         foreach(var cg in this.compilegroups.values) {
162                                 return cg.name;
163                                 
164                         }
165                         return "";
166                          
167                 }
168                 public string firstBuildModuleWith(JsRender.JsRender file)
169                 {
170                         foreach(var cg in this.compilegroups.values) {
171                                  if (cg.has_file(file)) {
172                                         return cg.name;
173                                  }
174                                  
175                                  
176                         }
177                         return this.firstBuildModule();
178                          
179                 }
180                 
181                 public void loadVapiIntoStore(GLib.ListStore ls) 
182                 {
183                         ls.remove_all();
184     
185                          
186                         var pal = (Palete.Gtk) this.palete;
187                         var pkgs = pal.packages(this);
188                         foreach (var p in pkgs) {
189                                 ls.append(new VapiSelection(this.packages, p));
190                         }
191                         
192                 }
193                 
194                 public void loadTargetsIntoStore(GLib.ListStore ls) 
195                 {
196                         ls.remove_all();
197                         foreach(var cg in this.compilegroups.values) {
198                                 ls.append(cg);
199                         }
200                 }
201                 
202                  
203                 
204                  
205                 
206                 public string[] vapidirs()
207                 {
208                         return this.pathsMatching("vapi", false);
209                 }
210                 
211                 
212                  
213                 public override Palete.LanguageClient getLanguageServer(string lang)
214                 {
215                         if (this.language_servers.has_key(lang)) {
216                                 return this.language_servers.get(lang);
217                         }
218                         switch( lang ) {
219                                 case "vala":
220                                         this.language_servers.set(lang, new Palete.LanguageClientVala(this));
221                                         break;
222                                 default :
223                                          return this.language_servers.get("dummy");
224                                          
225                                 }
226                                 return this.language_servers.get(lang);
227                 
228                 }
229                  
230                  
231                  
232                  
233                  
234                  
235                  // ------------------  new project stufff
236                 public override void initialize()
237                 {
238                         string[] dirs = {
239                                 "src",
240                                 "src/ui"
241                                 // ?? docs ?
242                                 //   
243                         };
244                         
245                         
246                         string[] vapis = {
247                                 "gtk4",
248                                 "gee-0.8",
249                                 "gio-2.0",
250                                  
251                                 "glib-2.0",
252                                 "gobject-2.0",
253                                  
254                                 // "json-glib-1.0",
255                                  
256                                 //"libadwaita-1",
257                                 //"libxml-2.0",
258                                 "posix"
259                                  
260  
261                         };
262                         for(var i = 0;  i < dirs.length; i++) {
263                                 this.makeProjectSubdir( dirs[i]);
264                         }
265                         for(var i = 0;  i < vapis.length; i++) {
266                                 this.packages.add(vapis[i]);
267                         
268                         }
269                         // create/// some dummy files?
270                         // application
271                         
272                         this.makeMain();
273                         this.makeApplication();
274                         this.makeWindow();
275                         
276
277                         var cg =  new GtkValaSettings(this, this.name);
278                         this.compilegroups.set(this.name, cg);
279                         cg.sources.add("src/Main.vala");
280                         cg.sources.add("src/%sApplication.vala".printf(this.name));
281                         cg.sources.add("src/ui/ui.Window.bjs");
282                         // rescan... not needed as it get's selected after initialization.
283                         this.load();
284                         var fn = this.getByPath(this.path + "/src/ui/ui.Window.bjs");
285                         try {
286                                 fn.loadItems();
287                         } catch (GLib.Error e) { } // do nothing?
288                         
289                         fn.save();
290                         
291                         
292                         
293                 }
294                 
295                 
296                 void makeTemplatedFile(string name, string[] str, string replace) 
297                 {
298                         var o = "";
299                         for(var i=0;i< str.length;i++) {
300                                 o += str[i].replace("%s", replace) + "\n";
301                         }
302                         this.writeFile(name, o);
303                 }
304                 public void writeFile(string name, string o) 
305                 {
306                         var f = GLib.File.new_for_path(this.path + "/" + name);
307                         try {
308                                 var data_out = new GLib.DataOutputStream( f.replace(null, false, GLib.FileCreateFlags.NONE, null) );
309                                 data_out.put_string(o, null);
310                                 data_out.close(null);
311                         } catch (GLib.Error e) {
312                                 GLib.debug("Error writing file %s", e.message);
313                         }
314                         
315                 } 
316                 
317                 void makeMain()
318                 {
319                         string[] str = {
320                                 "int main (string[] args)",
321                                 "{",
322                                 "       var app = new  %sApplication(  args);",
323                                 "       Gtk.init ();",
324                                 "       GLib.Log.set_always_fatal(LogLevelFlags.LEVEL_ERROR ); ",
325                                 "       app.activate.connect(() => {",
326                                 "               var w = new ui.Window();",   // ?? main window as UI window?
327                                 "               w.el.application  = app;",
328                                 "               w.ref();",
329                                 "               w.show();",
330                                 "       });",
331                                 "       var ret = app.run(args);",
332                                 "       return ret; ",
333                                 "}"
334                         };
335                         this.makeTemplatedFile("src/Main.vala", str, this.name); // fixme name needs to be code friendly!
336                 }
337                 void makeApplication()
338                 {
339                         string[] str = {
340                         
341                         
342                                 "public class %sApplication : Gtk.Application",
343                                 "{",
344                                 "       public %sApplication (string[] args) ",
345                                 "       {",
346                                 "               Object(",
347                                 "                       application_id: \"org.roojs.%s\",",
348                                 "                       flags: ApplicationFlags.FLAGS_NONE",
349                                 "               );",
350                                 "       }",
351                                 "}"
352                         };
353                                 
354                         
355                         this.makeTemplatedFile("src/%sApplication.vala".printf(this.name), str, this.name); // fixme name needs to be code friendly!
356                 }
357
358                 
359                 void makeWindow()
360                 {
361                         this.writeFile("src/ui/ui.Window.bjs", """{
362  "build_module" : "",
363  "items" : [
364   {
365    "$ xns" : "Gtk",
366    "| void show" : "() { this.el.show(); }",
367    "items" : [
368     {
369      "$ xns" : "Gtk",
370      "* prop" : "child",
371      "Gtk.Orientation orientation" : "Gtk.Orientation.HORIZONTAL",
372      "int spacing" : 0,
373      "items" : [
374       {
375        "$ xns" : "Gtk",
376        "string label" : "Hello World",
377        "xtype" : "Label"
378       }
379      ],
380      "xtype" : "Box"
381     }
382    ],
383    "xtype" : "Window"
384   }
385  ],
386  "name" : "ui.Window",
387  "gen_extended" : false
388 }
389 """);
390         }
391         
392  
393                         
394                 
395                  public override void   initDatabase()
396                 {
397                      // nOOP
398                 }
399         }
400          
401    
402 }