2 # Copyright (C) 2006, Red Hat, Inc.
3 # Copyright (C) 2007, One Laptop Per Child
4 # Copyright (C) 2007 Jan Alonzo <jmalonzo@unpluggable.com>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 from gettext
import gettext
as _
27 from inspector
import Inspector
29 class WebToolbar(gtk
.Toolbar
):
31 def __init__(self
, browser
):
32 gtk
.Toolbar
.__init
__(self
)
34 self
._browser
= browser
36 # navigational buttons
37 self
._back
= gtk
.ToolButton(gtk
.STOCK_GO_BACK
)
38 self
._back
.set_tooltip(gtk
.Tooltips(), _('Back'))
39 self
._back
.props
.sensitive
= False
40 self
._back
.connect('clicked', self
._go
_back
_cb
)
41 self
.insert(self
._back
, -1)
43 self
._forward
= gtk
.ToolButton(gtk
.STOCK_GO_FORWARD
)
44 self
._forward
.set_tooltip(gtk
.Tooltips(), _('Forward'))
45 self
._forward
.props
.sensitive
= False
46 self
._forward
.connect('clicked', self
._go
_forward
_cb
)
47 self
.insert(self
._forward
, -1)
50 self
._stop
_and
_reload
= gtk
.ToolButton(gtk
.STOCK_REFRESH
)
51 self
._stop
_and
_reload
.set_tooltip(gtk
.Tooltips(),
52 _('Stop and reload current page'))
53 self
._stop
_and
_reload
.connect('clicked', self
._stop
_and
_reload
_cb
)
54 self
.insert(self
._stop
_and
_reload
, -1)
55 self
._stop
_and
_reload
.show()
58 self
.insert(gtk
.SeparatorToolItem(), -1)
61 self
._zoom
_in
= gtk
.ToolButton(gtk
.STOCK_ZOOM_IN
)
62 self
._zoom
_in
.set_tooltip(gtk
.Tooltips(), _('Zoom in'))
63 self
._zoom
_in
.connect('clicked', self
._zoom
_in
_cb
)
64 self
.insert(self
._zoom
_in
, -1)
67 self
._zoom
_out
= gtk
.ToolButton(gtk
.STOCK_ZOOM_OUT
)
68 self
._zoom
_out
.set_tooltip(gtk
.Tooltips(), _('Zoom out'))
69 self
._zoom
_out
.connect('clicked', self
._zoom
_out
_cb
)
70 self
.insert(self
._zoom
_out
, -1)
73 self
._zoom
_hundred
= gtk
.ToolButton(gtk
.STOCK_ZOOM_100
)
74 self
._zoom
_hundred
.set_tooltip(gtk
.Tooltips(), _('100% zoom'))
75 self
._zoom
_hundred
.connect('clicked', self
._zoom
_hundred
_cb
)
76 self
.insert(self
._zoom
_hundred
, -1)
77 self
._zoom
_hundred
.show()
79 self
.insert(gtk
.SeparatorToolItem(), -1)
82 self
._entry
= gtk
.Entry()
83 self
._entry
.connect('activate', self
._entry
_activate
_cb
)
84 self
._current
_uri
= None
86 entry_item
= gtk
.ToolItem()
87 entry_item
.set_expand(True)
88 entry_item
.add(self
._entry
)
91 self
.insert(entry_item
, -1)
94 # scale other content besides from text as well
95 self
._browser
.set_full_content_zoom(True)
97 self
._browser
.connect("title-changed", self
._title
_changed
_cb
)
99 def set_loading(self
, loading
):
100 self
._loading
= loading
103 self
._show
_stop
_icon
()
104 self
._stop
_and
_reload
.set_tooltip(gtk
.Tooltips(), _('Stop'))
106 self
._show
_reload
_icon
()
107 self
._stop
_and
_reload
.set_tooltip(gtk
.Tooltips(), _('Reload'))
108 self
._update
_navigation
_buttons
()
110 def _set_address(self
, address
):
111 self
._entry
.props
.text
= address
112 self
._current
_uri
= address
114 def _update_navigation_buttons(self
):
115 can_go_back
= self
._browser
.can_go_back()
116 self
._back
.props
.sensitive
= can_go_back
118 can_go_forward
= self
._browser
.can_go_forward()
119 self
._forward
.props
.sensitive
= can_go_forward
121 def _entry_activate_cb(self
, entry
):
122 self
._browser
.open(entry
.props
.text
)
124 def _go_back_cb(self
, button
):
125 self
._browser
.go_back()
127 def _go_forward_cb(self
, button
):
128 self
._browser
.go_forward()
130 def _title_changed_cb(self
, widget
, frame
, title
):
131 self
._set
_address
(frame
.get_uri())
133 def _stop_and_reload_cb(self
, button
):
135 self
._browser
.stop_loading()
137 self
._browser
.reload()
139 def _show_stop_icon(self
):
140 self
._stop
_and
_reload
.set_stock_id(gtk
.STOCK_CANCEL
)
142 def _show_reload_icon(self
):
143 self
._stop
_and
_reload
.set_stock_id(gtk
.STOCK_REFRESH
)
145 def _zoom_in_cb(self
, widget
):
146 """Zoom into the page"""
147 self
._browser
.zoom_in()
149 def _zoom_out_cb(self
, widget
):
150 """Zoom out of the page"""
151 self
._browser
.zoom_out()
153 def _zoom_hundred_cb(self
, widget
):
155 if not (self
._browser
.get_zoom_level() == 1.0):
156 self
._browser
.set_zoom_level(1.0)
159 class BrowserPage(webkit
.WebView
):
162 webkit
.WebView
.__init
__(self
)
163 settings
= self
.get_settings()
164 settings
.set_property("enable-developer-extras",
167 class WebStatusBar(gtk
.Statusbar
):
170 gtk
.Statusbar
.__init
__(self
)
171 self
.iconbox
= gtk
.EventBox()
172 self
.iconbox
.add(gtk
.image_new_from_stock(gtk
.STOCK_INFO
,
173 gtk
.ICON_SIZE_BUTTON
))
174 self
.pack_start(self
.iconbox
, False, False, 6)
175 self
.iconbox
.hide_all()
177 def display(self
, text
, context
=None):
178 cid
= self
.get_context_id("pywebkitgtk")
179 self
.push(cid
, str(text
))
181 def show_javascript_info(self
):
184 def hide_javascript_info(self
):
187 class WebBrowser(gtk
.Window
):
190 gtk
.Window
.__init
__(self
)
192 logging
.debug("initializing web browser window")
194 self
._loading
= False
195 self
._browser
= BrowserPage()
196 self
._browser
.connect('load-started', self
._loading
_start
_cb
)
197 self
._browser
.connect('load-progress-changed',
198 self
._loading
_progress
_cb
)
199 self
._browser
.connect('load-finished', self
._loading
_stop
_cb
)
200 self
._browser
.connect("title-changed", self
._title
_changed
_cb
)
201 self
._browser
.connect("hovering-over-link", self
._hover
_link
_cb
)
202 self
._browser
.connect("status-bar-text-changed",
203 self
._statusbar
_text
_changed
_cb
)
204 self
._browser
.connect("icon-loaded", self
._icon
_loaded
_cb
)
205 self
._browser
.connect("selection-changed", self
._selection
_changed
_cb
)
206 self
._browser
.connect("set-scroll-adjustments",
207 self
._set
_scroll
_adjustments
_cb
)
208 self
._browser
.connect("populate-popup", self
._populate
_popup
)
210 self
._browser
.connect("console-message",
211 self
._javascript
_console
_message
_cb
)
212 self
._browser
.connect("script-alert",
213 self
._javascript
_script
_alert
_cb
)
214 self
._browser
.connect("script-confirm",
215 self
._javascript
_script
_confirm
_cb
)
216 self
._browser
.connect("script-prompt",
217 self
._javascript
_script
_prompt
_cb
)
219 self
._inspector
= Inspector(self
._browser
.get_web_inspector())
221 self
._scrolled
_window
= gtk
.ScrolledWindow()
222 self
._scrolled
_window
.props
.hscrollbar_policy
= gtk
.POLICY_AUTOMATIC
223 self
._scrolled
_window
.props
.vscrollbar_policy
= gtk
.POLICY_AUTOMATIC
224 self
._scrolled
_window
.add(self
._browser
)
225 self
._scrolled
_window
.show_all()
227 self
._toolbar
= WebToolbar(self
._browser
)
229 self
._statusbar
= WebStatusBar()
231 vbox
= gtk
.VBox(spacing
=4)
232 vbox
.pack_start(self
._toolbar
, expand
=False, fill
=False)
233 vbox
.pack_start(self
._scrolled
_window
)
234 vbox
.pack_end(self
._statusbar
, expand
=False, fill
=False)
237 self
.set_default_size(600, 480)
239 self
.connect('destroy', gtk
.main_quit
)
242 <html><head><title>About</title></head><body>
243 <h1>Welcome to <code>webbrowser.py</code></h1>
244 <p><a href="http://live.gnome.org/PyWebKitGtk">Homepage</a></p>
247 self
._browser
.load_string(about
, "text/html", "iso-8859-15", "about:")
251 def _set_title(self
, title
):
252 self
.props
.title
= title
254 def _loading_start_cb(self
, view
, frame
):
255 main_frame
= self
._browser
.get_main_frame()
256 if frame
is main_frame
:
257 self
._set
_title
(_("Loading %s - %s") % (frame
.get_title(),
259 self
._toolbar
.set_loading(True)
261 def _loading_stop_cb(self
, view
, frame
):
262 # FIXME: another frame may still be loading?
263 self
._toolbar
.set_loading(False)
265 def _loading_progress_cb(self
, view
, progress
):
266 self
._set
_progress
(_("%s%s loaded") % (progress
, '%'))
268 def _set_progress(self
, progress
):
269 self
._statusbar
.display(progress
)
271 def _title_changed_cb(self
, widget
, frame
, title
):
272 self
._set
_title
(_("%s") % title
)
274 def _hover_link_cb(self
, view
, title
, url
):
276 self
._statusbar
.display(url
)
278 self
._statusbar
.display('')
280 def _statusbar_text_changed_cb(self
, view
, text
):
282 self
._statusbar
.display(text
)
284 def _icon_loaded_cb(self
):
287 def _selection_changed_cb(self
):
288 print "selection changed"
290 def _set_scroll_adjustments_cb(self
, view
, hadjustment
, vadjustment
):
291 if hadjustment
and vadjustment
:
292 print "horizontal adjustment: %d", hadjustment
.props
.value
293 print "vertical adjustmnet: %d", vadjustment
.props
.value
295 def _navigation_requested_cb(self
, view
, frame
, networkRequest
):
298 def _javascript_console_message_cb(self
, view
, message
, line
, sourceid
):
299 self
._statusbar
.show_javascript_info()
301 def _javascript_script_alert_cb(self
, view
, frame
, message
):
304 def _javascript_script_confirm_cb(self
, view
, frame
, message
, isConfirmed
):
307 def _javascript_script_prompt_cb(self
, view
, frame
,
308 message
, default
, text
):
311 def _populate_popup(self
, view
, menu
):
312 aboutitem
= gtk
.MenuItem(label
="About PyWebKit")
313 menu
.append(aboutitem
)
314 aboutitem
.connect('activate', self
._about
_pywebkitgtk
_cb
)
317 def _about_pywebkitgtk_cb(self
, widget
):
318 self
._browser
.open("http://live.gnome.org/PyWebKitGtk")
321 if __name__
== "__main__":
322 webbrowser
= WebBrowser()