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