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.VPaned 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_viewbox viewbox;
16     public Xcls_AutoRedraw AutoRedraw;
17     public Xcls_viewcontainer viewcontainer;
18     public Xcls_view view;
19     public Xcls_inspectorcontainer inspectorcontainer;
20
21         // my vars (def)
22     public JsRender.JsRender file;
23
24     // ctor 
25     public Xcls_WindowRooView()
26     {
27         _this = this;
28         this.el = new Gtk.VPaned();
29
30         // my vars (dec)
31
32         // set gobject values
33         var child_0 = new Xcls_viewbox( _this );
34         child_0.ref();
35         this.el.pack1 (  child_0.el , true,true );
36         var child_1 = new Xcls_inspectorcontainer( _this );
37         child_1.ref();
38         this.el.pack2 (  child_1.el , true,true );
39     }
40
41     // user defined functions 
42     public void loadFile (JsRender.JsRender file)
43     {
44         this.file = file;
45         this.view.renderJS(true);
46     }
47     public void createThumb () {
48         
49         
50         if (this.file == null) {
51             return;
52         }
53         var filename = this.file.getIconFileName(false);
54         
55         var  win = this.el.get_parent_window();
56         var width = win.get_width();
57         var height = win.get_height();
58     
59         Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, width, this.el.position);
60     
61         screenshot.save(filename,"png");
62         return;
63         
64         
65         
66         
67         
68         
69         
70         var p = new WebKit.PrintOperation(_this.view.el);
71          
72         var ps = new Gtk.PrintSettings();
73         ps.set_printer("Print to File");
74         ps.set("output-file-format", "pdf");
75         ps.set("output-uri", "file://" + filename + ".pdf");
76     
77         // find the printer...
78         
79         /*
80         var ar = Gtk.PaperSize.get_paper_sizes(false);
81         var psetup = new Gtk.PageSetup();
82         for(var i = 0; i < ar.length(); i++) {
83             if (ar.nth_data(i).get_name() =="iso_a4") {
84                 psetup.set_paper_size(ar.nth_data(i));
85             }
86         }
87         psetup.set_orientation(Gtk.PageOrientation.LANDSCAPE);
88         
89          
90         p.set_page_setup(psetup);
91         */
92         p.set_print_settings(ps);
93         
94         p.finished.connect(() => {
95             print("creating thumbnail for " + filename + ".pdf\n"); 
96             var s = new Cairo.PdfSurface(filename + ".pdf", 400,400);
97         
98             s.write_to_png (filename);
99             
100            // var f = GLib.File.new_for_path (filename + ".pdf");
101            // f.delete();
102         });
103         
104         
105         p.print();
106         
107         // should we hold until it's printed...
108         
109           
110     
111         
112         
113     
114     
115         
116          
117     }
118     public void requestRedraw () {
119         this.view.renderJS(false);
120     }
121     public class Xcls_viewbox : Object 
122     {
123         public Gtk.VBox el;
124         private Xcls_WindowRooView  _this;
125
126
127             // my vars (def)
128
129         // ctor 
130         public Xcls_viewbox(Xcls_WindowRooView _owner )
131         {
132             _this = _owner;
133             _this.viewbox = this;
134             this.el = new Gtk.VBox( false, 0 );
135
136             // my vars (dec)
137
138             // set gobject values
139             var child_0 = new Xcls_HBox3( _this );
140             child_0.ref();
141             this.el.pack_start (  child_0.el , false,true,0 );
142             var child_1 = new Xcls_viewcontainer( _this );
143             child_1.ref();
144             this.el.pack_end (  child_1.el , true,true,0 );
145         }
146
147         // user defined functions 
148     }
149     public class Xcls_HBox3 : Object 
150     {
151         public Gtk.HBox el;
152         private Xcls_WindowRooView  _this;
153
154
155             // my vars (def)
156
157         // ctor 
158         public Xcls_HBox3(Xcls_WindowRooView _owner )
159         {
160             _this = _owner;
161             this.el = new Gtk.HBox( true, 0 );
162
163             // my vars (dec)
164
165             // set gobject values
166             this.el.height_request = 20;
167             this.el.vexpand = false;
168             var child_0 = new Xcls_Button4( _this );
169             child_0.ref();
170             this.el.pack_start (  child_0.el , false,false,0 );
171             var child_1 = new Xcls_AutoRedraw( _this );
172             child_1.ref();
173             this.el.pack_start (  child_1.el , false,false,0 );
174             var child_2 = new Xcls_Button6( _this );
175             child_2.ref();
176             this.el.pack_start (  child_2.el , false,false,0 );
177         }
178
179         // user defined functions 
180     }
181     public class Xcls_Button4 : Object 
182     {
183         public Gtk.Button el;
184         private Xcls_WindowRooView  _this;
185
186
187             // my vars (def)
188
189         // ctor 
190         public Xcls_Button4(Xcls_WindowRooView _owner )
191         {
192             _this = _owner;
193             this.el = new Gtk.Button();
194
195             // my vars (dec)
196
197             // set gobject values
198             this.el.label = "Redraw";
199
200             // listeners 
201             this.el.clicked.connect( ( ) => {
202                 _this.view.renderJS(  true);
203             });
204         }
205
206         // user defined functions 
207     }
208     public class Xcls_AutoRedraw : Object 
209     {
210         public Gtk.CheckButton el;
211         private Xcls_WindowRooView  _this;
212
213
214             // my vars (def)
215
216         // ctor 
217         public Xcls_AutoRedraw(Xcls_WindowRooView _owner )
218         {
219             _this = _owner;
220             _this.AutoRedraw = this;
221             this.el = new Gtk.CheckButton();
222
223             // my vars (dec)
224
225             // set gobject values
226             this.el.active = true;
227             this.el.label = "Auto Redraw On";
228
229             // listeners 
230             this.el.toggled.connect( (state) => {
231                 this.el.set_label(this.el.active  ? "Auto Redraw On" : "Auto Redraw Off");
232             });
233         }
234
235         // user defined functions 
236     }
237     public class Xcls_Button6 : Object 
238     {
239         public Gtk.Button el;
240         private Xcls_WindowRooView  _this;
241
242
243             // my vars (def)
244
245         // ctor 
246         public Xcls_Button6(Xcls_WindowRooView _owner )
247         {
248             _this = _owner;
249             this.el = new Gtk.Button();
250
251             // my vars (dec)
252
253             // set gobject values
254             this.el.label = "Full Redraw";
255
256             // listeners 
257             this.el.clicked.connect( () => {
258               _this.view.redraws = 99;
259                 _this.view.el.web_context.clear_cache();  
260               //_this.view.renderJS(true);
261               _this.view.reInit();
262             
263             });
264         }
265
266         // user defined functions 
267     }
268     public class Xcls_viewcontainer : Object 
269     {
270         public Gtk.ScrolledWindow el;
271         private Xcls_WindowRooView  _this;
272
273
274             // my vars (def)
275
276         // ctor 
277         public Xcls_viewcontainer(Xcls_WindowRooView _owner )
278         {
279             _this = _owner;
280             _this.viewcontainer = this;
281             this.el = new Gtk.ScrolledWindow( null, null );
282
283             // my vars (dec)
284
285             // set gobject values
286             this.el.shadow_type = Gtk.ShadowType.IN;
287             var child_0 = new Xcls_view( _this );
288             child_0.ref();
289             this.el.add (  child_0.el  );
290
291             // init method 
292
293             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
294         }
295
296         // user defined functions 
297     }
298     public class Xcls_view : Object 
299     {
300         public WebKit.WebView el;
301         private Xcls_WindowRooView  _this;
302
303
304             // my vars (def)
305         public string renderedData;
306         public bool refreshRequired;
307         public WebKit.WebInspector inspector;
308         public int redraws;
309         public GLib.DateTime lastRedraw;
310         public string runhtml;
311         public bool pendingRedraw;
312
313         // ctor 
314         public Xcls_view(Xcls_WindowRooView _owner )
315         {
316             _this = _owner;
317             _this.view = this;
318             this.el = new WebKit.WebView();
319
320             // my vars (dec)
321             this.renderedData = "";
322             this.refreshRequired = false;
323             this.redraws = 0;
324             this.lastRedraw = null;
325             this.runhtml = "";
326             this.pendingRedraw = false;
327
328             // set gobject values
329
330             // init method 
331
332             {
333                 // this may not work!?
334                 var settings =  this.el.get_settings();
335                 settings.enable_developer_extras = true;
336                 
337                 
338                 var fs= new FakeServer(this.el);
339                 fs.ref();
340                 // this was an attempt to change the url perms.. did not work..
341                 // settings.enable_file_access_from_file_uris = true;
342                 // settings.enable_offline_web_application_cache - true;
343                 // settings.enable_universal_access_from_file_uris = true;
344                
345                  
346                 
347                 
348                 
349             
350                  // FIXME - base url of script..
351                  // we need it so some of the database features work.
352                 this.el.load_html( "Render not ready" , 
353                         //fixme - should be a config option!
354                         // or should we catch stuff and fix it up..
355                         "http://localhost/app.Builder/"
356                 );
357                     
358                     
359                //this.el.open('file:///' + __script_path__ + '/../builder.html');
360                 /*
361                 Gtk.drag_dest_set
362                 (
363                         this.el,              //
364                         Gtk.DestDefaults.MOTION  | Gtk.DestDefaults.HIGHLIGHT,
365                         null,            // list of targets
366                         Gdk.DragAction.COPY         // what to do with data after dropped 
367                 );
368                                         
369                // print("RB: TARGETS : " + LeftTree.atoms["STRING"]);
370                 Gtk.drag_dest_set_target_list(this.el, this.get('/Window').targetList);
371                 */
372                 GLib.Timeout.add_seconds(1,  ()  =>{
373                      //print("run refresh?");
374                      if (this.el == null) {
375                         return false;
376                      }
377                      this.runRefresh(); 
378                      return true;
379                  });
380                 
381                 
382             }
383
384             // listeners 
385             this.el.script_dialog.connect( (dialog) => {
386                 if (this.el == null) {
387                     return true;
388                 }
389                 
390                  var msg = dialog.get_message();
391                  if (msg.length < 4) {
392                     return false;
393                  }
394                  if (msg.substring(0,4) != "IPC:") {
395                      return false;
396                  }
397                  var ar = msg.split(":", 3);
398                 if (ar.length < 3) {
399                     return false;
400                 }
401                 switch(ar[1]) {
402                     case "SAVEHTML":
403                         _this.file.saveHTML(ar[2]);
404                         return true;
405                     default:
406                         return false;
407                 }
408                 
409             });
410             this.el.show.connect( ( ) => {
411                 this.initInspector();;
412             });
413             this.el.drag_drop.connect( ( ctx, x, y,time, ud) => {
414                 return false;
415                 /*
416                 print("TARGET: drag-drop");
417                     var is_valid_drop_site = true;
418                     
419                      
420                     Gtk.drag_get_data
421                     (
422                             w,         // will receive 'drag-data-received' signal 
423                             ctx,        /* represents the current state of the DnD 
424                             this.get('/Window').atoms["STRING"],    /* the target type we want 
425                             time            /* time stamp 
426                     );
427                                     
428                                     
429                                     /* No target offered by source => error 
430                                    
431             
432                 return  is_valid_drop_site;
433                 */
434             });
435         }
436
437         // user defined functions 
438         public void reInit () {
439            print("reInit?");
440                  // if this happens destroy the webkit..
441                  // recreate it..
442              this.el.stop_loading();
443                  
444              if (_this.viewbox.el.get_parent() == null) {
445                 return;
446              }
447                  
448                  
449             _this.viewbox.el.remove(_this.viewcontainer.el);
450             _this.el.remove(_this.inspectorcontainer.el);        
451                  
452                  // destory seems to cause problems.
453                  //this.el.destroy();
454                 //_this.viewcontainer.el.destroy();
455                  //_this.inspectorcontainer.el.destroy();
456              var  inv =new Xcls_inspectorcontainer(_this);
457               inv.ref();
458               _this.el.pack2(inv.el,true,true);
459               
460               
461              this.el = null;         
462              var nv =new Xcls_viewcontainer(_this);
463              nv.ref();
464              _this.viewbox.el.pack_end(nv.el,true,true,0);
465                  
466                  
467              inv.el.show_all();
468              nv.el.show_all();
469                  //while(Gtk.events_pending ()) Gtk.main_iteration ();
470                  //_this.view.renderJS(true); 
471              _this.view.refreshRequired  = true;
472         }
473         public void runRefresh () 
474         {
475             // this is run every 2 seconds from the init..
476         
477           
478             
479             if (!this.refreshRequired) {
480                // print("no refresh required");
481                 return;
482             }
483         
484             if (this.lastRedraw != null) {
485                // do not redraw if last redraw was less that 5 seconds ago.
486                if ((int64)(new DateTime.now_local()).difference(this.lastRedraw) < 5000 ) {
487                     return;
488                 }
489             }
490             
491             if (_this.file == null) {
492                 return;
493             }
494             
495             
496              this.refreshRequired = false;
497            //  print("HTML RENDERING");
498              
499              
500              //this.get('/BottomPane').el.show();
501              //this.get('/BottomPane').el.set_current_page(2);// webkit inspector
502             _this.file.webkit_page_id  = this.el.get_page_id();
503             
504             var js = _this.file.toSourcePreview();
505         
506             if (js.length < 1) {
507                 print("no data");
508                 return;
509             }
510         //    var  data = js[0];
511             this.redraws++;
512           
513             var project = _this.file.project;  
514         
515              //print (project.fn);
516              // set it to non-empty.
517              
518         //     runhtml = runhtml.length ?  runhtml : '<script type="text/javascript"></script>'; 
519         
520         
521         //   this.runhtml  = this.runhtml || '';
522          
523          
524             // then we need to reload the browser using
525             // load_html_string..
526         
527             // then trigger a redraw once it's loaded..
528             this.pendingRedraw = true;
529         
530             var runhtml = "<script type=\"text/javascript\">\n" ;
531             string builderhtml;
532             
533             
534             GLib.FileUtils.get_contents(BuilderApplication.configDirectory() + "/resources/roo.builder.js", out builderhtml);
535         
536             runhtml += builderhtml + "\n";
537             runhtml += "</script>\n" ;
538         
539             // fix to make sure they are the same..
540             this.runhtml = project.runhtml;
541             // need to modify paths
542         
543             string inhtml;
544             var base_template = _this.file.project.base_template;
545             
546             if (base_template.length > 0 && !FileUtils.test(
547                 BuilderApplication.configDirectory() + "/resources/" +  base_template, FileTest.EXISTS)  
548                 ) {
549                    print("invalid base_template name - using default:  %s\n", base_template);
550                    base_template = "";
551             
552             }
553             
554             GLib.FileUtils.get_contents(
555                 BuilderApplication.configDirectory() + "/resources/" + 
556                     (base_template.length > 0 ? base_template :  "roo.builder.html")
557                     , out inhtml);
558             
559             
560             this.renderedData = js;
561         
562         
563             string js_src = js + "\n" +
564                 "Roo.onReady(function() {\n" +
565                 "if (" + _this.file.name +".show) " +  _this.file.name +".show({});\n" +
566                 "Roo.XComponent.build();\n" +
567                 "});\n";
568                 
569            // print("render js: " + js);
570             //if (!this.ready) {
571           //      console.log('not loaded yet');
572             //}
573             this.lastRedraw = new DateTime.now_local();
574         
575                 var html = inhtml.replace("</head>", runhtml + this.runhtml + 
576                 
577                         "<script type=\"text/javascript\">\n" +
578                         js_src + "\n" + 
579                         "</script>" + 
580                                 
581                 "</head>");
582                 //print("LOAD HTML " + html);
583                 
584                  var rootURL = _this.file.project.rootURL;
585            
586                 
587                 
588                 this.el.load_html( html , 
589                     //fixme - should be a config option!
590                     (rootURL.length > 0 ? rootURL : "xhttp://localhost/app.Builder.js/")
591                 );
592                 
593             // force the inspector...        
594                   this.initInspector();
595                 
596                 // - no need for this, the builder javascript will call it when build is complete
597                 //GLib.Timeout.add_seconds(1, () => {
598                 //    this.el.run_javascript("Builder.saveHTML()",null);
599                 //    return false;
600                 //});
601         //     print( "before render" +    this.lastRedraw);
602         //    print( "after render" +    (new Date()));
603             
604         }
605         public void initInspector () {
606             
607             if (this.inspector == this.el.get_inspector()) {
608                 this.inspector.show();
609                 this.inspector.open_window();        
610                 print("init inspecter called, and inspector is the same as existing\n");
611                 return;
612             }
613             print("new inspector?\n");
614         
615             this.inspector = this.el.get_inspector();
616             this.inspector.ref();
617             
618             // got a new inspector...
619                 
620             this.inspector.open_window.connect(() => {
621                  this.inspector = this.el.get_inspector();
622                 print("inspector attach\n");
623                 var wv = this.inspector.get_web_view();
624                 if (wv != null) {
625                     print("got inspector web view\n");
626                     
627                     var cn = _this.inspectorcontainer.el.get_child();
628                     if (cn != null) {
629                          _this.inspectorcontainer.el.remove(cn);
630                      }
631                     
632                     _this.inspectorcontainer.el.add(wv);
633                     wv.show();
634                 } else {
635                     //this.inspector.close();
636                     
637                     //this.inspector = null;
638                    
639          
640                 }
641                 return true;
642                
643             });
644             this.inspector.closed.connect(() => {
645                  print("inspector closed?!?");
646                  // if this happens destroy the webkit..
647                  // recreate it..
648                  this.el.stop_loading();
649                  
650                  if (_this.viewbox.el.get_parent() == null) {
651                     return;
652                  }
653                  
654                  
655                 _this.viewbox.el.remove(_this.viewcontainer.el);
656                 _this.el.remove(_this.inspectorcontainer.el);        
657                  
658                  // destory seems to cause problems.
659                  //this.el.destroy();
660                 //_this.viewcontainer.el.destroy();
661                  //_this.inspectorcontainer.el.destroy();
662         
663                  this.el = null;         
664                  var nv =new Xcls_viewcontainer(_this);
665                  nv.ref();
666                  _this.viewbox.el.pack_end(nv.el,true,true,0);
667                  
668                   var  inv =new Xcls_inspectorcontainer(_this);
669                   inv.ref();
670                   _this.el.pack2(inv.el,true,true);
671                  
672                  inv.el.show_all();
673                  nv.el.show_all();
674                  //while(Gtk.events_pending ()) Gtk.main_iteration ();
675                  //_this.view.renderJS(true); 
676                  _this.view.refreshRequired  = true;
677                
678             }); 
679             
680             this.inspector.show();
681         }
682         public void renderJS (bool force) {
683         
684             // this is the public redraw call..
685             // we refresh in a loop privately..
686             var autodraw = _this.AutoRedraw.el.active;
687             if (!autodraw && !force) {
688                 print("Skipping redraw - no force, and autodraw off");
689                 return;
690             }
691              
692             this.refreshRequired  = true;
693         }
694     }
695     public class Xcls_inspectorcontainer : Object 
696     {
697         public Gtk.ScrolledWindow el;
698         private Xcls_WindowRooView  _this;
699
700
701             // my vars (def)
702
703         // ctor 
704         public Xcls_inspectorcontainer(Xcls_WindowRooView _owner )
705         {
706             _this = _owner;
707             _this.inspectorcontainer = this;
708             this.el = new Gtk.ScrolledWindow( null, null );
709
710             // my vars (dec)
711
712             // set gobject values
713             this.el.shadow_type = Gtk.ShadowType.IN;
714
715             // init method 
716
717             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
718         }
719
720         // user defined functions 
721     }
722 }