| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ |
| 2 |
/* gdome-xml-event.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 <string.h> |
| 27 |
#include "gdome.h" |
| 28 |
#include "gdome-events.h" |
| 29 |
#include "gdome-refdebug.h" |
| 30 |
#include "gdome-evt-util.h" |
| 31 |
#include "gdome-xml-str.h" |
| 32 |
#include "gdome-xml-node.h" |
| 33 |
#include "gdome-evt-event.h" |
| 34 |
|
| 35 |
static struct { |
| 36 |
const char* name; |
| 37 |
guint32 code; |
| 38 |
} eventNameTypeMap[] = { |
| 39 |
{ "DOMSubtreeModified", DOM_SUBTREE_MODIFIED_EVENT_TYPE }, |
| 40 |
{ "DOMNodeInserted", DOM_NODE_INSERTED_EVENT_TYPE }, |
| 41 |
{ "DOMNodeInsertedIntoDocument", DOM_NODE_INSERTED_INTO_DOCUMENT_EVENT_TYPE }, |
| 42 |
{ "DOMNodeRemoved", DOM_NODE_REMOVED_EVENT_TYPE }, |
| 43 |
{ "DOMNodeRemovedFromDocument", DOM_NODE_REMOVED_FROM_DOCUMENT_EVENT_TYPE }, |
| 44 |
{ "DOMAttrModified", DOM_ATTR_MODIFIED_EVENT_TYPE }, |
| 45 |
{ "DOMCharacterDataModified", DOM_CHARACTER_DATA_MODIFIED_EVENT_TYPE }, |
| 46 |
|
| 47 |
/* some wildcards */ |
| 48 |
{ "", 0 }, |
| 49 |
{ "*", DOM_ALL_EVENT_TYPES }, |
| 50 |
|
| 51 |
{ NULL, 0 } |
| 52 |
}; |
| 53 |
|
| 54 |
const GdomeEventVtab gdome_evt_evnt_vtab = { |
| 55 |
gdome_evt_evnt_ref, |
| 56 |
gdome_evt_evnt_unref, |
| 57 |
gdome_evt_evnt_query_interface, |
| 58 |
gdome_evt_evnt_bubbles, |
| 59 |
gdome_evt_evnt_cancelable, |
| 60 |
gdome_evt_evnt_currentTarget, |
| 61 |
gdome_evt_evnt_eventPhase, |
| 62 |
gdome_evt_evnt_target, |
| 63 |
gdome_evt_evnt_timeStamp, |
| 64 |
gdome_evt_evnt_type, |
| 65 |
gdome_evt_evnt_initEvent, |
| 66 |
gdome_evt_evnt_preventDefault, |
| 67 |
gdome_evt_evnt_stopPropagation, |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* gdome_evt_evnt_mkref: |
| 73 |
* |
| 74 |
* Returns: a new allocated #GdomeEvent. |
| 75 |
*/ |
| 76 |
GdomeEvent * |
| 77 |
gdome_evt_evnt_mkref (void) { |
| 78 |
Gdome_evt_Event *result = NULL; |
| 79 |
|
| 80 |
result = g_new (Gdome_evt_Event, 1); |
| 81 |
#ifdef DEBUG_REFCNT |
| 82 |
gdome_refdbg_addRef ((void *)result, GDOME_REFDBG_EVENT); |
| 83 |
#endif |
| 84 |
memset(result, 0, sizeof(Gdome_evt_Event)); |
| 85 |
result->vtab = &gdome_evt_evnt_vtab; |
| 86 |
result->etype = GDOME_BASE_EVENT; |
| 87 |
result->refcnt = 1; |
| 88 |
result->super.user_data = NULL; |
| 89 |
|
| 90 |
return (GdomeEvent *)result; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* gdome_evt_evnt_ref: |
| 95 |
* @self: Event Object ref |
| 96 |
* @exc: Exception Object ref |
| 97 |
* |
| 98 |
* Increase the reference count of the specified Event. |
| 99 |
*/ |
| 100 |
void |
| 101 |
gdome_evt_evnt_ref (GdomeEvent *self, GdomeException *exc) |
| 102 |
{ |
| 103 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 104 |
|
| 105 |
g_return_if_fail (priv != NULL); |
| 106 |
g_return_if_fail (GDOME_XML_IS_EVNT (priv)); |
| 107 |
g_return_if_fail (exc != NULL); |
| 108 |
|
| 109 |
priv->refcnt++; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* gdome_evt_evnt_unref: |
| 114 |
* @self: Event Object ref |
| 115 |
* @exc: Exception Object ref |
| 116 |
* |
| 117 |
* Decrease the reference count of the specified Event. Free the Event |
| 118 |
* structure if the Event will have zero reference. |
| 119 |
*/ |
| 120 |
void |
| 121 |
gdome_evt_evnt_unref (GdomeEvent *self, GdomeException *exc) |
| 122 |
{ |
| 123 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 124 |
|
| 125 |
g_return_if_fail (priv != NULL); |
| 126 |
g_return_if_fail (GDOME_XML_IS_EVNT (priv)); |
| 127 |
g_return_if_fail (exc != NULL); |
| 128 |
|
| 129 |
if (--priv->refcnt == 0) { |
| 130 |
#ifdef DEBUG_REFCNT |
| 131 |
gdome_refdbg_delRef ((void *)self, GDOME_REFDBG_EVENT); |
| 132 |
#endif |
| 133 |
g_free (self); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* gdome_evt_evnt_query_interface: |
| 139 |
* @self: Event Object ref |
| 140 |
* @interface: name of the Interface needed. |
| 141 |
* @exc: Exception Object ref |
| 142 |
* |
| 143 |
* Returns: a reference to the object that implements the interface needed or |
| 144 |
* %NULL if this object doesn't implement the interface specified. |
| 145 |
*/ |
| 146 |
gpointer |
| 147 |
gdome_evt_evnt_query_interface (GdomeEvent *self, const char *interface, GdomeException *exc) |
| 148 |
{ |
| 149 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 150 |
|
| 151 |
g_return_val_if_fail (priv != NULL, NULL); |
| 152 |
g_return_val_if_fail (GDOME_XML_IS_EVNT (priv), NULL); |
| 153 |
g_return_val_if_fail (interface != NULL, NULL); |
| 154 |
g_return_val_if_fail (exc != NULL, NULL); |
| 155 |
|
| 156 |
if (!strcmp (interface, "Event")) { |
| 157 |
priv->refcnt++; |
| 158 |
return self; |
| 159 |
} |
| 160 |
else |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* gdome_evt_evnt_bubbles: |
| 166 |
* @self: Event Object ref |
| 167 |
* @exc: Exception Object ref |
| 168 |
* |
| 169 |
* Returns: If the event can bubble the value is %TRUE, else the value is |
| 170 |
* %FALSE. |
| 171 |
*/ |
| 172 |
GdomeBoolean |
| 173 |
gdome_evt_evnt_bubbles (GdomeEvent *self, GdomeException *exc) |
| 174 |
{ |
| 175 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 176 |
|
| 177 |
g_return_val_if_fail (priv != NULL, FALSE); |
| 178 |
g_return_val_if_fail (GDOME_XML_IS_EVNT (priv), FALSE); |
| 179 |
g_return_val_if_fail (exc != NULL, FALSE); |
| 180 |
|
| 181 |
return priv->bubbles; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* gdome_evt_evnt_cancelable: |
| 186 |
* @self: Event Object ref |
| 187 |
* @exc: Exception Object ref |
| 188 |
* |
| 189 |
* Returns: If the default action can be prevented the value is %TRUE, else |
| 190 |
* the value is %FALSE. |
| 191 |
*/ |
| 192 |
GdomeBoolean |
| 193 |
gdome_evt_evnt_cancelable (GdomeEvent *self, GdomeException *exc) |
| 194 |
{ |
| 195 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 196 |
|
| 197 |
g_return_val_if_fail (priv != NULL, FALSE); |
| 198 |
g_return_val_if_fail (GDOME_XML_IS_EVNT (priv), FALSE); |
| 199 |
g_return_val_if_fail (exc != NULL, FALSE); |
| 200 |
|
| 201 |
return priv->cancelable; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* gdome_evt_evnt_currentTarget: |
| 206 |
* @self: Event Object ref |
| 207 |
* @exc: Exception Object ref |
| 208 |
* |
| 209 |
* Returns: the EventTarget whose EventListeners are currently being |
| 210 |
* processed. This is particularly useful during capturing and bubbling. |
| 211 |
*/ |
| 212 |
GdomeEventTarget * |
| 213 |
gdome_evt_evnt_currentTarget (GdomeEvent *self, GdomeException *exc) |
| 214 |
{ |
| 215 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 216 |
|
| 217 |
g_return_val_if_fail (priv != NULL, NULL); |
| 218 |
g_return_val_if_fail (GDOME_XML_IS_EVNT (priv), NULL); |
| 219 |
g_return_val_if_fail (exc != NULL, NULL); |
| 220 |
|
| 221 |
return (GdomeEventTarget *)gdome_xml_n_mkref (priv->currentTarget); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* gdome_evt_evnt_eventPhase: |
| 226 |
* @self: Event Object ref |
| 227 |
* @exc: Exception Object ref |
| 228 |
* |
| 229 |
* Returns: the phase of event flow is currently being evaluated. |
| 230 |
*/ |
| 231 |
unsigned short |
| 232 |
gdome_evt_evnt_eventPhase (GdomeEvent *self, GdomeException *exc) |
| 233 |
{ |
| 234 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 235 |
|
| 236 |
g_return_val_if_fail (priv != NULL, FALSE); |
| 237 |
g_return_val_if_fail (GDOME_XML_IS_EVNT (priv), FALSE); |
| 238 |
g_return_val_if_fail (exc != NULL, FALSE); |
| 239 |
|
| 240 |
return priv->eventPhase; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* gdome_evt_evnt_target: |
| 245 |
* @self: Event Object ref |
| 246 |
* @exc: Exception Object ref |
| 247 |
* |
| 248 |
* Returns: the EventTarget to which the event was originally dispatched. |
| 249 |
*/ |
| 250 |
GdomeEventTarget * |
| 251 |
gdome_evt_evnt_target (GdomeEvent *self, GdomeException *exc) |
| 252 |
{ |
| 253 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 254 |
GdomeNode *result = NULL; |
| 255 |
|
| 256 |
g_return_val_if_fail (priv != NULL, NULL); |
| 257 |
g_return_val_if_fail (GDOME_XML_IS_EVNT (priv), NULL); |
| 258 |
g_return_val_if_fail (exc != NULL, NULL); |
| 259 |
|
| 260 |
result = priv->target; |
| 261 |
gdome_xml_n_ref (result, exc); |
| 262 |
return (GdomeEventTarget *)result; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* gdome_evt_evnt_timeStamp: |
| 267 |
* @self: Event Object ref |
| 268 |
* @exc: Exception Object ref |
| 269 |
* |
| 270 |
* Returns: the time (in milliseconds relative to the epoch) at which the |
| 271 |
* event was created. Due to the fact that some systems may not provide this |
| 272 |
* information the value of timeStamp may be not available for all events. |
| 273 |
* When not available, a value of 0 will be returned. Examples of epoch time |
| 274 |
* are the time of the system start or 0:0:0 UTC 1st January 1970. |
| 275 |
*/ |
| 276 |
GdomeDOMTimeStamp |
| 277 |
gdome_evt_evnt_timeStamp (GdomeEvent *self, GdomeException *exc) |
| 278 |
{ |
| 279 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 280 |
|
| 281 |
g_return_val_if_fail (priv != NULL, 0); |
| 282 |
g_return_val_if_fail (GDOME_XML_IS_EVNT (priv), 0); |
| 283 |
g_return_val_if_fail (exc != NULL, 0); |
| 284 |
|
| 285 |
return priv->timestamp; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* gdome_evt_evnt_type: |
| 290 |
* @self: Event Object ref |
| 291 |
* @exc: Exception Object ref |
| 292 |
* |
| 293 |
* Returns: The name of the event (case-insensitive). The name must be an XML |
| 294 |
* name. |
| 295 |
*/ |
| 296 |
GdomeDOMString * |
| 297 |
gdome_evt_evnt_type (GdomeEvent *self, GdomeException *exc) |
| 298 |
{ |
| 299 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 300 |
|
| 301 |
g_return_val_if_fail (priv != NULL, NULL); |
| 302 |
g_return_val_if_fail (GDOME_XML_IS_EVNT (priv), NULL); |
| 303 |
g_return_val_if_fail (exc != NULL, NULL); |
| 304 |
|
| 305 |
return gdome_xml_str_mkref_dup (gdome_evt_evnt_nameOfCode (priv->type)); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* gdome_evt_evnt_initEventByCode |
| 310 |
* |
| 311 |
* see the documentation for gdome_evt_evnt_initEvent |
| 312 |
*/ |
| 313 |
void |
| 314 |
gdome_evt_evnt_initEventByCode (GdomeEvent *self, guint32 eventTypeArg, GdomeBoolean canBubbleArg, GdomeBoolean cancelableArg, GdomeException *exc) |
| 315 |
{ |
| 316 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 317 |
|
| 318 |
g_return_if_fail (priv != NULL); |
| 319 |
g_return_if_fail (GDOME_XML_IS_EVNT (priv)); |
| 320 |
g_return_if_fail (exc != NULL); |
| 321 |
|
| 322 |
/* todo: If you call this on an internally generated event, there |
| 323 |
will be memory problems. Maybe we just need a flag for whether it |
| 324 |
was internally generated or created through DocumentEvent. Maybe |
| 325 |
another flag to check the "before dispatch" phase. */ |
| 326 |
priv->type = eventTypeArg; |
| 327 |
priv->bubbles = canBubbleArg; |
| 328 |
priv->cancelable = cancelableArg; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* gdome_evt_evnt_initEvent: |
| 333 |
* @self: Event Object ref |
| 334 |
* @eventTypeArg: Specifies the event type. This type may be any event type |
| 335 |
* currently defined in this specification or a new event type. |
| 336 |
* The string must be an XML name. |
| 337 |
* Any new event type must not begin with any upper, lower, or |
| 338 |
* mixed case version of the string "DOM". |
| 339 |
* This prefix is reserved for future DOM event sets. It is |
| 340 |
* also strongly recommended that third parties adding their |
| 341 |
* own events use their own prefix to avoid confusion and |
| 342 |
* lessen the probability of conflicts with other new events. |
| 343 |
* @canBubbleArg: Specifies whether or not the event can bubble. |
| 344 |
* @cancelableArg: Specifies whether or not the event's default action can be |
| 345 |
* prevented. |
| 346 |
* @exc: Exception Object ref |
| 347 |
* |
| 348 |
* The initEvent method is used to initialize the value of an Event created |
| 349 |
* through the gdome_doc_createEvent() API. This method may only be called |
| 350 |
* before the Event has been dispatched via the dispatchEvent method, though |
| 351 |
* it may be called multiple times during that phase if necessary. If called |
| 352 |
* multiple times the final invocation takes precedence. If called from a |
| 353 |
* subclass of Event interface only the values specified in the initEvent |
| 354 |
* method are modified, all other attributes are left unchanged. |
| 355 |
*/ |
| 356 |
void |
| 357 |
gdome_evt_evnt_initEvent (GdomeEvent *self, GdomeDOMString *eventTypeArg, GdomeBoolean canBubbleArg, GdomeBoolean cancelableArg, GdomeException *exc) |
| 358 |
{ |
| 359 |
g_return_if_fail (eventTypeArg != NULL); |
| 360 |
gdome_evt_evnt_initEventByCode (self, gdome_evt_evnt_codeOfName (eventTypeArg->str), canBubbleArg, cancelableArg, exc); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* gdome_evt_evnt_preventDefault: |
| 365 |
* @self: Event Object ref |
| 366 |
* @exc: Exception Object ref |
| 367 |
* |
| 368 |
* If an event is cancelable, the preventDefault method is used to signify |
| 369 |
* that the event is to be canceled, meaning any default action normally |
| 370 |
* taken by the implementation as a result of the event will not occur. If, |
| 371 |
* during any stage of event flow, the preventDefault method is called the |
| 372 |
* event is canceled. Any default action associated with the event will not |
| 373 |
* occur. Calling this method for a non-cancelable event has no effect. Once |
| 374 |
* preventDefault has been called it will remain in effect throughout the |
| 375 |
* remainder of the event's propagation. This method may be used during any |
| 376 |
* stage of event flow. |
| 377 |
*/ |
| 378 |
void |
| 379 |
gdome_evt_evnt_preventDefault (GdomeEvent *self, GdomeException *exc) |
| 380 |
{ |
| 381 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 382 |
|
| 383 |
g_return_if_fail (priv != NULL); |
| 384 |
g_return_if_fail (GDOME_XML_IS_EVNT (priv)); |
| 385 |
g_return_if_fail (exc != NULL); |
| 386 |
|
| 387 |
if (priv->cancelable) |
| 388 |
priv->default_prevented = TRUE; |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* gdome_evt_evnt_stopPropagation: |
| 393 |
* @self: Event Object ref |
| 394 |
* @exc: Exception Object ref |
| 395 |
* |
| 396 |
* The stopPropagation method is used prevent further propagation of an event |
| 397 |
* during event flow. If this method is called by any EventListener the event |
| 398 |
* will cease propagating through the tree. The event will complete dispatch |
| 399 |
* to all listeners on the current EventTarget before event flow stops. This |
| 400 |
* method may be used during any stage of event flow. |
| 401 |
*/ |
| 402 |
void |
| 403 |
gdome_evt_evnt_stopPropagation (GdomeEvent *self, GdomeException *exc) |
| 404 |
{ |
| 405 |
Gdome_evt_Event *priv = (Gdome_evt_Event *)self; |
| 406 |
|
| 407 |
g_return_if_fail (priv != NULL); |
| 408 |
g_return_if_fail (GDOME_XML_IS_EVNT (priv)); |
| 409 |
g_return_if_fail (exc != NULL); |
| 410 |
|
| 411 |
priv->propagation_stopped = TRUE; |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* gdome_evt_evnt_codeOfName: |
| 416 |
* @name: name of the event |
| 417 |
* |
| 418 |
* Returns the internal code corresponding to the event whose name is @name |
| 419 |
*/ |
| 420 |
guint32 |
| 421 |
gdome_evt_evnt_codeOfName (const char *name) |
| 422 |
{ |
| 423 |
unsigned i; |
| 424 |
|
| 425 |
for (i = 0; eventNameTypeMap[i].name != NULL && strcmp(eventNameTypeMap[i].name, name); i++) ; |
| 426 |
|
| 427 |
if (eventNameTypeMap[i].name != NULL) |
| 428 |
return eventNameTypeMap[i].code; |
| 429 |
else |
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* gdome_evt_evnt_nameOfCode: |
| 435 |
* @code: code of the event |
| 436 |
* |
| 437 |
* Returns the symbolic name corresponding to the event whose code is @code |
| 438 |
*/ |
| 439 |
const char* |
| 440 |
gdome_evt_evnt_nameOfCode (unsigned int code) |
| 441 |
{ |
| 442 |
unsigned i; |
| 443 |
|
| 444 |
for (i = 0; eventNameTypeMap[i].name != NULL && eventNameTypeMap[i].code != code; i++) ; |
| 445 |
|
| 446 |
if (eventNameTypeMap[i].name != NULL) |
| 447 |
return eventNameTypeMap[i].name; |
| 448 |
else |
| 449 |
return NULL; |
| 450 |
} |