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