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