File Coverage

blib/lib/Gtk2/Ex/Statusbar/DynamicContext.pm
Criterion Covered Total %
statement 15 30 50.0
branch 0 4 0.0
condition 0 6 0.0
subroutine 5 10 50.0
pod 4 4 100.0
total 24 54 44.4


line stmt bran cond sub pod time code
1             # Copyright 2009, 2010, 2011, 2012 Kevin Ryde
2              
3             # This file is part of Gtk2-Ex-WidgetBits.
4             #
5             # Gtk2-Ex-WidgetBits 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-WidgetBits 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-WidgetBits. If not, see .
17              
18             package Gtk2::Ex::Statusbar::DynamicContext;
19 1     1   798 use 5.008;
  1         4  
  1         41  
20 1     1   6 use strict;
  1         2  
  1         31  
21 1     1   5 use warnings;
  1         2  
  1         45  
22 1     1   5 use Carp;
  1         1  
  1         67  
23 1     1   6 use Scalar::Util;
  1         2  
  1         393  
24              
25             our $VERSION = 48;
26              
27             # Data hung on each $statusbar:
28             #
29             # $statusbar->{'Gtk2::Ex::Statusbar::DynamicContext.free'}
30             # An arrayref containing context strings currently free,
31             # ie. available for re-use.
32             #
33             # $statusbar->{'Gtk2::Ex::Statusbar::DynamicContext.seq'}
34             # An integer counting upwards to make context strings on $statusbar.
35             # Its current value is the most recent number created, so seq+1 is
36             # what to create for the next new string.
37             #
38             # The context strings are
39             #
40             # Gtk2::Ex::Statusbar::DynamicContext.1
41             # Gtk2::Ex::Statusbar::DynamicContext.2
42             #
43             # etc. The seq number in each $statusbar starts at 1. The same strings are
44             # used in different $statusbar widgets. This is fine, a context string only
45             # has to be unique within a given $statusbar, not globally.
46             #
47             # Each string 'Gtk2::Ex::Statusbar::DynamicContext.1' etc ends up going into
48             # the gtk quark table. Because each $statusbar effectively uses the same
49             # strings, in sequence, the quark table only grows to the peak context usage
50             # of any single statusbar.
51             #
52             # $statusbar->{'Gtk2::Ex::Statusbar::DynamicContext.free'} could
53             # hold an array of integer sequence numbers, or even a bit vector, instead
54             # of the full context strings. But the code for that would probably be more
55             # than any space saved.
56             #
57             # The DynamicContext objects are arrays instead of hashes, as think that
58             # might save a couple of bytes. Could change if a hash made subclassing
59             # easier.
60              
61             sub new {
62 0     0 1   my ($class, $statusbar) = @_;
63 0 0         $statusbar || croak 'No statusbar given';
64 0   0       my $context_str = pop @{$statusbar->{__PACKAGE__.'.free'}}
65             || __PACKAGE__ . '.' . ++$statusbar->{__PACKAGE__.'.seq'};
66 0           my $self = bless [ $statusbar, $context_str ], $class;
67 0           Scalar::Util::weaken ($self->[0]);
68 0           return $self;
69             }
70              
71 0     0 1   sub statusbar { return $_[0]->[0] }
72 0     0 1   sub str { return $_[0]->[1] }
73              
74             sub id {
75 0     0 1   my ($self) = @_;
76 0           my $statusbar = $self->statusbar;
77 0   0       return $statusbar && $statusbar->get_context_id ($self->str);
78             }
79              
80             sub DESTROY {
81 0     0     my ($self) = @_;
82 0 0         if (my $statusbar = $self->statusbar) {
83 0           push @{$statusbar->{__PACKAGE__.'.free'}}, $self->str;
  0            
84             }
85             }
86              
87             1;
88             __END__