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