src/Builder4/GtkView.bjs
[app.Builder.js] / src / Builder4 / GtkView.vala
1 static Xcls_GtkView  _GtkView;
2
3 public class Xcls_GtkView : Object
4 {
5     public Gtk.Box el;
6     private Xcls_GtkView  _this;
7
8     public static Xcls_GtkView singleton()
9     {
10         if (_GtkView == null) {
11             _GtkView= new Xcls_GtkView();
12         }
13         return _GtkView;
14     }
15     public Xcls_label_preview label_preview;
16     public Xcls_label_code label_code;
17     public Xcls_view_layout view_layout;
18     public Xcls_container container;
19     public Xcls_sourceview sourceview;
20
21         // my vars (def)
22     public Gtk.Widget lastObj;
23     public int width;
24     public JsRender.JsRender file;
25     public int height;
26     public Xcls_MainWindow main_window;
27
28     // ctor
29     public Xcls_GtkView()
30     {
31         _this = this;
32         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
33
34         // my vars (dec)
35         this.lastObj = null;
36         this.width = 0;
37         this.file = null;
38         this.height = 0;
39
40         // set gobject values
41         var child_0 = new Xcls_Notebook2( _this );
42         child_0.ref();
43         this.el.pack_start (  child_0.el , true,true,0 );
44
45         //listeners
46         this.el.size_allocate.connect( (aloc) => {
47         
48             this.width = aloc.width;
49             this.height =aloc.height;
50             });
51     }
52
53     // user defined functions
54     public void createThumb () {
55         
56         
57         if (this.file == null) {
58             return;
59         }
60         var filename = this.file.getIconFileName(false);
61         
62         var  win = this.el.get_parent_window();
63         var width = win.get_width();
64         var height = win.get_height();
65         try {
66              Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, width, height); // this.el.position?
67              screenshot.save(filename,"png");
68         } catch (Error e) {
69             
70         }
71     
72        
73         return;
74         
75         
76          
77          
78         
79         // should we hold until it's printed...
80         
81           
82     
83         
84         
85     
86     
87         
88          
89     }
90     public void loadFile (JsRender.JsRender file) 
91     {
92             this.file = null;
93             
94             if (file.tree == null) {
95                 return;
96             }
97            this.file = file;     
98             this.sourceview.loadFile();
99             
100             
101     
102             if (this.lastObj != null) {
103                 this.container.el.remove(this.lastObj);
104             }
105             
106             // hide the compile view at present..
107               
108             
109             var w = this.width;
110             var h = this.height;
111             
112             print("ALLOC SET SIZES %d, %d\n", w,h); 
113             
114             // set the container size min to 500/500 or 20 px less than max..
115             w = int.max (w-20, 500);
116             h = int.max (h-20, 500); 
117             
118             print("SET SIZES %d, %d\n", w,h);       
119             _this.container.el.set_size_request(w,h);
120             
121             _this.view_layout.el.set_size(w,h); // should be baded on calc.. -- see update_scrolled.
122             var rgba = Gdk.RGBA ();
123             rgba.parse ("#ccc");
124             _this.view_layout.el.override_background_color(Gtk.StateFlags.NORMAL, rgba);
125             
126             
127         var x = new JsRender.NodeToGtk(file.tree);
128             var obj = x.munge() as Gtk.Widget;
129             this.lastObj = null;
130         if (obj == null) {
131                 return;
132         }
133         this.lastObj = obj;
134             
135             this.container.el.add(obj);
136             obj.show_all();
137             
138              
139             
140     }
141     public class Xcls_Notebook2 : Object
142     {
143         public Gtk.Notebook el;
144         private Xcls_GtkView  _this;
145
146
147             // my vars (def)
148
149         // ctor
150         public Xcls_Notebook2(Xcls_GtkView _owner )
151         {
152             _this = _owner;
153             this.el = new Gtk.Notebook();
154
155             // my vars (dec)
156
157             // set gobject values
158             var child_0 = new Xcls_label_preview( _this );
159             child_0.ref();
160             var child_1 = new Xcls_label_code( _this );
161             child_1.ref();
162             var child_2 = new Xcls_ScrolledWindow5( _this );
163             child_2.ref();
164             this.el.append_page (  child_2.el , _this.label_preview.el );
165             var child_3 = new Xcls_ScrolledWindow8( _this );
166             child_3.ref();
167             this.el.append_page (  child_3.el , _this.label_code.el );
168         }
169
170         // user defined functions
171     }
172     public class Xcls_label_preview : Object
173     {
174         public Gtk.Label el;
175         private Xcls_GtkView  _this;
176
177
178             // my vars (def)
179
180         // ctor
181         public Xcls_label_preview(Xcls_GtkView _owner )
182         {
183             _this = _owner;
184             _this.label_preview = this;
185             this.el = new Gtk.Label( "Preview" );
186
187             // my vars (dec)
188
189             // set gobject values
190         }
191
192         // user defined functions
193     }
194
195     public class Xcls_label_code : Object
196     {
197         public Gtk.Label el;
198         private Xcls_GtkView  _this;
199
200
201             // my vars (def)
202
203         // ctor
204         public Xcls_label_code(Xcls_GtkView _owner )
205         {
206             _this = _owner;
207             _this.label_code = this;
208             this.el = new Gtk.Label( "Preview Generated Code" );
209
210             // my vars (dec)
211
212             // set gobject values
213         }
214
215         // user defined functions
216     }
217
218     public class Xcls_ScrolledWindow5 : Object
219     {
220         public Gtk.ScrolledWindow el;
221         private Xcls_GtkView  _this;
222
223
224             // my vars (def)
225
226         // ctor
227         public Xcls_ScrolledWindow5(Xcls_GtkView _owner )
228         {
229             _this = _owner;
230             this.el = new Gtk.ScrolledWindow( null, null );
231
232             // my vars (dec)
233
234             // set gobject values
235             var child_0 = new Xcls_view_layout( _this );
236             child_0.ref();
237             this.el.add (  child_0.el  );
238         }
239
240         // user defined functions
241     }
242     public class Xcls_view_layout : Object
243     {
244         public Gtk.Layout el;
245         private Xcls_GtkView  _this;
246
247
248             // my vars (def)
249
250         // ctor
251         public Xcls_view_layout(Xcls_GtkView _owner )
252         {
253             _this = _owner;
254             _this.view_layout = this;
255             this.el = new Gtk.Layout( null, null );
256
257             // my vars (dec)
258
259             // set gobject values
260             var child_0 = new Xcls_container( _this );
261             child_0.ref();
262             this.el.put (  child_0.el , 10,10 );
263         }
264
265         // user defined functions
266     }
267     public class Xcls_container : Object
268     {
269         public Gtk.HBox el;
270         private Xcls_GtkView  _this;
271
272
273             // my vars (def)
274
275         // ctor
276         public Xcls_container(Xcls_GtkView _owner )
277         {
278             _this = _owner;
279             _this.container = this;
280             this.el = new Gtk.HBox( true, 0 );
281
282             // my vars (dec)
283
284             // set gobject values
285         }
286
287         // user defined functions
288     }
289
290
291
292     public class Xcls_ScrolledWindow8 : Object
293     {
294         public Gtk.ScrolledWindow el;
295         private Xcls_GtkView  _this;
296
297
298             // my vars (def)
299
300         // ctor
301         public Xcls_ScrolledWindow8(Xcls_GtkView _owner )
302         {
303             _this = _owner;
304             this.el = new Gtk.ScrolledWindow( null, null );
305
306             // my vars (dec)
307
308             // set gobject values
309             var child_0 = new Xcls_sourceview( _this );
310             child_0.ref();
311             this.el.add (  child_0.el  );
312         }
313
314         // user defined functions
315     }
316     public class Xcls_sourceview : Object
317     {
318         public Gtk.SourceView el;
319         private Xcls_GtkView  _this;
320
321
322             // my vars (def)
323         public bool loading;
324
325         // ctor
326         public Xcls_sourceview(Xcls_GtkView _owner )
327         {
328             _this = _owner;
329             _this.sourceview = this;
330             this.el = new Gtk.SourceView();
331
332             // my vars (dec)
333             this.loading = true;
334
335             // set gobject values
336             this.el.editable = false;
337             this.el.show_line_marks = true;
338             this.el.show_line_numbers = true;
339
340             // init method
341
342             {
343                
344                 var description =   Pango.FontDescription.from_string("monospace");
345                 description.set_size(8000);
346                 this.el.override_font(description);
347             
348                 this.loading = true;
349                 var buf = this.el.get_buffer();
350                 buf.notify.connect((ps) => {
351                     if (this.loading) {
352                         return;
353                     }
354                     if (ps.name != "cursor-position") {
355                         return;
356                     }
357                     print("cursor changed : %d\n", buf.cursor_position);
358                     Gtk.TextIter cpos;
359                     buf.get_iter_at_offset(out cpos, buf.cursor_position);
360                     
361                     var ln = cpos.get_line();
362                     
363                     var node = _this.file.lineToNode(ln);
364                     if (node == null) {
365                         print("can not find node\n");
366                         return;
367                     }
368                     var ltree = _this.main_window.windowstate.left_tree;
369                     var tp = ltree.model.treePathFromNode(node);
370                     print("got tree path %s\n", tp);
371                     if (tp != "") {
372                         ltree.view.el.set_cursor(new Gtk.TreePath.from_string(tp), null, false);
373                     }
374                     
375                     // highlight the node..
376                     
377                 });
378               
379               
380               
381                 var attrs = new Gtk.SourceMarkAttributes();
382                 var  pink =   Gdk.RGBA();
383                 pink.parse ( "pink");
384                 attrs.set_background ( pink);
385                 attrs.set_icon_name ( "process-stop");    
386                 attrs.query_tooltip_text.connect(( mark) => {
387                     //print("tooltip query? %s\n", mark.name);
388                     return mark.name;
389                 });
390                 
391                 this.el.set_mark_attributes ("ERR", attrs, 1);
392                 
393                  var wattrs = new Gtk.SourceMarkAttributes();
394                 var  blue =   Gdk.RGBA();
395                 blue.parse ( "#ABF4EB");
396                 wattrs.set_background ( blue);
397                 wattrs.set_icon_name ( "process-stop");    
398                 wattrs.query_tooltip_text.connect(( mark) => {
399                     //print("tooltip query? %s\n", mark.name);
400                     return mark.name;
401                 });
402                 
403                 this.el.set_mark_attributes ("WARN", wattrs, 1);
404                 
405              
406                 
407                  var dattrs = new Gtk.SourceMarkAttributes();
408                 var  purple =   Gdk.RGBA();
409                 purple.parse ( "#EEA9FF");
410                 dattrs.set_background ( purple);
411                 dattrs.set_icon_name ( "process-stop");    
412                 dattrs.query_tooltip_text.connect(( mark) => {
413                     //print("tooltip query? %s\n", mark.name);
414                     return mark.name;
415                 });
416                 
417                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
418                 
419                 
420                 var gattrs = new Gtk.SourceMarkAttributes();
421                 var  grey =   Gdk.RGBA();
422                 grey.parse ( "#ccc");
423                 gattrs.set_background ( grey);
424              
425                 
426                 this.el.set_mark_attributes ("grey", gattrs, 1);
427                 
428                 
429                 
430                 
431                 
432                 
433             }
434         }
435
436         // user defined functions
437         public void nodeSelected (JsRender.Node? sel) {
438           
439             
440           
441             // this is connected in widnowstate
442             print("node selected");
443             var buf = this.el.get_buffer();
444          
445             var sbuf = (Gtk.SourceBuffer) buf;
446         
447            
448             while(Gtk.events_pending()) {
449                 Gtk.main_iteration();
450             }
451             
452            
453             // clear all the marks..
454              Gtk.TextIter start;
455             Gtk.TextIter end;     
456                 
457             sbuf.get_bounds (out start, out end);
458             sbuf.remove_source_marks (start, end, "grey");
459             
460             
461              if (sel == null) {
462                 // no highlighting..
463                 return;
464             }
465             Gtk.TextIter iter;   
466             sbuf.get_iter_at_line(out iter,  sel.line_start);
467             this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.0f);
468             
469             for (var i = 0; i < buf.get_line_count();i++) {
470                 if (i < sel.line_start || i > sel.line_end) {
471                    
472                     sbuf.get_iter_at_line(out iter, i);
473                     sbuf.create_source_mark(null, "grey", iter);
474                     
475                 }
476             
477             }
478             
479         
480         }
481         public void loadFile ( ) {
482             this.loading = true;
483             var buf = this.el.get_buffer();
484             buf.set_text("",0);
485             var sbuf = (Gtk.SourceBuffer) buf;
486         
487             
488         
489             if (_this.file == null || _this.file.xtype != "Gtk") {
490                 print("xtype != Gtk");
491                 this.loading = false;
492                 return;
493             }
494             
495             var valafn = "";
496               try {             
497                    var  regex = new Regex("\\.bjs$");
498                 
499                  
500                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
501                  } catch (GLib.RegexError e) {
502                      this.loading = false;
503                     return;
504                 }   
505         
506            if (!FileUtils.test(valafn,FileTest.IS_REGULAR) ) {
507                 print("File path has no errors\n");
508                 this.loading = false;
509                 return  ;
510             }
511             
512             string str;
513             GLib.FileUtils.get_contents (valafn, out str);
514         
515         //    print("setting str %d\n", str.length);
516             buf.set_text(str, str.length);
517             var lm = Gtk.SourceLanguageManager.get_default();
518              
519             //?? is javascript going to work as js?
520             
521             ((Gtk.SourceBuffer)(buf)) .set_language(lm.get_language(_this.file.language));
522           
523             
524             Gtk.TextIter start;
525             Gtk.TextIter end;     
526                 
527             sbuf.get_bounds (out start, out end);
528             sbuf.remove_source_marks (start, end, null); // remove all marks..
529             
530             
531             if (_this.main_window.windowstate.last_compile_result != null) {
532                 var obj = _this.main_window.windowstate.last_compile_result;
533                 this.highlightErrorsJson("ERR", obj);
534                 this.highlightErrorsJson("WARN", obj);
535                 this.highlightErrorsJson("DEPR", obj);                  
536             }
537             //while (Gtk.events_pending()) {
538              //   Gtk.main_iteration();
539            // }
540             
541             this.loading = false; 
542         }
543         public void highlightErrorsJson (string type, Json.Object obj) {
544               Gtk.TextIter start;
545              Gtk.TextIter end;   
546              
547              var buf =  this.el.get_buffer();
548                var sbuf = (Gtk.SourceBuffer)buf;
549                 buf.get_bounds (out start, out end);
550                 
551                 sbuf.remove_source_marks (start, end, type);
552                          
553              
554              // we should highlight other types of errors..
555             
556             if (!obj.has_member(type)) {
557                 print("Return has no errors\n");
558                 return  ;
559             }
560             var err = obj.get_object_member(type);
561             
562              
563         
564             var valafn = "";
565               try {             
566                    var  regex = new Regex("\\.bjs$");
567                 
568                  
569                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
570                  } catch (GLib.RegexError e) {
571                     return;
572                 }   
573         
574            if (!err.has_member(valafn)) {
575                 print("File path has no errors\n");
576                 return  ;
577             }
578             var lines = err.get_object_member(valafn);
579             
580            
581             
582             var tlines = buf.get_line_count () +1;
583             
584             lines.foreach_member((obj, line, node) => {
585                 
586                      Gtk.TextIter iter;
587             //        print("get inter\n");
588                     var eline = int.parse(line) -1  ;
589                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
590                     
591                     
592                     if (eline > tlines || eline < 0) {
593                         return;
594                     }
595                     sbuf.get_iter_at_line( out iter, eline);
596                     //print("mark line\n");
597                     var msg  = type + " on line: %d - %s".printf(eline+1, valafn);
598                     var ar = lines.get_array_member(line);
599                     for (var i = 0 ; i < ar.get_length(); i++) {
600                             msg += (msg.length > 0) ? "\n" : "";
601                             msg += ar.get_string_element(i);
602                     }
603                     
604                     
605                     sbuf.create_source_mark(msg, type, iter);
606                 } );
607                 return  ;
608             
609          
610         
611         
612         }
613     }
614
615
616
617 }