2001-11-13 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / eventlistener.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /*
24  * accessibleeventlistener.c: bonobo implementation of SpiListener.idl,
25  *   with added ability to attach/remove in-process callbacks.
26  *
27  */
28
29 #ifdef SPI_DEBUG
30 #include <stdio.h>
31 #endif
32
33 #include <config.h>
34 #include <bonobo/Bonobo.h>
35 #include <libspi/Accessibility.h>
36
37 /*
38  * This pulls the definition for the BonoboObject (GType)
39  */
40 #include "accessibleeventlistener.h"
41
42 /*
43  * Our parent Gtk object type
44  */
45 #define PARENT_TYPE SPI_LISTENER_TYPE
46
47 /*
48  * A pointer to our parent object class
49  */
50 static SpiListenerClass *spi_accessible_event_listener_parent_class;
51
52 /*
53  * Implemented GObject::finalize
54  */
55 static void
56 spi_accessible_event_listener_object_finalize (GObject *object)
57 {
58 #ifdef SPI_DEBUG
59         fprintf(stderr, "spi_listener_object_finalize called\n");
60 #endif
61         ((GObjectClass *) spi_accessible_event_listener_parent_class)->finalize (object);
62 }
63
64 /*
65  * CORBA Accessibility::Listener::notifyEvent method implementation
66  */
67
68 static void
69 impl_accessible_event_notify_event (PortableServer_Servant     servant,
70                                     const Accessibility_Event *e,
71                                     CORBA_Environment         *ev)
72 {
73   int n;
74   int len;
75   VoidEventListenerCB cb;
76   SpiAccessibleEventListener *listener = SPI_ACCESSIBLE_EVENT_SPI_LISTENER (
77                                        bonobo_object_from_servant (servant));
78   len = g_list_length (listener->callbacks);
79
80   for (n=0; n<len; ++n)
81     {
82       cb =  (VoidEventListenerCB) g_list_nth_data (listener->callbacks, n);
83       if (cb)
84         {
85           (*cb) (e);
86         }
87     }
88   /* Accessibility_Accessible_unref (e->source, ev); */
89 }
90
91 static void
92 spi_accessible_event_listener_class_init (SpiAccessibleEventListenerClass *klass)
93 {
94         GObjectClass * object_class = (GObjectClass *) klass;
95         SpiListenerClass * spi_listener_class = (SpiListenerClass *) klass;
96         POA_Accessibility_EventListener__epv *epv = &spi_listener_class->epv;
97         spi_accessible_event_listener_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
98
99         object_class->finalize = spi_accessible_event_listener_object_finalize;
100
101         epv->notifyEvent = impl_accessible_event_notify_event;
102 }
103
104 static void
105 spi_accessible_event_listener_init (SpiAccessibleEventListener *listener)
106 {
107         listener->callbacks = NULL;
108 }
109
110 GType
111 spi_accessible_event_listener_get_type (void)
112 {
113         static GType type = 0;
114
115         if (!type) {
116                 static const GTypeInfo tinfo = {
117                         sizeof (SpiAccessibleEventListenerClass),
118                         (GBaseInitFunc) NULL,
119                         (GBaseFinalizeFunc) NULL,
120                         (GClassInitFunc) spi_accessible_event_listener_class_init,
121                         (GClassFinalizeFunc) NULL,
122                         NULL, /* class data */
123                         sizeof (SpiListener),
124                         0, /* n preallocs */
125                         (GInstanceInitFunc) spi_accessible_event_listener_init,
126                         NULL /* value table */
127                 };
128                 /*
129                  *   Here we use bonobo_type_unique instead of
130                  * gtk_type_unique, this auto-generates a load of
131                  * CORBA structures for us. All derived types must
132                  * use bonobo_type_unique.
133                  */
134                 type = bonobo_type_unique (
135                         PARENT_TYPE,
136                         POA_Accessibility_EventListener__init,
137                         NULL,
138                         G_STRUCT_OFFSET (SpiListenerClass, epv),
139                         &tinfo,
140                         "SpiAccessibleEventListener");
141         }
142
143         return type;
144 }
145
146 SpiAccessibleEventListener *
147 spi_accessible_event_listener_new ()
148 {
149     SpiAccessibleEventListener *retval =
150     SPI_ACCESSIBLE_EVENT_SPI_LISTENER (g_object_new (spi_accessible_event_listener_get_type (), NULL));
151     return retval;
152 }
153
154 void   spi_accessible_event_listener_add_callback (SpiAccessibleEventListener *listener,
155                                                VoidEventListenerCB callback)
156 {
157   listener->callbacks = g_list_append (listener->callbacks, callback);
158 }
159
160 void   spi_accessible_event_listener_remove_callback (SpiAccessibleEventListener *listener,
161                                                   VoidEventListenerCB callback)
162 {
163   listener->callbacks = g_list_remove (listener->callbacks, callback);
164 }