import gtk
import gtk.glade

USE_CUSTOM_RENDERER = False

class CompletedEntry(gtk.Entry):
    
    def __init__(self):
        gtk.Entry.__init__(self)
        completion = gtk.EntryCompletion()
        completion.set_match_func(self.match_func)
        completion.connect("match-selected",
                           self.on_completion_match)
        completion.set_model(gtk.ListStore(str))
        if USE_CUSTOM_RENDERER:
            r = gtk.CellRendererText() # set up the completion renderer
            completion.pack_start(r)
            completion.set_cell_data_func(r, self.cell_data_func)        
        else:
            completion.set_text_column(0)
        self.set_completion(completion)

    def cell_data_func(self, column, renderer, model, iter, data=None):
        '''
        render the values in the completion popup model
        '''
        renderer.set_property('text', model[iter][0])
                
    def match_func(self, completion, key, iter):
        model = completion.get_model()
        return model[iter][0].startswith(self.get_text())
         
    def on_completion_match(self, completion, model, iter):
        self.set_text(model[iter][0])
        self.set_position(-1)

    def add_words(self, words):
        model = self.get_completion().get_model()
        for word in words:
            model.append([word])             

if __name__ == "__main__":
    win = gtk.Window()    
    win.connect('delete-event', gtk.main_quit)
    entry = CompletedEntry()    
    entry.add_words(['abc', 'def', 'ghi', 'jkl', 'mno',
                      'pqr', 'stu', 'vwx', 'yz'])
    win.add(entry)
    win.show_all()
    gtk.main()