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();
27 var child_0 = new Xcls_scrolled_window( _this );
29 this.el.add ( child_0.el );
32 this.el.destroy.connect( () => {
35 this.el.show.connect( () => {
37 print("resizing window\n");
38 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
39 var h = webkitpdf.opt_height > 0 ? webkitpdf.opt_height : 500;
43 print("loading url\n");
44 this.view.el.load_uri(webkitpdf.opt_url);
46 // if we have not finished in 25 seconds
48 GLib.Timeout.add_seconds(25, () => {
57 // user defined functions
58 public class Xcls_scrolled_window : Object
60 public Gtk.ScrolledWindow el;
61 private BrowserWindow _this;
67 public Xcls_scrolled_window(BrowserWindow _owner )
70 _this.scrolled_window = this;
71 this.el = new Gtk.ScrolledWindow();
74 var child_0 = new Xcls_view( _this );
76 this.el.add ( child_0.el );
81 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
85 // user defined functions
87 public class Xcls_view : Object
89 public WebKit.WebView el;
90 private BrowserWindow _this;
96 public Xcls_view(BrowserWindow _owner )
100 this.el = new WebKit.WebView();
105 this.el.mime_type_policy_decision_requested.connect( (p0, netreq, mimetype, pol ) => {
106 // print("resource response %s, %s\n", webres.get_mime_type(), netres.uri);
107 print("mimetype: %s", mimetype);
108 // netres.message.got_headers.connect(() => {
109 // print("GOT HEADERS? [%s] : %s\n",
110 // netres.message.request_headers.get_content_type(null),netres.uri);
113 return true; // default behaviour..
117 this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
118 print("resource req. started %s\n", netreq.uri);
121 this.el.load_finished.connect( ( ) => {
128 // user defined functions
129 public void printit () {
130 print("load_finsihed\n");
132 if (this.print_started) {
133 print("load_finsihed - already printing..\n");
137 this.print_started = true; // flag to stop double call.
140 // inject any javascript if needed..
141 if (webkitpdf.opt_inject_js != null) {
143 print("injecting javascript files \n");
144 foreach(var inject_js in webkitpdf.opt_inject_js) {
145 print("inject? %s\n", inject_js);
148 FileUtils.get_contents( inject_js, out str);
149 } catch(FileError e) {
150 print("Failed to inject %s\n", inject_js);
153 this.el.execute_script(str);
159 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
160 if (webkitpdf.opt_target_pdf != null) {
161 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
165 }, GLib.Priority.DEFAULT);
169 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
172 // ----------------- JPEG
173 // resize the window...
174 // window.document.documentElement.scrollHeight
175 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
176 print("Scroll height %d\n", scroll_height);
177 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
178 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
179 var h = int.min(scroll_height, 6000);
180 _this.scrolled_window.el.set_size_request( w, h);
181 _this.el.resize (w+50, h+50);
182 print("Resize to %d, %d\n", w,h);
183 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
186 }, GLib.Priority.DEFAULT);
191 }, GLib.Priority.DEFAULT);
197 public bool printpdf () {
199 var pe = new Gtk.PrintOperation();
200 pe.export_filename = webkitpdf.opt_target_pdf;
201 print("got callback for print");
203 /*pe.failed.connect(() => {
204 print("print failed\n");
209 pe.done.connect(() => {
210 print("print completed\n");
215 //pe.run_dialog(_this.el);
218 var list = Gtk.PaperSize.get_paper_sizes(false);
219 var psetup = new Gtk.PageSetup();
220 for(var i = 0; i < list.length(); i++ ) {
221 var entry = list.nth_data(i).copy();
222 if (entry.get_name() == "iso_a2") {
223 psetup.set_paper_size(entry);
229 pe.set_default_page_setup(psetup);
230 print("Calling Print?\n");
231 //pe.run_dialog(_this.el);
233 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
242 public void printpng () {
243 //var filename = "/tmp/test.pdf";
244 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
246 //if (le != LoadEvent.FINISHED) {
250 // what size is the documet.
252 print("making screenshot\n");
254 // fix vapi - get_snapshot add '?' to all null.
255 var pixmap = _this.view.el.get_snapshot( null );
261 // add out to gdkpixmap.get_size
262 pixmap.get_size( out w , out h);
263 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
264 print("pixbuf size: %d x %d\n ", w,h);
268 pixbuf.save(webkitpdf.opt_target_png, "png");
269 print("Saved to %s\n", webkitpdf.opt_target_png);
270 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
271 // var sf = view.get_snapshot.end(res);
273 // sf.write_to_png(Browser.opt_target_png);