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