| 1 |
/* Copyright (C) 1998-2002 The gtkmm Development Team |
| 2 |
* |
| 3 |
* This library is free software; you can redistribute it and/or |
| 4 |
* modify it under the terms of the GNU Lesser General Public |
| 5 |
* License as published by the Free Software Foundation; either |
| 6 |
* version 2.1 of the License, or (at your option) any later version. |
| 7 |
* |
| 8 |
* This library is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
* Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public |
| 14 |
* License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <glib.h> |
| 18 |
#include <gtkmmconfig.h> |
| 19 |
#include <gtkmm/main.h> |
| 20 |
#include <gtkmm/window.h> |
| 21 |
#include <glibmm/init.h> |
| 22 |
#include <giomm/init.h> |
| 23 |
#include <pangomm/wrap_init.h> |
| 24 |
#ifdef GTKMM_ATKMM_ENABLED |
| 25 |
#include <atkmm/wrap_init.h> |
| 26 |
#endif //GTKMM_ATKMM_ENABLED |
| 27 |
#include <gdkmm/wrap_init.h> |
| 28 |
#include <gtkmm/wrap_init.h> |
| 29 |
|
| 30 |
_DEPRECATE_IFDEF_START |
| 31 |
|
| 32 |
namespace |
| 33 |
{ |
| 34 |
|
| 35 |
/* This class tells sigc++ how to break GTK+ main signal connections. Since |
| 36 |
* the gtk_*_remove() functions have the same signature, all main signals can |
| 37 |
* be handled by a single class. Special handling is needed for signals that |
| 38 |
* don't support destroy notification; see the comment on connection_list_. |
| 39 |
* |
| 40 |
* This class is now used only by the deprecated key snooper API. The class |
| 41 |
* contains some code and data which is now unnecessary, after other code has |
| 42 |
* been removed. Both the key snooper API and the GtkMainConnectionNode class |
| 43 |
* can be removed in gtk+-4/gtkmm-4. |
| 44 |
*/ |
| 45 |
class GtkMainConnectionNode |
| 46 |
{ |
| 47 |
public: |
| 48 |
// A function taking a connection id, e.g. gtk_timeout_remove(). |
| 49 |
typedef void (*RemoveFunc) (guint); |
| 50 |
|
| 51 |
explicit GtkMainConnectionNode(const sigc::slot_base& slot); |
| 52 |
|
| 53 |
static void* notify(void* data); |
| 54 |
|
| 55 |
// Call this after installing the GTK+ callback. |
| 56 |
void install(guint conn_id, RemoveFunc remove_func, bool has_destroy_notify); |
| 57 |
|
| 58 |
inline sigc::slot_base* get_slot(); |
| 59 |
|
| 60 |
static bool list_remove(GtkMainConnectionNode* conn_node); |
| 61 |
static void list_notify_all(); |
| 62 |
|
| 63 |
private: |
| 64 |
sigc::slot_base slot_; |
| 65 |
static GSList* connection_list_; |
| 66 |
|
| 67 |
guint conn_id_; |
| 68 |
RemoveFunc remove_func_; |
| 69 |
bool has_destroy_notify_; |
| 70 |
bool destroyed_; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 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(). |
| 77 |
* |
| 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, |
| 81 |
* for instance. |
| 82 |
*/ |
| 83 |
// static |
| 84 |
GSList* GtkMainConnectionNode::connection_list_ = nullptr; |
| 85 |
|
| 86 |
GtkMainConnectionNode::GtkMainConnectionNode(const sigc::slot_base& slot) |
| 87 |
: |
| 88 |
slot_(slot), |
| 89 |
conn_id_ (0), |
| 90 |
remove_func_ (nullptr), |
| 91 |
has_destroy_notify_ (false), |
| 92 |
destroyed_ (false) |
| 93 |
{ |
| 94 |
slot_.set_parent(this, &GtkMainConnectionNode::notify); |
| 95 |
} |
| 96 |
|
| 97 |
//static: |
| 98 |
void* GtkMainConnectionNode::notify(void* data) |
| 99 |
{ |
| 100 |
// notification from the sigc++ side ... |
| 101 |
|
| 102 |
const auto self = static_cast<GtkMainConnectionNode*>(data); |
| 103 |
|
| 104 |
// this call might be triggered from destroy_notify_handler(). |
| 105 |
if(!self->destroyed_) |
| 106 |
{ |
| 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; |
| 110 |
|
| 111 |
// disconnect from the gtk+ side. |
| 112 |
(*(self->remove_func_))(self->conn_id_); |
| 113 |
|
| 114 |
// remove self from global list. |
| 115 |
if (!self->has_destroy_notify_) |
| 116 |
list_remove(self); |
| 117 |
|
| 118 |
// destruction of slot_ notifies all objects referring to it. |
| 119 |
delete self; |
| 120 |
} |
| 121 |
|
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
|
| 125 |
void GtkMainConnectionNode::install( |
| 126 |
guint conn_id, GtkMainConnectionNode::RemoveFunc remove_func, bool has_destroy_notify) |
| 127 |
{ |
| 128 |
conn_id_ = conn_id; |
| 129 |
remove_func_ = remove_func; |
| 130 |
has_destroy_notify_ = has_destroy_notify; |
| 131 |
|
| 132 |
if (!has_destroy_notify_) |
| 133 |
connection_list_ = g_slist_prepend(connection_list_, this); |
| 134 |
} |
| 135 |
|
| 136 |
inline |
| 137 |
sigc::slot_base* GtkMainConnectionNode::get_slot() |
| 138 |
{ |
| 139 |
return &slot_; |
| 140 |
} |
| 141 |
|
| 142 |
// static |
| 143 |
bool GtkMainConnectionNode::list_remove(GtkMainConnectionNode* conn_node) |
| 144 |
{ |
| 145 |
// The conn_node pointer is only valid if we still hold |
| 146 |
// a reference of the ConnectionNode in our global list. |
| 147 |
// |
| 148 |
if(GSList *const link = g_slist_find(connection_list_, conn_node)) |
| 149 |
{ |
| 150 |
connection_list_ = g_slist_delete_link(connection_list_, link); |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
/* Cleanup function to be called by the Gtk::Main destructor. The elements |
| 158 |
* are removed prior to notification, in order to avoid invalid elements in |
| 159 |
* the container. |
| 160 |
*/ |
| 161 |
// static |
| 162 |
void GtkMainConnectionNode::list_notify_all() |
| 163 |
{ |
| 164 |
while(connection_list_ != nullptr) |
| 165 |
{ |
| 166 |
GtkMainConnectionNode *const conn_node = |
| 167 |
static_cast<GtkMainConnectionNode*>(connection_list_->data); |
| 168 |
|
| 169 |
connection_list_ = g_slist_delete_link(connection_list_, connection_list_); |
| 170 |
|
| 171 |
// no need to search the list in notify(). |
| 172 |
conn_node->has_destroy_notify_ = true; |
| 173 |
|
| 174 |
// conn_node gets destroyed from notify(). |
| 175 |
notify(conn_node); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
} // anonymous namespace |
| 180 |
|
| 181 |
_DEPRECATE_IFDEF_END |
| 182 |
|
| 183 |
|
| 184 |
namespace Gtk |
| 185 |
{ |
| 186 |
|
| 187 |
_DEPRECATE_IFDEF_START |
| 188 |
|
| 189 |
/**** Gtk::KeySnooperSig ***************************************************/ |
| 190 |
|
| 191 |
sigc::connection KeySnooperSig::connect(const KeySnooperSig::SlotType& slot) |
| 192 |
{ |
| 193 |
const auto conn_node = new GtkMainConnectionNode(slot); |
| 194 |
const sigc::connection connection(*conn_node->get_slot()); |
| 195 |
|
| 196 |
const guint conn_id = gtk_key_snooper_install(&KeySnooperSig::gtk_callback, conn_node); |
| 197 |
|
| 198 |
conn_node->install(conn_id, >k_key_snooper_remove, false); |
| 199 |
return connection; |
| 200 |
} |
| 201 |
|
| 202 |
// static |
| 203 |
gint KeySnooperSig::gtk_callback(GtkWidget* widget, GdkEventKey* event, gpointer data) |
| 204 |
{ |
| 205 |
try |
| 206 |
{ |
| 207 |
// Recreate the specific SlotType from the generic slot node. |
| 208 |
auto conn_node = static_cast<GtkMainConnectionNode*>(data); |
| 209 |
auto pSlot = static_cast<SlotType*>(conn_node->get_slot()); |
| 210 |
return (*pSlot)(Glib::wrap(widget), event); |
| 211 |
} |
| 212 |
catch(...) |
| 213 |
{ |
| 214 |
Glib::exception_handlers_invoke(); |
| 215 |
} |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
/**** Gtk::Main -- static data *********************************************/ |
| 221 |
|
| 222 |
KeySnooperSig Main::signal_key_snooper_; |
| 223 |
|
| 224 |
Main* Main::instance_ = nullptr; |
| 225 |
|
| 226 |
/**** Gtk::Main -- construction/destruction ********************************/ |
| 227 |
|
| 228 |
Main::Main(int& argc, char**& argv, bool set_locale) |
| 229 |
{ |
| 230 |
init(&argc, &argv, set_locale); |
| 231 |
} |
| 232 |
|
| 233 |
Main::Main(int* argc, char*** argv, bool set_locale) |
| 234 |
{ |
| 235 |
init(argc, argv, set_locale); |
| 236 |
} |
| 237 |
|
| 238 |
/*Main::Main(int* argc, char*** argv, const std::string& parameter_string, const std::vector<const Glib::OptionEntry&>& entries, const std::string& translation_domain) |
| 239 |
{ |
| 240 |
init(argc, argv, parameter_string, entries, translation_domain); |
| 241 |
}*/ |
| 242 |
|
| 243 |
Main::Main(bool set_locale) |
| 244 |
{ |
| 245 |
init(nullptr, nullptr, set_locale); |
| 246 |
} |
| 247 |
|
| 248 |
Main::~Main() |
| 249 |
{ |
| 250 |
// A second Gtk::Main will produce a warning, but |
| 251 |
// Main::~Main would still run. So this prevents the crash. |
| 252 |
if(instance_ == this) |
| 253 |
{ |
| 254 |
instance_ = nullptr; |
| 255 |
GtkMainConnectionNode::list_notify_all(); |
| 256 |
|
| 257 |
// Release the gtkmm type registration tables, |
| 258 |
// allowing Main to be instantiated again: |
| 259 |
Glib::wrap_register_cleanup(); |
| 260 |
Glib::Error::register_cleanup(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
// protected |
| 265 |
void Main::init(int* argc, char*** argv, bool set_locale) |
| 266 |
{ |
| 267 |
if(instance_) |
| 268 |
{ |
| 269 |
g_warning("Gtk::Main::init() called twice"); |
| 270 |
} |
| 271 |
else |
| 272 |
{ |
| 273 |
if(!set_locale) |
| 274 |
gtk_disable_setlocale(); |
| 275 |
|
| 276 |
//TODO: Add support for gtk_init_check(). |
| 277 |
gtk_init(argc, argv); |
| 278 |
|
| 279 |
init_gtkmm_internals(); |
| 280 |
instance_ = this; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
Main::Main(int& argc, char**& argv, Glib::OptionContext& option_context) |
| 285 |
{ |
| 286 |
if(instance_) |
| 287 |
{ |
| 288 |
g_warning("Gtk::Main::init() called twice"); |
| 289 |
} |
| 290 |
else |
| 291 |
{ |
| 292 |
init_gtkmm_internals(); |
| 293 |
instance_ = this; |
| 294 |
|
| 295 |
//This reimplements some stuff from gtk_init_with_options(), |
| 296 |
//without calling check_setugid(), because that is not public API. |
| 297 |
|
| 298 |
add_gtk_option_group(option_context); |
| 299 |
|
| 300 |
option_context.parse(argc, argv); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
_DEPRECATE_IFDEF_END |
| 305 |
|
| 306 |
// This is a static method so that it can be used before Main is instantiated, |
| 307 |
// for instance in Gnome::canvas_init(). But if you use this method, you |
| 308 |
// _must_ have a Gtk::Main, so that Main::~Main() is called to clean this up |
| 309 |
// later. Of course I can't imagine any situation in which you wouldn't have |
| 310 |
// a Gtk::Main. |
| 311 |
// |
| 312 |
void Main::init_gtkmm_internals() |
| 313 |
{ |
| 314 |
static bool init_done = false; |
| 315 |
|
| 316 |
if(!init_done) |
| 317 |
{ |
| 318 |
Glib::init(); |
| 319 |
Gio::init(); |
| 320 |
|
| 321 |
// Populate the map of GTypes to C++ wrap_new() functions. |
| 322 |
Pango::wrap_init(); |
| 323 |
#ifdef GTKMM_ATKMM_ENABLED |
| 324 |
Atk::wrap_init(); |
| 325 |
#endif //GTKMM_ATKMM_ENABLED |
| 326 |
Gdk::wrap_init(); |
| 327 |
Gtk::wrap_init(); |
| 328 |
|
| 329 |
init_done = true; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
void Main::add_gtk_option_group(Glib::OptionContext& option_context, bool open_default_display) |
| 334 |
{ |
| 335 |
//Get the option group: |
| 336 |
Glib::OptionGroup gtkgroup( gtk_get_option_group(open_default_display) ); //Takes ownership of the GOptionGroup. |
| 337 |
|
| 338 |
//Give it to the option_context, which will also then own the underlying GOptionGroup, deleting it when necessary: |
| 339 |
option_context.add_group(gtkgroup); |
| 340 |
} |
| 341 |
|
| 342 |
_DEPRECATE_IFDEF_START |
| 343 |
|
| 344 |
/**** Gtk::Main -- static forwarder methods ********************************/ |
| 345 |
|
| 346 |
Main* Main::instance() |
| 347 |
{ |
| 348 |
return instance_; |
| 349 |
} |
| 350 |
|
| 351 |
void Main::run() |
| 352 |
{ |
| 353 |
instance_->run_impl(); |
| 354 |
} |
| 355 |
|
| 356 |
void Main::run(Gtk::Window& window) |
| 357 |
{ |
| 358 |
window.show(); |
| 359 |
window.signal_hide().connect(sigc::mem_fun(*instance_, &Main::on_window_hide)); |
| 360 |
instance_->run_impl(); |
| 361 |
} |
| 362 |
|
| 363 |
void Main::quit() |
| 364 |
{ |
| 365 |
instance_->quit_impl(); |
| 366 |
} |
| 367 |
|
| 368 |
guint Main::level() |
| 369 |
{ |
| 370 |
return instance_->level_impl(); |
| 371 |
} |
| 372 |
|
| 373 |
_DEPRECATE_IFDEF_END |
| 374 |
|
| 375 |
bool Main::iteration(bool blocking) |
| 376 |
{ |
| 377 |
return gtk_main_iteration_do(blocking); |
| 378 |
} |
| 379 |
|
| 380 |
bool Main::events_pending() |
| 381 |
{ |
| 382 |
return gtk_events_pending(); |
| 383 |
} |
| 384 |
|
| 385 |
_DEPRECATE_IFDEF_START |
| 386 |
|
| 387 |
/**** Gtk::Main -- static signal accessors *********************************/ |
| 388 |
|
| 389 |
KeySnooperSig& Main::signal_key_snooper() |
| 390 |
{ |
| 391 |
return signal_key_snooper_; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
/**** Gtk::Main -- protected virtuals **************************************/ |
| 396 |
|
| 397 |
void Main::run_impl() |
| 398 |
{ |
| 399 |
gtk_main(); |
| 400 |
} |
| 401 |
|
| 402 |
void Main::quit_impl() |
| 403 |
{ |
| 404 |
gtk_main_quit(); |
| 405 |
} |
| 406 |
|
| 407 |
guint Main::level_impl() |
| 408 |
{ |
| 409 |
return gtk_main_level(); |
| 410 |
} |
| 411 |
|
| 412 |
bool Main::iteration_impl(bool blocking) |
| 413 |
{ |
| 414 |
return gtk_main_iteration_do(blocking); |
| 415 |
} |
| 416 |
|
| 417 |
bool Main::events_pending_impl() |
| 418 |
{ |
| 419 |
return gtk_events_pending(); |
| 420 |
} |
| 421 |
|
| 422 |
void Main::on_window_hide() |
| 423 |
{ |
| 424 |
quit_impl(); |
| 425 |
} |
| 426 |
|
| 427 |
_DEPRECATE_IFDEF_END |
| 428 |
|
| 429 |
} //namespace Gtk |