Re: What signals should I be using with a GtkCombo?



    Thomas> I agree completely. I've looked a bit at the combo resently, and
    Thomas> the API doesn't strike me as particularly well chosen...

    >> I think GtkCombo should provide a value-changed signal along with a
    >> get_value method, but maybe I'm thinking too high-level. ;-)

    Thomas> 'ere you go then. I've attached a widget that wraps the combo
    Thomas> and translates the entry's "changed" and the lists
    Thomas> "select-child" into "changed" events. 

Just about what I wound up writing yesterday, only I wrote mine in Python.
It's reassuring to know I wasn't missing something terribly obvious in the
existing C API.  The attached code is for the new Gtk+/PyGtk versions (Gtk+
1.3.x).

-- 
Skip Montanaro (skip pobox com)
http://www.mojam.com/
http://www.musi-cal.com/

#!/usr/bin/env python

import gtk
import gobject

class Combo(gtk.GtkCombo):
    def __init__(self):
        gtk.GtkCombo.__init__(self)
        self.value = ""
        self.disable_activate()
        self.entry.connect("activate", self.activate_cb)
        self.list.connect("select-child", self.list_select_cb)
        
    def set_value(self, val):
        self.value = val
        self.entry.set_text(val)

    def activate_cb(self, e):
        newval = e.get_text()
        if newval != self.value:
            self.value = newval
            self.emit("value-changed")

    def set_popdown_strings(self, strings):
        self.popdown_strings = strings
        gtk.GtkCombo.set_popdown_strings(self, strings)

    def list_select_cb(self, lst, item):
        i = lst.child_position(item)
        newval = self.popdown_strings[i]
        if newval != self.value:
            self.value = newval
            self.emit("value-changed")

gobject.signal_new("value-changed", Combo,
                   gobject.SIGNAL_RUN_LAST|gobject.SIGNAL_ACTION,
                   gobject.TYPE_NONE, ())

def value_cb(c):
    print "new value:", c.value

def main():
    window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
    window.set_title("title")
    window.connect("destroy", gtk.mainquit)

    combo = Combo()
    combo.set_popdown_strings(["abc", "spam", "eggs", "ham"])
    combo.set_value("abc")
    
    combo.connect("value-changed", value_cb)
    window.add(combo)

    window.show_all()

    gtk.mainloop()

if __name__ == "__main__":
    main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]