import hildon import gtk import gtkhtml2 import urllib2 class MyApp: def __init__(self, html): # Create the main window self.window = hildon.StackableWindow() # Create the GtkHtml View and Document. # gtkhtml2.View is a GtkWidget, while gtkhtml2.Document deals with the html content self.view = gtkhtml2.View() self.document = gtkhtml2.Document() self.view.set_document(self.document) # Create a PannableArea, to allow finger scrolling self.pannableArea = hildon.PannableArea() # Add the gtkhtml2.View to the PannableArea. Do NOT use .add_with_viewport self.pannableArea.add(self.view) # Tell the PannableArea to scroll in both directions (only horizontal by default) self.pannableArea.set_property("mov-mode", hildon.MOVEMENT_MODE_BOTH) # Attach to the signals from the gtkhtml # link-clicked is triggered when a link is clicked on the HTML page self.document.connect("link_clicked", self._signal_link_clicked, self.document) # request-url is triggered when the gtkhtml2 object tries to load a url, in particular images self.document.connect("request-url", self._signal_request_url) # Add all that to the window self.window.add(self.pannableArea) # Load the actual document self.load_html(self.document, html) # Display everything self.window.show_all() def load_html(self, document, html): # Load the html data into a gtkhtml2.Document # Clear the current content document.clear() # Open a stream, of the type "text/html" (other content types are possible) document.open_stream("text/html") # Write the html data into the stream document.write_stream(html) # Close the stream document.close_stream() def _signal_link_clicked(self, object, link, document): # Called when a link is clicked. Fetch the URL with urllib2 f = urllib2.urlopen(link) html = f.read() f.close() # Load the fetched data in the gtkhtml2.Document self.load_html(document, html) def _signal_request_url(self, object, url, stream): # This function is called in particular for linked images on a page # Fetch the image, and write it into the stream provided # We first check it is a full URL. Relative URL would need more processing if (url.lower().startswith("http")): f = urllib2.urlopen(url) stream.write(f.read()) stream.close() def run(self): # Main loop self.window.connect("destroy", gtk.main_quit) gtk.main() if __name__ == "__main__": html = '''<a href="http://www.google.com">Link</a><BR /> Text<BR />And More Text<BR />''' app = MyApp(html) app.run()