Debian Codegraph @

lib/database/src/so/nodes/SoEventCallback.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.4 $
44 |
45 | Classes:
46 | SoEventCallback
47 |
48 | Author(s): David Mott
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/SoDB.h>
55 #include <Inventor/SoPickedPoint.h>
56 #include <Inventor/events/SoEvent.h>
57 #include <Inventor/nodes/SoEventCallback.h>
58
59 // internal class for storing event types, callback funcs, user data
60 typedef struct {
61 SoType eventType;
62 SoEventCallbackCB *func;
63 void *userData;
64 } SoEventCallbackData;
65
66 SO_NODE_SOURCE(SoEventCallback);
67
68 ////////////////////////////////////////////////////////////////////////
69 //
70 // Constructor
71 //
72 SoEventCallback::SoEventCallback()
73 //
74 ////////////////////////////////////////////////////////////////////////
75 {
76 SO_NODE_CONSTRUCTOR(SoEventCallback);
77
78 isBuiltIn = TRUE;
79 cblist = new SbPList;
80 pathOfInterest = NULL;
81 eventAction = NULL;
82 }
83
84 ////////////////////////////////////////////////////////////////////////
85 //
86 // Destructor
87 //
88 SoEventCallback::~SoEventCallback()
89 //
90 ////////////////////////////////////////////////////////////////////////
91 {
92 if (pathOfInterest != NULL)
93 pathOfInterest->unref();
94
95 // delete the data elements, and delete the callback list
96 for (int i = 0; i < cblist->getLength(); i++)
97 delete (SoEventCallbackData *) (*cblist)[i];
98 delete cblist;
99 }
100
101
102 ////////////////////////////////////////////////////////////////////////
103 //
104 // Pay attention to the passed type of event, calling f whenever
105 // and event of the passed type occurs. userData will be passed to f.
106 //
107 void
108 SoEventCallback::addEventCallback(
109 SoType eventType,
110 SoEventCallbackCB *f,
111 void *userData)
112 //
113 ////////////////////////////////////////////////////////////////////////
114 {
115 SoEventCallbackData *data = new SoEventCallbackData;
116 data->eventType = eventType;
117 data->func = f;
118 data->userData = userData;
119 cblist->append(data);
120 }
121
122 ////////////////////////////////////////////////////////////////////////
123 //
124 // No longer pay attention to the passed type of event for the function f.
125 //
126 void
127 SoEventCallback::removeEventCallback(
128 SoType eventType,
129 SoEventCallbackCB *f,
130 void *userData)
131 //
132 ////////////////////////////////////////////////////////////////////////
133 {
134 // loop from the end of the list, in case we remove items
135 for (int i = cblist->getLength() - 1; i >= 0; i--) {
136 SoEventCallbackData *data = (SoEventCallbackData *) (*cblist)[i];
137 if ((data->eventType == eventType) &&
138 (data->func == f) &&
139 (data->userData == userData)) {
140
141 // found the func/event type/data triplet - remove it!
142 cblist->remove(i);
143 delete data;
144 }
145 }
146 }
147
148 ////////////////////////////////////////////////////////////////////////
149 //
150 // This copies the passed path into pathOfInterest.
151 //
152 // Use: public
153 //
154 void
155 SoEventCallback::setPath(SoPath *path)
156 {
157 // ref the input path
158 if (path != NULL)
159 path->ref();
160
161 // nuke the old path
162 if (pathOfInterest != NULL) {
163 pathOfInterest->unref();
164 pathOfInterest = NULL;
165 }
166 // and copy the new path
167 if (path != NULL) {
168 pathOfInterest = path->copy();
169 pathOfInterest->ref();
170 }
171
172 // unref the input path
173 if (path != NULL)
174 path->unref();
175
176 }
177
178 ////////////////////////////////////////////////////////////////////////
179 //
180 // Invoke all callback funcs interested in the passed event.
181 //
182 // Use: private
183 //
184 void
185 SoEventCallback::invokeCallbacks(const SoEvent *e)
186 //
187 ////////////////////////////////////////////////////////////////////////
188 {
189 // Call all callback funcs interested in this event type
190 for (int i = 0; i < cblist->getLength(); i++) {
191 SoEventCallbackData *data = (SoEventCallbackData *) (*cblist)[i];
192 if (e->isOfType(data->eventType)) {
193 (*data->func) (data->userData, this);
194 }
195 }
196 }
197
198 ////////////////////////////////////////////////////////////////////////
199 //
200 // This is called for every SoHandleEventAction traversal.
201 //
202 // Use: protected
203 //
204 void
205 SoEventCallback::handleEvent(SoHandleEventAction *ha)
206 //
207 ////////////////////////////////////////////////////////////////////////
208 {
209 // set eventAction so that the app may call setHandled(), grab(), etc
210 eventAction = ha;
211
212 // Are we monitoring a path?
213 if (pathOfInterest == NULL)
214 invokeCallbacks(ha->getEvent());
215 else {
216 // make sure the path of interest was picked
217 const SoPickedPoint *pp = ha->getPickedPoint();
218
219 if ((pp != NULL) && pp->getPath()->containsPath(pathOfInterest))
220 invokeCallbacks(ha->getEvent());
221 }
222
223 eventAction = NULL;
224 }