2
/* $Id: main.ccg,v 1.13 2006/09/19 20:08:42 murrayc Exp $ */
6
* Copyright 1998-2002 The gtkmm Development Team
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free
20
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
#include <gtkmmconfig.h>
25
#include <gtkmm/main.h>
26
#include <gtkmm/window.h>
27
#include <glibmm/init.h>
28
#include <giomm/init.h>
29
#include <pangomm/wrap_init.h>
30
#ifdef GTKMM_ATKMM_ENABLED
31
#include <atkmm/wrap_init.h>
32
#endif //GTKMM_ATKMM_ENABLED
33
#include <gdkmm/wrap_init.h>
34
#include <gtkmm/wrap_init.h>
39
/* This class tells sigc++ how to break GTK+ main signal connections. Since
40
* the gtk_*_remove() functions have the same signature, all main signals can
41
* be handled by a single class. Special handling is needed for signals that
42
* don't support destroy notification; see the comment on connection_list_.
44
class GtkMainConnectionNode
47
// A function taking a connection id, e.g. gtk_timeout_remove().
48
typedef void (*RemoveFunc) (guint);
50
explicit GtkMainConnectionNode(const sigc::slot_base& slot);
52
static void* notify(void* data);
53
static void destroy_notify_handler(void* data);
55
// Call this after installing the GTK+ callback.
56
void install(guint conn_id, RemoveFunc remove_func, bool has_destroy_notify);
58
inline sigc::slot_base* get_slot();
60
static bool list_remove(GtkMainConnectionNode* conn_node);
61
static void list_notify_all();
64
sigc::slot_base slot_;
65
static GSList* connection_list_;
68
RemoveFunc remove_func_;
69
bool has_destroy_notify_;
74
/* The global connection_list_ is needed to deal with GTK+ main signals
75
* that don't support destroy notification. This applies only to
76
* gtk_key_snooper_install() and gtk_init_add().
78
* The list is static and not a member of Gtk::Main, in order to support
79
* connection to Gtk::Main::signal_run() before Gtk::Main is instantiated.
80
* Thus, it's possible to install initialization hooks in global constructors,
84
GSList* GtkMainConnectionNode::connection_list_ = 0;
86
GtkMainConnectionNode::GtkMainConnectionNode(const sigc::slot_base& slot)
91
has_destroy_notify_ (false),
94
slot_.set_parent(this, &GtkMainConnectionNode::notify);
98
void* GtkMainConnectionNode::notify(void* data)
100
// notification from the sigc++ side ...
102
GtkMainConnectionNode *const self = static_cast<GtkMainConnectionNode*>(data);
104
// this call might be triggered from destroy_notify_handler().
105
if(!self->destroyed_)
107
// during (*remove_func_)() destroy_notify_handler() might get called.
108
// This must not lead to the destruction of the object!
109
self->destroyed_ = true;
111
// disconnect from the gtk+ side.
112
(*(self->remove_func_))(self->conn_id_);
114
// remove self from global list.
115
if (!self->has_destroy_notify_)
118
// destruction of slot_ notifies all objects referring to it.
126
void GtkMainConnectionNode::destroy_notify_handler(void* data)
128
// notification from the gtk+ side ...
130
GtkMainConnectionNode *const self = static_cast<GtkMainConnectionNode*>(data);
132
// this call might be triggered from notify().
133
if(!self->destroyed_)
135
self->destroyed_ = true;
137
// The GTK+ side is disconnected now, thus the ID is no longer valid.
140
// remove self from global list.
141
if (!self->has_destroy_notify_)
144
// destruction of slot_ notifies all objects referring to it.
149
void GtkMainConnectionNode::install(
150
guint conn_id, GtkMainConnectionNode::RemoveFunc remove_func, bool has_destroy_notify)
153
remove_func_ = remove_func;
154
has_destroy_notify_ = has_destroy_notify;
156
if (!has_destroy_notify_)
157
connection_list_ = g_slist_prepend(connection_list_, this);
161
sigc::slot_base* GtkMainConnectionNode::get_slot()
167
bool GtkMainConnectionNode::list_remove(GtkMainConnectionNode* conn_node)
169
// The conn_node pointer is only valid if we still hold
170
// a reference of the ConnectionNode in our global list.
172
if(GSList *const link = g_slist_find(connection_list_, conn_node))
174
connection_list_ = g_slist_delete_link(connection_list_, link);
181
/* Cleanup function to be called by the Gtk::Main destructor. The elements
182
* are removed prior to notification, in order to avoid invalid elements in
186
void GtkMainConnectionNode::list_notify_all()
188
while(connection_list_ != 0)
190
GtkMainConnectionNode *const conn_node =
191
static_cast<GtkMainConnectionNode*>(connection_list_->data);
193
connection_list_ = g_slist_delete_link(connection_list_, connection_list_);
195
// no need to search the list in notify().
196
conn_node->has_destroy_notify_ = true;
198
// conn_node gets destroyed from notify().
203
} // anonymous namespace
209
/**** Gtk::KeySnooperSig ***************************************************/
211
sigc::connection KeySnooperSig::connect(const KeySnooperSig::SlotType& slot)
213
GtkMainConnectionNode *const conn_node = new GtkMainConnectionNode(slot);
214
const sigc::connection connection(*conn_node->get_slot());
216
const guint conn_id = gtk_key_snooper_install(&KeySnooperSig::gtk_callback, conn_node);
218
conn_node->install(conn_id, >k_key_snooper_remove, false);
223
gint KeySnooperSig::gtk_callback(GtkWidget* widget, GdkEventKey* event, gpointer data)
227
// Recreate the specific SlotType from the generic slot node.
228
GtkMainConnectionNode* conn_node = static_cast<GtkMainConnectionNode*>(data);
229
SlotType* pSlot = static_cast<SlotType*>(conn_node->get_slot());
230
return (*pSlot)(Glib::wrap(widget), event);
234
Glib::exception_handlers_invoke();
241
/**** Gtk::Main -- static data *********************************************/
243
KeySnooperSig Main::signal_key_snooper_;
244
Main* Main::instance_ = 0;
247
/**** Gtk::Main -- construction/destruction ********************************/
249
Main::Main(int& argc, char**& argv, bool set_locale)
251
init(&argc, &argv, set_locale);
254
Main::Main(int* argc, char*** argv, bool set_locale)
256
init(argc, argv, set_locale);
259
/*Main::Main(int* argc, char*** argv, const std::string& parameter_string, const std::vector<const Glib::OptionEntry&>& entries, const std::string& translation_domain)
261
init(argc, argv, parameter_string, entries, translation_domain);
264
Main::Main(bool set_locale)
266
init(0, 0, set_locale);
271
// A second Gtk::Main will produce a warning, but
272
// Main::~Main would still run. So this prevents the crash.
273
if(instance_ == this)
276
GtkMainConnectionNode::list_notify_all();
278
// Release the gtkmm type registration tables,
279
// allowing Main to be instantiated again:
280
Glib::wrap_register_cleanup();
281
Glib::Error::register_cleanup();
286
void Main::init(int* argc, char*** argv, bool set_locale)
290
g_warning("Gtk::Main::init() called twice");
295
gtk_disable_setlocale();
297
//TODO: Add support for gtk_init_check().
298
gtk_init(argc, argv);
300
init_gtkmm_internals();
305
Main::Main(int& argc, char**& argv, Glib::OptionContext& option_context)
309
g_warning("Gtk::Main::init() called twice");
313
init_gtkmm_internals();
316
//This reimplements some stuff from gtk_init_with_options(),
317
//without calling check_setugid(), because that is not public API.
319
add_gtk_option_group(option_context);
321
option_context.parse(argc, argv);
325
// This is a static method so that it can be used before Main is instantiated,
326
// for instance in Gnome::canvas_init(). But if you use this method, you
327
// _must_ have a Gtk::Main, so that Main::~Main() is called to clean this up
328
// later. Of course I can't imagine any situation in which you wouldn't have
331
void Main::init_gtkmm_internals()
333
static bool init_done = false;
340
// Populate the map of GTypes to C++ wrap_new() functions.
342
#ifdef GTKMM_ATKMM_ENABLED
344
#endif //GTKMM_ATKMM_ENABLED
352
void Main::add_gtk_option_group(Glib::OptionContext& option_context, bool open_default_display)
354
//Get the option group:
355
Glib::OptionGroup gtkgroup( gtk_get_option_group(open_default_display) ); //Takes ownership of the GOptionGroup.
357
//Give it to the option_context, which will also then own the underlying GOptionGroup, deleting it when necessary:
358
option_context.add_group(gtkgroup);
362
/**** Gtk::Main -- static forwarder methods ********************************/
364
Main* Main::instance()
371
instance_->run_impl();
374
void Main::run(Gtk::Window& window)
377
window.signal_hide().connect(sigc::mem_fun(*instance_, &Main::on_window_hide));
378
instance_->run_impl();
383
instance_->quit_impl();
388
return instance_->level_impl();
391
bool Main::iteration(bool blocking)
393
return instance_->iteration_impl(blocking);
396
bool Main::events_pending()
398
return instance_->events_pending_impl();
402
/**** Gtk::Main -- static signal accessors *********************************/
404
KeySnooperSig& Main::signal_key_snooper()
406
return signal_key_snooper_;
410
/**** Gtk::Main -- protected virtuals **************************************/
412
void Main::run_impl()
417
void Main::quit_impl()
422
guint Main::level_impl()
424
return gtk_main_level();
427
bool Main::iteration_impl(bool blocking)
429
return gtk_main_iteration_do(blocking);
432
bool Main::events_pending_impl()
434
return gtk_events_pending();
437
void Main::on_window_hide()
442
} /* namespace Gtk */