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 this.el.resize(1200,500);
41 print("loading url\n");
42 this.view.el.load_uri(webkitpdf.opt_url);
44 // if we have not finished in 25 seconds
46 GLib.Timeout.add_seconds(25, () => {
55 // user defined functions
56 public class Xcls_scrolled_window : Object
58 public Gtk.ScrolledWindow el;
59 private BrowserWindow _this;
65 public Xcls_scrolled_window(BrowserWindow _owner )
68 _this.scrolled_window = this;
69 this.el = new Gtk.ScrolledWindow( null, null );
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;
94 public bool print_started;
97 public Xcls_view(BrowserWindow _owner )
101 this.el = new WebKit.WebView();
104 this.print_started = false;
106 // set gobject values
109 this.el.mime_type_policy_decision_requested.connect( (p0, netreq, mimetype, pol ) => {
110 // print("resource response %s, %s\n", webres.get_mime_type(), netres.uri);
111 print("mimetype: %s", mimetype);
112 // netres.message.got_headers.connect(() => {
113 // print("GOT HEADERS? [%s] : %s\n",
114 // netres.message.request_headers.get_content_type(null),netres.uri);
117 return true; // default behaviour..
121 this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
122 print("resource req. started %s\n", netreq.uri);
125 this.el.load_finished.connect( ( ) => {
132 // user defined functions
133 public void printit () {
134 print("load_finsihed\n");
136 if (this.print_started) {
137 print("load_finsihed - already printing..\n");
141 this.print_started = true; // flag to stop double call.
144 // inject any javascript if needed..
145 if (webkitpdf.opt_inject_js != null) {
147 print("injecting javascript files \n");
148 foreach(var inject_js in webkitpdf.opt_inject_js) {
149 print("inject? %s\n", inject_js);
152 FileUtils.get_contents( inject_js, out str);
153 } catch(FileError e) {
154 print("Failed to inject %s\n", inject_js);
157 this.el.execute_script(str);
163 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
164 if (webkitpdf.opt_target_pdf != null) {
165 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
169 }, GLib.Priority.DEFAULT);
173 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
176 // ----------------- JPEG
177 // resize the window...
178 // window.document.documentElement.scrollHeight
179 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
180 print("Scroll height %d\n", scroll_height);
181 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
182 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
183 var h = int.min(scroll_height, 6000);
184 _this.scrolled_window.el.set_size_request( w, h);
185 _this.el.resize (w+50, h+50);
186 print("Resize to %d, %d\n", w,h);
187 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
190 }, GLib.Priority.DEFAULT);
195 }, GLib.Priority.DEFAULT);
201 public bool printpdf () {
203 var pe = new Gtk.PrintOperation();
204 pe.export_filename = webkitpdf.opt_target_pdf;
205 print("got callback for print");
207 /*pe.failed.connect(() => {
208 print("print failed\n");
213 pe.done.connect(() => {
214 print("print completed\n");
219 //pe.run_dialog(_this.el);
222 var list = Gtk.PaperSize.get_paper_sizes(false);
223 var psetup = new Gtk.PageSetup();
224 for(var i = 0; i < list.length(); i++ ) {
225 var entry = list.nth_data(i).copy();
226 if (entry.get_name() == "iso_a2") {
227 psetup.set_paper_size(entry);
233 pe.set_default_page_setup(psetup);
234 print("Calling Print?\n");
235 //pe.run_dialog(_this.el);
237 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
246 public void printpng () {
247 //var filename = "/tmp/test.pdf";
248 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
250 //if (le != LoadEvent.FINISHED) {
254 // what size is the documet.
256 print("making screenshot\n");
258 // fix vapi - get_snapshot add '?' to all null.
259 var pixmap = _this.view.el.get_snapshot( null );
265 // add out to gdkpixmap.get_size
266 pixmap.get_size( out w , out h);
267 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
268 print("pixbuf size: %d x %d\n ", w,h);
272 pixbuf.save(webkitpdf.opt_target_png, "png");
273 print("Saved to %s\n", webkitpdf.opt_target_png);
274 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
275 // var sf = view.get_snapshot.end(res);
277 // sf.write_to_png(Browser.opt_target_png);