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