Re: Closing a window to trigger the 'delete_event' signal



* Francois Marier <francois nit ca>:

Hi,

I was somehow expecting to be able to do something like
$window->close() and have it check the signal and do the right thing.
All the tutorials I've seen either call $window->destroy() or
Gtk2->main_quit directly, bypassing the delete_event completely.

Because the "delete_event" signal is not to be used by the application,
but only by the window manager.

You should always use the Gtk2::Widget::destroy method in order to
*close* a window. If I understood correctly what you're trying to
accomplish in your code, you wish to stop a user from exiting from your
app; closing a window is done via a widget, being it a menu or a button.
So, you should place the check on the attempts (or any system alike)
inside a callback attached to that widget.

+++

This is a dumb example, but you should *always* follow these rules. If
you don't want the user to quit using the "close" button in the title
bar, you should return TRUE from the "delete_event" callback - but you
should *never* synthetize a "delete_event" by yourself.

use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

$clicked = 0;

$w = Gtk2::Window->new ('toplevel');
$w->set_border_width (10);
$w->signal_connect (delete_event => sub { return TRUE unless $clicked; return FALSE; });
$w->signal_connect (destroy      => sub { Gtk2->main_quit; });

$box = Gtk2::VBox->new (FALSE, 0);
$w->add($box);

$label = Gtk2::Label->new ("Press the first button to activate the second.");
$box->pack_start ($label, FALSE, FALSE, 0);

$b1 = Gtk2::Button->new ("Press me, first");
$b2 = Gtk2::Button->new ("Quit");
$b2->set_sensitive (FALSE);

$b1->signal_connect (clicked => sub {
                $clicked = 1;
                $label->set_text ("Now press the second button to quit.");
                $b2->set_sensitive (TRUE);
        });

$b2->signal_connect (clicked => sub {
                return unless $clicked;
                
                $w->destroy;
        });

$box->pack_start ($b1, FALSE, FALSE, 0);
$box->pack_start ($b2, FALSE, FALSE, 0);

$w->show_all;

Gtk2->main;

0;

+++

Ciao,
 Emmanuele.

-- 
Emmanuele Bassi (Zefram)                 [ http://www.emmanuelebassi.net ]
GnuPG Key fingerprint = 4DD0 C90D 4070 F071 5738  08BD 8ECC DB8F A432 0FF4



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