1Gtk2::SimpleList(3) User Contributed Perl Documentation Gtk2::SimpleList(3)
2
3
4
6 Gtk2::SimpleList - A simple interface to Gtk2's complex MVC list widget
7
9 use Glib qw(TRUE FALSE);
10 use Gtk2 '-init';
11 use Gtk2::SimpleList;
12
13 my $slist = Gtk2::SimpleList->new (
14 'Text Field' => 'text',
15 'Markup Field' => 'markup',
16 'Int Field' => 'int',
17 'Double Field' => 'double',
18 'Bool Field' => 'bool',
19 'Scalar Field' => 'scalar',
20 'Pixbuf Field' => 'pixbuf',
21 );
22
23 @{$slist->{data}} = (
24 [ 'text', 1, 1.1, TRUE, $var, $pixbuf ],
25 [ 'text', 2, 2.2, FALSE, $var, $pixbuf ],
26 );
27
28 # (almost) anything you can do to an array you can do to
29 # $slist->{data} which is an array reference tied to the list model
30 push @{$slist->{data}}, [ 'text', 3, 3.3, TRUE, $var, $pixbuf ];
31
32 # mess with selections
33 $slist->get_selection->set_mode ('multiple');
34 $slist->get_selection->unselect_all;
35 $slist->select (1, 3, 5..9); # select rows by index
36 $slist->unselect (3, 8); # unselect rows by index
37 @sel = $slist->get_selected_indices;
38
39 # simple way to make text columns editable
40 $slist->set_column_editable ($col_num, TRUE);
41
42 # Gtk2::SimpleList derives from Gtk2::TreeView, so all methods
43 # on a treeview are available.
44 $slist->set_rules_hint (TRUE);
45 $slist->signal_connect (row_activated => sub {
46 my ($sl, $path, $column) = @_;
47 my $row_ref = $sl->get_row_data_from_path ($path);
48 # $row_ref is now an array ref to the double-clicked row's data.
49 });
50
51 # turn an existing TreeView into a SimpleList; useful for
52 # Glade-generated interfaces.
53 $simplelist = Gtk2::SimpleList->new_from_treeview (
54 $glade->get_widget ('treeview'),
55 'Text Field' => 'text',
56 'Int Field' => 'int',
57 'Double Field' => 'double',
58 );
59
61 SimpleList is a simple interface to the powerful but complex
62 Gtk2::TreeView and Gtk2::ListStore combination, implementing using tied
63 arrays to make thing simple and easy.
64
66 Gtk2 has a powerful, but complex MVC (Model, View, Controller) system
67 used to implement list and tree widgets. Gtk2::SimpleList automates
68 the complex setup work and allows you to treat the list model as a more
69 natural list of lists structure.
70
71 After creating a new Gtk2::SimpleList object with the desired columns
72 you may set the list data with a simple Perl array assignment. Rows may
73 be added or deleted with all of the normal array operations. You can
74 treat the "data" member of the list simplelist object as an array
75 reference, and manipulate the list data with Perl's normal array
76 operators.
77
78 A mechanism has also been put into place allowing columns to be Perl
79 scalars. The scalar is converted to text through Perl's normal
80 mechanisms and then displayed in the list. This same mechanism can be
81 expanded by defining arbitrary new column types before calling the new
82 function.
83
85 Glib::Object
86 +--- Gtk2::Object
87 +--- Gtk2::Widget
88 +--- Gtk2::TreeView
89 +--- Gtk2::SimpleList
90
92 $slist = Gtk2::SimpleList->new ($cname, $ctype, ...)
93 • $cname (string)
94
95 • $ctype (string)
96
97 Creates a new Gtk2::SimpleList object with the specified columns.
98 The parameter "cname" is the name of the column, what will be
99 displayed in the list headers if they are turned on. The parameter
100 ctype is the type of the column, one of:
101
102 text normal text strings
103 markup pango markup strings
104 int integer values
105 double double-precision floating point values
106 bool boolean values, displayed as toggle-able checkboxes
107 scalar a perl scalar, displayed as a text string by default
108 pixbuf a Gtk2::Gdk::Pixbuf
109
110 or the name of a custom type you add with "add_column_type". These
111 should be provided in pairs according to the desired columns for
112 your list.
113
114 $slist = Gtk2::SimpleList->new_from_treeview ($treeview, $cname,
115 $ctype, ...)
116 • $treeview (Gtk2::TreeView)
117
118 • $cname (string)
119
120 • $ctype (string)
121
122 Like "Gtk2::SimpleList->new()", but turns an existing
123 Gtk2::TreeView into a Gtk2::SimpleList. This is intended mostly
124 for use with stuff like Glade, where the widget is created for you.
125 This will create and attach a new model and remove any existing
126 columns from treeview. Returns treeview, re-blessed as a
127 Gtk2::SimpleList.
128
129 $slist->set_data_array ($arrayref)
130 • $arrayref (array reference)
131
132 Set the data in the list to the array reference $arrayref. This is
133 completely equivalent to @{$list->{data}} = @{$arrayref} and is
134 only here for convenience and for those programmers who don't like
135 to type-cast and have static, set once data.
136
137 @indices = $slist->get_selected_indices
138 Return the indices of the selected rows in the ListStore.
139
140 $slist->get_row_data_from_path ($path)
141 • $path (Gtk2::TreePath) the path of the desired row
142
143 Returns an array ref with the data of the row indicated by $path.
144
145 $slist->select ($index, ...);
146 $slist->unselect ($index, ...);
147 • $index (integer)
148
149 Select or unselect rows in the list by index. If the list is set
150 for multiple selection, all indices in the list will be set/unset;
151 otherwise, just the first is used. If the list is set for no
152 selection, then nothing happens.
153
154 To set the selection mode, or to select all or none of the rows,
155 use the normal TreeView/TreeSelection stuff, e.g.
156 $slist->get_selection and the TreeSelection methods "get_mode",
157 "set_mode", "select_all", and "unselect_all".
158
159 $slist->set_column_editable ($index, $editable)
160 • $index (integer)
161
162 • $editable (boolean)
163
164 boolean = $slist->get_column_editable ($index)
165 • $index (integer)
166
167 This is a very simple interface to Gtk2::TreeView's editable text
168 column cells. All columns which use the attr "text" (basically,
169 any text or number column, see "add_column_type") automatically
170 have callbacks installed to update data when cells are edited.
171 With "set_column_editable", you can enable the in-place editing.
172
173 "get_column_editable" tells you if column index is currently
174 editable.
175
176 Gtk2::SimpleList->add_column_type ($type_name, ...)
177 $type_name (string)
178
179 Add a new column type to the list of possible types. Initially six
180 column types are defined, text, int, double, bool, scalar, and
181 pixbuf. The bool column type uses a toggle cell renderer, the
182 pixbuf uses a pixbuf cell renderer, and the rest use text cell
183 renderers. In the process of adding a new column type you may use
184 any cell renderer you wish.
185
186 The first parameter is the column type name, the list of six are
187 examples. There are no restrictions on the names and you may even
188 overwrite the existing ones should you choose to do so. The
189 remaining parameters are the type definition consisting of key
190 value pairs. There are three required: type, renderer, and attr.
191 The type key determines what actual datatype will be stored in the
192 underlying model representation; this is a package name, e.g.
193 Glib::String, Glib::Int, Glib::Boolean, but in general if you want
194 an arbitrary Perl data structure you will want to use
195 'Glib::Scalar'. The renderer key should hold the class name of the
196 cell renderer to create for this column type; this may be any of
197 Gtk2::CellRendererText, Gtk2::CellRendererToggle,
198 Gtk2::CellRendererPixbuf, or some other, possibly custom, cell
199 renderer class. The attr key is magical; it may be either a
200 string, in which case it specifies the attribute which will be set
201 from the specified column (e.g. 'text' for a text renderer,
202 'active' for a toggle renderer, etc), or it may be a reference to a
203 subroutine which will be called each time the renderer needs to
204 draw the data.
205
206 This function, described as a GtkTreeCellDataFunc in the API
207 reference, will receive 5 parameters: $treecol, $cell, $model,
208 $iter, $col_num (when SimpleList hooks up the function, it sets the
209 column number to be passed as the user data). The data value for
210 the particular cell in question is available via $model->get
211 ($iter, $col_num); you can then do whatever it is you have to do to
212 render the cell the way you want. Here are some examples:
213
214 # just displays the value in a scalar as
215 # Perl would convert it to a string
216 Gtk2::SimpleList->add_column_type( 'a_scalar',
217 type => 'Glib::Scalar',
218 renderer => 'Gtk2::CellRendererText',
219 attr => sub {
220 my ($treecol, $cell, $model, $iter, $col_num) = @_;
221 my $info = $model->get ($iter, $col_num);
222 $cell->set (text => $info);
223 }
224 );
225
226 # sums up the values in an array ref and displays
227 # that in a text renderer
228 Gtk2::SimpleList->add_column_type( 'sum_of_array',
229 type => 'Glib::Scalar',
230 renderer => 'Gtk2::CellRendererText',
231 attr => sub {
232 my ($treecol, $cell, $model, $iter, $col_num) = @_;
233 my $sum = 0;
234 my $info = $model->get ($iter, $col_num);
235 foreach (@$info)
236 {
237 $sum += $_;
238 }
239 $cell->set (text => $sum);
240 }
241 );
242
244 After creating a new Gtk2::SimpleList object there will be a member
245 called "data" which is a tied array. That means data may be treated as
246 an array, but in reality the data resides in something else. There is
247 no need to understand the details of this it just means that you put
248 data into, take data out of, and modify it just like any other array.
249 This includes using array operations like push, pop, unshift, and
250 shift. For those of you very familiar with perl this section will prove
251 redundant, but just in case:
252
253 Adding and removing rows:
254
255 # push a row onto the end of the list
256 push @{$slist->{data}}, [col1_data, col2_data, ..., coln_data];
257 # pop a row off of the end of the list
258 $rowref = pop @{$slist->{data}};
259 # unshift a row onto the beginning of the list
260 unshift @{$slist->{data}}, [col1_data, col2_data, ..., coln_data];
261 # shift a row off of the beginning of the list
262 $rowref = shift @{$slist->{data}};
263 # delete the row at index $n, 0 indexed
264 splice @{ $slist->{data} }, $n, 1;
265 # set the entire list to be the data in a array
266 @{$slist->{data}} = ( [row1, ...], [row2, ...], [row3, ...] );
267
268 Getting at the data in the list:
269
270 # get an array reference to the entire nth row
271 $rowref = $slist->{data}[n];
272 # get the scalar in the mth column of the nth row, 0 indexed
273 $val = $slist->{data}[n][m];
274 # set an array reference to the entire nth row
275 $slist->{data}[n] = [col1_data, col2_data, ..., coln_data];
276 # get the scalar in the mth column of the nth row, 0 indexed
277 $slist->{data}[n][m] = $rowm_coln_value;
278
280 Perl(1), Glib(3pm), Gtk2(3pm), Gtk2::TreeView(3pm),
281 Gtk2::TreeModel(3pm), Gtk2::ListStore(3pm).
282
283 Note: Gtk2::SimpleList is deprecated in favor of
284 Gtk2::Ex::Simple::List, part of the Gtk2-Perl-Ex project at
285 http://gtk2-perl-ex.sf.net .
286
288 muppet <scott at asofyet dot org>
289 Ross McFarland <rwmcfa1 at neces dot com>
290 Gavin Brown <gavin dot brown at uk dot com>
291
293 Copyright 2003-2004 by the Gtk2-Perl team.
294
295 This library is free software; you can redistribute it and/or modify it
296 under the terms of the GNU Library General Public License as published
297 by the Free Software Foundation; either version 2.1 of the License, or
298 (at your option) any later version.
299
300 This library is distributed in the hope that it will be useful, but
301 WITHOUT ANY WARRANTY; without even the implied warranty of
302 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
303 Library General Public License for more details.
304
305 You should have received a copy of the GNU Library General Public
306 License along with this library; if not, write to the Free Software
307 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
308 02110-1301 USA.
309
310
311
312perl v5.36.0 2022-07-22 Gtk2::SimpleList(3)