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         public bool loading;
322
323         // ctor
324         public Xcls_sourceview(Xcls_GtkView _owner )
325         {
326             _this = _owner;
327             _this.sourceview = this;
328             this.el = new Gtk.SourceView();
329
330             // my vars (dec)
331             this.loading = true;
332
333             // set gobject values
334             this.el.editable = false;
335             this.el.show_line_marks = true;
336             this.el.show_line_numbers = true;
337
338             // init method
339
340             {
341                
342                 var description =   Pango.FontDescription.from_string("monospace");
343                 description.set_size(8000);
344                 this.el.override_font(description);
345                 this.loading = true;
346                 var buf = this.el.get_buffer();
347                 buf.notify.connect((ps) => {
348                     if (this.loading) {
349                         return;
350                     }
351                     if (ps.name != "cursor-position") {
352                         return;
353                     }
354                     print("cursor changed : %d\n", buf.cursor_position);
355                     Gtk.TextIter cpos;
356                     buf.get_iter_at_offset(out cpos, buf.cursor_position);
357                     
358                     var ln = cpos.get_line();
359                     
360                     var node = _this.file.lineToNode(ln);
361                     if (node == null) {
362                         print("can not find node\n");
363                         return;
364                     }
365                     var ltree = _this.main_window.windowstate.left_tree;
366                     var tp = ltree.model.treePathFromNode(node);
367                     print("got tree path %s\n", tp);
368                     if (tp != "") {
369                         ltree.view.el.set_cursor(new Gtk.TreePath.from_string(tp), null, false);
370                     }
371                     
372                     // highlight the node..
373                     
374                 });
375               
376               
377               
378                 var attrs = new Gtk.SourceMarkAttributes();
379                 var  pink =   Gdk.RGBA();
380                 pink.parse ( "pink");
381                 attrs.set_background ( pink);
382                 attrs.set_icon_name ( "process-stop");    
383                 attrs.query_tooltip_text.connect(( mark) => {
384                     //print("tooltip query? %s\n", mark.name);
385                     return mark.name;
386                 });
387                 
388                 this.el.set_mark_attributes ("ERR", attrs, 1);
389                 
390                  var wattrs = new Gtk.SourceMarkAttributes();
391                 var  blue =   Gdk.RGBA();
392                 blue.parse ( "#ABF4EB");
393                 wattrs.set_background ( blue);
394                 wattrs.set_icon_name ( "process-stop");    
395                 wattrs.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 ("WARN", wattrs, 1);
401                 
402              
403                 
404                  var dattrs = new Gtk.SourceMarkAttributes();
405                 var  purple =   Gdk.RGBA();
406                 purple.parse ( "#EEA9FF");
407                 dattrs.set_background ( purple);
408                 dattrs.set_icon_name ( "process-stop");    
409                 dattrs.query_tooltip_text.connect(( mark) => {
410                     //print("tooltip query? %s\n", mark.name);
411                     return mark.name;
412                 });
413                 
414                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
415                 
416                 
417                 var gattrs = new Gtk.SourceMarkAttributes();
418                 var  grey =   Gdk.RGBA();
419                 grey.parse ( "#ccc");
420                 gattrs.set_background ( grey);
421              
422                 
423                 this.el.set_mark_attributes ("grey", gattrs, 1);
424                 
425                 
426                 
427                 
428                 
429                 
430             }
431         }
432
433         // user defined functions
434         public void nodeSelected (JsRender.Node? sel) {
435           
436             
437           
438             // this is connected in widnowstate
439             print("node selected");
440             var buf = this.el.get_buffer();
441          
442             var sbuf = (Gtk.SourceBuffer) buf;
443         
444            
445             while(Gtk.events_pending()) {
446                 Gtk.main_iteration();
447             }
448             
449            
450             // clear all the marks..
451              Gtk.TextIter start;
452             Gtk.TextIter end;     
453                 
454             sbuf.get_bounds (out start, out end);
455             sbuf.remove_source_marks (start, end, "grey");
456             
457             
458              if (sel == null) {
459                 // no highlighting..
460                 return;
461             }
462             Gtk.TextIter iter;   
463             sbuf.get_iter_at_line(out iter,  sel.line_start);
464             this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.0f);
465             
466             for (var i = 0; i < buf.get_line_count();i++) {
467                 if (i < sel.line_start || i > sel.line_end) {
468                    
469                     sbuf.get_iter_at_line(out iter, i);
470                     sbuf.create_source_mark(null, "grey", iter);
471                     
472                 }
473             
474             }
475             
476         
477         }
478         public void loadFile ( ) {
479             this.loading = true;
480             var buf = this.el.get_buffer();
481             buf.set_text("",0);
482             var sbuf = (Gtk.SourceBuffer) buf;
483         
484             
485         
486             if (_this.file == null || _this.file.xtype != "Gtk") {
487                 print("xtype != Gtk");
488                 this.loading = false;
489                 return;
490             }
491             
492             var valafn = "";
493               try {             
494                    var  regex = new Regex("\\.bjs$");
495                 
496                  
497                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
498                  } catch (GLib.RegexError e) {
499                      this.loading = false;
500                     return;
501                 }   
502         
503            if (!FileUtils.test(valafn,FileTest.IS_REGULAR) ) {
504                 print("File path has no errors\n");
505                 this.loading = false;
506                 return  ;
507             }
508             
509             string str;
510             GLib.FileUtils.get_contents (valafn, out str);
511         
512         //    print("setting str %d\n", str.length);
513             buf.set_text(str, str.length);
514             var lm = Gtk.SourceLanguageManager.get_default();
515              
516             //?? is javascript going to work as js?
517             
518             ((Gtk.SourceBuffer)(buf)) .set_language(lm.get_language(_this.file.language));
519           
520             
521             Gtk.TextIter start;
522             Gtk.TextIter end;     
523                 
524             sbuf.get_bounds (out start, out end);
525             sbuf.remove_source_marks (start, end, null); // remove all marks..
526             
527             
528             if (_this.main_window.windowstate.last_compile_result != null) {
529                 var obj = _this.main_window.windowstate.last_compile_result;
530                 this.highlightErrorsJson("ERR", obj);
531                 this.highlightErrorsJson("WARN", obj);
532                 this.highlightErrorsJson("DEPR", obj);                  
533             }
534             while (Gtk.events_pending()) {
535                 Gtk.main_iteration();
536             }
537             
538             this.loading = false; 
539         }
540         public void highlightErrorsJson (string type, Json.Object obj) {
541               Gtk.TextIter start;
542              Gtk.TextIter end;   
543              
544              var buf =  this.el.get_buffer();
545                var sbuf = (Gtk.SourceBuffer)buf;
546                 buf.get_bounds (out start, out end);
547                 
548                 sbuf.remove_source_marks (start, end, type);
549                          
550              
551              // we should highlight other types of errors..
552             
553             if (!obj.has_member(type)) {
554                 print("Return has no errors\n");
555                 return  ;
556             }
557             var err = obj.get_object_member(type);
558             
559              
560         
561             var valafn = "";
562               try {             
563                    var  regex = new Regex("\\.bjs$");
564                 
565                  
566                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
567                  } catch (GLib.RegexError e) {
568                     return;
569                 }   
570         
571            if (!err.has_member(valafn)) {
572                 print("File path has no errors\n");
573                 return  ;
574             }
575             var lines = err.get_object_member(valafn);
576             
577            
578             
579             var tlines = buf.get_line_count () +1;
580             
581             lines.foreach_member((obj, line, node) => {
582                 
583                      Gtk.TextIter iter;
584             //        print("get inter\n");
585                     var eline = int.parse(line) -1  ;
586                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
587                     
588                     
589                     if (eline > tlines || eline < 0) {
590                         return;
591                     }
592                     sbuf.get_iter_at_line( out iter, eline);
593                     //print("mark line\n");
594                     var msg  = type + " on line: %d - %s".printf(eline+1, valafn);
595                     var ar = lines.get_array_member(line);
596                     for (var i = 0 ; i < ar.get_length(); i++) {
597                             msg += (msg.length > 0) ? "\n" : "";
598                             msg += ar.get_string_element(i);
599                     }
600                     
601                     
602                     sbuf.create_source_mark(msg, type, iter);
603                 } );
604                 return  ;
605             
606          
607         
608         
609         }
610     }
611
612
613
614 }