skewray has asked for the wisdom of the Perl Monks concerning the following question:

If I add a Tk::Menu popup to a Tk:Notebook, then how do I figure out in which tab the user clicked to initiate the menu popup? Notebook->Info( "focus" ) appears to just return the active tab. I could find out where the cursor is in the routine spawned by the popup menu, but that would return the location of the cursor after the menu item is clicked on, perhaps differing from the original tab.

Thanks,

Brian

Replies are listed 'Best First'.
Re: Popup Menus on Tk::Notebook tabs
by Marshall (Canon) on Sep 21, 2009 at 01:34 UTC
    It would be most helpful if you could post your current code.

    I presume you can get this far...

    #button 3 is "right click" $someWidget->bind('<3>', sub { my $widget = shift; print "right button clicked! Pop up menu now\n"; #run methods on $widget my $Ev = $widget->XEvent; #stuff is learned here... #then new window is generated });
    Basically to get the "right button, #3" to work, you have to "bind" this button to the Tk object. Show some code and we shall work on it.
Re: Popup Menus on Tk::Notebook tabs
by lamprecht (Friar) on Sep 21, 2009 at 06:55 UTC
    Hi,

    here is a short example (check Tk::bind for valid Event symbols/characters):

    use warnings; use strict; use Tk; my $mw = tkinit; my $n = $mw->NoteBook()->pack; for(qw/foo bar baz/){$n->add($_, -label => $_, );} $n->bind('<3>',[\&process_bt3,Ev('x'),Ev('y')]); MainLoop; sub process_bt3{ my ($w, $x, $y) = @_; my $selected = $w->identify($x,$y); print "selected Tab: $selected\n" if( $selected ); }

    Cheers, Christoph

    update: link for bind doc

      Here is the code that I came up with a few hours after posting, mostly cribbed from trolling the web. I have it in a .pm file:
      use warnings ; use strict ; use Tk ; use Tk::NoteBook ; my $popup_menu_items = [ [Button => "Close", -command => \&popup_close], [Button => "Other", -command => \&popup_other] ] ; my $tabs ; my $popup_menu ; sub sk_notebook_add { my $MW = shift ; $tabs = $MW->NoteBook()->pack( -fill=>'both', -expand=>1 ); $popup_menu = $tabs->Menu( -menuitems => $popup_menu_items, -tearo +ff => 0 ) ; $tabs->bind( "<Button-3>" => [ \&popup_spot, Ev('X'), Ev('Y') ] ) +; return $tabs ; } my $popup_tab_name ; sub popup_spot { my $x = $_[1] - $tabs->rootx ; my $y = $_[2] - $tabs->rooty ; $popup_tab_name = $tabs->identify( $x, $y ) ; $popup_menu->Popup( -popover => "cursor", -popanchor => 'nw' ) if( + $popup_tab_name ) ; } sub popup_close { $tabs->delete( $popup_tab_name ) ; } sub popup_other { print "name = $popup_tab_name\n" ; } 1 __END__
      I have only been programming Perl for a few weeks, so I am not sure what correct style is, but the 'global' variables in the module bother me. I am not sure how else to hand the appropriate widget to the popup subroutines, or even if having a cute way to do so is good Perl.

      Brian