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