Blame docs/reference/gobject/html/howto-interface.html

Packit ae235b
Packit ae235b
<html>
Packit ae235b
<head>
Packit ae235b
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Packit ae235b
<title>How to define and implement interfaces: GObject Reference Manual</title>
Packit ae235b
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
Packit ae235b
<link rel="home" href="index.html" title="GObject Reference Manual">
Packit ae235b
<link rel="up" href="pt02.html" title="Part IV. Tutorial">
Packit ae235b
<link rel="prev" href="howto-gobject-chainup.html" title="Chaining up">
Packit ae235b
<link rel="next" href="howto-interface-implement.html" title="Implementing interfaces">
Packit ae235b
<meta name="generator" content="GTK-Doc V1.27 (XML mode)">
Packit ae235b
<link rel="stylesheet" href="style.css" type="text/css">
Packit ae235b
</head>
Packit ae235b
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
Packit ae235b
Packit ae235b
Packit ae235b
Home
Packit ae235b
Up
Packit ae235b
Prev
Packit ae235b
Next
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
How to define and implement interfaces
Packit ae235b
Packit ae235b
Defining interfaces
Packit ae235b
Implementing interfaces
Packit ae235b
Interface definition prerequisites
Packit ae235b
Interface properties
Packit ae235b
Overriding interface methods
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Defining interfaces
Packit ae235b

Packit ae235b
    The theory behind how GObject interfaces work is given in
Packit ae235b
    the section called “Non-instantiable classed types: interfaces”; this section covers how to
Packit ae235b
    define and implement an interface.
Packit ae235b
  

Packit ae235b

Packit ae235b
    The first step is to get the header right. This interface
Packit ae235b
    defines three methods:
Packit ae235b

Packit ae235b
Packit ae235b
  
Packit ae235b
    
Packit ae235b
      
Packit ae235b
        
1
Packit ae235b
2
Packit ae235b
3
Packit ae235b
4
Packit ae235b
5
Packit ae235b
6
Packit ae235b
7
Packit ae235b
8
Packit ae235b
9
Packit ae235b
10
Packit ae235b
11
Packit ae235b
12
Packit ae235b
13
Packit ae235b
14
Packit ae235b
15
Packit ae235b
16
Packit ae235b
17
Packit ae235b
18
Packit ae235b
19
Packit ae235b
20
Packit ae235b
21
Packit ae235b
22
Packit ae235b
23
Packit ae235b
24
Packit ae235b
25
Packit ae235b
26
Packit ae235b
27
Packit ae235b
28
Packit ae235b
29
Packit ae235b
30
Packit ae235b
31
Packit ae235b
32
Packit ae235b
33
Packit ae235b
34
Packit ae235b
35
Packit ae235b
36
Packit ae235b
        
/*
Packit ae235b
 * Copyright/Licensing information.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#ifndef __VIEWER_EDITABLE_H__
Packit ae235b
#define __VIEWER_EDITABLE_H__
Packit ae235b
Packit ae235b
#include <glib-object.h>
Packit ae235b
Packit ae235b
G_BEGIN_DECLS
Packit ae235b
Packit ae235b
#define VIEWER_TYPE_EDITABLE viewer_editable_get_type ()
Packit ae235b
G_DECLARE_INTERFACE (ViewerEditable, viewer_editable, VIEWER, EDITABLE, GObject)
Packit ae235b
Packit ae235b
struct _ViewerEditableInterface
Packit ae235b
{
Packit ae235b
  GTypeInterface parent_iface;
Packit ae235b
Packit ae235b
  void (*save) (ViewerEditable  *self,
Packit ae235b
                GError         **error);
Packit ae235b
  void (*undo) (ViewerEditable  *self,
Packit ae235b
                guint            n_steps);
Packit ae235b
  void (*redo) (ViewerEditable  *self,
Packit ae235b
                guint            n_steps);
Packit ae235b
};
Packit ae235b
Packit ae235b
void viewer_editable_save (ViewerEditable  *self,
Packit ae235b
                           GError         **error);
Packit ae235b
void viewer_editable_undo (ViewerEditable  *self,
Packit ae235b
                           guint            n_steps);
Packit ae235b
void viewer_editable_redo (ViewerEditable  *self,
Packit ae235b
                           guint            n_steps);
Packit ae235b
Packit ae235b
G_END_DECLS
Packit ae235b
Packit ae235b
#endif /* __VIEWER_EDITABLE_H__ */
Packit ae235b
      
Packit ae235b
    
Packit ae235b
  
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
    This code is the same as the code for a normal GType
Packit ae235b
    which derives from a GObject except for a few details:
Packit ae235b
    

