Arguments in GObject-based object constructor

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Arguments in GObject-based object constructor

Jacob Kroon
Lets say I've written an abstract class Foo (a class that is not
supposed to be instantiated), and a
class that inherits from Foo called Bar, which is instantiable. Foo
takes 2 constructor arguments/properties
"x" and "y". Bar:s constructor takes the samt arguments. In order to
initialize some of Bar:s attributes,
it needs to know both "x" and "y". In the bar_init() function I can't
get the "x" and "y" properties,
because they arent set yet, so where do I initialize the attributes?
Should I hook into the get/set property of
both "x" and "y", and once both are set (check with some boolean
variables), I can initialize them in bar_set_property() ?
That way sounds a little too complicated so I think I'm missing something.

The GObject documentation states:

static void
maman_bar_init (GTypeInstance   *instance,
                gpointer         g_class)
{
  MamanBar *self = (MamanBar *)instance;
  self->private = g_new0 (MamanBarPrivate, 1);

  /* initialize all public and private members to reasonable default values. */
  /* If you need specific consruction properties to complete initialization,
   * delay initialization completion until the property is set.
   */
}

What does it mean by "delay initialization completion until the property
is set" ?

Regards Jacob Kroon
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Arguments in GObject-based object constructor

Tristan Van Berkom
Jacob Kroon wrote:
[...]
> What does it mean by "delay initialization completion until the property
> is set" ?

You can override the "constructor" in your class_init function

see: le-hacker.org/papers/gobject/

and read about the constructor, it is important that you chain up
to the real gobject constructor in your constructor, after the
real constructor returns, your my_instance_init() function will
have been called and all construct properties will have been
applied.

Cheers,
                         -Tristan
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Arguments in GObject-based object constructor

Roger Leigh
In reply to this post by Jacob Kroon
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jacob Kroon <[hidden email]> writes:

> Lets say I've written an abstract class Foo (a class that is not
> supposed to be instantiated), and a class that inherits from Foo
> called Bar, which is instantiable. Foo takes 2 constructor
> arguments/properties "x" and "y". Bar:s constructor takes the samt
> arguments. In order to initialize some of Bar:s attributes, it needs
> to know both "x" and "y". In the bar_init() function I can't get the
> "x" and "y" properties, because they arent set yet, so where do I
> initialize the attributes?

Object properties are set after construction has finished.  That is,
after the contructors and init functions have been called.  You need
to override the get_property and set_property vfuncs of GObjectClass,
and install properties of type G_PARAM_CONSTRUCT.

As an example, have a look at

http://arch.debian.org/cgi-bin/archzoom.cgi/rleigh@...--2005-uterm/uterm--mainline--0.1--patch-41/cse/cse-selection.h?template=plain
http://arch.debian.org/cgi-bin/archzoom.cgi/rleigh@...--2005-uterm/uterm--mainline--0.1--patch-41/cse/cse-selection.c?template=plain

This is a type derived from GObject.  Here's an example for a type
derived from GtkWidget (hrm, just spotted a bug!):

http://arch.debian.org/cgi-bin/archzoom.cgi/rleigh@...--2005-uterm/uterm--mainline--0.1--patch-41/cse/cse-canvas.h?template=plain
http://arch.debian.org/cgi-bin/archzoom.cgi/rleigh@...--2005-uterm/uterm--mainline--0.1--patch-41/cse/cse-canvas.c?template=plain

> Should I hook into the get/set property of both "x" and "y", and
> once both are set (check with some boolean variables), I can
> initialize them in bar_set_property() ?  That way sounds a little
> too complicated so I think I'm missing something.

If "x" and "y" are properties of the parent Bar class, simply call
g_object_new() with the properties to set.  You don't need to do
anything more, since it will handle setting the properties for you.


Regards,
Roger

- --
Roger Leigh
                Printing on GNU/Linux?  http://gimp-print.sourceforge.net/
                Debian GNU/Linux        http://www.debian.org/
                GPG Public Key: 0x25BFB848.  Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFCm5EwVcFcaSW/uEgRArahAKCl+DyuixROOQQJmae/5mP5Wc8fzwCbBYm5
Ow8w5Mjsn8qcsHqnp6MZWWw=
=jCfR
-----END PGP SIGNATURE-----
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list