1 /* $Id: object.ccg,v 1.8 2005/12/10 11:54:53 murrayc Exp $ */
3 /* Copyright 1998-2002 The gtkmm Development Team
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <glibmm/quark.h>
21 #include <gtk/gtkobject.h>
27 Object::Object(const Glib::ConstructParams& construct_params)
29 Glib::Object(construct_params)
31 gobject_disposed_ = false;
33 _init_unmanage(); //We don't like the GTK+ default memory management - we want to be in control._)
36 Object::Object(GtkObject* castitem)
38 Glib::Object((GObject*) castitem)
40 gobject_disposed_ = false;
42 _init_unmanage(); //We don't like the GTK+ default memory management - we want to be in control.
45 void Object::_init_unmanage(bool /* is_toplevel = false */)
51 //Glib::Object::Object has already stored a pointer to this C++ instance in the underlying C instance,
52 //and connected a callback which will, in turn, call our destroy_notify_(),
53 //so will will know if GTK+ disposes of the underlying instance.
55 // Most GTK+ objects are floating, by default. This means that the container widget controls their lifetime.
57 if(g_object_is_floating (gobject_)) //Top-level Windows and Dialogs can not be manag()ed, so there is no need to do this.
59 GLIBMM_DEBUG_REFERENCE(this, gobject_);
60 g_object_ref_sink(gobject_); //Stops it from being floating - we will make this optional ( see Gtk::manage() ),
62 #ifdef GLIBMM_DEBUG_REFCOUNTING
63 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);
64 g_warning(" c instance gtype: %s\n", G_OBJECT_TYPE_NAME(gobject_));
67 referenced_ = true; //Not managed.
71 //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.
72 //It's not floating because containers call g_object_sink() on child widgets to take control of them.
73 //We just ref() it so that we can unref it later.
74 //GLIBMM_DEBUG_REFERENCE(this, gobject_);
75 //g_object_ref(gobject_);
77 //Alternatively, it might be a top-level window (e.g. a Dialog). We would then be doing one too-many refs(),
78 //We do an extra unref() in Window::_destroy_c_instance() to take care of that.
80 referenced_ = false; //Managed. We should not try to unfloat GtkObjects that we did not instantiate.
85 void Object::_destroy_c_instance()
87 #ifdef GLIBMM_DEBUG_REFCOUNTING
88 g_warning("Gtk::Object::_destroy_c_instance() this=%10X, gobject_=%10X\n", this, gobject_);
90 g_warning(" gtypename: %s\n", G_OBJECT_TYPE_NAME(gobject_));
93 cpp_destruction_in_progress_ = true;
96 GtkObject* object = gobj();
100 g_assert(GTK_IS_OBJECT(object));
102 disconnect_cpp_wrapper();
103 //Unfortunately this means that our dispose callback will not be called, because the qdata has been removed.
104 //So we'll connect the callback again, just so that gobject_disposed_ gets set for use later in this same method.
108 //This probably isn't a problem now:
109 //If we are killing the C++ instance before the C instance, then this might lead to strange behaviour.
110 //If this is a problem, then you'll have to use a managed() object, which will die only upon GTK+'s request.
112 //We can't do anything with the gobject_ if it's already been disposed.
113 //This prevents us from unref-ing it again, or destroying it again after GTK+ has told us that it has been disposed.
114 if (!gobject_disposed_)
118 //It's not manage()ed so we just unref to destroy it
119 #ifdef GLIBMM_DEBUG_REFCOUNTING
120 g_warning("final unref: gtypename: %s, refcount: %d\n", G_OBJECT_TYPE_NAME(object), ((GObject*)object)->ref_count);
123 //Because we called disconnect_cpp_wrapper() our dispose callback will not be called, because the qdata has been removed.
124 //So we'll connect a callback again, just so that gobject_disposed_ gets set for use later in this same method.
125 gulong connection_id_destroy = g_signal_connect (object, "destroy", G_CALLBACK (&callback_destroy_), this);
127 GLIBMM_DEBUG_UNREFERENCE(this, object);
128 g_object_unref(object);
130 if(!gobject_disposed_) //or if(g_signal_handler_is_connected(object, connection_id_destroy))
131 g_signal_handler_disconnect(object, connection_id_destroy);
133 //destroy_notify() should have been called after the final g_object_unref() or gtk_object_destroy(), so gobject_disposed_ should now be true.
135 //If the C instance still isn't dead then insist, by calling gtk_object_destroy().
136 //This is necessary because even a manage()d widget is refed when added to a container.
137 // <danielk> That's simply not true. But references might be taken elsewhere,
138 // and gtk_object_destroy() just tells everyone "drop your refs, please!".
139 if (!gobject_disposed_)
141 #ifdef GLIBMM_DEBUG_REFCOUNTING
142 g_warning("Gtk::Object::_destroy_c_instance(): Calling gtk_object_destroy(): gobject_=%10X, gtypename=%s\n", object, G_OBJECT_TYPE_NAME(object));
145 g_assert(GTK_IS_OBJECT(object));
146 gtk_object_destroy(object); //Container widgets can respond to this.
151 //It's manag()ed, but the coder decided to delete it before deleting its parent.
152 //That should be OK because the Container can respond to that.
153 #ifdef GLIBMM_DEBUG_REFCOUNTING
154 g_warning("Gtk::Object::_destroy_c_instance(): Calling gtk_object_destroy(): gobject_=%10X\n", gobject_);
157 if (!gobject_disposed_)
159 g_assert(GTK_IS_OBJECT(object));
160 gtk_object_destroy(object);
165 //Glib::Object::~Object() will not g_object_unref() it too. because gobject_ is now 0.
171 #ifdef GLIBMM_DEBUG_REFCOUNTING
172 g_warning("Gtk::Object::~Object() gobject_=%10X\n", gobject_);
175 //This has probably been called already from Gtk::Object::_destroy(), which is called from derived destructors.
176 _destroy_c_instance();
179 void Object::disconnect_cpp_wrapper()
183 #ifdef GLIBMM_DEBUG_REFCOUNTING
184 g_warning("Gtk::Object::disconnect_cpp_wrapper() this=%10X, gobject_=%10X\n", this, gobject_);
186 g_warning(" gtypename: %s\n", G_OBJECT_TYPE_NAME(gobject_));
191 //Prevent gtk vfuncs and default signal handlers from calling our instance methods:
192 g_object_steal_qdata((GObject*)gobj(), Glib::quark_); //It will no longer be possible to get the C++ instance from the C instance.
194 //Allow us to prevent generation of a new C++ wrapper during destruction:
195 g_object_set_qdata((GObject*)gobj(), Glib::quark_cpp_wrapper_deleted_, (gpointer)true);
197 //Prevent C++ instance from using GTK+ object:
200 //TODO: Disconnect any signals, using gtk methods.
201 //We'll have to keep a record of all the connections.
205 void Object::destroy_notify_()
210 #ifdef GLIBMM_DEBUG_REFCOUNTING
211 g_warning("Gtk::Object::destroy_notify_: this=%10X, gobject_=%10X\n", this, gobject_);
213 g_warning(" gtypename=%s\n", G_OBJECT_TYPE_NAME(gobject_));
216 //Remember that it's been disposed (which only happens once):
217 //This also stops us from destroying it again in the destructor when it calls destroy_().
218 gobject_disposed_ = true;
220 if(!cpp_destruction_in_progress_) //This function might have been called as a side-effect of destroy() when it called gtk_object_destroy().
222 if (!referenced_) //If it's manage()ed.
224 #ifdef GLIBMM_DEBUG_REFCOUNTING
225 g_warning("Gtk::Object::destroy_notify_: before delete this.\n");
227 delete this; //Free the C++ instance.
229 else //It's not managed, but the C gobject_ just died before the C++ instance..
231 #ifdef GLIBMM_DEBUG_REFCOUNTING
232 g_warning("Gtk::Object::destroy_notify_: setting gobject_ to 0\n");
240 void Object::destroy_()
242 //Called from destructors.
245 #ifdef GLIBMM_DEBUG_REFCOUNTING
246 g_warning("Gtk::Object::destroy_(): gobject_: %10X\n", gobject_);
248 g_warning(" gtypename: %s\n", G_OBJECT_TYPE_NAME(gobject_));
251 if ( !cpp_destruction_in_progress_ ) //see comment below.
253 //Prevent destroy_notify_() from running as a possible side-effect of gtk_object_destroy.
254 //We can't predict whether destroy_notify_() will really be run, so we'll disconnect the C++ instance here.
255 cpp_destruction_in_progress_ = true;
257 //destroy the C instance:
258 _destroy_c_instance();
261 //The C++ destructor will be reached later. This function was called by a destructor.
264 void Object::set_manage()
267 //This object will not be unref()ed by gtkmm, though it could be destroyed if the coder deletes the C++ instance early.
268 //This method is overriden in Gtk::Window because they can not be manage()ed.
270 if (!referenced_) return; //It's already managed.
272 // remove our reference
273 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.
275 //g_warning("Object::set_manage(), making object floating: %s\n", G_OBJECT_TYPE_NAME(gobject_));
277 // Cowardly refuse to remove last reference make floating instead. //TODO: What does that comment mean?
278 #ifdef GLIBMM_DEBUG_REFCOUNTING
279 g_warning("Object::set_manage(): setting GTK_FLOATING: gobject_ = %p", (void*) gobj());
280 g_warning(" gtypename=%s\n", G_OBJECT_TYPE_NAME(gobj()));
282 //deprecated: GTK_OBJECT_SET_FLAGS(gobj(), GTK_FLOATING);
283 g_object_force_floating(gobject_);
287 g_warning("Object::set_manage(). Refcount seems to be 0. %s\n", G_OBJECT_TYPE_NAME(gobject_));
289 //DEF_GLIBMM_DEBUG_UNREF(this, gobj())
290 //g_object_unref(gobj());
293 //g_warning("Object::set_manage(): end: %s", G_OBJECT_TYPE_NAME(gobject_));
294 //g_warning(" refcount=%d", G_OBJECT(gobj())->ref_count);
299 void Object::callback_destroy_(GObject*, void* data) //static
301 //This is only used for a short time, then disconnected.
303 Object* cppObject = static_cast<Object*>(data);
304 if(cppObject) //This will be 0 if the C++ destructor has already run.
306 cppObject->gobject_disposed_ = true;
310 bool Object::is_managed_() const