Question

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.

Was it helpful?

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

OTHER TIPS

THe above answer is correct, but if you wrote it using visualruby it would be a lot easier:

class MyList < VR::Listview

  def initialize(:employee_names => String)
    add_row(:employee_names => "Vince")
    add_row(:employee_names => "Jhen")
    add_row(:employee_names => "Chris")
    add_row(:employee_names => "Shynne")
  end

end

That would set everything exactly like your example, including the title.

Then you just add it to a box, or scrolledwindow:

class GUI

  include GladeGUI

  def initialize()
    load_glade(__FILE__)
    @builder("Scrolledwindow1").add(MyList.new)
    show_window()
  end

end

Go to:

http://www.visualruby.net

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top