Context menu 
Author Message
 Context menu

Hi all!
May I make a menu which will appear when I press right button of mouse ?

Best regards,
Vladimir



Mon, 28 Mar 2005 03:49:10 GMT  
 Context menu

Quote:

> Hi all!
> May I make a menu which will appear when I press right button of
> mouse ?

Yes:

use Tk;

$top = new MainWindow;
$top->packPropagate(0);
$f = $top->Frame->pack(-expand => 1, -fill => "both");

my $popup_menu;
$popup_menu = $f->Menu(-title => "Men",
                       -tearoff => $Tk::platform eq 'unix');
$popup_menu->command(-label => "Route l?schen",
                     -command => \&delete_route,
                    );
$popup_menu->command(-label => "Suche wiederholen",
                     -command => \&re_search,
                    );
$popup_menu->command(-label => "Rckweg",
                     -command => \&way_back,
                    );
$f->menu($popup_menu);
$f->Tk::bind('<3>' => sub {
                 my $e = $_[0]->XEvent;
                 $_[0]->PostPopupMenu($e->X, $e->Y);
             });

MainLoop;
__END__

Regards,
        Slaven

--

sf-upload: make batch releases on SourceForge
        http://sf-upload.sf.net



Mon, 28 Mar 2005 04:56:59 GMT  
 Context menu
#-----------------------------------------------------------------

$matrixs{SERVICES} -> bind('<3>' => sub {
                  my $e = $_[0]->XEvent;


                  $_[0] -> PostPopupMenu($e->X, $e->Y);
                 });

$matrixs{SERVICES} -> bind('<ButtonRelease-3>' => sub {
                  print "asadsf";
                 });

#-----------------------------------------------------------------

The problem is: print "asadsf" doesn't work.
If I don't use $_[0] -> PostPopupMenu($e->X, $e->Y)
pritn "asadsf" works.
Where is a problem ?

Best regards,
Vladimir



Tue, 29 Mar 2005 02:20:55 GMT  
 Context menu

Quote:

> #-----------------------------------------------------------------

> $matrixs{SERVICES} -> bind('<3>' => sub {
>                   my $e = $_[0]->XEvent;


>                   $_[0] -> PostPopupMenu($e->X, $e->Y);
>                  });

> $matrixs{SERVICES} -> bind('<ButtonRelease-3>' => sub {
>                   print "asadsf";
>                  });

> #-----------------------------------------------------------------

> The problem is: print "asadsf" doesn't work.
> If I don't use $_[0] -> PostPopupMenu($e->X, $e->Y)
> pritn "asadsf" works.
> Where is a problem ?

I suspect the ButtonRelease event won't be sent to the
$matrixs{SERVICES} widget --- if a menu is posted, then Tk does a
grab, which means that all events are redirected to the grab master
(the menu widget).

It may work if you bind the ButtonRelease-3 event for the menu.

Regards,
        Slaven

--

Tired of using file selectors? Real programmers use the TAB key for
completion and not for jumping around. Try
        http://search.cpan.org/search?mode=module&query=Tk::PathEntry



Tue, 29 Mar 2005 06:28:01 GMT  
 Context menu

Quote:


> > Hi all!
> > May I make a menu which will appear when I press right button of
> > mouse ?

> Yes:

> use Tk;

> $top = new MainWindow;
> $top->packPropagate(0);
> $f = $top->Frame->pack(-expand => 1, -fill => "both");

> my $popup_menu;
> $popup_menu = $f->Menu(-title => "Men",
>                   -tearoff => $Tk::platform eq 'unix');
> $popup_menu->command(-label => "Route l?schen",
>                 -command => \&delete_route,
>                );
> $popup_menu->command(-label => "Suche wiederholen",
>                 -command => \&re_search,
>                );
> $popup_menu->command(-label => "Rckweg",
>                 -command => \&way_back,
>                );
> $f->menu($popup_menu);
> $f->Tk::bind('<3>' => sub {
>             my $e = $_[0]->XEvent;
>             $_[0]->PostPopupMenu($e->X, $e->Y);
>         });

> MainLoop;
> __END__

> Regards,
>    Slaven

When you use exactly that sample, but with trivial callback routines
implemented (say: sub way_back {print "hi"} ) something funny happens.
Under Win2000 the next mouse click will be lost!
For that reason I do not use context menus very often...
Is there a workaround?

