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