diff -Naur Curses-UI-0.9605/lib/Curses/UI/Notebook.pm Curses-UI-mod/lib/Curses/UI/Notebook.pm
--- Curses-UI-0.9605/lib/Curses/UI/Notebook.pm 2007-10-26 10:23:30.000000000 +0200
+++ Curses-UI-mod/lib/Curses/UI/Notebook.pm 2008-10-17 16:38:12.000000000 +0200
@@ -474,6 +474,13 @@
# Create a window for this page using same layout as widget's canvasscr.
my %userargs = @_;
keys_to_lowercase(\%userargs);
+
+ # grab callback arguments
+ foreach my $cbkey (qw/-on_activate -on_delete/) {
+ $this->{callback}{$page}{$cbkey} = delete $userargs{$cbkey}
+ if defined $userargs{$cbkey};
+ }
+
$this->add(
$page, 'Window',
@@ -524,6 +531,12 @@
unless (defined $this->{-id2object}->{$page});
debug_msg " deleting '$page' page";
+
+ if (defined $this->{callback}{$page}{-on_delete}) {
+ debug_msg " calling delete callback for $page";
+ $this->{callback}{$page}{-on_delete}->($this,$page);
+ }
+
my $active_page = $this->active_page;
@{$this->{-pages}} = grep($page ne $_, @{$this->{-pages}});
$this->activate_page($this->first_page) if ($page eq $active_page);
@@ -624,6 +637,11 @@
my $active_page = $this->active_page;
debug_msg " old active page = '$active_page'";
+ if (defined $this->{callback}{$page}{-on_activate}) {
+ debug_msg " calling activate callback for $page";
+ $this->{callback}{$page}{-on_activate}->($this,$page);
+ }
+
if ($active_page ne $page) {
$active_page = $this->{-active_page} = $page;
debug_msg " new active page = '$active_page'";
@@ -713,7 +731,7 @@
-y => 6,
-text => "Page #2.",
);
- my $page3 = $notebook->add_page('page 3');
+ my $page3 = $notebook->add_page('page 3', -on_activate => \&sub );
$page3->add(
undef, 'Label',
-x => 15,
@@ -831,7 +849,7 @@
See L<Curses::UI::Widget|Curses::UI::Widget> for explanations of these
methods.
-=item * B<add_page> ( PAGE )
+=item * B<add_page> ( PAGE [ , -on_activate => sub_ref ] [, -on_delete => ] )
Adds the specified page to the notebook object and creates an associated
window object. Returns the window object or undef on failure.
@@ -839,6 +857,13 @@
Note: the add fails if the page would otherwise cause the tab window to
overflow or is already part of the notebook object.
+The C<-on_activate> parameter specifies an optional call-back that
+will be invoked when the page is activated. This call-back will be
+called with the notebook widget and page name as parameter.
+
+Likewise for C<-on_delete> call-back. This one is invoked when the
+page is deleted.
+
=item * B<delete_page> ( PAGE )
Deletes the specified page from the notebook object and destroys its
diff -Naur Curses-UI-0.9605/t/13notebook.t Curses-UI-mod/t/13notebook.t
--- Curses-UI-0.9605/t/13notebook.t 2008-03-10 04:17:06.000000000 +0100
+++ Curses-UI-mod/t/13notebook.t 2008-10-17 16:38:58.000000000 +0200
@@ -1,4 +1,4 @@
-use Test::More tests => 25;
+use Test::More tests => 30;
use strict;
use warnings;
@@ -139,3 +139,23 @@
scalar(@{$nb1->{-pages}}) == 0,
'delete_page(), final page'
);
+
+my ($activated_widget,$activated_name) ;
+my ($deleted_widget,$deleted_name) ;
+
+my $ac_sub = sub { ($activated_widget,$activated_name) = @_ ;} ;
+my $del_sub = sub { ($deleted_widget,$deleted_name) = @_ ;} ;
+
+# create page with activation and deletion call-back
+my $cbpage = $nb1->add_page("CB Page", -on_activate => $ac_sub,
+ -on_delete => $del_sub );
+
+ok($cbpage, "Created page with callback") ;
+
+$nb1->activate_page('CB Page');
+is($activated_widget, $nb1, "activate callback called (widget ok)" );
+is($activated_name, 'CB Page', "activate callback called (name ok)");
+
+$nb1->delete_page('CB Page') ;
+is($deleted_widget, $nb1, "delete callback called (widget ok)" );
+is($deleted_name, 'CB Page', "delete callback called (name ok)");