rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / glibmm2 / glib / glibmm / class.cc
1 // -*- c++ -*-
2 /* $Id: class.cc 336 2006-10-04 12:06:14Z murrayc $ */
3
4 /* Copyright (C) 1998-2002 The gtkmm Development Team
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <glibmm/class.h>
22 #include <glibmm/property.h>
23 #include <glibmm/ustring.h>
24 #include <glibmm/utility.h>
25
26
27 namespace Glib
28 {
29
30 void Class::register_derived_type(GType base_type)
31 {
32   if(gtype_)
33     return; // already initialized
34
35   GTypeQuery base_query = { 0, 0, 0, 0, };
36   g_type_query(base_type, &base_query);
37
38   const GTypeInfo derived_info =
39   {
40     base_query.class_size,
41     0, // base_init
42     0, // base_finalize
43     class_init_func_,
44     0, // class_finalize
45     0, // class_data
46     base_query.instance_size,
47     0, // n_preallocs
48     0, // instance_init
49     0, // value_table
50   };
51
52   Glib::ustring derived_name = "gtkmm__";
53   derived_name += base_query.type_name;
54
55   gtype_ = g_type_register_static(base_type, derived_name.c_str(), &derived_info, GTypeFlags(0));
56 }
57
58 GType Class::clone_custom_type(const char* custom_type_name) const
59 {
60   std::string full_name ("gtkmm__CustomObject_");
61   Glib::append_canonical_typename(full_name, custom_type_name);
62
63   GType custom_type = g_type_from_name(full_name.c_str());
64
65   if(!custom_type)
66   {
67     g_return_val_if_fail(gtype_ != 0, 0);
68
69     // Cloned custom types derive from the wrapper's parent type,
70     // so that g_type_class_peek_parent() works correctly.
71     const GType base_type = g_type_parent(gtype_);
72
73     GTypeQuery base_query = { 0, 0, 0, 0, };
74     g_type_query(base_type, &base_query);
75
76     const GTypeInfo derived_info =
77     {
78       base_query.class_size,
79       0, // base_init
80       0, // base_finalize
81       &Class::custom_class_init_function,
82       0, // class_finalize
83       this, // class_data
84       base_query.instance_size,
85       0, // n_preallocs
86       0, // instance_init
87       0, // value_table
88     };
89
90     custom_type = g_type_register_static(
91         base_type, full_name.c_str(), &derived_info, GTypeFlags(0));
92   }
93
94   return custom_type;
95 }
96
97 // static
98 void Class::custom_class_init_function(void* g_class, void* class_data)
99 {
100   // The class_data pointer is set to 'this' by clone_custom_type().
101   const Class *const self = static_cast<Class*>(class_data);
102
103   g_return_if_fail(self->class_init_func_ != 0);
104
105   // Call the wrapper's class_init_function() to redirect
106   // the vfunc and default signal handler callbacks.
107   (*self->class_init_func_)(g_class, 0);
108
109 #ifdef GLIBMM_PROPERTIES_ENABLED
110   GObjectClass *const gobject_class = static_cast<GObjectClass*>(g_class);
111   gobject_class->get_property = &Glib::custom_get_property_callback;
112   gobject_class->set_property = &Glib::custom_set_property_callback;
113 #endif //GLIBMM_PROPERTIES_ENABLED
114 }
115
116 } // namespace Glib
117