Gtk2::Ex::Simple::List.3pm

Langue: en

Version: 2004-12-02 (fedora - 01/12/10)

Section: 3 (Bibliothèques de fonctions)

NAME

Gtk2::Ex::Simple::List - A simple interface to Gtk2's complex MVC list widget

SYNOPSIS

   use Glib qw(TRUE FALSE);
   use Gtk2 '-init';
   use Gtk2::Ex::Simple::List;
 
   my $slist = Gtk2::Ex::Simple::List->new (
                 'Text Field'    => 'text',
                 'Markup Field'  => 'markup',
                 'Int Field'     => 'int',
                 'Double Field'  => 'double',
                 'Bool Field'    => 'bool',
                 'Scalar Field'  => 'scalar',
                 'Pixbuf Field'  => 'pixbuf',
               );
 
   @{$slist->{data}} = (
           [ 'text', 1, 1.1,  TRUE, $var, $pixbuf ],
           [ 'text', 2, 2.2, FALSE, $var, $pixbuf ],
   );
 
   # (almost) anything you can do to an array you can do to 
   # $slist->{data} which is an array reference tied to the list model
   push @{$slist->{data}}, [ 'text', 3, 3.3, TRUE, $var, $pixbuf ];
 
   # mess with selections
   $slist->get_selection->set_mode ('multiple');
   $slist->get_selection->unselect_all;
   $slist->select (1, 3, 5..9); # select rows by index
   $slist->unselect (3, 8); # unselect rows by index
   @sel = $slist->get_selected_indices;
 
   # simple way to make text columns editable
   $slist->set_column_editable ($col_num, TRUE);
 
   # Gtk2::Ex::Simple::List derives from Gtk2::TreeView, so all methods
   # on a treeview are available.
   $slist->set_rules_hint (TRUE);
   $slist->signal_connect (row_activated => sub {
           my ($sl, $path, $column) = @_;
           my $row_ref = $sl->get_row_data_from_path ($path);
           # $row_ref is now an array ref to the double-clicked row's data.
       });
 
   # turn an existing TreeView into a SimpleList; useful for
   # Glade-generated interfaces.
   $simplelist = Gtk2::Ex::Simple::List->new_from_treeview (
                     $glade->get_widget ('treeview'),
                     'Text Field'    => 'text',
                     'Int Field'     => 'int',
                     'Double Field'  => 'double',
                  );
 
 

ABSTRACT

SimpleList is a simple interface to the powerful but complex Gtk2::TreeView and Gtk2::ListStore combination, implementing using tied arrays to make thing simple and easy.

DESCRIPTION

Gtk2 has a powerful, but complex MVC (Model, View, Controller) system used to implement list and tree widgets. Gtk2::Ex::Simple::List automates the complex setup work and allows you to treat the list model as a more natural list of lists structure.

After creating a new Gtk2::Ex::Simple::List object with the desired columns you may set the list data with a simple Perl array assignment. Rows may be added or deleted with all of the normal array operations. You can treat the "data" member of the Simple::List object as an array reference, and manipulate the list data with perl's normal array operators.

A mechanism has also been put into place allowing columns to be Perl scalars. The scalar is converted to text through Perl's normal mechanisms and then displayed in the list. This same mechanism can be expanded by defining arbitrary new column types before calling the new function.

OBJECT HIERARCHY

  Glib::Object
  +--- Gtk2::Object
       +--- Gtk2::Widget
            +--- Gtk2::TreeView
                 +--- Gtk2::Ex::Simple::List
 
 

METHODS

$slist = Gtk2::Ex::Simple::List->new ($cname, $ctype, ...)
*
$cname (string)
*
$ctype (string)

Creates a new Gtk2::Ex::Simple::List object with the specified columns. The parameter "cname" is the name of the column, what will be displayed in the list headers if they are turned on. The parameter ctype is the type of the column, one of:
  text    normal text strings
  markup  pango markup strings
  int     integer values
  double  double-precision floating point values
  bool    boolean values, displayed as toggle-able checkboxes
  scalar  a perl scalar, displayed as a text string by default
  pixbuf  a Gtk2::Gdk::Pixbuf
 
 

or the name of a custom type you add with "add_column_type". These should be provided in pairs according to the desired columns for your list.

$slist = Gtk2::Ex::Simple::List->new_from_treeview ($treeview, $cname, $ctype, ...)
*
$treeview (Gtk2::TreeView)
*
$cname (string)
*
$ctype (string)

Like "Gtk2::Ex::Simple::List->new()", but turns an existing Gtk2::TreeView into a Gtk2::Ex::Simple::List. This is intended mostly for use with stuff like Glade, where the widget is created for you. This will create and attach a new model and remove any existing columns from treeview. Returns treeview, re-blessed as a Gtk2::Ex::Simple::List.
$slist->set_data_array ($arrayref)
*
$arrayref (array reference)

Set the data in the list to the array reference $arrayref. This is completely equivalent to @{$list->{data}} = @{$arrayref} and is only here for convenience and for those programmers who don't like to type-cast and have static, set once data.
@indices = $slist->get_selected_indices
Return the indices of the selected rows in the ListStore.
$slist->get_row_data_from_path ($path)
*
$path (Gtk2::TreePath) the path of the desired row

Returns an array ref with the data of the row indicated by $path.
$slist->select ($index, ...);
$slist->unselect ($index, ...);
*
$index (integer)

