File Coverage

blib/lib/Tickit/ContainerWidget.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 19 19 100.0


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2009-2014 -- leonerd@leonerd.org.uk
5              
6             package Tickit::ContainerWidget;
7              
8 1     1   4 use strict;
  1         1  
  1         22  
9 1     1   3 use warnings;
  1         1  
  1         17  
10 1     1   21 use 5.010; # //
  1         2  
11 1     1   2 use feature qw( switch );
  1         1  
  1         101  
12 1     1   3 use base qw( Tickit::Widget );
  1         1  
  1         401  
13             no if $] >= 5.017011, warnings => 'experimental::smartmatch';
14              
15             our $VERSION = '0.51';
16              
17             use Carp;
18              
19             use Scalar::Util qw( refaddr );
20              
21             =head1 NAME
22              
23             C - abstract base class for widgets that contain
24             other widgets
25              
26             =head1 SYNOPSIS
27              
28             TODO
29              
30             =head1 DESCRIPTION
31              
32             This class acts as an abstract base class for widgets that contain at leaast
33             one other widget object. It provides storage for a hash of "options"
34             associated with each child widget.
35              
36             =head1 STYLE
37              
38             The following style tags are used:
39              
40             =over 4
41              
42             =item :focus-child
43              
44             Set whenever a child widget within the container has the input focus.
45              
46             =back
47              
48             =cut
49              
50             sub new
51             {
52             my $class = shift;
53              
54             foreach my $method (qw( children )) {
55             $class->can( $method ) or
56             croak "$class cannot ->$method - do you subclass and implement it?";
57             }
58              
59             my $self = $class->SUPER::new( @_ );
60              
61             $self->{child_opts} = {};
62              
63             return $self;
64             }
65              
66             =head2 $widget->add( $child, %opts )
67              
68             Sets the child widget's parent, stores the options for the child, and calls
69             the C method. The concrete implementation will have to
70             implement storage of this child widget.
71              
72             Returns the container C<$widget> itself, for easy chaining.
73              
74             =cut
75              
76             sub add
77             {
78             my $self = shift;
79             my ( $child, %opts ) = @_;
80              
81             $child->set_parent( $self );
82              
83             $self->{child_opts}{refaddr $child} = \%opts;
84              
85             $self->children_changed;
86              
87             return $self;
88             }
89              
90             =head2 $widget->remove( $child_or_index )
91              
92             Removes the child widget's parent, and calls the C method.
93             The concrete implementation will have to remove this child from its storage.
94              
95             Returns the container C<$widget> itself, for easy chaining.
96              
97             =cut
98              
99             sub remove
100             {
101             my $self = shift;
102             my ( $child ) = @_;
103              
104             $child->set_parent( undef );
105             $child->window->close if $child->window;
106             $child->set_window( undef );
107              
108             delete $self->{child_opts}{refaddr $child};
109              
110             $self->children_changed;
111              
112             return $self;
113             }
114              
115             =head2 %opts = $widget->child_opts( $child )
116              
117             =head2 $opts = $widget->child_opts( $child )
118              
119             Returns the options currently set for the given child as a key/value list in
120             list context, or as a HASH reference in scalar context. The HASH reference in
121             scalar context is the actual hash used to store the options - modifications to
122             it will be preserved.
123              
124             =cut
125              
126             sub child_opts
127             {
128             my $self = shift;
129             my ( $child ) = @_;
130              
131             my $opts = $self->{child_opts}{refaddr $child};
132             return $opts if !wantarray;
133             return %$opts;
134             }
135              
136             =head2 $widget->set_child_opts( $child, %newopts )
137              
138             Sets new options on the given child. Any options whose value is given as
139             C are deleted.
140              
141             =cut
142              
143             sub set_child_opts
144             {
145             my $self = shift;
146             my ( $child, %newopts ) = @_;
147              
148             my $opts = $self->{child_opts}{refaddr $child};
149              
150             foreach ( keys %newopts ) {
151             defined $newopts{$_} ? ( $opts->{$_} = $newopts{$_} ) : ( delete $opts->{$_} );
152             }
153              
154             $self->children_changed;
155             }
156              
157             sub child_resized
158             {
159             my $self = shift;
160             $self->reshape if $self->window;
161             $self->resized;
162             }
163              
164             sub children_changed
165             {
166             my $self = shift;
167              
168             $self->reshape if $self->window;
169             $self->resized;
170             }
171              
172             sub window_gained
173             {
174             my $self = shift;
175             $self->SUPER::window_gained( @_ );
176              
177             $self->window->set_focus_child_notify( 1 );
178             }
179              
180             sub window_lost
181             {
182             my $self = shift;
183              
184             $_->set_window( undef ) for $self->children;
185              
186             $self->SUPER::window_lost( @_ );
187             }
188              
189             sub _on_win_focus
190             {
191             my $self = shift;
192             $self->SUPER::_on_win_focus( @_ );
193              
194             $self->set_style_tag( "focus-child" => $_[1] ) if $_[2];
195             }
196              
197             =head2 $child = $widget->find_child( $how, $other, %args )
198              
199             Returns a child widget. The C<$how> argument determines how this is done,
200             relative to the child widget given by C<$other>:
201              
202             =over 4
203              
204             =item first
205              
206             The first child returned by C (C<$other> is ignored)
207              
208             =item last
209              
210             The last child returned by C (C<$other> is ignored)
211              
212             =item before
213              
214             The child widget just before C<$other> in the order given by C
215              
216             =item after
217              
218             The child widget just after C<$other> in the order given by C
219              
220             =back
221              
222             Takes the following named arguments:
223              
224             =over 8
225              
226             =item where => CODE
227              
228             Optional. If defined, gives a filter function to filter the list of children
229             before searching for the required one. Will be invoked once per child, with
230             the child widget set as C<$_>; it should return a boolean value to indicate if
231             that child should be included in the search.
232              
233             =back
234              
235             =cut
236              
237             sub find_child
238             {
239             my $self = shift;
240             my ( $how, $other, %args ) = @_;
241              
242             my $children = $args{children} // "children";
243             my @children = $self->$children;
244             if( my $where = $args{where} ) {
245             @children = grep { defined $other and $_ == $other or $where->() } @children;
246             }
247              
248             for( $how ) {
249             when( "first" ) {
250             return $children[0];
251             }
252             when( "last" ) {
253             return $children[-1];
254             }
255             when( "before" ) {
256             $children[$_] == $other and return $children[$_-1] for 1 .. $#children;
257             return undef;
258             }
259             when( "after" ) {
260             $children[$_] == $other and return $children[$_+1] for 0 .. $#children-1;
261             return undef;
262             }
263             default {
264             croak "Unrecognised ->find_child mode '$how'";
265             }
266             }
267             }
268              
269             use constant CONTAINER_OR_FOCUSABLE => sub {
270             $_->isa( "Tickit::ContainerWidget" ) or
271             $_->window && $_->window->is_visible && $_->CAN_FOCUS
272             };
273              
274             =head2 $widget->focus_next( $how, $other )
275              
276             Moves the input focus to the next widget in the widget tree, by searching in
277             the direction given by C<$how> relative to the widget given by C<$other>
278             (which must be an immediate child of C<$widget>).
279              
280             The direction C<$how> must be one of the following four values:
281              
282             =over 4
283              
284             =item first
285              
286             =item last
287              
288             Moves focus to the first or last child widget that can take focus. Recurses
289             into child widgets that are themselves containers. C<$other> is ignored.
290              
291             =item after
292              
293             =item before
294              
295             Moves focus to the next or previous child widget in tree order from the one
296             given by C<$other>. Recurses into child widgets that are themselves
297             containers, and out into parent containers.
298              
299             These searches will wrap around the widget tree; moving C the last node
300             in the widget tree will move to the first, and vice versa.
301              
302             =back
303              
304             This differs from C in that it performs a full tree search through
305             the widget tree, considering parents and children. If a C or C
306             search falls off the end of one node, it will recurse up to its parent and
307             search within the next child, and so on.
308              
309             Usually this would be used via the widget itself:
310              
311             $self->parent->focus_next( $how => $self );
312              
313             =cut
314              
315             sub focus_next
316             {
317             my $self = shift;
318             my ( $how, $other ) = @_;
319              
320             # This tree search has the potential to loop infinitely, if there are no
321             # focusable widgets at all. It would only do this if it cycles via the root
322             # widget twice in a row. Technically we could detect it earlier, but that
323             # is more difficult to arrange for
324             my $done_root;
325              
326             my $next;
327              
328             my $children = $self->can( "children_for_focus" ) || "children";
329              
330             while(1) {
331             $next = $self->find_child( $how, $other,
332             where => CONTAINER_OR_FOCUSABLE,
333             children => $children,
334             );
335             last if $next and $next->CAN_FOCUS;
336              
337             # Either we found a container (recurse into it),
338             if( $next ) {
339             my $childhow = $how;
340             if( $how eq "after" ) { $childhow = "first" }
341             elsif( $how eq "before" ) { $childhow = "last" }
342              
343             # See if child has it
344             return 1 if $next->focus_next( $childhow => undef );
345              
346             $other = $next;
347             redo;
348             }
349             # or we'll have to recurse up to my parent
350             elsif( my $parent = $self->parent ) {
351             if( $how eq "after" or $how eq "before" ) {
352             $other = $self;
353             $self = $parent;
354             redo;
355             }
356             else {
357             return undef;
358             }
359             }
360             # or we'll have to cycle around the root
361             else {
362             die "Cycled through the entire widget tree and did not find a focusable widget" if $done_root;
363             $done_root++;
364              
365             if( $how eq "after" ) { $how = "first" }
366             elsif( $how eq "before" ) { $how = "last" }
367             else { die "Cannot cycle how=$how around root widget"; }
368              
369             $other = undef;
370             redo;
371             }
372             }
373              
374             $next->take_focus;
375             return 1;
376             }
377              
378             =head1 SUBCLASS METHODS
379              
380             =head2 @children = $widget->children
381              
382             Required. Should return a list of all the contained child widgets. The order
383             is not specified, but should be in some stable order that makes sense given
384             the layout of the widget's children.
385              
386             This method is used by C to remove the windows from all the child
387             widgets automatically, and by C to obtain a child relative to
388             another given one.
389              
390             =head2 @children = $widget->children_for_focus
391              
392             Optional. If implemented, this method is called to obtain a list of child
393             widgets to perform a child search on when changing focus using the
394             C method. If it is not implemented, the regular C method
395             is called instead.
396              
397             Normally this method shouldn't be used, but it may be useful on container
398             widgets that also display "helper" widgets that should not be considered as
399             part of the main focus set. This method can then exclude them.
400              
401             =head2 $widget->children_changed
402              
403             Optional. If implemented, this method will be called after any change of the
404             contained child widgets or their options. Typically this will be used to set
405             windows on them by sub-dividing the window of the parent.
406              
407             If not overridden, the base implementation will call C.
408              
409             =head2 $widget->child_resized( $child )
410              
411             Optional. If implemented, this method will be called after a child widget
412             changes or may have changed its size requirements. Typically this will be used
413             to adjusts the windows allocated to children.
414              
415             If not overridden, the base implementation will call C.
416              
417             =head1 AUTHOR
418              
419             Paul Evans
420              
421             =cut
422              
423             0x55AA;