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