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;
50 /* Do not redirect if the domain is .com already */
51 if (/\.blogspot\.com/.match(blog)) {
52 var r = new Regex ("\\.blogspot\\..*?/");
53 blog = r.replace(blog, blog.length, 0, ".blogspot.com/ncr/");
56 print("loading url %s\n",blog);
57 this.view.el.load_uri(blog);
59 // if we have not finished in 25 seconds
62 GLib.Timeout.add_seconds(1, () => {
64 print("timeout 25 seconds :%d", cd);
66 print("calling printit - timed out");
78 // user defined functions
79 public class Xcls_scrolled_window : Object
81 public Gtk.ScrolledWindow el;
82 private BrowserWindow _this;
88 public Xcls_scrolled_window(BrowserWindow _owner )
91 _this.scrolled_window = this;
92 this.el = new Gtk.ScrolledWindow( null, null );
97 var child_0 = new Xcls_view( _this );
99 this.el.add ( child_0.el );
104 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
108 // user defined functions
110 public class Xcls_view : Object
112 public WebKit.WebView el;
113 private BrowserWindow _this;
117 public bool print_started;
121 public Xcls_view(BrowserWindow _owner )
125 this.el = new WebKit.WebView();
128 this.print_started = false;
131 // set gobject values
139 this.el.load_changed.connect( (ev ) => {
140 if (ev != WebKit.LoadEvent.FINISHED) {
151 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
152 // print("resource req. started %s\n", netreq.uri);
156 this.el.load_finished.connect( ( ) => {
166 // user defined functions
167 public void printit () {
168 print("load_finsihed\n");
170 if (this.print_started) {
171 print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
180 this.print_started = true; // flag to stop double call.
183 // inject any javascript if needed..
184 if (webkitpdf.opt_inject_js != null) {
186 print("injecting javascript files \n");
187 foreach(var inject_js in webkitpdf.opt_inject_js) {
188 print("inject? %s\n", inject_js);
191 FileUtils.get_contents( inject_js, out str);
192 } catch(FileError e) {
193 print("Failed to inject %s\n", inject_js);
197 GLib.MainLoop mainloop = new GLib.MainLoop();
198 this.el.run_javascript.begin(str, null, (obj,res) => {
204 this.el.execute_script(str);
209 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
210 if (webkitpdf.opt_target_pdf != null) {
211 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
212 print("delay done URL: %s\n", this.el.uri);
217 }, GLib.Priority.DEFAULT);
223 print("PNG not supported in webkit2\n");
227 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
230 // ----------------- JPEG
231 // resize the window...
232 // window.document.documentElement.scrollHeight
233 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
234 print("Scroll height %d\n", scroll_height);
235 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
236 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
237 var h = int.min(scroll_height, 6000);
238 _this.scrolled_window.el.set_size_request( w, h);
239 _this.el.resize (w+50, h+50);
240 print("Resize to %d, %d\n", w,h);
241 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
244 }, GLib.Priority.DEFAULT);
249 }, GLib.Priority.DEFAULT);
255 public bool printpdf () {
259 var list = Gtk.PaperSize.get_paper_sizes(false);
260 var psetup = new Gtk.PageSetup();
261 for(var i = 0; i < list.length(); i++ ) {
262 var entry = list.nth_data(i).copy();
263 if (entry.get_name() == "iso_a2") {
264 psetup.set_paper_size(entry);
271 var pe = new WebKit.PrintOperation(this.el);
272 pe.set_page_setup(psetup);
273 pe.finished.connect( () => {
274 print("print completed\n");
279 var ps = new Gtk.PrintSettings();
280 ps.set_printer("Print to File");
281 ps.set("output-file-format", "pdf");
282 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
285 pe.set_print_settings(ps);
292 var pe = new Gtk.PrintOperation();
293 pe.export_filename = webkitpdf.opt_target_pdf;
294 print("got callback for print\n");
296 /*pe.failed.connect(() => {
297 print("print failed\n");
302 pe.done.connect(() => {
303 print("print completed\n");
308 //pe.run_dialog(_this.el);
314 pe.set_default_page_setup(psetup);
315 print("Calling Print?\n");
316 //pe.run_dialog(_this.el);
323 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
332 public void printpng () {
333 //var filename = "/tmp/test.pdf";
334 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
336 //if (le != LoadEvent.FINISHED) {
340 // what size is the documet.
342 print("making screenshot\n");
346 // fix vapi - get_snapshot add '?' to all null.
347 var pixmap = _this.view.el.get_snapshot( null );
353 // add out to gdkpixmap.get_size
354 pixmap.get_size( out w , out h);
355 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
356 print("pixbuf size: %d x %d\n ", w,h);
360 pixbuf.save(webkitpdf.opt_target_png, "png");
361 print("Saved to %s\n", webkitpdf.opt_target_png);
362 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
363 // var sf = view.get_snapshot.end(res);
365 // sf.write_to_png(Browser.opt_target_png);