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