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