1 static BrowserWindow _BrowserWindow;
3 public class BrowserWindow : Object
6 private BrowserWindow _this;
8 public static BrowserWindow singleton()
10 if (_BrowserWindow == null) {
11 _BrowserWindow= new BrowserWindow();
13 return _BrowserWindow;
15 public Xcls_scrolled_window scrolled_window;
16 public Xcls_view view;
21 public BrowserWindow()
24 this.el = new Gtk.Window( Gtk.WindowType.TOPLEVEL );
29 var child_0 = new Xcls_scrolled_window( _this );
31 this.el.add ( child_0.el );
34 this.el.destroy.connect( () => {
37 this.el.show.connect( () => {
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;
45 print("loading url\n");
46 this.view.el.load_uri(webkitpdf.opt_url);
48 // if we have not finished in 25 seconds
50 GLib.Timeout.add_seconds(25, () => {
59 // user defined functions
60 public class Xcls_scrolled_window : Object
62 public Gtk.ScrolledWindow el;
63 private BrowserWindow _this;
69 public Xcls_scrolled_window(BrowserWindow _owner )
72 _this.scrolled_window = this;
73 this.el = new Gtk.ScrolledWindow( null, null );
78 var child_0 = new Xcls_view( _this );
80 this.el.add ( child_0.el );
85 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
89 // user defined functions
91 public class Xcls_view : Object
93 public WebKit.WebView el;
94 private BrowserWindow _this;
98 public bool print_started;
101 public Xcls_view(BrowserWindow _owner )
105 this.el = new WebKit.WebView();
108 this.print_started = false;
110 // set gobject values
116 this.el.load_changed.connect( (ev ) => {
117 if (ev == WebKit.LoadEvent.FINISHED) {
125 this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
126 print("resource req. started %s\n", netreq.uri);
130 this.el.load_finished.connect( ( ) => {
139 // user defined functions
140 public void printit () {
141 print("load_finsihed\n");
143 if (this.print_started) {
144 print("load_finsihed - already printing..\n");
148 this.print_started = true; // flag to stop double call.
151 // inject any javascript if needed..
152 if (webkitpdf.opt_inject_js != null) {
154 print("injecting javascript files \n");
155 foreach(var inject_js in webkitpdf.opt_inject_js) {
156 print("inject? %s\n", inject_js);
159 FileUtils.get_contents( inject_js, out str);
160 } catch(FileError e) {
161 print("Failed to inject %s\n", inject_js);
165 GLib.MainLoop mainloop = new GLib.MainLoop();
166 this.el.run_javascript.begin(str, null, (obj,res) => {
172 this.el.execute_script(str);
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, () => {
185 }, GLib.Priority.DEFAULT);
191 print("PNG not supported in webkit2\n");
195 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
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 , () => {
212 }, GLib.Priority.DEFAULT);
217 }, GLib.Priority.DEFAULT);
223 public bool printpdf () {
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);
239 var pe = new WebKit.PrintOperation(this.el);
240 pe.set_page_setup(psetup);
241 pe.finished.connect( () => {
242 print("print completed\n");
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);
253 pe.set_print_settings(ps);
260 var pe = new Gtk.PrintOperation();
261 pe.export_filename = webkitpdf.opt_target_pdf;
262 print("got callback for print\n");
264 /*pe.failed.connect(() => {
265 print("print failed\n");
270 pe.done.connect(() => {
271 print("print completed\n");
276 //pe.run_dialog(_this.el);
282 pe.set_default_page_setup(psetup);
283 print("Calling Print?\n");
284 //pe.run_dialog(_this.el);
292 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
301 public void printpng () {
302 //var filename = "/tmp/test.pdf";
303 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
305 //if (le != LoadEvent.FINISHED) {
309 // what size is the documet.
311 print("making screenshot\n");
313 // fix vapi - get_snapshot add '?' to all null.
314 var pixmap = _this.view.el.get_snapshot( null );
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);
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);
332 // sf.write_to_png(Browser.opt_target_png);