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