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