add back ghost/guardedalloc from trunk
[blender-staging.git] / intern / ghost / intern / GHOST_EventManager.cpp
1 /*
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file ghost/intern/GHOST_EventManager.cpp
30  *  \ingroup GHOST
31  */
32
33
34 /**
35
36  * $Id$
37  * Copyright (C) 2001 NaN Technologies B.V.
38  * @author      Maarten Gribnau
39  * @date        May 14, 2001
40  */
41
42 #include "GHOST_EventManager.h"
43 #include <algorithm>
44 #include "GHOST_Debug.h"
45
46
47 GHOST_EventManager::GHOST_EventManager()
48 {
49 }
50
51
52 GHOST_EventManager::~GHOST_EventManager()
53 {
54         disposeEvents();
55
56         TConsumerVector::iterator iter= m_consumers.begin();
57         while (iter != m_consumers.end())
58         {
59                 GHOST_IEventConsumer* consumer = *iter;
60                 delete consumer;
61                 m_consumers.erase(iter);
62                 iter = m_consumers.begin();
63         }
64 }
65
66
67 GHOST_TUns32 GHOST_EventManager::getNumEvents()
68 {
69         return (GHOST_TUns32) m_events.size();
70 }
71
72
73 GHOST_TUns32 GHOST_EventManager::getNumEvents(GHOST_TEventType type)
74 {
75         GHOST_TUns32 numEvents = 0;
76         TEventStack::iterator p;
77         for (p = m_events.begin(); p != m_events.end(); p++) {
78                 if ((*p)->getType() == type) {
79                         numEvents++;
80                 }
81         }
82         return numEvents;
83 }
84
85
86 GHOST_IEvent* GHOST_EventManager::peekEvent()
87 {
88         GHOST_IEvent* event = 0;
89         if (m_events.size() > 0) {
90                 event = m_events.back();
91         }
92         return event;
93 }
94
95
96 GHOST_TSuccess GHOST_EventManager::pushEvent(GHOST_IEvent* event)
97 {
98         GHOST_TSuccess success;
99         GHOST_ASSERT(event, "invalid event");
100         if (m_events.size() < m_events.max_size()) {
101                 m_events.push_front(event);
102                 success = GHOST_kSuccess;
103         }
104         else {
105                 success = GHOST_kFailure;
106         }
107         return success;
108 }
109
110
111 bool GHOST_EventManager::dispatchEvent(GHOST_IEvent* event)
112 {
113         bool handled;
114         if (event) {
115                 handled = true;
116                 TConsumerVector::iterator iter;
117                 for (iter = m_consumers.begin(); iter != m_consumers.end(); iter++) {
118                         if ((*iter)->processEvent(event)) {
119                                 handled = false;
120                         }
121                 }
122         }
123         else {
124                 handled = false;
125         }
126         return handled;
127 }
128
129
130 bool GHOST_EventManager::dispatchEvent()
131 {
132         GHOST_IEvent* event = popEvent(); 
133         bool handled = false;
134         if (event) {
135                 handled = dispatchEvent(event);
136                 delete event;
137         }
138         return handled;
139 }
140
141
142 bool GHOST_EventManager::dispatchEvents()
143 {
144         bool handled;
145         if (getNumEvents()) {
146                 handled = true;
147                 while (getNumEvents()) {
148                         if (!dispatchEvent()) {
149                                 handled = false;
150                         }
151                 }
152         }
153         else {
154                 handled = false;
155         }
156         return handled;
157 }
158
159
160 GHOST_TSuccess GHOST_EventManager::addConsumer(GHOST_IEventConsumer* consumer)
161 {
162         GHOST_TSuccess success;
163         GHOST_ASSERT(consumer, "invalid consumer");
164         
165         // Check to see whether the consumer is already in our list
166         TConsumerVector::const_iterator iter = std::find(m_consumers.begin(), m_consumers.end(), consumer);
167
168         if (iter == m_consumers.end()) {
169                 // Add the consumer
170                 m_consumers.push_back(consumer);
171                 success = GHOST_kSuccess;
172         }
173         else {
174                 success = GHOST_kFailure;
175         }
176         return success;
177 }
178
179
180 GHOST_TSuccess GHOST_EventManager::removeConsumer(GHOST_IEventConsumer* consumer)
181 {
182         GHOST_TSuccess success;
183         GHOST_ASSERT(consumer, "invalid consumer");
184
185         // Check to see whether the consumer is in our list
186         TConsumerVector::iterator iter = std::find(m_consumers.begin(), m_consumers.end(), consumer);
187
188         if (iter != m_consumers.end()) {
189                 // Remove the consumer
190                 m_consumers.erase(iter);
191                 success = GHOST_kSuccess;
192         }
193         else {
194                 success = GHOST_kFailure;
195         }
196         return success;
197 }
198
199
200 void GHOST_EventManager::removeWindowEvents(GHOST_IWindow* window)
201 {
202         TEventStack::iterator iter;
203         iter = m_events.begin();
204         while (iter != m_events.end())
205         {
206                 GHOST_IEvent* event = *iter;
207                 if (event->getWindow() == window)
208                 {
209             GHOST_PRINT("GHOST_EventManager::removeWindowEvents(): removing event\n");
210                         /*
211                          * Found an event for this window, remove it.
212                          * The iterator will become invalid.
213                          */
214                         delete event;
215                         m_events.erase(iter);
216                         iter = m_events.begin();
217                 }
218                 else
219                 {
220                         iter++;
221                 }
222         }
223 }
224
225 void GHOST_EventManager::removeTypeEvents(GHOST_TEventType type, GHOST_IWindow* window)
226 {
227         TEventStack::iterator iter;
228         iter = m_events.begin();
229         while (iter != m_events.end())
230         {
231                 GHOST_IEvent* event = *iter;
232                 if ((event->getType() == type) && (!window || (event->getWindow() == window)))
233                 {
234             GHOST_PRINT("GHOST_EventManager::removeTypeEvents(): removing event\n");
235                         /*
236                          * Found an event of this type for the window, remove it.
237                          * The iterator will become invalid.
238                          */
239                         delete event;
240                         m_events.erase(iter);
241                         iter = m_events.begin();
242                 }
243                 else
244                 {
245                         iter++;
246                 }
247         }
248 }
249
250
251 GHOST_IEvent* GHOST_EventManager::popEvent()
252 {
253         GHOST_IEvent* event = peekEvent();
254         if (event) {
255                 m_events.pop_back();
256         }
257         return event;
258 }
259
260
261 void GHOST_EventManager::disposeEvents()
262 {
263         while (m_events.size() > 0) {
264                 GHOST_ASSERT(m_events[0], "invalid event");
265                 delete m_events[0];
266                 m_events.pop_front();
267         }
268 }