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 * |[<!-- language="C" --> 39 * // Example of implementing a singleton using a constructor. 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 * ]| 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 * pspecs = the #GParamSpecs array 179 * defining the new properties 180 * 181 * Since: 2.26 182 */ 183 public void installProperties(ParamSpec[] pspecs) 184 { 185 GParamSpec*[] pspecsArray = new GParamSpec*[pspecs.length]; 186 for ( int i = 0; i < pspecs.length; i++ ) 187 { 188 pspecsArray[i] = pspecs[i].getParamSpecStruct(); 189 } 190 191 g_object_class_install_properties(gObjectClass, cast(uint)pspecs.length, pspecsArray.ptr); 192 } 193 194 /** 195 * Installs a new property. 196 * 197 * All properties should be installed during the class initializer. It 198 * is possible to install properties after that, but doing so is not 199 * recommend, and specifically, is not guaranteed to be thread-safe vs. 200 * use of properties on the same type on other threads. 201 * 202 * Note that it is possible to redefine a property in a derived class, 203 * by installing a property with the same name. This can be useful at times, 204 * e.g. to change the range of allowed values or the default value. 205 * 206 * Params: 207 * propertyId = the id for the new property 208 * pspec = the #GParamSpec for the new property 209 */ 210 public void installProperty(uint propertyId, ParamSpec pspec) 211 { 212 g_object_class_install_property(gObjectClass, propertyId, (pspec is null) ? null : pspec.getParamSpecStruct()); 213 } 214 215 /** 216 * Get an array of #GParamSpec* for all properties of a class. 217 * 218 * Returns: an array of 219 * #GParamSpec* which should be freed after use 220 */ 221 public ParamSpec[] listProperties() 222 { 223 uint nProperties; 224 225 auto __p = g_object_class_list_properties(gObjectClass, &nProperties); 226 227 if(__p is null) 228 { 229 return null; 230 } 231 232 ParamSpec[] arr = new ParamSpec[nProperties]; 233 for(int i = 0; i < nProperties; i++) 234 { 235 arr[i] = ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) __p[i]); 236 } 237 238 return arr; 239 } 240 241 /** 242 * Registers @property_id as referring to a property with the name 243 * @name in a parent class or in an interface implemented by @oclass. 244 * This allows this class to "override" a property implementation in 245 * a parent class or to provide the implementation of a property from 246 * an interface. 247 * 248 * Internally, overriding is implemented by creating a property of type 249 * #GParamSpecOverride; generally operations that query the properties of 250 * the object class, such as g_object_class_find_property() or 251 * g_object_class_list_properties() will return the overridden 252 * property. However, in one case, the @construct_properties argument of 253 * the @constructor virtual function, the #GParamSpecOverride is passed 254 * instead, so that the @param_id field of the #GParamSpec will be 255 * correct. For virtually all uses, this makes no difference. If you 256 * need to get the overridden property, you can call 257 * g_param_spec_get_redirect_target(). 258 * 259 * Params: 260 * propertyId = the new property ID 261 * name = the name of a property registered in a parent class or 262 * in an interface of this class. 263 * 264 * Since: 2.4 265 */ 266 public void overrideProperty(uint propertyId, string name) 267 { 268 g_object_class_override_property(gObjectClass, propertyId, Str.toStringz(name)); 269 } 270 }