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;
46 // we have to do silly stuff with blogspot!!?!?
47 var blog = webkitpdf.opt_url;
49 /* Do not redirect if the domain is .com already */
50 if (/\.blogspot\.com/.match(blog)) {
51 var r = new Regex ("\\.blogspot\\..*?/");
52 blog = r.replace(blog, blog.length, 0, ".blogspot.com/ncr/");
55 print("loading url %s\n",blog);
56 this.view.el.load_uri(blog);
58 // if we have not finished in 25 seconds
61 GLib.Timeout.add_seconds(1, () => {
63 print("timeout 25 seconds :%d", cd);
65 print("calling printit - timed out");
77 // user defined functions
78 public class Xcls_scrolled_window : Object
80 public Gtk.ScrolledWindow el;
81 private BrowserWindow _this;
87 public Xcls_scrolled_window(BrowserWindow _owner )
90 _this.scrolled_window = this;
91 this.el = new Gtk.ScrolledWindow( null, null );
96 var child_0 = new Xcls_view( _this );
98 this.el.add ( child_0.el );
103 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
107 // user defined functions
109 public class Xcls_view : Object
111 public WebKit.WebView el;
112 private BrowserWindow _this;
116 public bool print_started;
120 public Xcls_view(BrowserWindow _owner )
124 this.el = new WebKit.WebView();
127 this.print_started = false;
130 // set gobject values
138 this.el.load_changed.connect( (ev ) => {
139 if (ev != WebKit.LoadEvent.FINISHED) {
150 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
151 // print("resource req. started %s\n", netreq.uri);
155 this.el.load_finished.connect( ( ) => {
165 // user defined functions
166 public void printit () {
167 print("load_finsihed\n");
169 if (this.print_started) {
170 print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
179 this.print_started = true; // flag to stop double call.
182 // inject any javascript if needed..
183 if (webkitpdf.opt_inject_js != null) {
185 print("injecting javascript files \n");
186 foreach(var inject_js in webkitpdf.opt_inject_js) {
187 print("inject? %s\n", inject_js);
190 FileUtils.get_contents( inject_js, out str);
191 } catch(FileError e) {
192 print("Failed to inject %s\n", inject_js);
196 GLib.MainLoop mainloop = new GLib.MainLoop();
197 this.el.run_javascript.begin(str, null, (obj,res) => {
203 this.el.execute_script(str);
208 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
209 if (webkitpdf.opt_target_pdf != null) {
210 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
211 print("delay done URL: %s\n", this.el.uri);
216 }, GLib.Priority.DEFAULT);
222 print("PNG not supported in webkit2\n");
226 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
229 // ----------------- JPEG
230 // resize the window...
231 // window.document.documentElement.scrollHeight
232 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
233 print("Scroll height %d\n", scroll_height);
234 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
235 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
236 var h = int.min(scroll_height, 6000);
237 _this.scrolled_window.el.set_size_request( w, h);
238 _this.el.resize (w+50, h+50);
239 print("Resize to %d, %d\n", w,h);
240 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
243 }, GLib.Priority.DEFAULT);
248 }, GLib.Priority.DEFAULT);
254 public bool printpdf () {
258 var list = Gtk.PaperSize.get_paper_sizes(false);
259 var psetup = new Gtk.PageSetup();
260 for(var i = 0; i < list.length(); i++ ) {
261 var entry = list.nth_data(i).copy();
262 if (entry.get_name() == "iso_a2") {
263 psetup.set_paper_size(entry);
270 var pe = new WebKit.PrintOperation(this.el);
271 pe.set_page_setup(psetup);
272 pe.finished.connect( () => {
273 print("print completed\n");
278 var ps = new Gtk.PrintSettings();
279 ps.set_printer("Print to File");
280 ps.set("output-file-format", "pdf");
281 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
284 pe.set_print_settings(ps);
291 var pe = new Gtk.PrintOperation();
292 pe.export_filename = webkitpdf.opt_target_pdf;
293 print("got callback for print\n");
295 /*pe.failed.connect(() => {
296 print("print failed\n");
301 pe.done.connect(() => {
302 print("print completed\n");
307 //pe.run_dialog(_this.el);
313 pe.set_default_page_setup(psetup);
314 print("Calling Print?\n");
315 //pe.run_dialog(_this.el);
322 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
331 public void printpng () {
332 //var filename = "/tmp/test.pdf";
333 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
335 //if (le != LoadEvent.FINISHED) {
339 // what size is the documet.
341 print("making screenshot\n");
345 // fix vapi - get_snapshot add '?' to all null.
346 var pixmap = _this.view.el.get_snapshot( null );
352 // add out to gdkpixmap.get_size
353 pixmap.get_size( out w , out h);
354 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
355 print("pixbuf size: %d x %d\n ", w,h);
359 pixbuf.save(webkitpdf.opt_target_png, "png");
360 print("Saved to %s\n", webkitpdf.opt_target_png);
361 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
362 // var sf = view.get_snapshot.end(res);
364 // sf.write_to_png(Browser.opt_target_png);