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