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