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