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