Re: Popup menu for Gtk2::Entry

On Fri, 03 Feb 2012 12:18:05 +0100
Juergen Harms <Juergen Harms unige ch> wrote:

Thank you, these replies are very helpful:

That is precisely what I have been looking for: the second argument of 
the callback is the menu. However, creating a new MenuItem and appending 
it to the menu does not have the expected result - I will need to do 
some more digging, but that is a good start).

Here are 3 examples. 1 is a popup on a button, the others
on a label.
They might show a way to get it working on an Entry.

Hope they save you some searching thru the archives! :-)


On a button:

use warnings;
use strict;
use Gtk2 -init;

#by muppet

my %items = map {
    my $foo = $_;
    ( $foo => sub { print "$foo\n"; } )
} qw(red orange yellow green blue indigo violet);

my $window = Gtk2::Window->new;
$window->signal_connect( destroy => \&Gtk2::main_quit );

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

my $submenu = Gtk2::Menu->new;
while ( my ( $label, $callback ) = each %items ) {
    my $item = Gtk2::MenuItem->new($label);
    $item->signal_connect( activate => $callback );
my $item = Gtk2::MenuItem->new("Foo");

$item = Gtk2::MenuItem->new("Baz");
$item->signal_connect( activate => sub { print "Baz!\n" } );


my $button = Gtk2::Button->new("Clicky Clicky Clicky");

# only works when you click, not if you activate the button with the keyboard
    button_press_event => sub {
        my ( undef, $event ) = @_;
        $menu->popup( undef, undef, undef, undef, $event->button,
            $event->time );



and here is one on a Label. It appends to the conventional right click menu

#!/usr/bin/perl -w
use strict;
use Gtk2 -init;

# by muppet
my $window = Gtk2::Window->new;
my $label = Gtk2::Label->new ("Hi, i am a label");

# This is what will make it possible to get the context menu on the label
$label->set_selectable (1);
$label->signal_connect (populate_popup => sub {
        my (undef, $menu) = @_;
        my $item = Gtk2::MenuItem->new ('Whee');
        $menu->append ($item);

$window->add ($label);
$window->signal_connect (destroy => sub { Gtk2->main_quit });


and another on a Label, that overrides the default right click.

 #!/usr/bin/perl -w

# Copyright 2008, 2010 Kevin Ryde

# This file is part of Gtk2-Ex-MenuView.
# Gtk2-Ex-MenuView is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
# Gtk2-Ex-MenuView is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# You should have received a copy of the GNU General Public License along
# with Gtk2-Ex-MenuView.  If not, see <>.

# This is pretty much the simplest program that displays a MenuView.  The
# model is a ListStore and the item-create-or-update handler shows column 0
# from it.
# The menu is popped-up from a mouse button press.  Generally a menu is
# handled in one of two ways, either the submenu of a MenuItem in a MenuBar
# etc, or an explicit popup from an event like below.

use strict;
use warnings;
use Gtk2 '-init';
use Gtk2::Ex::MenuView;

my $liststore = Gtk2::ListStore->new ('Glib::String');
$liststore->set ($liststore->append, 0 => 'One');
$liststore->set ($liststore->append, 0 => 'Two');
$liststore->set ($liststore->append, 0 => 'Three');

my $menuview = Gtk2::Ex::MenuView->new (model => $liststore);

  (item_create_or_update => sub {
     my ($menuview, $item, $model, $path, $iter) = @_;
     my $str = $model->get ($iter, 0);  # column 0
     return Gtk2::MenuItem->new_with_label ($str);

  (activate => sub {
     my ($menuview, $item, $model, $path, $iter) = @_;
     print "activate, path=", $path->to_string, "\n";
     print "          data=", $model->get($iter,0), "\n";

my $toplevel = Gtk2::Window->new('toplevel');
$toplevel->signal_connect (destroy => sub { Gtk2->main_quit });

my $label = Gtk2::Label->new ("Press mouse button\nto popup menu");
$toplevel->add ($label);

$toplevel->add_events (['button-press-mask','button-release-mask']);
  (button_press_event => sub {
     my ($toplevel, $event) = @_;
     $menuview->popup (undef, undef, undef, undef,
                       $event->button, $event->time);

exit 0;

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