| 1 | package POE::Session::GladeXML2; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | |||
| 6 | #TODO - this should be in POE::Session | ||
| 7 | sub SE_DATA () { 3 } | ||
| 8 | |||
| 9 | our $VERSION = '0.40'; | ||
| 10 | use base qw(POE::Session); | ||
| 11 | |||
| 12 | # local var so we needn't worry about __PACKAGE__ being interpreted as | ||
| 13 | # a string literal | ||
| 14 | my $pkg = __PACKAGE__; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | POE::Session::GladeXML2 - emit POE events for Gtk2 callbacks | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | package test; | ||
| 23 | |||
| 24 | use POE::Session::GladeXML2; | ||
| 25 | |||
| 26 | # the name of the sub needs to match with the name you entered in | ||
| 27 | # the glade signal handler dialog. | ||
| 28 | sub on_button1_clicked { | ||
| 29 | print STDERR "button clicked\n"; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | [... object creation ...] | ||
| 34 | my $session = POE::Session::GladeXML2->create ( | ||
| 35 | glade_object => $self, | ||
| 36 | glade_file => 'test.glade', | ||
| 37 | glade_args => 'widgetname', | ||
| 38 | [... POE Session params ...] | ||
| 39 | ); | ||
| 40 | |||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | Gtk2::GladeXML->init; | ||
| 45 | my $foo = test->new; | ||
| 46 | $poe_kernel->run(); | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | A simple helper module that lets you connect callback names from | ||
| 51 | your .glade file with methods of an object. These methods are called | ||
| 52 | as POE callback or postback methods. L<POE::Session::GladeXML> automatically | ||
| 53 | determines whether gtk expects the signal handler to return a value. If | ||
| 54 | it does, a callback is used, otherwise a postback will be used. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | use Carp; | ||
| 59 | use Gtk2 -init; | ||
| 60 | use POE; | ||
| 61 | use Gtk2::GladeXML; | ||
| 62 | |||
| 63 | sub _session_autoconnect_helper { | ||
| 64 | my ($handler_name, $gobject, $signal_name, $signal_data, | ||
| 65 | $connect_object, $is_after, $session_object, $event_name) = @_; | ||
| 66 | |||
| 67 | # So things like Glade2::Widget can call this with an extra | ||
| 68 | # parameter that defines the event name, so you can have | ||
| 69 | # more than one event use the same handler. Glade2::Widget | ||
| 70 | # uses that to allow more than one widget to exist in the | ||
| 71 | # same session | ||
| 72 | $event_name = $handler_name unless defined $event_name; | ||
| 73 | |||
| 74 | $poe_kernel->state ($event_name, $session_object, $handler_name); | ||
| 75 | my $session = $poe_kernel->get_active_session; | ||
| 76 | my $handler; | ||
| 77 | |||
| 78 | my $sig_info = $gobject->signal_query ($signal_name); | ||
| 79 | |||
| 80 | if (!defined $sig_info->{'return_type'}) { | ||
| 81 | $handler = $session->postback ($event_name); | ||
| 82 | } else { | ||
| 83 | $handler = $session->callback ($event_name); | ||
| 84 | } | ||
| 85 | |||
| 86 | if ($connect_object) { | ||
| 87 | my ($func) = $is_after ? "signal_connect_object_after" : "signal_connect_object"; | ||
| 88 | $gobject->$func ($signal_name, $connect_object, $handler, $signal_data); | ||
| 89 | } else { | ||
| 90 | my ($func) = $is_after ? "signal_connect_after" : "signal_connect"; | ||
| 91 | $gobject->$func ($signal_name, $handler, $signal_data); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | =head1 CONSTRUCTOR | ||
| 96 | |||
| 97 | =head2 create (%args) | ||
| 98 | |||
| 99 | creates a POE::Session that connects the callbacks from the glade file | ||
| 100 | to the corresponding method of the object in the glade_object argument. | ||
| 101 | The glade_file argument contains the name of the glade file to use. Optional | ||
| 102 | arguments for Gtk::GladeXML->new are in the glade_args argument. See | ||
| 103 | C<Gtk2::GladeXML> for more information. | ||
| 104 | |||
| 105 | If you are using this to create your main window, you can put the name of | ||
| 106 | that in a glade_mainwin argument, which will then be used to make POE exit | ||
| 107 | when this window is closed. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | |||
| 112 | # subclassing POE::Session methods to work our evil^Wmagic | ||
| 113 | |||
| 114 | # here we stick our custom parameters into the newly created $self | ||
| 115 | sub instantiate { | ||
| 116 | my ($class, $params) = @_; | ||
| 117 | |||
| 118 | my $self = $class->SUPER::instantiate; | ||
| 119 | croak "expecting a hashref" unless (ref ($params) eq 'HASH'); | ||
| 120 | |||
| 121 | my $object = delete $params->{'glade_object'}; | ||
| 122 | croak $class . " MUST have a glade_object option" | ||
| 123 | unless (defined $object); | ||
| 124 | my $file = delete $params->{'glade_file'}; | ||
| 125 | croak $class . " MUST have a glade_file option" | ||
| 126 | unless (defined $file); | ||
| 127 | my $glade_args = delete $params->{'glade_args'}; | ||
| 128 | my $glade_mainwin = delete $params->{'glade_mainwin'}; | ||
| 129 | |||
| 130 | $self->[SE_DATA]->{$pkg}->{"glade_object"} = $object; | ||
| 131 | $self->[SE_DATA]->{$pkg}->{"glade_file"} = $file; | ||
| 132 | $self->[SE_DATA]->{$pkg}->{"glade_args"} = $glade_args if (defined $glade_args); | ||
| 133 | $self->[SE_DATA]->{$pkg}->{"glade_mainwin"} = $glade_mainwin if (defined $glade_mainwin); | ||
| 134 | |||
| 135 | return $self; | ||
| 136 | } | ||
| 137 | |||
| 138 | # help for try_alloc | ||
| 139 | sub _prepare_widgets { | ||
| 140 | my ($kernel, $session, @args) = @_[KERNEL, SESSION, ARG0..$#_]; | ||
| 141 | |||
| 142 | my $object = $session->[SE_DATA]->{$pkg}->{"glade_object"}; | ||
| 143 | my $file = $session->[SE_DATA]->{$pkg}->{"glade_file"}; | ||
| 144 | my $glade_args = $session->[SE_DATA]->{$pkg}->{"glade_args"}; | ||
| 145 | $glade_args = [] unless (defined $glade_args); | ||
| 146 | |||
| 147 | my $t = Gtk2::GladeXML->new ($file, @$glade_args); | ||
| 148 | croak "Couldn't create Gtk2::GladeXML" unless (defined $t); | ||
| 149 | |||
| 150 | $session->[SE_DATA]->{$pkg}->{'xml'} = $t; | ||
| 151 | $t->signal_autoconnect (\&_session_autoconnect_helper, $object); | ||
| 152 | my $mainwin = $session->[SE_DATA]->{$pkg}->{'glade_mainwin'}; | ||
| 153 | if (defined $mainwin) { | ||
| 154 | my $widget = $t->get_widget ($mainwin); | ||
| 155 | $kernel->signal_ui_destroy ($widget); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | # and here we use those to set up our _start | ||
| 160 | sub try_alloc { | ||
| 161 | my ($self, @start_args) = @_; | ||
| 162 | |||
| 163 | my $start_state = | ||
| 164 | $self->[POE::Session::SE_STATES]->{+POE::Session::EN_START}; | ||
| 165 | |||
| 166 | my $object = $self->[SE_DATA]->{$pkg}->{"glade_object"}; | ||
| 167 | my $file = $self->[SE_DATA]->{$pkg}->{"glade_file"}; | ||
| 168 | my $glade_args = $self->[SE_DATA]->{$pkg}->{"glade_args"}; | ||
| 169 | $glade_args = [] unless (defined $glade_args); | ||
| 170 | |||
| 171 | my $real_start_state; | ||
| 172 | |||
| 173 | # call any _start the user defined. | ||
| 174 | if (defined $start_state) { | ||
| 175 | $real_start_state = sub { | ||
| 176 | _prepare_widgets (@_); | ||
| 177 | |||
| 178 | if (ref ($start_state) ne 'CODE') { | ||
| 179 | $_[OBJECT] = $object; | ||
| 180 | } | ||
| 181 | if (ref($start_state) eq 'CODE') { | ||
| 182 | return &$start_state (@_); | ||
| 183 | } else { | ||
| 184 | my ($clobj, $state) = @$start_state; | ||
| 185 | shift @_; | ||
| 186 | return $clobj->$state (@_); | ||
| 187 | } | ||
| 188 | }; | ||
| 189 | } else { | ||
| 190 | $real_start_state = \&_prepare_widgets; | ||
| 191 | } | ||
| 192 | $self->[POE::Session::SE_STATES]->{+POE::Session::EN_START} = $real_start_state; | ||
| 193 | |||
| 194 | return $self->SUPER::try_alloc (@start_args); | ||
| 195 | } | ||
| 196 | |||
| 197 | =head1 METHODS | ||
| 198 | |||
| 199 | =head2 gladexml () | ||
| 200 | |||
| 201 | Returns the Gtk2::GladeXML object. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub gladexml { | ||
| 206 | my ($self) = @_; | ||
| 207 | |||
| 208 | return $self->[SE_DATA]->{$pkg}->{'xml'} | ||
| 209 | } | ||
| 210 | |||
| 211 | =head1 SEE ALSO | ||
| 212 | |||
| 213 | C<POE::Session> and C<Gtk2::GladeXML> | ||
| 214 | |||
| 215 | =head1 AUTHOR | ||
| 216 | |||
| 217 | Martijn van Beers <martijn@cpan.org> | ||
| 218 | |||
| 219 | =head1 LICENSE | ||
| 220 | |||
| 221 | This module is free software; you may reproduce and/or modify it | ||
| 222 | under the terms of the GPL license v2.0. See the file LICENSE in | ||
| 223 | the source tarball for more information | ||
| 224 | |||
| 225 | =head2 ACKNOWLEDGEMENTS | ||
| 226 | |||
| 227 | This module wouldn't be half as good without the invaluable advice | ||
| 228 | of Rocco Caputo and Scott Arrington. | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | 1; |