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