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