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