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