BrowserWindow.vala
[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             
117           
118 #else
119             //listeners
120                         this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
121                           print("resource req. started %s\n", netreq.uri);
122                              
123                         });
124 #endif
125             }
126
127             //listeners
128             this.el.load_finished.connect( ( ) => {
129                          
130                    this.printit(); 
131              
132             });
133         }
134
135         // user defined functions
136         public void printit () {
137            print("load_finsihed\n");
138            
139            if (this.print_started) {
140               print("load_finsihed - already printing..\n");
141               return;
142           }
143            
144             this.print_started = true; // flag to stop double call.
145             
146             
147                 // inject any javascript if needed..
148                 if (webkitpdf.opt_inject_js != null) {
149                 
150                         print("injecting javascript files \n");
151                         foreach(var inject_js in  webkitpdf.opt_inject_js) {
152                         print("inject? %s\n", inject_js);
153                                 string str;
154                                 try {
155                                         FileUtils.get_contents( inject_js, out str);
156                                 } catch(FileError e) {
157                                         print("Failed to inject %s\n",  inject_js);
158                                         continue;
159                                 }
160                                 this.el.execute_script(str);
161                         }
162                 }
163                 
164                 
165                 
166                 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
167                 if (webkitpdf.opt_target_pdf != null) {
168                         GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
169         
170                                 this.printpdf();
171                                  return false;
172                         }, GLib.Priority.DEFAULT);
173                         return;
174                 }
175                         
176                         
177             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
178            
179                         
180                         // ----------------- JPEG 
181                         // resize the window...
182                         // window.document.documentElement.scrollHeight
183                         var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
184                         print("Scroll height %d\n", scroll_height);
185                         if (scroll_height> 1024 || webkitpdf.opt_width > 0  || webkitpdf.opt_height > 0  ) {
186                                 var w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;
187                                 var h = int.min(scroll_height, 6000);
188                                 _this.scrolled_window.el.set_size_request( w, h); 
189                                 _this.el.resize (w+50, h+50);
190                                 print("Resize to %d, %d\n", w,h);
191                             GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { 
192                                         this.printpng();
193                                         return false;
194                                   }, GLib.Priority.DEFAULT);
195                             return false;
196                         }
197                         this.printpng();                
198                         return false;
199                   }, GLib.Priority.DEFAULT);
200         
201         
202             
203             
204         }
205         public bool printpdf () {
206                  
207                         var  pe = new  Gtk.PrintOperation();
208                         pe.export_filename = webkitpdf.opt_target_pdf;
209                         print("got callback for print\n");
210                         pe.ref();
211                         /*pe.failed.connect(() => {
212                                 print("print failed\n");
213                                  Gtk.main_quit();
214                         
215                         })
216                         */;
217                          pe.done.connect(() => {
218                                 print("print completed\n");
219                                  Gtk.main_quit();
220                         
221                         });
222                         
223                         //pe.run_dialog(_this.el);
224                  
225                         
226                     var list = Gtk.PaperSize.get_paper_sizes(false);
227                     var psetup = new Gtk.PageSetup();
228                     for(var i = 0; i < list.length(); i++ ) {
229                         var entry = list.nth_data(i).copy();
230                         if (entry.get_name() == "iso_a2") {
231                             psetup.set_paper_size(entry);
232                         }
233                     }
234                         
235         
236         
237                     pe.set_default_page_setup(psetup);
238                     print("Calling Print?\n");
239                     //pe.run_dialog(_this.el);
240                     try {
241                             this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
242                     } catch (Error e) {
243                                 // print failed...
244                         }
245                     
246                     
247                      
248                         return false;
249         }
250         public void printpng () {
251             //var filename = "/tmp/test.pdf";
252             //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
253         
254             //if (le != LoadEvent.FINISHED) {
255             //    return;
256            // }
257            
258         // what size is the documet.
259         
260              print("making screenshot\n");
261              
262             // fix vapi - get_snapshot add '?' to all null.
263             var pixmap = _this.view.el.get_snapshot( null );
264         
265             
266             
267             int w,h;
268             
269             // add out to gdkpixmap.get_size
270                 pixmap.get_size( out  w , out   h);
271                 var pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
272                 print("pixbuf size: %d x %d\n ", w,h);
273                  
274             
275             
276              pixbuf.save(webkitpdf.opt_target_png, "png");
277              print("Saved to %s\n", webkitpdf.opt_target_png);
278             //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
279             //    var sf = view.get_snapshot.end(res);
280         
281             //    sf.write_to_png(Browser.opt_target_png);
282             //});
283             Gtk.main_quit();
284              
285            
286             
287             
288         
289         }
290     }
291
292
293 }