Re: label with context menu (populate-popup)

Peter Daum wrote:
Is there an easy way to get a context menu on a label?
According to the documentation, it should support a
'populate-popup' signal, so it looks like this should be
painless. If I try the following (which works fine on other
I get no warning, only the signal is never triggered.
The documentation for GtkLabel doesn't say when this
signal occurs, I just assumed it would be triggered
whith the right mouse button...

Labels are "no-window" widgets.  You must have a window to receive an event. 
Labels have no window and therefore receive no events.

The way to get a Label to have a window is to make it selectable.  In fact,
it's when it is selectable that the popup menu is useful, because you can
select clipboard operations.

To wit:

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

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 });

muppet <scott at asofyet dot org>

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