Debian Codegraph @

libgdome/events/gdome-evt-propagation.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
2 /* gdome-evt-propagation.c
3 *
4 * Copyright (C) 1999 Raph Levien <[email protected]>
5 * Copyright (C) 2000 Mathieu Lacage <[email protected]>
6 * Copyright (C) 2000 Anders Carlsson <[email protected]>
7 * Copyright (C) 2001 Paolo Casarini <[email protected]>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 /********************************************************************
25 ** Helper functions for the Implementation of Event Propagation **
26 ********************************************************************/
27
28 #include <glib.h>
29 #include <string.h>
30 #include <libxml/tree.h>
31 #include "gdome.h"
32 #include "gdome-events.h"
33 #include "gdome-xml-node.h"
34 #include "gdome-evt-event.h"
35 #include "gdome-evt-eventl.h"
36 #include "gdome-evt-propagation.h"
37
38 typedef struct _Gdome_evt_PropagationEl Gdome_evt_PropagationEl;
39 struct _Gdome_evt_PropagationEl {
40 GdomeEventListener *listener;
41 GdomeNode *target;
42 Gdome_evt_PropagationEl *next;
43 };
44
45 static void
46 add_timestamp (GdomeEvent *event)
47 {
48 Gdome_evt_Event *priv = (Gdome_evt_Event *)event;
49 GTimeVal tv;
50
51 g_get_current_time (&tv);
52
53 if (priv->timestamp == 0)
54 priv->timestamp = (tv.tv_sec * (long long)1000) + tv.tv_usec / 1000;
55 }
56
57 void
58 gdome_evt_invokeListeners (Gdome_evt_PropagationEl *list,
59 GdomeEvent *event)
60 {
61 Gdome_evt_PropagationEl *el;
62 Gdome_evt_Event *ev_priv = (Gdome_evt_Event *)event;
63 GdomeException exc = 0;
64
65 el = list;
66 while (el) {
67 if (!ev_priv->propagation_stopped) {
68 ev_priv->currentTarget = ((Gdome_xml_Node *)el->target)->n;
69 gdome_evntl_handleEvent (el->listener, event, &exc);
70 }
71
72 gdome_n_unref (el->target, &exc);
73 gdome_evntl_unref (el->listener, &exc);
74
75 el = el->next;
76 }
77 }
78
79 void
80 gdome_evt_fireEvent (GdomeEvent *event)
81 {
82 GMemChunk *mem_chunk;
83 Gdome_evt_PropagationEl *el;
84 Gdome_evt_PropagationEl *captureList = NULL;
85 Gdome_evt_PropagationEl *attargetList = NULL;
86 Gdome_evt_PropagationEl *bubbleList = NULL;
87 Gdome_evt_PropagationEl *bubbleList_lastEl = NULL;
88
89 Gdome_evt_Event *ev_priv = (Gdome_evt_Event *)event;
90 Gdome_xml_Node *priv = (Gdome_xml_Node *)ev_priv->target;
91 Gdome_xml_ListenerList *ll;
92 GdomeException exc = 0;
93
94 xmlNode *p;
95
96 mem_chunk = g_mem_chunk_create (Gdome_evt_PropagationEl, 128, G_ALLOC_ONLY);
97
98 add_timestamp (event);
99
100 /* Builds capture and bubble propagation lists */
101 p = priv->n->parent;
102 while (p != NULL) {
103
104 /* if this node has at least one listener */
105 if (p->_private != NULL && ((Gdome_xml_Node *)p->_private)->ll != NULL) {
106 Gdome_xml_Node *node = p->_private;
107
108 /* scans listeners list */
109 ll = node->ll;
110 while (ll) {
111 /* if the current listener is for this event */
112 if (ev_priv->type == ll->type) {
113 /* builds a new propagation el */
114 el = g_chunk_new (Gdome_evt_PropagationEl, mem_chunk);
115 gdome_evntl_ref (ll->listener, &exc);
116 el->listener = ll->listener;
117 gdome_n_ref ((GdomeNode *)node, &exc);
118 el->target = (GdomeNode *)node;
119 el->next = NULL;
120
121 /* prepends it in the capture list */
122 if (ll->useCapture) {
123 if (captureList != NULL) {
124 el->next = captureList;
125 }
126 captureList = el;
127 }
128 /* appends it in the bubble list if the event makes bubbles */
129 else if (ev_priv->bubbles) {
130 if (bubbleList == NULL) {
131 bubbleList = el;
132 } else {
133 g_assert (bubbleList_lastEl != NULL);
134 bubbleList_lastEl->next = el;
135 }
136 bubbleList_lastEl = el;
137 }
138 }
139 ll = ll->next;
140 }
141 }
142 p = p->parent;
143 }
144
145 /* Builds attarget propagation list */
146 ll = priv->ll;
147 while (ll) {
148 /* if the current listener is for this event */
149 if (!ll->useCapture && ev_priv->type == ll->type) {
150 /* builds a new propagation el */
151 el = g_chunk_new (Gdome_evt_PropagationEl, mem_chunk);
152 gdome_evntl_ref (ll->listener, &exc);
153 el->listener = ll->listener;
154 gdome_n_ref ((GdomeNode *)priv, &exc);
155 el->target = (GdomeNode *)priv;
156 el->next = NULL;
157
158 /* prepends it in the attarget list */
159 if (attargetList != NULL) {
160 el->next = attargetList;
161 }
162 attargetList = el;
163 }
164 ll = ll->next;
165 }
166
167 /* Flow control flags init */
168 ev_priv->default_prevented = FALSE;
169 ev_priv->propagation_stopped = FALSE;
170
171 /* CAPTURING PHASE */
172 if (captureList != NULL) {
173 ev_priv->eventPhase = GDOME_CAPTURING_PHASE;
174 gdome_evt_invokeListeners (captureList, event);
175 }
176
177 /* AT_TARGET */
178 if (attargetList != NULL) {
179 ev_priv->eventPhase = GDOME_AT_TARGET;
180 gdome_evt_invokeListeners (attargetList, event);
181 }
182
183 /* BUBBLING PHASE */
184 if (bubbleList != NULL) {
185 ev_priv->eventPhase = GDOME_BUBBLING_PHASE;
186 gdome_evt_invokeListeners (bubbleList, event);
187 }
188
189 g_mem_chunk_destroy (mem_chunk);
190 }