Re: Blocking Main Window destroy siginal



On Sun, 2004-09-19 at 08:52, Jasun Wurster wrote:
Hi,

I would like to prevent my app from being closed by the user during some 
database writes and data transfers.

Is there a way to block the Window destroy signal so that if the 'x' button in 
the main window is clicked it is ignored and the app keeps running?

I do believe all you need to do is connect the 'delete-event' signal as
the destroy signal is not the one being sent when the user clicks the
"x".

This example illustrates the usage of a toggled switch passed as a
reference to the signals which controls the exiting function.

<code>
/usr/bin/perl -e '
use strict;
use Glib qw( TRUE FALSE );
use Gtk2 -init;

my $block = FALSE;
my $window = new Gtk2::Window ( "toplevel" );
my $button = new Gtk2::Button ( "toggle block" );
$window->add( $button );
$button->signal_connect( clicked => sub {
                             my ( $w, $bl ) = ( @_ );
                             # invert the state
                             $$bl = ( $$bl == TRUE ) ? FALSE : TRUE;
                             # report the current state
                             if ( $$bl == TRUE ) {
                                 print STDERR "BLOCKED\n";
                             } else {
                                 print STDERR "UNBLOCKED\n";
                             }
                         }, \$block );
$window->signal_connect( "delete-event" => sub {
                             my ( $w, $e, $bl ) = ( @_ );
                             # unless we're blocked, quit:
                             unless ( $$bl == TRUE ) {
                                 main_quit Gtk2;
                             }
                             return( $$bl );
                         }, \$block );
$window->show_all();
main Gtk2;
'
</code>

Although in the time it took me to write this I've noticed there are two
replies already. heh. I need to learn to type faster if I want to beat
the likes of muppet to the punch :)

Anyways, hope this helps some at least.

Cheers!

-- 
Kevin C. Krinke <kckrinke opendoorsoftware com>
Open Door Software Inc.




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