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 _
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
)
165 class WebStatusBar(gtk
.Statusbar
):
168 gtk
.Statusbar
.__init
__(self
)
169 self
.iconbox
= gtk
.EventBox()
170 self
.iconbox
.add(gtk
.image_new_from_stock(gtk
.STOCK_INFO
,
171 gtk
.ICON_SIZE_BUTTON
))
172 self
.pack_start(self
.iconbox
, False, False, 6)
173 self
.iconbox
.hide_all()
175 def display(self
, text
, context
=None):
176 cid
= self
.get_context_id("pywebkitgtk")
177 self
.push(cid
, str(text
))
179 def show_javascript_info(self
):
182 def hide_javascript_info(self
):
186 class WebBrowser(gtk
.Window
):
189 gtk
.Window
.__init
__(self
)
191 logging
.debug("initializing web browser window")
193 self
._loading
= False
194 self
._browser
= BrowserPage()
195 self
._browser
.connect('load-started', self
._loading
_start
_cb
)
196 self
._browser
.connect('load-progress-changed',
197 self
._loading
_progress
_cb
)
198 self
._browser
.connect('load-finished', self
._loading
_stop
_cb
)
199 self
._browser
.connect("title-changed", self
._title
_changed
_cb
)
200 self
._browser
.connect("hovering-over-link", self
._hover
_link
_cb
)
201 self
._browser
.connect("status-bar-text-changed",
202 self
._statusbar
_text
_changed
_cb
)
203 self
._browser
.connect("icon-loaded", self
._icon
_loaded
_cb
)
204 self
._browser
.connect("selection-changed", self
._selection
_changed
_cb
)
205 self
._browser
.connect("set-scroll-adjustments",
206 self
._set
_scroll
_adjustments
_cb
)
207 self
._browser
.connect("populate-popup", self
._populate
_popup
)
209 self
._browser
.connect("console-message",
210 self
._javascript
_console
_message
_cb
)
211 self
._browser
.connect("script-alert",
212 self
._javascript
_script
_alert
_cb
)
213 self
._browser
.connect("script-confirm",
214 self
._javascript
_script
_confirm
_cb
)
215 self
._browser
.connect("script-prompt",
216 self
._javascript
_script
_prompt
_cb
)
218 self
._scrolled
_window
= gtk
.ScrolledWindow()
219 self
._scrolled
_window
.props
.hscrollbar_policy
= gtk
.POLICY_AUTOMATIC
220 self
._scrolled
_window
.props
.vscrollbar_policy
= gtk
.POLICY_AUTOMATIC
221 self
._scrolled
_window
.add(self
._browser
)
222 self
._scrolled
_window
.show_all()
224 self
._toolbar
= WebToolbar(self
._browser
)
226 self
._statusbar
= WebStatusBar()
228 vbox
= gtk
.VBox(spacing
=4)
229 vbox
.pack_start(self
._toolbar
, expand
=False, fill
=False)
230 vbox
.pack_start(self
._scrolled
_window
)
231 vbox
.pack_end(self
._statusbar
, expand
=False, fill
=False)
234 self
.set_default_size(600, 480)
236 self
.connect('destroy', gtk
.main_quit
)
239 <html><head><title>About</title></head><body>
240 <h1>Welcome to <code>webbrowser.py</code></h1>
241 <p><a href="http://live.gnome.org/PyWebKitGtk">Homepage</a></p>
244 self
._browser
.load_string(about
, "text/html", "iso-8859-15", "about:")
248 def _set_title(self
, title
):
249 self
.props
.title
= title
251 def _loading_start_cb(self
, view
, frame
):
252 main_frame
= self
._browser
.get_main_frame()
253 if frame
is main_frame
:
254 self
._set
_title
(_("Loading %s - %s") % (frame
.get_title(),
256 self
._toolbar
.set_loading(True)
258 def _loading_stop_cb(self
, view
, frame
):
259 # FIXME: another frame may still be loading?
260 self
._toolbar
.set_loading(False)
262 def _loading_progress_cb(self
, view
, progress
):
263 self
._set
_progress
(_("%s%s loaded") % (progress
, '%'))
265 def _set_progress(self
, progress
):
266 self
._statusbar
.display(progress
)
268 def _title_changed_cb(self
, widget
, frame
, title
):
269 self
._set
_title
(_("%s") % title
)
271 def _hover_link_cb(self
, view
, title
, url
):
273 self
._statusbar
.display(url
)
275 self
._statusbar
.display('')
277 def _statusbar_text_changed_cb(self
, view
, text
):
279 self
._statusbar
.display(text
)
281 def _icon_loaded_cb(self
):
284 def _selection_changed_cb(self
):
285 print "selection changed"
287 def _set_scroll_adjustments_cb(self
, view
, hadjustment
, vadjustment
):
288 self
._scrolled
_window
.props
.hadjustment
= hadjustment
289 self
._scrolled
_window
.props
.vadjustment
= vadjustment
291 def _navigation_requested_cb(self
, view
, frame
, networkRequest
):
294 def _javascript_console_message_cb(self
, view
, message
, line
, sourceid
):
295 self
._statusbar
.show_javascript_info()
297 def _javascript_script_alert_cb(self
, view
, frame
, message
):
300 def _javascript_script_confirm_cb(self
, view
, frame
, message
, isConfirmed
):
303 def _javascript_script_prompt_cb(self
, view
, frame
,
304 message
, default
, text
):
307 def _populate_popup(self
, view
, menu
):
308 aboutitem
= gtk
.MenuItem(label
="About PyWebKit")
309 menu
.append(aboutitem
)
310 aboutitem
.connect('activate', self
._about
_pywebkitgtk
_cb
)
313 def _about_pywebkitgtk_cb(self
, widget
):
314 self
._browser
.open("http://live.gnome.org/PyWebKitGtk")
317 if __name__
== "__main__":
318 webbrowser
= WebBrowser()