| 1 | package Gapp::Object; | ||
| 2 | { | ||
| 3 | $Gapp::Object::VERSION = '0.60'; | ||
| 4 | } | ||
| 5 | |||
| 6 | 3 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use MooseX::LazyRequire; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use MooseX::SemiAffordanceAccessor; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use MooseX::StrictConstructor; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 3 | ▶ | use MooseX::Types::Moose qw( ArrayRef HashRef Str Undef ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 0 | ▶ | use Gtk2; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use Gapp::Layout::Default; | ||
| 15 | use Gapp::Types qw( GappAction GappLayout ); | ||
| 16 | |||
| 17 | |||
| 18 | # passed to gobject constructor | ||
| 19 | |||
| 20 | has 'args' => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Maybe[ArrayRef]', | ||
| 23 | ); | ||
| 24 | |||
| 25 | |||
| 26 | # signals to connect to | ||
| 27 | has 'signal_connect' => ( | ||
| 28 | is => 'ro', | ||
| 29 | isa => 'ArrayRef', | ||
| 30 | default => sub { [ ] }, | ||
| 31 | reader => 'connected_signals', | ||
| 32 | ); | ||
| 33 | |||
| 34 | |||
| 35 | # the method/function used to construct the gobject | ||
| 36 | has 'constructor' => ( | ||
| 37 | is => 'rw', | ||
| 38 | isa => 'Str', | ||
| 39 | default => 'new', | ||
| 40 | ); | ||
| 41 | |||
| 42 | # allow user to customize widget, called after gobject is built | ||
| 43 | has 'customize' => ( | ||
| 44 | is => 'rw', | ||
| 45 | isa => 'CodeRef|Undef', | ||
| 46 | ); | ||
| 47 | |||
| 48 | # gobject class | ||
| 49 | has 'gclass' => ( | ||
| 50 | is => 'rw', | ||
| 51 | isa => 'Str', | ||
| 52 | lazy_required => 1, | ||
| 53 | ); | ||
| 54 | |||
| 55 | # the actual gtk-widget | ||
| 56 | has 'gobject' => ( | ||
| 57 | is => 'rw', | ||
| 58 | isa => 'Object', | ||
| 59 | lazy_build => 1, | ||
| 60 | predicate => 'has_gobject', | ||
| 61 | ); | ||
| 62 | |||
| 63 | # explicitly set the layout | ||
| 64 | has 'layout' => ( | ||
| 65 | is => 'rw', | ||
| 66 | isa => GappLayout|Undef, | ||
| 67 | predicate => 'has_layout', | ||
| 68 | coerce => 1, | ||
| 69 | ); | ||
| 70 | |||
| 71 | # the layout to use (parent layout if not explicitly set for this widget) | ||
| 72 | has '_used_layout' => ( | ||
| 73 | is => 'rw', | ||
| 74 | isa => GappLayout|Undef, | ||
| 75 | clearer => '_clear_used_layout', | ||
| 76 | ); | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | has '_gapp_signals' => ( | ||
| 81 | is => 'rw', | ||
| 82 | isa => 'HashRef', | ||
| 83 | default => sub { { } }, | ||
| 84 | ); | ||
| 85 | |||
| 86 | |||
| 87 | sub find_layout { | ||
| 88 | my ( $self, $default ) = @_; | ||
| 89 | return $self->_used_layout if $self->_used_layout; | ||
| 90 | |||
| 91 | if ( $self->layout ) { | ||
| 92 | $self->_set_used_layout( $self->layout ); | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | if ( $self->can('parent') && $self->parent ) { | ||
| 96 | $self->_set_used_layout( $self->parent->find_layout ); | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | no warnings; | ||
| 100 | $self->_set_used_layout( $default || $Gapp::Layout || Gapp::Layout::Default->Layout ); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | return $self->_used_layout; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _forget_layout { | ||
| 107 | my ( $self ) = @_; | ||
| 108 | $self->_clear_used_layout; | ||
| 109 | |||
| 110 | if ( $self->can('children') ) { | ||
| 111 | $_->_forget_layout for $self->children; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | # for subclassing | ||
| 116 | sub _on_set_layout { | ||
| 117 | |||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | # properties to apply to the widget | ||
| 123 | has 'properties' => ( | ||
| 124 | is => 'ro', | ||
| 125 | isa => 'HashRef', | ||
| 126 | traits => [qw( Hash )], | ||
| 127 | default => sub { { } }, | ||
| 128 | handles => { | ||
| 129 | get_property => 'get', | ||
| 130 | set_property => 'set', | ||
| 131 | } | ||
| 132 | ); | ||
| 133 | |||
| 134 | # gapp traits to apply | ||
| 135 | has 'traits' => ( | ||
| 136 | is => 'rw', | ||
| 137 | isa => 'ArrayRef', | ||
| 138 | default => sub { [ ] }, | ||
| 139 | ); | ||
| 140 | |||
| 141 | # we replace the new method with our own which applies any | ||
| 142 | # traits that may have been passed in | ||
| 143 | sub new { | ||
| 144 | my ( $class, %args ) = @_; | ||
| 145 | my ( $new_class, @traits ) = $class->interpolate_class(\%args); | ||
| 146 | $new_class->_new( %args, ( scalar(@traits) ? ( traits => \@traits ) : () ) ); | ||
| 147 | } | ||
| 148 | |||
| 149 | # access to Moose's new | ||
| 150 | sub _new { | ||
| 151 | my ( $class, @args ) = @_; | ||
| 152 | $class->SUPER::new( @args ); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | # create an anonamous class with the traits applied | ||
| 157 | # copied and modified from Moose::Meta::Attribute | ||
| 158 | sub interpolate_class { | ||
| 159 | my ( $class, $options ) = @_; | ||
| 160 | |||
| 161 | $class = ref( $class ) || $class; | ||
| 162 | |||
| 163 | my @traits; | ||
| 164 | if ( my $traits = $options->{traits} ) { | ||
| 165 | |||
| 166 | my $i = 0; | ||
| 167 | |||
| 168 | while ( $i < @$traits ) { | ||
| 169 | |||
| 170 | my $trait = $traits->[$i++]; | ||
| 171 | |||
| 172 | next if ref($trait); # options to a trait we discarded | ||
| 173 | |||
| 174 | # resolve short trait names | ||
| 175 | $trait = Gapp::Util::resolve_gapp_trait_alias( Widget => $trait ) | ||
| 176 | || $trait; | ||
| 177 | |||
| 178 | next if $class->does( $trait ); | ||
| 179 | |||
| 180 | push @traits, $trait; | ||
| 181 | |||
| 182 | # are there options? | ||
| 183 | push @traits, $traits->[$i++] | ||
| 184 | if $traits->[$i] && ref($traits->[$i]); | ||
| 185 | |||
| 186 | } | ||
| 187 | |||
| 188 | # apply the traits | ||
| 189 | if ( @traits ) { | ||
| 190 | $class = Moose::Util::with_traits( $class, @traits ); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | return ( wantarray ? ( $class, @traits ) : $class ); | ||
| 195 | } | ||
| 196 | |||
| 197 | # returns widgets higher in the heirarchy | ||
| 198 | sub find_ancestors { | ||
| 199 | my ( $self ) = @_; | ||
| 200 | my $parent = $self->parent; | ||
| 201 | return $parent ? ( $parent, $parent->find_ancestors ) : (); | ||
| 202 | } | ||
| 203 | |||
| 204 | # returns the top-most widget in the heirarchy | ||
| 205 | sub find_toplevel { | ||
| 206 | my ( $self ) = @_; | ||
| 207 | my @ancestors = $self->find_ancestors; | ||
| 208 | return @ancestors ? $ancestors[-1] : $self; | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | # connect signals to the g-object | ||
| 213 | # connects the signal immediately if the g-object exists | ||
| 214 | # otherwise stores signal info to connect once g-object is constructed | ||
| 215 | sub signal_connect { | ||
| 216 | my ( $self, $name, $code, @args ) = @_; | ||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | # attach the signal if the gtk widget has been constructed | ||
| 221 | if ( $self->has_gobject ) { | ||
| 222 | $self->_apply_signal( [ $name, $code, @args ] ); | ||
| 223 | } | ||
| 224 | else { | ||
| 225 | push @{ $self->connected_signals }, [ $name, $code, @args ]; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | |||
| 231 | # call customize code-ref if set | ||
| 232 | sub _apply_customize { | ||
| 233 | my ( $self ) = @_; | ||
| 234 | $self->customize->( $self, $self->gobject ) if $self->customize; | ||
| 235 | } | ||
| 236 | |||
| 237 | # call builder method from layout | ||
| 238 | sub _apply_builders { | ||
| 239 | my ( $self ) = @_; | ||
| 240 | no warnings; | ||
| 241 | $self->find_layout->build_widget( $self ); | ||
| 242 | } | ||
| 243 | |||
| 244 | # call builder method from layout | ||
| 245 | sub _apply_stylers { | ||
| 246 | my ( $self ) = @_; | ||
| 247 | no warnings; | ||
| 248 | $self->find_layout->style_widget( $self ); | ||
| 249 | } | ||
| 250 | |||
| 251 | # call builder method from layout | ||
| 252 | sub _apply_painters { | ||
| 253 | my ( $self ) = @_; | ||
| 254 | no warnings; | ||
| 255 | $self->find_layout->paint_widget( $self ) if $self->can('action') && $self->action; | ||
| 256 | } | ||
| 257 | |||
| 258 | # apply any properties to the gobject | ||
| 259 | sub _apply_properties { | ||
| 260 | my ( $self ) = @_; | ||
| 261 | for my $p ( keys %{ $self->properties } ) { | ||
| 262 | my $value = $self->properties->{$p}; | ||
| 263 | $self->gobject->set( $p, $self->properties->{$p} ); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | # apply any signals to the gobject | ||
| 268 | sub _apply_signals { | ||
| 269 | my ( $self ) = @_; | ||
| 270 | map { $self->_apply_signal( $_ ) } @{ $self->connected_signals }; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub _apply_signal { | ||
| 274 | my ( $self, $signal ) = @_; | ||
| 275 | my ( $name, $action, @args ) = @$signal; | ||
| 276 | |||
| 277 | return if $name =~ /^gapp/; | ||
| 278 | |||
| 279 | if ( is_GappAction( $action ) ) { | ||
| 280 | $self->gobject->signal_connect( $name => sub { | ||
| 281 | my ( $gtkw, @gtkargs ) = @_; | ||
| 282 | return $action->perform( $self, \@args, $gtkw, \@gtkargs ); | ||
| 283 | }); | ||
| 284 | } | ||
| 285 | else { | ||
| 286 | $self->gobject->signal_connect( $name => sub { | ||
| 287 | my ( $gtkw, @gtkargs ) = @_; | ||
| 288 | return $action->( $self, \@args, $gtkw, \@gtkargs ); | ||
| 289 | }); | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | # construct and format the widget | ||
| 294 | sub _build_gobject { | ||
| 295 | my ( $self ) = @_; | ||
| 296 | |||
| 297 | $self->_apply_stylers; | ||
| 298 | |||
| 299 | my $w = $self->_construct_gobject( @_ ); | ||
| 300 | $self->_apply_properties; | ||
| 301 | $self->_apply_builders; | ||
| 302 | $self->_apply_painters; | ||
| 303 | $self->_apply_signals; | ||
| 304 | $self->_apply_customize; | ||
| 305 | |||
| 306 | return $w; | ||
| 307 | } | ||
| 308 | |||
| 309 | |||
| 310 | # create and set the actual gobject | ||
| 311 | sub _construct_gobject { | ||
| 312 | my ( $self ) = @_; | ||
| 313 | |||
| 314 | my $gclass = $self->gclass; | ||
| 315 | my $gconstructor = $self->constructor; | ||
| 316 | |||
| 317 | # use any build-arguments if they exist | ||
| 318 | my $w = $gclass->$gconstructor( $self->args ? @{$self->args} : ( ) ); | ||
| 319 | $self->set_gobject( $w ); | ||
| 320 | return $w; | ||
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | 1; | ||
| 326 | |||
| 327 | __END__ | ||