1 // Generated by gtkmmproc -- DO NOT MODIFY!
4 #include <gtkmm/object.h>
5 #include <gtkmm/private/object_p.h>
9 /* Copyright 1998-2002 The gtkmm Development Team
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the Free
23 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <glibmm/quark.h>
27 #include <gtk/gtkobject.h>
33 Object::Object(const Glib::ConstructParams& construct_params)
35 Glib::Object(construct_params)
37 gobject_disposed_ = false;
39 _init_unmanage(); //We don't like the GTK+ default memory management - we want to be in control._)
42 Object::Object(GtkObject* castitem)
44 Glib::Object((GObject*) castitem)
46 gobject_disposed_ = false;
48 _init_unmanage(); //We don't like the GTK+ default memory management - we want to be in control.
51 void Object::_init_unmanage(bool /* is_toplevel = false */)
57 //Glib::Object::Object has already stored a pointer to this C++ instance in the underlying C instance,
58 //and connected a callback which will, in turn, call our destroy_notify_(),
59 //so will will know if GTK+ disposes of the underlying instance.
61 // Most GTK+ objects are floating, by default. This means that the container widget controls their lifetime.
63 if(g_object_is_floating (gobject_)) //Top-level Windows and Dialogs can not be manag()ed, so there is no need to do this.
65 GLIBMM_DEBUG_REFERENCE(this, gobject_);
66 g_object_ref_sink(gobject_); //Stops it from being floating - we will make this optional ( see Gtk::manage() ),
68 #ifdef GLIBMM_DEBUG_REFCOUNTING
69 g_warning("gtkmm after sink: C++ instance: %p, C instance: %p, refcount=%d\n", (void*)(Glib::ObjectBase*)this, (void*)gobject_, G_OBJECT(gobject_)->ref_count);
70 g_warning(" c instance gtype: %s\n", G_OBJECT_TYPE_NAME(gobject_));
73 referenced_ = true; //Not managed.
77 //This widget is already not floating. It's probably already been added to a GTK+ container, and has just had Glib::wrap() called on it.
78 //It's not floating because containers call g_object_sink() on child widgets to take control of them.
79 //We just ref() it so that we can unref it later.
80 //GLIBMM_DEBUG_REFERENCE(this, gobject_);
81 //g_object_ref(gobject_);
83 //Alternatively, it might be a top-level window (e.g. a Dialog). We would then be doing one too-many refs(),
84 //We do an extra unref() in Window::_destroy_c_instance() to take care of that.
86 referenced_ = false; //Managed. We should not try to unfloat GtkObjects that we did not instantiate.
91 void Object::_destroy_c_instance()
93 #ifdef GLIBMM_DEBUG_REFCOUNTING
94 g_warning("Gtk::Object::_destroy_c_instance() this=%10X, gobject_=%10X\n", this, gobject_);
96 g_warning(" gtypename: %s\n", G_OBJECT_TYPE_NAME(gobject_));
99 cpp_destruction_in_progress_ = true;
102 GtkObject* object = gobj();
106 g_assert(GTK_IS_OBJECT(object));
108 disconnect_cpp_wrapper();
109 //Unfortunately this means that our dispose callback will not be called, because the qdata has been removed.
110 //So we'll connect the callback again, just so that gobject_disposed_ gets set for use later in this same method.
114 //This probably isn't a problem now:
115 //If we are killing the C++ instance before the C instance, then this might lead to strange behaviour.
116 //If this is a problem, then you'll have to use a managed() object, which will die only upon GTK+'s request.
118 //We can't do anything with the gobject_ if it's already been disposed.
119 //This prevents us from unref-ing it again, or destroying it again after GTK+ has told us that it has been disposed.
120 if (!gobject_disposed_)
124 //It's not manage()ed so we just unref to destroy it
125 #ifdef GLIBMM_DEBUG_REFCOUNTING
126 g_warning("final unref: gtypename: %s, refcount: %d\n", G_OBJECT_TYPE_NAME(object), ((GObject*)object)->ref_count);
129 //Because we called disconnect_cpp_wrapper() our dispose callback will not be called, because the qdata has been removed.
130 //So we'll connect a callback again, just so that gobject_disposed_ gets set for use later in this same method.
131 gulong connection_id_destroy = g_signal_connect (object, "destroy", G_CALLBACK (&callback_destroy_), this);
133 GLIBMM_DEBUG_UNREFERENCE(this, object);
134 g_object_unref(object);
136 if(!gobject_disposed_) //or if(g_signal_handler_is_connected(object, connection_id_destroy))
137 g_signal_handler_disconnect(object, connection_id_destroy);
139 //destroy_notify() should have been called after the final g_object_unref() or gtk_object_destroy(), so gobject_disposed_ should now be true.
141 //If the C instance still isn't dead then insist, by calling gtk_object_destroy().
142 //This is necessary because even a manage()d widget is refed when added to a container.
143 // <danielk> That's simply not true. But references might be taken elsewhere,
144 // and gtk_object_destroy() just tells everyone "drop your refs, please!".
145 if (!gobject_disposed_)
147 #ifdef GLIBMM_DEBUG_REFCOUNTING
148 g_warning("Gtk::Object::_destroy_c_instance(): Calling gtk_object_destroy(): gobject_=%10X, gtypename=%s\n", object, G_OBJECT_TYPE_NAME(object));
151 g_assert(GTK_IS_OBJECT(object));
152 gtk_object_destroy(object); //Container widgets can respond to this.
157 //It's manag()ed, but the coder decided to delete it before deleting its parent.
158 //That should be OK because the Container can respond to that.
159 #ifdef GLIBMM_DEBUG_REFCOUNTING
160 g_warning("Gtk::Object::_destroy_c_instance(): Calling gtk_object_destroy(): gobject_=%10X\n", gobject_);
163 if (!gobject_disposed_)
165 g_assert(GTK_IS_OBJECT(object));
166 gtk_object_destroy(object);
171 //Glib::Object::~Object() will not g_object_unref() it too. because gobject_ is now 0.
177 #ifdef GLIBMM_DEBUG_REFCOUNTING
178 g_warning("Gtk::Object::~Object() gobject_=%10X\n", gobject_);
181 //This has probably been called already from Gtk::Object::_destroy(), which is called from derived destructors.
182 _destroy_c_instance();
185 void Object::disconnect_cpp_wrapper()
189 #ifdef GLIBMM_DEBUG_REFCOUNTING
190 g_warning("Gtk::Object::disconnect_cpp_wrapper() this=%10X, gobject_=%10X\n", this, gobject_);
192 g_warning(" gtypename: %s\n", G_OBJECT_TYPE_NAME(gobject_));
197 //Prevent gtk vfuncs and default signal handlers from calling our instance methods:
198 g_object_steal_qdata((GObject*)gobj(), Glib::quark_); //It will no longer be possible to get the C++ instance from the C instance.
200 //Allow us to prevent generation of a new C++ wrapper during destruction:
201 g_object_set_qdata((GObject*)gobj(), Glib::quark_cpp_wrapper_deleted_, (gpointer)true);
203 //Prevent C++ instance from using GTK+ object:
206 //TODO: Disconnect any signals, using gtk methods.
207 //We'll have to keep a record of all the connections.
211 void Object::destroy_notify_()
216 #ifdef GLIBMM_DEBUG_REFCOUNTING
217 g_warning("Gtk::Object::destroy_notify_: this=%10X, gobject_=%10X\n", this, gobject_);
219 g_warning(" gtypename=%s\n", G_OBJECT_TYPE_NAME(gobject_));
222 //Remember that it's been disposed (which only happens once):
223 //This also stops us from destroying it again in the destructor when it calls destroy_().
224 gobject_disposed_ = true;
226 if(!cpp_destruction_in_progress_) //This function might have been called as a side-effect of destroy() when it called gtk_object_destroy().
228 if (!referenced_) //If it's manage()ed.
230 #ifdef GLIBMM_DEBUG_REFCOUNTING
231 g_warning("Gtk::Object::destroy_notify_: before delete this.\n");
233 delete this; //Free the C++ instance.
235 else //It's not managed, but the C gobject_ just died before the C++ instance..
237 #ifdef GLIBMM_DEBUG_REFCOUNTING
238 g_warning("Gtk::Object::destroy_notify_: setting gobject_ to 0\n");
246 void Object::destroy_()
248 //Called from destructors.
251 #ifdef GLIBMM_DEBUG_REFCOUNTING
252 g_warning("Gtk::Object::destroy_(): gobject_: %10X\n", gobject_);
254 g_warning(" gtypename: %s\n", G_OBJECT_TYPE_NAME(gobject_));
257 if ( !cpp_destruction_in_progress_ ) //see comment below.
259 //Prevent destroy_notify_() from running as a possible side-effect of gtk_object_destroy.
260 //We can't predict whether destroy_notify_() will really be run, so we'll disconnect the C++ instance here.
261 cpp_destruction_in_progress_ = true;
263 //destroy the C instance:
264 _destroy_c_instance();
267 //The C++ destructor will be reached later. This function was called by a destructor.
270 void Object::set_manage()
273 //This object will not be unref()ed by gtkmm, though it could be destroyed if the coder deletes the C++ instance early.
274 //This method is overriden in Gtk::Window because they can not be manage()ed.
276 if (!referenced_) return; //It's already managed.
278 // remove our reference
279 if (gobject_->ref_count >= 1) //This should only happen just after creation. We don't use "==1" because GtkButton starts with a refcount of 2 when using a mnemonic.
281 //g_warning("Object::set_manage(), making object floating: %s\n", G_OBJECT_TYPE_NAME(gobject_));
283 // Cowardly refuse to remove last reference make floating instead. //TODO: What does that comment mean?
284 #ifdef GLIBMM_DEBUG_REFCOUNTING
285 g_warning("Object::set_manage(): setting GTK_FLOATING: gobject_ = %p", (void*) gobj());
286 g_warning(" gtypename=%s\n", G_OBJECT_TYPE_NAME(gobj()));
288 //deprecated: GTK_OBJECT_SET_FLAGS(gobj(), GTK_FLOATING);
289 g_object_force_floating(gobject_);
293 g_warning("Object::set_manage(). Refcount seems to be 0. %s\n", G_OBJECT_TYPE_NAME(gobject_));
295 //DEF_GLIBMM_DEBUG_UNREF(this, gobj())
296 //g_object_unref(gobj());
299 //g_warning("Object::set_manage(): end: %s", G_OBJECT_TYPE_NAME(gobject_));
300 //g_warning(" refcount=%d", G_OBJECT(gobj())->ref_count);
305 void Object::callback_destroy_(GObject*, void* data) //static
307 //This is only used for a short time, then disconnected.
309 Object* cppObject = static_cast<Object*>(data);
310 if(cppObject) //This will be 0 if the C++ destructor has already run.
312 cppObject->gobject_disposed_ = true;
316 bool Object::is_managed_() const
326 } // anonymous namespace
332 Gtk::Object* wrap(GtkObject* object, bool take_copy)
334 return dynamic_cast<Gtk::Object *> (Glib::wrap_auto ((GObject*)(object), take_copy));
337 } /* namespace Glib */
343 /* The *_Class implementation: */
345 const Glib::Class& Object_Class::init()
347 if(!gtype_) // create the GType if necessary
349 // Glib::Class has to know the class init function to clone custom types.
350 class_init_func_ = &Object_Class::class_init_function;
352 // This is actually just optimized away, apparently with no harm.
353 // Make sure that the parent type has been created.
354 //CppClassParent::CppObjectType::get_type();
356 // Create the wrapper type, with the same class/instance size as the base type.
357 register_derived_type(gtk_object_get_type());
359 // Add derived versions of interfaces, if the C type implements any interfaces:
365 void Object_Class::class_init_function(void* g_class, void* class_data)
367 BaseClassType *const klass = static_cast<BaseClassType*>(g_class);
368 CppClassParent::class_init_function(klass, class_data);
370 #ifdef GLIBMM_VFUNCS_ENABLED
371 #endif //GLIBMM_VFUNCS_ENABLED
373 #ifdef GLIBMM_DEFAULT_SIGNAL_HANDLERS_ENABLED
374 #endif //GLIBMM_DEFAULT_SIGNAL_HANDLERS_ENABLED
377 #ifdef GLIBMM_VFUNCS_ENABLED
378 #endif //GLIBMM_VFUNCS_ENABLED
380 #ifdef GLIBMM_DEFAULT_SIGNAL_HANDLERS_ENABLED
381 #endif //GLIBMM_DEFAULT_SIGNAL_HANDLERS_ENABLED
384 Glib::ObjectBase* Object_Class::wrap_new(GObject* o)
386 return manage(new Object((GtkObject*)(o)));
391 /* The implementation: */
393 Object::CppClassType Object::object_class_; // initialize static member
395 GType Object::get_type()
397 return object_class_.init().get_type();
400 GType Object::get_base_type()
402 return gtk_object_get_type();
406 #ifdef GLIBMM_PROPERTIES_ENABLED
407 Glib::PropertyProxy<void*> Object::property_user_data()
409 return Glib::PropertyProxy<void*>(this, "user-data");
411 #endif //GLIBMM_PROPERTIES_ENABLED
413 #ifdef GLIBMM_PROPERTIES_ENABLED
414 Glib::PropertyProxy_ReadOnly<void*> Object::property_user_data() const
416 return Glib::PropertyProxy_ReadOnly<void*>(this, "user-data");
418 #endif //GLIBMM_PROPERTIES_ENABLED
421 #ifdef GLIBMM_DEFAULT_SIGNAL_HANDLERS_ENABLED
422 #endif //GLIBMM_DEFAULT_SIGNAL_HANDLERS_ENABLED
424 #ifdef GLIBMM_VFUNCS_ENABLED
425 #endif //GLIBMM_VFUNCS_ENABLED