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