Class/Instance Initializers

You will have to set up a class initializer for the GObject if your AtkObject implementation either:

  1. Redefines any function calls defined by the object's parent. This is typically necessary when an object needs to implement a function like atk_object_get_n_accessible_children(). This is necessary if the object has children, but they are not represented with widgets.

    For example, if your ATK implementation needs to over-ride the AtkObject function get_name(), then the class initializer would look like:

    Example 1-8Class initializer that overrides parent's get_name() function
    myatkimp_mytype_class_init (GailLabelClass *klass) 
      AtkObjectClass *class = ATK_OBJECT_CLASS (klass); 
      class->get_name = myatkimp_mytype_get_name; 
  2. Requires a parent->init, parent->notify_gtk, or parent->finalize function. This example defines all three:

    Example 1-9Class initializer that defines its own init(), notify_gtk() and finalize() functions
    static ParentObjectType *parent_class = NULL; 
    myatkimp_mytype_class_init (GailLabelClass *klass) 
       ParentObjectType *parent_class = (ParentObjectType*)klass; 
        * Caching the parent_class is necessary if the init, 
        * notify_gtk, or finalize functions are set up. 
        parent_class = g_type_class_ref (MYATKIMP_TYPE_PARENT); 
        parent_class->init = myatkimp_mytype_widget_init; 
        parent_class->notify_gtk = myatkimp_mytype_real_notify_gtk; 
        parent_class->finalize = myatkimp_mytype_finalize; 
    1. parent->init

      A parent->init() function may be necessary if the ATK implementation needs to do either of the following:

      1. Cache any data obtained from a backing GTK widget.
      2. Listen to any signals from the backing GTK widget.

      Here is an example of both:

      Example 1-10A custom init() function
      gail_tree_view_widget_init (MyatkimpMytype  *mytype, 
                                  GtkWidget       *gtk_widget) 
         /* Make sure to call the parent's init function */ 
         parent_class->init (widget, gtk_widget); 
         /* Cache a value in the ATK implementation */ 
         mytype->cached_value = gtk_widget_function_call(); 
         /* Listen to a signal */ 
         gtk_signal_connect (GTK_OBJECT (gtk_widget), 
                             GTK_SIGNAL_FUNC (_myatkimp_mytype_signal_type), 

      In this example, if the specified signal-type signal were generated on the backing gtk_widget, then the _myatkimp_mytype_signal_type() function would be called.

    2. parent->notify_gtk

      If the ATK implementation needs to listen to any property notifications on the backing GTK object, a parent->notify_gtk() function may be necessary. For example:

      Example 1-11A custom notify_gtk() function
      myatkimp_mytype_real_notify_gtk (GObject    *obj, 
                                       GParamSpec *pspec) 
         GtkWidget *widget = GTK_WIDGET (obj); 
         AtkObject* atk_obj = gtk_widget_get_accessible (widget); 
         if (strcmp (pspec->name, "property-of-interest") == 0) 
            /* Handle the property change. */ 
            parent_class->notify_gtk (obj, pspec); 
    3. parent->finalize

      If it is necessary to free any data when a GObject instance is destroyed, then a finalize() function is needed to free the memory. For example:

      Example 1-12A custom finalize() function
      myatkimp_mytype_finalize (GObject *object) 
         MyAtkimpMyType *my_type = MYATKIMP_MYTYPE (object); 
         g_object_unref (my_type->cached_value); 
         G_OBJECT_CLASS (parent_class)->finalize (object);