1package PDF::Template::Container;
2
3use strict;
4
5BEGIN {
6    use vars qw(@ISA);
7    @ISA = qw(PDF::Template::Base);
8
9    use PDF::Template::Base;
10}
11
12# Containers are objects that can contain arbitrary elements, such as
13# PageDefs or Loops.
14
15sub new
16{
17    my $class = shift;
18    my $self = $class->SUPER::new(@_);
19
20    $self->{ELEMENTS} = [] unless UNIVERSAL::isa($self->{ELEMENTS}, 'ARRAY');
21
22    return $self;
23}
24
25sub _do_page
26{
27    my $self = shift;
28    my ($context, $method) = @_;
29
30    for my $e (@{$self->{ELEMENTS}})
31    {
32        $e->enter_scope($context);
33        $e->$method($context);
34        $e->exit_scope($context, 1);
35    }
36
37    return 1;
38}
39
40sub begin_page { _do_page @_, 'begin_page' }
41#{
42#    my $self = shift;
43#    my ($context) = @_;
44#
45#    for my $e (@{$self->{ELEMENTS}})
46#    {
47#        $e->enter_scope($context);
48#        $e->begin_page($context);
49#        $e->exit_scope($context, 1);
50#    }
51#
52#    return 1;
53#}
54
55sub end_page { _do_page @_, 'end_page' }
56#{
57#    my $self = shift;
58#    my ($context) = @_;
59#
60#    for my $e (@{$self->{ELEMENTS}})
61#    {
62#        $e->enter_scope($context);
63#        $e->end_page($context);
64#        $e->exit_scope($context, 1);
65#    }
66#
67#    return 1;
68#}
69
70sub reset
71{
72    my $self = shift;
73
74    $self->SUPER::reset;
75    $_->reset for @{$self->{ELEMENTS}};
76}
77
78sub iterate_over_children
79{
80    my $self = shift;
81    my ($context) = @_;
82
83    my $continue = 1;
84
85    for my $e (grep !$_->has_rendered, @{$self->{ELEMENTS}})
86    {
87        $e->enter_scope($context);
88
89        my $rc;
90        if ($rc = $e->render($context))
91        {
92            $e->mark_as_rendered;
93        }
94        $continue = $rc if $continue;
95
96        $e->exit_scope($context);
97    }
98
99    return $continue;
100}
101
102sub render
103{
104    my $self = shift;
105    my ($context) = @_;
106
107    return 0 unless $self->should_render($context);
108
109    return $self->iterate_over_children($context);
110}
111
112sub max_of
113{
114    my $self = shift;
115    my ($context, $attr) = @_;
116
117    my $max = $context->get($self, $attr);
118
119    ELEMENT:
120    foreach my $e (@{$self->{ELEMENTS}})
121    {
122        $e->enter_scope($context);
123
124        my $v = $e->isa('CONTAINER')
125            ? $e->max_of($context, $attr)
126            : $e->calculate($context, $attr);
127
128        $max = $v if $max < $v;
129
130        $e->exit_scope($context, 1);
131    }
132
133    return $max;
134}
135
136sub total_of
137{
138    my $self = shift;
139    my ($context, $attr) = @_;
140
141    my $total = 0;
142
143    ELEMENT:
144    foreach my $e (@{$self->{ELEMENTS}})
145    {
146        $e->enter_scope($context);
147
148        $total += $e->isa('CONTAINER')
149            ? $e->total_of($context, $attr)
150            : $e->calculate($context, $attr);
151
152        $e->exit_scope($context, 1);
153    }
154
155    return $total;
156}
157
1581;
159__END__
160