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