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