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.ObjectG; 26 27 private import core.memory; 28 private import glib.ConstructionException; 29 private import glib.Str; 30 private import gobject.Binding; 31 private import gobject.Closure; 32 private import gobject.ObjectG; 33 private import gobject.ParamSpec; 34 private import gobject.Signals; 35 private import gobject.Value; 36 private import gtkc.Loader; 37 private import gtkc.gobject; 38 public import gtkc.gobjecttypes; 39 private import gtkc.paths; 40 41 42 /** 43 * All the fields in the GObject structure are private 44 * to the #GObject implementation and should never be accessed directly. 45 */ 46 public class ObjectG 47 { 48 /** the main Gtk struct */ 49 protected GObject* gObject; 50 51 /** Get the main Gtk struct */ 52 public GObject* getObjectGStruct() 53 { 54 return gObject; 55 } 56 57 /** the main Gtk struct as a void* */ 58 protected void* getStruct() 59 { 60 return cast(void*)gObject; 61 } 62 63 protected bool isGcRoot; 64 65 /** 66 * Sets our main struct and passes store it on the gobject. 67 * Add a gabage collector root to the gtk+ struct so it doesn't get collect 68 */ 69 public this (GObject* gObject, bool ownedRef = false) 70 { 71 this.gObject = gObject; 72 if ( gObject !is null ) 73 { 74 setDataFull("GObject", cast(void*)this, cast(GDestroyNotify)&destroyNotify); 75 addToggleRef(cast(GToggleNotify)&toggleNotify, cast(void*)this); 76 77 //If the refCount is larger then 1 toggleNotify isn't called 78 if (gObject.refCount > 1 && !isGcRoot) 79 { 80 GC.addRoot(cast(void*)this); 81 isGcRoot = true; 82 } 83 84 //Remove the floating reference if there is one. 85 if ( isFloating() ) 86 { 87 refSink(); 88 unref(); 89 } 90 //If we already owned this reference remove the one added by addToggleRef. 91 else if ( ownedRef ) 92 { 93 unref(); 94 } 95 96 //When constructed via GtkBuilder set the structs. 97 if ( getStruct() is null) 98 { 99 setStruct(gObject); 100 } 101 } 102 } 103 104 extern(C) 105 { 106 static void destroyNotify(ObjectG obj) 107 { 108 if ( obj.isGcRoot ) 109 { 110 GC.removeRoot(cast(void*)obj); 111 obj.isGcRoot = false; 112 } 113 114 obj.gObject = null; 115 } 116 117 static void toggleNotify(ObjectG obj, GObject* object, int isLastRef) 118 { 119 if ( isLastRef && obj.isGcRoot ) 120 { 121 GC.removeRoot(cast(void*)obj); 122 obj.isGcRoot = false; 123 } 124 else if ( !obj.isGcRoot ) 125 { 126 GC.addRoot(cast(void*)obj); 127 obj.isGcRoot = true; 128 } 129 } 130 } 131 132 ~this() 133 { 134 if ( Linker.isLoaded(LIBRARY.GOBJECT) && gObject !is null ) 135 { 136 // Remove the GDestroyNotify callback, 137 // for when the D object is destroyed before the C one. 138 g_object_steal_data(gObject, cast(char*)"GObject"); 139 140 if ( isGcRoot ) 141 { 142 GC.removeRoot(cast(void*)this); 143 isGcRoot = false; 144 } 145 146 unref(); 147 } 148 } 149 150 /** 151 * Gets a D Object from the objects table of associations. 152 * Params: 153 * obj = GObject containing the associations. 154 * Returns: the D Object if found, or a newly constructed object if no such Object exists. 155 */ 156 public static RT getDObject(T, RT=T, U)(U obj, bool ownedRef = false) 157 { 158 if ( obj is null ) 159 { 160 return null; 161 } 162 163 static if ( is(T : ObjectG) ) 164 { 165 auto p = g_object_get_data(cast(GObject*)obj, Str.toStringz("GObject")); 166 167 if ( p !is null ) 168 { 169 static if ( is(RT == interface ) ) 170 { 171 return cast(RT)cast(ObjectG)p; 172 } 173 else 174 { 175 return cast(RT)p; 176 } 177 } 178 else 179 { 180 return new T(obj, ownedRef); 181 } 182 } 183 else 184 { 185 return new T(obj); 186 } 187 } 188 189 protected void setStruct(GObject* obj) 190 { 191 gObject = cast(GObject*)obj; 192 } 193 194 /** */ 195 public void setProperty(string propertyName, int value) 196 { 197 setProperty(propertyName, new Value(value)); 198 } 199 200 /** */ 201 public void setProperty(string propertyName, string value) 202 { 203 setProperty(propertyName, new Value(value)); 204 } 205 206 /** */ 207 public void setProperty(string propertyName, long value) 208 { 209 //We use g_object_set instead of g_object_set_property, because Value doesn't like longs and ulongs for some reason. 210 g_object_set( gObject, Str.toStringz(propertyName), value, null); 211 } 212 213 /** */ 214 public void setProperty(string propertyName, ulong value) 215 { 216 g_object_set( gObject, Str.toStringz(propertyName), value, null); 217 } 218 219 deprecated("Use the member function") 220 public static void unref(ObjectG obj) 221 { 222 obj.unref(); 223 } 224 225 deprecated("Use the member function") 226 public static ObjectG doref(ObjectG obj) 227 { 228 return obj.doref(); 229 } 230 231 int[string] connectedSignals; 232 233 void delegate(ParamSpec, ObjectG)[] onNotifyListeners; 234 /** 235 * The notify signal is emitted on an object when one of its 236 * properties has been changed. Note that getting this signal 237 * doesn't guarantee that the value of the property has actually 238 * changed, it may also be emitted when the setter for the property 239 * is called to reinstate the previous value. 240 * 241 * This signal is typically used to obtain change notification for a 242 * single property. 243 * 244 * It is important to note that you must use 245 * canonical parameter names for the property. 246 * 247 * Params: 248 * dlg = The callback. 249 * property = Set this if you only want to receive the signal for a specific property. 250 * connectFlags = The behavior of the signal's connection. 251 */ 252 void addOnNotify(void delegate(ParamSpec, ObjectG) dlg, string property = "", ConnectFlags connectFlags=cast(ConnectFlags)0) 253 { 254 string signalName; 255 256 if ( property == "" ) 257 signalName = "notify"; 258 else 259 signalName = "notify::"~ property; 260 261 if ( !(signalName in connectedSignals) ) 262 { 263 Signals.connectData( 264 this, 265 signalName, 266 cast(GCallback)&callBackNotify, 267 cast(void*)this, 268 null, 269 connectFlags); 270 connectedSignals[signalName] = 1; 271 } 272 onNotifyListeners ~= dlg; 273 } 274 extern(C) static void callBackNotify(GObject* gobjectStruct, GParamSpec* pspec, ObjectG _objectG) 275 { 276 foreach ( void delegate(ParamSpec, ObjectG) dlg ; _objectG.onNotifyListeners ) 277 { 278 dlg(ObjectG.getDObject!(ParamSpec)(pspec), _objectG); 279 } 280 } 281 282 /** 283 */ 284 285 /** */ 286 public static GType getType() 287 { 288 return g_initially_unowned_get_type(); 289 } 290 291 /** 292 * Creates a new instance of a #GObject subtype and sets its properties. 293 * 294 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY) 295 * which are not explicitly specified are set to their default values. 296 * 297 * Params: 298 * objectType = the type id of the #GObject subtype to instantiate 299 * firstPropertyName = the name of the first property 300 * varArgs = the value of the first property, followed optionally by more 301 * name/value pairs, followed by %NULL 302 * 303 * Return: a new instance of @object_type 304 * 305 * Throws: ConstructionException GTK+ fails to create the object. 306 */ 307 public this(GType objectType, string firstPropertyName, void* varArgs) 308 { 309 auto p = g_object_new_valist(objectType, Str.toStringz(firstPropertyName), varArgs); 310 311 if(p is null) 312 { 313 throw new ConstructionException("null returned by new_valist"); 314 } 315 316 this(cast(GObject*) p, true); 317 } 318 319 /** 320 * Creates a new instance of a #GObject subtype and sets its properties. 321 * 322 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY) 323 * which are not explicitly specified are set to their default values. 324 * 325 * Params: 326 * objectType = the type id of the #GObject subtype to instantiate 327 * nParameters = the length of the @parameters array 328 * parameters = an array of #GParameter 329 * 330 * Return: a new instance of 331 * @object_type 332 * 333 * Throws: ConstructionException GTK+ fails to create the object. 334 */ 335 public this(GType objectType, GParameter[] parameters) 336 { 337 auto p = g_object_newv(objectType, cast(uint)parameters.length, parameters.ptr); 338 339 if(p is null) 340 { 341 throw new ConstructionException("null returned by newv"); 342 } 343 344 this(cast(GObject*) p, true); 345 } 346 347 /** */ 348 public static size_t compatControl(size_t what, void* data) 349 { 350 return g_object_compat_control(what, data); 351 } 352 353 /** 354 * Find the #GParamSpec with the given name for an 355 * interface. Generally, the interface vtable passed in as @g_iface 356 * will be the default vtable from g_type_default_interface_ref(), or, 357 * if you know the interface has already been loaded, 358 * g_type_default_interface_peek(). 359 * 360 * Params: 361 * gIface = any interface vtable for the interface, or the default 362 * vtable for the interface 363 * propertyName = name of a property to lookup. 364 * 365 * Return: the #GParamSpec for the property of the 366 * interface with the name @property_name, or %NULL if no 367 * such property exists. 368 * 369 * Since: 2.4 370 */ 371 public static ParamSpec interfaceFindProperty(void* gIface, string propertyName) 372 { 373 auto p = g_object_interface_find_property(gIface, Str.toStringz(propertyName)); 374 375 if(p is null) 376 { 377 return null; 378 } 379 380 return ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) p); 381 } 382 383 /** 384 * Add a property to an interface; this is only useful for interfaces 385 * that are added to GObject-derived types. Adding a property to an 386 * interface forces all objects classes with that interface to have a 387 * compatible property. The compatible property could be a newly 388 * created #GParamSpec, but normally 389 * g_object_class_override_property() will be used so that the object 390 * class only needs to provide an implementation and inherits the 391 * property description, default value, bounds, and so forth from the 392 * interface property. 393 * 394 * This function is meant to be called from the interface's default 395 * vtable initialization function (the @class_init member of 396 * #GTypeInfo.) It must not be called after after @class_init has 397 * been called for any object types implementing this interface. 398 * 399 * Params: 400 * gIface = any interface vtable for the interface, or the default 401 * vtable for the interface. 402 * pspec = the #GParamSpec for the new property 403 * 404 * Since: 2.4 405 */ 406 public static void interfaceInstallProperty(void* gIface, ParamSpec pspec) 407 { 408 g_object_interface_install_property(gIface, (pspec is null) ? null : pspec.getParamSpecStruct()); 409 } 410 411 /** 412 * Lists the properties of an interface.Generally, the interface 413 * vtable passed in as @g_iface will be the default vtable from 414 * g_type_default_interface_ref(), or, if you know the interface has 415 * already been loaded, g_type_default_interface_peek(). 416 * 417 * Params: 418 * gIface = any interface vtable for the interface, or the default 419 * vtable for the interface 420 * 421 * Return: a 422 * pointer to an array of pointers to #GParamSpec 423 * structures. The paramspecs are owned by GLib, but the 424 * array should be freed with g_free() when you are done with 425 * it. 426 * 427 * Since: 2.4 428 */ 429 public static ParamSpec[] interfaceListProperties(void* gIface) 430 { 431 uint nPropertiesP; 432 433 auto p = g_object_interface_list_properties(gIface, &nPropertiesP); 434 435 if(p is null) 436 { 437 return null; 438 } 439 440 ParamSpec[] arr = new ParamSpec[nPropertiesP]; 441 for(int i = 0; i < nPropertiesP; i++) 442 { 443 arr[i] = ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) p[i]); 444 } 445 446 return arr; 447 } 448 449 /** 450 * Increases the reference count of the object by one and sets a 451 * callback to be called when all other references to the object are 452 * dropped, or when this is already the last reference to the object 453 * and another reference is established. 454 * 455 * This functionality is intended for binding @object to a proxy 456 * object managed by another memory manager. This is done with two 457 * paired references: the strong reference added by 458 * g_object_add_toggle_ref() and a reverse reference to the proxy 459 * object which is either a strong reference or weak reference. 460 * 461 * The setup is that when there are no other references to @object, 462 * only a weak reference is held in the reverse direction from @object 463 * to the proxy object, but when there are other references held to 464 * @object, a strong reference is held. The @notify callback is called 465 * when the reference from @object to the proxy object should be 466 * "toggled" from strong to weak (@is_last_ref true) or weak to strong 467 * (@is_last_ref false). 468 * 469 * Since a (normal) reference must be held to the object before 470 * calling g_object_add_toggle_ref(), the initial state of the reverse 471 * link is always strong. 472 * 473 * Multiple toggle references may be added to the same gobject, 474 * however if there are multiple toggle references to an object, none 475 * of them will ever be notified until all but one are removed. For 476 * this reason, you should only ever use a toggle reference if there 477 * is important state in the proxy object. 478 * 479 * Params: 480 * notify = a function to call when this reference is the 481 * last reference to the object, or is no longer 482 * the last reference. 483 * data = data to pass to @notify 484 * 485 * Since: 2.8 486 */ 487 public void addToggleRef(GToggleNotify notify, void* data) 488 { 489 g_object_add_toggle_ref(gObject, notify, data); 490 } 491 492 /** 493 * Adds a weak reference from weak_pointer to @object to indicate that 494 * the pointer located at @weak_pointer_location is only valid during 495 * the lifetime of @object. When the @object is finalized, 496 * @weak_pointer will be set to %NULL. 497 * 498 * Note that as with g_object_weak_ref(), the weak references created by 499 * this method are not thread-safe: they cannot safely be used in one 500 * thread if the object's last g_object_unref() might happen in another 501 * thread. Use #GWeakRef if thread-safety is required. 502 * 503 * Params: 504 * weakPointerLocation = The memory address of a pointer. 505 */ 506 public void addWeakPointer(ref void* weakPointerLocation) 507 { 508 g_object_add_weak_pointer(gObject, &weakPointerLocation); 509 } 510 511 /** 512 * Creates a binding between @source_property on @source and @target_property 513 * on @target. Whenever the @source_property is changed the @target_property is 514 * updated using the same value. For instance: 515 * 516 * |[ 517 * g_object_bind_property (action, "active", widget, "sensitive", 0); 518 * ]| 519 * 520 * Will result in the "sensitive" property of the widget #GObject instance to be 521 * updated with the same value of the "active" property of the action #GObject 522 * instance. 523 * 524 * If @flags contains %G_BINDING_BIDIRECTIONAL then the binding will be mutual: 525 * if @target_property on @target changes then the @source_property on @source 526 * will be updated as well. 527 * 528 * The binding will automatically be removed when either the @source or the 529 * @target instances are finalized. To remove the binding without affecting the 530 * @source and the @target you can just call g_object_unref() on the returned 531 * #GBinding instance. 532 * 533 * A #GObject can have multiple bindings. 534 * 535 * Params: 536 * sourceProperty = the property on @source to bind 537 * target = the target #GObject 538 * targetProperty = the property on @target to bind 539 * flags = flags to pass to #GBinding 540 * 541 * Return: the #GBinding instance representing the 542 * binding between the two #GObject instances. The binding is released 543 * whenever the #GBinding reference count reaches zero. 544 * 545 * Since: 2.26 546 */ 547 public Binding bindProperty(string sourceProperty, ObjectG target, string targetProperty, GBindingFlags flags) 548 { 549 auto p = g_object_bind_property(gObject, Str.toStringz(sourceProperty), (target is null) ? null : target.getObjectGStruct(), Str.toStringz(targetProperty), flags); 550 551 if(p is null) 552 { 553 return null; 554 } 555 556 return ObjectG.getDObject!(Binding)(cast(GBinding*) p); 557 } 558 559 /** 560 * Complete version of g_object_bind_property(). 561 * 562 * Creates a binding between @source_property on @source and @target_property 563 * on @target, allowing you to set the transformation functions to be used by 564 * the binding. 565 * 566 * If @flags contains %G_BINDING_BIDIRECTIONAL then the binding will be mutual: 567 * if @target_property on @target changes then the @source_property on @source 568 * will be updated as well. The @transform_from function is only used in case 569 * of bidirectional bindings, otherwise it will be ignored 570 * 571 * The binding will automatically be removed when either the @source or the 572 * @target instances are finalized. To remove the binding without affecting the 573 * @source and the @target you can just call g_object_unref() on the returned 574 * #GBinding instance. 575 * 576 * A #GObject can have multiple bindings. 577 * 578 * The same @user_data parameter will be used for both @transform_to 579 * and @transform_from transformation functions; the @notify function will 580 * be called once, when the binding is removed. If you need different data 581 * for each transformation function, please use 582 * g_object_bind_property_with_closures() instead. 583 * 584 * Params: 585 * sourceProperty = the property on @source to bind 586 * target = the target #GObject 587 * targetProperty = the property on @target to bind 588 * flags = flags to pass to #GBinding 589 * transformTo = the transformation function 590 * from the @source to the @target, or %NULL to use the default 591 * transformFrom = the transformation function 592 * from the @target to the @source, or %NULL to use the default 593 * userData = custom data to be passed to the transformation functions, 594 * or %NULL 595 * notify = function to be called when disposing the binding, to free the 596 * resources used by the transformation functions 597 * 598 * Return: the #GBinding instance representing the 599 * binding between the two #GObject instances. The binding is released 600 * whenever the #GBinding reference count reaches zero. 601 * 602 * Since: 2.26 603 */ 604 public Binding bindPropertyFull(string sourceProperty, ObjectG target, string targetProperty, GBindingFlags flags, GBindingTransformFunc transformTo, GBindingTransformFunc transformFrom, void* userData, GDestroyNotify notify) 605 { 606 auto p = g_object_bind_property_full(gObject, Str.toStringz(sourceProperty), (target is null) ? null : target.getObjectGStruct(), Str.toStringz(targetProperty), flags, transformTo, transformFrom, userData, notify); 607 608 if(p is null) 609 { 610 return null; 611 } 612 613 return ObjectG.getDObject!(Binding)(cast(GBinding*) p); 614 } 615 616 /** 617 * Creates a binding between @source_property on @source and @target_property 618 * on @target, allowing you to set the transformation functions to be used by 619 * the binding. 620 * 621 * This function is the language bindings friendly version of 622 * g_object_bind_property_full(), using #GClosures instead of 623 * function pointers. 624 * 625 * Params: 626 * sourceProperty = the property on @source to bind 627 * target = the target #GObject 628 * targetProperty = the property on @target to bind 629 * flags = flags to pass to #GBinding 630 * transformTo = a #GClosure wrapping the transformation function 631 * from the @source to the @target, or %NULL to use the default 632 * transformFrom = a #GClosure wrapping the transformation function 633 * from the @target to the @source, or %NULL to use the default 634 * 635 * Return: the #GBinding instance representing the 636 * binding between the two #GObject instances. The binding is released 637 * whenever the #GBinding reference count reaches zero. 638 * 639 * Since: 2.26 640 */ 641 public Binding bindPropertyWithClosures(string sourceProperty, ObjectG target, string targetProperty, GBindingFlags flags, Closure transformTo, Closure transformFrom) 642 { 643 auto p = g_object_bind_property_with_closures(gObject, Str.toStringz(sourceProperty), (target is null) ? null : target.getObjectGStruct(), Str.toStringz(targetProperty), flags, (transformTo is null) ? null : transformTo.getClosureStruct(), (transformFrom is null) ? null : transformFrom.getClosureStruct()); 644 645 if(p is null) 646 { 647 return null; 648 } 649 650 return ObjectG.getDObject!(Binding)(cast(GBinding*) p); 651 } 652 653 /** 654 * This is a variant of g_object_get_data() which returns 655 * a 'duplicate' of the value. @dup_func defines the 656 * meaning of 'duplicate' in this context, it could e.g. 657 * take a reference on a ref-counted object. 658 * 659 * If the @key is not set on the object then @dup_func 660 * will be called with a %NULL argument. 661 * 662 * Note that @dup_func is called while user data of @object 663 * is locked. 664 * 665 * This function can be useful to avoid races when multiple 666 * threads are using object data on the same key on the same 667 * object. 668 * 669 * Params: 670 * key = a string, naming the user data pointer 671 * dupFunc = function to dup the value 672 * userData = passed as user_data to @dup_func 673 * 674 * Return: the result of calling @dup_func on the value 675 * associated with @key on @object, or %NULL if not set. 676 * If @dup_func is %NULL, the value is returned 677 * unmodified. 678 * 679 * Since: 2.34 680 */ 681 public void* dupData(string key, GDuplicateFunc dupFunc, void* userData) 682 { 683 return g_object_dup_data(gObject, Str.toStringz(key), dupFunc, userData); 684 } 685 686 /** 687 * This is a variant of g_object_get_qdata() which returns 688 * a 'duplicate' of the value. @dup_func defines the 689 * meaning of 'duplicate' in this context, it could e.g. 690 * take a reference on a ref-counted object. 691 * 692 * If the @quark is not set on the object then @dup_func 693 * will be called with a %NULL argument. 694 * 695 * Note that @dup_func is called while user data of @object 696 * is locked. 697 * 698 * This function can be useful to avoid races when multiple 699 * threads are using object data on the same key on the same 700 * object. 701 * 702 * Params: 703 * quark = a #GQuark, naming the user data pointer 704 * dupFunc = function to dup the value 705 * userData = passed as user_data to @dup_func 706 * 707 * Return: the result of calling @dup_func on the value 708 * associated with @quark on @object, or %NULL if not set. 709 * If @dup_func is %NULL, the value is returned 710 * unmodified. 711 * 712 * Since: 2.34 713 */ 714 public void* dupQdata(GQuark quark, GDuplicateFunc dupFunc, void* userData) 715 { 716 return g_object_dup_qdata(gObject, quark, dupFunc, userData); 717 } 718 719 /** 720 * This function is intended for #GObject implementations to re-enforce 721 * a [floating][floating-ref] object reference. Doing this is seldom 722 * required: all #GInitiallyUnowneds are created with a floating reference 723 * which usually just needs to be sunken by calling g_object_ref_sink(). 724 * 725 * Since: 2.10 726 */ 727 public void forceFloating() 728 { 729 g_object_force_floating(gObject); 730 } 731 732 /** 733 * Increases the freeze count on @object. If the freeze count is 734 * non-zero, the emission of "notify" signals on @object is 735 * stopped. The signals are queued until the freeze count is decreased 736 * to zero. Duplicate notifications are squashed so that at most one 737 * #GObject::notify signal is emitted for each property modified while the 738 * object is frozen. 739 * 740 * This is necessary for accessors that modify multiple properties to prevent 741 * premature notification while the object is still being modified. 742 */ 743 public void freezeNotify() 744 { 745 g_object_freeze_notify(gObject); 746 } 747 748 /** 749 * Gets a named field from the objects table of associations (see g_object_set_data()). 750 * 751 * Params: 752 * key = name of the key for that association 753 * 754 * Return: the data if found, or %NULL if no such data exists. 755 */ 756 public void* getData(string key) 757 { 758 return g_object_get_data(gObject, Str.toStringz(key)); 759 } 760 761 /** 762 * Gets a property of an object. @value must have been initialized to the 763 * expected type of the property (or a type to which the expected type can be 764 * transformed) using g_value_init(). 765 * 766 * In general, a copy is made of the property contents and the caller is 767 * responsible for freeing the memory by calling g_value_unset(). 768 * 769 * Note that g_object_get_property() is really intended for language 770 * bindings, g_object_get() is much more convenient for C programming. 771 * 772 * Params: 773 * propertyName = the name of the property to get 774 * value = return location for the property value 775 */ 776 public void getProperty(string propertyName, Value value) 777 { 778 g_object_get_property(gObject, Str.toStringz(propertyName), (value is null) ? null : value.getValueStruct()); 779 } 780 781 /** 782 * This function gets back user data pointers stored via 783 * g_object_set_qdata(). 784 * 785 * Params: 786 * quark = A #GQuark, naming the user data pointer 787 * 788 * Return: The user data pointer set, or %NULL 789 */ 790 public void* getQdata(GQuark quark) 791 { 792 return g_object_get_qdata(gObject, quark); 793 } 794 795 /** 796 * Gets properties of an object. 797 * 798 * In general, a copy is made of the property contents and the caller 799 * is responsible for freeing the memory in the appropriate manner for 800 * the type, for instance by calling g_free() or g_object_unref(). 801 * 802 * See g_object_get(). 803 * 804 * Params: 805 * firstPropertyName = name of the first property to get 806 * varArgs = return location for the first property, followed optionally by more 807 * name/return location pairs, followed by %NULL 808 */ 809 public void getValist(string firstPropertyName, void* varArgs) 810 { 811 g_object_get_valist(gObject, Str.toStringz(firstPropertyName), varArgs); 812 } 813 814 /** 815 * Checks whether @object has a [floating][floating-ref] reference. 816 * 817 * Return: %TRUE if @object has a floating reference 818 * 819 * Since: 2.10 820 */ 821 public bool isFloating() 822 { 823 return g_object_is_floating(gObject) != 0; 824 } 825 826 /** 827 * Emits a "notify" signal for the property @property_name on @object. 828 * 829 * When possible, eg. when signaling a property change from within the class 830 * that registered the property, you should use g_object_notify_by_pspec() 831 * instead. 832 * 833 * Note that emission of the notify signal may be blocked with 834 * g_object_freeze_notify(). In this case, the signal emissions are queued 835 * and will be emitted (in reverse order) when g_object_thaw_notify() is 836 * called. 837 * 838 * Params: 839 * propertyName = the name of a property installed on the class of @object. 840 */ 841 public void notify(string propertyName) 842 { 843 g_object_notify(gObject, Str.toStringz(propertyName)); 844 } 845 846 /** 847 * Emits a "notify" signal for the property specified by @pspec on @object. 848 * 849 * This function omits the property name lookup, hence it is faster than 850 * g_object_notify(). 851 * 852 * One way to avoid using g_object_notify() from within the 853 * class that registered the properties, and using g_object_notify_by_pspec() 854 * instead, is to store the GParamSpec used with 855 * g_object_class_install_property() inside a static array, e.g.: 856 * 857 * |[<!-- language="C" --> 858 * enum 859 * { 860 * PROP_0, 861 * PROP_FOO, 862 * PROP_LAST 863 * }; 864 * 865 * static GParamSpec *properties[PROP_LAST]; 866 * 867 * static void 868 * my_object_class_init (MyObjectClass *klass) 869 * { 870 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo", 871 * 0, 100, 872 * 50, 873 * G_PARAM_READWRITE); 874 * g_object_class_install_property (gobject_class, 875 * PROP_FOO, 876 * properties[PROP_FOO]); 877 * } 878 * ]| 879 * 880 * and then notify a change on the "foo" property with: 881 * 882 * |[<!-- language="C" --> 883 * g_object_notify_by_pspec (self, properties[PROP_FOO]); 884 * ]| 885 * 886 * Params: 887 * pspec = the #GParamSpec of a property installed on the class of @object. 888 * 889 * Since: 2.26 890 */ 891 public void notifyByPspec(ParamSpec pspec) 892 { 893 g_object_notify_by_pspec(gObject, (pspec is null) ? null : pspec.getParamSpecStruct()); 894 } 895 896 /** 897 * Increases the reference count of @object. 898 * 899 * Return: the same @object 900 */ 901 public ObjectG doref() 902 { 903 auto p = g_object_ref(gObject); 904 905 if(p is null) 906 { 907 return null; 908 } 909 910 return ObjectG.getDObject!(ObjectG)(cast(GObject*) p); 911 } 912 913 /** 914 * Increase the reference count of @object, and possibly remove the 915 * [floating][floating-ref] reference, if @object has a floating reference. 916 * 917 * In other words, if the object is floating, then this call "assumes 918 * ownership" of the floating reference, converting it to a normal 919 * reference by clearing the floating flag while leaving the reference 920 * count unchanged. If the object is not floating, then this call 921 * adds a new normal reference increasing the reference count by one. 922 * 923 * Return: @object 924 * 925 * Since: 2.10 926 */ 927 public ObjectG refSink() 928 { 929 auto p = g_object_ref_sink(gObject); 930 931 if(p is null) 932 { 933 return null; 934 } 935 936 return ObjectG.getDObject!(ObjectG)(cast(GObject*) p); 937 } 938 939 /** 940 * Removes a reference added with g_object_add_toggle_ref(). The 941 * reference count of the object is decreased by one. 942 * 943 * Params: 944 * notify = a function to call when this reference is the 945 * last reference to the object, or is no longer 946 * the last reference. 947 * data = data to pass to @notify 948 * 949 * Since: 2.8 950 */ 951 public void removeToggleRef(GToggleNotify notify, void* data) 952 { 953 g_object_remove_toggle_ref(gObject, notify, data); 954 } 955 956 /** 957 * Removes a weak reference from @object that was previously added 958 * using g_object_add_weak_pointer(). The @weak_pointer_location has 959 * to match the one used with g_object_add_weak_pointer(). 960 * 961 * Params: 962 * weakPointerLocation = The memory address of a pointer. 963 */ 964 public void removeWeakPointer(ref void* weakPointerLocation) 965 { 966 g_object_remove_weak_pointer(gObject, &weakPointerLocation); 967 } 968 969 /** 970 * Compares the user data for the key @key on @object with 971 * @oldval, and if they are the same, replaces @oldval with 972 * @newval. 973 * 974 * This is like a typical atomic compare-and-exchange 975 * operation, for user data on an object. 976 * 977 * If the previous value was replaced then ownership of the 978 * old value (@oldval) is passed to the caller, including 979 * the registered destroy notify for it (passed out in @old_destroy). 980 * Its up to the caller to free this as he wishes, which may 981 * or may not include using @old_destroy as sometimes replacement 982 * should not destroy the object in the normal way. 983 * 984 * Params: 985 * key = a string, naming the user data pointer 986 * oldval = the old value to compare against 987 * newval = the new value 988 * destroy = a destroy notify for the new value 989 * oldDestroy = destroy notify for the existing value 990 * 991 * Return: %TRUE if the existing value for @key was replaced 992 * by @newval, %FALSE otherwise. 993 * 994 * Since: 2.34 995 */ 996 public bool replaceData(string key, void* oldval, void* newval, GDestroyNotify destroy, GDestroyNotify* oldDestroy) 997 { 998 return g_object_replace_data(gObject, Str.toStringz(key), oldval, newval, destroy, oldDestroy) != 0; 999 } 1000 1001 /** 1002 * Compares the user data for the key @quark on @object with 1003 * @oldval, and if they are the same, replaces @oldval with 1004 * @newval. 1005 * 1006 * This is like a typical atomic compare-and-exchange 1007 * operation, for user data on an object. 1008 * 1009 * If the previous value was replaced then ownership of the 1010 * old value (@oldval) is passed to the caller, including 1011 * the registered destroy notify for it (passed out in @old_destroy). 1012 * Its up to the caller to free this as he wishes, which may 1013 * or may not include using @old_destroy as sometimes replacement 1014 * should not destroy the object in the normal way. 1015 * 1016 * Params: 1017 * quark = a #GQuark, naming the user data pointer 1018 * oldval = the old value to compare against 1019 * newval = the new value 1020 * destroy = a destroy notify for the new value 1021 * oldDestroy = destroy notify for the existing value 1022 * 1023 * Return: %TRUE if the existing value for @quark was replaced 1024 * by @newval, %FALSE otherwise. 1025 * 1026 * Since: 2.34 1027 */ 1028 public bool replaceQdata(GQuark quark, void* oldval, void* newval, GDestroyNotify destroy, GDestroyNotify* oldDestroy) 1029 { 1030 return g_object_replace_qdata(gObject, quark, oldval, newval, destroy, oldDestroy) != 0; 1031 } 1032 1033 /** 1034 * Releases all references to other objects. This can be used to break 1035 * reference cycles. 1036 * 1037 * This function should only be called from object system implementations. 1038 */ 1039 public void runDispose() 1040 { 1041 g_object_run_dispose(gObject); 1042 } 1043 1044 /** 1045 * Each object carries around a table of associations from 1046 * strings to pointers. This function lets you set an association. 1047 * 1048 * If the object already had an association with that name, 1049 * the old association will be destroyed. 1050 * 1051 * Params: 1052 * key = name of the key 1053 * data = data to associate with that key 1054 */ 1055 public void setData(string key, void* data) 1056 { 1057 g_object_set_data(gObject, Str.toStringz(key), data); 1058 } 1059 1060 /** 1061 * Like g_object_set_data() except it adds notification 1062 * for when the association is destroyed, either by setting it 1063 * to a different value or when the object is destroyed. 1064 * 1065 * Note that the @destroy callback is not called if @data is %NULL. 1066 * 1067 * Params: 1068 * key = name of the key 1069 * data = data to associate with that key 1070 * destroy = function to call when the association is destroyed 1071 */ 1072 public void setDataFull(string key, void* data, GDestroyNotify destroy) 1073 { 1074 g_object_set_data_full(gObject, Str.toStringz(key), data, destroy); 1075 } 1076 1077 /** 1078 * Sets a property on an object. 1079 * 1080 * Params: 1081 * propertyName = the name of the property to set 1082 * value = the value 1083 */ 1084 public void setProperty(string propertyName, Value value) 1085 { 1086 g_object_set_property(gObject, Str.toStringz(propertyName), (value is null) ? null : value.getValueStruct()); 1087 } 1088 1089 /** 1090 * This sets an opaque, named pointer on an object. 1091 * The name is specified through a #GQuark (retrived e.g. via 1092 * g_quark_from_static_string()), and the pointer 1093 * can be gotten back from the @object with g_object_get_qdata() 1094 * until the @object is finalized. 1095 * Setting a previously set user data pointer, overrides (frees) 1096 * the old pointer set, using #NULL as pointer essentially 1097 * removes the data stored. 1098 * 1099 * Params: 1100 * quark = A #GQuark, naming the user data pointer 1101 * data = An opaque user data pointer 1102 */ 1103 public void setQdata(GQuark quark, void* data) 1104 { 1105 g_object_set_qdata(gObject, quark, data); 1106 } 1107 1108 /** 1109 * This function works like g_object_set_qdata(), but in addition, 1110 * a void (*destroy) (gpointer) function may be specified which is 1111 * called with @data as argument when the @object is finalized, or 1112 * the data is being overwritten by a call to g_object_set_qdata() 1113 * with the same @quark. 1114 * 1115 * Params: 1116 * quark = A #GQuark, naming the user data pointer 1117 * data = An opaque user data pointer 1118 * destroy = Function to invoke with @data as argument, when @data 1119 * needs to be freed 1120 */ 1121 public void setQdataFull(GQuark quark, void* data, GDestroyNotify destroy) 1122 { 1123 g_object_set_qdata_full(gObject, quark, data, destroy); 1124 } 1125 1126 /** 1127 * Sets properties on an object. 1128 * 1129 * Params: 1130 * firstPropertyName = name of the first property to set 1131 * varArgs = value for the first property, followed optionally by more 1132 * name/value pairs, followed by %NULL 1133 */ 1134 public void setValist(string firstPropertyName, void* varArgs) 1135 { 1136 g_object_set_valist(gObject, Str.toStringz(firstPropertyName), varArgs); 1137 } 1138 1139 /** 1140 * Remove a specified datum from the object's data associations, 1141 * without invoking the association's destroy handler. 1142 * 1143 * Params: 1144 * key = name of the key 1145 * 1146 * Return: the data if found, or %NULL if no such data exists. 1147 */ 1148 public void* stealData(string key) 1149 { 1150 return g_object_steal_data(gObject, Str.toStringz(key)); 1151 } 1152 1153 /** 1154 * This function gets back user data pointers stored via 1155 * g_object_set_qdata() and removes the @data from object 1156 * without invoking its destroy() function (if any was 1157 * set). 1158 * Usually, calling this function is only required to update 1159 * user data pointers with a destroy notifier, for example: 1160 * |[<!-- language="C" --> 1161 * void 1162 * object_add_to_user_list (GObject *object, 1163 * const gchar *new_string) 1164 * { 1165 * // the quark, naming the object data 1166 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list"); 1167 * // retrive the old string list 1168 * GList *list = g_object_steal_qdata (object, quark_string_list); 1169 * 1170 * // prepend new string 1171 * list = g_list_prepend (list, g_strdup (new_string)); 1172 * // this changed 'list', so we need to set it again 1173 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list); 1174 * } 1175 * static void 1176 * free_string_list (gpointer data) 1177 * { 1178 * GList *node, *list = data; 1179 * 1180 * for (node = list; node; node = node->next) 1181 * g_free (node->data); 1182 * g_list_free (list); 1183 * } 1184 * ]| 1185 * Using g_object_get_qdata() in the above example, instead of 1186 * g_object_steal_qdata() would have left the destroy function set, 1187 * and thus the partial string list would have been freed upon 1188 * g_object_set_qdata_full(). 1189 * 1190 * Params: 1191 * quark = A #GQuark, naming the user data pointer 1192 * 1193 * Return: The user data pointer set, or %NULL 1194 */ 1195 public void* stealQdata(GQuark quark) 1196 { 1197 return g_object_steal_qdata(gObject, quark); 1198 } 1199 1200 /** 1201 * Reverts the effect of a previous call to 1202 * g_object_freeze_notify(). The freeze count is decreased on @object 1203 * and when it reaches zero, queued "notify" signals are emitted. 1204 * 1205 * Duplicate notifications for each property are squashed so that at most one 1206 * #GObject::notify signal is emitted for each property, in the reverse order 1207 * in which they have been queued. 1208 * 1209 * It is an error to call this function when the freeze count is zero. 1210 */ 1211 public void thawNotify() 1212 { 1213 g_object_thaw_notify(gObject); 1214 } 1215 1216 /** 1217 * Decreases the reference count of @object. When its reference count 1218 * drops to 0, the object is finalized (i.e. its memory is freed). 1219 * 1220 * If the pointer to the #GObject may be reused in future (for example, if it is 1221 * an instance variable of another object), it is recommended to clear the 1222 * pointer to %NULL rather than retain a dangling pointer to a potentially 1223 * invalid #GObject instance. Use g_clear_object() for this. 1224 */ 1225 public void unref() 1226 { 1227 g_object_unref(gObject); 1228 } 1229 1230 /** 1231 * This function essentially limits the life time of the @closure to 1232 * the life time of the object. That is, when the object is finalized, 1233 * the @closure is invalidated by calling g_closure_invalidate() on 1234 * it, in order to prevent invocations of the closure with a finalized 1235 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are 1236 * added as marshal guards to the @closure, to ensure that an extra 1237 * reference count is held on @object during invocation of the 1238 * @closure. Usually, this function will be called on closures that 1239 * use this @object as closure data. 1240 * 1241 * Params: 1242 * closure = GClosure to watch 1243 */ 1244 public void watchClosure(Closure closure) 1245 { 1246 g_object_watch_closure(gObject, (closure is null) ? null : closure.getClosureStruct()); 1247 } 1248 1249 /** 1250 * Adds a weak reference callback to an object. Weak references are 1251 * used for notification when an object is finalized. They are called 1252 * "weak references" because they allow you to safely hold a pointer 1253 * to an object without calling g_object_ref() (g_object_ref() adds a 1254 * strong reference, that is, forces the object to stay alive). 1255 * 1256 * Note that the weak references created by this method are not 1257 * thread-safe: they cannot safely be used in one thread if the 1258 * object's last g_object_unref() might happen in another thread. 1259 * Use #GWeakRef if thread-safety is required. 1260 * 1261 * Params: 1262 * notify = callback to invoke before the object is freed 1263 * data = extra data to pass to notify 1264 */ 1265 public void weakRef(GWeakNotify notify, void* data) 1266 { 1267 g_object_weak_ref(gObject, notify, data); 1268 } 1269 1270 /** 1271 * Removes a weak reference callback to an object. 1272 * 1273 * Params: 1274 * notify = callback to search for 1275 * data = data to search for 1276 */ 1277 public void weakUnref(GWeakNotify notify, void* data) 1278 { 1279 g_object_weak_unref(gObject, notify, data); 1280 } 1281 1282 /** 1283 * Clears a reference to a #GObject. 1284 * 1285 * @object_ptr must not be %NULL. 1286 * 1287 * If the reference is %NULL then this function does nothing. 1288 * Otherwise, the reference count of the object is decreased and the 1289 * pointer is set to %NULL. 1290 * 1291 * A macro is also included that allows this function to be used without 1292 * pointer casts. 1293 * 1294 * Params: 1295 * objectPtr = a pointer to a #GObject reference 1296 * 1297 * Since: 2.28 1298 */ 1299 public static void clearObject(ref ObjectG objectPtr) 1300 { 1301 GObject* outobjectPtr = objectPtr.getObjectGStruct(); 1302 1303 g_clear_object(&outobjectPtr); 1304 1305 objectPtr = ObjectG.getDObject!(ObjectG)(outobjectPtr); 1306 } 1307 }