1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gobject.ObjectClass;
26 
27 private import glib.Str;
28 private import gobject.ObjectG;
29 private import gobject.ParamSpec;
30 private import gobject.c.functions;
31 public  import gobject.c.types;
32 public  import gtkc.gobjecttypes;
33 
34 
35 /**
36  * The class structure for the GObject type.
37  * 
38  * <example>
39  * <title>Implementing singletons using a constructor</title>
40  * <programlisting>
41  * static MySingleton *the_singleton = NULL;
42  * 
43  * static GObject*
44  * my_singleton_constructor (GType                  type,
45  * guint                  n_construct_params,
46  * GObjectConstructParam *construct_params)
47  * {
48  * GObject *object;
49  * 
50  * if (!the_singleton)
51  * {
52  * object = G_OBJECT_CLASS (parent_class)->constructor (type,
53  * n_construct_params,
54  * construct_params);
55  * the_singleton = MY_SINGLETON (object);
56  * }
57  * else
58  * object = g_object_ref (G_OBJECT (the_singleton));
59  * 
60  * return object;
61  * }
62  * </programlisting></example>
63  */
64 public class ObjectClass
65 {
66 	/** the main Gtk struct */
67 	protected GObjectClass* gObjectClass;
68 	protected bool ownedRef;
69 
70 	/** Get the main Gtk struct */
71 	public GObjectClass* getObjectClassStruct(bool transferOwnership = false)
72 	{
73 		if (transferOwnership)
74 			ownedRef = false;
75 		return gObjectClass;
76 	}
77 
78 	/** the main Gtk struct as a void* */
79 	protected void* getStruct()
80 	{
81 		return cast(void*)gObjectClass;
82 	}
83 
84 	/**
85 	 * Sets our main struct and passes it to the parent class.
86 	 */
87 	public this (GObjectClass* gObjectClass, bool ownedRef = false)
88 	{
89 		this.gObjectClass = gObjectClass;
90 		this.ownedRef = ownedRef;
91 	}
92 
93 
94 	/**
95 	 * Looks up the #GParamSpec for a property of a class.
96 	 *
97 	 * Params:
98 	 *     propertyName = the name of the property to look up
99 	 *
100 	 * Returns: the #GParamSpec for the property, or
101 	 *     %NULL if the class doesn't have a property of that name
102 	 */
103 	public ParamSpec findProperty(string propertyName)
104 	{
105 		auto p = g_object_class_find_property(gObjectClass, Str.toStringz(propertyName));
106 
107 		if(p is null)
108 		{
109 			return null;
110 		}
111 
112 		return ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) p);
113 	}
114 
115 	/**
116 	 * Installs new properties from an array of #GParamSpecs.
117 	 *
118 	 * All properties should be installed during the class initializer.  It
119 	 * is possible to install properties after that, but doing so is not
120 	 * recommend, and specifically, is not guaranteed to be thread-safe vs.
121 	 * use of properties on the same type on other threads.
122 	 *
123 	 * The property id of each property is the index of each #GParamSpec in
124 	 * the @pspecs array.
125 	 *
126 	 * The property id of 0 is treated specially by #GObject and it should not
127 	 * be used to store a #GParamSpec.
128 	 *
129 	 * This function should be used if you plan to use a static array of
130 	 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this
131 	 * class initialization:
132 	 *
133 	 * |[<!-- language="C" -->
134 	 * enum {
135 	 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
136 	 * };
137 	 *
138 	 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
139 	 *
140 	 * static void
141 	 * my_object_class_init (MyObjectClass *klass)
142 	 * {
143 	 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
144 	 *
145 	 * obj_properties[PROP_FOO] =
146 	 * g_param_spec_int ("foo", "Foo", "Foo",
147 	 * -1, G_MAXINT,
148 	 * 0,
149 	 * G_PARAM_READWRITE);
150 	 *
151 	 * obj_properties[PROP_BAR] =
152 	 * g_param_spec_string ("bar", "Bar", "Bar",
153 	 * NULL,
154 	 * G_PARAM_READWRITE);
155 	 *
156 	 * gobject_class->set_property = my_object_set_property;
157 	 * gobject_class->get_property = my_object_get_property;
158 	 * g_object_class_install_properties (gobject_class,
159 	 * N_PROPERTIES,
160 	 * obj_properties);
161 	 * }
162 	 * ]|
163 	 *
164 	 * allows calling g_object_notify_by_pspec() to notify of property changes:
165 	 *
166 	 * |[<!-- language="C" -->
167 	 * void
168 	 * my_object_set_foo (MyObject *self, gint foo)
169 	 * {
170 	 * if (self->foo != foo)
171 	 * {
172 	 * self->foo = foo;
173 	 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
174 	 * }
175 	 * }
176 	 * ]|
177 	 *
178 	 * Params:
179 	 *     pspecs = the #GParamSpecs array
180 	 *         defining the new properties
181 	 *
182 	 * Since: 2.26
183 	 */
184 	public void installProperties(ParamSpec[] pspecs)
185 	{
186 		GParamSpec*[] pspecsArray = new GParamSpec*[pspecs.length];
187 		for ( int i = 0; i < pspecs.length; i++ )
188 		{
189 			pspecsArray[i] = pspecs[i].getParamSpecStruct();
190 		}
191 
192 		g_object_class_install_properties(gObjectClass, cast(uint)pspecs.length, pspecsArray.ptr);
193 	}
194 
195 	/**
196 	 * Installs a new property.
197 	 *
198 	 * All properties should be installed during the class initializer.  It
199 	 * is possible to install properties after that, but doing so is not
200 	 * recommend, and specifically, is not guaranteed to be thread-safe vs.
201 	 * use of properties on the same type on other threads.
202 	 *
203 	 * Note that it is possible to redefine a property in a derived class,
204 	 * by installing a property with the same name. This can be useful at times,
205 	 * e.g. to change the range of allowed values or the default value.
206 	 *
207 	 * Params:
208 	 *     propertyId = the id for the new property
209 	 *     pspec = the #GParamSpec for the new property
210 	 */
211 	public void installProperty(uint propertyId, ParamSpec pspec)
212 	{
213 		g_object_class_install_property(gObjectClass, propertyId, (pspec is null) ? null : pspec.getParamSpecStruct());
214 	}
215 
216 	/**
217 	 * Get an array of #GParamSpec* for all properties of a class.
218 	 *
219 	 * Returns: an array of
220 	 *     #GParamSpec* which should be freed after use
221 	 */
222 	public ParamSpec[] listProperties()
223 	{
224 		uint nProperties;
225 
226 		auto p = g_object_class_list_properties(gObjectClass, &nProperties);
227 
228 		if(p is null)
229 		{
230 			return null;
231 		}
232 
233 		ParamSpec[] arr = new ParamSpec[nProperties];
234 		for(int i = 0; i < nProperties; i++)
235 		{
236 			arr[i] = ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) p[i]);
237 		}
238 
239 		return arr;
240 	}
241 
242 	/**
243 	 * Registers @property_id as referring to a property with the name
244 	 * @name in a parent class or in an interface implemented by @oclass.
245 	 * This allows this class to "override" a property implementation in
246 	 * a parent class or to provide the implementation of a property from
247 	 * an interface.
248 	 *
249 	 * Internally, overriding is implemented by creating a property of type
250 	 * #GParamSpecOverride; generally operations that query the properties of
251 	 * the object class, such as g_object_class_find_property() or
252 	 * g_object_class_list_properties() will return the overridden
253 	 * property. However, in one case, the @construct_properties argument of
254 	 * the @constructor virtual function, the #GParamSpecOverride is passed
255 	 * instead, so that the @param_id field of the #GParamSpec will be
256 	 * correct.  For virtually all uses, this makes no difference. If you
257 	 * need to get the overridden property, you can call
258 	 * g_param_spec_get_redirect_target().
259 	 *
260 	 * Params:
261 	 *     propertyId = the new property ID
262 	 *     name = the name of a property registered in a parent class or
263 	 *         in an interface of this class.
264 	 *
265 	 * Since: 2.4
266 	 */
267 	public void overrideProperty(uint propertyId, string name)
268 	{
269 		g_object_class_override_property(gObjectClass, propertyId, Str.toStringz(name));
270 	}
271 }