| 1 | # Copyright 2010 Kevin Ryde | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | # This file is part of Gtk2-Ex-TiedListColumn. | ||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||
| 5 | # Gtk2-Ex-TiedListColumn 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-TiedListColumn is distributed in the hope that it will be useful, | ||||||||||||||||||||||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||||||||||||||||||||||
| 13 | # Public License for more details. | ||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||||||||||||||
| 16 | # with Gtk2-Ex-TiedListColumn. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | package Gtk2::Ex::TiedMenuChildren; | ||||||||||||||||||||||
| 20 | 1 | ▶ | use 5.008; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 21 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 22 | 1 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 23 | 1 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 24 | 1 | ▶ | use Gtk2::Ex::ContainerBits; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 25 | 1 | ▶ | use List::Util qw(min max); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | # uncomment this to run the ### lines | ||||||||||||||||||||||
| 28 | #use Smart::Comments; | ||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 1 | our $VERSION = 5; | |||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | 0 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 33 | 0 | my ($class, $menu) = @_; | |||||||||||||||||||||
| 34 | 0 | tie (my @array, $class, $menu); | |||||||||||||||||||||
| 35 | 0 | return \@array; | |||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | 0 | ▶ | sub TIEARRAY { | ||||||||||||||||||||
|
Subroutine
TIEARRAY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 39 | 0 | my ($class, $menu) = @_; | |||||||||||||||||||||
| 40 | 0 | return bless \$menu, $class; | |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | # optional, not needed | ||||||||||||||||||||||
| 44 | # sub UNTIE { } | ||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | # tied object func | ||||||||||||||||||||||
| 47 | 0 | ▶ | sub menu { | ||||||||||||||||||||
|
Subroutine
menu: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 48 | 0 | return ${$_[0]}; | |||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | # negative indices already normalized to >=0 by the time they get here | ||||||||||||||||||||||
| 52 | 0 | ▶ | sub FETCH { | ||||||||||||||||||||
|
Subroutine
FETCH: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 53 | 0 | my ($self, $index) = @_; | |||||||||||||||||||||
| 54 | #### TiedChildren FETCH: $index | ||||||||||||||||||||||
| 55 | 0 | return (($$self)->get_children)[$index]; | |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||
| 57 | |||||||||||||||||||||||
| 58 | # negative indices already normalized to >=0 by the time they get here | ||||||||||||||||||||||
| 59 | 0 | ▶ | sub STORE { | ||||||||||||||||||||
|
Subroutine
STORE: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||
| 60 | 0 | my ($self, $index, $new) = @_; | |||||||||||||||||||||
| 61 | #### TiedChildren STORE: [ $index, $new ] | ||||||||||||||||||||||
| 62 | 0 | my $menu = $$self; | |||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | 0 | ▶ | if (my $old = $self->FETCH ($index)) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 65 | 0 | ▶ | if ($old == $new) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 66 | 0 | return; # already what's wanted | |||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | 0 | $menu->remove ($old); | |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | 0 | ▶ | if (defined $new) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 72 | 0 | $menu->insert ($new, $index); | |||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | 0 | ▶ | sub FETCHSIZE { | ||||||||||||||||||||
|
Subroutine
FETCHSIZE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 77 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 78 | ### TiedChildren FETCHSIZE | ||||||||||||||||||||||
| 79 | 0 | my @children = ($$self)->get_children; | |||||||||||||||||||||
| 80 | 0 | return scalar(@children); | |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | # big negative sizes normalized to 0 by the time they get here | ||||||||||||||||||||||
| 84 | 0 | ▶ | sub STORESIZE { | ||||||||||||||||||||
|
Subroutine
STORESIZE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 85 | 0 | my ($self, $want_size) = @_; | |||||||||||||||||||||
| 86 | ### TiedChildren STORESIZE: $want_size | ||||||||||||||||||||||
| 87 | ### currently: $self->FETCHSIZE | ||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | 0 | my $menu = $$self; | |||||||||||||||||||||
| 90 | 0 | my @children = $menu->get_children; | |||||||||||||||||||||
| 91 | 0 | ▶ | if ($want_size < @children) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 92 | 0 | Gtk2::Ex::ContainerBits::remove_widgets | |||||||||||||||||||||
| 93 | ($menu, splice (@children, $want_size)); | ||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | 0 | ▶ | sub EXTEND { | ||||||||||||||||||||
|
Subroutine
EXTEND: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||
| 99 | |||||||||||||||||||||||
| 100 | # negative indices already normalized to >=0 by the time they get here | ||||||||||||||||||||||
| 101 | 0 | ▶ | sub EXISTS { | ||||||||||||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 102 | 0 | my ($self, $index) = @_; | |||||||||||||||||||||
| 103 | ### TiedChildren EXISTS: $index | ||||||||||||||||||||||
| 104 | 0 | return defined((($$self)->get_children)[$index]); | |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 0 | ▶ | sub DELETE { | ||||||||||||||||||||
|
Subroutine
DELETE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 108 | 0 | my ($self, $index) = @_; | |||||||||||||||||||||
| 109 | ### TiedChildren DELETE: $index | ||||||||||||||||||||||
| 110 | 0 | my $menu = $$self; | |||||||||||||||||||||
| 111 | 0 | my $ret; | |||||||||||||||||||||
| 112 | 0 | ▶ | if ($ret = $self->FETCH ($index)) { # if such an element | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 113 | 0 | $menu->remove ($ret); | |||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||
| 115 | 0 | return $ret; | |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | 0 | ▶ | sub CLEAR { | ||||||||||||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 119 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 120 | ### TiedChildren CLEAR | ||||||||||||||||||||||
| 121 | 0 | Gtk2::Ex::ContainerBits::remove_all ($$self); | |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | 0 | ▶ | sub PUSH { | ||||||||||||||||||||
|
Subroutine
PUSH: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 125 | 0 | my $self = shift; | |||||||||||||||||||||
| 126 | 0 | my $menu = $$self; | |||||||||||||||||||||
| 127 | 0 | ▶ | while (@_) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 128 | 0 | $menu->append (shift @_); | |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | 0 | ▶ | sub POP { | ||||||||||||||||||||
|
Subroutine
POP: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 133 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 134 | ### TiedChildren POP | ||||||||||||||||||||||
| 135 | 0 | my $menu = $$self; | |||||||||||||||||||||
| 136 | 0 | my $ret = ($menu->get_children)[-1]; | |||||||||||||||||||||
| 137 | 0 | ▶ | if (defined $ret) { # if not empty menu | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 138 | 0 | $menu->remove ($ret); | |||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||
| 140 | 0 | return $ret; | |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | 0 | ▶ | sub SHIFT { | ||||||||||||||||||||
|
Subroutine
SHIFT: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 144 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 145 | 0 | return DELETE($self, 0); | |||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||
| 147 | |||||||||||||||||||||||
| 148 | # don't have to return the new size here, FETCHSIZE is called separately | ||||||||||||||||||||||
| 149 | 0 | ▶ | sub UNSHIFT { | ||||||||||||||||||||
|
Subroutine
UNSHIFT: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 150 | 0 | my $self = shift; | |||||||||||||||||||||
| 151 | ### TiedChildren UNSHIFT | ||||||||||||||||||||||
| 152 | 0 | my $menu = $$self; | |||||||||||||||||||||
| 153 | 0 | ▶ | while (@_) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 154 | 0 | $menu->prepend (pop @_); | |||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||
| 157 | |||||||||||||||||||||||
| 158 | 0 | ▶ | sub SPLICE { | ||||||||||||||||||||
|
Subroutine
SPLICE: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||
| 159 | 0 | my $self = shift; | |||||||||||||||||||||
| 160 | 0 | my $offset = shift; | |||||||||||||||||||||
| 161 | 0 | my $length = shift; | |||||||||||||||||||||
| 162 | 0 | my $menu = $$self; | |||||||||||||||||||||
| 163 | 0 | my @children = $menu->get_children; | |||||||||||||||||||||
| 164 | 0 | my $total = scalar @children; | |||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | # carp similar to "use warnings" on ordinary arrays | ||||||||||||||||||||||
| 167 | 0 | ▶ | if (! defined $offset) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 168 | 0 | $offset = 0; | |||||||||||||||||||||
| 169 | } elsif ($offset < -$total) { | ||||||||||||||||||||||
| 170 | 0 | carp "TiedChildren: offset $offset before start of array"; | |||||||||||||||||||||
| 171 | 0 | $offset = 0; | |||||||||||||||||||||
| 172 | } elsif ($offset < 0) { | ||||||||||||||||||||||
| 173 | 0 | $offset = $total + $offset; | |||||||||||||||||||||
| 174 | } elsif ($offset > $total) { | ||||||||||||||||||||||
| 175 | 0 | carp "TiedChildren: offset $offset past end of array"; | |||||||||||||||||||||
| 176 | 0 | $offset = $total; | |||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||
| 178 | |||||||||||||||||||||||
| 179 | 0 | my @ret = splice (@children, $offset, $length); | |||||||||||||||||||||
| 180 | 0 | Gtk2::Ex::ContainerBits::remove_widgets ($menu, @ret); | |||||||||||||||||||||
| 181 | |||||||||||||||||||||||
| 182 | 0 | ▶ | while (@_) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 183 | 0 | $menu->insert (pop @_, $offset); | |||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||
| 185 | ### ret: map {$_->get_name} @ret | ||||||||||||||||||||||
| 186 | 0 | ▶ | return (wantarray ? @ret : $ret[-1]); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||
| 188 | |||||||||||||||||||||||
| 189 | 1 | 1; | |||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||