Select or unselect rows in the list by index. If the list is set for multiple selection, all indices in the list will be set/unset; otherwise, just the first is used. If the list is set for no selection, then nothing happens.
To set the selection mode, or to select all or none of the rows, use the normal TreeView/TreeSelection stuff, e.g. $slist->get_selection and the TreeSelection methods "get_mode", "set_mode", "select_all", and "unselect_all".
$slist->set_column_editable ($index, $editable)
*
$index (integer)
*
$editable (boolean)
boolean = $slist->get_column_editable ($index)
*
$index (integer)

This is a very simple interface to Gtk2::TreeView's editable text column cells. All columns which use the attr ``text'' (basically, any text or number column, see "add_column_type") automatically have callbacks installed to update data when cells are edited. With "set_column_editable", you can enable the in-place editing.
"get_column_editable" tells you if column index is currently editable.
Gtk2::Ex::Simple::List->add_column_type ($type_name, ...)
$type_name (string)

Add a new column type to the list of possible types. Initially six column types are defined, text, int, double, bool, scalar, and pixbuf. The bool column type uses a toggle cell renderer, the pixbuf uses a pixbuf cell renderer, and the rest use text cell renderers. In the process of adding a new column type you may use any cell renderer you wish.
The first parameter is the column type name, the list of six are examples. There are no restrictions on the names and you may even overwrite the existing ones should you choose to do so. The remaining parameters are the type definition consisting of key value pairs. There are three required: type, renderer, and attr. The type key determines what actual datatype will be stored in the underlying model representation; this is a package name, e.g. Glib::String, Glib::Int, Glib::Boolean, but in general if you want an arbitrary Perl data structure you will want to use 'Glib::Scalar'. The renderer key should hold the class name of the cell renderer to create for this column type; this may be any of Gtk2::CellRendererText, Gtk2::CellRendererToggle, Gtk2::CellRendererPixbuf, or some other, possibly custom, cell renderer class. The attr key is magical; it may be either a string, in which case it specifies the attribute which will be set from the specified column (e.g. 'text' for a text renderer, 'active' for a toggle renderer, etc), or it may be a reference to a subroutine which will be called each time the renderer needs to draw the data.
This function, described as a GtkTreeCellDataFunc in the API reference, will receive 5 parameters: $treecol, $cell, $model, $iter, $col_num (when SimpleList hooks up the function, it sets the column number to be passed as the user data). The data value for the particular cell in question is available via $model->get ($iter, $col_num); you can then do whatever it is you have to do to render the cell the way you want. Here are some examples:
   # just displays the value in a scalar as 
   # Perl would convert it to a string
   Gtk2::Ex::Simple::List->add_column_type( 'a_scalar', 
           type     => 'Glib::Scalar',
           renderer => 'Gtk2::CellRendererText',
           attr     => sub {
                my ($treecol, $cell, $model, $iter, $col_num) = @_;
                my $info = $model->get ($iter, $col_num);
                $cell->set (text => $info);
           }
      );
 
   # sums up the values in an array ref and displays 
   # that in a text renderer
   Gtk2::Ex::Simple::List->add_column_type( 'sum_of_array', 
           type     => 'Glib::Scalar',
           renderer => 'Gtk2::CellRendererText',
           attr     => sub {
                my ($treecol, $cell, $model, $iter, $col_num) = @_;
                my $sum = 0;
                my $info = $model->get ($iter, $col_num);
                foreach (@$info)
                {
                    $sum += $_;
                }
                $cell->set (text => $sum);
           } 
      );
 
 

MODIFYING LIST DATA

After creating a new Gtk2::Ex::Simple::List object there will be a member called "data" which is a tied array. That means data may be treated as an array, but in reality the data resides in something else. There is no need to understand the details of this it just means that you put data into, take data out of, and modify it just like any other array. This includes using array operations like push, pop, unshift, and shift. For those of you very familiar with perl this section will prove redundant, but just in case:
   Adding and removing rows:
   
     # push a row onto the end of the list
     push @{$slist->{data}}, [col1_data, col2_data, ..., coln_data];
     # pop a row off of the end of the list
     $rowref = pop @{$slist->{data}};
     # unshift a row onto the beginning of the list
     unshift @{$slist->{data}}, [col1_data, col2_data, ..., coln_data];
     # shift a row off of the beginning of the list
     $rowref = shift @{$slist->{data}};
     # delete the row at index $n, 0 indexed
     splice @{ $slist->{data} }, $n, 1;
     # set the entire list to be the data in a array
     @{$slist->{data}} = ( [row1, ...], [row2, ...], [row3, ...] );
 
   Getting at the data in the list:
   
     # get an array reference to the entire nth row
     $rowref = $slist->{data}[n];
     # get the scalar in the mth column of the nth row, 0 indexed
     $val = $slist->{data}[n][m];
     # set an array reference to the entire nth row
     $slist->{data}[n] = [col1_data, col2_data, ..., coln_data];
     # get the scalar in the mth column of the nth row, 0 indexed
     $slist->{data}[n][m] = $rowm_coln_value;
 
 

SEE ALSO

Perl(1), Glib(3pm), Gtk2(3pm), Gtk2::TreeView(3pm), Gtk2::TreeModel(3pm), Gtk2::ListStore(3pm).

AUTHORS

  muppet <scott at asofyet dot org>
  Ross McFarland <rwmcfa1 at neces dot com>
  Gavin Brown <gavin dot brown at uk dot com>
 
 
Copyright 2004 by the Gtk2-Perl team.

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA.