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