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( ( ) => {
140 this.el.load_finished.connect( ( ) => {
147 // user defined functions
148 public void printit () {
149 print("load_finsihed\n");
151 if (this.print_started) {
152 print("load_finsihed - already printing..\n");
156 this.print_started = true; // flag to stop double call.
159 // inject any javascript if needed..
160 if (webkitpdf.opt_inject_js != null) {
162 print("injecting javascript files \n");
163 foreach(var inject_js in webkitpdf.opt_inject_js) {
164 print("inject? %s\n", inject_js);
167 FileUtils.get_contents( inject_js, out str);
168 } catch(FileError e) {
169 print("Failed to inject %s\n", inject_js);
173 GLib.MainLoop mainloop = new GLib.MainLoop();
174 this.el.run_javascript.begin(str, null, (obj,res) => {
180 this.el.execute_script(str);
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, () => {
193 }, GLib.Priority.DEFAULT);
199 print("PNG not supported in webkit2\n");
203 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
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 , () => {
220 }, GLib.Priority.DEFAULT);
225 }, GLib.Priority.DEFAULT);
231 public bool printpdf () {
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);
247 var pe = new WebKit.PrintOperation(this.el);
248 pe.set_page_setup(psetup);
249 pe.finished.connect( () => {
250 print("print completed\n");
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);
261 pe.set_print_settings(ps);
268 var pe = new Gtk.PrintOperation();
269 pe.export_filename = webkitpdf.opt_target_pdf;
270 print("got callback for print\n");
272 /*pe.failed.connect(() => {
273 print("print failed\n");
278 pe.done.connect(() => {
279 print("print completed\n");
284 //pe.run_dialog(_this.el);
290 pe.set_default_page_setup(psetup);
291 print("Calling Print?\n");
292 //pe.run_dialog(_this.el);
300 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
309 public void printpng () {
310 //var filename = "/tmp/test.pdf";
311 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
313 //if (le != LoadEvent.FINISHED) {
317 // what size is the documet.
319 print("making screenshot\n");
321 // fix vapi - get_snapshot add '?' to all null.
322 var pixmap = _this.view.el.get_snapshot( null );
328 // add out to gdkpixmap.get_size
329 pixmap.get_size( out w , out h);
330 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
331 print("pixbuf size: %d x %d\n ", w,h);
335 pixbuf.save(webkitpdf.opt_target_png, "png");
336 print("Saved to %s\n", webkitpdf.opt_target_png);
337 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
338 // var sf = view.get_snapshot.end(res);
340 // sf.write_to_png(Browser.opt_target_png);