Re: Question about getting values from nested GTK::Boxes

On Feb 7, 2007, at 9:43 AM, Mike Martin wrote:

I have the following function which gets the values from nested h/ vboxes.

[snip code that walks the widget tree]

As you can see I am getting the values several levels deep from
dynamically inserted boxes from a container box

The function works well, but seems very clunky, so my question is
whether there is a better way to do this.

Why not keep a separate list of the entries you want to read? Build the list as the widgets are added, and then you have a reference to each one you're actually interested in without having to walk the widget tree.

Completely untested example code fragments:

# Add a new entry to the box/table/widget tree.  Returns the entry
# that was added (so you can add signals to it), and stores a reference
# to that entry in the list referred to by $toplevel->{entry_list} so they
# can be scraped easily, later.
sub add_file_entry {
    my ($toplevel, $box, $description) = @_;

    my ($label, $entry) = make_entry_and_label ($description);
    my $hbox = Gtk2::HBox->new;
    $hbox->add ($label);
    $hbox->add ($entry);
    $toplevel->{entry_size_group}->add_widget ($entry);

    my $entry_list = $toplevel->{entry_list};
    push @$entry_list, $entry;

    return $entry;

# Collect all the filenames from the entries added by add_file_entry().
sub collect_file_names {
    my ($toplevel) = @_;
    return map { Glib::filename_from_unicode $_->get_text }
            @{ $toplevel->{entry_list} };

I've used variations on this technique in many apps.

Doing a good job around here is like wetting your pants in a dark suit; you get a warm feeling, but no one notices.
  -- unknown

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