File Coverage

blib/lib/Gtk2/Ex/ComboBox/Text.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011 Kevin Ryde
2              
3             # This file is part of Gtk2-Ex-ComboBoxBits.
4             #
5             # Gtk2-Ex-ComboBoxBits is free software; you can redistribute it and/or
6             # modify it under the terms of the GNU General Public License as published
7             # by the Free Software Foundation; either version 3, or (at your option) any
8             # later version.
9             #
10             # Gtk2-Ex-ComboBoxBits is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Gtk2-Ex-ComboBoxBits. If not, see .
17              
18             package Gtk2::Ex::ComboBox::Text;
19 1     1   786 use 5.008;
  1         4  
  1         43  
20 1     1   7 use strict;
  1         2  
  1         37  
21 1     1   4 use warnings;
  1         2  
  1         38  
22 1     1   5 use Carp;
  1         2  
  1         79  
23 1     1   506 use Gtk2;
  0            
  0            
24             use Scalar::Util;
25             use Gtk2::Ex::ComboBoxBits 'set_active_text';
26              
27             # uncomment this to run the ### lines
28             #use Smart::Comments;
29              
30             our $VERSION = 32;
31              
32             use Glib::Object::Subclass
33             'Gtk2::ComboBox',
34             signals => { notify => \&_do_notify },
35             properties => [ Glib::ParamSpec->string
36             ('active-text',
37             'Active text',
38             'The selected text value.',
39             (eval {Glib->VERSION(1.240);1}
40             ? undef # default
41             : ''), # no undef/NULL before Perl-Glib 1.240
42             Glib::G_PARAM_READWRITE),
43              
44              
45             # these are not gettable, so the default doesn't matter,
46             # but give undef
47             #
48             Glib::ParamSpec->string
49             ('append-text',
50             'append-text',
51             'Append a text string.',
52             (eval {Glib->VERSION(1.240);1}
53             ? undef # default
54             : ''), # no undef/NULL before Perl-Glib 1.240
55             ['writable']),
56              
57             Glib::ParamSpec->string
58             ('prepend-text',
59             'prepend-text',
60             'Prepend a text string.',
61             (eval {Glib->VERSION(1.240);1}
62             ? undef # default
63             : ''), # no undef/NULL before Perl-Glib 1.240
64             ['writable']),
65             ];
66              
67             # Gtk2::ComboBox::new_text creates a Gtk2::ComboBox, must override to get a
68             # subclass Gtk2::Ex::ComboBoxBits
69             # could think about offering this as a ComboBox::Subclass mix-in
70             sub new_text {
71             return shift->new(@_);
72             }
73              
74             my $renderer = Gtk2::CellRendererText->new;
75              
76             sub INIT_INSTANCE {
77             my ($self) = @_;
78              
79             # same as gtk_combo_box_new_text(), which alas it doesn't make available
80             # for general use
81             $self->set_model (Gtk2::ListStore->new ('Glib::String'));
82             $self->pack_start ($renderer, 1);
83             $self->set_attributes ($renderer, text => 0);
84             }
85              
86             sub GET_PROPERTY {
87             my ($self) = @_;
88             ### Text GET_PROPERTY: $_[1]->get_name
89              
90             # my $pname = $pspec->get_name;
91             # if ($pname eq 'active_text') {
92             return $self->get_active_text;
93             }
94              
95             sub SET_PROPERTY {
96             my ($self, $pspec, $newval) = @_;
97             ### Text SET_PROPERTY: $pspec->get_name, $newval
98              
99             my $pname = $pspec->get_name;
100             if ($pname eq 'active_text') {
101             $self->set_active_text ($newval);
102             } else {
103             # append_text or prepend_text
104             $self->$pname ($newval);
105             }
106             }
107              
108             # 'notify' class closure
109             sub _do_notify {
110             my ($self, $pspec) = @_;
111             ### ComboBox-Test _do_notify()
112             shift->signal_chain_from_overridden (@_);
113              
114             if ($pspec->get_name eq 'active') {
115             $self->notify ('active-text');
116             }
117             }
118              
119             1;
120             __END__