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                     // highlight the node..
352                     
353                 });
354               
355               
356               
357                 var attrs = new Gtk.SourceMarkAttributes();
358                 var  pink = new Gdk.RGBA();
359                 pink.parse ( "pink");
360                 attrs.set_background ( pink);
361                 attrs.set_icon_name ( "process-stop");    
362                 attrs.query_tooltip_text.connect(( mark) => {
363                     //print("tooltip query? %s\n", mark.name);
364                     return mark.name;
365                 });
366                 
367                 this.el.set_mark_attributes ("ERR", attrs, 1);
368                 
369                  var wattrs = new Gtk.SourceMarkAttributes();
370                 var  blue = new Gdk.RGBA();
371                 blue.parse ( "#ABF4EB");
372                 wattrs.set_background ( blue);
373                 wattrs.set_icon_name ( "process-stop");    
374                 wattrs.query_tooltip_text.connect(( mark) => {
375                     //print("tooltip query? %s\n", mark.name);
376                     return mark.name;
377                 });
378                 
379                 this.el.set_mark_attributes ("WARN", wattrs, 1);
380                 
381              
382                 
383                  var dattrs = new Gtk.SourceMarkAttributes();
384                 var  purple = new Gdk.RGBA();
385                 purple.parse ( "#EEA9FF");
386                 dattrs.set_background ( purple);
387                 dattrs.set_icon_name ( "process-stop");    
388                 dattrs.query_tooltip_text.connect(( mark) => {
389                     //print("tooltip query? %s\n", mark.name);
390                     return mark.name;
391                 });
392                 
393                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
394                 
395                 
396                 var gattrs = new Gtk.SourceMarkAttributes();
397                 var  grey = new Gdk.RGBA();
398                 grey.parse ( "#ccc");
399                 gattrs.set_background ( grey);
400              
401                 
402                 this.el.set_mark_attributes ("grey", gattrs, 1);
403                 
404                 
405                 
406                 
407                 
408                 
409             }
410         }
411
412         // user defined functions
413         public void nodeSelected (JsRender.Node? sel) {
414           
415             
416           
417             // this is connected in widnowstate
418             print("node selected");
419             var buf = this.el.get_buffer();
420          
421             var sbuf = (Gtk.SourceBuffer) buf;
422         
423            
424             while(Gtk.events_pending()) {
425                 Gtk.main_iteration();
426             }
427             
428            
429             // clear all the marks..
430              Gtk.TextIter start;
431             Gtk.TextIter end;     
432                 
433             sbuf.get_bounds (out start, out end);
434             sbuf.remove_source_marks (start, end, "grey");
435             
436             
437              if (sel == null) {
438                 // no highlighting..
439                 return;
440             }
441             Gtk.TextIter iter;   
442             sbuf.get_iter_at_line(out iter,  sel.line_start);
443             this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.0f);
444             
445             for (var i = 0; i < buf.get_line_count();i++) {
446                 if (i < sel.line_start || i > sel.line_end) {
447                    
448                     sbuf.get_iter_at_line(out iter, i);
449                     sbuf.create_source_mark(null, "grey", iter);
450                     
451                 }
452             
453             }
454             
455         
456         }
457         public void loadFile ( ) {
458             var buf = this.el.get_buffer();
459             buf.set_text("",0);
460             var sbuf = (Gtk.SourceBuffer) buf;
461         
462             
463         
464             if (_this.file == null || _this.file.xtype != "Gtk") {
465                 print("xtype != Gtk");
466                 return;
467             }
468             
469             var valafn = "";
470               try {             
471                    var  regex = new Regex("\\.bjs$");
472                 
473                  
474                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
475                  } catch (GLib.RegexError e) {
476                     return;
477                 }   
478         
479            if (!FileUtils.test(valafn,FileTest.IS_REGULAR) ) {
480                 print("File path has no errors\n");
481                 return  ;
482             }
483             
484             string str;
485             GLib.FileUtils.get_contents (valafn, out str);
486         
487         //    print("setting str %d\n", str.length);
488             buf.set_text(str, str.length);
489             var lm = Gtk.SourceLanguageManager.get_default();
490              
491             //?? is javascript going to work as js?
492             
493             ((Gtk.SourceBuffer)(buf)) .set_language(lm.get_language(_this.file.language));
494           
495             
496             Gtk.TextIter start;
497             Gtk.TextIter end;     
498                 
499             sbuf.get_bounds (out start, out end);
500             sbuf.remove_source_marks (start, end, null); // remove all marks..
501             
502             
503             if (_this.main_window.windowstate.last_compile_result != null) {
504                 var obj = _this.main_window.windowstate.last_compile_result;
505                 this.highlightErrorsJson("ERR", obj);
506                 this.highlightErrorsJson("WARN", obj);
507                 this.highlightErrorsJson("DEPR", obj);                  
508             }
509          
510         }
511         public void highlightErrorsJson (string type, Json.Object obj) {
512               Gtk.TextIter start;
513              Gtk.TextIter end;   
514              
515              var buf =  this.el.get_buffer();
516                var sbuf = (Gtk.SourceBuffer)buf;
517                 buf.get_bounds (out start, out end);
518                 
519                 sbuf.remove_source_marks (start, end, type);
520                          
521              
522              // we should highlight other types of errors..
523             
524             if (!obj.has_member(type)) {
525                 print("Return has no errors\n");
526                 return  ;
527             }
528             var err = obj.get_object_member(type);
529             
530              
531         
532             var valafn = "";
533               try {             
534                    var  regex = new Regex("\\.bjs$");
535                 
536                  
537                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
538                  } catch (GLib.RegexError e) {
539                     return;
540                 }   
541         
542            if (!err.has_member(valafn)) {
543                 print("File path has no errors\n");
544                 return  ;
545             }
546             var lines = err.get_object_member(valafn);
547             
548            
549             
550             var tlines = buf.get_line_count () +1;
551             
552             lines.foreach_member((obj, line, node) => {
553                 
554                      Gtk.TextIter iter;
555             //        print("get inter\n");
556                     var eline = int.parse(line) -1  ;
557                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
558                     
559                     
560                     if (eline > tlines || eline < 0) {
561                         return;
562                     }
563                     sbuf.get_iter_at_line( out iter, eline);
564                     //print("mark line\n");
565                     var msg  = type + " on line: %d - %s".printf(eline+1, valafn);
566                     var ar = lines.get_array_member(line);
567                     for (var i = 0 ; i < ar.get_length(); i++) {
568                             msg += (msg.length > 0) ? "\n" : "";
569                             msg += ar.get_string_element(i);
570                     }
571                     
572                     
573                     sbuf.create_source_mark(msg, type, iter);
574                 } );
575                 return  ;
576             
577          
578         
579         
580         }
581     }
582
583
584
585 }