bbd43a043ad585a5767b2fcf4cf4a820397045bb
[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 string runjs;
309         public int redraws;
310         public GLib.DateTime lastRedraw;
311         public string runhtml;
312         public bool pendingRedraw;
313
314         // ctor 
315         public Xcls_view(Xcls_WindowRooView _owner )
316         {
317             _this = _owner;
318             _this.view = this;
319             this.el = new WebKit.WebView();
320
321             // my vars (dec)
322             this.renderedData = "";
323             this.refreshRequired = false;
324             this.runjs = "";
325             this.redraws = 0;
326             this.lastRedraw = null;
327             this.runhtml = "";
328             this.pendingRedraw = false;
329
330             // set gobject values
331
332             // init method 
333
334             {
335                 // this may not work!?
336                 var settings =  this.el.get_settings();
337                 settings.enable_developer_extras = true;
338                 
339                 
340                 var fs= new FakeServer(this.el);
341                 fs.ref();
342                 // this was an attempt to change the url perms.. did not work..
343                 // settings.enable_file_access_from_file_uris = true;
344                 // settings.enable_offline_web_application_cache - true;
345                 // settings.enable_universal_access_from_file_uris = true;
346                
347                  
348                 
349                 
350                 
351             
352                  // FIXME - base url of script..
353                  // we need it so some of the database features work.
354                 this.el.load_html( "Render not ready" , 
355                         //fixme - should be a config option!
356                         // or should we catch stuff and fix it up..
357                         "http://localhost/app.Builder/"
358                 );
359                     
360                     
361                //this.el.open('file:///' + __script_path__ + '/../builder.html');
362                 /*
363                 Gtk.drag_dest_set
364                 (
365                         this.el,              //
366                         Gtk.DestDefaults.MOTION  | Gtk.DestDefaults.HIGHLIGHT,
367                         null,            // list of targets
368                         Gdk.DragAction.COPY         // what to do with data after dropped 
369                 );
370                                         
371                // print("RB: TARGETS : " + LeftTree.atoms["STRING"]);
372                 Gtk.drag_dest_set_target_list(this.el, this.get('/Window').targetList);
373                 */
374                 GLib.Timeout.add_seconds(1,  ()  =>{
375                      //print("run refresh?");
376                      if (this.el == null) {
377                         return false;
378                      }
379                      this.runRefresh(); 
380                      return true;
381                  });
382                 
383                 
384             }
385
386             // listeners 
387             this.el.script_dialog.connect( (dialog) => {
388                 if (this.el == null) {
389                     return true;
390                 }
391                 
392                  var msg = dialog.get_message();
393                  if (msg.length < 4) {
394                     return false;
395                  }
396                  if (msg.substring(0,4) != "IPC:") {
397                      return false;
398                  }
399                  var ar = msg.split(":", 3);
400                 if (ar.length < 3) {
401                     return false;
402                 }
403                 switch(ar[1]) {
404                     case "SAVEHTML":
405                         _this.file.saveHTML(ar[2]);
406                         return true;
407                     default:
408                         return false;
409                 }
410                 
411             });
412             this.el.show.connect( ( ) => {
413                 this.initInspector();;
414             });
415             this.el.drag_drop.connect( ( ctx, x, y,time, ud) => {
416                 return false;
417                 /*
418                 print("TARGET: drag-drop");
419                     var is_valid_drop_site = true;
420                     
421                      
422                     Gtk.drag_get_data
423                     (
424                             w,         // will receive 'drag-data-received' signal 
425                             ctx,        /* represents the current state of the DnD 
426                             this.get('/Window').atoms["STRING"],    /* the target type we want 
427                             time            /* time stamp 
428                     );
429                                     
430                                     
431                                     /* No target offered by source => error 
432                                    
433             
434                 return  is_valid_drop_site;
435                 */
436             });
437             this.el.load_changed.connect( (le) => {
438                 if (le != WebKit.LoadEvent.FINISHED) {
439                     return;
440                 }
441                 if (this.runjs.length < 1) {
442                     return;
443                 }
444                 this.el.run_javascript(this.runjs, null);
445             });
446         }
447
448         // user defined functions 
449         public void reInit () {
450            print("reInit?");
451                  // if this happens destroy the webkit..
452                  // recreate it..
453              this.el.stop_loading();
454                  
455              if (_this.viewbox.el.get_parent() == null) {
456                 return;
457              }
458                  
459                  
460             _this.viewbox.el.remove(_this.viewcontainer.el);
461             _this.el.remove(_this.inspectorcontainer.el);        
462                  
463                  // destory seems to cause problems.
464                  //this.el.destroy();
465                 //_this.viewcontainer.el.destroy();
466                  //_this.inspectorcontainer.el.destroy();
467              var  inv =new Xcls_inspectorcontainer(_this);
468               inv.ref();
469               _this.el.pack2(inv.el,true,true);
470               
471               
472              this.el = null;         
473              var nv =new Xcls_viewcontainer(_this);
474              nv.ref();
475              _this.viewbox.el.pack_end(nv.el,true,true,0);
476                  
477                  
478              inv.el.show_all();
479              nv.el.show_all();
480                  //while(Gtk.events_pending ()) Gtk.main_iteration ();
481                  //_this.view.renderJS(true); 
482              _this.view.refreshRequired  = true;
483         }
484         public void runRefresh () 
485         {
486             // this is run every 2 seconds from the init..
487         
488           
489             
490             if (!this.refreshRequired) {
491                // print("no refresh required");
492                 return;
493             }
494         
495             if (this.lastRedraw != null) {
496                // do not redraw if last redraw was less that 5 seconds ago.
497                if ((int64)(new DateTime.now_local()).difference(this.lastRedraw) < 5000 ) {
498                     return;
499                 }
500             }
501             
502             if (_this.file == null) {
503                 return;
504             }
505             
506             
507              this.refreshRequired = false;
508            //  print("HTML RENDERING");
509              
510              
511              //this.get('/BottomPane').el.show();
512              //this.get('/BottomPane').el.set_current_page(2);// webkit inspector
513             _this.file.webkit_page_id  = this.el.get_page_id();
514             
515             var js = _this.file.toSourcePreview();
516         
517             if (js.length < 1) {
518                 print("no data");
519                 return;
520             }
521         //    var  data = js[0];
522             this.redraws++;
523           
524             var project = _this.file.project;  
525         
526              //print (project.fn);
527              // set it to non-empty.
528              
529         //     runhtml = runhtml.length ?  runhtml : '<script type="text/javascript"></script>'; 
530         
531         
532         //   this.runhtml  = this.runhtml || '';
533          
534          
535             // then we need to reload the browser using
536             // load_html_string..
537         
538             // then trigger a redraw once it's loaded..
539             this.pendingRedraw = true;
540         
541             var runhtml = "<script type=\"text/javascript\">\n" ;
542             string builderhtml;
543             
544             
545             GLib.FileUtils.get_contents(BuilderApplication.configDirectory() + "/resources/roo.builder.js", out builderhtml);
546         
547             runhtml += builderhtml + "\n";
548             runhtml += "</script>\n" ;
549         
550             // fix to make sure they are the same..
551             this.runhtml = project.runhtml;
552             // need to modify paths
553         
554             string inhtml;
555             var base_template = _this.file.project.base_template;
556             
557             if (base_template.length > 0 && !FileUtils.test(
558                 BuilderApplication.configDirectory() + "/resources/" +  base_template, FileTest.EXISTS)  
559                 ) {
560                    print("invalid base_template name - using default:  %s\n", base_template);
561                    base_template = "";
562             
563             }
564             
565             GLib.FileUtils.get_contents(
566                 BuilderApplication.configDirectory() + "/resources/" + 
567                     (base_template.length > 0 ? base_template :  "roo.builder.html")
568                     , out inhtml);
569             
570             
571             this.renderedData = js;
572         
573         
574             string js_src = js + "\n" +
575                 "Roo.onReady(function() {\n" +
576                 "if (" + _this.file.name +".show) " +  _this.file.name +".show({});\n" +
577                 "Roo.XComponent.build();\n" +
578                 "});\n";
579                 
580            // print("render js: " + js);
581             //if (!this.ready) {
582           //      console.log('not loaded yet');
583             //}
584             this.lastRedraw = new DateTime.now_local();
585         
586             this.runjs = js_src;
587                 var html = inhtml.replace("</head>", runhtml + this.runhtml + 
588                 
589                       //  "<script type=\"text/javascript\">\n" +
590                       //  js_src + "\n" + 
591                       //  "</script>" + 
592                                 
593                 "</head>");
594                 //print("LOAD HTML " + html);
595                 
596                  var rootURL = _this.file.project.rootURL;
597            
598                 
599                 
600                 this.el.load_html( html , 
601                     //fixme - should be a config option!
602                     (rootURL.length > 0 ? rootURL : "xhttp://localhost/app.Builder.js/")
603                 );
604                 
605             // force the inspector...        
606                //   this.initInspector();
607                 
608                 // - no need for this, the builder javascript will call it when build is complete
609                 //GLib.Timeout.add_seconds(1, () => {
610                 //    this.el.run_javascript("Builder.saveHTML()",null);
611                 //    return false;
612                 //});
613         //     print( "before render" +    this.lastRedraw);
614         //    print( "after render" +    (new Date()));
615             
616         }
617         public void initInspector () {
618             
619            /* if (this.inspector == this.el.get_inspector()) {
620                 this.inspector.show();
621                 this.inspector.open_window();        
622                 print("init inspecter called, and inspector is the same as existing\n");
623                 return;
624             }
625             print("new inspector?\n");
626         */
627             this.inspector = this.el.get_inspector();
628             this.inspector.ref();
629             
630             // got a new inspector...
631                 
632             this.inspector.open_window.connect(() => {
633                  this.inspector = this.el.get_inspector();
634                 print("inspector attach\n");
635                 var wv = this.inspector.get_web_view();
636                 if (wv != null) {
637                     print("got inspector web view\n");
638                     
639                     var cn = _this.inspectorcontainer.el.get_child();
640                     if (cn != null) {
641                          _this.inspectorcontainer.el.remove(cn);
642                      }
643                     
644                     _this.inspectorcontainer.el.add(wv);
645                     wv.show();
646                 } else {
647                     //this.inspector.close();
648                     
649                     //this.inspector = null;
650                    
651          
652                 }
653                 return true;
654                
655             });
656             /*
657             this.inspector.closed.connect(() => {
658                  print("inspector closed?!?");
659                  // if this happens destroy the webkit..
660                  // recreate it..
661                  this.el.stop_loading();
662                  
663                  if (_this.viewbox.el.get_parent() == null) {
664                     return;
665                  }
666                  
667                  
668                 _this.viewbox.el.remove(_this.viewcontainer.el);
669                 _this.el.remove(_this.inspectorcontainer.el);        
670                  
671                  // destory seems to cause problems.
672                  //this.el.destroy();
673                 //_this.viewcontainer.el.destroy();
674                  //_this.inspectorcontainer.el.destroy();
675         
676                  this.el = null;         
677                  var nv =new Xcls_viewcontainer(_this);
678                  nv.ref();
679                  _this.viewbox.el.pack_end(nv.el,true,true,0);
680                  
681                   var  inv =new Xcls_inspectorcontainer(_this);
682                   inv.ref();
683                   _this.el.pack2(inv.el,true,true);
684                  
685                  inv.el.show_all();
686                  nv.el.show_all();
687                  //while(Gtk.events_pending ()) Gtk.main_iteration ();
688                  //_this.view.renderJS(true); 
689                  _this.view.refreshRequired  = true;
690                
691             }); 
692             */
693             
694             this.inspector.show();
695         }
696         public void renderJS (bool force) {
697         
698             // this is the public redraw call..
699             // we refresh in a loop privately..
700             var autodraw = _this.AutoRedraw.el.active;
701             if (!autodraw && !force) {
702                 print("Skipping redraw - no force, and autodraw off");
703                 return;
704             }
705              
706             this.refreshRequired  = true;
707         }
708     }
709     public class Xcls_inspectorcontainer : Object 
710     {
711         public Gtk.ScrolledWindow el;
712         private Xcls_WindowRooView  _this;
713
714
715             // my vars (def)
716
717         // ctor 
718         public Xcls_inspectorcontainer(Xcls_WindowRooView _owner )
719         {
720             _this = _owner;
721             _this.inspectorcontainer = this;
722             this.el = new Gtk.ScrolledWindow( null, null );
723
724             // my vars (dec)
725
726             // set gobject values
727             this.el.shadow_type = Gtk.ShadowType.IN;
728
729             // init method 
730
731             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
732         }
733
734         // user defined functions 
735     }
736 }