1Gtk3::SimpleList(3) User Contributed Perl Documentation Gtk3::SimpleList(3)
2
3
4
6 Gtk3::SimpleList - A simple interface to Gtk3's complex MVC list widget
7
9 use Glib qw(TRUE FALSE);
10 use Gtk3 '-init';
11 use Gtk3::SimpleList;
12
13 my $slist = Gtk3::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 # Gtk3::SimpleList derives from Gtk3::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 = Gtk3::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 Gtk3::TreeView and Gtk3::ListStore combination, implementing using tied
63 arrays to make thing simple and easy.
64
66 Gtk3 has a powerful, but complex MVC (Model, View, Controller) system
67 used to implement list and tree widgets. Gtk3::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 Gtk3::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 +--- Gtk3::Object
87 +--- Gtk3::Widget
88 +--- Gtk3::TreeView
89 +--- Gtk3::SimpleList
90
92 $slist = Gtk3::SimpleList->new ($cname, $ctype, ...)
93 • $cname (string)
94
95 • $ctype (string)
96
97 Creates a new Gtk3::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 Gtk3::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 = Gtk3::SimpleList->new_from_treeview ($treeview, $cname,
115 $ctype, ...)
116 • $treeview (Gtk3::TreeView)
117
118 • $cname (string)
119
120 • $ctype (string)
121
122 Like "Gtk3::SimpleList->new()", but turns an existing
123 Gtk3::TreeView into a Gtk3::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 Gtk3::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 (Gtk3::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 Gtk3::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 Gtk3::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 Gtk3::CellRendererText, Gtk3::CellRendererToggle,
198 Gtk3::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 Gtk3::SimpleList->add_column_type( 'a_scalar',
217 type => 'Glib::Scalar',
218 renderer => 'Gtk3::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 Gtk3::SimpleList->add_column_type( 'sum_of_array',
229 type => 'Glib::Scalar',
230 renderer => 'Gtk3::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 Gtk3::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), Gtk3(3pm), Gtk3::TreeView(3pm),
281 Gtk3::TreeModel(3pm), Gtk3::ListStore(3pm).
282
283 Note: Gtk2::SimpleList was deprecated in favor of
284 Gtk2::Ex::Simple::List, part of the Gtk2-Perl-Ex project at
285 http://gtk2-perl-ex.sf.net . Gtk3::SimpleList is a simple port of
286 Gtk2::SimpleList on top of Gtk3 for its users that wanted to switch to
287 Gtk3.
288
290 muppet <scott at asofyet dot org>
291 Ross McFarland <rwmcfa1 at neces dot com>
292 Gavin Brown <gavin dot brown at uk dot com>
293 Thierry Vignaud
294
296 Copyright 2003-2004 by the Gtk2-Perl team. Copyright 2013 by Thierry
297 Vignaud
298
299 This library is free software; you can redistribute it and/or modify it
300 under the terms of the GNU Library General Public License as published
301 by the Free Software Foundation; either version 2.1 of the License, or
302 (at your option) any later version.
303
304 This library is distributed in the hope that it will be useful, but
305 WITHOUT ANY WARRANTY; without even the implied warranty of
306 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
307 Library General Public License for more details.
308
309 You should have received a copy of the GNU Library General Public
310 License along with this library; if not, write to the Free Software
311 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307
312 USA.
313
314
315
316perl v5.36.0 2023-02-08 Gtk3::SimpleList(3)