| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ |
| 2 |
/* gdome-xml-eventl.c |
| 3 |
* |
| 4 |
* Copyright (C) 1999 Raph Levien <[email protected]> |
| 5 |
* Copyright (C) 2000 Mathieu Lacage <[email protected]> |
| 6 |
* Copyright (C) 2001 Paolo Casarini <[email protected]> |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Lesser General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2.1 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this library; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
# include <config.h> |
| 25 |
#endif |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include "gdome.h" |
| 29 |
#include "gdome-events.h" |
| 30 |
#include "gdome-refdebug.h" |
| 31 |
#include "gdome-evt-util.h" |
| 32 |
#include "gdome-evt-eventl.h" |
| 33 |
#include "gdome-evt-event.h" |
| 34 |
|
| 35 |
|
| 36 |
const GdomeEventListenerVtab gdome_evt_evntl_vtab = |
| 37 |
{ |
| 38 |
gdome_evt_evntl_ref, |
| 39 |
gdome_evt_evntl_unref, |
| 40 |
gdome_evt_evntl_query_interface, |
| 41 |
gdome_evt_evntl_handleEvent |
| 42 |
}; |
| 43 |
|
| 44 |
/** |
| 45 |
* gdome_evt_evntl_aux_mkref: |
| 46 |
* @callback: the callback function that is to be called when |
| 47 |
* gdome_evntl_handleEvent() is called. |
| 48 |
* @priv: private data |
| 49 |
* @priv_callback: the callback function to be called just before |
| 50 |
* the event listener is destroyed, so that the |
| 51 |
* client can do something sensible with the |
| 52 |
* priv field (like unreffing it) |
| 53 |
* |
| 54 |
* Returns: a new allocated #GdomeEventListener. |
| 55 |
*/ |
| 56 |
GdomeEventListener * |
| 57 |
gdome_evt_evntl_aux_mkref (void (*callback) (GdomeEventListener *self, |
| 58 |
GdomeEvent *event, |
| 59 |
GdomeException *exc), |
| 60 |
gpointer priv, |
| 61 |
void (*priv_callback)(GdomeEventListener* self)) |
| 62 |
{ |
| 63 |
Gdome_evt_EventListener *result = NULL; |
| 64 |
|
| 65 |
result = g_new (Gdome_evt_EventListener, 1); |
| 66 |
#ifdef DEBUG_REFCNT |
| 67 |
gdome_refdbg_addRef ((void *)result, GDOME_REFDBG_EVENTLISTENER); |
| 68 |
#endif |
| 69 |
result->refcnt = 1; |
| 70 |
result->vtab = (GdomeEventListenerVtab *) &gdome_evt_evntl_vtab; |
| 71 |
result->super.user_data = NULL; |
| 72 |
|
| 73 |
result->callback = callback; |
| 74 |
result->priv = priv; |
| 75 |
result->priv_callback = priv_callback; |
| 76 |
|
| 77 |
return (GdomeEventListener *)result; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* gdome_evt_evntl_mkref: |
| 83 |
* @callback: the callback function that is to be called when |
| 84 |
* gdome_evntl_handleEvent() is called. |
| 85 |
* @priv: private data |
| 86 |
* |
| 87 |
* Returns: a new allocated #GdomeEventListener. |
| 88 |
*/ |
| 89 |
GdomeEventListener * |
| 90 |
gdome_evt_evntl_mkref (void (*callback) (GdomeEventListener *self, |
| 91 |
GdomeEvent *event, |
| 92 |
GdomeException *exc), |
| 93 |
gpointer priv) |
| 94 |
{ |
| 95 |
return gdome_evt_evntl_aux_mkref(callback, priv, NULL); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/* |
| 100 |
an extension to the DOM spec. You can attach abitrary data |
| 101 |
to the listener and get it back through this function |
| 102 |
when the listener is called. |
| 103 |
This is used specifically by Gill. |
| 104 |
It is baaaad. I need to find another way to do this fast. |
| 105 |
*/ |
| 106 |
/** |
| 107 |
* gdome_evt_evntl_get_priv: |
| 108 |
* @self: EventListener Object Ref |
| 109 |
* |
| 110 |
* DEPRECATED |
| 111 |
* Returns: the priv data field associated with this object |
| 112 |
*/ |
| 113 |
gpointer |
| 114 |
gdome_evt_evntl_get_priv (GdomeEventListener *self) |
| 115 |
{ |
| 116 |
Gdome_evt_EventListener *priv = (Gdome_evt_EventListener *) self; |
| 117 |
|
| 118 |
g_return_val_if_fail (priv != NULL, NULL); |
| 119 |
g_return_val_if_fail (GDOME_XML_IS_EVNTL (priv), NULL); |
| 120 |
|
| 121 |
return priv->priv; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* gdome_evt_evntl_ref: |
| 126 |
* @self: EventListener Object ref |
| 127 |
* @exc: Exception Object ref |
| 128 |
* |
| 129 |
* Increase the reference count of the specified EventListener. |
| 130 |
*/ |
| 131 |
void |
| 132 |
gdome_evt_evntl_ref (GdomeEventListener *self, GdomeException *exc) |
| 133 |
{ |
| 134 |
Gdome_evt_EventListener *priv = (Gdome_evt_EventListener *)self; |
| 135 |
|
| 136 |
g_return_if_fail (priv != NULL); |
| 137 |
g_return_if_fail (GDOME_XML_IS_EVNTL (priv)); |
| 138 |
g_return_if_fail (exc != NULL); |
| 139 |
|
| 140 |
priv->refcnt++; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* gdome_evt_evntl_unref: |
| 145 |
* @self: EventListener Object ref |
| 146 |
* @exc: Exception Object ref |
| 147 |
* |
| 148 |
* Decrease the reference count of the specified EventListener. Free the |
| 149 |
* EventListener structure if the EventListener will have zero reference. |
| 150 |
*/ |
| 151 |
void |
| 152 |
gdome_evt_evntl_unref (GdomeEventListener *self, GdomeException *exc) |
| 153 |
{ |
| 154 |
Gdome_evt_EventListener *priv = (Gdome_evt_EventListener *)self; |
| 155 |
|
| 156 |
g_return_if_fail (priv != NULL); |
| 157 |
g_return_if_fail (GDOME_XML_IS_EVNTL (priv)); |
| 158 |
g_return_if_fail (exc != NULL); |
| 159 |
|
| 160 |
if (--priv->refcnt == 0) { |
| 161 |
if (priv->priv_callback != NULL) priv->priv_callback(self); |
| 162 |
#ifdef DEBUG_REFCNT |
| 163 |
gdome_refdbg_delRef ((void *)self, GDOME_REFDBG_EVENTLISTENER); |
| 164 |
#endif |
| 165 |
g_free (self); |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* gdome_evt_evntl_query_interface: |
| 172 |
* @self: EventListener Object ref |
| 173 |
* @interface: name of the Interface needed. |
| 174 |
* @exc: Exception Object ref |
| 175 |
* |
| 176 |
* Returns: a reference to the object that implements the interface needed or |
| 177 |
* %NULL if this object doesn't implement the interface specified. |
| 178 |
*/ |
| 179 |
gpointer |
| 180 |
gdome_evt_evntl_query_interface (GdomeEventListener *self, const char *interface, GdomeException *exc) |
| 181 |
{ |
| 182 |
Gdome_evt_EventListener *priv = (Gdome_evt_EventListener *)self; |
| 183 |
|
| 184 |
g_return_val_if_fail (priv != NULL, NULL); |
| 185 |
g_return_val_if_fail (GDOME_XML_IS_EVNTL (priv), NULL); |
| 186 |
g_return_val_if_fail (interface != NULL, NULL); |
| 187 |
g_return_val_if_fail (exc != NULL, NULL); |
| 188 |
|
| 189 |
if (!strcmp (interface, "EventListener")) { |
| 190 |
priv->refcnt++; |
| 191 |
|
| 192 |
return self; |
| 193 |
} |
| 194 |
else |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* gdome_evt_evntl_handleEvent: |
| 200 |
* @self: EventListener Object ref |
| 201 |
* @evt: the Event Object ref that contains contextual information about the |
| 202 |
* event. |
| 203 |
* @exc: Exception Object ref |
| 204 |
* |
| 205 |
* This method is called whenever an event occurs of the type for which the |
| 206 |
* EventListener interface was registered. |
| 207 |
*/ |
| 208 |
void |
| 209 |
gdome_evt_evntl_handleEvent (GdomeEventListener *self, GdomeEvent *evt, GdomeException *exc) |
| 210 |
{ |
| 211 |
Gdome_evt_EventListener *priv = (Gdome_evt_EventListener *) self; |
| 212 |
|
| 213 |
g_return_if_fail (priv != NULL); |
| 214 |
g_return_if_fail (GDOME_XML_IS_EVNTL (priv)); |
| 215 |
g_return_if_fail (evt != NULL); |
| 216 |
g_return_if_fail (GDOME_XML_IS_EVNT (evt)); |
| 217 |
g_return_if_fail (exc != NULL); |
| 218 |
|
| 219 |
priv->callback (self, evt, exc); |
| 220 |
} |