I'm working on a Perl/Tk application, and wanted to know how popup menus (such as those created when you right-click on something in, say, Netscape) work.
There doesn't seem to be any built-in support (i.e. a specific module) for creating these -- I had to look through the Tk source distribution to find what I wanted.
I basically copied their code verbatim, and it works, but relies on some sort of magic I don't understand.
Here's the relevant snippet:
# A right-click in the viewer should bring up the popup menu.
$viewer->bind("<3>", [\&post_popup_menu]);
sub post_popup_menu {
my $w = shift; # no idea how this works.
my $X = $w->XEvent->X;
my $Y = $w->XEvent->Y;
$viewer_menu->Post($X, $Y);
}
The line that mystifies me is the first line of
post_popup_menu... I'm not sending the callback any arguments, but it still manages to receive a mysterious
$w variable somehow. After doing some preliminary poking with Data::Dumper, it looks like $w is the
$viewer that the menu is associated with.
Beyond that, however, I'm stumped... anyone know how Tk works this magic?