import gtk
import gobject

class Wii:
    def __init__(self):
        self.limit = 0
        self.runs = 0

    def match_func(self, completion, key, iter, column):
        self.limit = self.limit + 1
        self.runs = self.runs + 1

        print ""
        print "[run: %02d] self.limit: %d" % (self.runs, self.limit)

        if self.limit > 4:
            print "[run: %02d] returning False early" % (self.runs)
            return False

        model = completion.get_model()
        text = model.get_value(iter, column)

        print "[run: %02d] comparing %s to %s" % (self.runs, key, text)

        if text.startswith(key):
            print "[run: %02d] telling EC to draw %s" % (self.runs, text)
            return True

        return False

    def changed_cb(self, editable, args=None):
        self.limit = 0
        print "(changed_cb) [run: %02d] resetting self.limit to 0" % self.runs


ls = gtk.ListStore(gobject.TYPE_STRING)
ls.append(['test'])
ls.append(['test1'])
ls.append(['test2'])
ls.append(['test3'])
ls.append(['test4'])
ls.append(['untest'])

ec = gtk.EntryCompletion()
e = gtk.Entry()
w = gtk.Window()

wii = Wii()

ec.set_model(ls)
ec.set_text_column(0)

ec.set_match_func(wii.match_func, 0)

e.connect('changed', wii.changed_cb)
e.set_completion(ec)

w.add(e)
w.show_all()
w.connect('delete_event', gtk.main_quit)

gtk.main()
