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