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
113 this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
114 print("resource req. started %s\n", netreq.uri);
117 this.el.load_finished.connect( ( ) => {
124 // user defined functions
125 public void printit () {
126 print("load_finsihed\n");
128 if (this.print_started) {
129 print("load_finsihed - already printing..\n");
133 this.print_started = true; // flag to stop double call.
136 // inject any javascript if needed..
137 if (webkitpdf.opt_inject_js != null) {
139 print("injecting javascript files \n");
140 foreach(var inject_js in webkitpdf.opt_inject_js) {
141 print("inject? %s\n", inject_js);
144 FileUtils.get_contents( inject_js, out str);
145 } catch(FileError e) {
146 print("Failed to inject %s\n", inject_js);
149 this.el.execute_script(str);
155 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
156 if (webkitpdf.opt_target_pdf != null) {
157 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
161 }, GLib.Priority.DEFAULT);
166 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
169 // ----------------- JPEG
170 // resize the window...
171 // window.document.documentElement.scrollHeight
172 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
173 print("Scroll height %d\n", scroll_height);
174 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
175 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
176 var h = int.min(scroll_height, 6000);
177 _this.scrolled_window.el.set_size_request( w, h);
178 _this.el.resize (w+50, h+50);
179 print("Resize to %d, %d\n", w,h);
180 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
183 }, GLib.Priority.DEFAULT);
188 }, GLib.Priority.DEFAULT);
194 public bool printpdf () {
196 var pe = new Gtk.PrintOperation();
197 pe.export_filename = webkitpdf.opt_target_pdf;
198 print("got callback for print\n");
200 /*pe.failed.connect(() => {
201 print("print failed\n");
206 pe.done.connect(() => {
207 print("print completed\n");
212 //pe.run_dialog(_this.el);
215 var list = Gtk.PaperSize.get_paper_sizes(false);
216 var psetup = new Gtk.PageSetup();
217 for(var i = 0; i < list.length(); i++ ) {
218 var entry = list.nth_data(i).copy();
219 if (entry.get_name() == "iso_a2") {
220 psetup.set_paper_size(entry);
226 pe.set_default_page_setup(psetup);
227 print("Calling Print?\n");
228 //pe.run_dialog(_this.el);
230 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
239 public void printpng () {
240 //var filename = "/tmp/test.pdf";
241 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
243 //if (le != LoadEvent.FINISHED) {
247 // what size is the documet.
249 print("making screenshot\n");
251 // fix vapi - get_snapshot add '?' to all null.
252 var pixmap = _this.view.el.get_snapshot( null );
258 // add out to gdkpixmap.get_size
259 pixmap.get_size( out w , out h);
260 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
261 print("pixbuf size: %d x %d\n ", w,h);
265 pixbuf.save(webkitpdf.opt_target_png, "png");
266 print("Saved to %s\n", webkitpdf.opt_target_png);
267 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
268 // var sf = view.get_snapshot.end(res);
270 // sf.write_to_png(Browser.opt_target_png);