2 //valac --vapidir=./vapi --pkg glib-2.0 --pkg webkit-1.0 --pkg gtk+-2.0 wksnapshot.vala -o /tmp/wksnapshot
6 // gdk vapi class Pixmap:: public void get_size (out int width, out int height);
7 // gtk vapi widget get_snapshot (Rectangle? ....)
14 public class Browser : Window {
17 this.add(this.create_web_window());
18 this.resize(1320,1024);
19 this.destroy.connect(() => { Gtk.main_quit(); });
22 private ScrolledWindow create_web_window() {
23 var view = new WebView();
24 view.load_uri(Browser.opt_url);
26 var scrolled_window = new ScrolledWindow(null, null);
27 scrolled_window.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
28 scrolled_window.add(view);
31 view.load_finished.connect((le) => {
33 // resize the window...
34 // window.document.documentElement.scrollHeight
35 var scroll_height = (int) view.get_dom_document().document_element.scroll_height;
36 if (scroll_height> 1024) {
37 scrolled_window.set_size_request(1200, Int.min(scroll_height, 6000));
38 print("Resize to %d\n", scroll_height);
41 //var filename = "/tmp/test.pdf";
42 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
44 //if (le != LoadEvent.FINISHED) {
48 // what size is the documet.
50 GLib.Timeout.add_seconds(Browser.opt_delay, () => {
51 print("making screenshot\n");
54 var pixmap = view.get_snapshot( null );
59 pixmap.get_size( out w , out h);
60 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
61 print("pixbuf size: %d x %d\n ", w,h);
65 pixbuf.save(Browser.opt_target_png, "png");
66 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
67 // var sf = view.get_snapshot.end(res);
69 // sf.write_to_png(Browser.opt_target_png);
73 }, GLib.Priority.DEFAULT);
80 return scrolled_window;
83 public static string? opt_url = null;
84 public static uint opt_delay = 0;
85 public static string opt_target_png = null;
86 const OptionEntry[] options = {
89 { "url", 0, 0, OptionArg.STRING, ref opt_url, "Url to grab", null },
90 { "delay", 0, 0, OptionArg.INT, ref opt_delay, "Delay before snapshot in seconds", null },
91 { "png", 0, 0, OptionArg.STRING, ref opt_target_png, "File to write (PNG)", null },
94 public static int main(string[] args) {
98 var opt_context = new OptionContext ("wksnapshot");
102 opt_context.set_help_enabled (true);
103 opt_context.add_main_entries (options, null);
104 opt_context.parse (ref args);
106 if (Browser.opt_url == null) {
107 throw new OptionError.BAD_VALUE("missing url");
109 if (Browser.opt_target_png == null) {
110 throw new OptionError.BAD_VALUE("missing png");
113 } catch (OptionError e) {
114 stdout.printf ("error: %s\n", e.message);
115 stdout.printf ("Run '%s --help' to see a full list of available command line options.\n %s",
116 args[0], opt_context.get_help(true,null));
121 var browser = new Browser();