2
* Copyright (C) 2003-2004 by the gtk2-perl team (see the file AUTHORS for
5
* This library is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU Library General Public License as published by
7
* the Free Software Foundation; either version 2.1 of the License, or (at your
8
* option) any later version.
10
* This library is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13
* License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; if not, write to the Free Software Foundation,
17
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA.
19
* $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Glib/GObject.xs,v 1.37.2.1 2004/03/17 02:56:07 muppetman Exp $
23
* the POD directives in here will be stripped by xsubpp before compilation,
24
* and are intended to be extracted by podselect when creating xs api
25
* reference documentation. pod must NOT appear within C comments, because
26
* it gets replaced by a comment that says "embedded pod stripped".
31
To deal with the intricate interaction of the different reference-counting semantics of Perl objects versus GObjects, the bindings create a combined PerlObject+GObject, with the GObject's pointer in magic attached to the Perl object, and the Perl object's pointer in the GObject's user data. Thus it's not really a "wrapper", but we refer to it as one, because "combined Perl object + GObject" is a cumbersome and confusing mouthful.
33
GObjects are represented as blessed hash references. The GObject user data mechanism is not typesafe, and thus is used only for unsigned integer values; the Perl-level hash is available for any type of user data. The combined nature of the wrapper means that data stored in the hash will stick around as long as the object is alive.
35
Since the C pointer is stored in attached magic, the C pointer is not available to the Perl developer via the hash object, so there's no need to worry about breaking it from perl.
37
Propers go to Marc Lehmann for dreaming most of this up.
45
typedef struct _ClassInfo ClassInfo;
46
typedef struct _SinkFunc SinkFunc;
56
GPerlObjectSinkFunc func;
59
static GHashTable * types_by_type = NULL;
60
static GHashTable * types_by_package = NULL;
62
/* store outside of the class info maps any options we expect to be sparse;
63
* this will save us a fair amount of space. */
64
static GHashTable * nowarn_by_type = NULL;
65
static GArray * sink_funcs = NULL;
67
static GQuark wrapper_quark; /* this quark stores the object's wrapper sv */
70
/* thread safety locks for the modifiables above */
71
G_LOCK_DEFINE_STATIC (types_by_type);
72
G_LOCK_DEFINE_STATIC (types_by_package);
73
G_LOCK_DEFINE_STATIC (nowarn_by_type);
74
G_LOCK_DEFINE_STATIC (sink_funcs);
78
class_info_new (GType gtype,
81
ClassInfo * class_info;
83
class_info = g_new0 (ClassInfo, 1);
84
class_info->gtype = gtype;
85
class_info->package = g_strdup (package);
86
/* Taking a reference to the stash is not really correct,
87
* as the stash might be replaced, giving us the wrong stash.
88
* Fortunately doing this is not documented nor really supported,
89
* nor does perl cope with it gracefully. So this just shields us
91
class_info->stash = (HV *)SvREFCNT_inc (gv_stashpv (package, 1));
97
class_info_destroy (ClassInfo * class_info)
100
SvREFCNT_dec (class_info->stash);
101
g_free (class_info->package);
107
=item void gperl_register_object (GType gtype, const char * package)
109
tell the GPerl type subsystem what Perl package corresponds with a given
110
GObject by GType. automagically sets up @I<package>::ISA for you.
112
note that @ISA will not be created for gtype until gtype's parent has
113
been registered. if you are experiencing strange problems with a class'
114
@ISA not being set up, change the order in which you register them.
119
gperl_register_object (GType gtype,
120
const char * package)
123
ClassInfo * class_info;
125
G_LOCK (types_by_type);
126
G_LOCK (types_by_package);
128
if (!types_by_type) {
129
/* we put the same data pointer into each hash table, so we
130
* must only associate the destructor with one of them.
131
* also, for the string-keyed hashes, the keys will be
132
* destroyed by the ClassInfo destructor, so we don't need
133
* a key_destroy_func. */
134
types_by_type = g_hash_table_new_full (g_direct_hash,
139
types_by_package = g_hash_table_new_full (g_str_hash,
144
class_info = class_info_new (gtype, package);
145
g_hash_table_insert (types_by_type,
146
(gpointer) class_info->gtype, class_info);
147
g_hash_table_insert (types_by_package, class_info->package, class_info);
148
/* warn ("registered class %s to package %s\n", class_info->class, class_info->package); */
150
parent_type = g_type_parent (gtype);
151
if (parent_type != 0) {
152
static GList * pending_isa = NULL;
156
* add this class to the list of pending ISA creations.
158
* "list of pending ISA creations?!?" you ask...
159
* to minimize the possible errors in setting up the class
160
* relationships, we only require the caller to provide
161
* the GType and name of the corresponding package; we don't
162
* also require the name of the parent class' package, since
163
* getting the parent GType is more likely to be error-free.
164
* (the developer setting up the registrations may have bad
165
* information, for example.)
167
* the nasty side effect is that the parent GType may not
168
* yet have been registered at the time the child type is
169
* registered. so, we keep a list of classes for which
170
* ISA has not yet been set up, and each time we run through
171
* this function, we'll try to eliminate as many as possible.
173
* since this one is fresh we append it to the list, so that
174
* we have a chance of registering its parent first.
176
pending_isa = g_list_append (pending_isa, class_info);
178
/* handle whatever pending requests we can */
179
/* not a for loop, because we're modifying the list as we go */
182
ClassInfo * parent_class_info;
184
/* NOTE: reusing class_info --- it's not the same as
185
* it was at the top of the function */
186
class_info = (ClassInfo*)(i->data);
188
parent_class_info = (ClassInfo *)
189
g_hash_table_lookup (types_by_type,
190
(gpointer) g_type_parent
191
(class_info->gtype));
193
if (parent_class_info) {
194
gperl_set_isa (class_info->package,
195
parent_class_info->package);
196
pending_isa = g_list_remove (pending_isa,
198
/* go back to the beginning, in case we
199
* just registered one that is the base
200
* of several items earlier in the list.
201
* besides, it's dangerous to remove items
202
* while iterating... */
211
G_UNLOCK (types_by_type);
212
G_UNLOCK (types_by_package);
216
=item void gperl_register_sink_func (GType gtype, GPerlObjectSinkFunc func)
218
Tell gperl_new_object() to use I<func> to claim ownership of objects derived
221
gperl_new_object() always refs a GObject when wrapping it for the first time.
222
To have the Perl wrapper claim ownership of a GObject as part of
223
gperl_new_object(), you unref the object after ref'ing it. however, different
224
GObject subclasses have different ways to claim ownership; for example,
225
GtkObject simply requires you to call gtk_object_sink(). To make this concept
226
generic, this function allows you to register a function to be called when then
227
wrapper should claim ownership of the object. The I<func> registered for a
228
given I<type> will be called on any object for which C<< g_type_isa
229
(G_TYPE_OBJECT (object), type) >> succeeds.
231
If no sinkfunc is found for an object, g_object_unref() will be used.
233
Even though GObjects don't need sink funcs, we need to have them in Glib
234
as a hook for upstream objects. If we create a GtkObject (or any
235
other type of object which uses a different way to claim ownership) via
236
Glib::Object->new, any upstream wrappers, such as gtk2perl_new_object(), will
237
B<not> be called. Having a sink func facility down here enables us always to
242
* this stuff is directly inspired by pygtk. i didn't actually copy
243
* and paste the code, but it sure looks like i did, down to the names.
244
* hey, they were the obvious names!
246
* for the record, i think this is a rather dodgy way to do sink funcs
247
* --- it presumes that you'll find the right one first; i prepend new
248
* registrees in the hopes that this will work out, but nothing guarantees
249
* that this will work. to do it right, the wrappers need to have
250
* some form of inherited vtable or something... but i've had enough
251
* problems just getting the object caching working, so i can't really
252
* mess with that right now.
255
gperl_register_sink_func (GType gtype,
256
GPerlObjectSinkFunc func)
263
sink_funcs = g_array_new (FALSE, FALSE, sizeof (SinkFunc));
266
g_array_prepend_val (sink_funcs, sf);
268
G_UNLOCK (sink_funcs);
272
* helper for gperl_new_object; do whatever you have to do to this
273
* object to ensure that the calling code now owns the object. assumes
274
* the object has already been ref'd once. to do this, we look up the
275
* proper sink func; if none has been registered for this type, then
276
* just call g_object_unref.
279
gperl_object_take_ownership (GObject * object)
285
for (i = 0 ; i < sink_funcs->len ; i++)
286
if (g_type_is_a (G_OBJECT_TYPE (object),
287
g_array_index (sink_funcs,
288
SinkFunc, i).gtype)) {
289
g_array_index (sink_funcs,
290
SinkFunc, i).func (object);
291
G_UNLOCK (sink_funcs);
296
G_UNLOCK (sink_funcs);
298
g_object_unref (object);
302
=item void gperl_object_set_no_warn_unreg_subclass (GType gtype, gboolean nowarn)
304
how's that for a long and supposedly self-documenting function name!
305
(sorry...). basically, it does just as it says -- if I<nowarn> is true,
306
do not spew a warning if a GType derived from I<gtype> is not registered
307
with the bindings' type system. this is important for things like
308
GtkStyles (unregistered subclasses come from theme engines) and GdkGCs
309
(unregistered subclasses come from various gdk backends) for which it's not
310
possible or practical to force the registration of the classes. in
311
general, we want to warn about the unregistered types because it may mean
312
that a developer has forgotten something.
314
note: this assumes I<gtype> has already been registered with
315
gperl_register_object().
319
gperl_object_set_no_warn_unreg_subclass (GType gtype,
322
G_LOCK (nowarn_by_type);
324
if (!nowarn_by_type) {
327
nowarn_by_type = g_hash_table_new (g_direct_hash,
330
g_hash_table_insert (nowarn_by_type,
332
GINT_TO_POINTER (nowarn));
334
G_UNLOCK (nowarn_by_type);
338
gperl_object_get_no_warn_unreg_subclass (GType gtype)
342
G_LOCK (nowarn_by_type);
347
result = GPOINTER_TO_INT
348
(g_hash_table_lookup (nowarn_by_type,
351
G_UNLOCK (nowarn_by_type);
357
=item const char * gperl_object_package_from_type (GType gtype)
359
get the package corresponding to I<gtype>; returns NULL if I<gtype>
364
gperl_object_package_from_type (GType gtype)
367
ClassInfo * class_info;
369
G_LOCK (types_by_type);
371
class_info = (ClassInfo *)
372
g_hash_table_lookup (types_by_type, (gpointer) gtype);
374
G_UNLOCK (types_by_type);
377
return class_info->package;
381
croak ("internal problem: gperl_object_package_from_type "
382
"called before any classes were registered");
383
return NULL; /* not reached */
387
=item HV * gperl_object_stash_from_type (GType gtype)
389
Get the stash corresponding to I<gtype>; returns NULL if I<gtype> is
390
not registered. The stash is useful for C<bless>ing.
395
gperl_object_stash_from_type (GType gtype)
398
ClassInfo * class_info;
400
G_LOCK (types_by_type);
402
class_info = (ClassInfo *)
403
g_hash_table_lookup (types_by_type, (gpointer) gtype);
405
G_UNLOCK (types_by_type);
408
return class_info->stash;
412
croak ("internal problem: gperl_object_stash_from_type "
413
"called before any classes were registered");
414
return NULL; /* not reached */
418
=item GType gperl_object_type_from_package (const char * package)
420
Inverse of gperl_object_package_from_type(), returns 0 if I<package>
426
gperl_object_type_from_package (const char * package)
428
if (types_by_package) {
429
ClassInfo * class_info;
431
G_LOCK (types_by_package);
433
class_info = (ClassInfo *)
434
g_hash_table_lookup (types_by_package, package);
436
G_UNLOCK (types_by_package);
439
return class_info->gtype;
443
croak ("internal problem: gperl_object_type_from_package "
444
"called before any classes were registered");
445
return 0; /* not reached */
449
* this function is called whenever the gobject gets destroyed. this only
450
* happens if the perl object is no longer referenced anywhere else, so
451
* put it to final rest here.
454
gobject_destroy_wrapper (SV *obj)
456
if (PL_in_clean_objs)
460
warn ("gobject_destroy_wrapper (%p)[%d]", obj, SvREFCNT (obj));
462
sv_unmagic (obj, PERL_MAGIC_ext);
464
/* we might want to optimize away the call to DESTROY here for non-perl classes. */
469
=item SV * gperl_new_object (GObject * object, gboolean own)
471
Use this function to get the perl part of a GObject. If I<object>
472
has never been seen by perl before, a new, empty perl object will
473
be created and added to a private key under I<object>'s qdata. If
474
I<object> already has a perl part, a new reference to it will be
475
created. The gobject + perl object together form a combined object that
476
is properly refcounted, i.e. both parts will stay alive as long as at
477
least one of them is alive, and only when both perl object and gobject are
478
no longer referenced will both be freed.
480
The perl object will be blessed into the package corresponding to the GType
481
returned by calling G_OBJECT_TYPE() on I<object>; if that class has not
482
been registered via gperl_register_object(), this function will emit a
483
warning to that effect (with warn()), and attempt to bless it into the
484
first known class in the object's ancestry. Since Glib::Object is
485
already registered, you'll get a Glib::Object if you are lazy, and thus
486
this function can fail only if I<object> isn't descended from GObject,
487
in which case it croaks. (In reality, if you pass a non-GObject to this
488
function, you'll be lucky if you don't get a segfault, as there's not
489
really a way to trap that.) In practice these warnings can be unavoidable,
490
so you can use gperl_object_set_no_warn_unreg_subclass() to quell them
491
on a class-by-class basis.
493
However, when perl code is calling a GObject constructor (any function
494
which returns a new GObject), call gperl_new_object() with I<own> set to
495
%TRUE; this will cause the first matching sink function to be called
496
on the GObject to claim ownership of that object, so that it will be
497
destroyed when the perl object goes out of scope. The default sink func
498
is g_object_unref(); other types should supply the proper function;
499
e.g., GtkObject should use gtk_object_sink() here.
501
Returns the blessed perl object, or #&PL_sv_undef if object was #NULL.
506
gperl_new_object (GObject * object,
512
/* take the easy way out if we can */
515
warn ("gperl_new_object (NULL) => undef");
520
if (!G_IS_OBJECT (object))
521
croak ("object %p is not really a GObject", object);
523
/* fetch existing wrapper_data */
524
obj = (SV *)g_object_get_qdata (object, wrapper_quark);
527
/* create the perl object */
528
GType gtype = G_OBJECT_TYPE (object);
530
HV *stash = gperl_object_stash_from_type (gtype);
532
/* there are many possible cases in which we may be asked to
533
* create a wrapper for objects whose GTypes are not
534
* registered with us; we need to find the first known class
535
* and use that. see the docs for
536
* gperl_object_set_no_warn_unreg_subclass for more info. */
538
/* walk the anscestry to the first known GType.
539
* since GObject is registered to Glib::Object,
540
* this will always succeed. */
541
GType parent = gtype;
542
while (stash == NULL) {
543
parent = g_type_parent (parent);
544
stash = gperl_object_stash_from_type (parent);
546
if (!gperl_object_get_no_warn_unreg_subclass (parent))
547
warn ("GType '%s' is not registered with "
548
"GPerl; representing this object as "
549
"first known parent type '%s' instead",
551
g_type_name (parent));
555
* Create the "object", a hash.
557
* This does not need to be a HV, the only problem is finding
558
* out what to use, and HV is certainly the way to go for any
562
/* this increases the combined object's refcount. */
563
obj = (SV *)newHV ();
565
sv_magic (obj, 0, PERL_MAGIC_ext, (const char *)object, 0);
567
/* this is the one refcount that represents all non-zero perl
568
* refcounts. it is just temporarily given to the gobject,
569
* DESTROY takes it back again. this effectively increases
570
* the combined refcount by one. */
571
g_object_ref (object);
573
/* create the wrapper to return, the _noinc decreases the
574
* combined refcount by one. */
575
sv = newRV_noinc (obj);
577
/* bless into the package */
578
sv_bless (sv, stash);
580
/* attach it to the gobject */
581
g_object_set_qdata_full (object,
584
(GDestroyNotify)gobject_destroy_wrapper);
586
/* the noinc above is actually the trick, as it leaves the
587
* attached object's refcount artificially one too low,
588
* so DESTROY gets called when all handed-out refs are gone
589
* and we still have the object attached. DESTROY will
590
* then borrow the ref added by g_object_ref back, and
591
* thus will eventually trigger gobject destruction, which
592
* in turn will trigger perl wrapper destruction. */
595
warn ("gperl_new_object%d %s(%p)[%d] => %s (%p) (NEW)", own,
596
G_OBJECT_TYPE_NAME (object), object, object->ref_count,
597
gperl_object_package_from_type (G_OBJECT_TYPE (object)),
601
/* create the wrapper to return, increases the combined
602
* refcount by one. */
603
sv = newRV_inc (obj);
605
/* Now we need to handle the case of a gobject that has
606
* been DESTROYed but gets "revived" later. This operation
607
* does not alter the refcount of the combined object.
608
* This can only happen if the call with own is not
609
* the first call. Unfortunately, this is the common case
610
* for gobjectclasses implemented in perl.
612
if (object->ref_count == 1 && own) {
613
g_object_ref (object);
620
warn ("gperl_new_object%d %s(%p)[%d] => %s (%p)[%d] (PRE-OWN)", own,
621
G_OBJECT_TYPE_NAME (object), object, object->ref_count,
622
gperl_object_package_from_type (G_OBJECT_TYPE (object)),
623
SvRV (sv), SvREFCNT (SvRV (sv)));
626
gperl_object_take_ownership (object);
632
=item GObject * gperl_get_object (SV * sv)
634
retrieve the GObject pointer from a Perl object. Returns NULL if I<sv> is not
637
Note, this one is not safe -- in general you want to use
638
gperl_get_object_check().
643
gperl_get_object (SV * sv)
647
if (!sv || !SvOK (sv) || !SvROK (sv) || !(mg = mg_find (SvRV (sv), PERL_MAGIC_ext)))
649
return (GObject *) mg->mg_ptr;
653
=item GObject * gperl_get_object_check (SV * sv, GType gtype);
655
croaks if I<sv> is undef or is not blessed into the package corresponding
656
to I<gtype>. use this for bringing parameters into xsubs from perl.
657
Returns the same as gperl_get_object() (provided it doesn't croak first).
662
gperl_get_object_check (SV * sv,
665
const char * package;
666
package = gperl_object_package_from_type (gtype);
668
croak ("INTERNAL: GType %s (%d) is not registered with GPerl!",
669
g_type_name (gtype), gtype);
670
if (!sv || !SvROK (sv) || !sv_derived_from (sv, package))
671
croak ("variable is not of type %s", package);
672
return gperl_get_object (sv);
676
=item SV * gperl_object_check_type (SV * sv, GType gtype)
678
Essentially the same as gperl_get_object_check().
680
FIXME this croaks if the types aren't compatible, but it would be useful if it just return FALSE instead.
685
gperl_object_check_type (SV * sv,
688
gperl_get_object_check (sv, gtype);
694
/* helper for g_object_[gs]et_parameter */
696
init_property_value (GObject * object,
701
pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object),
704
const char * classname =
705
gperl_object_package_from_type (G_OBJECT_TYPE (object));
707
classname = G_OBJECT_TYPE_NAME (object);
708
croak ("type %s does not support property '%s'",
711
g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
715
=item typedef GObject GObject_noinc
717
=item typedef GObject GObject_ornull
719
=item newSVGObject(obj)
721
=item newSVGObject_noinc(obj)
725
=item SvGObject_ornull(sv)
732
MODULE = Glib::Object PACKAGE = Glib::Object PREFIX = g_object_
734
=for object Glib::Object Bindings for GObject
737
=for position DESCRIPTION
741
GObject is the base object class provided by the gobject library. It provides
742
object properties with a notification system, and emittable signals.
744
Glib::Object is the corresponding Perl object class. Glib::Objects are
745
represented by blessed hash references, with a magical connection to the
751
gperl_register_object (G_TYPE_OBJECT, "Glib::Object");
752
wrapper_quark = g_quark_from_static_string ("Perl-wrapper-object");
758
GObject *object = gperl_get_object (sv);
760
if (!object) /* Happens on object destruction. */
763
warn ("DESTROY< (%p)[%d] => %s (%p)[%d]",
764
object, object->ref_count,
765
gperl_object_package_from_type (G_OBJECT_TYPE (object)),
766
sv, SvREFCNT (SvRV(sv)));
768
/* gobject object still exists, so take back the refcount we lend it. */
769
/* this operation does NOT change the refcount of the combined object. */
771
if (PL_in_clean_objs) {
772
/* be careful during global destruction. basically,
773
* don't bother, since refcounting is no longer meaningful. */
774
sv_unmagic (SvRV (sv), PERL_MAGIC_ext);
776
g_object_steal_qdata (object, wrapper_quark);
778
SvREFCNT_inc (SvRV (sv));
780
g_object_unref (object);
782
warn ("DESTROY> (%p)[%d] => %s (%p)[%d]",
783
object, object->ref_count,
784
gperl_object_package_from_type (G_OBJECT_TYPE (object)),
785
sv, SvREFCNT (SvRV(sv)));
791
=for signature object = $class->new (...)
793
=for arg ... of key/value pairs, property values to set on creation
795
Instantiate a Glib::Object of type I<$class>. Any key/value pairs in
796
I<...> are used to set properties on the new object; see C<set>.
797
This is designed to be inherited by Perl-derived subclasses (see
798
L<Glib::Object::Subclass>), but you can actually use it to create
799
any GObject-derived type.
803
g_object_new (class, ...)
807
GParameter * params = NULL;
810
GObjectClass *oclass = NULL;
813
object_type = gperl_object_type_from_package (class);
815
croak ("%s is not registered with gperl as an object type",
817
if (G_TYPE_IS_ABSTRACT (object_type))
818
croak ("cannot create instance of abstract (non-instantiatable)"
819
" type `%s'", g_type_name (object_type));
820
if (items > FIRST_ARG) {
822
if (NULL == (oclass = g_type_class_ref (object_type)))
823
croak ("could not get a reference to type class");
824
n_params = (items - FIRST_ARG) / 2;
825
params = g_new0 (GParameter, n_params);
826
for (i = 0 ; i < n_params ; i++) {
827
const char * key = SvPV_nolen (ST (FIRST_ARG+i*2+0));
829
pspec = g_object_class_find_property (oclass, key);
833
for (j = 0 ; j < i ; j++)
834
g_value_unset (¶ms[j].value);
837
croak ("type %s does not support property '%s'",
840
g_value_init (¶ms[i].value,
841
G_PARAM_SPEC_VALUE_TYPE (pspec));
842
/* note: this croaks if there is a problem. this is
843
* usually the right thing to do, because if it
844
* doesn't know how to convert the value, then there's
845
* something seriously wrong; however, it means that
846
* if there is a problem, all non-trivial values we've
847
* converted will be leaked. */
848
gperl_value_from_sv (¶ms[i].value,
849
ST (FIRST_ARG+i*2+1));
850
params[i].name = key; /* will be valid until this
851
* xsub is finished */
856
object = g_object_newv (object_type, n_params, params);
858
/* this wrapper *must* own this object!
859
* because we've been through initialization, the perl object
860
* will already exist at this point --- but this still causes
861
* gperl_object_take_ownership to be called. */
862
RETVAL = gperl_new_object (object, TRUE);
866
for (i = 0 ; i < n_params ; i++)
867
g_value_unset (¶ms[i].value);
871
g_type_class_unref (oclass);
876
=for apidoc Glib::Object::get
877
=for arg ... (list) list of property names
879
Fetch and return the values for the object properties named in I<...>.
883
=for apidoc Glib::Object::get_property
884
=for arg ... (__hide__)
891
g_object_get (object, ...)
894
Glib::Object::get = 0
895
Glib::Object::get_property = 1
900
PERL_UNUSED_VAR (ix);
901
EXTEND (SP, items-1);
902
for (i = 1; i < items; i++) {
903
char *name = SvPV_nolen (ST (i));
904
init_property_value (object, name, &value);
905
g_object_get_property (object, name, &value);
906
PUSHs (sv_2mortal (gperl_sv_from_value (&value)));
907
g_value_unset (&value);
911
=for apidoc Glib::Object::set
912
=for signature $object->set (key => $value, ...)
913
=for arg ... (key/value pairs)
915
Set object properties.
919
=for apidoc Glib::Object::set_property
920
=for signature $object->set_property (key => $value, ...)
921
=for arg ... (__hide__)
928
g_object_set (object, ...)
931
Glib::Object::set = 0
932
Glib::Object::set_property = 1
937
PERL_UNUSED_VAR (ix);
938
if (0 != ((items - 1) % 2))
939
croak ("set method expects name => value pairs "
940
"(odd number of arguments detected)");
942
for (i = 1; i < items; i += 2) {
943
char *name = SvPV_nolen (ST (i));
944
SV *newval = ST (i + 1);
946
init_property_value (object, name, &value);
947
gperl_value_from_sv (&value, newval);
948
g_object_set_property (object, name, &value);
949
g_value_unset (&value);
955
Stops emission of "notify" signals on I<$object>. The signals are queued
956
until C<thaw_notify> is called on I<$object>.
959
void g_object_freeze_notify (GObject * object)
963
Reverts the effect of a previous call to C<freeze_notify>. This causes all
964
queued "notify" signals on I<$object> to be emitted.
967
void g_object_thaw_notify (GObject * object)
971
List all the object properties for I<$object_or_class_name>; returns them as
972
a list of hashes, containing these keys:
988
g_object_list_properties (object_or_class_name)
989
SV * object_or_class_name
993
guint n_props = 0, i;
995
if (object_or_class_name &&
996
SvOK (object_or_class_name) &&
997
SvROK (object_or_class_name)) {
998
GObject * object = SvGObject (object_or_class_name);
1000
croak ("wha? NULL object in list_properties");
1001
type = G_OBJECT_TYPE (object);
1003
type = gperl_object_type_from_package
1004
(SvPV_nolen (object_or_class_name));
1006
croak ("package %s is not registered with GPerl",
1007
SvPV_nolen (object_or_class_name));
1009
if (G_TYPE_IS_OBJECT (type))
1011
/* classes registered by perl are kept alive by the bindings.
1012
* those coming straight from C are not. if we had an actual
1013
* object, the class will be alive, but if we just had a
1014
* package, the class may not exist yet. thus, we'll have to
1015
* do an honest ref here, rather than a peek.
1017
GObjectClass * object_class = g_type_class_ref (type);
1018
props = g_object_class_list_properties (object_class, &n_props);
1019
g_type_class_unref (object_class);
1021
#if GLIB_CHECK_VERSION(2,4,0)
1022
else if (G_TYPE_IS_INTERFACE (type))
1024
gpointer iface = g_type_default_interface_ref (type);
1025
props = g_object_interface_list_properties (iface, &n_props);
1026
g_type_default_interface_unref (iface);
1032
warn ("list_properties: %d properties\n", n_props);
1034
for (i = 0; i < n_props; i++) {
1036
HV * property = newHV ();
1038
hv_store (property, "name", 4,
1039
newSVpv (g_param_spec_get_name (props[i]), 0), 0);
1041
/* map type names to package names, if possible */
1042
pv = gperl_package_from_type (props[i]->value_type);
1043
if (!pv) pv = g_type_name (props[i]->value_type);
1044
hv_store (property, "type", 4, newSVpv (pv, 0), 0);
1046
pv = gperl_package_from_type (props[i]->owner_type);
1047
if (!pv) pv = g_type_name (props[i]->owner_type);
1048
hv_store (property, "owner_type", 10, newSVpv (pv, 0), 0);
1050
/* this one can be NULL, it seems */
1051
pv = g_param_spec_get_blurb (props[i]);
1052
if (pv) hv_store (property, "descr", 5, newSVpv (pv, 0), 0);
1053
hv_store (property, "flags", 5, newSVGParamFlags (props[i]->flags), 0) ;
1055
XPUSHs (sv_2mortal (newRV_noinc((SV*)property)));
1062
GObject provides an arbitrary data mechanism that assigns unsigned integers
1063
to key names. Functionality overlaps with the hash used as the Perl object
1064
instance, so we strongly recommend you use hash keys for your data storage.
1065
The GObject data values cannot store type information, so they are not safe
1066
to use for anything but integer values, and you really should use this method
1067
only if you know what you are doing.
1071
g_object_set_data (object, key, data)
1076
if (SvROK (data) || !SvIOK (data))
1077
croak ("set_data only sets unsigned integers, use"
1078
" a key in the object hash for anything else");
1079
g_object_set_data (object, key, INT2PTR (gpointer, SvUV (data)));
1084
Fetch the integer stored under the object data key I<$key>. These values do not
1085
have types; type conversions must be done manually. See C<set_data>.
1089
g_object_get_data (object, key)
1093
RETVAL = PTR2UV (g_object_get_data (object, key));
1099
### rudimentary support for foreign objects.
1102
=for apidoc Glib::Object::new_from_pointer
1104
=for arg pointer (unsigned) a C pointer value as an integer.
1106
=for arg noinc (boolean) if true, do not increase the GObject's reference count when creating the Perl wrapper. this typically means that when the Perl wrapper will own the object. in general you don't want to do that, so the default is false.
1108
Create a Perl Glib::Object reference for the C object pointed to by I<$pointer>.
1109
You should need this I<very> rarely; it's intended to support foreign objects.
1111
NOTE: the cast from arbitrary integer to GObject may result in a core dump without
1112
warning, because the type-checking macro G_OBJECT() attempts to dereference the
1113
pointer to find a GTypeClass structure, and there is no portable way to validate
1118
new_from_pointer (class, pointer, noinc=FALSE)
1122
RETVAL = gperl_new_object (G_OBJECT (pointer), noinc);
1129
Complement of C<new_from_pointer>.
1133
get_pointer (object)
1142
=for arg all if FALSE (or omitted) tie only properties for this object's class, if TRUE tie the properties of this and all parent classes.
1144
A special method avaiable to Glib::Object derivatives, it uses perl's tie
1145
facilities to associate hash keys with the properties of the object. For
1148
$button->tie_properties;
1149
# equivilent to $button->set (label => 'Hello World');
1150
$button->{label} = 'Hello World';
1151
print "the label is: ".$button->{label}."\n";
1153
Attempts to write to read-only properties will croak, reading a write-only
1154
property will return '[write-only]'.
1156
Care must be taken when using tie_properties with objects of types created with
1157
Glib::Object::Subclass as there may be clashes with existing hash keys that
1158
could cause infinite loops. The solution is to use custom property get/set
1159
functions to alter the storage locations of the properties.
1162
tie_properties (GObject * object, gboolean all=FALSE)