File: main.cc

package info (click to toggle)
gtkmm3.0 3.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 159,024 kB
  • ctags: 22,555
  • sloc: xml: 107,819; sh: 11,425; cpp: 7,074; makefile: 241; perl: 235
file content (477 lines) | stat: -rw-r--r-- 11,435 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Generated by gmmproc 2.42.0 -- DO NOT MODIFY!

#undef GTK_DISABLE_DEPRECATED
#define GDK_DISABLE_DEPRECATION_WARNINGS 1
 

#include <glibmm.h>

#include <gtkmm/main.h>
#include <gtkmm/private/main_p.h>


/*
 * Copyright 1998-2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <glib.h>
#include <gtkmmconfig.h>
#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <glibmm/init.h>
#include <giomm/init.h>
#include <pangomm/wrap_init.h>
#ifdef GTKMM_ATKMM_ENABLED
#include <atkmm/wrap_init.h>
#endif //GTKMM_ATKMM_ENABLED
#include <gdkmm/wrap_init.h>
#include <gtkmm/wrap_init.h>

#ifndef GTKMM_DISABLE_DEPRECATED


namespace
{

/* This class tells sigc++ how to break GTK+ main signal connections.  Since
 * the gtk_*_remove() functions have the same signature, all main signals can
 * be handled by a single class.  Special handling is needed for signals that
 * don't support destroy notification; see the comment on connection_list_.
 */
class GtkMainConnectionNode
{
public:
  // A function taking a connection id, e.g. gtk_timeout_remove().
  typedef void (*RemoveFunc) (guint);

  explicit GtkMainConnectionNode(const sigc::slot_base& slot);

  static void* notify(void* data);
  static void destroy_notify_handler(void* data);

  // Call this after installing the GTK+ callback.
  void install(guint conn_id, RemoveFunc remove_func, bool has_destroy_notify);

  inline sigc::slot_base* get_slot();

  static bool list_remove(GtkMainConnectionNode* conn_node);
  static void list_notify_all();

private:
  sigc::slot_base slot_;
  static GSList*  connection_list_;

  guint           conn_id_;
  RemoveFunc      remove_func_;
  bool            has_destroy_notify_;
  bool            destroyed_;
};


/* The global connection_list_ is needed to deal with GTK+ main signals
 * that don't support destroy notification.  This applies only to
 * gtk_key_snooper_install() and gtk_init_add().
 *
 * The list is static and not a member of Gtk::Main, in order to support
 * connection to Gtk::Main::signal_run() before Gtk::Main is instantiated.
 * Thus, it's possible to install initialization hooks in global constructors,
 * for instance.
 */
// static
GSList* GtkMainConnectionNode::connection_list_ = 0;

GtkMainConnectionNode::GtkMainConnectionNode(const sigc::slot_base& slot)
:
  slot_(slot),
  conn_id_             (0),
  remove_func_         (0),
  has_destroy_notify_          (false),
  destroyed_           (false)
{
  slot_.set_parent(this, &GtkMainConnectionNode::notify);
}

//static:
void* GtkMainConnectionNode::notify(void* data)
{
  // notification from the sigc++ side ...

  GtkMainConnectionNode *const self = static_cast<GtkMainConnectionNode*>(data);

  // this call might be triggered from destroy_notify_handler().
  if(!self->destroyed_)
  {
    // during (*remove_func_)() destroy_notify_handler() might get called.
    // This must not lead to the destruction of the object!
    self->destroyed_ = true;

    // disconnect from the gtk+ side.
    (*(self->remove_func_))(self->conn_id_);

    // remove self from global list.
    if (!self->has_destroy_notify_)
      list_remove(self);

    // destruction of slot_ notifies all objects referring to it.
    delete self;
  }

  return 0;
}