Kindly
Mike



Wed, 30 Mar 2005 20:36:14 GMT  
 Context menu

Quote:



> > > Hi all!
> > > May I make a menu which will appear when I press right button of
> > > mouse ?

> > Yes:

> > use Tk;

> > $top = new MainWindow;
> > $top->packPropagate(0);
> > $f = $top->Frame->pack(-expand => 1, -fill => "both");

> > my $popup_menu;
> > $popup_menu = $f->Menu(-title => "Men",
> >                      -tearoff => $Tk::platform eq 'unix');
> > $popup_menu->command(-label => "Route l?schen",
> >                    -command => \&delete_route,
> >                   );
> > $popup_menu->command(-label => "Suche wiederholen",
> >                    -command => \&re_search,
> >                   );
> > $popup_menu->command(-label => "Rckweg",
> >                    -command => \&way_back,
> >                   );
> > $f->menu($popup_menu);
> > $f->Tk::bind('<3>' => sub {
> >                my $e = $_[0]->XEvent;
> >                $_[0]->PostPopupMenu($e->X, $e->Y);
> >            });

> > MainLoop;
> > __END__

> > Regards,
> >       Slaven

> When you use exactly that sample, but with trivial callback routines
> implemented (say: sub way_back {print "hi"} ) something funny happens.
> Under Win2000 the next mouse click will be lost!
> For that reason I do not use context menus very often...
> Is there a workaround?

Maybe it's a focus issue? Is the main window still active after the
popup menu was popped down?

Regards,
        Slaven

--

Visualize XML files in a Tk text widget:
        http://search.cpan.org/search?mode=module&query=Tk::XMLViewer



Wed, 30 Mar 2005 21:31:36 GMT  
 Context menu

Quote:
> > > $f->Tk::bind('<3>' => sub {
> > > my $e = $_[0]->XEvent;
> > > $_[0]->PostPopupMenu($e->X, $e->Y);
> > >      });

> > > MainLoop;
> > > __END__

> > > Regards,
> > > Slaven

> > When you use exactly that sample, but with trivial callback routines
> > implemented (say: sub way_back {print "hi"} ) something funny happens.
> > Under Win2000 the next mouse click will be lost!
> > For that reason I do not use context menus very often...
> > Is there a workaround?

> Maybe it's a focus issue? Is the main window still active after the
> popup menu was popped down?

> Regards,
> Slaven

Well it seems so... I wonder why nobody else experienced that effect.
I experimented some time ago and found that:

...  bind .....sub {...
    $top->Busy();
... PostPopUpMenue.....
    $top->Unbusy()

Quote:
}

made the program behave as expected, but because the cursor changed it was
out of question....
Any idea what one could try?
Thank you Mike


Thu, 31 Mar 2005 03:54:32 GMT  
 Context menu

Quote:

> Hi all!
> May I make a menu which will appear when I press right button of mouse
> ?

> Best regards,
> Vladimir

Does this help?  Post() also accepts a menu item index:

#!/usr/local/bin/perl -w

use Tk;
use Tk::LabOptionmenu;
use Tk::ROText;
use strict;

my $mw = MainWindow->new;
$mw->geometry('400x100');
$mw->focus;

my $toolbar = $mw->Frame->pack(qw/-fill x -expand 1/);
my $file = $toolbar->Menubutton(
     -text   => 'File',
     -relief => 'raised',
);
$file->pack(qw/-side left/);;

my $menu = $file->Menu(-tearoff => 0, -menuitems => [
     [qw/command ~New/],
     '',
     [qw/command ~Open/],
     '',
     [qw/command ~Save/],
     '',
     [qw/command ~Close/],
     '',
     [qw/command ~Quit/, -command => \&exit],
]);
$file->configure(-menu => $menu);

my $menu_index = 0;
my $cursor = $toolbar->LabOptionmenu(
     -label    => 'menu_index',
     -labelPack => [qw/-side left/],
     -variable => \$menu_index,
     -options  => [(0, 2, 4, 6, 8)],
);
$cursor->pack(qw/-side right/);

my $t = $mw->Text->pack;
$t->insert('end', <<"EOT");

