81f9653ea6074147876be7d0a60fa1778eccec6e
[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 createThumb () {
43         
44         
45         if (this.file == null) {
46             return;
47         }
48         var filename = this.file.getIconFileName(false);
49         
50         var  win = this.el.get_parent_window();
51         var width = win.get_width();
52         var height = win.get_height();
53     
54         Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, width, this.el.position);
55     
56         screenshot.save(filename,"png");
57         return;
58         
59         
60         
61         
62         
63         
64         
65         var p = new WebKit.PrintOperation(_this.view.el);
66          
67         var ps = new Gtk.PrintSettings();
68         ps.set_printer("Print to File");
69         ps.set("output-file-format", "pdf");
70         ps.set("output-uri", "file://" + filename + ".pdf");
71     
72         // find the printer...
73         
74         /*
75         var ar = Gtk.PaperSize.get_paper_sizes(false);
76         var psetup = new Gtk.PageSetup();
77         for(var i = 0; i < ar.length(); i++) {
78             if (ar.nth_data(i).get_name() =="iso_a4") {
79                 psetup.set_paper_size(ar.nth_data(i));
80             }
81         }
82         psetup.set_orientation(Gtk.PageOrientation.LANDSCAPE);
83         
84          
85         p.set_page_setup(psetup);
86         */
87         p.set_print_settings(ps);
88         
89         p.finished.connect(() => {
90             print("creating thumbnail for " + filename + ".pdf\n"); 
91             var s = new Cairo.PdfSurface(filename + ".pdf", 400,400);
92         
93             s.write_to_png (filename);
94             
95            // var f = GLib.File.new_for_path (filename + ".pdf");
96            // f.delete();
97         });
98         
99         
100         p.print();
101         
102         // should we hold until it's printed...
103         
104           
105     
106         
107         
108     
109     
110         
111          
112     }
113     public void loadFile (JsRender.JsRender file)
114     {
115         this.file = file;
116         this.view.renderJS(true);
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             
262             });
263         }
264
265         // user defined functions 
266     }
267     public class Xcls_viewcontainer : Object 
268     {
269         public Gtk.ScrolledWindow el;
270         private Xcls_WindowRooView  _this;
271
272
273             // my vars (def)
274
275         // ctor 
276         public Xcls_viewcontainer(Xcls_WindowRooView _owner )
277         {
278             _this = _owner;
279             _this.viewcontainer = this;
280             this.el = new Gtk.ScrolledWindow( null, null );
281
282             // my vars (dec)
283
284             // set gobject values
285             this.el.shadow_type = Gtk.ShadowType.IN;
286             var child_0 = new Xcls_view( _this );
287             child_0.ref();
288             this.el.add (  child_0.el  );
289
290             // init method 
291
292             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
293         }
294
295         // user defined functions 
296     }
297     public class Xcls_view : Object 
298     {
299         public WebKit.WebView el;
300         private Xcls_WindowRooView  _this;
301
302
303             // my vars (def)
304         public string renderedData;
305         public bool refreshRequired;
306         public WebKit.WebInspector inspector;
307         public int redraws;
308         public GLib.DateTime lastRedraw;
309         public string runhtml;
310         public bool pendingRedraw;
311
312         // ctor 
313         public Xcls_view(Xcls_WindowRooView _owner )
314         {
315             _this = _owner;
316             _this.view = this;
317             this.el = new WebKit.WebView();
318
319             // my vars (dec)
320             this.renderedData = "";
321             this.refreshRequired = false;
322             this.redraws = 0;
323             this.lastRedraw = null;
324             this.runhtml = "";
325             this.pendingRedraw = false;
326
327             // set gobject values
328
329             // init method 
330
331             {
332                 // this may not work!?
333                 var settings =  this.el.get_settings();
334                 settings.enable_developer_extras = true;
335                 
336                 
337                 var fs= new FakeServer(this.el);
338                 fs.ref();
339                 // this was an attempt to change the url perms.. did not work..
340                 // settings.enable_file_access_from_file_uris = true;
341                 // settings.enable_offline_web_application_cache - true;
342                 // settings.enable_universal_access_from_file_uris = true;
343                
344                  
345                 
346                 
347                 
348             
349                  // FIXME - base url of script..
350                  // we need it so some of the database features work.
351                 this.el.load_html( "Render not ready" , 
352                         //fixme - should be a config option!
353                         // or should we catch stuff and fix it up..
354                         "http://localhost/app.Builder/"
355                 );
356                     
357                     
358                //this.el.open('file:///' + __script_path__ + '/../builder.html');
359                 /*
360                 Gtk.drag_dest_set
361                 (
362                         this.el,              //
363                         Gtk.DestDefaults.MOTION  | Gtk.DestDefaults.HIGHLIGHT,
364                         null,            // list of targets
365                         Gdk.DragAction.COPY         // what to do with data after dropped 
366                 );
367                                         
368                // print("RB: TARGETS : " + LeftTree.atoms["STRING"]);
369                 Gtk.drag_dest_set_target_list(this.el, this.get('/Window').targetList);
370                 */
371                 GLib.Timeout.add_seconds(1,  ()  =>{
372                      //print("run refresh?");
373                      if (this.el == null) {
374                         return false;
375                      }
376                      this.runRefresh(); 
377                      return true;
378                  });
379                 
380                 
381             }
382
383             // listeners 
384             this.el.script_dialog.connect( (dialog) => {
385                 if (this.el == null) {
386                     return true;
387                 }
388                 
389                  var msg = dialog.get_message();
390                  if (msg.length < 4) {
391                     return false;
392                  }
393                  if (msg.substring(0,4) != "IPC:") {
394                      return false;
395                  }
396                  var ar = msg.split(":", 3);
397                 if (ar.length < 3) {
398                     return false;
399                 }
400                 switch(ar[1]) {
401                     case "SAVEHTML":
402                         _this.file.saveHTML(ar[2]);
403                         return true;
404                     default:
405                         return false;
406                 }
407                 
408             });
409             this.el.show.connect( ( ) => {
410                 this.initInspector();;
411             });
412             this.el.drag_drop.connect( ( ctx, x, y,time, ud) => {
413                 return false;
414                 /*
415                 print("TARGET: drag-drop");
416                     var is_valid_drop_site = true;
417                     
418                      
419                     Gtk.drag_get_data
420                     (
421                             w,         // will receive 'drag-data-received' signal 
422                             ctx,        /* represents the current state of the DnD 
423                             this.get('/Window').atoms["STRING"],    /* the target type we want 
424                             time            /* time stamp 
425                     );
426                                     
427                                     
428                                     /* No target offered by source => error 
429                                    
430             
431                 return  is_valid_drop_site;
432                 */
433             });
434         }
435
436         // user defined functions 
437         public void runRefresh () 
438         {
439             // this is run every 2 seconds from the init..
440         
441           
442             
443             if (!this.refreshRequired) {
444                // print("no refresh required");
445                 return;
446             }
447         
448             if (this.lastRedraw != null) {
449                // do not redraw if last redraw was less that 5 seconds ago.
450                if ((int64)(new DateTime.now_local()).difference(this.lastRedraw) < 5000 ) {
451                     return;
452                 }
453             }
454             
455             if (_this.file == null) {
456                 return;
457             }
458             
459             
460              this.refreshRequired = false;
461            //  print("HTML RENDERING");
462              
463              
464              //this.get('/BottomPane').el.show();
465              //this.get('/BottomPane').el.set_current_page(2);// webkit inspector
466             _this.file.webkit_page_id  = this.el.get_page_id();
467             
468             var js = _this.file.toSourcePreview();
469         
470             if (js.length < 1) {
471                 print("no data");
472                 return;
473             }
474         //    var  data = js[0];
475             this.redraws++;
476           
477             var project = _this.file.project;  
478         
479              //print (project.fn);
480              // set it to non-empty.
481              
482         //     runhtml = runhtml.length ?  runhtml : '<script type="text/javascript"></script>'; 
483         
484         
485         //   this.runhtml  = this.runhtml || '';
486          
487          
488             // then we need to reload the browser using
489             // load_html_string..
490         
491             // then trigger a redraw once it's loaded..
492             this.pendingRedraw = true;
493         
494             var runhtml = "<script type=\"text/javascript\">\n" ;
495             string builderhtml;
496             
497             
498             GLib.FileUtils.get_contents(BuilderApplication.configDirectory() + "/resources/roo.builder.js", out builderhtml);
499         
500             runhtml += builderhtml + "\n";
501             runhtml += "</script>\n" ;
502         
503             // fix to make sure they are the same..
504             this.runhtml = project.runhtml;
505             // need to modify paths
506         
507             string inhtml;
508             var base_template = _this.file.project.base_template;
509             
510             if (base_template.length > 0 && !FileUtils.test(
511                 BuilderApplication.configDirectory() + "/resources/" +  base_template, FileTest.EXISTS)  
512                 ) {
513                    print("invalid base_template name - using default:  %s\n", base_template);
514                    base_template = "";
515             
516             }
517             
518             GLib.FileUtils.get_contents(
519                 BuilderApplication.configDirectory() + "/resources/" + 
520                     (base_template.length > 0 ? base_template :  "roo.builder.html")
521                     , out inhtml);
522             
523             
524             this.renderedData = js;
525         
526         
527             string js_src = js + "\n" +
528                 "Roo.onReady(function() {\n" +
529                 "if (" + _this.file.name +".show) " +  _this.file.name +".show({});\n" +
530                 "Roo.XComponent.build();\n" +
531                 "});\n";
532                 
533            // print("render js: " + js);
534             //if (!this.ready) {
535           //      console.log('not loaded yet');
536             //}
537             this.lastRedraw = new DateTime.now_local();
538         
539                 var html = inhtml.replace("</head>", runhtml + this.runhtml + 
540                 
541                         "<script type=\"text/javascript\">\n" +
542                         js_src + "\n" + 
543                         "</script>" + 
544                                 
545                 "</head>");
546                 //print("LOAD HTML " + html);
547                 
548                  var rootURL = _this.file.project.rootURL;
549            
550                 
551                 
552                 this.el.load_html( html , 
553                     //fixme - should be a config option!
554                     (rootURL.length > 0 ? rootURL : "http://localhost/app.Builder/")
555                 );
556                 
557             // force the inspector...        
558                   this.initInspector();
559                 
560                 // - no need for this, the builder javascript will call it when build is complete
561                 //GLib.Timeout.add_seconds(1, () => {
562                 //    this.el.run_javascript("Builder.saveHTML()",null);
563                 //    return false;
564                 //});
565         //     print( "before render" +    this.lastRedraw);
566         //    print( "after render" +    (new Date()));
567             
568         }
569         public void initInspector () {
570             
571             if (this.inspector == this.el.get_inspector()) {
572                 this.inspector.show();
573                 this.inspector.open_window();        
574                 print("init inspecter called, and inspector is the same as existing\n");
575                 return;
576             }
577             print("new inspector?\n");
578         
579             this.inspector = this.el.get_inspector();
580             this.inspector.ref();
581             
582             // got a new inspector...
583                 
584             this.inspector.open_window.connect(() => {
585                  this.inspector = this.el.get_inspector();
586                 print("inspector attach\n");
587                 var wv = this.inspector.get_web_view();
588                 if (wv != null) {
589                     print("got inspector web view\n");
590                     _this.inspectorcontainer.el.add(wv);
591                     wv.show();
592                 } else {
593                     //this.inspector.close();
594                     
595                     //this.inspector = null;
596                    
597          
598                 }
599                 return true;
600                
601             });
602             this.inspector.closed.connect(() => {
603                  print("inspector closed?!?");
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.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         
621                  this.el = null;         
622                  var nv =new Xcls_viewcontainer(_this);
623                  nv.ref();
624                  _this.viewbox.el.pack_end(nv.el,true,true,0);
625                  
626                   var  inv =new Xcls_inspectorcontainer(_this);
627                   inv.ref();
628                   _this.el.pack2(inv.el,true,true);
629                  
630                  inv.el.show_all();
631                  nv.el.show_all();
632                  //while(Gtk.events_pending ()) Gtk.main_iteration ();
633                  //_this.view.renderJS(true); 
634                  _this.view.refreshRequired  = true;
635                
636             }); 
637             
638             this.inspector.show();
639         }
640         public void renderJS (bool force) {
641         
642             // this is the public redraw call..
643             // we refresh in a loop privately..
644             var autodraw = _this.AutoRedraw.el.active;
645             if (!autodraw && !force) {
646                 print("Skipping redraw - no force, and autodraw off");
647                 return;
648             }
649             this.refreshRequired  = true;
650         }
651     }
652     public class Xcls_inspectorcontainer : Object 
653     {
654         public Gtk.ScrolledWindow el;
655         private Xcls_WindowRooView  _this;
656
657
658             // my vars (def)
659
660         // ctor 
661         public Xcls_inspectorcontainer(Xcls_WindowRooView _owner )
662         {
663             _this = _owner;
664             _this.inspectorcontainer = this;
665             this.el = new Gtk.ScrolledWindow( null, null );
666
667             // my vars (dec)
668
669             // set gobject values
670             this.el.shadow_type = Gtk.ShadowType.IN;
671
672             // init method 
673
674             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
675         }
676
677         // user defined functions 
678     }
679 }