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.mime_type_policy_decision_requested.connect( (p0, netreq, mimetype, pol ) => {
114 // print("resource response %s, %s\n", webres.get_mime_type(), netres.uri);
115 print("mimetype: %s", mimetype);
116 // netres.message.got_headers.connect(() => {
117 // print("GOT HEADERS? [%s] : %s\n",
118 // netres.message.request_headers.get_content_type(null),netres.uri);
121 return true; // default behaviour..
125 this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
126 print("resource req. started %s\n", netreq.uri);
129 this.el.load_finished.connect( ( ) => {
136 // user defined functions
137 public void printit () {
138 print("load_finsihed\n");
140 if (this.print_started) {
141 print("load_finsihed - already printing..\n");
145 this.print_started = true; // flag to stop double call.
148 // inject any javascript if needed..
149 if (webkitpdf.opt_inject_js != null) {
151 print("injecting javascript files \n");
152 foreach(var inject_js in webkitpdf.opt_inject_js) {
153 print("inject? %s\n", inject_js);
156 FileUtils.get_contents( inject_js, out str);
157 } catch(FileError e) {
158 print("Failed to inject %s\n", inject_js);
161 this.el.execute_script(str);
167 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
168 if (webkitpdf.opt_target_pdf != null) {
169 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
173 }, GLib.Priority.DEFAULT);
178 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
181 // ----------------- JPEG
182 // resize the window...
183 // window.document.documentElement.scrollHeight
184 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
185 print("Scroll height %d\n", scroll_height);
186 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
187 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
188 var h = int.min(scroll_height, 6000);
189 _this.scrolled_window.el.set_size_request( w, h);
190 _this.el.resize (w+50, h+50);
191 print("Resize to %d, %d\n", w,h);
192 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
195 }, GLib.Priority.DEFAULT);
200 }, GLib.Priority.DEFAULT);
206 public bool printpdf () {
208 var pe = new Gtk.PrintOperation();
209 pe.export_filename = webkitpdf.opt_target_pdf;
210 print("got callback for print\n");
212 /*pe.failed.connect(() => {
213 print("print failed\n");
218 pe.done.connect(() => {
219 print("print completed\n");
224 //pe.run_dialog(_this.el);
227 var list = Gtk.PaperSize.get_paper_sizes(false);
228 var psetup = new Gtk.PageSetup();
229 for(var i = 0; i < list.length(); i++ ) {
230 var entry = list.nth_data(i).copy();
231 if (entry.get_name() == "iso_a2") {
232 psetup.set_paper_size(entry);
238 pe.set_default_page_setup(psetup);
239 print("Calling Print?\n");
240 //pe.run_dialog(_this.el);
242 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
251 public void printpng () {
252 //var filename = "/tmp/test.pdf";
253 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
255 //if (le != LoadEvent.FINISHED) {
259 // what size is the documet.
261 print("making screenshot\n");
263 // fix vapi - get_snapshot add '?' to all null.
264 var pixmap = _this.view.el.get_snapshot( null );
270 // add out to gdkpixmap.get_size
271 pixmap.get_size( out w , out h);
272 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
273 print("pixbuf size: %d x %d\n ", w,h);
277 pixbuf.save(webkitpdf.opt_target_png, "png");
278 print("Saved to %s\n", webkitpdf.opt_target_png);
279 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
280 // var sf = view.get_snapshot.end(res);
282 // sf.write_to_png(Browser.opt_target_png);