1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
# Copyright (c) 2003-2005 Ruby-GNOME2 Project Team
# This program is licenced under the same licence as Ruby-GNOME2.
#
# $Id: editable_cells.rb,v 1.7 2005/02/06 18:25:13 kzys Exp $
=begin
= Tree View/Editable Cells
This demo demonstrates the use of editable cells in a Gtk::TreeView. If
you're new to the Gtk::TreeView widgets and associates, look into
the Gtk::ListStore example first.
=end
require 'common'
module Demo
class EditableCells < BasicWindow
Item = Struct.new('Item', :number, :product, :editable)
COLUMN_NUMBER, COLUMN_PRODUCT, COLUMN_EDITABLE, NUM_COLUMNS = *(0..4).to_a
def initialize
super('Shopping list')
self.border_width = 5
vbox = Gtk::VBox.new(false, 5)
add(vbox)
vbox.pack_start(Gtk::Label.new('Shopping list (you can edit the cells!)'),
false, false, 0)
sw = Gtk::ScrolledWindow.new
sw.shadow_type = Gtk::SHADOW_ETCHED_IN
sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
vbox.pack_start(sw, true, true, 0)
# create model
model = create_model
# create tree view
treeview = Gtk::TreeView.new(model)
treeview.rules_hint = true
treeview.selection.mode = Gtk::SELECTION_SINGLE
add_columns(treeview)
sw.add(treeview)
# some buttons
hbox = Gtk::HBox.new(true, 4)
vbox.pack_start(hbox, false, false, 0)
button = Gtk::Button.new('Add item')
button.signal_connect('clicked') do
add_item(model)
end
hbox.pack_start(button, true, true, 0)
button = Gtk::Button.new('Remove item')
button.signal_connect('clicked') do
remove_item(treeview)
end
hbox.pack_start(button, true, true, 0)
set_default_size(320, 200)
end
def create_model
# create array
@articles = []
add_items
# create list store
model = Gtk::ListStore.new(Integer, String, TrueClass)
# add items
@articles.each do |article|
iter = model.append
article.each_with_index do |value, index|
iter.set_value(index, value)
end
end
return model
end
def add_items
item = Item.new(3, 'bottles of coke', true)
@articles.push(item)
item = Item.new(5, 'packages of noodles', true)
@articles.push(item)
item = Item.new(2, 'packages of chocolate chip cookies', true)
@articles.push(item)
item = Item.new(1, 'can vanilla ice cream', true)
@articles.push(item)
item = Item.new(6, 'eggs', true)
@articles.push(item)
end
def add_columns(treeview)
model = treeview.model
# number column
renderer = Gtk::CellRendererText.new
renderer.signal_connect('edited') do |*args|
cell_edited(*args.push(model))
end
treeview.insert_column(-1, 'Number', renderer,
{
:text => COLUMN_NUMBER,
:editable => COLUMN_EDITABLE,
})
def renderer.column
COLUMN_NUMBER
end
# product column
renderer = Gtk::CellRendererText.new
renderer.signal_connect('edited') do |*args|
cell_edited(*args.push(model))
end
def renderer.column
COLUMN_PRODUCT
end
treeview.insert_column(-1, 'Product', renderer,
{
:text => COLUMN_PRODUCT,
:editable => COLUMN_EDITABLE,
})
end
def cell_edited(cell, path_string, new_text, model)
path = Gtk::TreePath.new(path_string)
column = cell.column
iter = model.get_iter(path)
case column
when COLUMN_NUMBER
i = iter.path.indices[0]
@articles[i].number = new_text.to_i
iter.set_value(column, @articles[i].number)
when COLUMN_PRODUCT
i = iter.path.indices[0]
@articles[i].product = new_text
iter.set_value(column, @articles[i].product)
end
end
def add_item(model)
foo = Item.new(0, 'Description here', true)
@articles.concat([foo])
iter = model.append
foo.each_with_index do |value, index|
iter.set_value(index, value)
end
end
def remove_item(treeview)
model = treeview.model
selection = treeview.selection
if iter = selection.selected
@articles.delete_at(iter.path.indices[0])
model.remove(iter)
end
end
end
end
|