Re: Simple question about SimpleList




On Aug 16, 2006, at 2:29 PM, Andrius Aštrauskas wrote:

some corrections:

I'm new to this list, and gtk2-perl too.

Welcome!

Is it possible to wrap text (i.e. break long lines) in SimpleList?
Couldn't find it in the docs...

Depends on what version of gtk+ you are using.

In gtk+ 2.0.x - 2.6.x, you're out of luck; the only way to get line breaks in the TreeView's cells is to add "\n"s to the strings yourself.

In gtk+ >= 2.8.0, set set the CellRendererText's wrap-width to something greater than -1 to set the width at which text is wrapped. The default wrap-mode is PANGO_WRAP_CHAR, which can break in the middle of a word, so you might want to set it to "word", instead.

http://developer.gnome.org/doc/API/2.0/gtk/ GtkCellRendererText.html#GtkCellRendererText--wrap-width http://developer.gnome.org/doc/API/2.0/gtk/ GtkCellRendererText.html#GtkCellRendererText--wrap-mode

An obvious drawback is that you specify the cell's wrap width in pixels. In order to have it flow to fill the available space, you must do some potentially nontrivial sizing logic.

Here's a very simple example.

-=-=-=-=-
#!/usr/bin/perl -w
use strict;
use Gtk2 -init;
use Gtk2::SimpleList; # should be Gtk2::Ex::SimpleList...

my $slist = Gtk2::SimpleList->new ('Name' => 'text',
                                   'Blurb' => 'text');
@{ $slist->{data} } = (
['Fred', 'The boy scout with the dandy ascot. Probably snogs Daphne when the group splits up to search.'], ['Daphne', '"Danger-prone Daphne", the attractive but somewhat ditsy red-head.'], ['Velma', 'A smart and strong female who wears glasses and short hair, and thus is called a lesbian by many.'], ['Shaggy', 'Supposedly a beatnik, portrayed as something of a lovable idiot. Loves to eat, and usually manages to save the day despite great cowardice.'], ['Scooby', 'A talking Great Dane with an insatiable appetite for chocolate and anchovy pizza. Amazingly, he is more of a coward that Shaggy.'],
);


# TreeView (SimpleList's parent class) uses a CellRenderer to draw all the # cells in a given TreeViewColumn. We need a reference to the cell renderer # in order to tell it how to wrap. We ask a TreeView for a TreeViewColumn # by index. We can fetch the whole list of cell renderers for that column;
# SimpleList only packs one, so we just get element [0].
my $cell = ($slist->get_column(1)->get_cell_renderers())[0];

# Now, we just set properties.
$cell->set (wrap_mode => 'word',  # wrap at word boundaries
            wrap_width => 200);   # wrap at 200 pixels


my $window = Gtk2::Window->new;
$window->add ($slist);

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



--
She's obviously your child. She looks like you, she talks a lot, and most of it is gibberish.
  -- Elysse, to me, of Zella.




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