2015-02-20 15:29:43 +01:00
<?xml version='1.0' encoding="UTF-8"?>
2012-04-23 03:45:08 +02:00
< !DOCTYPE part PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
2010-01-07 09:47:20 +01:00
]>
<part label= "IV" >
<title > Tutorial</title>
<partintro >
<para >
This chapter tries to answer the real-life questions of users and presents
2015-02-20 13:42:52 +01:00
the most common use cases in order from most likely to least
likely.
2010-01-07 09:47:20 +01:00
</para>
</partintro>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<chapter id= "howto-gobject" >
2007-11-13 08:10:42 +01:00
<title > How to define and implement a new GObject</title>
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:04:45 +01:00
This chapter focuses on the implementation of a subtype of GObject, for
2023-05-10 05:11:24 +02:00
example to create a custom class hierarchy, or to subclass a GTK widget.
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
<para >
Throughout the chapter, a running example of a file viewer program is used,
which has a <type > ViewerFile</type> class to represent a single file being
viewed, and various derived classes for different types of files with
special functionality, such as audio files. The example application also
supports editing files (for example, to tweak a photo being viewed), using
a <type > ViewerEditable</type> interface.
</para>
2007-02-10 23:08:42 +01:00
<sect1 id= "howto-gobject-header" >
<title > Boilerplate header code</title>
2004-01-22 19:39:45 +01:00
<para >
2008-07-18 19:55:13 +02:00
The first step before writing the code for your GObject is to write the
type's header which contains the needed type, function and macro
definitions. Each of these elements is nothing but a convention which
2015-02-20 13:42:52 +01:00
is followed by almost all users of GObject, and has been refined over
multiple years of experience developing GObject-based code. If you are
writing a library, it is particularly important for you to adhere closely
to these conventions; users of your library will assume that you have.
Even if not writing a library, it will help other people who want to work
on your project.
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
<para >
Pick a name convention for your headers and source code and stick to it:
<itemizedlist >
2008-07-18 19:55:13 +02:00
<listitem > <para > use a dash to separate the prefix from the typename:
2015-10-18 20:34:44 +02:00
<filename > viewer-file.h</filename> and <filename > viewer-file.c</filename>
2008-07-18 19:55:13 +02:00
(this is the convention used by Nautilus and most GNOME libraries).</para> </listitem>
<listitem > <para > use an underscore to separate the prefix from the
2015-10-18 20:34:44 +02:00
typename: <filename > viewer_file.h</filename> and
<filename > viewer_file.c</filename> .</para> </listitem>
2008-07-18 19:55:13 +02:00
<listitem > <para > Do not separate the prefix from the typename:
2015-10-18 20:34:44 +02:00
<filename > viewerfile.h</filename> and <filename > viewerfile.c</filename> .
2023-05-10 05:11:24 +02:00
(this is the convention used by GTK)</para> </listitem>
2007-02-10 23:08:42 +01:00
</itemizedlist>
2010-09-19 20:33:52 +02:00
Some people like the first two solutions better: it makes reading file
names easier for those with poor eyesight.
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 13:42:52 +01:00
The basic conventions for any header which exposes a GType are described
in <xref linkend= "gtype-conventions" /> .
2007-02-10 23:08:42 +01:00
</para>
2004-11-04 15:52:33 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-10-18 20:34:44 +02:00
If you want to declare a type named ‘ file’ in namespace ‘ viewer’ , name the
type instance <function > ViewerFile</function> and its class
<function > ViewerFileClass</function> (names are case sensitive). The
2015-02-20 13:42:52 +01:00
recommended method of declaring a type differs based on whether the type
is final or derivable.
2015-02-18 18:58:32 +01:00
</para>
<para >
2015-02-20 13:42:52 +01:00
Final types cannot be subclassed further, and should be the default choice
for new types — changing a final type to be derivable is always a change
that will be compatible with existing uses of the code, but the converse
will often cause problems. Final types are declared using
<link linkend= "G-DECLARE-FINAL-TYPE:CAPS" > <function > G_DECLARE_FINAL_TYPE</function> </link> ,
and require a structure to hold the instance data to be declared in the
source code (not the header file).
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2004-01-22 19:39:45 +01:00
/*
* Copyright/Licensing information.
*/
2008-07-18 19:55:13 +02:00
/* inclusion guard */
2015-10-18 20:34:44 +02:00
#ifndef __VIEWER_FILE_H__
#define __VIEWER_FILE_H__
2004-01-22 19:39:45 +01:00
2008-07-18 19:55:13 +02:00
#include < glib-object.h>
2004-01-22 19:39:45 +01:00
/*
* Potentially, include other headers on which this header depends.
*/
2015-02-20 13:42:52 +01:00
G_BEGIN_DECLS
2004-01-22 19:39:45 +01:00
/*
2015-02-20 13:42:52 +01:00
* Type declaration.
2004-01-22 19:39:45 +01:00
*/
2015-10-18 20:34:44 +02:00
#define VIEWER_TYPE_FILE viewer_file_get_type ()
G_DECLARE_FINAL_TYPE (ViewerFile, viewer_file, VIEWER, FILE, GObject)
2004-01-22 19:39:45 +01:00
2015-02-20 13:42:52 +01:00
/*
* Method definitions.
*/
2015-10-18 20:34:44 +02:00
ViewerFile *viewer_file_new (void);
2008-07-18 19:55:13 +02:00
2015-02-20 13:42:52 +01:00
G_END_DECLS
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
#endif /* __VIEWER_FILE_H__ */
2015-02-20 13:42:52 +01:00
</programlisting> </informalexample>
</para>
<para >
Derivable types <emphasis > can</emphasis> be subclassed further, and their class and
instance structures form part of the public API which must not be changed
if API stability is cared about. They are declared using
<link linkend= "G-DECLARE-DERIVABLE-TYPE:CAPS" > <function > G_DECLARE_DERIVABLE_TYPE</function> </link> :
<informalexample > <programlisting >
/*
* Copyright/Licensing information.
*/
/* inclusion guard */
2015-10-18 20:34:44 +02:00
#ifndef __VIEWER_FILE_H__
#define __VIEWER_FILE_H__
2015-02-20 13:42:52 +01:00
#include < glib-object.h>
/*
* Potentially, include other headers on which this header depends.
*/
G_BEGIN_DECLS
/*
* Type declaration.
*/
2015-10-18 20:34:44 +02:00
#define VIEWER_TYPE_FILE viewer_file_get_type ()
G_DECLARE_DERIVABLE_TYPE (ViewerFile, viewer_file, VIEWER, FILE, GObject)
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
struct _ViewerFileClass
2008-07-18 19:55:13 +02:00
{
GObjectClass parent_class;
2015-02-20 13:42:52 +01:00
/* Class virtual function fields. */
2015-10-18 20:34:44 +02:00
void (* open) (ViewerFile *file,
GError **error);
2004-01-22 19:39:45 +01:00
2015-02-20 13:42:52 +01:00
/* Padding to allow adding up to 12 new virtual functions without
* breaking ABI. */
gpointer padding[12];
};
2004-01-22 19:39:45 +01:00
/*
* Method definitions.
*/
2015-10-18 20:34:44 +02:00
ViewerFile *viewer_file_new (void);
2015-02-20 13:42:52 +01:00
G_END_DECLS
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
#endif /* __VIEWER_FILE_H__ */
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 13:42:52 +01:00
The convention for header includes is to add the minimum number of
<function > #include</function> directives to the top of your headers needed
to compile that header. This
2015-10-18 20:34:44 +02:00
allows client code to simply <function > #include "viewer-file.h"</function> ,
2015-02-20 13:42:52 +01:00
without needing to know the prerequisites for
2015-10-18 20:34:44 +02:00
<filename > viewer-file.h</filename> .
2007-02-10 23:08:42 +01:00
</para>
</sect1>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<sect1 id= "howto-gobject-code" >
<title > Boilerplate code</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 13:42:52 +01:00
In your code, the first step is to <function > #include</function> the
needed headers:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2004-01-22 19:39:45 +01:00
/*
* Copyright information
*/
2015-10-18 20:34:44 +02:00
#include "viewer-file.h"
2004-01-22 19:39:45 +01:00
2015-02-20 13:42:52 +01:00
/* Private structure definition. */
typedef struct {
2015-10-18 20:34:44 +02:00
gchar *filename;
2004-06-10 11:41:43 +02:00
/* stuff */
2015-10-18 20:34:44 +02:00
} ViewerFilePrivate;
2004-01-22 19:39:45 +01:00
/*
* forward definitions
*/
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 13:42:52 +01:00
If the class is being declared as final using
<function > G_DECLARE_FINAL_TYPE</function> , its instance structure should
be defined in the C file:
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
struct _ViewerFile
2015-02-20 13:42:52 +01:00
{
GObject parent_instance;
/* Other members, including private data. */
2018-11-18 09:05:28 +01:00
};
2015-02-20 13:42:52 +01:00
</programlisting> </informalexample>
</para>
<para >
Call the <function > G_DEFINE_TYPE</function> macro (or
<function > G_DEFINE_TYPE_WITH_PRIVATE</function> if your class needs
private data — final types do <emphasis > not</emphasis> need private data)
using the name
2008-07-18 19:55:13 +02:00
of the type, the prefix of the functions and the parent GType to
reduce the amount of boilerplate needed. This macro will:
<itemizedlist >
2015-10-18 20:34:44 +02:00
<listitem > <simpara > implement the <function > viewer_file_get_type</function>
2008-07-18 19:55:13 +02:00
function</simpara> </listitem>
<listitem > <simpara > define a parent class pointer accessible from
the whole .c file</simpara> </listitem>
2015-02-20 13:42:52 +01:00
<listitem > <simpara > add private instance data to the type (if using
<function > G_DEFINE_TYPE_WITH_PRIVATE</function> )</simpara> </listitem>
2008-07-18 19:55:13 +02:00
</itemizedlist>
2015-02-20 13:42:52 +01:00
</para>
<para >
If the class has been declared as final using
<function > G_DECLARE_FINAL_TYPE</function> (see
<xref linkend= "howto-gobject-header" /> ), private data should be placed in
2015-10-18 20:34:44 +02:00
the instance structure, <type > ViewerFile</type> , and
2015-02-20 13:42:52 +01:00
<function > G_DEFINE_TYPE</function> should be used instead of
<function > G_DEFINE_TYPE_WITH_PRIVATE</function> . The instance structure
for a final class is not exposed publicly, and is not embedded in the
instance structures of any derived classes (because the class is final);
so its size can vary without causing incompatibilities for code which uses
the class. Conversely, private data for derivable classes
<emphasis > must</emphasis> be included in a private structure, and
<function > G_DEFINE_TYPE_WITH_PRIVATE</function> must be used.
2008-07-18 19:55:13 +02:00
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
G_DEFINE_TYPE (ViewerFile, viewer_file, G_TYPE_OBJECT)
2015-02-20 13:42:52 +01:00
</programlisting> </informalexample>
or
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
G_DEFINE_TYPE_WITH_PRIVATE (ViewerFile, viewer_file, G_TYPE_OBJECT)
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
2008-07-18 19:55:13 +02:00
<para >
It is also possible to use the
<function > G_DEFINE_TYPE_WITH_CODE</function> macro to control the
2015-02-20 13:42:52 +01:00
<function > get_type</function> function implementation — for instance, to
add a call to the <function > G_IMPLEMENT_INTERFACE</function> macro to
implement an interface.
2008-07-18 19:55:13 +02:00
</para>
2007-02-10 23:08:42 +01:00
</sect1>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<sect1 id= "howto-gobject-construction" >
2015-02-20 14:04:45 +01:00
<title > Object construction</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
People often get confused when trying to construct their GObjects because of the
sheer number of different ways to hook into the objects's construction process: it is
difficult to figure which is the <emphasis > correct</emphasis> , recommended way.
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
<xref linkend= "gobject-construction-table" /> shows what user-provided functions
2007-11-13 08:10:42 +01:00
are invoked during object instantiation and in which order they are invoked.
2007-02-10 23:08:42 +01:00
A user looking for the equivalent of the simple C++ constructor function should use
2015-02-20 13:51:18 +01:00
the <function > instance_init</function> method. It will be invoked after
all the parents’ <function > instance_init</function>
2007-02-10 23:08:42 +01:00
functions have been invoked. It cannot take arbitrary construction parameters
(as in C++) but if your object needs arbitrary parameters to complete initialization,
you can use construction properties.
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 13:51:18 +01:00
Construction properties will be set only after all
<function > instance_init</function> functions have run.
2008-05-17 07:56:29 +02:00
No object reference will be returned to the client of <function > <link linkend= "g-object-new" > g_object_new</link> </function>
2007-02-10 23:08:42 +01:00
until all the construction properties have been set.
</para>
2004-01-22 19:39:45 +01:00
2013-06-12 16:18:29 +02:00
<para >
It is important to note that object construction cannot <emphasis > ever</emphasis>
fail. If you require a fallible GObject construction, you can use the
2015-02-20 13:51:18 +01:00
<link linkend= "GInitable" > <type > GInitable</type> </link> and
<link linkend= "GAsyncInitable" > <type > GAsyncInitable</type> </link>
interfaces provided by the GIO library.
2013-06-12 16:18:29 +02:00
</para>
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:04:45 +01:00
You should write the following code first:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
G_DEFINE_TYPE_WITH_PRIVATE (ViewerFile, viewer_file, G_TYPE_OBJECT)
2013-06-12 16:18:29 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_class_init (ViewerFileClass *klass)
2013-06-12 16:18:29 +02:00
{
}
2004-01-22 19:39:45 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_init (ViewerFile *self)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFilePrivate *priv = viewer_file_get_instance_private (self);
2004-01-22 19:39:45 +01:00
2014-04-29 09:47:14 +02:00
/* initialize all public and private members to reasonable default values.
* They are all automatically initialized to 0 to begin with. */
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:08:34 +01:00
If you need special construction properties (with
<link linkend= "G-PARAM-CONSTRUCT-ONLY:CAPS" > <function > G_PARAM_CONSTRUCT_ONLY</function> </link>
set), install the properties in
2013-06-12 16:18:29 +02:00
the <function > class_init()</function> function, override the <function > set_property()</function>
and <function > get_property()</function> methods of the GObject class,
and implement them as described by <xref linkend= "gobject-properties" /> .
2015-02-20 14:08:34 +01:00
</para>
2011-12-07 00:15:58 +01:00
2015-02-20 14:08:34 +01:00
<para >
Property IDs must start from 1, as 0 is reserved for internal use by
GObject.
<informalexample > <programlisting >
enum
{
2015-10-18 20:34:44 +02:00
PROP_FILENAME = 1,
PROP_ZOOM_LEVEL,
2011-12-07 00:15:58 +01:00
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
2004-01-22 19:39:45 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_class_init (ViewerFileClass *klass)
2004-01-22 19:39:45 +01:00
{
2015-02-20 14:08:34 +01:00
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
object_class->set_property = viewer_file_set_property;
object_class->get_property = viewer_file_get_property;
obj_properties[PROP_FILENAME] =
g_param_spec_string ("filename",
"Filename",
"Name of the file to load and display from.",
NULL /* default value */,
2018-04-10 16:31:40 +02:00
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
2015-10-18 20:34:44 +02:00
obj_properties[PROP_ZOOM_LEVEL] =
g_param_spec_uint ("zoom-level",
"Zoom level",
"Zoom level to view the file at.",
0 /* minimum value */,
10 /* maximum value */,
2 /* default value */,
2018-04-10 16:31:40 +02:00
G_PARAM_READWRITE);
2011-12-07 00:15:58 +01:00
2015-02-20 14:08:34 +01:00
g_object_class_install_properties (object_class,
2011-12-07 00:15:58 +01:00
N_PROPERTIES,
obj_properties);
2004-01-22 19:39:45 +01:00
}
2011-12-07 00:15:58 +01:00
</programlisting> </informalexample>
2013-06-12 16:18:29 +02:00
If you need this, make sure you can build and run code similar to the
code shown above. Also, make sure your construct properties can be set
without side effects during construction.
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2008-07-18 19:55:13 +02:00
<para >
2019-08-23 23:45:16 +02:00
Some people sometimes need to complete the initialization of an instance
2013-06-12 16:18:29 +02:00
of a type only after the properties passed to the constructors have been
set. This is possible through the use of the <function > constructor()</function>
class method as described in <xref linkend= "gobject-instantiation" /> or,
2015-02-20 14:04:45 +01:00
more simply, using the <function > constructed()</function> class method.
Note that the <function > constructed()</function>
2013-06-12 16:18:29 +02:00
virtual function will only be invoked after the properties marked as
2015-02-20 14:04:45 +01:00
<function > G_PARAM_CONSTRUCT_ONLY</function> or
2015-02-20 13:51:18 +01:00
<function > G_PARAM_CONSTRUCT</function> have been consumed, but
2013-06-12 16:18:29 +02:00
before the regular properties passed to <function > g_object_new()</function>
have been set.
2008-07-18 19:55:13 +02:00
</para>
2007-02-10 23:08:42 +01:00
</sect1>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<sect1 id= "howto-gobject-destruction" >
2015-02-20 14:04:45 +01:00
<title > Object destruction</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2008-07-18 19:55:13 +02:00
Again, it is often difficult to figure out which mechanism to use to
hook into the object's destruction process: when the last
<function > <link linkend= "g-object-unref" > g_object_unref</link> </function>
function call is made, a lot of things happen as described in
<xref linkend= "gobject-destruction-table" /> .
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:04:45 +01:00
The destruction process of your object is in two phases: dispose and
finalize. This split is necessary to handle
2013-06-12 16:18:29 +02:00
potential cycles due to the nature of the reference counting mechanism
2015-02-20 14:04:45 +01:00
used by GObject, as well as dealing with temporary revival of
2013-06-12 16:18:29 +02:00
instances in case of signal emission during the destruction sequence.
2015-02-20 14:04:45 +01:00
See <xref linkend= "gobject-memory-cycles" /> for more information.
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
struct _ViewerFilePrivate
2008-07-18 19:55:13 +02:00
{
2015-10-18 20:34:44 +02:00
gchar *filename;
guint zoom_level;
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
GInputStream *input_stream;
2004-01-22 19:39:45 +01:00
};
2015-10-18 20:34:44 +02:00
G_DEFINE_TYPE_WITH_PRIVATE (ViewerFile, viewer_file, G_TYPE_OBJECT)
2004-11-05 08:33:28 +01:00
2004-01-22 19:39:45 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_dispose (GObject *gobject)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFilePrivate *priv = viewer_file_get_instance_private (VIEWER_FILE (gobject));
2004-01-22 19:39:45 +01:00
2013-06-12 16:18:29 +02:00
/* In dispose(), you are supposed to free all types referenced from this
2004-01-22 19:39:45 +01:00
* object which might themselves hold a reference to self. Generally,
* the most simple solution is to unref all members on which you own a
* reference.
*/
2004-11-05 08:33:28 +01:00
2013-06-12 16:18:29 +02:00
/* dispose() might be called multiple times, so we must guard against
* calling g_object_unref() on an invalid GObject by setting the member
2014-12-16 12:23:12 +01:00
* NULL; g_clear_object() does this for us.
2008-07-18 19:55:13 +02:00
*/
2015-10-18 20:34:44 +02:00
g_clear_object (& priv->input_stream);
2008-07-18 19:55:13 +02:00
2013-06-12 16:18:29 +02:00
/* Always chain up to the parent class; there is no need to check if
* the parent class implements the dispose() virtual function: it is
* always guaranteed to do so
*/
2015-10-18 20:34:44 +02:00
G_OBJECT_CLASS (viewer_file_parent_class)->dispose (gobject);
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_finalize (GObject *gobject)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFilePrivate *priv = viewer_file_get_instance_private (VIEWER_FILE (gobject));
2004-11-05 08:33:28 +01:00
2015-10-18 20:34:44 +02:00
g_free (priv->filename);
2008-07-18 19:55:13 +02:00
2013-06-12 16:18:29 +02:00
/* Always chain up to the parent class; as with dispose(), finalize()
* is guaranteed to exist on the parent's class virtual function table
*/
2015-10-18 20:34:44 +02:00
G_OBJECT_CLASS (viewer_file_parent_class)->finalize (gobject);
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_class_init (ViewerFileClass *klass)
2004-01-22 19:39:45 +01:00
{
2015-02-20 14:10:04 +01:00
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
object_class->dispose = viewer_file_dispose;
object_class->finalize = viewer_file_finalize;
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_init (ViewerFile *self);
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFilePrivate *priv = viewer_file_get_instance_private (self);
2004-11-05 08:33:28 +01:00
2015-10-18 20:34:44 +02:00
priv->input_stream = g_object_new (VIEWER_TYPE_INPUT_STREAM, NULL);
priv->filename = /* would be set as a property */;
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2008-07-18 19:55:13 +02:00
<para >
It is possible that object methods might be invoked after dispose is
run and before finalize runs. GObject does not consider this to be a
program error: you must gracefully detect this and neither crash nor
2015-02-20 14:04:45 +01:00
warn the user, by having a disposed instance revert to an inert state.
2007-02-10 23:08:42 +01:00
</para>
</sect1>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<sect1 id= "howto-gobject-methods" >
<title > Object methods</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
Just as with C++, there are many different ways to define object
2008-07-18 19:55:13 +02:00
methods and extend them: the following list and sections draw on
2013-06-12 16:18:29 +02:00
C++ vocabulary. (Readers are expected to know basic C++ concepts.
2008-07-18 19:55:13 +02:00
Those who have not had to write C++ code recently can refer to e.g.
<ulink url= "http://www.cplusplus.com/doc/tutorial/" /> to refresh
their memories.)
2007-02-10 23:08:42 +01:00
<itemizedlist >
<listitem > <para >
non-virtual public methods,
</para> </listitem>
<listitem > <para >
virtual public methods and
</para> </listitem>
<listitem > <para >
virtual private methods
</para> </listitem>
</itemizedlist>
</para>
2004-01-22 19:39:45 +01:00
2015-02-20 13:51:18 +01:00
<sect2 id= "non-virtual-public-methods" >
2007-02-10 23:08:42 +01:00
<title > Non-virtual public methods</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:04:45 +01:00
These are the simplest, providing a simple method which
acts on the object. Provide a function
2008-07-18 19:55:13 +02:00
prototype in the header and an implementation of that prototype
2007-02-10 23:08:42 +01:00
in the source file.
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2004-01-22 19:39:45 +01:00
/* declaration in the header. */
2015-10-18 20:34:44 +02:00
void viewer_file_open (ViewerFile *self,
GError **error);
2008-07-18 19:55:13 +02:00
2004-01-22 19:39:45 +01:00
/* implementation in the source file */
2008-07-18 19:55:13 +02:00
void
2015-10-18 20:34:44 +02:00
viewer_file_open (ViewerFile *self,
GError **error)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
g_return_if_fail (VIEWER_IS_FILE (self));
g_return_if_fail (error == NULL || *error == NULL);
2008-07-18 19:55:13 +02:00
2004-01-22 19:39:45 +01:00
/* do stuff here. */
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
</sect2>
2004-01-22 19:39:45 +01:00
2015-02-20 13:51:18 +01:00
<sect2 id= "virtual-public-methods" >
2007-02-10 23:08:42 +01:00
<title > Virtual public methods</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 13:42:52 +01:00
This is the preferred way to create GObjects with overridable methods:
<itemizedlist >
<listitem > <para >
Define the common method and its virtual function in the
class structure in the public header
</para> </listitem>
<listitem > <para >
Define the common method in the header file and implement it in the
source file
</para> </listitem>
<listitem > <para >
Implement a base version of the virtual function in the source
file and initialize the virtual function pointer to this
implementation in the object’ s <function > class_init</function>
function; or leave it as <constant > NULL</constant> for a ‘ pure
virtual’ method which must be overridden by derived classes
</para> </listitem>
<listitem > <para >
Re-implement the virtual function in each derived class which needs
to override it
</para> </listitem>
</itemizedlist>
</para>
<para >
Note that virtual functions can only be defined if the class is
derivable, declared using
<link linkend= "G-DECLARE-DERIVABLE-TYPE:CAPS" > <function > G_DECLARE_DERIVABLE_TYPE</function> </link>
so the class structure can be defined.
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
/* declaration in viewer-file.h. */
#define VIEWER_TYPE_FILE viewer_file_get_type ()
G_DECLARE_DERIVABLE_TYPE (ViewerFile, viewer_file, VIEWER, FILE, GObject)
2015-02-20 14:12:49 +01:00
2015-10-18 20:34:44 +02:00
struct _ViewerFileClass
2008-07-18 19:55:13 +02:00
{
GObjectClass parent_class;
2004-01-22 19:39:45 +01:00
/* stuff */
2015-10-18 20:34:44 +02:00
void (*open) (ViewerFile *self,
GError **error);
2015-02-20 14:12:49 +01:00
/* Padding to allow adding up to 12 new virtual functions without
* breaking ABI. */
gpointer padding[12];
2004-01-22 19:39:45 +01:00
};
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
void viewer_file_open (ViewerFile *self,
GError **error);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
/* implementation in viewer-file.c */
2008-07-18 19:55:13 +02:00
void
2015-10-18 20:34:44 +02:00
viewer_file_open (ViewerFile *self,
GError **error)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFileClass *klass;
2015-02-20 14:14:08 +01:00
2015-10-18 20:34:44 +02:00
g_return_if_fail (VIEWER_IS_FILE (self));
g_return_if_fail (error == NULL || *error == NULL);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
klass = VIEWER_FILE_GET_CLASS (self);
g_return_if_fail (klass->open != NULL);
2015-02-20 14:14:08 +01:00
2015-10-18 20:34:44 +02:00
klass->open (self, error);
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2015-10-18 20:34:44 +02:00
The code above simply redirects the <function > open</function> call
2015-02-20 13:42:52 +01:00
to the relevant virtual function.
2007-02-10 23:08:42 +01:00
</para>
2004-06-10 11:41:43 +02:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:04:45 +01:00
It is possible to provide a default
2008-07-18 19:55:13 +02:00
implementation for this class method in the object's
<function > class_init</function> function: initialize the
2015-10-18 20:34:44 +02:00
<function > klass-> open</function> field to a pointer to the
2015-02-20 13:51:18 +01:00
actual implementation.
2014-04-29 09:47:14 +02:00
By default, class methods that are not inherited are initialized to
2015-02-20 13:51:18 +01:00
<function > NULL</function> , and thus are to be considered "pure virtual".
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2008-07-18 19:55:13 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_real_close (ViewerFile *self,
GError **error)
2004-01-22 19:39:45 +01:00
{
2004-06-10 11:41:43 +02:00
/* Default implementation for the virtual method. */
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_class_init (ViewerFileClass *klass)
2004-01-22 19:39:45 +01:00
{
2013-06-12 16:18:29 +02:00
/* this is not necessary, except for demonstration purposes.
*
* pure virtual method: mandates implementation in children.
*/
2015-10-18 20:34:44 +02:00
klass->open = NULL;
2008-07-18 19:55:13 +02:00
2004-06-10 11:41:43 +02:00
/* merely virtual method. */
2015-10-18 20:34:44 +02:00
klass->close = viewer_file_real_close;
2004-01-22 19:39:45 +01:00
}
2008-07-18 19:55:13 +02:00
void
2015-10-18 20:34:44 +02:00
viewer_file_open (ViewerFile *self,
GError **error)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFileClass *klass;
2015-02-20 14:14:08 +01:00
2015-10-18 20:34:44 +02:00
g_return_if_fail (VIEWER_IS_FILE (self));
g_return_if_fail (error == NULL || *error == NULL);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
klass = VIEWER_FILE_GET_CLASS (self);
2015-02-20 14:14:08 +01:00
2013-06-12 16:18:29 +02:00
/* if the method is purely virtual, then it is a good idea to
* check that it has been overridden before calling it, and,
* depending on the intent of the class, either ignore it silently
* or warn the user.
2015-02-20 14:14:08 +01:00
*/
2015-10-18 20:34:44 +02:00
g_return_if_fail (klass->open != NULL);
klass->open (self, error);
2004-01-22 19:39:45 +01:00
}
2008-07-18 19:55:13 +02:00
void
2015-10-18 20:34:44 +02:00
viewer_file_close (ViewerFile *self,
GError **error)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFileClass *klass;
2015-02-20 14:14:08 +01:00
2015-10-18 20:34:44 +02:00
g_return_if_fail (VIEWER_IS_FILE (self));
g_return_if_fail (error == NULL || *error == NULL);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
klass = VIEWER_FILE_GET_CLASS (self);
if (klass->close != NULL)
klass->close (self, error);
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
</sect2>
2004-01-22 19:39:45 +01:00
2015-02-20 13:51:18 +01:00
<sect2 id= "virtual-private-methods" >
2007-02-10 23:08:42 +01:00
<title > Virtual private Methods</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 13:51:18 +01:00
These are very similar to <link linkend= "virtual-public-methods" > virtual
public methods</link> . They just don't
have a public function to call directly. The header
2015-02-20 13:42:52 +01:00
file contains only a declaration of the virtual function:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
/* declaration in viewer-file.h. */
struct _ViewerFileClass
2008-07-18 19:55:13 +02:00
{
2004-01-22 19:39:45 +01:00
GObjectClass parent;
2015-10-18 20:34:44 +02:00
/* Public virtual method as before. */
void (*open) (ViewerFile *self,
GError **error);
/* Private helper function to work out whether the file can be loaded via
* memory mapped I/O, or whether it has to be read as a stream. */
gboolean (*can_memory_map) (ViewerFile *self);
2015-02-20 14:14:08 +01:00
/* Padding to allow adding up to 12 new virtual functions without
* breaking ABI. */
gpointer padding[12];
2004-01-22 19:39:45 +01:00
};
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
void viewer_file_open (ViewerFile *self, GError **error);
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2015-02-20 13:42:52 +01:00
These virtual functions are often used to delegate part of the job
2008-07-18 19:55:13 +02:00
to child classes:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2004-01-22 19:39:45 +01:00
/* this accessor function is static: it is not exported outside of this file. */
2015-10-18 20:34:44 +02:00
static gboolean
viewer_file_can_memory_map (ViewerFile *self)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
return VIEWER_FILE_GET_CLASS (self)->can_memory_map (self);
2004-01-22 19:39:45 +01:00
}
2008-07-18 19:55:13 +02:00
void
2015-10-18 20:34:44 +02:00
viewer_file_open (ViewerFile *self,
GError **error)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
g_return_if_fail (VIEWER_IS_FILE (self));
g_return_if_fail (error == NULL || *error == NULL);
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
/*
* Try to load the file using memory mapped I/O, if the implementation of the
* class determines that is possible using its private virtual method.
2004-01-22 19:39:45 +01:00
*/
2015-10-18 20:34:44 +02:00
if (viewer_file_can_memory_map (self))
{
/* Load the file using memory mapped I/O. */
}
else
{
/* Fall back to trying to load the file using streaming I/O… */
}
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2008-07-18 19:55:13 +02:00
Again, it is possible to provide a default implementation for this
2015-02-20 13:42:52 +01:00
private virtual function:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
static gboolean
viewer_file_real_can_memory_map (ViewerFile *self)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
/* As an example, always return false. Or, potentially return true if the
* file is local. */
return FALSE;
}
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
static void
viewer_file_class_init (ViewerFileClass *klass)
{
/* non-pure virtual method; does not have to be implemented in children. */
klass->can_memory_map = viewer_file_real_can_memory_map;
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-10-18 20:34:44 +02:00
Derived classes can then override the method with code such as:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2004-01-22 19:39:45 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_audio_file_class_init (ViewerAudioFileClass *klass)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFileClass *file_class = VIEWER_FILE_CLASS (klass);
2008-07-18 19:55:13 +02:00
2015-02-20 13:42:52 +01:00
/* implement pure virtual function. */
2015-10-18 20:34:44 +02:00
file_class->can_memory_map = viewer_audio_file_can_memory_map;
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
</sect2>
</sect1>
2004-06-10 11:41:43 +02:00
2007-02-10 23:08:42 +01:00
<sect1 id= "howto-gobject-chainup" >
<title > Chaining up</title>
2008-07-18 19:55:13 +02:00
<para > Chaining up is often loosely defined by the following set of
conditions:
2007-02-10 23:08:42 +01:00
<itemizedlist >
<listitem > <para > Parent class A defines a public virtual method named <function > foo</function> and
provides a default implementation.</para> </listitem>
<listitem > <para > Child class B re-implements method <function > foo</function> .</para> </listitem>
2015-02-20 14:04:45 +01:00
<listitem > <para > B’ s implementation of <function > foo</function> calls (‘ chains up to’ ) its parent class A’ s implementation of <function > foo</function> .</para> </listitem>
2007-02-10 23:08:42 +01:00
</itemizedlist>
2015-02-20 14:04:45 +01:00
There are various uses of this idiom:
2007-02-10 23:08:42 +01:00
<itemizedlist >
2013-06-12 16:18:29 +02:00
<listitem > <para > You need to extend the behaviour of a class without modifying its code. You create
2007-02-10 23:08:42 +01:00
a subclass to inherit its implementation, re-implement a public virtual method to modify the behaviour
2013-06-12 16:18:29 +02:00
and chain up to ensure that the previous behaviour is not really modified, just extended.
2007-02-10 23:08:42 +01:00
</para> </listitem>
2015-02-20 14:04:45 +01:00
<listitem > <para > You need to implement the
<ulink url= "http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern" > Chain
Of Responsibility pattern</ulink> : each object of the inheritance
2007-11-13 08:10:42 +01:00
tree chains up to its parent (typically, at the beginning or the end of the method) to ensure that
2015-02-20 14:04:45 +01:00
each handler is run in turn.</para> </listitem>
2007-02-10 23:08:42 +01:00
</itemizedlist>
</para>
2004-06-09 22:22:04 +02:00
2007-02-10 23:08:42 +01:00
<para >
2007-11-13 08:10:42 +01:00
To explicitly chain up to the implementation of the virtual method in the parent class,
2007-02-10 23:08:42 +01:00
you first need a handle to the original parent class structure. This pointer can then be used to
2015-02-20 13:42:52 +01:00
access the original virtual function pointer and invoke it directly.
2007-02-10 23:08:42 +01:00
<footnote >
<para >
The <emphasis > original</emphasis> adjective used in this sentence is not innocuous. To fully
2015-02-20 14:04:45 +01:00
understand its meaning, recall how class structures are initialized: for each object type,
the class structure associated with this object is created by first copying the class structure of its
parent type (a simple <function > memcpy</function> ) and then by invoking the <function > class_init</function> callback on
the resulting class structure. Since the <function > class_init</function> callback is responsible for overwriting the class structure
with the user re-implementations of the class methods, the modified copy of the parent class
structure stored in the derived instance cannot be used. A copy of the class structure of an instance of the parent
class is needed.
2007-02-10 23:08:42 +01:00
</para>
</footnote>
</para>
2015-02-20 14:04:45 +01:00
<para >
Use the <function > parent_class</function> pointer created and initialized
by the
<link linkend= "G-DEFINE-TYPE:CAPS" > <function > G_DEFINE_TYPE</function> </link>
family of macros, for instance:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2004-06-10 11:41:43 +02:00
static void
2015-02-20 14:15:15 +01:00
b_method_to_call (B *obj, gint some_param)
2004-06-10 11:41:43 +02:00
{
/* do stuff before chain up */
2008-07-18 19:55:13 +02:00
2013-06-12 16:18:29 +02:00
/* call the method_to_call() virtual function on the
* parent of BClass, AClass.
*
* remember the explicit cast to AClass*
*/
2015-02-20 14:15:15 +01:00
A_CLASS (b_parent_class)->method_to_call (obj, some_param);
2008-07-18 19:55:13 +02:00
2004-06-10 11:41:43 +02:00
/* do stuff after chain up */
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
</sect1>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
</chapter>
<!-- End Howto GObject -->
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<chapter id= "howto-interface" >
2007-11-13 08:10:42 +01:00
<title > How to define and implement interfaces</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<sect1 id= "howto-interface-define" >
2013-03-18 04:01:21 +01:00
<title > Defining interfaces</title>
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:04:45 +01:00
The theory behind how GObject interfaces work is given in
2023-04-13 16:37:00 +02:00
<xref linkend= "gtype-non-instantiatable-non-classed" /> ; this section covers how to
2015-02-20 14:04:45 +01:00
define and implement an interface.
2007-02-10 23:08:42 +01:00
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:04:45 +01:00
The first step is to get the header right. This interface
2017-11-25 21:20:59 +01:00
defines three methods:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-02-20 14:12:49 +01:00
/*
* Copyright/Licensing information.
*/
2015-10-18 20:34:44 +02:00
#ifndef __VIEWER_EDITABLE_H__
#define __VIEWER_EDITABLE_H__
2004-01-22 19:39:45 +01:00
#include < glib-object.h>
2015-02-20 14:12:49 +01:00
G_BEGIN_DECLS
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
#define VIEWER_TYPE_EDITABLE viewer_editable_get_type ()
G_DECLARE_INTERFACE (ViewerEditable, viewer_editable, VIEWER, EDITABLE, GObject)
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
struct _ViewerEditableInterface
2008-07-18 19:55:13 +02:00
{
GTypeInterface parent_iface;
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
void (*save) (ViewerEditable *self,
GError **error);
void (*undo) (ViewerEditable *self,
guint n_steps);
void (*redo) (ViewerEditable *self,
guint n_steps);
2004-01-22 19:39:45 +01:00
};
2015-10-18 20:34:44 +02:00
void viewer_editable_save (ViewerEditable *self,
GError **error);
void viewer_editable_undo (ViewerEditable *self,
guint n_steps);
void viewer_editable_redo (ViewerEditable *self,
guint n_steps);
2004-01-22 19:39:45 +01:00
2015-02-20 14:12:49 +01:00
G_END_DECLS
2015-10-18 20:34:44 +02:00
#endif /* __VIEWER_EDITABLE_H__ */
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2010-09-19 20:50:31 +02:00
This code is the same as the code for a normal <link linkend= "GType" > <type > GType</type> </link>
which derives from a <link linkend= "GObject" > <type > GObject</type> </link> except for a few details:
2007-02-10 23:08:42 +01:00
<itemizedlist >
<listitem > <para >
2015-02-20 14:12:49 +01:00
The <function > _GET_CLASS</function> function is called
<function > _GET_IFACE</function> (and is defined by
<link linkend= "G-DECLARE-INTERFACE:CAPS" > <function > G_DECLARE_INTERFACE</function> </link> ).
2007-02-10 23:08:42 +01:00
</para> </listitem>
<listitem > <para >
2015-10-18 20:34:44 +02:00
The instance type, <type > ViewerEditable</type> , is not fully defined: it is
2008-07-18 19:55:13 +02:00
used merely as an abstract type which represents an instance of
whatever object which implements the interface.
</para> </listitem>
<listitem > <para >
2015-10-18 20:34:44 +02:00
The parent of the <type > ViewerEditableInterface</type> is
2015-02-20 14:12:49 +01:00
<type > GTypeInterface</type> , not <type > GObjectClass</type> .
2007-02-10 23:08:42 +01:00
</para> </listitem>
</itemizedlist>
</para>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-10-18 20:34:44 +02:00
The implementation of the <type > ViewerEditable</type> type itself is trivial:
2007-02-10 23:08:42 +01:00
<itemizedlist >
2012-05-05 12:51:16 +02:00
<listitem > <para > <function > <link linkend= "G-DEFINE-INTERFACE:CAPS" > G_DEFINE_INTERFACE</link> </function>
2015-10-18 20:34:44 +02:00
creates a <function > viewer_editable_get_type</function> function which registers the
2012-05-05 12:51:16 +02:00
type in the type system. The third argument is used to define a
<link linkend= "howto-interface-prerequisite" > prerequisite interface</link>
(which we'll talk about more later). Just pass <code > 0</code> for this
argument when an interface has no prerequisite.
2007-02-10 23:08:42 +01:00
</para> </listitem>
2015-10-18 20:34:44 +02:00
<listitem > <para > <function > viewer_editable_default_init</function> is expected
2007-02-10 23:08:42 +01:00
to register the interface's signals if there are any (we will see a bit
2012-05-05 12:51:16 +02:00
later how to use them).</para> </listitem>
2015-10-18 20:34:44 +02:00
<listitem > <para > The interface methods <function > viewer_editable_save</function> ,
<function > viewer_editable_undo</function> and <function > viewer_editable_redo</function> dereference the interface
2012-05-05 12:51:16 +02:00
structure to access its associated interface function and call it.
2007-02-10 23:08:42 +01:00
</para> </listitem>
</itemizedlist>
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2014-10-17 12:54:02 +02:00
G_DEFINE_INTERFACE (ViewerEditable, viewer_editable, G_TYPE_OBJECT)
2012-05-05 12:51:16 +02:00
2004-01-22 19:39:45 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_editable_default_init (ViewerEditableInterface *iface)
2004-01-22 19:39:45 +01:00
{
2012-05-05 12:51:16 +02:00
/* add properties and signals to the interface here */
2004-01-22 19:39:45 +01:00
}
2012-05-05 12:51:16 +02:00
void
2015-10-18 20:34:44 +02:00
viewer_editable_save (ViewerEditable *self,
GError **error)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerEditableInterface *iface;
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
g_return_if_fail (VIEWER_IS_EDITABLE (self));
g_return_if_fail (error == NULL || *error == NULL);
iface = VIEWER_EDITABLE_GET_IFACE (self);
g_return_if_fail (iface->save != NULL);
iface->save (self, error);
2004-01-22 19:39:45 +01:00
}
2008-07-18 19:55:13 +02:00
void
2015-10-18 20:34:44 +02:00
viewer_editable_undo (ViewerEditable *self,
guint n_steps)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerEditableInterface *iface;
g_return_if_fail (VIEWER_IS_EDITABLE (self));
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
iface = VIEWER_EDITABLE_GET_IFACE (self);
g_return_if_fail (iface->undo != NULL);
iface->undo (self, n_steps);
}
void
viewer_editable_redo (ViewerEditable *self,
guint n_steps)
{
ViewerEditableInterface *iface;
g_return_if_fail (VIEWER_IS_EDITABLE (self));
iface = VIEWER_EDITABLE_GET_IFACE (self);
g_return_if_fail (iface->redo != NULL);
iface->redo (self, n_steps);
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2004-01-22 19:39:45 +01:00
</para>
2005-04-22 12:27:37 +02:00
</sect1>
2007-02-10 23:08:42 +01:00
2005-04-22 12:27:37 +02:00
<sect1 id= "howto-interface-implement" >
2013-03-18 04:01:21 +01:00
<title > Implementing interfaces</title>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2008-07-18 19:55:13 +02:00
Once the interface is defined, implementing it is rather trivial.
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2015-02-20 14:04:45 +01:00
The first step is to define a normal final GObject class exactly as in
<xref linkend= "howto-gobject-header" /> .
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2015-10-18 20:34:44 +02:00
The second step is to implement <type > ViewerFile</type> by defining
2015-02-20 14:04:45 +01:00
it using
2012-05-05 12:51:16 +02:00
<function > <link linkend= "G-DEFINE-TYPE-WITH-CODE:CAPS" > G_DEFINE_TYPE_WITH_CODE</link> </function>
2015-02-20 14:04:45 +01:00
and
2012-05-05 12:51:16 +02:00
<function > <link linkend= "G-IMPLEMENT-INTERFACE:CAPS" > G_IMPLEMENT_INTERFACE</link> </function>
2015-02-20 14:04:45 +01:00
instead of
<function > <link linkend= "G-DEFINE-TYPE:CAPS" > G_DEFINE_TYPE</link> </function> :
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
static void viewer_file_editable_interface_init (ViewerEditableInterface *iface);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
G_DEFINE_TYPE_WITH_CODE (ViewerFile, viewer_file, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE,
viewer_file_editable_interface_init))
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2015-02-20 14:04:45 +01:00
This definition is very much like all the similar functions seen
previously. The only interface-specific code present here is the use of
2012-05-05 12:51:16 +02:00
<function > <link linkend= "G-IMPLEMENT-INTERFACE:CAPS" > G_IMPLEMENT_INTERFACE</link> </function> .
2004-01-22 19:39:45 +01:00
</para>
2008-07-18 19:55:13 +02:00
2012-05-05 12:51:16 +02:00
<note > <para > Classes can implement multiple interfaces by using multiple calls to
<function > <link linkend= "G-IMPLEMENT-INTERFACE:CAPS" > G_IMPLEMENT_INTERFACE</link> </function>
inside the call to
<function > <link linkend= "G-DEFINE-TYPE-WITH-CODE:CAPS" > G_DEFINE_TYPE_WITH_CODE</link> </function>
</para> </note>
2007-02-10 23:08:42 +01:00
2004-01-22 19:39:45 +01:00
<para >
2015-10-18 20:34:44 +02:00
<function > viewer_file_editable_interface_init</function> , the interface
2008-07-18 19:55:13 +02:00
initialization function: inside it every virtual method of the interface
must be assigned to its implementation:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2008-07-18 19:55:13 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_editable_save (ViewerFile *self,
GError **error)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
g_print ("File implementation of editable interface save method: %s.\n",
self->filename);
2012-05-05 12:51:16 +02:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_editable_undo (ViewerFile *self,
guint n_steps)
2012-05-05 12:51:16 +02:00
{
2015-10-18 20:34:44 +02:00
g_print ("File implementation of editable interface undo method: %s.\n",
self->filename);
2004-01-22 19:39:45 +01:00
}
2008-07-18 19:55:13 +02:00
2004-01-22 19:39:45 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_editable_redo (ViewerFile *self,
guint n_steps)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
g_print ("File implementation of editable interface redo method: %s.\n",
self->filename);
2004-01-22 19:39:45 +01:00
}
2008-07-18 19:55:13 +02:00
2004-01-22 19:39:45 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_editable_interface_init (ViewerEditableInterface *iface)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
iface->save = viewer_file_editable_save;
iface->undo = viewer_file_editable_undo;
iface->redo = viewer_file_editable_redo;
}
static void
viewer_file_init (ViewerFile *self)
{
/* Instance variable initialisation code. */
2004-01-22 19:39:45 +01:00
}
2018-05-25 06:32:00 +02:00
</programlisting> </informalexample>
</para>
<para >
If the object is not of final type, e.g. was declared using
<function > <link linkend= "G-DECLARE-DERIVABLE-TYPE:CAPS" > G_DECLARE_DERIVABLE_TYPE</link> </function>
then
<function > <link linkend= "G-ADD-PRIVATE:CAPS" > G_ADD_PRIVATE</link> </function>
macro should be added. The private structure should be declared exactly
as for a normal derivable object, see <xref linkend= "howto-gobject-code" /> .
<informalexample > <programlisting >
G_DEFINE_TYPE_WITH_CODE (ViewerFile, viewer_file, G_TYPE_OBJECT,
G_ADD_PRIVATE (ViewerFile)
G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE,
viewer_file_editable_interface_init))
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2004-01-22 19:39:45 +01:00
</para>
2007-02-10 23:08:42 +01:00
</sect1>
2012-05-05 12:51:16 +02:00
<sect1 id= "howto-interface-prerequisite" >
2007-02-10 23:08:42 +01:00
<title > Interface definition prerequisites</title>
<para >
2008-07-18 19:55:13 +02:00
To specify that an interface requires the presence of other interfaces
when implemented, GObject introduces the concept of
<emphasis > prerequisites</emphasis> : it is possible to associate
2012-05-05 12:51:16 +02:00
a list of prerequisite types to an interface. For example, if
2008-07-18 19:55:13 +02:00
object A wishes to implement interface I1, and if interface I1 has a
prerequisite on interface I2, A has to implement both I1 and I2.
2007-02-10 23:08:42 +01:00
</para>
<para >
2008-07-18 19:55:13 +02:00
The mechanism described above is, in practice, very similar to
Java's interface I1 extends interface I2. The example below shows
the GObject equivalent:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
/* Make the ViewerEditableLossy interface require ViewerEditable interface. */
2014-10-17 12:54:02 +02:00
G_DEFINE_INTERFACE (ViewerEditableLossy, viewer_editable_lossy, VIEWER_TYPE_EDITABLE)
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2012-05-05 12:51:16 +02:00
In the <function > <link linkend= "G-DEFINE-INTERFACE:CAPS" > G_DEFINE_INTERFACE</link> </function>
call above, the third parameter defines the prerequisite type. This
is the GType of either an interface or a class. In this case
2015-10-18 20:34:44 +02:00
the <type > ViewerEditable</type> interface is a prerequisite of
<type > ViewerEditableLossy</type> . The code
2012-05-05 12:51:16 +02:00
below shows how an implementation can implement both interfaces and
register their implementations:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2008-07-18 19:55:13 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_editable_lossy_compress (ViewerEditableLossy *editable)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFile *self = VIEWER_FILE (editable);
g_print ("File implementation of lossy editable interface compress method: %s.\n",
self->filename);
}
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
static void
viewer_file_editable_lossy_interface_init (ViewerEditableLossyInterface *iface)
{
iface->compress = viewer_file_editable_lossy_compress;
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_editable_save (ViewerEditable *editable,
GError **error)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFile *self = VIEWER_FILE (editable);
g_print ("File implementation of editable interface save method: %s.\n",
self->filename);
2004-01-22 19:39:45 +01:00
}
2008-07-18 19:55:13 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_editable_undo (ViewerEditable *editable,
guint n_steps)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFile *self = VIEWER_FILE (editable);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
g_print ("File implementation of editable interface undo method: %s.\n",
self->filename);
2012-05-05 12:51:16 +02:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_editable_redo (ViewerEditable *editable,
guint n_steps)
2012-05-05 12:51:16 +02:00
{
2015-10-18 20:34:44 +02:00
ViewerFile *self = VIEWER_FILE (editable);
2012-05-05 12:51:16 +02:00
2015-10-18 20:34:44 +02:00
g_print ("File implementation of editable interface redo method: %s.\n",
self->filename);
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_editable_interface_init (ViewerEditableInterface *iface)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
iface->save = viewer_file_editable_save;
iface->undo = viewer_file_editable_undo;
iface->redo = viewer_file_editable_redo;
2004-01-22 19:39:45 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_class_init (ViewerFileClass *klass)
2004-01-22 19:39:45 +01:00
{
2015-02-20 14:14:08 +01:00
/* Nothing here. */
2004-01-22 19:39:45 +01:00
}
2008-07-18 19:55:13 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_init (ViewerFile *self)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
/* Instance variable initialisation code. */
2004-01-22 19:39:45 +01:00
}
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
G_DEFINE_TYPE_WITH_CODE (ViewerFile, viewer_file, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE,
viewer_file_editable_interface_init)
G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE_LOSSY,
viewer_file_editable_lossy_interface_init))
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2008-07-18 19:55:13 +02:00
It is very important to notice that the order in which interface
implementations are added to the main object is not random:
<function > <link linkend= "g-type-add-interface-static" > g_type_add_interface_static</link> </function> ,
2012-05-05 12:51:16 +02:00
which is called by
<function > <link linkend= "G-DEFINE-INTERFACE:CAPS" > G_IMPLEMENT_INTERFACE</link> </function> ,
must be invoked first on the interfaces which have no prerequisites and then on
2008-07-18 19:55:13 +02:00
the others.
2007-02-10 23:08:42 +01:00
</para>
</sect1>
<sect1 id= "howto-interface-properties" >
2012-05-05 12:51:16 +02:00
<title > Interface properties</title>
2007-02-10 23:08:42 +01:00
<para >
2012-05-05 12:51:16 +02:00
GObject interfaces can also have
2008-07-18 19:55:13 +02:00
properties. Declaration of the interface properties is similar to
declaring the properties of ordinary GObject types as explained in
2012-05-05 12:51:16 +02:00
<xref linkend= "gobject-properties" /> , except that
<function > <link linkend= "g-object-interface-install-property" > g_object_interface_install_property</link> </function>
is used to declare the properties instead of
<function > <link linkend= "g-object-class-install-property" > g_object_class_install_property</link> </function> .
2007-02-10 23:08:42 +01:00
</para>
<para >
2015-10-18 20:34:44 +02:00
To include a property named 'autosave-frequency' of type <type > gdouble</type> in the
<type > ViewerEditable</type> interface example code above, we only need to
add one call in <function > viewer_editable_default_init</function> as shown
2015-02-20 14:04:45 +01:00
below:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2004-11-04 15:52:33 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_editable_default_init (ViewerEditableInterface *iface)
2004-11-04 15:52:33 +01:00
{
2014-06-20 19:25:03 +02:00
g_object_interface_install_property (iface,
2015-10-18 20:34:44 +02:00
g_param_spec_double ("autosave-frequency",
"Autosave frequency",
"Frequency (in per-seconds) to autosave backups of the editable content at. "
"Or zero to disable autosaves.",
0.0, /* minimum */
G_MAXDOUBLE, /* maximum */
0.0, /* default */
2022-05-20 13:55:18 +02:00
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2004-11-04 15:52:33 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
2004-11-04 15:52:33 +01:00
2007-02-10 23:08:42 +01:00
<para >
One point worth noting is that the declared property wasn't assigned an
2008-07-18 19:55:13 +02:00
integer ID. The reason being that integer IDs of properties are used
2015-02-20 14:04:45 +01:00
only inside the <function > get_property</function> and
<function > set_property</function> virtual methods. Since interfaces
declare but do not <emphasis > implement</emphasis> properties, there is no
need to assign integer IDs to them.
2007-02-10 23:08:42 +01:00
</para>
<para >
2014-06-24 17:49:00 +02:00
An implementation declares and defines its properties in the usual
2008-07-18 19:55:13 +02:00
way as explained in <xref linkend= "gobject-properties" /> , except for one
2012-05-05 12:51:16 +02:00
small change: it can declare the properties of the interface it
2008-07-18 19:55:13 +02:00
implements using <function > <link linkend= "g-object-class-override-property" > g_object_class_override_property</link> </function>
instead of <function > <link linkend= "g-object-class-install-property" > g_object_class_install_property</link> </function> .
The following code snippet shows the modifications needed in the
2015-10-18 20:34:44 +02:00
<type > ViewerFile</type> declaration and implementation above:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
struct _ViewerFile
2008-07-18 19:55:13 +02:00
{
GObject parent_instance;
2015-10-18 20:34:44 +02:00
gdouble autosave_frequency;
2004-11-04 15:52:33 +01:00
};
enum
{
2015-10-18 20:34:44 +02:00
PROP_AUTOSAVE_FREQUENCY = 1,
2015-02-20 14:08:34 +01:00
N_PROPERTIES
2008-07-18 19:55:13 +02:00
};
2004-11-04 15:52:33 +01:00
static void
2015-10-18 20:34:44 +02:00
viewer_file_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
2004-11-04 15:52:33 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFile *file = VIEWER_FILE (object);
2004-11-04 15:52:33 +01:00
2008-07-18 19:55:13 +02:00
switch (prop_id)
{
2015-10-18 20:34:44 +02:00
case PROP_AUTOSAVE_FREQUENCY:
file->autosave_frequency = g_value_get_double (value);
2004-11-04 15:52:33 +01:00
break;
2008-07-18 19:55:13 +02:00
2004-11-04 15:52:33 +01:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
2008-07-18 19:55:13 +02:00
}
2004-11-04 15:52:33 +01:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
2004-11-04 15:52:33 +01:00
{
2015-10-18 20:34:44 +02:00
ViewerFile *file = VIEWER_FILE (object);
2004-11-04 15:52:33 +01:00
2008-07-18 19:55:13 +02:00
switch (prop_id)
{
2015-10-18 20:34:44 +02:00
case PROP_AUTOSAVE_FREQUENCY:
g_value_set_double (value, file->autosave_frequency);
2004-11-04 15:52:33 +01:00
break;
2008-07-18 19:55:13 +02:00
2004-11-04 15:52:33 +01:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
2008-07-18 19:55:13 +02:00
}
}
static void
2015-10-18 20:34:44 +02:00
viewer_file_class_init (ViewerFileClass *klass)
2008-07-18 19:55:13 +02:00
{
2015-02-20 14:08:34 +01:00
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
object_class->set_property = viewer_file_set_property;
object_class->get_property = viewer_file_get_property;
2008-07-18 19:55:13 +02:00
2015-10-18 20:34:44 +02:00
g_object_class_override_property (object_class, PROP_AUTOSAVE_FREQUENCY, "autosave-frequency");
2004-11-04 15:52:33 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
</sect1>
2012-05-05 12:51:16 +02:00
<sect1 id= "howto-interface-override" >
<title > Overriding interface methods</title>
<para >
2015-02-20 14:04:45 +01:00
If a base class already implements an interface and a derived
class needs to implement the same interface but needs to override certain
methods, you must reimplement the interface and set only the interface
methods which need overriding.
2012-05-05 12:51:16 +02:00
</para>
<para >
2015-10-18 20:34:44 +02:00
In this example, <type > ViewerAudioFile</type> is derived from
<type > ViewerFile</type> . Both implement the <type > ViewerEditable</type>
interface. <type > ViewerAudioFile</type> only implements one method of the
<type > ViewerEditable</type> interface and uses the base class implementation of
2015-02-20 13:51:18 +01:00
the other.
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2012-05-05 12:51:16 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_audio_file_editable_save (ViewerEditable *editable,
GError **error)
2012-05-05 12:51:16 +02:00
{
2015-10-18 20:34:44 +02:00
ViewerAudioFile *self = VIEWER_AUDIO_FILE (editable);
2015-02-20 14:14:08 +01:00
2015-10-18 20:34:44 +02:00
g_print ("Audio file implementation of editable interface save method.\n");
2012-05-05 12:51:16 +02:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_audio_file_editable_interface_init (ViewerEditableInterface *iface)
2012-05-05 12:51:16 +02:00
{
2015-10-18 20:34:44 +02:00
/* Override the implementation of save(). */
iface->save = viewer_audio_file_editable_save;
2012-05-05 12:51:16 +02:00
/*
2015-10-18 20:34:44 +02:00
* Leave iface->undo and ->redo alone, they are already set to the
2012-05-05 12:51:16 +02:00
* base class implementation.
*/
}
2015-10-18 20:34:44 +02:00
G_DEFINE_TYPE_WITH_CODE (ViewerAudioFile, viewer_audio_file, VIEWER_TYPE_FILE,
G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE,
viewer_audio_file_editable_interface_init))
2012-05-05 12:51:16 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_audio_file_class_init (ViewerAudioFileClass *klass)
2012-05-05 12:51:16 +02:00
{
2015-02-20 14:12:49 +01:00
/* Nothing here. */
2012-05-05 12:51:16 +02:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_audio_file_init (ViewerAudioFile *self)
2012-05-05 12:51:16 +02:00
{
2015-02-20 14:12:49 +01:00
/* Nothing here. */
2012-05-05 12:51:16 +02:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2012-05-05 12:51:16 +02:00
</para>
<para >
To access the base class interface implementation use
<function > <link linkend= "g-type-interface-peek-parent" > g_type_interface_peek_parent</link> </function>
from within an interface's <function > default_init</function> function.
</para>
<para >
2015-02-20 14:04:45 +01:00
To call the base class implementation of an interface
2019-08-23 23:45:16 +02:00
method from a derived class where than interface method has been
2015-02-20 14:04:45 +01:00
overridden, stash away the pointer returned from
2012-05-05 12:51:16 +02:00
<function > <link linkend= "g-type-interface-peek-parent" > g_type_interface_peek_parent</link> </function>
in a global variable.
</para>
<para >
2015-10-18 20:34:44 +02:00
In this example <type > ViewerAudioFile</type> overrides the
<function > save</function> interface method. In its overridden method
2012-05-05 12:51:16 +02:00
it calls the base class implementation of the same interface method.
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
static ViewerEditableInterface *viewer_editable_parent_interface = NULL;
2012-05-05 12:51:16 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_audio_file_editable_save (ViewerEditable *editable,
GError **error)
2012-05-05 12:51:16 +02:00
{
2015-10-18 20:34:44 +02:00
ViewerAudioFile *self = VIEWER_AUDIO_FILE (editable);
2015-02-20 14:12:49 +01:00
2015-10-18 20:34:44 +02:00
g_print ("Audio file implementation of editable interface save method.\n");
2012-05-05 12:51:16 +02:00
2015-02-20 14:12:49 +01:00
/* Now call the base implementation */
2015-10-18 20:34:44 +02:00
viewer_editable_parent_interface->save (editable, error);
2012-05-05 12:51:16 +02:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_audio_file_editable_interface_init (ViewerEditableInterface *iface)
2012-05-05 12:51:16 +02:00
{
2015-10-18 20:34:44 +02:00
viewer_editable_parent_interface = g_type_interface_peek_parent (iface);
2015-02-20 14:12:49 +01:00
2015-10-18 20:34:44 +02:00
iface->save = viewer_audio_file_editable_save;
2012-05-05 12:51:16 +02:00
}
2015-10-18 20:34:44 +02:00
G_DEFINE_TYPE_WITH_CODE (ViewerAudioFile, viewer_audio_file, VIEWER_TYPE_FILE,
G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE,
viewer_audio_file_editable_interface_init))
2012-05-05 12:51:16 +02:00
static void
2015-10-18 20:34:44 +02:00
viewer_audio_file_class_init (ViewerAudioFileClass *klass)
2012-05-05 12:51:16 +02:00
{
2015-02-20 14:12:49 +01:00
/* Nothing here. */
2012-05-05 12:51:16 +02:00
}
static void
2015-10-18 20:34:44 +02:00
viewer_audio_file_init (ViewerAudioFile *self)
2012-05-05 12:51:16 +02:00
{
2015-02-20 14:12:49 +01:00
/* Nothing here. */
2012-05-05 12:51:16 +02:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2012-05-05 12:51:16 +02:00
</para>
</sect1>
2005-04-22 12:27:37 +02:00
</chapter>
2007-02-10 23:08:42 +01:00
<!-- End Howto Interfaces -->
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<chapter id= "howto-signals" >
2007-11-13 08:10:42 +01:00
<title > How to create and use signals</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:04:45 +01:00
The signal system in GType is pretty complex and
2008-07-18 19:55:13 +02:00
flexible: it is possible for its users to connect at runtime any
number of callbacks (implemented in any language for which a binding
exists)
2007-02-10 23:08:42 +01:00
<footnote >
2008-07-18 19:55:13 +02:00
<para > A Python callback can be connected to any signal on any
2013-06-12 16:18:29 +02:00
C-based GObject, and vice versa, assuming that the Python object
inherits from GObject.</para>
2007-02-10 23:08:42 +01:00
</footnote>
to any signal and to stop the emission of any signal at any
2008-07-18 19:55:13 +02:00
state of the signal emission process. This flexibility makes it
2015-02-20 14:04:45 +01:00
possible to use GSignal for much more than just emitting signals to
multiple clients.
2007-02-10 23:08:42 +01:00
</para>
<sect1 id= "howto-simple-signals" >
2008-07-18 19:55:13 +02:00
<title > Simple use of signals</title>
2004-01-22 19:39:45 +01:00
2007-02-10 23:08:42 +01:00
<para >
2015-02-20 14:04:45 +01:00
The most basic use of signals is to implement event
2015-10-18 20:34:44 +02:00
notification. For example, given a <type > ViewerFile</type> object with
2015-02-20 14:04:45 +01:00
a <function > write</function> method, a signal could be emitted whenever
the file is changed using that method.
2008-07-18 19:55:13 +02:00
The code below shows how the user can connect a callback to the
"changed" signal.
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2015-10-18 20:34:44 +02:00
file = g_object_new (VIEWER_FILE_TYPE, NULL);
2004-01-22 19:39:45 +01:00
2015-02-20 14:04:45 +01:00
g_signal_connect (file, "changed", (GCallback) changed_event, NULL);
2004-01-22 19:39:45 +01:00
2015-10-18 20:34:44 +02:00
viewer_file_write (file, buffer, strlen (buffer));
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2007-02-10 23:08:42 +01:00
</para>
<para >
2015-10-18 20:34:44 +02:00
The <type > ViewerFile</type> signal is registered in the
2015-02-20 13:51:18 +01:00
<function > class_init</function> function:
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2008-07-18 19:55:13 +02:00
file_signals[CHANGED] =
g_signal_newv ("changed",
2015-02-20 14:16:08 +01:00
G_TYPE_FROM_CLASS (object_class),
2004-06-10 11:41:43 +02:00
G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
2008-07-18 19:55:13 +02:00
NULL /* closure */,
2004-06-10 11:41:43 +02:00
NULL /* accumulator */,
2008-07-18 19:55:13 +02:00
NULL /* accumulator data */,
2015-02-24 09:50:53 +01:00
NULL /* C marshaller */,
2004-06-10 11:41:43 +02:00
G_TYPE_NONE /* return_type */,
0 /* n_params */,
NULL /* param_types */);
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2015-10-18 20:34:44 +02:00
and the signal is emitted in <function > viewer_file_write</function> :
2014-09-18 16:41:32 +02:00
<informalexample > <programlisting >
2008-07-18 19:55:13 +02:00
void
2015-10-18 20:34:44 +02:00
viewer_file_write (ViewerFile *self,
const guint8 *buffer,
gsize size)
2004-01-22 19:39:45 +01:00
{
2015-10-18 20:34:44 +02:00
g_return_if_fail (VIEWER_IS_FILE (self));
2015-02-20 14:04:45 +01:00
g_return_if_fail (buffer != NULL || size == 0);
2004-06-10 11:41:43 +02:00
/* First write data. */
2008-07-18 19:55:13 +02:00
2004-06-10 11:41:43 +02:00
/* Then, notify user of data written. */
2008-07-18 19:55:13 +02:00
g_signal_emit (self, file_signals[CHANGED], 0 /* details */);
2004-01-22 19:39:45 +01:00
}
2014-09-18 16:41:32 +02:00
</programlisting> </informalexample>
2015-02-20 14:04:45 +01:00
As shown above, the details parameter can safely be set to zero if no
detail needs to be conveyed. For a discussion of what it can be used for,
see <xref linkend= "signal-detail" />
2008-07-18 19:55:13 +02:00
</para>
<para >
2015-02-24 09:50:53 +01:00
The C signal marshaller should always be <literal > NULL</literal> , in which
case the best marshaller for the given closure type will be chosen by
GLib. This may be an internal marshaller specific to the closure type, or
2015-02-20 14:04:45 +01:00
<function > g_cclosure_marshal_generic</function> , which implements generic
conversion of arrays of parameters to C callback invocations. GLib used to
2015-02-24 09:50:53 +01:00
require the user to write or generate a type-specific marshaller and pass
that, but that has been deprecated in favour of automatic selection of
marshallers.
</para>
<para >
Note that <function > g_cclosure_marshal_generic</function> is slower than
non-generic marshallers, so should be avoided for performance critical
code. However, performance critical code should rarely be using signals
anyway, as emitting a signal blocks on emitting it to all listeners, which
has potentially unbounded cost.
2007-02-10 23:08:42 +01:00
</para>
</sect1>
2005-04-22 12:27:37 +02:00
</chapter>
2010-01-07 09:47:20 +01:00
</part>