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