On Thu, 2002-01-24 at 20:36, James Henstridge wrote:
> Edgar Denny wrote:
>
> >I can't figure out if its possible to add a GtkWidget to a GtkListStore
> >and render it.
> >
> >Is it possible, and if so how?
> >
> It is not possible to put a GtkWidget into a treestore/liststore. It is
> possible to overlay a entry on top of the tree view (there is a demo in
> gtk-demo doing it), but I don't have python code to do it off hand.
>
> James.
>
I guess you mean editable_cells.c. I'm attaching a Python version of
this code.
The problem is that there seems to be a bug in it, and the same bug
exists in the C version as well. The bug is that when you remove a row
with the Remove Item button, the item is not properly deleted - the
Number column is deleted, but Product column is not. Its most obvious
when you try to delete the last row.
I'm using gtk+-1.3.12 on Debian unstable.
Edgar
#!/usr/bin/python2.2
import gobject
import gtk
item_list = ((3, 'bottles of coke'),
(5, 'packages of noodles'),
(2, 'packages of chocolate chip cookies'),
(1, 'can vanilla ice cream'),
(6, 'eggs'))
class Item:
pass
articles = []
COLUMN_NUMBER = 0
COLUMN_PRODUCT = 1
COLUMN_EDITABLE = 2
def add_items():
for number, product in item_list:
foo = Item()
foo.number = number
foo.product = product
foo.editable = gtk.TRUE
articles.append( foo)
def create_model():
add_items()
model = gtk.ListStore( gobject.TYPE_INT, gobject.TYPE_STRING,
gobject.TYPE_BOOLEAN)
for item in articles:
iter = model.append()
model.set_value( iter, 0, item.number)
model.set_value( iter, 1, item.product)
model.set_value( iter, 2, item.editable)
return model
def add_item( widget, model):
foo = Item()
foo.number = 0
foo.product = 'Description Here'
foo.editable = gtk.TRUE
articles.append( foo)
iter = model.append()
model.set_value( iter, COLUMN_NUMBER, foo.number)
model.set_value( iter, COLUMN_PRODUCT, foo.product)
model.set_value( iter, COLUMN_EDITABLE, foo.editable)
def remove_item( widget, treeview):
selection = treeview.get_selection()
s = selection.get_selected()
if s:
model, iter = s
path = model.get_path( iter)
model.remove( iter)
del articles[path[0]]
def cell_edited( cell, path_string, text, data):
model, column = data
iter = model.get_iter_root()
if not model.get_iter_from_string( iter, path_string):
return
path = model.get_path( iter)
if column == COLUMN_NUMBER:
articles[path[0]].number = int( text)
model.set_value( iter, column, int( text))
elif column == COLUMN_PRODUCT:
articles[path[0]].product = text
model.set_value( iter, column, text)
def add_columns( treeview):
model = treeview.get_model()
renderer = gtk.CellRendererText()
renderer.connect( 'edited', cell_edited, ( model, COLUMN_NUMBER))
column = gtk.TreeViewColumn( 'Number', renderer, text=COLUMN_NUMBER,
editable=COLUMN_EDITABLE)
treeview.append_column( column)
renderer = gtk.CellRendererText()
renderer.connect( 'edited', cell_edited, ( model, COLUMN_PRODUCT))
column = gtk.TreeViewColumn( 'Product', renderer, text=COLUMN_PRODUCT,
editable=COLUMN_EDITABLE)
treeview.append_column( column)
def do_editable_cells():
window = gtk.Window( gtk.WINDOW_TOPLEVEL)
window.set_title( 'Shopping List')
window.set_border_width( 5)
window.connect( 'destroy', lambda w: gtk.main_quit( ))
vbox = gtk.VBox( gtk.FALSE, 5)
window.add( vbox)
vbox.pack_start( gtk.Label( 'Shopping list ( you can edit cells!)'),
gtk.FALSE, gtk.FALSE, 0)
sw = gtk.ScrolledWindow()
sw.set_shadow_type( gtk.SHADOW_ETCHED_IN)
sw.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
vbox.pack_start( sw, gtk.TRUE, gtk.TRUE, 0)
model = create_model()
treeview = gtk.TreeView( model)
treeview.set_rules_hint( gtk.TRUE)
selection = treeview.get_selection()
selection.set_mode( gtk.SELECTION_SINGLE)
add_columns( treeview)
sw.add( treeview)
hbox = gtk.HBox( gtk.TRUE, 4)
vbox.pack_start( hbox, gtk.FALSE, gtk.FALSE, 0)
button = gtk.Button( label='Add Item')
button.connect( 'clicked', add_item, model)
hbox.pack_start( button, gtk.TRUE, gtk.TRUE, 0)
button = gtk.Button( label='Remove Item')
button.connect( 'clicked', remove_item, treeview)
hbox.pack_start( button, gtk.TRUE, gtk.TRUE, 0)
window.set_default_size( 320, 200)
window.show_all()
gtk.mainloop()
if __name__ == '__main__': do_editable_cells()