Skip Menu |

This queue is for tickets about the Curses-UI CPAN distribution.

Report information
The Basics
Id: 39915
Status: resolved
Priority: 0/
Queue: Curses-UI

People
Owner: Nobody in particular
Requestors: DDUMONT [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.9605
Fixed in: (no value)



Subject: Need a call-back when activating notebook page
Hello I would like to be able to invoke a callback in notebook when activating a page. Typically the API would be: $nb->add('new page', activate => $sub_ref ) ; The $subref would get the activated page as parameter. Does this make sense ? If yes, I'll provide a patch. Thanks
On Thu Oct 09 08:03:43 2008, DDUMONT wrote: Show quoted text
> $nb->add('new page', activate => $sub_ref ) ;
Oops, my bad. The correct method is add_page not add. So the API would be: $nb->add_page('new page', activate => $sub_ref ) ; All the best
Thanks for the offer. To be honest, if you're using notebooks, you know them better than I do. I have yet to write any code using one, and they were added before I took over maintainership. Feel free to send a patch, and I'll get a new release out soon. Thank you again! On Thu Oct 09 08:03:43 2008, DDUMONT wrote: Show quoted text
> Hello > > I would like to be able to invoke a callback in notebook when activating > a page. > > Typically the API would be: > > $nb->add('new page', activate => $sub_ref ) ; > > The $subref would get the activated page as parameter. > > Does this make sense ? > > If yes, I'll provide a patch. > > Thanks
On Tue Oct 14 12:38:10 2008, MDXI wrote: Show quoted text
> Thanks for the offer. To be honest, if you're using notebooks, you know > them better than I do. I have yet to write any code using one, and they > were added before I took over maintainership.
To be honest also, I've just begun using them ;-) Anyway, here's a first stab at a patch including test updates and doc addition. Please review the parameters style that I've added to add_page. Feel free to change them. All the best
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-16 15:44:26.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); + } + 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); + } + 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 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-16 15:34:10.000000000 +0200 @@ -1,4 +1,4 @@ -use Test::More tests => 25; +use Test::More tests => 28; use strict; use warnings; @@ -139,3 +139,21 @@ scalar(@{$nb1->{-pages}}) == 0, 'delete_page(), final page' ); + +my $activated = 0; +my $deleted = 0; + +my $ac_sub = sub { $activated = shift ;} ; +my $del_sub = sub { $deleted = shift ;} ; + +# 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, $nb1, "activate callback called"); + +$nb1->delete_page('CB Page') ; +is($deleted, $nb1, "delete callback called");
On Thu Oct 16 09:52:23 2008, DDUMONT wrote: Show quoted text
> Anyway, here's a first stab at a patch including test updates and doc > addition. Please review the parameters style that I've added to > add_page. Feel free to change them.
On 2nd thought, providing also the page name in the callback will probably be useful. A second patch will follow shortly. All the best
On Fri Oct 17 10:32:44 2008, DDUMONT wrote: Show quoted text
> On 2nd thought, providing also the page name in the callback will > probably be useful. A second patch will follow shortly.
Here we go. A full patch to be applied against Curses::UI 0.9605 All the best
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)");
Hello Le Mar. Oct. 14 12:38:10 2008, MDXI a écrit : Show quoted text
> Thanks for the offer. To be honest, if you're using notebooks, you know > them better than I do. I have yet to write any code using one, and they > were added before I took over maintainership. > > Feel free to send a patch, and I'll get a new release out soon.
I've sent a patch about 2 months ago. Could you create a new release of Curses::UI ? I will not be able to release the next version of Config::Model::CursesUI until you release Curses::UI All the best
Subject: Re: [rt.cpan.org #39915] Need a call-back when activating notebook page
Date: Mon, 15 Dec 2008 02:45:20 -0500
To: bug-Curses-UI [...] rt.cpan.org
From: "Shawn Boyette" <mdxi [...] cpan.org>
My apologies. If it's not done a few days from now, please yell at me :) On Sun, Dec 7, 2008 at 7:36 AM, Dominique Dumont via RT <bug-Curses-UI@rt.cpan.org> wrote: Show quoted text
> Queue: Curses-UI > Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=39915 > > > Hello > > Le Mar. Oct. 14 12:38:10 2008, MDXI a écrit :
>> Thanks for the offer. To be honest, if you're using notebooks, you know >> them better than I do. I have yet to write any code using one, and they >> were added before I took over maintainership. >> >> Feel free to send a patch, and I'll get a new release out soon.
> > I've sent a patch about 2 months ago. Could you create a new release of > Curses::UI ? > > I will not be able to release the next version of > Config::Model::CursesUI until you release Curses::UI > > All the best > >
-- Shawn Boyette <sboyette@gmail.com>
On Mon Dec 15 02:45:31 2008, MDXI wrote: Show quoted text
> My apologies. If it's not done a few days from now, please yell at me :)
I got the new version. It works fine and I was able to release Config::Model::CursesUI 1.102 :-) Thanks a bunch