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