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