home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / perl5 / Gtk2 / SimpleList.pm < prev    next >
Encoding:
Perl POD Document  |  2005-06-16  |  23.9 KB  |  848 lines

  1. #
  2. # $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Gtk2/pm/SimpleList.pm,v 1.27 2005/06/16 04:38:11 muppetman Exp $
  3. #
  4.  
  5. #########################
  6. package Gtk2::SimpleList;
  7.  
  8. use strict;
  9. use Carp;
  10. use Gtk2;
  11.  
  12. our @ISA = 'Gtk2::TreeView';
  13.  
  14. our $VERSION = '0.15';
  15.  
  16. our %column_types = (
  17.   'hidden' => {type=>'Glib::String',                                        attr=>'hidden'},
  18.   'text'   => {type=>'Glib::String',  renderer=>'Gtk2::CellRendererText',   attr=>'text'},
  19.   'markup' => {type=>'Glib::String',  renderer=>'Gtk2::CellRendererText',   attr=>'markup'},
  20.   'int'    => {type=>'Glib::Int',     renderer=>'Gtk2::CellRendererText',   attr=>'text'},
  21.   'double' => {type=>'Glib::Double',  renderer=>'Gtk2::CellRendererText',   attr=>'text'},
  22.   'bool'   => {type=>'Glib::Boolean', renderer=>'Gtk2::CellRendererToggle', attr=>'active'},
  23.   'scalar' => {type=>'Glib::Scalar',  renderer=>'Gtk2::CellRendererText',   
  24.       attr=> sub { 
  25.           my ($tree_column, $cell, $model, $iter, $i) = @_;
  26.           my ($info) = $model->get ($iter, $i);
  27.           $cell->set (text => $info || '' );
  28.       } },
  29.   'pixbuf' => {type=>'Gtk2::Gdk::Pixbuf', renderer=>'Gtk2::CellRendererPixbuf', attr=>'pixbuf'},
  30. );
  31.  
  32. # this is some cool shit
  33. sub add_column_type
  34. {
  35.     shift;    # don't want/need classname
  36.     my $name = shift;
  37.     $column_types{$name} = { @_ };
  38. }
  39.  
  40. sub text_cell_edited {
  41.     my ($cell_renderer, $text_path, $new_text, $slist) = @_;
  42.     my $path = Gtk2::TreePath->new_from_string ($text_path);
  43.     my $model = $slist->get_model;
  44.     my $iter = $model->get_iter ($path);
  45.     $model->set ($iter, $cell_renderer->{column}, $new_text);
  46. }
  47.  
  48. sub new {
  49.     croak "Usage: $_[0]\->new (title => type, ...)\n"
  50.         . " expecting a list of column title and type name pairs.\n"
  51.         . " can't create a SimpleList with no columns"
  52.         unless @_ >= 3; # class, key1, val1
  53.     return shift->new_from_treeview (Gtk2::TreeView->new (), @_);
  54. }
  55.  
  56. sub new_from_treeview {
  57.     my $class = shift;
  58.     my $view = shift;
  59.     croak "treeview is not a Gtk2::TreeView"
  60.         unless defined ($view)
  61.            and UNIVERSAL::isa ($view, 'Gtk2::TreeView');
  62.     croak "Usage: $class\->new_from_treeview (treeview, title => type, ...)\n"
  63.         . " expecting a treeview reference and list of column title and type name pairs.\n"
  64.         . " can't create a SimpleList with no columns"
  65.         unless @_ >= 2; # key1, val1
  66.     my @column_info = ();
  67.     for (my $i = 0; $i < @_ ; $i+=2) {
  68.         my $typekey = $_[$i+1];
  69.         croak "expecting pairs of title=>type"
  70.             unless $typekey;
  71.         croak "unknown column type $typekey, use one of "
  72.             . join(", ", keys %column_types)
  73.             unless exists $column_types{$typekey};
  74.         my $type = $column_types{$typekey}{type};
  75.         if (not defined $type) {
  76.             $type = 'Glib::String';
  77.             carp "column type $typekey has no type field; did you"
  78.                . " create a custom column type incorrectly?\n"
  79.                . "limping along with $type";
  80.         }
  81.         push @column_info, {
  82.             title => $_[$i],
  83.             type => $type,
  84.             rtype => $column_types{$_[$i+1]}{renderer},
  85.             attr => $column_types{$_[$i+1]}{attr},
  86.         };
  87.     }
  88.     my $model = Gtk2::ListStore->new (map { $_->{type} } @column_info);
  89.     # just in case, 'cause i'm paranoid like that.
  90.     map { $view->remove_column ($_) } $view->get_columns;
  91.     $view->set_model ($model);
  92.     for (my $i = 0; $i < @column_info ; $i++) {
  93.         if( 'CODE' eq ref $column_info[$i]{attr} )
  94.         {
  95.             $view->insert_column_with_data_func (-1,
  96.                 $column_info[$i]{title},
  97.                 $column_info[$i]{rtype}->new,
  98.                 $column_info[$i]{attr}, $i);
  99.         }
  100.         elsif ('hidden' eq $column_info[$i]{attr})
  101.         {
  102.             # skip hidden column
  103.         }
  104.         else
  105.         {
  106.             my $column = Gtk2::TreeViewColumn->new_with_attributes (
  107.                 $column_info[$i]{title},
  108.                 $column_info[$i]{rtype}->new,
  109.                 $column_info[$i]{attr} => $i,
  110.             );
  111.             $view->append_column ($column);
  112.     
  113.             if ($column_info[$i]{attr} eq 'active') {
  114.                 # make boolean columns respond to editing.
  115.                 my $r = $column->get_cell_renderers;
  116.                 $r->set (activatable => 1);
  117.                 $r->{column_index} = $i;
  118.                 $r->signal_connect (toggled => sub {
  119.                     my ($renderer, $row, $slist) = @_;
  120.                     my $col = $renderer->{column_index};
  121.                     my $model = $slist->get_model;
  122.                     my $iter = $model->iter_nth_child (undef, $row);
  123.                     my $val = $model->get ($iter, $col);
  124.                     $model->set ($iter, $col, !$val);
  125.                     }, $view);
  126.  
  127.             } elsif ($column_info[$i]{attr} eq 'text') {
  128.                 # attach a decent 'edited' callback to any
  129.                 # columns using a text renderer.  we do NOT
  130.                 # turn on editing by default.
  131.                 my $r = $column->get_cell_renderers;
  132.                 $r->{column} = $i;
  133.                 $r->signal_connect
  134.                     (edited => \&text_cell_edited, $view);
  135.             }
  136.         }
  137.     }
  138.  
  139.     my @a;
  140.     tie @a, 'Gtk2::SimpleList::TiedList', $model;
  141.  
  142.     $view->{data} = \@a;
  143.     return bless $view, $class;
  144. }
  145.  
  146. sub set_column_editable {
  147.     my ($self, $index, $editable) = @_;
  148.     my $column = $self->get_column ($index);
  149.     croak "invalid column index $index"
  150.         unless defined $column;
  151.     my $cell_renderer = $column->get_cell_renderers;
  152.     $cell_renderer->set (editable => $editable);
  153. }
  154.  
  155. sub get_column_editable {
  156.     my ($self, $index, $editable) = @_;
  157.     my $column = $self->get_column ($index);
  158.     croak "invalid column index $index"
  159.         unless defined $column;
  160.     my $cell_renderer = $column->get_cell_renderers;
  161.     return $cell_renderer->get ('editable');
  162. }
  163.  
  164. sub get_selected_indices {
  165.     my $self = shift;
  166.     my $selection = $self->get_selection;
  167.     return () unless $selection;
  168.     # warning: this assumes that the TreeModel is actually a ListStore.
  169.     # if the model is a TreeStore, get_indices will return more than one
  170.     # index, which tells you how to get all the way down into the tree,
  171.     # but all the indices will be squashed into one array... so, ah,
  172.     # don't use this for TreeStores!
  173.     map { $_->get_indices } $selection->get_selected_rows;
  174. }
  175.  
  176. sub select {
  177.     my $self = shift;
  178.     my $selection = $self->get_selection;
  179.     my @inds = (@_ > 1 && $selection->get_mode ne 'multiple')
  180.              ? $_[0]
  181.          : @_;
  182.     my $model = $self->get_model;
  183.     foreach my $i (@inds) {
  184.         my $iter = $model->iter_nth_child (undef, $i);
  185.         next unless $iter;
  186.         $selection->select_iter ($iter);
  187.     }
  188. }
  189.  
  190. sub unselect {
  191.     my $self = shift;
  192.     my $selection = $self->get_selection;
  193.     my @inds = (@_ > 1 && $selection->get_mode ne 'multiple')
  194.              ? $_[0]
  195.          : @_;
  196.     my $model = $self->get_model;
  197.     foreach my $i (@inds) {
  198.         my $iter = $model->iter_nth_child (undef, $i);
  199.         next unless $iter;
  200.         $selection->unselect_iter ($iter);
  201.     }
  202. }
  203.  
  204. sub set_data_array
  205. {
  206.     @{$_[0]->{data}} = @{$_[1]};
  207. }
  208.  
  209. sub get_row_data_from_path
  210. {
  211.     my ($self, $path) = @_;
  212.  
  213.     # $path->get_depth always 1 for SimpleList
  214.     # my $depth = $path->get_depth;
  215.  
  216.     # array has only one member for SimpleList
  217.     my @indices = $path->get_indices;
  218.     my $index = $indices[0];
  219.  
  220.     return $self->{data}->[$index];
  221. }
  222.  
  223. ##################################
  224. package Gtk2::SimpleList::TiedRow;
  225.  
  226. use strict;
  227. use Gtk2;
  228. use Carp;
  229.  
  230. =cut
  231.  
  232. TiedRow is the lowest-level tie, allowing you to treat a row as an array
  233. of column data.
  234.  
  235. =cut
  236.  
  237. sub TIEARRAY {
  238.     my $class = shift;
  239.     my $model = shift;
  240.     my $iter = shift;
  241.  
  242.     croak "usage tie (\@ary, 'class', model, iter)"
  243.         unless $model && UNIVERSAL::isa ($model, 'Gtk2::TreeModel');
  244.  
  245.     return bless {
  246.         model => $model,
  247.         iter => $iter,
  248.     }, $class;
  249. }
  250.  
  251. sub FETCH { # this, index
  252.     return $_[0]->{model}->get ($_[0]->{iter}, $_[1]);
  253. }
  254.  
  255. sub STORE { # this, index, value
  256.     return $_[0]->{model}->set ($_[0]->{iter}, $_[1], $_[2])
  257.         if defined $_[2]; # allow 0, but not undef
  258. }
  259.  
  260. sub FETCHSIZE { # this
  261.     return $_[0]{model}->get_n_columns;
  262. }
  263.  
  264. sub EXISTS { 
  265.     return( $_[1] < $_[0]{model}->get_n_columns );
  266. }
  267.  
  268. sub EXTEND { } # can't change the length, ignore
  269. sub CLEAR { } # can't change the length, ignore
  270.  
  271. sub new {
  272.     my ($class, $model, $iter) = @_;
  273.     my @a;
  274.     tie @a, __PACKAGE__, $model, $iter;
  275.     return \@a;
  276. }
  277.  
  278. sub POP { croak "pop called on a TiedRow, but you can't change its size"; }
  279. sub PUSH { croak "push called on a TiedRow, but you can't change its size"; }
  280. sub SHIFT { croak "shift called on a TiedRow, but you can't change its size"; }
  281. sub UNSHIFT { croak "unshift called on a TiedRow, but you can't change its size"; }
  282. sub SPLICE { croak "splice called on a TiedRow, but you can't change its size"; }
  283. #sub DELETE { croak "delete called on a TiedRow, but you can't change its size"; }
  284. sub STORESIZE { carp "STORESIZE operation not supported"; }
  285.  
  286.  
  287. ###################################
  288. package Gtk2::SimpleList::TiedList;
  289.  
  290. use strict;
  291. use Gtk2;
  292. use Carp;
  293.  
  294. =cut
  295.  
  296. TiedList is an array in which each element is a row in the liststore.
  297.  
  298. =cut
  299.  
  300. sub TIEARRAY {
  301.     my $class = shift;
  302.     my $model = shift;
  303.  
  304.     croak "usage tie (\@ary, 'class', model)"
  305.         unless $model && UNIVERSAL::isa ($model, 'Gtk2::TreeModel');
  306.  
  307.     return bless {
  308.         model => $model,
  309.     }, $class;
  310. }
  311.  
  312. sub FETCH { # this, index
  313.     my $iter = $_[0]->{model}->iter_nth_child (undef, $_[1]);
  314.     return undef unless defined $iter;
  315.     my @row;
  316.     tie @row, 'Gtk2::SimpleList::TiedRow', $_[0]->{model}, $iter;
  317.     return \@row;
  318. }
  319.  
  320. sub STORE { # this, index, value
  321.     my $iter = $_[0]->{model}->iter_nth_child (undef, $_[1]);
  322.     $iter = $_[0]->{model}->insert ($_[1])
  323.         if not defined $iter;
  324.     my @row;
  325.     tie @row, 'Gtk2::SimpleList::TiedRow', $_[0]->{model}, $iter;
  326.     if ('ARRAY' eq ref $_[2]) {
  327.         @row = @{$_[2]};
  328.     } else {
  329.         $row[0] = $_[2];
  330.     }
  331.  
  332.     return $_[2];
  333. }
  334.  
  335. sub FETCHSIZE { # this
  336.     return $_[0]->{model}->iter_n_children (undef);
  337. }
  338.  
  339. sub PUSH { # this, list
  340.     my $model = shift()->{model};
  341.     my $iter;
  342.     foreach (@_)
  343.     {
  344.         $iter = $model->append;
  345.         my @row;
  346.         tie @row, 'Gtk2::SimpleList::TiedRow', $model, $iter;
  347.         if ('ARRAY' eq ref $_) {
  348.             @row = @$_;
  349.         } else {
  350.             $row[0] = $_;
  351.         }
  352.     }
  353.     return $model->iter_n_children (undef);
  354. }
  355.  
  356. sub POP { # this
  357.     my $model = $_[0]->{model};
  358.     my $index = $model->iter_n_children-1;
  359.     my $iter = $model->iter_nth_child(undef, $index);
  360.     return undef unless $iter;
  361.     my $ret = [ $model->get ($iter) ];
  362.     $model->remove($iter) if( $index >= 0 );
  363.     return $ret;
  364. }
  365.  
  366. sub SHIFT { # this
  367.     my $model = $_[0]->{model};
  368.     my $iter = $model->iter_nth_child(undef, 0);
  369.     return undef unless $iter;
  370.     my $ret = [ $model->get ($iter) ];
  371.     $model->remove($iter) if( $model->iter_n_children );
  372.     return $ret;
  373. }
  374.  
  375. sub UNSHIFT { # this, list
  376.     my $model = shift()->{model};
  377.     my $iter;
  378.     foreach (@_)
  379.     {
  380.         $iter = $model->prepend;
  381.         my @row;
  382.         tie @row, 'Gtk2::SimpleList::TiedRow', $model, $iter;
  383.         if ('ARRAY' eq ref $_) {
  384.             @row = @$_;
  385.         } else {
  386.             $row[0] = $_;
  387.         }
  388.     }
  389.     return $model->iter_n_children (undef);
  390. }
  391.  
  392. # note: really, arrays aren't supposed to support the delete operator this
  393. #       way, but we don't want to break existing code.
  394. sub DELETE { # this, key
  395.     my $model = $_[0]->{model};
  396.     my $ret;
  397.     if ($_[1] < $model->iter_n_children (undef)) {
  398.         my $iter = $model->iter_nth_child (undef, $_[1]);
  399.         return undef unless $iter;
  400.         $ret = [ $model->get ($iter) ];
  401.         $model->remove ($iter);
  402.     }
  403.     return $ret;
  404. }
  405.  
  406. sub CLEAR { # this
  407.     $_[0]->{model}->clear;
  408. }
  409.  
  410. # note: arrays aren't supposed to support exists, either.
  411. sub EXISTS { # this, key
  412.     return( $_[1] < $_[0]->{model}->iter_n_children );
  413. }
  414.  
  415. # we can't really, reasonably, extend the tree store in one go, it will be 
  416. # extend as items are added
  417. sub EXTEND {}
  418.  
  419. sub get_model {
  420.     return $_[0]{model};
  421. }
  422.  
  423. sub STORESIZE { carp "STORESIZE: operation not supported"; }
  424.  
  425. sub SPLICE { # this, offset, length, list
  426.     my $self = shift;
  427.     # get the model and the number of rows    
  428.     my $model = $self->{model};
  429.     # get the offset
  430.     my $offset = shift || 0;
  431.     # if offset is neg, invert it
  432.     $offset = $model->iter_n_children (undef) + $offset if ($offset < 0);
  433.     # get the number of elements to remove
  434.     my $length = shift;
  435.     # if len was undef, not just false, calculate it
  436.     $length = $self->FETCHSIZE() - $offset unless (defined ($length));
  437.     # get any elements we need to insert into their place
  438.     my @list = @_;
  439.     
  440.     # place to store any returns
  441.     my @ret = ();
  442.  
  443.     # remove the desired elements
  444.     my $ret;
  445.     for (my $i = $offset; $i < $offset+$length; $i++)
  446.     {
  447.         # things will be shifting forward, so always delete at offset
  448.         $ret = $self->DELETE ($offset);
  449.         push @ret, $ret if defined $ret;
  450.     }
  451.  
  452.     # insert the passed list at offset in reverse order, so the will
  453.     # be in the correct order
  454.     foreach (reverse @list)
  455.     {
  456.         # insert a new row
  457.         $model->insert ($offset);
  458.         # and put the data in it
  459.         $self->STORE ($offset, $_);
  460.     }
  461.     
  462.     # return deleted rows in array context, the last row otherwise
  463.     # if nothing deleted return empty
  464.     return (@ret ? (wantarray ? @ret : $ret[-1]) : ());
  465. }
  466.  
  467. 1;
  468.  
  469. __END__
  470. # documentation is a good thing.
  471.  
  472. =head1 NAME
  473.  
  474. Gtk2::SimpleList - A simple interface to Gtk2's complex MVC list widget
  475.  
  476. =head1 SYNOPSIS
  477.  
  478.   use Glib qw(TRUE FALSE);
  479.   use Gtk2 '-init';
  480.   use Gtk2::SimpleList;
  481.  
  482.   my $slist = Gtk2::SimpleList->new (
  483.                 'Text Field'    => 'text',
  484.                 'Markup Field'  => 'markup',
  485.                 'Int Field'     => 'int',
  486.                 'Double Field'  => 'double',
  487.                 'Bool Field'    => 'bool',
  488.                 'Scalar Field'  => 'scalar',
  489.                 'Pixbuf Field'  => 'pixbuf',
  490.               );
  491.  
  492.   @{$slist->{data}} = (
  493.           [ 'text', 1, 1.1,  TRUE, $var, $pixbuf ],
  494.           [ 'text', 2, 2.2, FALSE, $var, $pixbuf ],
  495.   );
  496.  
  497.   # (almost) anything you can do to an array you can do to 
  498.   # $slist->{data} which is an array reference tied to the list model
  499.   push @{$slist->{data}}, [ 'text', 3, 3.3, TRUE, $var, $pixbuf ];
  500.  
  501.   # mess with selections
  502.   $slist->get_selection->set_mode ('multiple');
  503.   $slist->get_selection->unselect_all;
  504.   $slist->select (1, 3, 5..9); # select rows by index
  505.   $slist->unselect (3, 8); # unselect rows by index
  506.   @sel = $slist->get_selected_indices;
  507.  
  508.   # simple way to make text columns editable
  509.   $slist->set_column_editable ($col_num, TRUE);
  510.  
  511.   # Gtk2::SimpleList derives from Gtk2::TreeView, so all methods
  512.   # on a treeview are available.
  513.   $slist->set_rules_hint (TRUE);
  514.   $slist->signal_connect (row_activated => sub {
  515.           my ($sl, $path, $column) = @_;
  516.       my $row_ref = $sl->get_row_data_from_path ($path);
  517.       # $row_ref is now an array ref to the double-clicked row's data.
  518.       });
  519.  
  520.   # turn an existing TreeView into a SimpleList; useful for
  521.   # Glade-generated interfaces.
  522.   $simplelist = Gtk2::SimpleList->new_from_treeview (
  523.                     $glade->get_widget ('treeview'),
  524.                     'Text Field'    => 'text',
  525.                     'Int Field'     => 'int',
  526.                     'Double Field'  => 'double',
  527.                  );
  528.  
  529. =head1 ABSTRACT
  530.  
  531. SimpleList is a simple interface to the powerful but complex Gtk2::TreeView
  532. and Gtk2::ListStore combination, implementing using tied arrays to make
  533. thing simple and easy.
  534.  
  535. =head1 DESCRIPTION
  536.  
  537. Gtk2 has a powerful, but complex MVC (Model, View, Controller) system used to
  538. implement list and tree widgets.  Gtk2::SimpleList automates the complex setup
  539. work and allows you to treat the list model as a more natural list of lists
  540. structure.
  541.  
  542. After creating a new Gtk2::SimpleList object with the desired columns you may
  543. set the list data with a simple Perl array assignment. Rows may be added or
  544. deleted with all of the normal array operations. You can treat the C<data>
  545. member of the list simplelist object as an array reference, and manipulate the
  546. list data with perl's normal array operators.
  547.  
  548. A mechanism has also been put into place allowing columns to be Perl scalars.
  549. The scalar is converted to text through Perl's normal mechanisms and then
  550. displayed in the list. This same mechanism can be expanded by defining
  551. arbitrary new column types before calling the new function. 
  552.  
  553. =head1 OBJECT HIERARCHY
  554.  
  555.  Glib::Object
  556.  +--- Gtk2::Object
  557.       +--- Gtk2::Widget
  558.            +--- Gtk2::TreeView
  559.             +--- Gtk2::SimpleList
  560.  
  561. =head1 METHODS
  562.  
  563. =over
  564.  
  565. =item $slist = Gtk2::SimpleList->new ($cname, $ctype, ...)
  566.  
  567. =over
  568.  
  569. =over
  570.  
  571. =item * $cname (string)
  572.  
  573. =item * $ctype (string)
  574.  
  575. =back
  576.  
  577. =back
  578.  
  579. Creates a new Gtk2::SimpleList object with the specified columns. The parameter
  580. C<cname> is the name of the column, what will be displayed in the list headers if
  581. they are turned on. The parameter ctype is the type of the column, one of:
  582.  
  583.  text    normal text strings
  584.  markup  pango markup strings
  585.  int     integer values
  586.  double  double-precision floating point values
  587.  bool    boolean values, displayed as toggle-able checkboxes
  588.  scalar  a perl scalar, displayed as a text string by default
  589.  pixbuf  a Gtk2::Gdk::Pixbuf
  590.  
  591. or the name of a custom type you add with C<add_column_type>.
  592. These should be provided in pairs according to the desired columns for your
  593. list.
  594.  
  595. =item $slist = Gtk2::SimpleList->new_from_treeview ($treeview, $cname, $ctype, ...)
  596.  
  597. =over
  598.  
  599. =over
  600.  
  601. =item * $treeview (Gtk2::TreeView)
  602.  
  603. =item * $cname (string)
  604.  
  605. =item * $ctype (string)
  606.  
  607. =back
  608.  
  609. =back
  610.  
  611. Like C<< Gtk2::SimpleList->new() >>, but turns an existing Gtk2::TreeView into
  612. a Gtk2::SimpleList.  This is intended mostly for use with stuff like Glade,
  613. where the widget is created for you.  This will create and attach a new model
  614. and remove any existing columns from I<treeview>.  Returns I<treeview>,
  615. re-blessed as a Gtk2::SimpleList.
  616.  
  617. =item $slist->set_data_array ($arrayref)
  618.  
  619. =over
  620.  
  621. =over
  622.  
  623. =item * $arrayref (array reference)
  624.  
  625. =back
  626.  
  627. =back
  628.  
  629. Set the data in the list to the array reference $arrayref. This is completely
  630. equivalent to @{$list->{data}} = @{$arrayref} and is only here for convenience
  631. and for those programmers who don't like to type-cast and have static, set once
  632. data.
  633.  
  634. =item @indices = $slist->get_selected_indices
  635.  
  636. Return the indices of the selected rows in the ListStore.
  637.  
  638. =item $slist->get_row_data_from_path ($path)
  639.  
  640. =over
  641.  
  642. =over
  643.  
  644. =item * $path (Gtk2::TreePath) the path of the desired row 
  645.  
  646. =back
  647.  
  648. =back
  649.  
  650. Returns an array ref with the data of the row indicated by $path.
  651.  
  652. =item $slist->select ($index, ...);
  653.  
  654. =item $slist->unselect ($index, ...);
  655.  
  656. =over
  657.  
  658. =over
  659.  
  660. =item * $index (integer)
  661.  
  662. =back
  663.  
  664. =back
  665.  
  666. Select or unselect rows in the list by index.  If the list is set for multiple
  667. selection, all indices in the list will be set/unset; otherwise, just the
  668. first is used.  If the list is set for no selection, then nothing happens.
  669.  
  670. To set the selection mode, or to select all or none of the rows, use the normal
  671. TreeView/TreeSelection stuff, e.g.  $slist->get_selection and the TreeSelection
  672. methods C<get_mode>, C<set_mode>, C<select_all>, and C<unselect_all>.
  673.  
  674. =item $slist->set_column_editable ($index, $editable)
  675.  
  676. =over
  677.  
  678. =over
  679.  
  680. =item * $index (integer)
  681.  
  682. =item * $editable (boolean)
  683.  
  684. =back
  685.  
  686. =back
  687.  
  688. =item boolean = $slist->get_column_editable ($index)
  689.  
  690. =over
  691.  
  692. =over
  693.  
  694. =item * $index (integer)
  695.  
  696. =back
  697.  
  698. =back
  699.  
  700. This is a very simple interface to Gtk2::TreeView's editable text column cells.
  701. All columns which use the attr "text" (basically, any text or number column,
  702. see C<add_column_type>) automatically have callbacks installed to update data
  703. when cells are edited.  With C<set_column_editable>, you can enable the
  704. in-place editing.
  705.  
  706. C<get_column_editable> tells you if column I<index> is currently editable.
  707.  
  708. =item Gtk2::SimpleList->add_column_type ($type_name, ...)
  709.  
  710.  
  711. =over
  712.  
  713. =over
  714.  
  715. =item $type_name (string)
  716.  
  717. =back
  718.  
  719. =back
  720.  
  721. Add a new column type to the list of possible types. Initially six column types
  722. are defined, text, int, double, bool, scalar, and pixbuf. The bool column type
  723. uses a toggle cell renderer, the pixbuf uses a pixbuf cell renderer, and the
  724. rest use text cell renderers. In the process of adding a new column type you
  725. may use any cell renderer you wish. 
  726.  
  727. The first parameter is the column type name, the list of six are examples.
  728. There are no restrictions on the names and you may even overwrite the existing
  729. ones should you choose to do so. The remaining parameters are the type
  730. definition consisting of key value pairs. There are three required: type,
  731. renderer, and attr. The type key determines what actual datatype will be
  732. stored in the underlying model representation; this is a package name, e.g.
  733. Glib::String, Glib::Int, Glib::Boolean, but in general if you want an
  734. arbitrary Perl data structure you will want to use 'Glib::Scalar'. The
  735. renderer key should hold the class name of the cell renderer to create for this
  736. column type; this may be any of Gtk2::CellRendererText,
  737. Gtk2::CellRendererToggle, Gtk2::CellRendererPixbuf, or some other, possibly
  738. custom, cell renderer class.  The attr key is magical; it may be either a
  739. string, in which case it specifies the attribute which will be set from the
  740. specified column (e.g. 'text' for a text renderer, 'active' for a toggle
  741. renderer, etc), or it may be a reference to a subroutine which will be called
  742. each time the renderer needs to draw the data.
  743.  
  744. This function, described as a GtkTreeCellDataFunc in the API reference, 
  745. will receive 5 parameters: $treecol, $cell, $model, $iter,
  746. $col_num (when SimpleList hooks up the function, it sets the column number to
  747. be passed as the user data).  The data value for the particular cell in question
  748. is available via $model->get ($iter, $col_num); you can then do whatever it is
  749. you have to do to render the cell the way you want.  Here are some examples:
  750.  
  751.   # just displays the value in a scalar as 
  752.   # Perl would convert it to a string
  753.   Gtk2::SimpleList->add_column_type( 'a_scalar', 
  754.           type     => 'Glib::Scalar',
  755.       renderer => 'Gtk2::CellRendererText',
  756.           attr     => sub {
  757.                my ($treecol, $cell, $model, $iter, $col_num) = @_;
  758.                my $info = $model->get ($iter, $col_num);
  759.                $cell->set (text => $info);
  760.       }
  761.      );
  762.  
  763.   # sums up the values in an array ref and displays 
  764.   # that in a text renderer
  765.   Gtk2::SimpleList->add_column_type( 'sum_of_array', 
  766.           type     => 'Glib::Scalar',
  767.       renderer => 'Gtk2::CellRendererText',
  768.           attr     => sub {
  769.                my ($treecol, $cell, $model, $iter, $col_num) = @_;
  770.                my $sum = 0;
  771.                my $info = $model->get ($iter, $col_num);
  772.                foreach (@$info)
  773.                {
  774.                    $sum += $_;
  775.                }
  776.                $cell->set (text => $sum);
  777.           } 
  778.      );
  779.  
  780. =back
  781.  
  782. =head1 MODIFYING LIST DATA
  783.  
  784. After creating a new Gtk2::SimpleList object there will be a member called C<data>
  785. which is a tied array. That means data may be treated as an array, but in
  786. reality the data resides in something else. There is no need to understand the
  787. details of this it just means that you put data into, take data out of, and
  788. modify it just like any other array. This includes using array operations like
  789. push, pop, unshift, and shift. For those of you very familiar with perl this
  790. section will prove redundant, but just in case:
  791.  
  792.   Adding and removing rows:
  793.   
  794.     # push a row onto the end of the list
  795.     push @{$slist->{data}}, [col1_data, col2_data, ..., coln_data];
  796.     # pop a row off of the end of the list
  797.     $rowref = pop @{$slist->{data}};
  798.     # unshift a row onto the beginning of the list
  799.     unshift @{$slist->{data}}, [col1_data, col2_data, ..., coln_data];
  800.     # shift a row off of the beginning of the list
  801.     $rowref = shift @{$slist->{data}};
  802.     # delete the row at index $n, 0 indexed
  803.     splice @{ $slist->{data} }, $n, 1;
  804.     # set the entire list to be the data in a array
  805.     @{$slist->{data}} = ( [row1, ...], [row2, ...], [row3, ...] );
  806.  
  807.   Getting at the data in the list:
  808.   
  809.     # get an array reference to the entire nth row
  810.     $rowref = $slist->{data}[n];
  811.     # get the scalar in the mth column of the nth row, 0 indexed
  812.     $val = $slist->{data}[n][m];
  813.     # set an array reference to the entire nth row
  814.     $slist->{data}[n] = [col1_data, col2_data, ..., coln_data];
  815.     # get the scalar in the mth column of the nth row, 0 indexed
  816.     $slist->{data}[n][m] = $rowm_coln_value;
  817.  
  818. =head1 SEE ALSO
  819.  
  820. Perl(1), Glib(3pm), Gtk2(3pm), Gtk2::TreeView(3pm), Gtk2::TreeModel(3pm),
  821. Gtk2::ListStore(3pm).
  822.  
  823. =head1 AUTHORS
  824.  
  825.  muppet <scott at asofyet dot org>
  826.  Ross McFarland <rwmcfa1 at neces dot com>
  827.  Gavin Brown <gavin dot brown at uk dot com>
  828.  
  829. =head1 COPYRIGHT AND LICENSE
  830.  
  831. Copyright 2003-2004 by the Gtk2-Perl team.
  832.  
  833. This library is free software; you can redistribute it and/or modify it under
  834. the terms of the GNU Library General Public License as published by the Free
  835. Software Foundation; either version 2.1 of the License, or (at your option) any
  836. later version.
  837.  
  838. This library is distributed in the hope that it will be useful, but WITHOUT ANY
  839. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  840. PARTICULAR PURPOSE.  See the GNU Library General Public License for more
  841. details.
  842.  
  843. You should have received a copy of the GNU Library General Public License along
  844. with this library; if not, write to the Free Software Foundation, Inc., 59
  845. Temple Place - Suite 330, Boston, MA  02111-1307  USA.
  846.  
  847. =cut
  848.