MOVED webkit-1.0.deps to vapi/webkit-1.0.deps
[app.webkitpdf] / wksnapshot.vala
1
2 //valac --vapidir=./vapi --pkg glib-2.0 --pkg webkit-1.0 --pkg gtk+-2.0 wksnapshot.vala -o /tmp/wksnapshot
3
4 // not used?
5
6 // gdk vapi  class Pixmap::     public void get_size (out int width, out int height);
7 // gtk vapi  widget   get_snapshot (Rectangle? ....)
8
9
10 using GLib;
11 using Gtk;
12 using WebKit;
13  
14 public class Browser :  Window {
15    
16     public Browser() {
17         this.add(this.create_web_window());
18         this.resize(1320,1024);
19         this.destroy.connect(() => { Gtk.main_quit(); });
20     }
21  
22     private ScrolledWindow create_web_window() {
23         var view = new WebView();
24         view.load_uri(Browser.opt_url);
25  
26         var scrolled_window = new ScrolledWindow(null, null);
27         scrolled_window.set_policy(PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
28         scrolled_window.add(view);
29         
30
31         view.load_finished.connect((le) => {
32         
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);
39                         }
40         
41             //var filename = "/tmp/test.pdf";
42             //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
43
44             //if (le != LoadEvent.FINISHED) {
45             //    return;
46            // }
47            
48            // what size is the documet.
49            
50             GLib.Timeout.add_seconds(Browser.opt_delay, () => { 
51                 print("making screenshot\n");
52                  
53                 
54                 var pixmap = view.get_snapshot( null );
55  
56                 
57                 
58                 int w,h;
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);
62                                  
63                 
64                 
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);
68
69                 //    sf.write_to_png(Browser.opt_target_png);
70                 //});
71                 Gtk.main_quit();
72                 return false;
73             }, GLib.Priority.DEFAULT);
74             
75             
76    
77         });
78
79
80         return scrolled_window;
81     }
82  
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 = {
87                 
88                         
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 },
92                         { null }
93         };
94     public static int main(string[] args) {
95         Gtk.init(ref args);
96  
97  
98         var opt_context = new OptionContext ("wksnapshot");
99                 
100                 
101                 try {
102                         opt_context.set_help_enabled (true);
103                         opt_context.add_main_entries (options, null);
104                         opt_context.parse (ref args);
105                         
106             if (Browser.opt_url == null) {
107                 throw new OptionError.BAD_VALUE("missing url");
108             }
109                         if (Browser.opt_target_png == null) {
110                 throw new OptionError.BAD_VALUE("missing png");
111             }
112             
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));
117                         return 0;
118                 }
119  
120  
121         var browser = new Browser();
122         browser.show_all();
123  
124         Gtk.main();
125  
126         return 0;
127     }
128 }
129
130
131