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 void forwardSearch () {
192     
193         if (this.searchcontext == null) {
194                 return;
195         }
196         
197         Gtk.TextIter beg, st,en;
198          
199         this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
200         this.searchcontext.forward(beg, out st, out en);
201         this.last_search_end = en.get_offset();
202         
203         this.view.el.grab_focus();
204         this.buffer.el.place_cursor(st);
205         this.view.el.scroll_to_iter(st,  0.1f, true, 0.0f, 0.5f);
206         
207     
208     }
209     public class Xcls_notebook : Object
210     {
211         public Gtk.Notebook el;
212         private Xcls_GtkView  _this;
213
214
215             // my vars (def)
216
217         // ctor
218         public Xcls_notebook(Xcls_GtkView _owner )
219         {
220             _this = _owner;
221             _this.notebook = this;
222             this.el = new Gtk.Notebook();
223
224             // my vars (dec)
225
226             // set gobject values
227             var child_0 = new Xcls_label_preview( _this );
228             child_0.ref();
229             var child_1 = new Xcls_label_code( _this );
230             child_1.ref();
231             var child_2 = new Xcls_ScrolledWindow5( _this );
232             child_2.ref();
233             this.el.append_page (  child_2.el , _this.label_preview.el );
234             var child_3 = new Xcls_ScrolledWindow8( _this );
235             child_3.ref();
236             this.el.append_page (  child_3.el , _this.label_code.el );
237         }
238
239         // user defined functions
240     }
241     public class Xcls_label_preview : Object
242     {
243         public Gtk.Label el;
244         private Xcls_GtkView  _this;
245
246
247             // my vars (def)
248
249         // ctor
250         public Xcls_label_preview(Xcls_GtkView _owner )
251         {
252             _this = _owner;
253             _this.label_preview = this;
254             this.el = new Gtk.Label( "Preview" );
255
256             // my vars (dec)
257
258             // set gobject values
259         }
260
261         // user defined functions
262     }
263
264     public class Xcls_label_code : Object
265     {
266         public Gtk.Label el;
267         private Xcls_GtkView  _this;
268
269
270             // my vars (def)
271
272         // ctor
273         public Xcls_label_code(Xcls_GtkView _owner )
274         {
275             _this = _owner;
276             _this.label_code = this;
277             this.el = new Gtk.Label( "Preview Generated Code" );
278
279             // my vars (dec)
280
281             // set gobject values
282         }
283
284         // user defined functions
285     }
286
287     public class Xcls_ScrolledWindow5 : Object
288     {
289         public Gtk.ScrolledWindow el;
290         private Xcls_GtkView  _this;
291
292
293             // my vars (def)
294
295         // ctor
296         public Xcls_ScrolledWindow5(Xcls_GtkView _owner )
297         {
298             _this = _owner;
299             this.el = new Gtk.ScrolledWindow( null, null );
300
301             // my vars (dec)
302
303             // set gobject values
304             var child_0 = new Xcls_view_layout( _this );
305             child_0.ref();
306             this.el.add (  child_0.el  );
307         }
308
309         // user defined functions
310     }
311     public class Xcls_view_layout : Object
312     {
313         public Gtk.Layout el;
314         private Xcls_GtkView  _this;
315
316
317             // my vars (def)
318
319         // ctor
320         public Xcls_view_layout(Xcls_GtkView _owner )
321         {
322             _this = _owner;
323             _this.view_layout = this;
324             this.el = new Gtk.Layout( null, null );
325
326             // my vars (dec)
327
328             // set gobject values
329             var child_0 = new Xcls_container( _this );
330             child_0.ref();
331             this.el.put (  child_0.el , 10,10 );
332         }
333
334         // user defined functions
335     }
336     public class Xcls_container : Object
337     {
338         public Gtk.Box el;
339         private Xcls_GtkView  _this;
340
341
342             // my vars (def)
343
344         // ctor
345         public Xcls_container(Xcls_GtkView _owner )
346         {
347             _this = _owner;
348             _this.container = this;
349             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
350
351             // my vars (dec)
352
353             // set gobject values
354         }
355
356         // user defined functions
357     }
358
359
360
361     public class Xcls_ScrolledWindow8 : Object
362     {
363         public Gtk.ScrolledWindow el;
364         private Xcls_GtkView  _this;
365
366
367             // my vars (def)
368
369         // ctor
370         public Xcls_ScrolledWindow8(Xcls_GtkView _owner )
371         {
372             _this = _owner;
373             this.el = new Gtk.ScrolledWindow( null, null );
374
375             // my vars (dec)
376
377             // set gobject values
378             var child_0 = new Xcls_sourceview( _this );
379             child_0.ref();
380             this.el.add (  child_0.el  );
381         }
382
383         // user defined functions
384     }
385     public class Xcls_sourceview : Object
386     {
387         public Gtk.SourceView el;
388         private Xcls_GtkView  _this;
389
390
391             // my vars (def)
392         public bool loading;
393
394         // ctor
395         public Xcls_sourceview(Xcls_GtkView _owner )
396         {
397             _this = _owner;
398             _this.sourceview = this;
399             this.el = new Gtk.SourceView();
400
401             // my vars (dec)
402             this.loading = true;
403
404             // set gobject values
405             this.el.editable = false;
406             this.el.show_line_marks = true;
407             this.el.show_line_numbers = true;
408
409             // init method
410
411             {
412                
413                 var description =   Pango.FontDescription.from_string("monospace");
414                 description.set_size(8000);
415                 this.el.override_font(description);
416             
417                 this.loading = true;
418                 var buf = this.el.get_buffer();
419                 buf.notify.connect((ps) => {
420                     if (this.loading) {
421                         return;
422                     }
423                     if (ps.name != "cursor-position") {
424                         return;
425                     }
426                     print("cursor changed : %d\n", buf.cursor_position);
427                     Gtk.TextIter cpos;
428                     buf.get_iter_at_offset(out cpos, buf.cursor_position);
429                     
430                     var ln = cpos.get_line();
431                     
432                     var node = _this.file.lineToNode(ln);
433                     if (node == null) {
434                         print("can not find node\n");
435                         return;
436                     }
437                     var ltree = _this.main_window.windowstate.left_tree;
438                     var tp = ltree.model.treePathFromNode(node);
439                     print("got tree path %s\n", tp);
440                     if (tp != "") {
441                         ltree.view.el.set_cursor(new Gtk.TreePath.from_string(tp), null, false);
442                     }
443                     
444                     // highlight the node..
445                     
446                 });
447               
448               
449               
450                 var attrs = new Gtk.SourceMarkAttributes();
451                 var  pink =   Gdk.RGBA();
452                 pink.parse ( "pink");
453                 attrs.set_background ( pink);
454                 attrs.set_icon_name ( "process-stop");    
455                 attrs.query_tooltip_text.connect(( mark) => {
456                     //print("tooltip query? %s\n", mark.name);
457                     return mark.name;
458                 });
459                 
460                 this.el.set_mark_attributes ("ERR", attrs, 1);
461                 
462                  var wattrs = new Gtk.SourceMarkAttributes();
463                 var  blue =   Gdk.RGBA();
464                 blue.parse ( "#ABF4EB");
465                 wattrs.set_background ( blue);
466                 wattrs.set_icon_name ( "process-stop");    
467                 wattrs.query_tooltip_text.connect(( mark) => {
468                     //print("tooltip query? %s\n", mark.name);
469                     return mark.name;
470                 });
471                 
472                 this.el.set_mark_attributes ("WARN", wattrs, 1);
473                 
474              
475                 
476                  var dattrs = new Gtk.SourceMarkAttributes();
477                 var  purple =   Gdk.RGBA();
478                 purple.parse ( "#EEA9FF");
479                 dattrs.set_background ( purple);
480                 dattrs.set_icon_name ( "process-stop");    
481                 dattrs.query_tooltip_text.connect(( mark) => {
482                     //print("tooltip query? %s\n", mark.name);
483                     return mark.name;
484                 });
485                 
486                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
487                 
488                 
489                 var gattrs = new Gtk.SourceMarkAttributes();
490                 var  grey =   Gdk.RGBA();
491                 grey.parse ( "#ccc");
492                 gattrs.set_background ( grey);
493              
494                 
495                 this.el.set_mark_attributes ("grey", gattrs, 1);
496                 
497                 
498                 
499                 
500                 
501                 
502             }
503         }
504
505         // user defined functions
506         public void nodeSelected (JsRender.Node? sel) {
507           
508             
509           
510             // this is connected in widnowstate
511             print("node selected");
512             var buf = this.el.get_buffer();
513          
514             var sbuf = (Gtk.SourceBuffer) buf;
515         
516            
517             while(Gtk.events_pending()) {
518                 Gtk.main_iteration();
519             }
520             
521            
522             // clear all the marks..
523              Gtk.TextIter start;
524             Gtk.TextIter end;     
525                 
526             sbuf.get_bounds (out start, out end);
527             sbuf.remove_source_marks (start, end, "grey");
528             
529             
530              if (sel == null) {
531                 // no highlighting..
532                 return;
533             }
534             Gtk.TextIter iter;   
535             sbuf.get_iter_at_line(out iter,  sel.line_start);
536             this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.0f);
537             
538             for (var i = 0; i < buf.get_line_count();i++) {
539                 if (i < sel.line_start || i > sel.line_end) {
540                    
541                     sbuf.get_iter_at_line(out iter, i);
542                     sbuf.create_source_mark(null, "grey", iter);
543                     
544                 }
545             
546             }
547             
548         
549         }
550         public string toString () {
551            Gtk.TextIter s;
552             Gtk.TextIter e;
553             this.el.get_buffer().get_start_iter(out s);
554             this.el.get_buffer().get_end_iter(out e);
555             var ret = this.el.get_buffer().get_text(s,e,true);
556             //print("TO STRING? " + ret);
557             return ret;
558         }
559         public void loadFile ( ) {
560             this.loading = true;
561             var buf = this.el.get_buffer();
562             buf.set_text("",0);
563             var sbuf = (Gtk.SourceBuffer) buf;
564         
565             
566         
567             if (_this.file == null || _this.file.xtype != "Gtk") {
568                 print("xtype != Gtk");
569                 this.loading = false;
570                 return;
571             }
572             
573             var valafn = "";
574               try {             
575                    var  regex = new Regex("\\.bjs$");
576                 
577                  
578                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
579                  } catch (GLib.RegexError e) {
580                      this.loading = false;
581                     return;
582                 }   
583             
584         
585            if (!FileUtils.test(valafn,FileTest.IS_REGULAR) ) {
586                 print("File path has no errors\n");
587                 this.loading = false;
588                 return  ;
589             }
590             
591             string str;
592             try {
593             
594                 GLib.FileUtils.get_contents (valafn, out str);
595             } catch (Error e) {
596                 this.loading = false;
597                 return  ;
598             }
599         
600         //    print("setting str %d\n", str.length);
601             buf.set_text(str, str.length);
602             var lm = Gtk.SourceLanguageManager.get_default();
603              
604             //?? is javascript going to work as js?
605             
606             ((Gtk.SourceBuffer)(buf)) .set_language(lm.get_language(_this.file.language));
607           
608             
609             Gtk.TextIter start;
610             Gtk.TextIter end;     
611                 
612             sbuf.get_bounds (out start, out end);
613             sbuf.remove_source_marks (start, end, null); // remove all marks..
614             
615             
616             if (_this.main_window.windowstate.last_compile_result != null) {
617                 var obj = _this.main_window.windowstate.last_compile_result;
618                 this.highlightErrorsJson("ERR", obj);
619                 this.highlightErrorsJson("WARN", obj);
620                 this.highlightErrorsJson("DEPR", obj);                  
621             }
622             //while (Gtk.events_pending()) {
623              //   Gtk.main_iteration();
624            // }
625             
626             this.loading = false; 
627         }
628         public void highlightErrorsJson (string type, Json.Object obj) {
629               Gtk.TextIter start;
630              Gtk.TextIter end;   
631              
632              var buf =  this.el.get_buffer();
633                var sbuf = (Gtk.SourceBuffer)buf;
634                 buf.get_bounds (out start, out end);
635                 
636                 sbuf.remove_source_marks (start, end, type);
637                          
638              
639              // we should highlight other types of errors..
640             
641             if (!obj.has_member(type)) {
642                 print("Return has no errors\n");
643                 return  ;
644             }
645             var err = obj.get_object_member(type);
646             
647             if (_this.file == null) { 
648                 return; // just in case the file has not loaded yet?
649             }
650          
651         
652             var valafn = "";
653               try {             
654                    var  regex = new Regex("\\.bjs$");
655                 
656                  
657                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
658                  } catch (GLib.RegexError e) {
659                     return;
660                 }   
661         
662            if (!err.has_member(valafn)) {
663                 print("File path has no errors\n");
664                 return  ;
665             }
666             var lines = err.get_object_member(valafn);
667             
668            
669             
670             var tlines = buf.get_line_count () +1;
671             
672             lines.foreach_member((obj, line, node) => {
673                 
674                      Gtk.TextIter iter;
675             //        print("get inter\n");
676                     var eline = int.parse(line) -1  ;
677                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
678                     
679                     
680                     if (eline > tlines || eline < 0) {
681                         return;
682                     }
683                     sbuf.get_iter_at_line( out iter, eline);
684                     //print("mark line\n");
685                     var msg  = type + " on line: %d - %s".printf(eline+1, valafn);
686                     var ar = lines.get_array_member(line);
687                     for (var i = 0 ; i < ar.get_length(); i++) {
688                             msg += (msg.length > 0) ? "\n" : "";
689                             msg += ar.get_string_element(i);
690                     }
691                     
692                     
693                     sbuf.create_source_mark(msg, type, iter);
694                 } );
695                 return  ;
696             
697          
698         
699         
700         }
701     }
702
703
704
705 }