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, () => {
51 print("Calling printit - timeout 25 seconds");
60 // user defined functions
61 public class Xcls_scrolled_window : Object
63 public Gtk.ScrolledWindow el;
64 private BrowserWindow _this;
70 public Xcls_scrolled_window(BrowserWindow _owner )
73 _this.scrolled_window = this;
74 this.el = new Gtk.ScrolledWindow( null, null );
79 var child_0 = new Xcls_view( _this );
81 this.el.add ( child_0.el );
86 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
90 // user defined functions
92 public class Xcls_view : Object
94 public WebKit.WebView el;
95 private BrowserWindow _this;
99 public bool print_started;
102 public Xcls_view(BrowserWindow _owner )
106 this.el = new WebKit.WebView();
109 this.print_started = false;
111 // set gobject values
117 this.el.load_changed.connect( (ev ) => {
118 if (ev == WebKit.LoadEvent.FINISHED) {
126 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
127 // print("resource req. started %s\n", netreq.uri);
131 this.el.load_finished.connect( ( ) => {
140 // user defined functions
141 public void printit () {
142 print("load_finsihed\n");
144 if (this.print_started) {
145 print("load_finsihed - already printing..\n");
149 this.print_started = true; // flag to stop double call.
152 // inject any javascript if needed..
153 if (webkitpdf.opt_inject_js != null) {
155 print("injecting javascript files \n");
156 foreach(var inject_js in webkitpdf.opt_inject_js) {
157 print("inject? %s\n", inject_js);
160 FileUtils.get_contents( inject_js, out str);
161 } catch(FileError e) {
162 print("Failed to inject %s\n", inject_js);
166 GLib.MainLoop mainloop = new GLib.MainLoop();
167 this.el.run_javascript.begin(str, null, (obj,res) => {
173 this.el.execute_script(str);
180 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
181 if (webkitpdf.opt_target_pdf != null) {
182 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
186 }, GLib.Priority.DEFAULT);
192 print("PNG not supported in webkit2\n");
196 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
199 // ----------------- JPEG
200 // resize the window...
201 // window.document.documentElement.scrollHeight
202 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
203 print("Scroll height %d\n", scroll_height);
204 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
205 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
206 var h = int.min(scroll_height, 6000);
207 _this.scrolled_window.el.set_size_request( w, h);
208 _this.el.resize (w+50, h+50);
209 print("Resize to %d, %d\n", w,h);
210 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
213 }, GLib.Priority.DEFAULT);
218 }, GLib.Priority.DEFAULT);
224 public bool printpdf () {
228 var list = Gtk.PaperSize.get_paper_sizes(false);
229 var psetup = new Gtk.PageSetup();
230 for(var i = 0; i < list.length(); i++ ) {
231 var entry = list.nth_data(i).copy();
232 if (entry.get_name() == "iso_a2") {
233 psetup.set_paper_size(entry);
240 var pe = new WebKit.PrintOperation(this.el);
241 pe.set_page_setup(psetup);
242 pe.finished.connect( () => {
243 print("print completed\n");
248 var ps = new Gtk.PrintSettings();
249 ps.set_printer("Print to File");
250 ps.set("output-file-format", "pdf");
251 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
254 pe.set_print_settings(ps);
261 var pe = new Gtk.PrintOperation();
262 pe.export_filename = webkitpdf.opt_target_pdf;
263 print("got callback for print\n");
265 /*pe.failed.connect(() => {
266 print("print failed\n");
271 pe.done.connect(() => {
272 print("print completed\n");
277 //pe.run_dialog(_this.el);
283 pe.set_default_page_setup(psetup);
284 print("Calling Print?\n");
285 //pe.run_dialog(_this.el);
293 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
302 public void printpng () {
303 //var filename = "/tmp/test.pdf";
304 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
306 //if (le != LoadEvent.FINISHED) {
310 // what size is the documet.
312 print("making screenshot\n");
316 // fix vapi - get_snapshot add '?' to all null.
317 var pixmap = _this.view.el.get_snapshot( null );
323 // add out to gdkpixmap.get_size
324 pixmap.get_size( out w , out h);
325 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
326 print("pixbuf size: %d x %d\n ", w,h);
330 pixbuf.save(webkitpdf.opt_target_png, "png");
331 print("Saved to %s\n", webkitpdf.opt_target_png);
332 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
333 // var sf = view.get_snapshot.end(res);
335 // sf.write_to_png(Browser.opt_target_png);