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 gtkc.gobject; 31 public import gtkc.gobjecttypes; 32 33 34 /** 35 * The class structure for the GObject type. 36 * 37 * <example> 38 * <title>Implementing singletons using a constructor</title> 39 * <programlisting> 40 * static MySingleton *the_singleton = NULL; 41 * 42 * static GObject* 43 * my_singleton_constructor (GType type, 44 * guint n_construct_params, 45 * GObjectConstructParam *construct_params) 46 * { 47 * GObject *object; 48 * 49 * if (!the_singleton) 50 * { 51 * object = G_OBJECT_CLASS (parent_class)->constructor (type, 52 * n_construct_params, 53 * construct_params); 54 * the_singleton = MY_SINGLETON (object); 55 * } 56 * else 57 * object = g_object_ref (G_OBJECT (the_singleton)); 58 * 59 * return object; 60 * } 61 * </programlisting></example> 62 */ 63 public class ObjectClass 64 { 65 /** the main Gtk struct */ 66 protected GObjectClass* gObjectClass; 67 protected bool ownedRef; 68 69 /** Get the main Gtk struct */ 70 public GObjectClass* getObjectClassStruct(bool transferOwnership = false) 71 { 72 if (transferOwnership) 73 ownedRef = false; 74 return gObjectClass; 75 } 76 77 /** the main Gtk struct as a void* */ 78 protected void* getStruct() 79 { 80 return cast(void*)gObjectClass; 81 } 82 83 /** 84 * Sets our main struct and passes it to the parent class. 85 */ 86 public this (GObjectClass* gObjectClass, bool ownedRef = false) 87 { 88 this.gObjectClass = gObjectClass; 89 this.ownedRef = ownedRef; 90 } 91 92 93 /** 94 * Looks up the #GParamSpec for a property of a class. 95 * 96 * Params: 97 * propertyName = the name of the property to look up 98 * 99 * Returns: the #GParamSpec for the property, or 100 * %NULL if the class doesn't have a property of that name 101 */ 102 public ParamSpec findProperty(string propertyName) 103 { 104 auto p = g_object_class_find_property(gObjectClass, Str.toStringz(propertyName)); 105 106 if(p is null) 107 { 108 return null; 109 } 110 111 return ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) p); 112 } 113 114 /** 115 * Installs new properties from an array of #GParamSpecs. 116 * 117 * All properties should be installed during the class initializer. It 118 * is possible to install properties after that, but doing so is not 119 * recommend, and specifically, is not guaranteed to be thread-safe vs. 120 * use of properties on the same type on other threads. 121 * 122 * The property id of each property is the index of each #GParamSpec in 123 * the @pspecs array. 124 * 125 * The property id of 0 is treated specially by #GObject and it should not 126 * be used to store a #GParamSpec. 127 * 128 * This function should be used if you plan to use a static array of 129 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this 130 * class initialization: 131 * 132 * |[<!-- language="C" --> 133 * enum { 134 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES 135 * }; 136 * 137 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; 138 * 139 * static void 140 * my_object_class_init (MyObjectClass *klass) 141 * { 142 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass); 143 * 144 * obj_properties[PROP_FOO] = 145 * g_param_spec_int ("foo", "Foo", "Foo", 146 * -1, G_MAXINT, 147 * 0, 148 * G_PARAM_READWRITE); 149 * 150 * obj_properties[PROP_BAR] = 151 * g_param_spec_string ("bar", "Bar", "Bar", 152 * NULL, 153 * G_PARAM_READWRITE); 154 * 155 * gobject_class->set_property = my_object_set_property; 156 * gobject_class->get_property = my_object_get_property; 157 * g_object_class_install_properties (gobject_class, 158 * N_PROPERTIES, 159 * obj_properties); 160 * } 161 * ]| 162 * 163 * allows calling g_object_notify_by_pspec() to notify of property changes: 164 * 165 * |[<!-- language="C" --> 166 * void 167 * my_object_set_foo (MyObject *self, gint foo) 168 * { 169 * if (self->foo != foo) 170 * { 171 * self->foo = foo; 172 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]); 173 * } 174 * } 175 * ]| 176 * 177 * Params: 178 * nPspecs = the length of the #GParamSpecs array 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 }