Ending due to ctrl-c



Is there a proper way to end due to the program recieving a SIGINT?
I've cooked up an example of what I'm talking about but it has the
maybe un-good problem of calling Gtk::Main::quit() from the signal
handler.  I've learned that signal handlers should never do anything
but set a sentinel flag, so I'm slightly concerned.

Here's the example:
/*
* Filename:  test.cc
* Created:   2007-Feb-06 08:05:13 PM
* Modified:  2007-Feb-06 08:21:04 PM
*
* g++ -o test test.cc `pkg-config --cflags --libs gtkmm-2.4`
*/

#include <signal.h>
#include <iostream>
#include <gtkmm.h>

Gtk::Window* window ;

bool asked_to_close = false ;

void
close_request( int signal )
{
	if( signal != SIGINT )
	{
		return ;
	}

	asked_to_close = true ;
	Gtk::Main::quit() ;
}

bool
close_window( GdkEventWindowState* event )
{
	if( event->new_window_state == ( GdkWindowState ) Gdk::WINDOW_STATE_WITHDRAWN )
	{
		Gtk::Main::quit() ;
	}

	return false ;
}

int
main( int argc, char* argv[] )
{
	signal( SIGINT, close_request ) ;

	Gtk::Main m( argc, argv ) ;

	window = new Gtk::Window() ;
	window->signal_window_state_event().connect( sigc::ptr_fun( close_window ) ) ;
	window->show() ;

	m.run() ;

	if( asked_to_close )
	{
		std::cerr << "Ctrl-C closed." << std::endl ;
	}
}


This method works, but like I said, I'm worried about the call to
Gtk::Main::quit(). I pondered having a Glib::signal_idle function that
checked a boolean flag, but that seemed like less of a good idea.

Thanks,
Paul



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