Re: Gridlines in Gtk2::TreeView

this is really a question for gtk-app-devel-list, as it's not actually perl-specific.

On Aug 27, 2004, at 5:51 PM, ofey aikon wrote:

Is there a way I can get gridlines separating columns (and rows too, perhaps) using Gtk2::Treeview. Like a spreadsheet, for example. Its much easier to spot which cell belongs to which column that way.

TreeView is explicitly not designed to be a grid, so doing spreadsheetish things with it will be hard.

Is this something that should be done using themes ?

my gut says yes, but i don't know any particulars.

Is there an alternate solution to my problem ?

an Evil But Fun solution to try would be to create a custom cell renderer that simply draws a skinny vertical separator, and pack one of those into each of the columns after the first. this could have performance problems if you have a lot of data (since you'll be marshaling a *lot* of vfunc calls to perl), and would not be terribly theme-friendly, but it would be an interesting experiement.

alternatively, you could set the background color for every other column's renderer to a different color to create vertical striping, like this:

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

my $ncols = 10;
my $nrows = 10;

# list store with a bunch of columns and several rows of random data
my $model = Gtk2::ListStore->new (('Glib::String')x$ncols);
for (1..$nrows) {
        $model->set ($model->append,
                     map { $_, sprintf ("%8.3f", rand) } 0..$ncols-1);

# view with a text renderer for each column
my $view = Gtk2::TreeView->new ($model);
for (0..$ncols-1) {
        my $cell = Gtk2::CellRendererText->new;
        # make every other cell gray.  this uses a hardcoded color, so it
        # won't play nicely with themes, but at least shows that it's easy
        # to set the color once you figure out what you want it to be.
        # there's also the cell-background-gdk property if you have a
        # Gtk2::Gdk::Color instead of a string.
        $cell->set (cell_background => '#eeeeee')
                if $_ % 2;
        $view->append_column (Gtk2::TreeViewColumn->new_with_attributes
                                        ("col $_", $cell, text => $_));

my $window = Gtk2::Window->new;
$window->signal_connect (destroy => sub {Gtk2->main_quit});
$window->add ($view);

Holy crap, dude, we have kids!
        -- Elysse, six days after giving birth to twins

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