Re: popup menus?



On Friday, August 1, 2003, at 04:27  PM, Matthew Weier OPhinney wrote:

I've been banging my head against a wall all day, so perhaps its time to
turn to the list...

please, think of the wall!  ;-)


this sort of thing works for me:

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

use Gtk2 -init;

my $w = Gtk2::Window->new;
$w->set_size_request (200, 200);
$w->signal_connect (delete_event => sub {Gtk2->main_quit;});
$w->add (Gtk2::Label->new ('right-click me!'));
# if you use a button or a drawing area, this may not be
# necessary, but plain old windows don't listen for
# button-press events.
$w->add_events ('button-press-mask');
$w->signal_connect (button_press_event => sub {
                my ($widget, $event) = @_;
                # only pay attention to right-clicks
                return 0 unless 3 == $event->button;

                print "right clicked!\n";
                # here i create a menu by hand....
                my $menu = Gtk2::Menu->new;
                foreach (qw/ one two red blue /) {
                        my $item = Gtk2::MenuItem->new ("$_ fish");
                        $item->show;
                        $menu->append ($item);
                        $item->signal_connect (activate => sub {
                                        print "@_ (fish)\n";
                                }, $_);
                }

                # and pop it up.
                $menu->popup (undef, undef, undef, undef,
                              $event->button, $event->time);
        });
$w->show_all;
Gtk2->main;
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

since i'm popping a menu out of thin air, i don't need the first two parameters, which are the parent shell and the parent item (both don't exist, so, undef). since this is a simple example i'm not going to clutter it up by demonstrating a menu positioning function, so i pass undef for the third parameter, and the fourth as well, since that would be data to be passed to it.

the fifth is the button to track, and the sixth is the timestamp. i successfully passed undef, for the timestamp, but the bindings complained at me. would it be proper to substitute GDK_CURRENT_TIME or the like as a default value for the timestamp?


i created the menu by hand a) to show that it's really easy, b) because i've always done it that way and don't know how to get ItemFactory to do it, which never bothered me because c) you usually use ItemFactory to take care of accelerators for you, but this is a right-click context menu and you don't need accelerators for these! that said, i usually have a convenience function wrap up creating context and option menus for me.



    (2) I'm not certain the correct way to cause the menu to popup. The
        Gtk2 specs indicate I need to tell menu.popup the button that
        caused it to raise and the timestamp. I don't know where to get
        these.

generally, the button and timestamp come from the GdkEvent that came with the signal at which you are popping up the menu.


--
muppet <scott at asofyet dot org>




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]