Re: Creating new widgets




On Oct 11, 2006, at 1:42 AM, Matthew Braid wrote:

There's also the possibility of doing the same thing inside a ListView with editable cells for the entires. But from what I've seen there are problems with combobox/ entries in listviews, and I haven't seen a cell renderer for buttons.

You'd have to write a CellRendererButton. It's actually not very hard. Entries are easy (CellRendererText), and as of later versions of gtk+, so are comboboxes (CellRendererCombo).


Can anyone point me in the right direction? I can work around this but I've found this MEF widget quite handy in the past.

If you're not willing to use a TreeView, then it sounds to me like a job for boxes and the handy method Gtk2::Container::remove.

Here's a short procedural example. I'll leave turning it into a flexible and reusable class as an exercise for you. (Things you could do with it as a class: emit signals when rows are added and removed, decouple the created widget from the add_row() method so you can create things other than just entries, etc, etc.)


#!/usr/bin/perl -w
use strict;
use Gtk2 -init;

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


sub add_row {
        my ($vbox, $previous) = @_;
        my $hbox = Gtk2::HBox->new;
        my $entry = Gtk2::Entry->new;
        my $add = Gtk2::Button->new ("V");
        my $del = Gtk2::Button->new ("X");
        $hbox->pack_start ($entry, 1, 1, 0);
        $hbox->pack_start ($add, 0, 0, 0);
        $hbox->pack_start ($del, 0, 0, 0);
        $add->signal_connect (clicked => sub {
                my $row = $_[0]->get_parent;
                my $box = $row->get_parent;
                add_row ($box, $row);
        });
        $del->signal_connect (clicked => sub {
                my $row = $_[0]->get_parent;
                my $box = $row->get_parent;
                $box->remove ($row);
        });

        my $i = 0;
        if (defined $previous) {
                foreach my $c ($vbox->get_children) {
                        if ($c == $previous) {
                                last;
                        } else {
                                $i++;
                        }
                }
        }
        $vbox->pack_start ($hbox, 0, 0, 0);
        $vbox->reorder_child ($hbox, $i+1);
        $hbox->show_all;
        return $entry;
}


my $box = Gtk2::VBox->new;
add_row ($box, undef);

$window->add ($box);

$window->show_all;
Gtk2->main;
__END__


--
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]