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
|
#
# checklist.py: A class (derived from GtkTreeView) that provides a list of
# checkbox / text string pairs
#
# Brent Fox <bfox@redhat.com>
# Jeremy Katz <katzj@redhat.com>
#
# Copyright 2001 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import gtk
import gobject
class CheckList (gtk.TreeView):
"""A class (derived from gtk.TreeView) that provides a list of
checkbox / text string pairs"""
# XXX need to handle the multicolumn case better still....
def __init__ (self, columns = 1):
self.store = gtk.ListStore(gobject.TYPE_BOOLEAN,
gobject.TYPE_STRING, gobject.TYPE_STRING)
gtk.TreeView.__init__ (self, self.store)
renderer = gtk.CellRendererToggle()
column = gtk.TreeViewColumn('Checkbox', renderer, active=0)
column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
column.set_fixed_width(50)
column.set_clickable(gtk.TRUE)
renderer.connect ("toggled", self.toggled_item)
self.append_column(column)
# XXX we only handle two text columns right now
if columns > 2:
raise RuntimeError, "CheckList supports a maximum of 2 columns"
self.columns = columns
# add the string columns to the tree view widget
for i in range(1, columns + 1):
renderer = gtk.CellRendererText()
column = gtk.TreeViewColumn('Text', renderer, text=i)
column.set_clickable(gtk.FALSE)
self.append_column(column)
self.set_rules_hint(gtk.FALSE)
self.set_headers_visible(gtk.FALSE)
self.columns_autosize()
def append_row (self, textList, init_value):
"""Add a row to the list.
text: text to display in the row
init_value: initial state of the indicator"""
textList = ("",) + textList
iter = self.store.append()
self.store.set_value(iter, 0, init_value)
# add the text for the number of columns we have
i = 1
for text in textList[1:self.columns + 1]:
self.store.set_value(iter, i, textList[i])
i = i + 1
def toggled_item(self, data, row):
"""Set a function to be called when the value of a row is toggled.
The function will be called with two arguments, the clicked item
in the row and a string for which row was clicked."""
iter = self.store.get_iter(int(row))
val = self.store.get_value(iter, 0)
self.store.set_value(iter, 0, not val)
def clear (self):
"Remove all rows"
self.store.clear()
def get_active(self, row):
"""Return FALSE or TRUE as to whether or not the row is toggled
similar to GtkToggleButtons"""
iter = self.store.get_iter(row)
return self.store.get_value(iter, 0)
def set_active(self, row, is_active):
"Set row to be is_active, similar to GtkToggleButton"
iter = self.store.get_iter(row)
self.store.set_value(iter, 0, is_active)
def get_text(self, row, column):
"Get the text from row and column"
iter = self.store.get_iter(row)
return self.store.get_value(iter, column)
|