Reply
Thread Tools
Posts: 324 | Thanked: 371 times | Joined on Dec 2009 @ Vancouver, BC
#1
I had a lot of trouble finding information on how to display a bit of HTML in my python/gtk application (the webkit module for QT application makes it way easier and nicer than in gtk, but QT doesn't seem to have the hildon widgets readily available quite yet). So, after a lot of looking around, here is a small example on how to use gtkhtml2 with finger scrolling, on Maemo Fremantle. One little gripe that remains is that it does text selection at the same time as scrolling.

Code:
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()
 

The Following 3 Users Say Thank You to Slocan For This Useful Post:
Posts: 144 | Thanked: 134 times | Joined on Jan 2010 @ Hamburg
#2
Thanks, that's exactly what i was looking for!

But there is a problem:

"link_clicked" doesn't seem to be triggered in my script. Even in your example.
 
Posts: 324 | Thanked: 371 times | Joined on Dec 2009 @ Vancouver, BC
#3
Hmm, it works for me, both in scratchbox and on the n900.
If you start it from the shell, are you getting any error messages in there?
 
Posts: 144 | Thanked: 134 times | Joined on Jan 2010 @ Hamburg
#4
ok, was my fault

The links are extremely difficult to hit.
At random it works to click one, but I don't know how it happened.

Last edited by mbo; 2010-01-04 at 11:29. Reason: typo
 
Posts: 324 | Thanked: 371 times | Joined on Dec 2009 @ Vancouver, BC
#5
Yes, it is quite tricky to hit the links right. Using the stylus or finger nails are a must. But it is not made to be a full featured web-browser, it's just to display a bit of HTML formatting. If you're generating the HTML yourself, using images for links might make it easier to click.
 
Posts: 144 | Thanked: 134 times | Joined on Jan 2010 @ Hamburg
#6
I'm displaying a search result list, so images are no option for me.

I switched to hildon.TouchSelector, which works quite well for my app.
 
Reply


 
Forum Jump


All times are GMT. The time now is 10:30.