Re: problems destroying Gtk2::Window

On Sep 17, 2009, at 4:40 AM, Tomáš Bažant wrote:

i working an a small application which uses one main Gtk2::Window. i
have connected the destroy-event and delete-event to it, but after
clicking on X close button of the window manager, the window disappears
and i have no chance to manage it any further.

any idea how to make it obey?

Depends on what you want to do. Are you wanting to catch the user clicking the "x" so you can prompt before closing the window?

The delete-event signal has a return type, and what you return from delete-event is important!

Here's one that does pretty much everything and should give you a feel for it. You connect to destroy to kill the main loop so that the main loop stops no matter how the window is destroyed. You connect to delete-event to trap the user trying to kill the window via the window manager (including shortcuts like Alt+F4, etc). There's really no reason to connect to destroy-event.

#!/usr/bin/env perl
use strict;
use warnings;
use Gtk2 -init;
use Glib ':constants';

my $window = Gtk2::Window->new;
$window->set_title ("I do stuff.");

my $want_prompt_check =
Gtk2::CheckButton->new ("Ask before closing via window manager kill");

my $kill_me_button = Gtk2::Button->new ("I demand that you shoot me now");

$kill_me_button->signal_connect (clicked => sub {
    print "Clicky!  Killing the window directly.\n";

$window->signal_connect (delete_event => sub {
    print "Window manager kill event!\n";
    my $should_prompt = $want_prompt_check->get_active;
    print "  User " . ($should_prompt ? "wants" : "does not want")
        . " to be prompted first.\n";

    if ($should_prompt) {
        my $dialog = Gtk2::MessageDialog->new
            ($window, [], 'question', 'none',
             "Do you really want to kill the window?");
        $dialog->add_buttons ( "No, just kidding" => 'cancel',
                               "Yes, kill it" => 'accept');
        my $response = $dialog->run;

        if ($response ne 'accept') {
print "User didn't say yes, returning TRUE to say we handled the"
                . "event\nand inhibit the default event actions.\n";
            return TRUE;

print "Returning FALSE from delete-event will let the default action happen.\n"
        . "(The default action is to destroy the window.)\n";
    return FALSE;

$window->signal_connect (destroy => sub {
    print "Woe betide me, i art dead.  I will stop main loop now.\n";

my $vbox = Gtk2::VBox->new;
$vbox->pack_start ($want_prompt_check, FALSE, FALSE, 0);
$vbox->pack_start ($kill_me_button, FALSE, FALSE, 0);
$window->add ($vbox);


print "Main loop finished!\n";


Santa ate all the cookies!
  -- Zella's first, indignant words on Christmas morning, '07.

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