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