// static
void GtkMainConnectionNode::destroy_notify_handler(void* data)
{
  // notification from the gtk+ side ...

  GtkMainConnectionNode *const self = static_cast<GtkMainConnectionNode*>(data);

  // this call might be triggered from notify().
  if(!self->destroyed_)
  {
    self->destroyed_ = true;

    // The GTK+ side is disconnected now, thus the ID is no longer valid.
    self->conn_id_ = 0;

    // remove self from global list.
    if (!self->has_destroy_notify_)
      list_remove(self);

    // destruction of slot_ notifies all objects referring to it.
    delete self;
  }
}

void GtkMainConnectionNode::install(
    guint conn_id, GtkMainConnectionNode::RemoveFunc remove_func, bool has_destroy_notify)
{
  conn_id_     = conn_id;
  remove_func_ = remove_func;
  has_destroy_notify_  = has_destroy_notify;

  if (!has_destroy_notify_)
    connection_list_ = g_slist_prepend(connection_list_, this);
}

inline
sigc::slot_base* GtkMainConnectionNode::get_slot()
{
  return &slot_;
}

// static
bool GtkMainConnectionNode::list_remove(GtkMainConnectionNode* conn_node)
{
  // The conn_node pointer is only valid if we still hold
  // a reference of the ConnectionNode in our global list.
  //
  if(GSList *const link = g_slist_find(connection_list_, conn_node))
  {
    connection_list_ = g_slist_delete_link(connection_list_, link);
    return true;
  }

  return false;
}

/* Cleanup function to be called by the Gtk::Main destructor.  The elements
 * are removed prior to notification, in order to avoid invalid elements in
 * the container.
 */
// static
void GtkMainConnectionNode::list_notify_all()
{
  while(connection_list_ != 0)
  {
    GtkMainConnectionNode *const conn_node =
        static_cast<GtkMainConnectionNode*>(connection_list_->data);

    connection_list_ = g_slist_delete_link(connection_list_, connection_list_);

    // no need to search the list in notify().
    conn_node->has_destroy_notify_ = true;

    // conn_node gets destroyed from notify().
    notify(conn_node);
  }
}

} // anonymous namespace

#endif // GTKMM_DISABLE_DEPRECATED


