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