| 1 | package Gtk2::Ex::FormFactory::Container; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use base qw( Gtk2::Ex::FormFactory::Widget ); | ||
| 6 | |||
| 7 | sub get_content { shift->{content} } | ||
| 8 | sub get_title { shift->{title} } | ||
| 9 | |||
| 10 | sub set_content { shift->{content} = $_[1] } | ||
| 11 | sub set_title { shift->{title} = $_[1] } | ||
| 12 | |||
| 13 | sub isa_container { 1 } | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | my %par = @_; | ||
| 18 | my ($content, $title) = @par{'content','title'}; | ||
| 19 | |||
| 20 | my $self = $class->SUPER::new(@_); | ||
| 21 | |||
| 22 | #-- Handle some defaults for 'content' parameter | ||
| 23 | if ( not defined $content ) { | ||
| 24 | $content = []; | ||
| 25 | } elsif ( ref $content ne 'ARRAY' ) { | ||
| 26 | $content = [ $content ]; | ||
| 27 | } | ||
| 28 | |||
| 29 | #-- For convenience the developer may write pairs of | ||
| 30 | #-- the Widget's short name and a hash ref with its | ||
| 31 | #-- attributes instead of adding real objects. This | ||
| 32 | #-- loop search for such non-objects and creates | ||
| 33 | #-- objects accordingly. | ||
| 34 | my @content_with_objects; | ||
| 35 | for ( my $i=0; $i < @{$content}; ++$i ) { | ||
| 36 | if ( not defined $content->[$i] ) { | ||
| 37 | die "Child #$i of ".$self->get_type."/".$self->get_name. | ||
| 38 | " is not defined"; | ||
| 39 | } | ||
| 40 | if ( not ref $content->[$i] ) { | ||
| 41 | #-- No object, so derive the class name | ||
| 42 | #-- from the short name | ||
| 43 | my $class = $content->[$i]; | ||
| 44 | $class =~ s/^(.)/uc($1)/e; | ||
| 45 | $class =~ s/_(.)/uc($1)/eg; | ||
| 46 | $class =~ s/_//g; | ||
| 47 | $class = "Gtk2::Ex::FormFactory::$class"; | ||
| 48 | |||
| 49 | #-- And create the object | ||
| 50 | my $object = $class->new(%{$content->[$i+1]}); | ||
| 51 | push @content_with_objects, $object; | ||
| 52 | |||
| 53 | #-- Skip next entry in @content | ||
| 54 | ++$i; | ||
| 55 | |||
| 56 | } else { | ||
| 57 | #-- Regular objects are taken as is | ||
| 58 | push @content_with_objects, $content->[$i]; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | $self->set_content(\@content_with_objects); | ||
| 63 | $self->set_title($title); | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub debug_dump { | ||
| 69 | my $self = shift; | ||
| 70 | my ($level) = @_; | ||
| 71 | |||
| 72 | $self->SUPER::debug_dump($level); | ||
| 73 | |||
| 74 | foreach my $c ( @{$self->get_content} ) { | ||
| 75 | if ( $c ) { | ||
| 76 | $c->debug_dump($level+1); | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | print " "x($level+1),"UNDEF\n"; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub build { | ||
| 87 | my $self = shift; | ||
| 88 | |||
| 89 | #-- First build the widget itself | ||
| 90 | $self->SUPER::build(@_); | ||
| 91 | |||
| 92 | #-- Now build the children | ||
| 93 | $self->build_children; | ||
| 94 | |||
| 95 | 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub build_children { | ||
| 99 | my $self = shift; | ||
| 100 | |||
| 101 | $Gtk2::Ex::FormFactory::DEBUG && | ||
| 102 | print "$self->build_children\n"; | ||
| 103 | |||
| 104 | my $layouter = $self->get_form_factory->get_layouter; | ||
| 105 | |||
| 106 | foreach my $child ( @{$self->get_content} ) { | ||
| 107 | $child->set_parent($self); | ||
| 108 | $child->set_form_factory($self->get_form_factory); | ||
| 109 | $child->build; | ||
| 110 | $layouter->add_widget_to_container ($child, $self); | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub add_child_widget { | ||
| 117 | my $self = shift; | ||
| 118 | my ($child) = @_; | ||
| 119 | |||
| 120 | push @{$self->get_content}, $child; | ||
| 121 | |||
| 122 | return unless $self->get_built; | ||
| 123 | |||
| 124 | my $form_factory = $self->get_form_factory; | ||
| 125 | my $layouter = $form_factory->get_layouter; | ||
| 126 | |||
| 127 | $form_factory->register_all_widgets($child); | ||
| 128 | |||
| 129 | $child->set_parent($self); | ||
| 130 | $child->set_form_factory($form_factory); | ||
| 131 | $child->build; | ||
| 132 | |||
| 133 | $layouter->add_widget_to_container($child, $self); | ||
| 134 | |||
| 135 | $child->connect_signals; | ||
| 136 | $child->update_all; | ||
| 137 | $child->get_gtk_parent_widget->show_all; | ||
| 138 | |||
| 139 | 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub remove_child_widget { | ||
| 143 | my $self = shift; | ||
| 144 | my ($child) = @_; | ||
| 145 | |||
| 146 | my $found; | ||
| 147 | my $i = 0; | ||
| 148 | foreach my $c ( @{$self->get_content} ) { | ||
| 149 | $found = 1, last if $c eq $child; | ||
| 150 | ++$i; | ||
| 151 | } | ||
| 152 | |||
| 153 | die "Widget '".$child->get_name. | ||
| 154 | "' no child of container '". | ||
| 155 | $self->get_name."'" unless $found; | ||
| 156 | |||
| 157 | splice @{$self->get_content}, $i, 1; | ||
| 158 | |||
| 159 | return unless $self->get_built; | ||
| 160 | |||
| 161 | my $child_gtk_widget = $child->get_gtk_parent_widget; | ||
| 162 | my $gtk_widget = $self->get_gtk_widget; | ||
| 163 | |||
| 164 | $gtk_widget->remove($child_gtk_widget); | ||
| 165 | |||
| 166 | $child->cleanup; | ||
| 167 | |||
| 168 | 1; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub update_all { | ||
| 172 | my $self = shift; | ||
| 173 | |||
| 174 | $self->SUPER::update(@_); | ||
| 175 | foreach my $c ( @{$self->get_content} ) { | ||
| 176 | $c->update_all; | ||
| 177 | } | ||
| 178 | |||
| 179 | 1; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub apply_changes_all { | ||
| 183 | my $self = shift; | ||
| 184 | |||
| 185 | $self->SUPER::apply_changes(@_); | ||
| 186 | |||
| 187 | foreach my $c ( @{$self->get_content} ) { | ||
| 188 | $c->apply_changes_all; | ||
| 189 | } | ||
| 190 | |||
| 191 | 1; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub commit_proxy_buffers_all { | ||
| 195 | my $self = shift; | ||
| 196 | |||
| 197 | $self->SUPER::commit_proxy_buffers(@_); | ||
| 198 | |||
| 199 | foreach my $c ( @{$self->get_content} ) { | ||
| 200 | $c->commit_proxy_buffers_all; | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub discard_proxy_buffers_all { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | $self->SUPER::discard_proxy_buffers(@_); | ||
| 210 | |||
| 211 | foreach my $c ( @{$self->get_content} ) { | ||
| 212 | $c->discard_proxy_buffers_all; | ||
| 213 | } | ||
| 214 | |||
| 215 | 1; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub connect_signals { | ||
| 219 | my $self = shift; | ||
| 220 | |||
| 221 | $self->SUPER::connect_signals(@_); | ||
| 222 | |||
| 223 | foreach my $c ( @{$self->get_content} ) { | ||
| 224 | $c->connect_signals; | ||
| 225 | } | ||
| 226 | |||
| 227 | 1; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub cleanup { | ||
| 231 | my $self = shift; | ||
| 232 | |||
| 233 | foreach my $c ( @{$self->get_content} ) { | ||
| 234 | $c->cleanup; | ||
| 235 | } | ||
| 236 | |||
| 237 | $self->SUPER::cleanup(@_); | ||
| 238 | |||
| 239 | $self->set_content([]); | ||
| 240 | |||
| 241 | 1; | ||
| 242 | } | ||
| 243 | |||
| 244 | 1; | ||
| 245 | |||
| 246 | __END__ |