1 // Generated by gtkmmproc -- DO NOT MODIFY!
3 #include <gtkmm/main.h>
4 #include <gtkmm/private/main_p.h>
11 * Copyright 1998-2002 The gtkmm Development Team
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
23 * You should have received a copy of the GNU Library General Public
24 * License along with this library; if not, write to the Free
25 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <gtkmmconfig.h>
30 #include <gtkmm/main.h>
31 #include <gtkmm/window.h>
32 #include <glibmm/init.h>
33 #include <pangomm/wrap_init.h>
34 #include <atkmm/wrap_init.h>
35 #include <gdkmm/wrap_init.h>
36 #include <gtkmm/wrap_init.h>
41 /* This class tells sigc++ how to break GTK+ main signal connections. Since
42 * the gtk_*_remove() functions have the same signature, all main signals can
43 * be handled by a single class. Special handling is needed for signals that
44 * don't support destroy notification; see the comment on connection_list_.
46 class GtkMainConnectionNode
49 // A function taking a connection id, e.g. gtk_timeout_remove().
50 typedef void (*RemoveFunc) (guint);
52 explicit GtkMainConnectionNode(const sigc::slot_base& slot);
54 static void* notify(void* data);
55 static void destroy_notify_handler(void* data);
57 // Call this after installing the GTK+ callback.
58 void install(guint conn_id, RemoveFunc remove_func, bool has_destroy_notify);
60 inline sigc::slot_base* get_slot();
62 static bool list_remove(GtkMainConnectionNode* conn_node);
63 static void list_notify_all();
66 sigc::slot_base slot_;
67 static GSList* connection_list_;
70 RemoveFunc remove_func_;
71 bool has_destroy_notify_;
76 /* The global connection_list_ is needed to deal with GTK+ main signals
77 * that don't support destroy notification. This applies only to
78 * gtk_key_snooper_install() and gtk_init_add().
80 * The list is static and not a member of Gtk::Main, in order to support
81 * connection to Gtk::Main::signal_run() before Gtk::Main is instantiated.
82 * Thus, it's possible to install initialization hooks in global constructors,
86 GSList* GtkMainConnectionNode::connection_list_ = 0;
88 GtkMainConnectionNode::GtkMainConnectionNode(const sigc::slot_base& slot)
93 has_destroy_notify_ (false),
96 slot_.set_parent(this, &GtkMainConnectionNode::notify);
100 void* GtkMainConnectionNode::notify(void* data)
102 // notification from the sigc++ side ...
104 GtkMainConnectionNode *const self = static_cast<GtkMainConnectionNode*>(data);
106 // this call might be triggered from destroy_notify_handler().
107 if(!self->destroyed_)
109 // during (*remove_func_)() destroy_notify_handler() might get called.
110 // This must not lead to the destruction of the object!
111 self->destroyed_ = true;
113 // disconnect from the gtk+ side.
114 (*(self->remove_func_))(self->conn_id_);
116 // remove self from global list.
117 if (!self->has_destroy_notify_)
120 // destruction of slot_ notifies all objects referring to it.
128 void GtkMainConnectionNode::destroy_notify_handler(void* data)
130 // notification from the gtk+ side ...
132 GtkMainConnectionNode *const self = static_cast<GtkMainConnectionNode*>(data);
134 // this call might be triggered from notify().
135 if(!self->destroyed_)
137 self->destroyed_ = true;
139 // The GTK+ side is disconnected now, thus the ID is no longer valid.
142 // remove self from global list.
143 if (!self->has_destroy_notify_)
146 // destruction of slot_ notifies all objects referring to it.
151 void GtkMainConnectionNode::install(
152 guint conn_id, GtkMainConnectionNode::RemoveFunc remove_func, bool has_destroy_notify)
155 remove_func_ = remove_func;
156 has_destroy_notify_ = has_destroy_notify;
158 if (!has_destroy_notify_)
159 connection_list_ = g_slist_prepend(connection_list_, this);
163 sigc::slot_base* GtkMainConnectionNode::get_slot()
169 bool GtkMainConnectionNode::list_remove(GtkMainConnectionNode* conn_node)
171 // The conn_node pointer is only valid if we still hold
172 // a reference of the ConnectionNode in our global list.
174 if(GSList *const link = g_slist_find(connection_list_, conn_node))
176 connection_list_ = g_slist_delete_link(connection_list_, link);
183 /* Cleanup function to be called by the Gtk::Main destructor. The elements
184 * are removed prior to notification, in order to avoid invalid elements in
188 void GtkMainConnectionNode::list_notify_all()
190 while(connection_list_ != 0)
192 GtkMainConnectionNode *const conn_node =
193 static_cast<GtkMainConnectionNode*>(connection_list_->data);
195 connection_list_ = g_slist_delete_link(connection_list_, connection_list_);
197 // no need to search the list in notify().
198 conn_node->has_destroy_notify_ = true;
200 // conn_node gets destroyed from notify().
205 } // anonymous namespace
211 /**** Gtk::RunSig **********************************************************/
213 sigc::connection RunSig::connect(const RunSig::SlotType& slot)
215 GtkMainConnectionNode *const conn_node = new GtkMainConnectionNode(slot);
216 const sigc::connection connection(*conn_node->get_slot());
218 // The callback will be invoked the next time gtk_main() is called.
219 gtk_init_add(&RunSig::gtk_callback, conn_node);
221 conn_node->install(0, 0, false); // there's no gtk_init_remove()
226 gboolean RunSig::gtk_callback(gpointer data)
228 GtkMainConnectionNode *const conn_node =
229 static_cast<GtkMainConnectionNode*>(data);
231 if(GtkMainConnectionNode::list_remove(conn_node))
235 // Recreate the specific SlotType from the generic slot_base.
236 SlotType* pSlot = static_cast<SlotType*>(conn_node->get_slot());
241 Glib::exception_handlers_invoke();
244 // We don't need it anymore, since RunSig is a one-shot signal.
245 GtkMainConnectionNode::destroy_notify_handler(conn_node);
252 /**** Gtk::QuitSig *********************************************************/
254 sigc::connection QuitSig::connect(const QuitSig::SlotType& slot, guint main_level)
256 GtkMainConnectionNode *const conn_node = new GtkMainConnectionNode(slot);
257 const sigc::connection connection(*conn_node->get_slot());
259 const guint conn_id = gtk_quit_add_full(
260 main_level, &QuitSig::gtk_callback, 0 /* marshaller */,
261 conn_node, &GtkMainConnectionNode::destroy_notify_handler);
263 conn_node->install(conn_id, >k_quit_remove, true);
268 gboolean QuitSig::gtk_callback(gpointer data)
273 GtkMainConnectionNode* node = static_cast<GtkMainConnectionNode*>(data);
274 SlotType* pSlot = static_cast<SlotType*>(node->get_slot());
279 Glib::exception_handlers_invoke();
285 /**** Gtk::KeySnooperSig ***************************************************/
287 sigc::connection KeySnooperSig::connect(const KeySnooperSig::SlotType& slot)
289 GtkMainConnectionNode *const conn_node = new GtkMainConnectionNode(slot);
290 const sigc::connection connection(*conn_node->get_slot());
292 const guint conn_id = gtk_key_snooper_install(&KeySnooperSig::gtk_callback, conn_node);
294 conn_node->install(conn_id, >k_key_snooper_remove, false);
299 gint KeySnooperSig::gtk_callback(GtkWidget* widget, GdkEventKey* event, gpointer data)
303 // Recreate the specific SlotType from the generic slot node.
304 GtkMainConnectionNode* conn_node = static_cast<GtkMainConnectionNode*>(data);
305 SlotType* pSlot = static_cast<SlotType*>(conn_node->get_slot());
306 return (*pSlot)(Glib::wrap(widget), event);
310 Glib::exception_handlers_invoke();
316 /**** Gtk::Main -- static data *********************************************/
318 RunSig Main::signal_run_;
319 QuitSig Main::signal_quit_;
320 KeySnooperSig Main::signal_key_snooper_;
321 Main* Main::instance_ = 0;
324 /**** Gtk::Main -- construction/destruction ********************************/
326 Main::Main(int& argc, char**& argv, bool set_locale)
328 init(&argc, &argv, set_locale);
331 Main::Main(int* argc, char*** argv, bool set_locale)
333 init(argc, argv, set_locale);
336 /*Main::Main(int* argc, char*** argv, const std::string& parameter_string, const Glib::ArrayHandle<const Glib::OptionEntry&>& entries, const std::string& translation_domain)
338 init(argc, argv, parameter_string, entries, translation_domain);
341 // Default ctor only used by subclasses:
346 init_gtkmm_internals();
351 g_warning("Gtk::Main instantiated twice");
357 // A second Gtk::Main will produce a warning, but
358 // Main::~Main would still run. So this prevents the crash.
359 if(instance_ == this)
362 GtkMainConnectionNode::list_notify_all();
369 void Main::init(int* argc, char*** argv, bool set_locale)
373 g_warning("Gtk::Main::init() called twice");
378 gtk_disable_setlocale();
380 //TODO: Add support for gtk_init_check().
381 gtk_init(argc, argv);
383 init_gtkmm_internals();
388 Main::Main(int& argc, char**& argv, Glib::OptionContext& option_context)
392 g_warning("Gtk::Main::init() called twice");
396 init_gtkmm_internals();
399 //This reimplements some stuff from gtk_init_with_options(),
400 //without calling check_setugid(), because that is not public API.
402 add_gtk_option_group(option_context);
403 option_context.parse(argc, argv);
407 // This is a static method so that it can be used before Main is instantiated,
408 // for instance in Gnome::canvas_init(). But if you use this method, you
409 // _must_ have a Gtk::Main, so that Main::~Main() is called to clean this up
410 // later. Of course I can't imagine any situation in which you wouldn't have
413 void Main::init_gtkmm_internals()
415 static bool init_done = false;
421 // Populate the map of GTypes to C++ wrap_new() functions.
431 void Main::add_gtk_option_group(Glib::OptionContext& option_context, bool open_default_display)
433 //Get the option group:
434 Glib::OptionGroup gtkgroup( gtk_get_option_group(open_default_display) ); //Takes ownership of the GOptionGroup.
436 //Give it to the option_context, which will also then own the underlying GOptionGroup, deleting it when necessary:
437 option_context.add_group(gtkgroup);
441 /**** Gtk::Main -- static forwarder methods ********************************/
443 Main* Main::instance()
450 instance_->run_impl();
453 void Main::run(Gtk::Window& window)
456 window.signal_hide().connect(sigc::mem_fun(*instance_, &Main::on_window_hide));
457 instance_->run_impl();
462 instance_->quit_impl();
467 return instance_->level_impl();
470 bool Main::iteration(bool blocking)
472 return instance_->iteration_impl(blocking);
475 bool Main::events_pending()
477 return instance_->events_pending_impl();
481 /**** Gtk::Main -- static signal accessors *********************************/
483 RunSig& Main::signal_run()
488 QuitSig& Main::signal_quit()
493 KeySnooperSig& Main::signal_key_snooper()
495 return signal_key_snooper_;
499 /**** Gtk::Main -- protected virtuals **************************************/
501 void Main::run_impl()
506 void Main::quit_impl()
511 guint Main::level_impl()
513 return gtk_main_level();
516 bool Main::iteration_impl(bool blocking)
518 return gtk_main_iteration_do(blocking);
521 bool Main::events_pending_impl()
523 return gtk_events_pending();
526 void Main::on_window_hide()
531 } /* namespace Gtk */
536 } // anonymous namespace