Gtk2-Ex-ComboBoxBits

 view release on metacpan or  search on metacpan

lib/Gtk2/Ex/ComboBox/Enum.pm  view on Meta::CPAN

use Gtk2::Ex::ComboBoxBits 5; # v.5 for set_active_text() when no model

# uncomment this to run the ### lines
#use Smart::Comments;

our $VERSION = 32;

use Glib::Object::Subclass
  'Gtk2::ComboBox',
  signals => { notify => \&_do_notify,
               'nick-to-display'
               => { param_types   => ['Glib::String'],
                    return_type   => 'Glib::String',
                    flags         => ['action','run-last'],
                    class_closure => \&default_nick_to_display,
                    accumulator   => \&Glib::Ex::SignalBits::accumulator_first_defined,
                  },
             },
  properties => [
                 # FIXME: default enum-type is undef but
                 # Glib::ParamSpec->gtype() comes out as 'Glib::Enum'
                 #
                 (Glib::ParamSpec->can('gtype')
                  ?
                  # gtype() new in Glib 2.10 and Perl-Glib 1.240
                  Glib::ParamSpec->gtype
                  ('enum-type',
                   'Enum type',
                   'The enum class to display.',
                   'Glib::Enum',
                   Glib::G_PARAM_READWRITE)
                  :
                  Glib::ParamSpec->string
                  ('enum-type',
                   'Enum type',
                   'The enum class to display.',
                   (eval {Glib->VERSION(1.240);1}
                    ? undef # default
                    : ''),  # no undef/NULL before Perl-Glib 1.240
                   Glib::G_PARAM_READWRITE)),

                 Glib::ParamSpec->string
                 ('active-nick',
                  'Active enum nick',
                  'The selected enum value, as its nick.',
                  (eval {Glib->VERSION(1.240);1}
                   ? undef # default
                   : ''),  # no undef/NULL before Perl-Glib 1.240
                  Glib::G_PARAM_READWRITE),
                ];

use constant _COLUMN_NICK => 0;

my $renderer = Gtk2::CellRendererText->new;
$renderer->set (ypad => 0);

sub INIT_INSTANCE {
  my ($self) = @_;

  $self->pack_start ($renderer, 1);
  Scalar::Util::weaken (my $weak_self = $self);
  $self->set_cell_data_func ($renderer, \&_cell_data, \$weak_self);
}
sub _cell_data {
  my ($cellview, $renderer, $model, $iter, $ref_weak_self) = @_;
  ### ComboBox-Enum _cell_data()
  ### path: $model->get_path($iter)->to_string
  ### nick: $model->get_value ($iter, _COLUMN_NICK)

  my $self = $$ref_weak_self || return;
  my $nick = $model->get_value ($iter, _COLUMN_NICK);

  # can be called at the $store->append() stage, before nick stored
  if (defined $nick) {
    $nick = $self->signal_emit ('nick-to-display', $nick);
  }
  $renderer->set (text => $nick);
}

sub GET_PROPERTY {
  my ($self, $pspec) = @_;
  my $pname = $pspec->get_name;
  ### Enum GET_PROPERTY: $pname

  if ($pname eq 'active_nick') {
    return $self->get_active_nick;
  }
  # $pname eq 'enum_type'
  return $self->{$pname};
}

sub SET_PROPERTY {
  my ($self, $pspec, $newval) = @_;
  my $pname = $pspec->get_name;
  ### Enum SET_PROPERTY: $pname, $newval

  if ($pname eq 'enum_type') {
    my $enum_type = $self->{$pname} = $newval;

    # preserve active by its nick, if new type has that value
    # set_active() below will notify if the active changes, in particular to
    # -1 if nick not known in the new enum_type
    $newval = $self->get('active-nick');

    # Crib note: In gtk 2.4 believe set_model() doesn't accept NULL,
    # including through set_property(), though a combobox starts out with
    # NULL for the model.  If $enum_type is unset then just clear the
    # existing model, don't try to set undef.
    #
    my $model = $self->get_model;
    if ($model) {
      $model->clear;
    }
    if (defined $enum_type && $enum_type ne '') {
      if (! $model) {
        $model = Gtk2::ListStore->new ('Glib::String');
        $self->set_model ($model);
      }
      my @nicks = map {$_->{'nick'}} Glib::Type->list_values($enum_type);
      #       if ($self->{'sort'}) {
      #         , $self->signal_emit('nick-to-display',$_->{'nick'})



( run in 0.721 second using v1.01-cache-2.11-cpan-d8267643d1d )