src/Builder4/WindowRooView.bjs
[app.Builder.js] / src / Builder4 / WindowRooView.vala
1 static Xcls_WindowRooView  _WindowRooView;
2
3 public class Xcls_WindowRooView : Object
4 {
5     public Gtk.Box el;
6     private Xcls_WindowRooView  _this;
7
8     public static Xcls_WindowRooView singleton()
9     {
10         if (_WindowRooView == null) {
11             _WindowRooView= new Xcls_WindowRooView();
12         }
13         return _WindowRooView;
14     }
15     public Xcls_notebook notebook;
16     public Xcls_label_preview label_preview;
17     public Xcls_label_code label_code;
18     public Xcls_paned paned;
19     public Xcls_viewbox viewbox;
20     public Xcls_AutoRedraw AutoRedraw;
21     public Xcls_viewcontainer viewcontainer;
22     public Xcls_view view;
23     public Xcls_inspectorcontainer inspectorcontainer;
24     public Xcls_sourceview sourceview;
25     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 propSelected;
937
938         // ctor
939         public Xcls_sourceview(Xcls_WindowRooView _owner )
940         {
941             _this = _owner;
942             _this.sourceview = this;
943             this.el = new Gtk.SourceView();
944
945             // my vars (dec)
946             this.loading = true;
947             this.allow_node_scroll = true;
948             this.propSelected = "";
949
950             // set gobject values
951             this.el.editable = false;
952             this.el.show_line_marks = true;
953             this.el.show_line_numbers = true;
954             var child_0 = new Xcls_buffer( _this );
955             child_0.ref();
956             this.el.set_buffer (  child_0.el  );
957
958             // init method
959
960             {
961                
962                 var description =   Pango.FontDescription.from_string("monospace");
963                 description.set_size(8000);
964                 this.el.override_font(description);
965             
966                 this.loading = true;
967                 //var buf = this.el.get_buffer();
968                 //buf.notify.connect(this.onCursorChanged);
969               
970               
971               
972                 var attrs = new Gtk.SourceMarkAttributes();
973                 var  pink =   Gdk.RGBA();
974                 pink.parse ( "pink");
975                 attrs.set_background ( pink);
976                 attrs.set_icon_name ( "process-stop");    
977                 attrs.query_tooltip_text.connect(( mark) => {
978                     //print("tooltip query? %s\n", mark.name);
979                     return mark.name;
980                 });
981                 
982                 this.el.set_mark_attributes ("ERR", attrs, 1);
983                 
984                  var wattrs = new Gtk.SourceMarkAttributes();
985                 var  blue =   Gdk.RGBA();
986                 blue.parse ( "#ABF4EB");
987                 wattrs.set_background ( blue);
988                 wattrs.set_icon_name ( "process-stop");    
989                 wattrs.query_tooltip_text.connect(( mark) => {
990                     //print("tooltip query? %s\n", mark.name);
991                     return mark.name;
992                 });
993                 
994                 this.el.set_mark_attributes ("WARN", wattrs, 1);
995                 
996              
997                 
998                  var dattrs = new Gtk.SourceMarkAttributes();
999                 var  purple =   Gdk.RGBA();
1000                 purple.parse ( "#EEA9FF");
1001                 dattrs.set_background ( purple);
1002                 dattrs.set_icon_name ( "process-stop");    
1003                 dattrs.query_tooltip_text.connect(( mark) => {
1004                     //print("tooltip query? %s\n", mark.name);
1005                     return mark.name;
1006                 });
1007                 
1008                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
1009                 
1010                 
1011                 var gattrs = new Gtk.SourceMarkAttributes();
1012                 var  grey =   Gdk.RGBA();
1013                 grey.parse ( "#ccc");
1014                 gattrs.set_background ( grey);
1015              
1016                 
1017                 this.el.set_mark_attributes ("grey", gattrs, 1);
1018                 
1019                 
1020                 
1021                 
1022                 
1023                 
1024             }
1025
1026             //listeners
1027             this.el.button_release_event.connect( () => {
1028                 this.onCursorChanged();
1029             
1030                 return false;
1031             });
1032         }
1033
1034         // user defined functions
1035         public void onCursorChanged (/*ParamSpec ps*/) {
1036                   if (this.loading) {
1037                     return;
1038                 }
1039                // if (ps.name != "cursor-position") {
1040                //     return;
1041                // }
1042         
1043                 var buf = this.el.get_buffer();
1044                 print("cursor changed : %d\n", buf.cursor_position);
1045                 Gtk.TextIter cpos;
1046                 buf.get_iter_at_offset(out cpos, buf.cursor_position);
1047                 
1048                 var ln = cpos.get_line();
1049                         print("cursor changed line : %d\n", ln);
1050                 var node = _this.file.lineToNode(ln+1);
1051          
1052                 if (node == null) {
1053                     print("can not find node\n");
1054                     return;
1055                 }
1056                 var prop = node.lineToProp(ln+1);
1057                 print("prop : %s", prop == null ? "???" : prop);
1058                 
1059                 
1060                 
1061                 
1062                 
1063                 
1064                 var ltree = _this.main_window.windowstate.left_tree;
1065                 var tp = ltree.model.treePathFromNode(node);
1066                 print("got tree path %s\n", tp);
1067                 if (tp != "") {
1068                        this.allow_node_scroll = false; /// block node scrolling..
1069                        
1070                        
1071                         //print("changing cursor on tree..\n");
1072                        
1073          
1074                     
1075                     // let's try allowing editing on the methods.
1076                     // a little klunky at present..
1077                     this.propSelected = "";
1078                     if (prop != null) {
1079                                 //see if we can find it..
1080                                 var kv = prop.split(":");
1081                                 if (kv[0] == "p") {
1082                                 
1083                                         //var k = prop.get_key(kv[1]);
1084                                         // fixme -- need to determine if it's an editable property...
1085                                         this.propSelected = prop;
1086                                         
1087                                 } else if (kv[0] == "l") {
1088                                          this.propSelected = prop;
1089                                         
1090                                 }
1091                     }
1092                     ltree.view.setCursor(tp, "editor");
1093                    // ltree.view.el.set_cursor(new Gtk.TreePath.from_string(tp), null, false); 
1094                    this.nodeSelected(node,false);
1095                     
1096                     // scrolling is disabled... as node selection calls scroll 10ms after it changes.
1097                     GLib.Timeout.add_full(GLib.Priority.DEFAULT,100 , () => {
1098                             this.allow_node_scroll = true;
1099                             return false;
1100                     });
1101                 }
1102                 
1103                 // highlight the node..
1104         }
1105         public void nodeSelected (JsRender.Node? sel, bool scroll ) {
1106           
1107             
1108           
1109             // this is connected in widnowstate
1110             print("node selected\n");
1111             var buf = this.el.get_buffer();
1112          
1113             var sbuf = (Gtk.SourceBuffer) buf;
1114         
1115            
1116             while(Gtk.events_pending()) {
1117                 Gtk.main_iteration();
1118             }
1119             
1120            
1121             // clear all the marks..
1122             Gtk.TextIter start;
1123             Gtk.TextIter end;     
1124                 
1125             sbuf.get_bounds (out start, out end);
1126             sbuf.remove_source_marks (start, end, "grey");
1127             
1128             
1129              if (sel == null) {
1130                      print("no selected node\n");
1131                 // no highlighting..
1132                 return;
1133             }
1134             
1135             print("highlight region %d to %d\n", sel.line_start,sel.line_end);
1136             Gtk.TextIter iter;   
1137             sbuf.get_iter_at_line(out iter,  sel.line_start);
1138             
1139             
1140             Gtk.TextIter cur_iter;
1141             sbuf.get_iter_at_offset(out cur_iter, sbuf.cursor_position);
1142             
1143             //var cur_line = cur_iter.get_line();
1144             //if (cur_line > sel.line_start && cur_line < sel.line_end) {
1145             
1146             //} else {
1147             if (scroll) {
1148                         print("scrolling to node -- should occur on node picking.\n");
1149                 this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
1150                 }
1151             
1152             var start_line = sel.line_start;
1153             var end_line = sel.line_end;
1154             
1155             
1156             this.el.editable = false;
1157             // now if we have selected a property...
1158             if (this.propSelected.length> 0 ) {
1159         
1160                         int nstart, nend;
1161                         if (sel.getPropertyRange(this.propSelected, out nstart, out nend) && nend > nstart) {
1162                                 start_line = nstart;
1163                                 end_line = nend;
1164                                 this.el.editable = true;
1165                         }
1166                         print("propSelected = %s range  %d -> %d\n", this.propSelected, start_line, end_line);          
1167                         
1168                         
1169             }
1170             
1171             // check selection - if it's out of 'bounds'
1172             if (this.el.editable && sbuf.get_has_selection()) {
1173                         Gtk.TextIter sel_start_iter, sel_end_iter;
1174                         sbuf.get_selection_bounds(out sel_start_iter, out sel_end_iter);
1175                         
1176                         if (sel_start_iter.get_line() < start_line || sel_end_iter.get_line() > end_line ||
1177                                 sel_start_iter.get_line() > end_line   || sel_end_iter.get_line() < start_line                  ) {
1178                                 // save?
1179                                 this.el.editable = false;
1180                         }
1181             
1182             }
1183             
1184             
1185             
1186             
1187             for (var i = 0; i < buf.get_line_count();i++) {
1188                 if (i < (start_line -1) || i > (end_line -1)) {
1189                    
1190                     sbuf.get_iter_at_line(out iter, i);
1191                     sbuf.create_source_mark(null, "grey", iter);
1192                     
1193                 }
1194             
1195             }
1196             
1197         
1198         }
1199         public string toString () {
1200            Gtk.TextIter s;
1201             Gtk.TextIter e;
1202             this.el.get_buffer().get_start_iter(out s);
1203             this.el.get_buffer().get_end_iter(out e);
1204             var ret = this.el.get_buffer().get_text(s,e,true);
1205             //print("TO STRING? " + ret);
1206             return ret;
1207         }
1208         public void loadFile ( ) {
1209             this.loading = true;
1210             
1211             
1212             // get the cursor and scroll position....
1213             var buf = this.el.get_buffer();
1214                 var cpos = buf.cursor_position;
1215             
1216            print("BEFORE LOAD cursor = %d\n", cpos);
1217            
1218             var vadj_pos = this.el.get_vadjustment().get_value();
1219            
1220             
1221          
1222             buf.set_text("",0);
1223             var sbuf = (Gtk.SourceBuffer) buf;
1224         
1225             
1226         
1227             if (_this.file == null || _this.file.xtype != "Roo") {
1228                 print("xtype != Roo");
1229                 this.loading = false;
1230                 return;
1231             }
1232             
1233             // get the string from the rendered tree...
1234              
1235              var str = _this.file.toSource();
1236              
1237         //    print("setting str %d\n", str.length);
1238             buf.set_text(str, str.length);
1239             var lm = Gtk.SourceLanguageManager.get_default();
1240              
1241             //?? is javascript going to work as js?
1242             
1243             ((Gtk.SourceBuffer)(buf)) .set_language(lm.get_language(_this.file.language));
1244           
1245             
1246             Gtk.TextIter start;
1247             Gtk.TextIter end;     
1248                 
1249             sbuf.get_bounds (out start, out end);
1250             sbuf.remove_source_marks (start, end, null); // remove all marks..
1251             
1252              GLib.Timeout.add(500, () => {
1253                
1254                print("RESORTING cursor to = %d\n", cpos);
1255                         Gtk.TextIter cpos_iter;
1256                         buf.get_iter_at_offset(out cpos_iter, cpos);
1257                         buf.place_cursor(cpos_iter); 
1258                         
1259                         this.el.get_vadjustment().set_value(vadj_pos);;
1260                         this.onCursorChanged();
1261                         _this.buffer.checkSyntax();
1262                         return false;
1263                 });
1264                         
1265             this.loading = false; 
1266             _this.buffer.dirty = false;
1267         }
1268         public void highlightErrorsJson (string type, Json.Object obj) {
1269                // this is a hook for the vala code - it has no value in javascript 
1270                // as we only have one error ususally....
1271                 return  ;
1272             
1273          
1274         
1275         
1276         }
1277     }
1278     public class Xcls_buffer : Object
1279     {
1280         public Gtk.SourceBuffer el;
1281         private Xcls_WindowRooView  _this;
1282
1283
1284             // my vars (def)
1285         public bool dirty;
1286         public int error_line;
1287
1288         // ctor
1289         public Xcls_buffer(Xcls_WindowRooView _owner )
1290         {
1291             _this = _owner;
1292             _this.buffer = this;
1293             this.el = new Gtk.SourceBuffer( null );
1294
1295             // my vars (dec)
1296             this.dirty = false;
1297             this.error_line = -1;
1298
1299             // set gobject values
1300
1301             //listeners
1302             this.el.changed.connect( () => {
1303                 // check syntax??
1304                 // ??needed..??
1305                // _this.save_button.el.sensitive = true;
1306                 ///?? has changed occured during loading?
1307                 if (_this.sourceview.loading) {
1308                         return;
1309                 }
1310                 
1311                 print("- PREVIEW EDITOR CHANGED--");
1312                 
1313                 
1314                  this.checkSyntax();
1315                
1316                 this.dirty = true;
1317             
1318             
1319                 return ;
1320             });
1321         }
1322
1323         // user defined functions
1324         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
1325                  
1326             this.error_line = validate_res.size;
1327                 
1328             if (this.error_line < 1) {
1329                   return true;
1330             }
1331             var tlines = this.el.get_line_count ();
1332             Gtk.TextIter iter;
1333             var valiter = validate_res.map_iterator();
1334             while (valiter.next()) {
1335             
1336         //        print("get inter\n");
1337                 var eline = valiter.get_key();
1338                 if (eline > tlines) {
1339                     continue;
1340                 }
1341                 this.el.get_iter_at_line( out iter, eline);
1342                 //print("mark line\n");
1343                 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
1344             }   
1345             return false;
1346         }
1347         public   string toString () {
1348             
1349             Gtk.TextIter s;
1350             Gtk.TextIter e;
1351             this.el.get_start_iter(out s);
1352             this.el.get_end_iter(out e);
1353             var ret = this.el.get_text(s,e,true);
1354             //print("TO STRING? " + ret);
1355             return ret;
1356         }
1357         public   bool checkSyntax () {
1358          
1359            
1360             var str = this.toString();
1361             
1362             // needed???
1363             if (this.error_line > 0) {
1364                  Gtk.TextIter start;
1365                  Gtk.TextIter end;     
1366                 this.el.get_bounds (out start, out end);
1367         
1368                 this.el.remove_source_marks (start, end, null);
1369             }
1370             
1371             if (str.length < 1) {
1372                 print("checkSyntax - empty string?\n");
1373                 return false;
1374             }
1375             
1376            if (_this.file == null) {
1377                return false;
1378            }
1379             var p = Palete.factory(_this.file.xtype);  // returns Roo | Gtk  | PlainFile 
1380             
1381          
1382             if (_this.file.language != "js") {
1383                         return false; // no syntax.
1384                 }
1385                 
1386                 
1387         
1388                 return this.highlightErrors(p.validateJavascript(
1389                 str, 
1390                  "", // _this.key, 
1391                 "file", //_this.ptype,
1392                 _this.file,
1393                 null
1394             ));    
1395              
1396         }
1397     }
1398
1399
1400
1401
1402 }