File Coverage

blib/lib/Gtk2/Ex/ComboBoxBits.pm
Criterion Covered Total %
statement 12 41 29.2
branch 0 18 0.0
condition 0 6 0.0
subroutine 4 9 44.4
pod 4 4 100.0
total 20 78 25.6


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::ComboBoxBits;
19 3     3   31563 use 5.008;
  3         14  
  3         154  
20 3     3   19 use strict;
  3         8  
  3         106  
21 3     3   18 use warnings;
  3         10  
  3         115  
22              
23 3     3   18 use Exporter;
  3         6  
  3         1566  
24             our @ISA = ('Exporter');
25             our @EXPORT_OK = qw(get_active_path
26             set_active_path
27             set_active_text
28             find_text_iter);
29              
30             # uncomment this to run the ### lines
31             #use Smart::Comments;
32              
33             our $VERSION = 32;
34              
35             #------------------------------------------------------------------------------
36              
37             sub get_active_path {
38 0     0 1   my ($combobox) = @_;
39 0           my ($model, $iter);
40 0   0       return (($model = $combobox->get_model)
41             && ($iter = $combobox->get_active_iter)
42             && $model->get_path ($combobox->get_active_iter));
43             }
44              
45             sub set_active_path {
46 0     0 1   my ($combobox, $path) = @_;
47             ### set_active_path()
48              
49             # Non-existent rows go to set_active(-1) since the Perl-Gtk2
50             # set_active_iter() doesn't accept undef (NULL) until 1.240.
51             # If ready to demand that version could
52             # $combobox->set_active_iter ($model && $path && $model->get_iter($path));
53              
54 0 0         if ($path) {
55 0 0         if ($path->get_depth == 1) {
56             # top-level row using set_active()
57 0           $combobox->set_active ($path->get_indices);
58 0           return;
59             }
60              
61 0 0         if (my $model = $combobox->get_model) {
62 0 0         if (my $iter = $model->get_iter($path)) {
63 0           $combobox->set_active_iter ($iter);
64 0           return;
65             }
66             }
67             }
68             # path=undef, or no model, or path not in model
69             # FIXME: ->set_active(-1) spits a warning in gtk 2.28 if no model,
70             # want Gtk2 1.240 for ->set_active_iter(undef)
71 0 0         if ($combobox->get_model) {
72 0           $combobox->set_active (-1);
73             }
74             }
75              
76             #------------------------------------------------------------------------------
77              
78             sub set_active_text {
79 0     0 1   my ($combobox, $str) = @_;
80             ### ComboBoxBits set_active_text(): $str
81              
82 0 0 0       if (defined $str && (my $iter = find_text_iter ($combobox, $str))) {
83             ### $iter
84 0           $combobox->set_active_iter ($iter);
85             } else {
86             # As of Gtk 2.20 set_active() throws a g_log() warning if there's no
87             # model set. Prefer to quietly do nothing to make no active item when
88             # already no active item.
89 0 0         if ($combobox->get_model) {
90             # pending perl-gtk 1.240 set_active_iter() accepting undef
91 0           $combobox->set_active (-1);
92             }
93             }
94             ### set_active_text() active num now: $combobox->get_active
95             }
96              
97             sub find_text_iter {
98 0     0 1   my ($combobox, $str) = @_;
99             ### ComboBoxBits find_text_iter(): $str
100 0           my $ret;
101 0 0         if (my $model = $combobox->get_model) {
102             $model->foreach (sub {
103 0     0     my ($model, $path, $iter) = @_;
104             ### get_value: $model->get_value ($iter, 0)
105 0 0         if ($str eq $model->get_value ($iter, 0)) {
106             ### found at: $path->to_string
107 0           $ret = $iter->copy;
108 0           return 1; # stop
109             }
110 0           return 0; # continue
111 0           });
112             }
113             ### $ret
114 0           return $ret;
115             }
116              
117             1;
118             __END__