<p> invokes \$menu->post(cursor_x, cursor_y)

<P> invokes \$menu->Post(cursor_x, cursor_y, menu_index)
EOT

$mw->bind('<p>' => [sub {

     $menu->post($x, $y);

Quote:
}, Ev('X'), Ev('Y')]);

$mw->bind('<P>' => [sub {

     $menu->Post($x, $y, $menu_index);

Quote:
}, Ev('X'), Ev('Y')]);

MainLoop;

-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the



Thu, 31 Mar 2005 06:35:31 GMT  
 Context menu



Quote:
> > > When you use exactly that sample, but with trivial callback routines
> > > implemented (say: sub way_back {print "hi"} ) something funny happens.
> > > Under Win2000 the next mouse click will be lost!
> > > For that reason I do not use context menus very often...
> > > Is there a workaround?

I might add, the same thing happens with the standard context menu bindings
of Tk::Text: When you discard that menu by clicking somewhere aside the
menu, a new mouse click is possible. When you discard the menu via excape
key, the next mouse click - be it left or right - is not accepted.

I am usind ActiveStates 5.61 Perl, Tk 800.023 and Windows 2000

Thanks for any suggestion
Mike



Thu, 31 Mar 2005 18:12:12 GMT  
 Context menu


Quote:


> > > > When you use exactly that sample, but with trivial callback routines
> > > > implemented (say: sub way_back {print "hi"} ) something funny happens.
> > > > Under Win2000 the next mouse click will be lost!
> > > > For that reason I do not use context menus very often...
> > > > Is there a workaround?

> I might add, the same thing happens with the standard context menu bindings
> of Tk::Text: When you discard that menu by clicking somewhere aside the
> menu, a new mouse click is possible. When you discard the menu via excape
> key, the next mouse click - be it left or right - is not accepted.

This is because the menu does a local grab.  The only solution I've
found is that commands dispatched from the menu need to do a grabRelease
as part of their processing.  You could probably add a binding to the
menu to do this when you press escape as well.
--
Kevin Michael Vail | Dogbert: That's circular reasoning.

http://www.vaildc.net/kevin/


Thu, 31 Mar 2005 23:29:19 GMT  
 Context menu

Quote:


>>Does this help?  Post() also accepts a menu item index:

>>#!/usr/local/bin/perl -w

>>use Tk;
>>use Tk::LabOptionmenu;
>>use Tk::ROText;
>>use strict;
> Where can I find Tk::LabOptionmenu ?
> Nothing shows up on cpan, or google.

Sorry ... mailed privately.


Fri, 01 Apr 2005 03:23:54 GMT  
 Context menu

Quote:
> This is because the menu does a local grab.  The only solution I've
> found is that commands dispatched from the menu need to do a grabRelease
> as part of their processing.  You could probably add a binding to the
> menu to do this when you press escape as well.

Kevin,
thank you for this great advice! I changed Slaven's code - well its
standard code anyway - to:

$this_is_new =$f->menu($popup_menu);
$f->Tk::bind('<3>' => sub {
my $e = $_[0]->XEvent;
my $pop= $_[0]->PostPopupMenu($e->X, $e->Y);
$this_is_new -> grabRelease();
     });

and it works as expected! Even the (minor) problem with ecsape is
gone!

Mike



Fri, 01 Apr 2005 23:34:57 GMT  
 Context menu
A couple of things to note here.

1. In the example code below,

  my $t = $mw->Text->pack;

should probably be:

  my $t = $mw->ROText->pack;

Otherwise the "p" and "P" keys used to pop-up the
$file menu will get inserted into the contents of
the text widget. And the Tk::ROText module is
mentioned at the top of the program but never used.

2. The menu that pops up on a right mouse button
press is apparently a default one provided by the
Text or ROText widget. To address Vladimir's query,
it might be good to have an example which either
replaced that menu with one defined by the user,
or show how a user could customize it. (Beyond my
knowledge currently without delving deep into the
module code.)

