Search code examples
rubylistboxgtkpygtkglade

How to make a Multiple-Select List Box in Ruby Glade/GTK, maybe using TreeView?


I am trying to make a multiple-select listbox in glade/ruby program and I am wondering how I go about doing this, like what element I add to the window and the corresponding example code I can use. I was looking at GTKList but it says it is deprecated now, and also I don't know how to get it working in ruby. GTK List docs say to use TreeView, but I have no idea how to set that up.

Just to be clear, I would like something like this, where the user can select multiple entries:

alt text http://geekswithblogs.net/images/geekswithblogs_net/dotNETvinz/OutputPreselectListBox.JPG

Thanks for the help guys! I am really desperate on this question.


Solution

  • Basically, you have to use GtkTreeView and set its "model" property to a GtkListStore that contains your data. GtkTreeView manages selection with GtkTreeSelection class. Use gtk_tree_view_get_selection (or whatever it is mapped to in ruby-gtk) to get the GtkTreeSelection. And set the selection mode to "multiple".

    Here's an example in Python. In Ruby/Gtk it should be similar.

    import pygtk
    pygtk.require('2.0')
    import gtk
    import gobject
    
    
    w = gtk.Window()
    w.connect('destroy', lambda w:gtk.main_quit())
    
    l = gtk.ListStore(gobject.TYPE_STRING)
    
    l.append(('Vinz',))
    l.append(('Jhen',))
    l.append(('Chris',))
    l.append(('Shynne',))
    
    treeview = gtk.TreeView()
    treeview.set_model(l)
    
    column = gtk.TreeViewColumn()
    cell = gtk.CellRendererText()
    column.pack_start(cell)
    column.add_attribute(cell,'text',0)
    treeview.append_column(column)
    
    treeview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
    
    def print_selected(treeselection):
        (model,pathlist)=treeselection.get_selected_rows()
        print pathlist
    
    treeview.get_selection().connect('changed',lambda s: print_selected(s))
    
    w.add(treeview)
    
    w.show_all()
    
    gtk.main()