1 #ifndef _GLIBMM_EXTRACLASSINIT_H
2 #define _GLIBMM_EXTRACLASSINIT_H
3 /* Copyright (C) 2017 The glibmm Development Team
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <glibmm/objectbase.h>
20 
21 namespace Glib
22 {
23 
24 /** A convenience class for named custom types.
25  *
26  * Use it if you need to add code to GType's class init function and/or
27  * need an instance init function.
28  * Example:
29  * @code
30  * #include <glibmm/extraclassinit.h>
31  *
32  * class MyExtraInit : public Glib::ExtraClassInit
33  * {
34  * public:
35  *   MyExtraInit(const Glib::ustring& css_name)
36  *   :
37  *   Glib::ExtraClassInit(my_extra_class_init_function, &m_css_name, my_instance_init_function),
38  *   m_css_name(css_name)
39  *   { }
40  *
41  * private:
42  *   static void my_extra_class_init_function(void* g_class, void* class_data)
43  *   {
44  *     const auto klass = static_cast<GtkWidgetClass*>(g_class);
45  *     const auto css_name = static_cast<Glib::ustring*>(class_data);
46  *     gtk_widget_class_set_css_name(klass, css_name->c_str());
47  *   }
48  *   static void my_instance_init_function(GTypeInstance* instance, void* g_class)
49  *   {
50  *     gtk_widget_set_has_surface(GTK_WIDGET(instance), true);
51  *   }
52  *
53  *   Glib::ustring m_css_name;
54  * };
55  *
56  * class MyWidget : public MyExtraInit, public Gtk::Widget
57  * {
58  * public:
59  *   MyWidget()
60  *   :
61  *   // The GType name will be gtkmm__CustomObject_MyWidget
62  *   Glib::ObjectBase("MyWidget"), // Unique class name
63  *   MyExtraInit("my-widget"),
64  *   Gtk::Widget()
65  *   {
66  *     // ...
67  *   }
68  *   // ...
69  * };
70  * @endcode
71  *
72  * @note Classes derived from %ExtraClassInit (MyExtraInit in the example)
73  * must be listed before Glib::Object or a class derived from
74  * %Glib::Object (Gtk::Widget in the example) in the list of base classes.
75  *
76  * @newin{2,60}
77  */
78 class GLIBMM_API ExtraClassInit : virtual public ObjectBase
79 {
80 protected:
81   /** Constructor.
82    *
83    * @param class_init_func Pointer to an extra class init function.
84    *        nullptr, if no extra class init function is needed.
85    * @param class_data Class data pointer, passed to the class init function.
86    *        Can be nullptr, if the class init function does not need it.
87    * @param instance_init_func Pointer to an instance init function.
88    *        nullptr, if no instance init function is needed.
89    */
90   explicit ExtraClassInit(GClassInitFunc class_init_func, void* class_data = nullptr,
91     GInstanceInitFunc instance_init_func = nullptr);
92 };
93 
94 } // namespace Glib
95 
96 #endif /* _GLIBMM_EXTRACLASSINIT_H */
97