namespace Gtk
{

#ifndef GTKMM_DISABLE_DEPRECATED


/**** Gtk::KeySnooperSig ***************************************************/

sigc::connection KeySnooperSig::connect(const KeySnooperSig::SlotType& slot)
{
  GtkMainConnectionNode *const conn_node = new GtkMainConnectionNode(slot);
  const sigc::connection connection(*conn_node->get_slot());

  const guint conn_id = gtk_key_snooper_install(&KeySnooperSig::gtk_callback, conn_node);

  conn_node->install(conn_id, &gtk_key_snooper_remove, false);
  return connection;
}

// static
gint KeySnooperSig::gtk_callback(GtkWidget* widget, GdkEventKey* event, gpointer data)
{
  try
  {
    // Recreate the specific SlotType from the generic slot node.
    GtkMainConnectionNode* conn_node = static_cast<GtkMainConnectionNode*>(data);
    SlotType* pSlot = static_cast<SlotType*>(conn_node->get_slot());
    return (*pSlot)(Glib::wrap(widget), event);
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }

  return 0;
}

/**** Gtk::Main -- static data *********************************************/

KeySnooperSig Main::signal_key_snooper_;

Main*         Main::instance_ = 0;

/**** Gtk::Main -- construction/destruction ********************************/

Main::Main(int& argc, char**& argv, bool set_locale)
{
  init(&argc, &argv, set_locale);
}

Main::Main(int* argc, char*** argv, bool set_locale)
{
  init(argc, argv, set_locale);
}

/*Main::Main(int* argc, char*** argv, const std::string& parameter_string, const std::vector<const Glib::OptionEntry&>& entries, const std::string& translation_domain)
{
  init(argc, argv, parameter_string, entries, translation_domain);
}*/

Main::Main(bool set_locale)
{
  init(0, 0, set_locale);
}

Main::~Main()
{
  // A second Gtk::Main will produce a warning, but
  // Main::~Main would still run. So this prevents the crash.
  if(instance_ == this)
  {
    instance_ = 0;
    GtkMainConnectionNode::list_notify_all();

    // Release the gtkmm type registration tables,
    // allowing Main to be instantiated again:
    Glib::wrap_register_cleanup();
    Glib::Error::register_cleanup();
  }
}

// protected
void Main::init(int* argc, char*** argv, bool set_locale)
{
  if(instance_)
  {
    g_warning("Gtk::Main::init() called twice");
  }
  else
  {
    if(!set_locale)
      gtk_disable_setlocale();

    //TODO: Add support for gtk_init_check().
    gtk_init(argc, argv);

    init_gtkmm_internals();
    instance_ = this;
  }
}

Main::Main(int& argc, char**& argv, Glib::OptionContext& option_context)
{
  if(instance_)
  {
    g_warning("Gtk::Main::init() called twice");
  }
  else
  {
    init_gtkmm_internals();
    instance_ = this;

    //This reimplements some stuff from gtk_init_with_options(),
    //without calling check_setugid(), because that is not public API.

    add_gtk_option_group(option_context);

    option_context.parse(argc, argv);
  }
}

#endif // GTKMM_DISABLE_DEPRECATED


// This is a static method so that it can be used before Main is instantiated,
// for instance in Gnome::canvas_init().  But if you use this method, you
// _must_ have a Gtk::Main, so that Main::~Main() is called to clean this up
// later.  Of course I can't imagine any situation in which you wouldn't have
// a Gtk::Main.
//
void Main::init_gtkmm_internals()
{
  static bool init_done = false;

  if(!init_done)
  {
    Glib::init();
    Gio::init();

    // Populate the map of GTypes to C++ wrap_new() functions.
    Pango::wrap_init();
#ifdef GTKMM_ATKMM_ENABLED
    Atk::wrap_init();
#endif //GTKMM_ATKMM_ENABLED
    Gdk::wrap_init();
    Gtk::wrap_init();

    init_done = true;
  }
}

void Main::add_gtk_option_group(Glib::OptionContext& option_context, bool open_default_display)
{
  //Get the option group:
  Glib::OptionGroup gtkgroup( gtk_get_option_group(open_default_display) ); //Takes ownership of the GOptionGroup.

  //Give it to the option_context, which will also then own the underlying GOptionGroup, deleting it when necessary:
  option_context.add_group(gtkgroup);
}

#ifndef GTKMM_DISABLE_DEPRECATED


/**** Gtk::Main -- static forwarder methods ********************************/

Main* Main::instance()
{
  return instance_;
}

void Main::run()
{
  instance_->run_impl();
}

void Main::run(Gtk::Window& window)
{
  window.show();
  window.signal_hide().connect(sigc::mem_fun(*instance_, &Main::on_window_hide));
  instance_->run_impl();
}

void Main::quit()
{
  instance_->quit_impl();
}

guint Main::level()
{
  return instance_->level_impl();
}

#endif // GTKMM_DISABLE_DEPRECATED


bool Main::iteration(bool blocking)
{
  return gtk_main_iteration_do(blocking);
}

bool Main::events_pending()
{
  return gtk_events_pending();
}

#ifndef GTKMM_DISABLE_DEPRECATED


/**** Gtk::Main -- static signal accessors *********************************/

KeySnooperSig& Main::signal_key_snooper()
{
  return signal_key_snooper_;
}


/**** Gtk::Main -- protected virtuals **************************************/

void Main::run_impl()
{
  gtk_main();
}

void Main::quit_impl()
{
  gtk_main_quit();
}

guint Main::level_impl()
{
  return gtk_main_level();
}

bool Main::iteration_impl(bool blocking)
{
  return gtk_main_iteration_do(blocking);
}

bool Main::events_pending_impl()
{
  return gtk_events_pending();
}

void Main::on_window_hide()
{
  quit_impl();
}

#endif // GTKMM_DISABLE_DEPRECATED


} //namespace Gtk


namespace
{
} // anonymous namespace