popup menu

Thomas Bayen writes:

In my program I want to open a popup menu when the user clicks a button.
It's a bit like an optionmenu but not really the same so I do not want
to use an optionmenu.

(This is not the same problem I had some time ago with my self-made
popup window, asking for help on this list. This time I try to use just
the "standard" popup menu.)

There is an example in C in the docs on page
http://developer.gnome.org/doc/API/2.0/gtk/GtkMenu.html but I can't make
it work with perl. I have no event (because the "clicked" signal of
Gtk2::Button does not give me one). But the function
gtk_get_current_event_time() mentioned in
seems not to exist. :-(

If I do not set the time parameter (but give 0 or undef) I get a small
popup window just with a border and size 1x1 but without menu items.

What can I do? Has someone a working example for a popup menu?

Okay, this is kinda kludged because my original code is *way* too ugly
to show anyone right now, but first we attach the button press:

        $treeview = $gladexml->get_widget('treeview1');
        $treeview->signal_connect(button_press_event =>  \&on_treeview1_button_press_event);

I do it explicitly because for some reason the autoconnect doesn't
seem to work, but I haven't gone in and tested enough to report it as
a bug.

sub on_treeview1_button_press_event
    my ($widget, $event) = @_;

    # Check for a right click
    if ($event->type() eq 'button-press' 
        && $event->button() == 3)
        my ($x,$y) = $event->get_coords;
        my ($path, $column, $px, $py) = $widget->get_path_at_pos($x,$y);

        my $menu = Gtk2::Menu->new();

        my $menuitemWrite = Gtk2::MenuItem->new('Write');
        $menuitemWrite->signal_connect(activate => sub { print "Write\n"});

        my $menuitem1 = Gtk2::MenuItem->new("Hey");
        my $menuitem2 = Gtk2::MenuItem->new("There");
        my $menuitem3 = Gtk2::MenuItem->new("Dude");
        $menuitem1->signal_connect(activate =>
                                   sub { print "Activated $menuitem1 on $widget for $node\n" });
        $menuitem3->signal_connect(activate =>
                                   sub { print "Activated $menuitem1 on $widget for $node\n" });
        $menuitem2->signal_connect(activate =>
                                   sub { print "Activated $menuitem1 on $widget for $node\n" });


        $menu->popup(undef, # parent menu shell
                     undef, # parent menu item
                     undef, # menu pos func
                     undef, # data
        return 1;
    return undef;

Probably horribly buggy and I've cleaned out enough stuff that it
might not even be syntactically correct, but should get something more
than 1x1 appearing on the screen and give you a place to start.


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