| 1 | package Gapp::Container; | ||
| 2 | { | ||
| 3 | $Gapp::Container::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use MooseX::SemiAffordanceAccessor; | ||
| 8 | |||
| 9 | extends 'Gapp::Widget'; | ||
| 10 | |||
| 11 | use Gapp::Types qw( GappContent ); | ||
| 12 | |||
| 13 | # the contents of the widget | ||
| 14 | has 'content' => ( | ||
| 15 | is => 'rw', | ||
| 16 | isa => GappContent, | ||
| 17 | default => sub { [ ] }, | ||
| 18 | traits => [qw( Array )], | ||
| 19 | trigger => sub { | ||
| 20 | my ( $self, $content ) = @_; | ||
| 21 | map { | ||
| 22 | confess 'cannot add undefined value to ' . $self if ! $_; | ||
| 23 | $_->set_parent( $self ); | ||
| 24 | } @$content; | ||
| 25 | }, | ||
| 26 | initializer => sub { | ||
| 27 | my ( $self, $content, $writer ) = @_; | ||
| 28 | map { | ||
| 29 | confess 'cannot add undefined value to ' . $self if ! $_; | ||
| 30 | $_->set_parent( $self ); | ||
| 31 | } @$content; | ||
| 32 | return $writer->($content); | ||
| 33 | }, | ||
| 34 | handles => { | ||
| 35 | _add_content => 'push', | ||
| 36 | children => 'elements', | ||
| 37 | }, | ||
| 38 | lazy => 1, | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | sub BUILD { | ||
| 43 | my ( $self ) = @_; | ||
| 44 | |||
| 45 | for my $child ( @{$self->content} ) { | ||
| 46 | $child->set_parent( $self ); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub BUILDARGS { | ||
| 52 | my $class = shift; | ||
| 53 | my %args = @_ == 1 && is_HashRef( $_[0] ) ? %{$_[0]} : @_; | ||
| 54 | |||
| 55 | for my $att ( qw(border_width resize_mode) ) { | ||
| 56 | $args{properties}{$att} = delete $args{$att} if exists $args{$att}; | ||
| 57 | } | ||
| 58 | |||
| 59 | __PACKAGE__->SUPER::BUILDARGS( %args ); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | after '_build_gobject' => sub { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | for ( @{$self->content} ) { | ||
| 68 | #$_->set_parent( $self ); | ||
| 69 | $self->find_layout->pack_widget( $_, $self); | ||
| 70 | } | ||
| 71 | }; | ||
| 72 | |||
| 73 | sub add { | ||
| 74 | my ( $self, @args ) = @_; | ||
| 75 | |||
| 76 | # TODO: SHOULD JUST ADD TO CONTENT ARRAY IF OBJECT NOT BUILD | ||
| 77 | # IF OBJECT IS BUILT, THEN PACK IMEDIATELY | ||
| 78 | for ( @args ) { | ||
| 79 | $_->set_parent ( $self ); | ||
| 80 | |||
| 81 | $self->find_layout->pack_widget( $_, $self) if $self->has_gobject; | ||
| 82 | $self->_add_content( $_ ); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | sub find { | ||
| 87 | my ( $self, $name ) = @_; | ||
| 88 | |||
| 89 | my @array = $self->children; | ||
| 90 | |||
| 91 | while ( my $c = shift @array ) { | ||
| 92 | return $c if $c->name eq $name; | ||
| 93 | push @array, $c->children if $c->can('children'); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | # return a list of all descendants | ||
| 100 | sub find_descendants { | ||
| 101 | my ( $self ) = @_; | ||
| 102 | |||
| 103 | my @descendants; | ||
| 104 | |||
| 105 | for my $w ( @{ $self->content } ) { | ||
| 106 | push @descendants, $w; | ||
| 107 | push @descendants, $w->find_descendants if $w->can( 'find_descendants' ); | ||
| 108 | } | ||
| 109 | |||
| 110 | return @descendants; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | __END__ |