Packit ae235b
    Packit ae235b
  • Packit ae235b
            The _GET_CLASS function is called
    Packit ae235b
            _GET_IFACE (and is defined by
    Packit ae235b
            G_DECLARE_INTERFACE).
    Packit ae235b
          

    Packit ae235b
  • Packit ae235b
            The instance type, ViewerEditable, is not fully defined: it is
    Packit ae235b
            used merely as an abstract type which represents an instance of
    Packit ae235b
            whatever object which implements the interface.
    Packit ae235b
          

    Packit ae235b
  • Packit ae235b
            The parent of the ViewerEditableInterface is
    Packit ae235b
            GTypeInterface, not GObjectClass.
    Packit ae235b
          

    Packit ae235b
    Packit ae235b

    Packit ae235b
      

    Packit ae235b

    Packit ae235b
        The implementation of the ViewerEditable type itself is trivial:
    Packit ae235b
        

    Packit ae235b
      Packit ae235b
    • G_DEFINE_INTERFACE

    • Packit ae235b
             creates a viewer_editable_get_type function which registers the
      Packit ae235b
             type in the type system. The third argument is used to define a
      Packit ae235b
             prerequisite interface
      Packit ae235b
             (which we'll talk about more later). Just pass 0 for this
      Packit ae235b
             argument when an interface has no prerequisite.
      Packit ae235b
             

      Packit ae235b
    • viewer_editable_default_init is expected

    • Packit ae235b
            to register the interface's signals if there are any (we will see a bit
      Packit ae235b
            later how to use them).

      Packit ae235b
    • The interface methods viewer_editable_save,

    • Packit ae235b
            viewer_editable_undo and viewer_editable_redo dereference the interface
      Packit ae235b
            structure to access its associated interface function and call it.
      Packit ae235b
            

      Packit ae235b
      Packit ae235b

      Packit ae235b

      Packit ae235b
      Packit ae235b
        
      Packit ae235b
          
      Packit ae235b
            
      Packit ae235b
              
      1
      Packit ae235b
      2
      Packit ae235b
      3
      Packit ae235b
      4
      Packit ae235b
      5
      Packit ae235b
      6
      Packit ae235b
      7
      Packit ae235b
      8
      Packit ae235b
      9
      Packit ae235b
      10
      Packit ae235b
      11
      Packit ae235b
      12
      Packit ae235b
      13
      Packit ae235b
      14
      Packit ae235b
      15
      Packit ae235b
      16
      Packit ae235b
      17
      Packit ae235b
      18
      Packit ae235b
      19
      Packit ae235b
      20
      Packit ae235b
      21
      Packit ae235b
      22
      Packit ae235b
      23
      Packit ae235b
      24
      Packit ae235b
      25
      Packit ae235b
      26
      Packit ae235b
      27
      Packit ae235b
      28
      Packit ae235b
      29
      Packit ae235b
      30
      Packit ae235b
      31
      Packit ae235b
      32
      Packit ae235b
      33
      Packit ae235b
      34
      Packit ae235b
      35
      Packit ae235b
      36
      Packit ae235b
      37
      Packit ae235b
      38
      Packit ae235b
      39
      Packit ae235b
      40
      Packit ae235b
      41
      Packit ae235b
      42
      Packit ae235b
      43
      Packit ae235b
      44
      Packit ae235b
      45
      Packit ae235b
      46
      Packit ae235b
      47
      Packit ae235b
              
      G_DEFINE_INTERFACE (ViewerEditable, viewer_editable, G_TYPE_OBJECT)
      Packit ae235b
      Packit ae235b
      static void
      Packit ae235b
      viewer_editable_default_init (ViewerEditableInterface *iface)
      Packit ae235b
      {
      Packit ae235b
          /* add properties and signals to the interface here */
      Packit ae235b
      }
      Packit ae235b
      Packit ae235b
      void
      Packit ae235b
      viewer_editable_save (ViewerEditable  *self,
      Packit ae235b
                            GError         **error)
      Packit ae235b
      {
      Packit ae235b
        ViewerEditableInterface *iface;
      Packit ae235b
      Packit ae235b
        g_return_if_fail (VIEWER_IS_EDITABLE (self));
      Packit ae235b
        g_return_if_fail (error == NULL || *error == NULL);
      Packit ae235b
      Packit ae235b
        iface = VIEWER_EDITABLE_GET_IFACE (self);
      Packit ae235b
        g_return_if_fail (iface->save != NULL);
      Packit ae235b
        iface->save (self, error);
      Packit ae235b
      }
      Packit ae235b
      Packit ae235b
      void
      Packit ae235b
      viewer_editable_undo (ViewerEditable *self,
      Packit ae235b
                            guint           n_steps)
      Packit ae235b
      {
      Packit ae235b
        ViewerEditableInterface *iface;
      Packit ae235b
      Packit ae235b
        g_return_if_fail (VIEWER_IS_EDITABLE (self));
      Packit ae235b
      Packit ae235b
        iface = VIEWER_EDITABLE_GET_IFACE (self);
      Packit ae235b
        g_return_if_fail (iface->undo != NULL);
      Packit ae235b
        iface->undo (self, n_steps);
      Packit ae235b
      }
      Packit ae235b
      Packit ae235b
      void
      Packit ae235b
      viewer_editable_redo (ViewerEditable *self,
      Packit ae235b
                            guint           n_steps)
      Packit ae235b
      {
      Packit ae235b
        ViewerEditableInterface *iface;
      Packit ae235b
      Packit ae235b
        g_return_if_fail (VIEWER_IS_EDITABLE (self));
      Packit ae235b
      Packit ae235b
        iface = VIEWER_EDITABLE_GET_IFACE (self);
      Packit ae235b
        g_return_if_fail (iface->redo != NULL);
      Packit ae235b
        iface->redo (self, n_steps);
      Packit ae235b
      }
      Packit ae235b
            
      Packit ae235b
          
      Packit ae235b
        
      Packit ae235b
      Packit ae235b
      Packit ae235b

      Packit ae235b
          

      Packit ae235b
      Packit ae235b
      Packit ae235b
      Packit ae235b

      Generated by GTK-Doc V1.27
      Packit ae235b
      </body>
      Packit ae235b
      </html>