#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.
| [reply] [d/l] |
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 | [reply] [d/l] |
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
| [reply] [d/l] |