Re: TreeViews, IconViews, and images




On May 29, 2006, at 11:57 AM, Wouter Verhelst wrote:

I've been looking over the C API docs and the Perl examples, but I can't
seem to figure out how one is supposed to set up a TreeStore (or a
ListStore) so that it would be possible to show an image in a TreeView
or an IconView. Could someone point me towards a simple example? I did
figure out that it must involve a CellRendererPixbuf somehow, but I
haven't been able to figure out how the pieces fit together. The rest of the treeview works somewhat (I got that far, though not by understaning
everything), but this is really puzzling me

Depends on whether you need generic images or can use stock items (even custom stock items). The custom stock items are very easy, the generic images are also easy, but you do it a different way. For full flexibility, you insert the column with a cell data func and do whatever you want --- the stock browser in Gtk2/examples does that.

But, to cut to the chase with a simple example:


#!/usr/bin/perl -w

use strict;
use Glib ':constants';
use Gtk2 -init;

my $window = Gtk2::Window->new;

#
# Using stock pixbufs is a lot easier than this, because you just store
# the stock id in the model and set up the TreeViewColumn with an attribute
# that sets stock_id on the renderer from that column.
#
# Here, we'll assume you have actual pixbufs in the model, which is the
# generic case.  We will, however, render stock icons for the pixbufs,
# because i'm too lazy to do all the work to set up something that scans
# a directory and thumbnails all the images in it.
#

# model just contains references to actual images.
my $model = Gtk2::ListStore->new ('Gtk2::Gdk::Pixbuf');

$model->set ($model->append, 0, $window->render_icon ($_, 'dialog'))
        foreach (qw(gtk-ok gtk-cancel gtk-open gtk-close));


my $treeview = Gtk2::TreeView->new ($model);
$treeview->set_headers_visible (FALSE);
$treeview->insert_column_with_attributes
                (-1, # append
                 "", # this won't be visible because we turned off headings
                 Gtk2::CellRendererPixbuf->new,  # the renderer
                 pixbuf => 0);  # get the pixbuf property from model col 0

$window->add ($treeview);
$window->show_all;
$window->signal_connect (destroy => sub {Gtk2->main_quit});
Gtk2->main;

__END__



Also, while I'm at it, I would like to set up a TreeView so that
optionally, the user can choose to not display the 'leaf' items in a
TreeStore; those would be shown in the IconView that is going to be
right next to the TreeView, with the icons in the iconview being
changed when the selection on the TreeView changes. Is that possible
without building two TreeStore objects? If so, how?

I would be surprised if you could actually get IconView to use a TreeStore. I think it wants list data (depth=1).

At any rate, the first solution to come to mind would be to store only the directories in the TreeStore attached to the TreeView, and each time the selected node of the tree is changed, populate and attach a new ListStore to the IconView to display the contents of that tree node. This also involves storing less data in memory since you don't have to populate the whole tree.


--
Without treatment, a common cold will last about seven days.
With treatment, it will last about a week.
  -- conventional wisdom




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]