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