3. I notice a small bug/feature. When $menu_index
is set to 0 or 2, the Post(x,y,index) method works
fine on the $file menu. But note what happens when
$menu_index is 4 and then 6. The menu is posted,
but the menu is more and more offset relative to the
mouse cursor (with respect to the menu item it should
be over). Finally when $menu_index is 8, it apparently
is too low altogether so it just points to the middle
of the menu. I believe what is going on here is that
the Y coordinate to position the menu is being
miscalculated due to the '' separators (which are much
thinner than the normal space that a menu row would
occupy). Steve, can you confirm this behavior.
I am running under Win32 ActivePerl build 631 with
Tk 800.023 .

--
Mike Arms

Quote:
-----Original Message-----

Sent: Saturday, October 12, 2002 3:42 PM
To: Vladimir


Subject: Re: Context menu


> Hi all!
> May I make a menu which will appear when I press right button of mouse
> ?

> Best regards,
> Vladimir

Does this help?  Post() also accepts a menu item index:

#!/usr/local/bin/perl -w

use Tk;
use Tk::LabOptionmenu;
use Tk::ROText;
use strict;

my $mw = MainWindow->new;
$mw->geometry('400x100');
$mw->focus;

my $toolbar = $mw->Frame->pack(qw/-fill x -expand 1/);
my $file = $toolbar->Menubutton(
     -text   => 'File',
     -relief => 'raised',
);
$file->pack(qw/-side left/);;

my $menu = $file->Menu(-tearoff => 0, -menuitems => [
     [qw/command ~New/],
     '',
     [qw/command ~Open/],
     '',
     [qw/command ~Save/],
     '',
     [qw/command ~Close/],
     '',
     [qw/command ~Quit/, -command => \&exit],
]);
$file->configure(-menu => $menu);

my $menu_index = 0;
my $cursor = $toolbar->LabOptionmenu(
     -label    => 'menu_index',
     -labelPack => [qw/-side left/],
     -variable => \$menu_index,
     -options  => [(0, 2, 4, 6, 8)],
);
$cursor->pack(qw/-side right/);

my $t = $mw->Text->pack;
$t->insert('end', <<"EOT");

<p> invokes \$menu->post(cursor_x, cursor_y)

<P> invokes \$menu->Post(cursor_x, cursor_y, menu_index)
EOT

$mw->bind('<p>' => [sub {

     $menu->post($x, $y);
}, Ev('X'), Ev('Y')]);

$mw->bind('<P>' => [sub {

     $menu->Post($x, $y, $menu_index);
}, Ev('X'), Ev('Y')]);

MainLoop;

-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the




Sun, 03 Apr 2005 02:58:50 GMT  
 Context menu

IIRC if you do

my $mymenu = $text->Menu(...)

$text->menu( $mymenu );

