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