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