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