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