src/Builder4/WindowRooView.bjs
[app.Builder.js] / src / Builder4 / WindowRooView.vala
1 static Xcls_WindowRooView  _WindowRooView;
2
3 public class Xcls_WindowRooView : Object
4 {
5     public Gtk.Box el;
6     private Xcls_WindowRooView  _this;
7
8     public static Xcls_WindowRooView singleton()
9     {
10         if (_WindowRooView == null) {
11             _WindowRooView= new Xcls_WindowRooView();
12         }
13         return _WindowRooView;
14     }
15     public Xcls_notebook notebook;
16     public Xcls_label_preview label_preview;
17     public Xcls_label_code label_code;
18     public Xcls_paned paned;
19     public Xcls_viewbox viewbox;
20     public Xcls_AutoRedraw AutoRedraw;
21     public Xcls_viewcontainer viewcontainer;
22     public Xcls_view view;
23     public Xcls_inspectorcontainer inspectorcontainer;
24     public Xcls_sourceview sourceview;
25
26         // my vars (def)
27     public Gtk.Widget lastObj;
28     public int width;
29     public int last_search_end;
30     public Gtk.SourceSearchContext searchcontext;
31     public JsRender.JsRender file;
32     public int height;
33     public Xcls_MainWindow main_window;
34
35     // ctor
36     public Xcls_WindowRooView()
37     {
38         _this = this;
39         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
40
41         // my vars (dec)
42         this.lastObj = null;
43         this.width = 0;
44         this.last_search_end = 0;
45         this.file = null;
46         this.height = 0;
47
48         // set gobject values
49         this.el.hexpand = true;
50         var child_0 = new Xcls_notebook( _this );
51         child_0.ref();
52         this.el.pack_start (  child_0.el , true,true,0 );
53     }
54
55     // user defined functions
56     public void scroll_to_line (int line) {
57        this.notebook.el.page = 1;// code preview...
58        
59        GLib.Timeout.add(500, () => {
60        
61        
62            
63            
64                   var buf = this.sourceview.el.get_buffer();
65          
66                 var sbuf = (Gtk.SourceBuffer) buf;
67     
68     
69                 Gtk.TextIter iter;   
70                 sbuf.get_iter_at_line(out iter,  line);
71                 this.sourceview.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
72                 return false;
73         });   
74     
75        
76     }
77     public void createThumb () {
78         
79         
80         if (this.file == null) {
81             return;
82         }
83         var filename = this.file.getIconFileName(false);
84         
85         var  win = this.el.get_parent_window();
86         var width = win.get_width();
87       //  var height = win.get_height();
88         try { 
89             Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, width, this.el.position);
90             screenshot.save(filename,"png");
91         } catch(Error e) {
92             //noop
93         }
94     
95         
96          
97         
98          
99     }
100     public void loadFile (JsRender.JsRender file)
101     {
102         this.file = file;
103         this.view.renderJS(true);
104         this.notebook.el.page = 0;// gtk preview 
105         this.sourceview.loadFile();   
106         
107     }
108     public int search (string txt) {
109         this.notebook.el.page = 1;
110         var s = new Gtk.SourceSearchSettings();
111         var buf = (Gtk.SourceBuffer) this.sourceview.el.get_buffer();
112         this.searchcontext = new Gtk.SourceSearchContext(buf,s);
113         this.searchcontext.set_highlight(true);
114         s.set_search_text(txt);
115         
116         Gtk.TextIter beg, st,en;
117          
118         buf.get_start_iter(out beg);
119         this.searchcontext.forward(beg, out st, out en);
120         this.last_search_end  = 0;
121         return this.searchcontext.get_occurrences_count();
122     
123        
124     }
125     public void requestRedraw () {
126         this.view.renderJS(false);
127     }
128     public void forwardSearch () {
129     
130         if (this.searchcontext == null) {
131                 return;
132         }
133         this.notebook.el.page = 1;
134         Gtk.TextIter beg, st,en, stl;
135         
136         var buf = this.sourceview.el.get_buffer();
137         buf.get_iter_at_offset(out beg, this.last_search_end);
138         if (!this.searchcontext.forward(beg, out st, out en)) {
139                 this.last_search_end = 0;
140         } else { 
141                 this.last_search_end = en.get_offset();
142         
143                 this.sourceview.el.grab_focus();
144          
145                 buf.place_cursor(st);
146                 var ln = st.get_line();
147                 buf.get_iter_at_line(out stl,ln);
148                  
149                 this.sourceview.el.scroll_to_iter(stl,  0.0f, true, 0.0f, 0.5f);
150         }
151     
152     }
153     public class Xcls_notebook : Object
154     {
155         public Gtk.Notebook el;
156         private Xcls_WindowRooView  _this;
157
158
159             // my vars (def)
160
161         // ctor
162         public Xcls_notebook(Xcls_WindowRooView _owner )
163         {
164             _this = _owner;
165             _this.notebook = this;
166             this.el = new Gtk.Notebook();
167
168             // my vars (dec)
169
170             // set gobject values
171             var child_0 = new Xcls_label_preview( _this );
172             child_0.ref();
173             var child_1 = new Xcls_label_code( _this );
174             child_1.ref();
175             var child_2 = new Xcls_paned( _this );
176             child_2.ref();
177             this.el.add (  child_2.el  );
178             var child_3 = new Xcls_ScrolledWindow14( _this );
179             child_3.ref();
180             this.el.append_page (  child_3.el , _this.label_code.el );
181         }
182
183         // user defined functions
184     }
185     public class Xcls_label_preview : Object
186     {
187         public Gtk.Label el;
188         private Xcls_WindowRooView  _this;
189
190
191             // my vars (def)
192
193         // ctor
194         public Xcls_label_preview(Xcls_WindowRooView _owner )
195         {
196             _this = _owner;
197             _this.label_preview = this;
198             this.el = new Gtk.Label( "Preview" );
199
200             // my vars (dec)
201
202             // set gobject values
203         }
204
205         // user defined functions
206     }
207
208     public class Xcls_label_code : Object
209     {
210         public Gtk.Label el;
211         private Xcls_WindowRooView  _this;
212
213
214             // my vars (def)
215
216         // ctor
217         public Xcls_label_code(Xcls_WindowRooView _owner )
218         {
219             _this = _owner;
220             _this.label_code = this;
221             this.el = new Gtk.Label( "Preview Generated Code" );
222
223             // my vars (dec)
224
225             // set gobject values
226         }
227
228         // user defined functions
229     }
230
231     public class Xcls_paned : Object
232     {
233         public Gtk.Paned el;
234         private Xcls_WindowRooView  _this;
235
236
237             // my vars (def)
238
239         // ctor
240         public Xcls_paned(Xcls_WindowRooView _owner )
241         {
242             _this = _owner;
243             _this.paned = this;
244             this.el = new Gtk.Paned( Gtk.Orientation.VERTICAL );
245
246             // my vars (dec)
247
248             // set gobject values
249             var child_0 = new Xcls_viewbox( _this );
250             child_0.ref();
251             this.el.pack1 (  child_0.el , true,true );
252             var child_1 = new Xcls_inspectorcontainer( _this );
253             child_1.ref();
254             this.el.pack2 (  child_1.el , true,true );
255         }
256
257         // user defined functions
258     }
259     public class Xcls_viewbox : Object
260     {
261         public Gtk.Box el;
262         private Xcls_WindowRooView  _this;
263
264
265             // my vars (def)
266
267         // ctor
268         public Xcls_viewbox(Xcls_WindowRooView _owner )
269         {
270             _this = _owner;
271             _this.viewbox = this;
272             this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
273
274             // my vars (dec)
275
276             // set gobject values
277             this.el.homogeneous = false;
278             var child_0 = new Xcls_Box7( _this );
279             child_0.ref();
280             this.el.pack_start (  child_0.el , false,true,0 );
281             var child_1 = new Xcls_viewcontainer( _this );
282             child_1.ref();
283             this.el.pack_end (  child_1.el , true,true,0 );
284         }
285
286         // user defined functions
287     }
288     public class Xcls_Box7 : Object
289     {
290         public Gtk.Box el;
291         private Xcls_WindowRooView  _this;
292
293
294             // my vars (def)
295
296         // ctor
297         public Xcls_Box7(Xcls_WindowRooView _owner )
298         {
299             _this = _owner;
300             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
301
302             // my vars (dec)
303
304             // set gobject values
305             this.el.homogeneous = true;
306             this.el.height_request = 20;
307             this.el.vexpand = false;
308             var child_0 = new Xcls_Button8( _this );
309             child_0.ref();
310             this.el.pack_start (  child_0.el , false,false,0 );
311             var child_1 = new Xcls_AutoRedraw( _this );
312             child_1.ref();
313             this.el.pack_start (  child_1.el , false,false,0 );
314             var child_2 = new Xcls_Button10( _this );
315             child_2.ref();
316             this.el.pack_start (  child_2.el , false,false,0 );
317         }
318
319         // user defined functions
320     }
321     public class Xcls_Button8 : Object
322     {
323         public Gtk.Button el;
324         private Xcls_WindowRooView  _this;
325
326
327             // my vars (def)
328
329         // ctor
330         public Xcls_Button8(Xcls_WindowRooView _owner )
331         {
332             _this = _owner;
333             this.el = new Gtk.Button();
334
335             // my vars (dec)
336
337             // set gobject values
338             this.el.label = "Redraw";
339
340             //listeners
341             this.el.clicked.connect( ( ) => {
342                 _this.view.renderJS(  true);
343             });
344         }
345
346         // user defined functions
347     }
348
349     public class Xcls_AutoRedraw : Object
350     {
351         public Gtk.CheckButton el;
352         private Xcls_WindowRooView  _this;
353
354
355             // my vars (def)
356
357         // ctor
358         public Xcls_AutoRedraw(Xcls_WindowRooView _owner )
359         {
360             _this = _owner;
361             _this.AutoRedraw = this;
362             this.el = new Gtk.CheckButton();
363
364             // my vars (dec)
365
366             // set gobject values
367             this.el.active = true;
368             this.el.label = "Auto Redraw On";
369
370             //listeners
371             this.el.toggled.connect( (state) => {
372                 this.el.set_label(this.el.active  ? "Auto Redraw On" : "Auto Redraw Off");
373             });
374         }
375
376         // user defined functions
377     }
378
379     public class Xcls_Button10 : Object
380     {
381         public Gtk.Button el;
382         private Xcls_WindowRooView  _this;
383
384
385             // my vars (def)
386
387         // ctor
388         public Xcls_Button10(Xcls_WindowRooView _owner )
389         {
390             _this = _owner;
391             this.el = new Gtk.Button();
392
393             // my vars (dec)
394
395             // set gobject values
396             this.el.label = "Full Redraw";
397
398             //listeners
399             this.el.clicked.connect( () => {
400               _this.view.redraws = 99;
401                 _this.view.el.web_context.clear_cache();  
402               //_this.view.renderJS(true);
403               FakeServerCache.clear();
404               _this.view.reInit();
405             
406             });
407         }
408
409         // user defined functions
410     }
411
412
413     public class Xcls_viewcontainer : Object
414     {
415         public Gtk.ScrolledWindow el;
416         private Xcls_WindowRooView  _this;
417
418
419             // my vars (def)
420
421         // ctor
422         public Xcls_viewcontainer(Xcls_WindowRooView _owner )
423         {
424             _this = _owner;
425             _this.viewcontainer = this;
426             this.el = new Gtk.ScrolledWindow( null, null );
427
428             // my vars (dec)
429
430             // set gobject values
431             this.el.shadow_type = Gtk.ShadowType.IN;
432             var child_0 = new Xcls_view( _this );
433             child_0.ref();
434             this.el.add (  child_0.el  );
435
436             // init method
437
438             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
439         }
440
441         // user defined functions
442     }
443     public class Xcls_view : Object
444     {
445         public WebKit.WebView el;
446         private Xcls_WindowRooView  _this;
447
448
449             // my vars (def)
450         public string renderedData;
451         public bool refreshRequired;
452         public WebKit.WebInspector inspector;
453         public string runjs;
454         public int redraws;
455         public GLib.DateTime lastRedraw;
456         public string runhtml;
457         public bool pendingRedraw;
458
459         // ctor
460         public Xcls_view(Xcls_WindowRooView _owner )
461         {
462             _this = _owner;
463             _this.view = this;
464             this.el = new WebKit.WebView();
465
466             // my vars (dec)
467             this.renderedData = "";
468             this.refreshRequired = false;
469             this.runjs = "";
470             this.redraws = 0;
471             this.lastRedraw = null;
472             this.runhtml = "";
473             this.pendingRedraw = false;
474
475             // set gobject values
476
477             // init method
478
479             {
480                 // this may not work!?
481                 var settings =  this.el.get_settings();
482                 settings.enable_developer_extras = true;
483                 
484                 
485                 var fs= new FakeServer(this.el);
486                 fs.ref();
487                 // this was an attempt to change the url perms.. did not work..
488                 // settings.enable_file_access_from_file_uris = true;
489                 // settings.enable_offline_web_application_cache - true;
490                 // settings.enable_universal_access_from_file_uris = true;
491                
492                  
493                 
494                 
495                 
496             
497                  // FIXME - base url of script..
498                  // we need it so some of the database features work.
499                 this.el.load_html( "Render not ready" , 
500                         //fixme - should be a config option!
501                         // or should we catch stuff and fix it up..
502                         "http://localhost/app.Builder/"
503                 );
504                     
505                     
506                //this.el.open('file:///' + __script_path__ + '/../builder.html');
507                 /*
508                 Gtk.drag_dest_set
509                 (
510                         this.el,              //
511                         Gtk.DestDefaults.MOTION  | Gtk.DestDefaults.HIGHLIGHT,
512                         null,            // list of targets
513                         Gdk.DragAction.COPY         // what to do with data after dropped 
514                 );
515                                         
516                // print("RB: TARGETS : " + LeftTree.atoms["STRING"]);
517                 Gtk.drag_dest_set_target_list(this.el, this.get('/Window').targetList);
518                 */
519                 GLib.Timeout.add_seconds(1,  ()  =>{
520                      //print("run refresh?");
521                      if (this.el == null) {
522                         return false;
523                      }
524                      this.runRefresh(); 
525                      return true;
526                  });
527                 
528                 
529             }
530
531             //listeners
532             this.el.script_dialog.connect( (dialog) => {
533                 if (this.el == null) {
534                     return true;
535                 }
536                 
537                  var msg = dialog.get_message();
538                  if (msg.length < 4) {
539                     return false;
540                  }
541                  if (msg.substring(0,4) != "IPC:") {
542                      return false;
543                  }
544                  var ar = msg.split(":", 3);
545                 if (ar.length < 3) {
546                     return false;
547                 }
548                 switch(ar[1]) {
549                     case "SAVEHTML":
550                         _this.file.saveHTML(ar[2]);
551                         return true;
552                     default:
553                         return false;
554                 }
555                 
556             });
557             this.el.show.connect( ( ) => {
558                 this.initInspector();;
559             });
560             this.el.drag_drop.connect( ( ctx, x, y,time, ud) => {
561                 return false;
562                 /*
563                 print("TARGET: drag-drop");
564                     var is_valid_drop_site = true;
565                     
566                      
567                     Gtk.drag_get_data
568                     (
569                             w,         // will receive 'drag-data-received' signal 
570                             ctx,        /* represents the current state of the DnD 
571                             this.get('/Window').atoms["STRING"],    /* the target type we want 
572                             time            /* time stamp 
573                     );
574                                     
575                                     
576                                     /* No target offered by source => error 
577                                    
578             
579                 return  is_valid_drop_site;
580                 */
581             });
582             this.el.load_changed.connect( (le) => {
583                 if (le != WebKit.LoadEvent.FINISHED) {
584                     return;
585                 }
586                 if (this.runjs.length < 1) {
587                     return;
588                 }
589               //  this.el.run_javascript(this.runjs, null);
590                  FakeServerCache.remove(    this.runjs);
591                 this.runjs = "";
592             });
593         }
594
595         // user defined functions
596         public void reInit () {
597            print("reInit?");
598                  // if this happens destroy the webkit..
599                  // recreate it..
600              this.el.stop_loading();
601                  
602              if (_this.viewbox.el.get_parent() == null) {
603                 return;
604              }
605                  
606                  
607             _this.viewbox.el.remove(_this.viewcontainer.el);
608             _this.el.remove(_this.inspectorcontainer.el);        
609                  
610                  // destory seems to cause problems.
611                  //this.el.destroy();
612                 //_this.viewcontainer.el.destroy();
613                  //_this.inspectorcontainer.el.destroy();
614              var  inv =new Xcls_inspectorcontainer(_this);
615               inv.ref();
616               _this.el.pack2(inv.el,true,true);
617               
618               
619              this.el = null;         
620              var nv =new Xcls_viewcontainer(_this);
621              nv.ref();
622              _this.viewbox.el.pack_end(nv.el,true,true,0);
623                  
624                  
625              inv.el.show_all();
626              nv.el.show_all();
627                  //while(Gtk.events_pending ()) Gtk.main_iteration ();
628                  //_this.view.renderJS(true); 
629              _this.view.refreshRequired  = true;
630         }
631         public void runRefresh () 
632         {
633             // this is run every 2 seconds from the init..
634         
635           
636             
637             if (!this.refreshRequired) {
638                // print("no refresh required");
639                 return;
640             }
641         
642             if (this.lastRedraw != null) {
643                // do not redraw if last redraw was less that 5 seconds ago.
644                if ((int64)(new DateTime.now_local()).difference(this.lastRedraw) < 5000 ) {
645                     return;
646                 }
647             }
648             
649             if (_this.file == null) {
650                 return;
651             }
652             
653             
654              this.refreshRequired = false;
655            //  print("HTML RENDERING");
656              
657              
658              //this.get('/BottomPane').el.show();
659              //this.get('/BottomPane').el.set_current_page(2);// webkit inspector
660             _this.file.webkit_page_id  = this.el.get_page_id();
661             
662             var js = _this.file.toSourcePreview();
663         
664             if (js.length < 1) {
665                 print("no data");
666                 return;
667             }
668         //    var  data = js[0];
669             this.redraws++;
670           
671             var project = _this.file.project;  
672         
673              //print (project.fn);
674              // set it to non-empty.
675              
676         //     runhtml = runhtml.length ?  runhtml : '<script type="text/javascript"></script>'; 
677         
678         
679         //   this.runhtml  = this.runhtml || '';
680          
681          
682             // then we need to reload the browser using
683             // load_html_string..
684         
685             // then trigger a redraw once it's loaded..
686             this.pendingRedraw = true;
687         
688             var runhtml = "<script type=\"text/javascript\">\n" ;
689             string builderhtml;
690             
691             try {
692                 GLib.FileUtils.get_contents(BuilderApplication.configDirectory() + "/resources/roo.builder.js", out builderhtml);
693             } catch (Error e) {
694                 builderhtml = "";
695             }
696         
697             runhtml += builderhtml + "\n";
698             runhtml += "</script>\n" ;
699         
700             // fix to make sure they are the same..
701             this.runhtml = project.runhtml;
702             // need to modify paths
703         
704             string inhtml;
705             var base_template = _this.file.project.base_template;
706             
707             if (base_template.length > 0 && !FileUtils.test(
708                 BuilderApplication.configDirectory() + "/resources/" +  base_template, FileTest.EXISTS)  
709                 ) {
710                    print("invalid base_template name - using default:  %s\n", base_template);
711                    base_template = "";
712             
713             }
714             try {
715                 GLib.FileUtils.get_contents(
716                     BuilderApplication.configDirectory() + "/resources/" + 
717                         (base_template.length > 0 ? base_template :  "roo.builder.html")
718                         , out inhtml);
719             
720             } catch (Error e) {
721                 inhtml = "";
722             }    
723             this.renderedData = js;
724         
725         
726             string js_src = js + "\n" +
727                 "Roo.onReady(function() {\n" +
728                 "if (" + _this.file.name +".show) " +  _this.file.name +".show({});\n" +
729                 "Roo.XComponent.build();\n" +
730                 "});\n";
731                 
732            // print("render js: " + js);
733             //if (!this.ready) {
734           //      console.log('not loaded yet');
735             //}
736             this.lastRedraw = new DateTime.now_local();
737         
738         
739             //this.runjs = js_src;
740             var fc =    FakeServerCache.factory_with_data(js_src);
741             this.runjs = fc.fname;
742             
743                 var html = inhtml.replace("</head>", runhtml + this.runhtml + 
744                     "<script type=\"text/javascript\" src=\"xhttp://localhost" + fc.fname + "\"></script>" +   
745                       //  "<script type=\"text/javascript\">\n" +
746                       //  js_src + "\n" + 
747                       //  "</script>" + 
748                                 
749                 "</head>");
750                 //print("LOAD HTML " + html);
751                 
752                  var rootURL = _this.file.project.rootURL;
753            
754                 
755                 
756                 this.el.load_html( html , 
757                     //fixme - should be a config option!
758                     (rootURL.length > 0 ? rootURL : "xhttp://localhost/app.Builder.js/")
759                 );
760                 
761             // force the inspector...        
762                //   this.initInspector();
763                 
764                 // - no need for this, the builder javascript will call it when build is complete
765                 //GLib.Timeout.add_seconds(1, () => {
766                 //    this.el.run_javascript("Builder.saveHTML()",null);
767                 //    return false;
768                 //});
769         //     print( "before render" +    this.lastRedraw);
770         //    print( "after render" +    (new Date()));
771             
772         }
773         public void initInspector () {
774             
775            /* if (this.inspector == this.el.get_inspector()) {
776                 this.inspector.show();
777                 this.inspector.open_window();        
778                 print("init inspecter called, and inspector is the same as existing\n");
779                 return;
780             }
781             print("new inspector?\n");
782         */
783             this.inspector = this.el.get_inspector();
784             this.inspector.ref();
785             
786             // got a new inspector...
787                 
788             this.inspector.open_window.connect(() => {
789                  this.inspector = this.el.get_inspector();
790                 print("inspector attach\n");
791                 var wv = this.inspector.get_web_view();
792                 if (wv != null) {
793                     print("got inspector web view\n");
794                     
795                     var cn = _this.inspectorcontainer.el.get_child();
796                     if (cn != null) {
797                          _this.inspectorcontainer.el.remove(cn);
798                      }
799                     
800                     _this.inspectorcontainer.el.add(wv);
801                     wv.show();
802                 } else {
803                     //this.inspector.close();
804                     
805                     //this.inspector = null;
806                    
807          
808                 }
809                 return true;
810                
811             });
812             /*
813             this.inspector.closed.connect(() => {
814                  print("inspector closed?!?");
815                  // if this happens destroy the webkit..
816                  // recreate it..
817                  this.el.stop_loading();
818                  
819                  if (_this.viewbox.el.get_parent() == null) {
820                     return;
821                  }
822                  
823                  
824                 _this.viewbox.el.remove(_this.viewcontainer.el);
825                 _this.el.remove(_this.inspectorcontainer.el);        
826                  
827                  // destory seems to cause problems.
828                  //this.el.destroy();
829                 //_this.viewcontainer.el.destroy();
830                  //_this.inspectorcontainer.el.destroy();
831         
832                  this.el = null;         
833                  var nv =new Xcls_viewcontainer(_this);
834                  nv.ref();
835                  _this.viewbox.el.pack_end(nv.el,true,true,0);
836                  
837                   var  inv =new Xcls_inspectorcontainer(_this);
838                   inv.ref();
839                   _this.el.pack2(inv.el,true,true);
840                  
841                  inv.el.show_all();
842                  nv.el.show_all();
843                  //while(Gtk.events_pending ()) Gtk.main_iteration ();
844                  //_this.view.renderJS(true); 
845                  _this.view.refreshRequired  = true;
846                
847             }); 
848             */
849             
850             this.inspector.show();
851         }
852         public void renderJS (bool force) {
853         
854             // this is the public redraw call..
855             // we refresh in a loop privately..
856             var autodraw = _this.AutoRedraw.el.active;
857             if (!autodraw && !force) {
858                 print("Skipping redraw - no force, and autodraw off");
859                 return;
860             }
861              
862             this.refreshRequired  = true;
863         }
864     }
865
866
867
868     public class Xcls_inspectorcontainer : Object
869     {
870         public Gtk.ScrolledWindow el;
871         private Xcls_WindowRooView  _this;
872
873
874             // my vars (def)
875
876         // ctor
877         public Xcls_inspectorcontainer(Xcls_WindowRooView _owner )
878         {
879             _this = _owner;
880             _this.inspectorcontainer = this;
881             this.el = new Gtk.ScrolledWindow( null, null );
882
883             // my vars (dec)
884
885             // set gobject values
886             this.el.shadow_type = Gtk.ShadowType.IN;
887
888             // init method
889
890             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
891         }
892
893         // user defined functions
894     }
895
896
897     public class Xcls_ScrolledWindow14 : Object
898     {
899         public Gtk.ScrolledWindow el;
900         private Xcls_WindowRooView  _this;
901
902
903             // my vars (def)
904
905         // ctor
906         public Xcls_ScrolledWindow14(Xcls_WindowRooView _owner )
907         {
908             _this = _owner;
909             this.el = new Gtk.ScrolledWindow( null, null );
910
911             // my vars (dec)
912
913             // set gobject values
914             var child_0 = new Xcls_sourceview( _this );
915             child_0.ref();
916             this.el.add (  child_0.el  );
917         }
918
919         // user defined functions
920     }
921     public class Xcls_sourceview : Object
922     {
923         public Gtk.SourceView el;
924         private Xcls_WindowRooView  _this;
925
926
927             // my vars (def)
928         public bool loading;
929         public bool allow_node_scroll;
930
931         // ctor
932         public Xcls_sourceview(Xcls_WindowRooView _owner )
933         {
934             _this = _owner;
935             _this.sourceview = this;
936             this.el = new Gtk.SourceView();
937
938             // my vars (dec)
939             this.loading = true;
940             this.allow_node_scroll = true;
941
942             // set gobject values
943             this.el.editable = false;
944             this.el.show_line_marks = true;
945             this.el.show_line_numbers = true;
946
947             // init method
948
949             {
950                
951                 var description =   Pango.FontDescription.from_string("monospace");
952                 description.set_size(8000);
953                 this.el.override_font(description);
954             
955                 this.loading = true;
956                 var buf = this.el.get_buffer();
957                 buf.notify.connect((ps) => {
958                     if (this.loading) {
959                         return;
960                     }
961                     if (ps.name != "cursor-position") {
962                         return;
963                     }
964                     print("cursor changed : %d\n", buf.cursor_position);
965                     Gtk.TextIter cpos;
966                     buf.get_iter_at_offset(out cpos, buf.cursor_position);
967                     
968                     var ln = cpos.get_line();
969              
970                     var node = _this.file.lineToNode(ln);
971              
972                     if (node == null) {
973                         print("can not find node\n");
974                         return;
975                     }
976                     var ltree = _this.main_window.windowstate.left_tree;
977                     var tp = ltree.model.treePathFromNode(node);
978                     print("got tree path %s\n", tp);
979                     if (tp != "") {
980                        this.allow_node_scroll = false;        
981                        print("changing cursor on tree..\n");
982                         ltree.view.el.set_cursor(new Gtk.TreePath.from_string(tp), null, false);
983                         // scrolling is disabled... as node selection calls scroll 10ms after it changes.
984                         GLib.Timeout.add_full(GLib.Priority.DEFAULT,100 , () => {
985                             this.allow_node_scroll = true;
986                             return false;
987                         });
988                     }
989                     
990                     // highlight the node..
991                     
992                 });
993               
994               
995               
996                 var attrs = new Gtk.SourceMarkAttributes();
997                 var  pink =   Gdk.RGBA();
998                 pink.parse ( "pink");
999                 attrs.set_background ( pink);
1000                 attrs.set_icon_name ( "process-stop");    
1001                 attrs.query_tooltip_text.connect(( mark) => {
1002                     //print("tooltip query? %s\n", mark.name);
1003                     return mark.name;
1004                 });
1005                 
1006                 this.el.set_mark_attributes ("ERR", attrs, 1);
1007                 
1008                  var wattrs = new Gtk.SourceMarkAttributes();
1009                 var  blue =   Gdk.RGBA();
1010                 blue.parse ( "#ABF4EB");
1011                 wattrs.set_background ( blue);
1012                 wattrs.set_icon_name ( "process-stop");    
1013                 wattrs.query_tooltip_text.connect(( mark) => {
1014                     //print("tooltip query? %s\n", mark.name);
1015                     return mark.name;
1016                 });
1017                 
1018                 this.el.set_mark_attributes ("WARN", wattrs, 1);
1019                 
1020              
1021                 
1022                  var dattrs = new Gtk.SourceMarkAttributes();
1023                 var  purple =   Gdk.RGBA();
1024                 purple.parse ( "#EEA9FF");
1025                 dattrs.set_background ( purple);
1026                 dattrs.set_icon_name ( "process-stop");    
1027                 dattrs.query_tooltip_text.connect(( mark) => {
1028                     //print("tooltip query? %s\n", mark.name);
1029                     return mark.name;
1030                 });
1031                 
1032                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
1033                 
1034                 
1035                 var gattrs = new Gtk.SourceMarkAttributes();
1036                 var  grey =   Gdk.RGBA();
1037                 grey.parse ( "#ccc");
1038                 gattrs.set_background ( grey);
1039              
1040                 
1041                 this.el.set_mark_attributes ("grey", gattrs, 1);
1042                 
1043                 
1044                 
1045                 
1046                 
1047                 
1048             }
1049         }
1050
1051         // user defined functions
1052         public void nodeSelected (JsRender.Node? sel) {
1053           
1054             
1055           
1056             // this is connected in widnowstate
1057             print("node selected\n");
1058             var buf = this.el.get_buffer();
1059          
1060             var sbuf = (Gtk.SourceBuffer) buf;
1061         
1062            
1063             while(Gtk.events_pending()) {
1064                 Gtk.main_iteration();
1065             }
1066             
1067            
1068             // clear all the marks..
1069              Gtk.TextIter start;
1070             Gtk.TextIter end;     
1071                 
1072             sbuf.get_bounds (out start, out end);
1073             sbuf.remove_source_marks (start, end, "grey");
1074             
1075             
1076              if (sel == null) {
1077                 // no highlighting..
1078                 return;
1079             }
1080             Gtk.TextIter iter;   
1081             sbuf.get_iter_at_line(out iter,  sel.line_start);
1082             
1083             
1084             Gtk.TextIter cur_iter;
1085             sbuf.get_iter_at_offset(out cur_iter, sbuf.cursor_position);
1086             
1087             //var cur_line = cur_iter.get_line();
1088             //if (cur_line > sel.line_start && cur_line < sel.line_end) {
1089             
1090             //} else {
1091             if (this.allow_node_scroll) {
1092                          
1093                 this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
1094                 }
1095             
1096              
1097             
1098             for (var i = 0; i < buf.get_line_count();i++) {
1099                 if (i < sel.line_start || i > sel.line_end) {
1100                    
1101                     sbuf.get_iter_at_line(out iter, i);
1102                     sbuf.create_source_mark(null, "grey", iter);
1103                     
1104                 }
1105             
1106             }
1107             
1108         
1109         }
1110         public string toString () {
1111            Gtk.TextIter s;
1112             Gtk.TextIter e;
1113             this.el.get_buffer().get_start_iter(out s);
1114             this.el.get_buffer().get_end_iter(out e);
1115             var ret = this.el.get_buffer().get_text(s,e,true);
1116             //print("TO STRING? " + ret);
1117             return ret;
1118         }
1119         public void loadFile ( ) {
1120             this.loading = true;
1121             var buf = this.el.get_buffer();
1122             buf.set_text("",0);
1123             var sbuf = (Gtk.SourceBuffer) buf;
1124         
1125             
1126         
1127             if (_this.file == null || _this.file.xtype != "Gtk") {
1128                 print("xtype != Gtk");
1129                 this.loading = false;
1130                 return;
1131             }
1132             
1133             var valafn = "";
1134               try {             
1135                    var  regex = new Regex("\\.bjs$");
1136                 
1137                  
1138                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
1139                  } catch (GLib.RegexError e) {
1140                      this.loading = false;
1141                     return;
1142                 }   
1143             
1144         
1145            if (!FileUtils.test(valafn,FileTest.IS_REGULAR) ) {
1146                 print("File path has no errors\n");
1147                 this.loading = false;
1148                 return  ;
1149             }
1150             
1151             string str;
1152             try {
1153             
1154                 GLib.FileUtils.get_contents (valafn, out str);
1155             } catch (Error e) {
1156                 this.loading = false;
1157                 return  ;
1158             }
1159         
1160         //    print("setting str %d\n", str.length);
1161             buf.set_text(str, str.length);
1162             var lm = Gtk.SourceLanguageManager.get_default();
1163              
1164             //?? is javascript going to work as js?
1165             
1166             ((Gtk.SourceBuffer)(buf)) .set_language(lm.get_language(_this.file.language));
1167           
1168             
1169             Gtk.TextIter start;
1170             Gtk.TextIter end;     
1171                 
1172             sbuf.get_bounds (out start, out end);
1173             sbuf.remove_source_marks (start, end, null); // remove all marks..
1174             
1175             
1176             if (_this.main_window.windowstate.last_compile_result != null) {
1177                 var obj = _this.main_window.windowstate.last_compile_result;
1178                 this.highlightErrorsJson("ERR", obj);
1179                 this.highlightErrorsJson("WARN", obj);
1180                 this.highlightErrorsJson("DEPR", obj);                  
1181             }
1182             //while (Gtk.events_pending()) {
1183              //   Gtk.main_iteration();
1184            // }
1185             
1186             this.loading = false; 
1187         }
1188         public void highlightErrorsJson (string type, Json.Object obj) {
1189               Gtk.TextIter start;
1190              Gtk.TextIter end;   
1191              
1192              var buf =  this.el.get_buffer();
1193                var sbuf = (Gtk.SourceBuffer)buf;
1194                 buf.get_bounds (out start, out end);
1195                 
1196                 sbuf.remove_source_marks (start, end, type);
1197                          
1198              
1199              // we should highlight other types of errors..
1200             
1201             if (!obj.has_member(type)) {
1202                 print("Return has no errors\n");
1203                 return  ;
1204             }
1205             var err = obj.get_object_member(type);
1206             
1207             if (_this.file == null) { 
1208                 return; // just in case the file has not loaded yet?
1209             }
1210          
1211         
1212             var valafn = "";
1213               try {             
1214                    var  regex = new Regex("\\.bjs$");
1215                 
1216                  
1217                     valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
1218                  } catch (GLib.RegexError e) {
1219                     return;
1220                 }   
1221         
1222            if (!err.has_member(valafn)) {
1223                 print("File path has no errors\n");
1224                 return  ;
1225             }
1226             var lines = err.get_object_member(valafn);
1227             
1228            
1229             
1230             var tlines = buf.get_line_count () +1;
1231             
1232             lines.foreach_member((obj, line, node) => {
1233                 
1234                      Gtk.TextIter iter;
1235             //        print("get inter\n");
1236                     var eline = int.parse(line) -1  ;
1237                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
1238                     
1239                     
1240                     if (eline > tlines || eline < 0) {
1241                         return;
1242                     }
1243                     sbuf.get_iter_at_line( out iter, eline);
1244                     //print("mark line\n");
1245                     var msg  = type + " on line: %d - %s".printf(eline+1, valafn);
1246                     var ar = lines.get_array_member(line);
1247                     for (var i = 0 ; i < ar.get_length(); i++) {
1248                             msg += (msg.length > 0) ? "\n" : "";
1249                             msg += ar.get_string_element(i);
1250                     }
1251                     
1252                     
1253                     sbuf.create_source_mark(msg, type, iter);
1254                 } );
1255                 return  ;
1256             
1257          
1258         
1259         
1260         }
1261     }
1262
1263
1264
1265 }