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