BrowserWindow.bjs
[app.webkitpdf] / BrowserWindow.vala
1 static BrowserWindow  _BrowserWindow;
2
3 public class BrowserWindow : Object
4 {
5     public Gtk.Window el;
6     private BrowserWindow  _this;
7
8     public static BrowserWindow singleton()
9     {
10         if (_BrowserWindow == null) {
11             _BrowserWindow= new BrowserWindow();
12         }
13         return _BrowserWindow;
14     }
15     public Xcls_scrolled_window scrolled_window;
16     public Xcls_view view;
17
18         // my vars (def)
19
20     // ctor
21     public BrowserWindow()
22     {
23         _this = this;
24         this.el = new Gtk.Window( Gtk.WindowType.TOPLEVEL );
25
26         // my vars (dec)
27
28         // set gobject values
29         var child_0 = new Xcls_scrolled_window( _this );
30         child_0.ref();
31         this.el.add (  child_0.el  );
32
33         //listeners
34         this.el.destroy.connect( () => {
35             Gtk.main_quit();
36         });
37         this.el.show.connect( () => {
38                 
39                 print("resizing window\n");
40                 var w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;
41                 var h = webkitpdf.opt_height > 0 ?  webkitpdf.opt_height  :  500;
42                                 
43                 
44                 this.el.resize(w,h);
45                 print("loading url\n");
46                 this.view.el.load_uri(webkitpdf.opt_url);
47                 
48                 // if we have not finished in 25 seconds
49                 // call printit...
50                 var cd = 25;
51                   GLib.Timeout.add_seconds(1, () => { 
52                         cd--;
53                         print("timeout 25 seconds :%d", cd);
54                         if (cd < 1) {
55                                 print("calling printit - timed out", cd);
56                                 _this.view.printit();
57                                 return true;                    
58                         }
59                         return false;
60         
61                 });
62                 
63         
64         });
65     }
66
67     // user defined functions
68     public class Xcls_scrolled_window : Object
69     {
70         public Gtk.ScrolledWindow el;
71         private BrowserWindow  _this;
72
73
74             // my vars (def)
75
76         // ctor
77         public Xcls_scrolled_window(BrowserWindow _owner )
78         {
79             _this = _owner;
80             _this.scrolled_window = this;
81             this.el = new Gtk.ScrolledWindow( null, null );
82
83             // my vars (dec)
84
85             // set gobject values
86             var child_0 = new Xcls_view( _this );
87             child_0.ref();
88             this.el.add (  child_0.el  );
89
90             // init method
91
92             {
93                 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
94             }
95         }
96
97         // user defined functions
98     }
99     public class Xcls_view : Object
100     {
101         public WebKit.WebView el;
102         private BrowserWindow  _this;
103
104
105             // my vars (def)
106         public bool print_started;
107
108         // ctor
109         public Xcls_view(BrowserWindow _owner )
110         {
111             _this = _owner;
112             _this.view = this;
113             this.el = new WebKit.WebView();
114
115             // my vars (dec)
116             this.print_started = false;
117
118             // set gobject values
119
120             // init method
121
122             {
123             #if GTK3
124                          this.el.load_changed.connect( (ev ) => {
125                                         if (ev == WebKit.LoadEvent.FINISHED) {
126                                    this.printit(); 
127                                }
128                          
129                         });
130             
131             #else
132              //listeners
133                       //  this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
134                         //  print("resource req. started %s\n", netreq.uri);
135                              
136                       //  });
137                         
138                         this.el.load_finished.connect( ( ) => {
139                                          
140                                this.printit(); 
141                          
142                         });
143             #endif
144             }
145         }
146
147         // user defined functions
148         public void printit () {
149            print("load_finsihed\n");
150            
151            if (this.print_started) {
152               print("load_finsihed - already printing..\n");
153               return;
154           }
155            
156             this.print_started = true; // flag to stop double call.
157             
158             
159                 // inject any javascript if needed..
160                 if (webkitpdf.opt_inject_js != null) {
161                 
162                         print("injecting javascript files \n");
163                         foreach(var inject_js in  webkitpdf.opt_inject_js) {
164                         print("inject? %s\n", inject_js);
165                                 string str;
166                                 try {
167                                         FileUtils.get_contents( inject_js, out str);
168                                 } catch(FileError e) {
169                                         print("Failed to inject %s\n",  inject_js);
170                                         continue;
171                                 }
172         #if GTK3
173                                 GLib.MainLoop mainloop = new GLib.MainLoop();
174                                 this.el.run_javascript.begin(str, null, (obj,res) => {
175                                         mainloop.quit();
176                                 });
177                                 mainloop.run();
178                                 
179         #else                   
180                                 this.el.execute_script(str);
181         #endif
182                         }
183                 }
184                 
185                 
186                 
187                 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
188                 if (webkitpdf.opt_target_pdf != null) {
189                         GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
190         
191                                 this.printpdf();
192                                  return false;
193                         }, GLib.Priority.DEFAULT);
194                         return;
195                 }
196                         
197         
198         #if GTK3
199                 print("PNG not supported in webkit2\n");
200                 return;
201         #else
202                         
203             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
204            
205                         
206                         // ----------------- JPEG 
207                         // resize the window...
208                         // window.document.documentElement.scrollHeight
209                         var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
210                         print("Scroll height %d\n", scroll_height);
211                         if (scroll_height> 1024 || webkitpdf.opt_width > 0  || webkitpdf.opt_height > 0  ) {
212                                 var w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;
213                                 var h = int.min(scroll_height, 6000);
214                                 _this.scrolled_window.el.set_size_request( w, h); 
215                                 _this.el.resize (w+50, h+50);
216                                 print("Resize to %d, %d\n", w,h);
217                             GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { 
218                                         this.printpng();
219                                         return false;
220                                   }, GLib.Priority.DEFAULT);
221                             return false;
222                         }
223                         this.printpng();                
224                         return false;
225                   }, GLib.Priority.DEFAULT);
226         
227         
228         #endif    
229             
230         }
231         public bool printpdf () {
232         
233         
234         
235                         var list = Gtk.PaperSize.get_paper_sizes(false);
236                     var psetup = new Gtk.PageSetup();
237                     for(var i = 0; i < list.length(); i++ ) {
238                         var entry = list.nth_data(i).copy();
239                         if (entry.get_name() == "iso_a2") {
240                             psetup.set_paper_size(entry);
241                         }
242                     }
243                     
244         
245         #if GTK3         
246         
247                         var  pe = new  WebKit.PrintOperation(this.el);
248                         pe.set_page_setup(psetup);
249                         pe.finished.connect( () => {
250                                 print("print completed\n");
251                                  Gtk.main_quit();
252                         
253                         });
254                         
255                         var ps = new Gtk.PrintSettings();
256                     ps.set_printer("Print to File");
257                     ps.set("output-file-format", "pdf");
258                     ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
259         
260                         
261                         pe.set_print_settings(ps);
262                         
263         
264                         pe.print();
265                                   
266         
267         #else       
268                         var  pe = new  Gtk.PrintOperation();
269                         pe.export_filename = webkitpdf.opt_target_pdf;
270                         print("got callback for print\n");
271                         pe.ref();
272                         /*pe.failed.connect(() => {
273                                 print("print failed\n");
274                                  Gtk.main_quit();
275                         
276                         })
277                         */;
278                          pe.done.connect(() => {
279                                 print("print completed\n");
280                                  Gtk.main_quit();
281                         
282                         });
283                         
284                         //pe.run_dialog(_this.el);
285                  
286                         
287                         
288         
289         
290                     pe.set_default_page_setup(psetup);
291                     print("Calling Print?\n");
292                     //pe.run_dialog(_this.el);
293                     
294                     
295         
296         
297         
298                     try {
299                             this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
300                     } catch (Error e) {
301                                 // print failed...
302                         }
303         #endif      
304                     
305                      
306                         return false;
307         }
308         public void printpng () {
309             //var filename = "/tmp/test.pdf";
310             //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
311         
312             //if (le != LoadEvent.FINISHED) {
313             //    return;
314            // }
315            
316         // what size is the documet.
317         
318              print("making screenshot\n");
319         #if GTK3
320         
321         #else     
322             // fix vapi - get_snapshot add '?' to all null.
323             var pixmap = _this.view.el.get_snapshot( null );
324         
325             
326             
327             int w,h;
328             
329             // add out to gdkpixmap.get_size
330                 pixmap.get_size( out  w , out   h);
331                 var pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
332                 print("pixbuf size: %d x %d\n ", w,h);
333                  
334             
335             
336              pixbuf.save(webkitpdf.opt_target_png, "png");
337              print("Saved to %s\n", webkitpdf.opt_target_png);
338             //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
339             //    var sf = view.get_snapshot.end(res);
340         
341             //    sf.write_to_png(Browser.opt_target_png);
342             //});
343             Gtk.main_quit();
344              
345         #endif   
346             
347             
348         
349         }
350     }
351
352
353 }