| 1 | from gi.repository import WebKit |
|---|
| 2 | from gi.repository import Gtk |
|---|
| 3 | from gi.repository import Gdk |
|---|
| 4 | import cairo |
|---|
| 5 | |
|---|
| 6 | WINDOW_WIDTH, WINDOW_HEIGHT = 400, 300 |
|---|
| 7 | |
|---|
| 8 | def _destroy_cb(widget, data=None): |
|---|
| 9 | Gtk.main_quit() |
|---|
| 10 | |
|---|
| 11 | # The following two callbacks are never called: |
|---|
| 12 | |
|---|
| 13 | def _attributes_changed_cb(webview, viewport_attrs): |
|---|
| 14 | print "changed!" |
|---|
| 15 | print viewport_attrs |
|---|
| 16 | |
|---|
| 17 | def _attributes_recompute_cb(webview, viewport_attrs): |
|---|
| 18 | print "requested!" |
|---|
| 19 | print viewport_attrs |
|---|
| 20 | |
|---|
| 21 | window = Gtk.Window() |
|---|
| 22 | window.resize(WINDOW_WIDTH, WINDOW_HEIGHT) |
|---|
| 23 | window.connect("destroy", _destroy_cb) |
|---|
| 24 | window.show() |
|---|
| 25 | |
|---|
| 26 | s = Gtk.ScrolledWindow() |
|---|
| 27 | window.add(s) |
|---|
| 28 | s.show() |
|---|
| 29 | v = WebKit.WebView() |
|---|
| 30 | v.connect('viewport-attributes-changed', _attributes_changed_cb) |
|---|
| 31 | v.connect('viewport-attributes-recompute-requested', _attributes_recompute_cb) |
|---|
| 32 | s.add(v) |
|---|
| 33 | |
|---|
| 34 | # Trying to change enforce-96-dpi setting: |
|---|
| 35 | |
|---|
| 36 | settings = v.get_settings() |
|---|
| 37 | print "Previous enforce-96-dpi:", settings.get_property('enforce-96-dpi') |
|---|
| 38 | settings.set_property('enforce-96-dpi', False) |
|---|
| 39 | print "New enforce-96-dpi:", settings.get_property('enforce-96-dpi') |
|---|
| 40 | |
|---|
| 41 | # Trying to change some attributes: |
|---|
| 42 | |
|---|
| 43 | attributes = v.get_viewport_attributes() |
|---|
| 44 | print "current dpi", attributes.props.device_dpi |
|---|
| 45 | attributes.props.device_dpi = 134 # XO device DPI is 134 |
|---|
| 46 | print "current pixel ratio", attributes.props.device_pixel_ratio |
|---|
| 47 | attributes.props.device_width = 1200 |
|---|
| 48 | attributes.props.device_height = 900 |
|---|
| 49 | print "new dpi", attributes.props.device_dpi |
|---|
| 50 | |
|---|
| 51 | v.load_uri('http://google.com') |
|---|
| 52 | |
|---|
| 53 | # Changing the zoom level works: |
|---|
| 54 | #v.set_zoom_level(0.4) |
|---|
| 55 | |
|---|
| 56 | v.show() |
|---|
| 57 | |
|---|
| 58 | Gtk.main() |
|---|