It will over-ride the whole right-mouse menu.
There are also un(der) documented hooks to allow a derived class
to provide items for the built-in menu. e.g. ROText and Text differ
in Edit menu contents (can't Cut from a ROText...)

One reason it is under-documented is what we have is my generalization
of someone else's (Greg London?) pop-up menu for Text. With the generalization
it is almost (but not quite) general enough to be a feature of widgets in
general

Quote:
>3. I notice a small bug/feature. When $menu_index
>is set to 0 or 2, the Post(x,y,index) method works
>fine on the $file menu. But note what happens when
>$menu_index is 4 and then 6. The menu is posted,
>but the menu is more and more offset relative to the
>mouse cursor (with respect to the menu item it should
>be over). Finally when $menu_index is 8, it apparently
>is too low altogether so it just points to the middle
>of the menu. I believe what is going on here is that
>the Y coordinate to position the menu is being
>miscalculated due to the '' separators (which are much
>thinner than the normal space that a menu row would
>occupy). Steve, can you confirm this behavior.
>I am running under Win32 ActivePerl build 631 with
>Tk 800.023 .

Juding by the amount of re-write in tk8.4 Win32 Menu layout
on earlier tk's was rather suspect.

Quote:

>--
>Mike Arms

>-----Original Message-----

>Sent: Saturday, October 12, 2002 3:42 PM
>To: Vladimir

>Subject: Re: Context menu


>> Hi all!
>> May I make a menu which will appear when I press right button of mouse
>> ?

>> Best regards,
>> Vladimir

>Does this help?  Post() also accepts a menu item index:

>#!/usr/local/bin/perl -w

>use Tk;
>use Tk::LabOptionmenu;
>use Tk::ROText;
>use strict;

>my $mw = MainWindow->new;
>$mw->geometry('400x100');
>$mw->focus;

>my $toolbar = $mw->Frame->pack(qw/-fill x -expand 1/);
>my $file = $toolbar->Menubutton(
>     -text   => 'File',
>     -relief => 'raised',
>);
>$file->pack(qw/-side left/);;

>my $menu = $file->Menu(-tearoff => 0, -menuitems => [
>     [qw/command ~New/],
>     '',
>     [qw/command ~Open/],
>     '',
>     [qw/command ~Save/],
>     '',
>     [qw/command ~Close/],
>     '',
>     [qw/command ~Quit/, -command => \&exit],
>]);
>$file->configure(-menu => $menu);

>my $menu_index = 0;
>my $cursor = $toolbar->LabOptionmenu(
>     -label    => 'menu_index',
>     -labelPack => [qw/-side left/],
>     -variable => \$menu_index,
>     -options  => [(0, 2, 4, 6, 8)],
>);
>$cursor->pack(qw/-side right/);

>my $t = $mw->Text->pack;
>$t->insert('end', <<"EOT");

><p> invokes \$menu->post(cursor_x, cursor_y)

><P> invokes \$menu->Post(cursor_x, cursor_y, menu_index)
>EOT

>$mw->bind('<p>' => [sub {

>     $menu->post($x, $y);
>}, Ev('X'), Ev('Y')]);

>$mw->bind('<P>' => [sub {

>     $menu->Post($x, $y, $menu_index);
>}, Ev('X'), Ev('Y')]);

>MainLoop;

>-++**==--++**==--++**==--++**==--++**==--++**==--++**==
>This message was posted through the Stanford campus mailing list
>server.  If you wish to unsubscribe from this mailing list, send the


--
Nick Ing-Simmons
http://www.ni-s.u-net.com/

-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the



Sun, 03 Apr 2005 04:33:41 GMT  
 Context menu

Quote:

> A couple of things to note here.

> 1. In the example code below,

>   my $t = $mw->Text->pack;

> should probably be:

>   my $t = $mw->ROText->pack;

OK.

Quote:

> 2. The menu that pops up on a right mouse button
> press is apparently a default one provided by the
> Text or ROText widget. To address Vladimir's query,
> it might be good to have an example which either
> replaced that menu with one defined by the user,
> or show how a user could customize it. (Beyond my
> knowledge currently without delving deep into the
> module code.)

Sorry, I never actually clicked in the (RO)Text widget, so the default
menu never popped up - Nick described how to
disable/change it.

$mw->bind('<ButtonRelease-3>' => [sub {

     $menu->Post($x, $y, $menu_index);

Quote:
}, Ev('X'), Ev('Y')]);

I suppose my actual intent was to have him try the Post() method.

Quote:

> 3. I notice a small bug/feature. When $menu_index
> is set to 0 or 2, the Post(x,y,index) method works
> fine on the $file menu. But note what happens when
> $menu_index is 4 and then 6. The menu is posted,
> but the menu is more and more offset relative to the
> mouse cursor (with respect to the menu item it should
> be over). Finally when $menu_index is 8, it apparently
> is too low altogether so it just points to the middle
> of the menu. I believe what is going on here is that
> the Y coordinate to position the menu is being
> miscalculated due to the '' separators (which are much
> thinner than the normal space that a menu row would
> occupy). Steve, can you confirm this behavior.
> I am running under Win32 ActivePerl build 631 with
> Tk 800.023 .

On Mac OS X it's always centered on the menu item.
Oh, well (;

-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the



Mon, 04 Apr 2005 01:33:34 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Perl TK and context sensitive menus

2. Strange context interpretation [rexexp][context]

3. array context versus scalar context

4. menu.pl-2.3 - perl menus - Part02/02

5. menu.pl-2.3 - perl menus - Part01/02

6. menu.pl-2.3 - perl menus - Announcement

7. menu.pl-2.2 - perl menus - Part02/02

8. menu.pl-2.2 - perl menus - Part01/02

9. menu.pl-2.0 - perl menus - Part02/02

10. menu.pl-2.0 - perl menus - Part01/02

11. menu.pl v1.3 - curseperl menu facility

12. menu.pl - perl menu package

 

 
Powered by phpBB® Forum Software