Re: Glib::ParamSpec->enum or boxed




Jeffrey Ratcliffe wrote:
Below is some demo code for composite widget. At the moment, it used
Glib::ParamSpec->string for the property, but as only three values are
possible, enum or boxed I assume would be better - but I can't see how
to do it.

Any hints?

Super easy.  Changes to your code inline below:


Thanks

Jeff

#!/usr/bin/perl

package Foo::PageRange;

use strict;
use warnings;
use Gtk2;
use Glib qw(TRUE FALSE);             # To get TRUE and FALSE

# Note: in a BEGIN block to ensure that the registration is complete
#       by the time the use Subclass goes to look for it.
BEGIN {
    Glib::Type->register_enum ('Foo::PageRange::Range',
                                qw(current selected all));
}

use Glib::Object::Subclass
    Gtk2::Frame::,
    signals => {
        changed => {},
    },
    properties => [

# The only real difference here is enum instead of string, and adding
# the enumeration's package name.
        Glib::ParamSpec->enum (
                'range', # name
                'Range', # nickname
                'Either current, selected, or all', #blurb
                'Foo::PageRange::Range',
                'all', # default
                [qw/readable writable/] #flags

    ]
    ;


sub INIT_INSTANCE {
 my ($self, $range) = @_;
 $range = 'all' if (!$range);
 $self->{range} = $range;
 my $frame = Gtk2::Frame->new;
 $self->add ($frame);
  my $vbox = Gtk2::VBox -> new;
# $vboxp -> set_border_width($border_width);
  $frame -> add ($vbox);

# Instead of repetitive and error-prone code to manage the radio
# buttons by hand, let's play golf, using a single loop and some
# introspection to do the whole thing:

  my $radio = undef;
  foreach my $value (Glib::Type->list_values ('Foo::PageRange::Range')) {
    $radio = Gtk2::RadioButton->new ($radio, ucfirst $value->{nick});
    $radio->signal_connect (toggled => sub {
      $self->set_active ($value->{nick}) if $_[0]->get_active;
    });
    $vbox->pack_start ($radio, TRUE, TRUE, 0);
    $radio->set_active (TRUE) if $self->{range} eq $value->{nick};
  }

# The rest is unchanged.


}


sub get_active {
 my ($self) = @_;
 return $self->get ('range');
}


sub set_active {
 my ($self, $range) = @_;
 $self->{range} = $range;
 $self->signal_emit ('changed');
}


package main;

use strict;
use warnings;
use Glib qw(TRUE FALSE);             # To get TRUE and FALSE
use Gtk2 -init;

my $window = Gtk2::Window->new;
$window->set_title ('Page Range');
$window->signal_connect (delete_event => sub { Gtk2->main_quit; TRUE });

my $vbox = Gtk2::VBox->new;
$window->add ($vbox);

my $pr = Foo::PageRange->new;
$vbox->pack_start ($pr, TRUE, TRUE, 0);

$pr->signal_connect (changed => sub {
 warn $_[0]->get_active, "\n";
});

$window->show_all;
Gtk2->main;
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list



-- 
muppet <scott at asofyet dot org>




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