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 68 /** Get the main Gtk struct */ 69 public GObjectClass* getObjectClassStruct() 70 { 71 return gObjectClass; 72 } 73 74 /** the main Gtk struct as a void* */ 75 protected void* getStruct() 76 { 77 return cast(void*)gObjectClass; 78 } 79 80 /** 81 * Sets our main struct and passes it to the parent class. 82 */ 83 public this (GObjectClass* gObjectClass) 84 { 85 this.gObjectClass = gObjectClass; 86 } 87 88 89 /** 90 * Looks up the #GParamSpec for a property of a class. 91 * 92 * Params: 93 * propertyName = the name of the property to look up 94 * 95 * Return: the #GParamSpec for the property, or 96 * %NULL if the class doesn't have a property of that name 97 */ 98 public ParamSpec findProperty(string propertyName) 99 { 100 auto p = g_object_class_find_property(gObjectClass, Str.toStringz(propertyName)); 101 102 if(p is null) 103 { 104 return null; 105 } 106 107 return ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) p); 108 } 109 110 /** 111 * Installs new properties from an array of #GParamSpecs. 112 * 113 * All properties should be installed during the class initializer. It 114 * is possible to install properties after that, but doing so is not 115 * recommend, and specifically, is not guaranteed to be thread-safe vs. 116 * use of properties on the same type on other threads. 117 * 118 * The property id of each property is the index of each #GParamSpec in 119 * the @pspecs array. 120 * 121 * The property id of 0 is treated specially by #GObject and it should not 122 * be used to store a #GParamSpec. 123 * 124 * This function should be used if you plan to use a static array of 125 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this 126 * class initialization: 127 * 128 * |[<!-- language="C" --> 129 * enum { 130 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES 131 * }; 132 * 133 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; 134 * 135 * static void 136 * my_object_class_init (MyObjectClass *klass) 137 * { 138 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass); 139 * 140 * obj_properties[PROP_FOO] = 141 * g_param_spec_int ("foo", "Foo", "Foo", 142 * -1, G_MAXINT, 143 * 0, 144 * G_PARAM_READWRITE); 145 * 146 * obj_properties[PROP_BAR] = 147 * g_param_spec_string ("bar", "Bar", "Bar", 148 * NULL, 149 * G_PARAM_READWRITE); 150 * 151 * gobject_class->set_property = my_object_set_property; 152 * gobject_class->get_property = my_object_get_property; 153 * g_object_class_install_properties (gobject_class, 154 * N_PROPERTIES, 155 * obj_properties); 156 * } 157 * ]| 158 * 159 * allows calling g_object_notify_by_pspec() to notify of property changes: 160 * 161 * |[<!-- language="C" --> 162 * void 163 * my_object_set_foo (MyObject *self, gint foo) 164 * { 165 * if (self->foo != foo) 166 * { 167 * self->foo = foo; 168 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]); 169 * } 170 * } 171 * ]| 172 * 173 * Params: 174 * nPspecs = the length of the #GParamSpecs array 175 * pspecs = the #GParamSpecs array 176 * defining the new properties 177 * 178 * Since: 2.26 179 */ 180 public void installProperties(ParamSpec[] pspecs) 181 { 182 GParamSpec*[] pspecsArray = new GParamSpec*[pspecs.length]; 183 for ( int i = 0; i < pspecs.length; i++ ) 184 { 185 pspecsArray[i] = pspecs[i].getParamSpecStruct(); 186 } 187 188 g_object_class_install_properties(gObjectClass, cast(uint)pspecs.length, pspecsArray.ptr); 189 } 190 191 /** 192 * Installs a new property. 193 * 194 * All properties should be installed during the class initializer. It 195 * is possible to install properties after that, but doing so is not 196 * recommend, and specifically, is not guaranteed to be thread-safe vs. 197 * use of properties on the same type on other threads. 198 * 199 * Note that it is possible to redefine a property in a derived class, 200 * by installing a property with the same name. This can be useful at times, 201 * e.g. to change the range of allowed values or the default value. 202 * 203 * Params: 204 * propertyId = the id for the new property 205 * pspec = the #GParamSpec for the new property 206 */ 207 public void installProperty(uint propertyId, ParamSpec pspec) 208 { 209 g_object_class_install_property(gObjectClass, propertyId, (pspec is null) ? null : pspec.getParamSpecStruct()); 210 } 211 212 /** 213 * Get an array of #GParamSpec* for all properties of a class. 214 * 215 * Return: an array of 216 * #GParamSpec* which should be freed after use 217 */ 218 public ParamSpec[] listProperties() 219 { 220 uint nProperties; 221 222 auto p = g_object_class_list_properties(gObjectClass, &nProperties); 223 224 if(p is null) 225 { 226 return null; 227 } 228 229 ParamSpec[] arr = new ParamSpec[nProperties]; 230 for(int i = 0; i < nProperties; i++) 231 { 232 arr[i] = ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) p[i]); 233 } 234 235 return arr; 236 } 237 238 /** 239 * Registers @property_id as referring to a property with the name 240 * @name in a parent class or in an interface implemented by @oclass. 241 * This allows this class to "override" a property implementation in 242 * a parent class or to provide the implementation of a property from 243 * an interface. 244 * 245 * Internally, overriding is implemented by creating a property of type 246 * #GParamSpecOverride; generally operations that query the properties of 247 * the object class, such as g_object_class_find_property() or 248 * g_object_class_list_properties() will return the overridden 249 * property. However, in one case, the @construct_properties argument of 250 * the @constructor virtual function, the #GParamSpecOverride is passed 251 * instead, so that the @param_id field of the #GParamSpec will be 252 * correct. For virtually all uses, this makes no difference. If you 253 * need to get the overridden property, you can call 254 * g_param_spec_get_redirect_target(). 255 * 256 * Params: 257 * propertyId = the new property ID 258 * name = the name of a property registered in a parent class or 259 * in an interface of this class. 260 * 261 * Since: 2.4 262 */ 263 public void overrideProperty(uint propertyId, string name) 264 { 265 g_object_class_override_property(gObjectClass, propertyId, Str.toStringz(name)); 266 } 267 }