Blender  V2.59
GHOST_EventManager.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: GHOST_EventManager.cpp 38908 2011-08-02 04:28:05Z merwin $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00042 #include "GHOST_EventManager.h"
00043 #include <algorithm>
00044 #include "GHOST_Debug.h"
00045 #include <stdio.h> // [mce] temp debug
00046 
00047 GHOST_EventManager::GHOST_EventManager()
00048 {
00049 }
00050 
00051 
00052 GHOST_EventManager::~GHOST_EventManager()
00053 {
00054         disposeEvents();
00055 
00056         TConsumerVector::iterator iter= m_consumers.begin();
00057         while (iter != m_consumers.end())
00058         {
00059                 GHOST_IEventConsumer* consumer = *iter;
00060                 delete consumer;
00061                 m_consumers.erase(iter);
00062                 iter = m_consumers.begin();
00063         }
00064 }
00065 
00066 
00067 GHOST_TUns32 GHOST_EventManager::getNumEvents()
00068 {
00069         return (GHOST_TUns32) m_events.size();
00070 }
00071 
00072 
00073 GHOST_TUns32 GHOST_EventManager::getNumEvents(GHOST_TEventType type)
00074 {
00075         GHOST_TUns32 numEvents = 0;
00076         TEventStack::iterator p;
00077         for (p = m_events.begin(); p != m_events.end(); p++) {
00078                 if ((*p)->getType() == type) {
00079                         numEvents++;
00080                 }
00081         }
00082         return numEvents;
00083 }
00084 
00085 
00086 GHOST_IEvent* GHOST_EventManager::peekEvent()
00087 {
00088         GHOST_IEvent* event = 0;
00089         if (m_events.size() > 0) {
00090                 event = m_events.back();
00091         }
00092         return event;
00093 }
00094 
00095 
00096 GHOST_TSuccess GHOST_EventManager::pushEvent(GHOST_IEvent* event)
00097 {
00098         GHOST_TSuccess success;
00099         GHOST_ASSERT(event, "invalid event");
00100         if (m_events.size() < m_events.max_size()) {
00101                 m_events.push_front(event);
00102                 success = GHOST_kSuccess;
00103         }
00104         else {
00105                 success = GHOST_kFailure;
00106         }
00107         return success;
00108 }
00109 
00110 
00111 bool GHOST_EventManager::dispatchEvent(GHOST_IEvent* event)
00112 {
00113         bool handled;
00114         if (event) {
00115                 handled = true;
00116                 TConsumerVector::iterator iter;
00117                 for (iter = m_consumers.begin(); iter != m_consumers.end(); iter++) {
00118                         if ((*iter)->processEvent(event)) {
00119                                 handled = false;
00120                         }
00121                 }
00122         }
00123         else {
00124                 handled = false;
00125         }
00126         return handled;
00127 }
00128 
00129 
00130 bool GHOST_EventManager::dispatchEvent()
00131 {
00132         GHOST_IEvent* event = popEvent(); 
00133         bool handled = false;
00134         if (event) {
00135                 handled = dispatchEvent(event);
00136                 delete event;
00137         }
00138         return handled;
00139 }
00140 
00141 
00142 bool GHOST_EventManager::dispatchEvents()
00143 {
00144         bool handled;
00145         if (getNumEvents()) {
00146                 handled = true;
00147                 while (getNumEvents()) {
00148                         if (!dispatchEvent()) {
00149                                 handled = false;
00150                         }
00151                 }
00152         }
00153         else {
00154                 handled = false;
00155         }
00156         return handled;
00157 }
00158 
00159 
00160 GHOST_TSuccess GHOST_EventManager::addConsumer(GHOST_IEventConsumer* consumer)
00161 {
00162         GHOST_TSuccess success;
00163         GHOST_ASSERT(consumer, "invalid consumer");
00164         
00165         // Check to see whether the consumer is already in our list
00166         TConsumerVector::const_iterator iter = std::find(m_consumers.begin(), m_consumers.end(), consumer);
00167 
00168         if (iter == m_consumers.end()) {
00169                 // Add the consumer
00170                 m_consumers.push_back(consumer);
00171                 success = GHOST_kSuccess;
00172         }
00173         else {
00174                 success = GHOST_kFailure;
00175         }
00176         return success;
00177 }
00178 
00179 
00180 GHOST_TSuccess GHOST_EventManager::removeConsumer(GHOST_IEventConsumer* consumer)
00181 {
00182         GHOST_TSuccess success;
00183         GHOST_ASSERT(consumer, "invalid consumer");
00184 
00185         // Check to see whether the consumer is in our list
00186         TConsumerVector::iterator iter = std::find(m_consumers.begin(), m_consumers.end(), consumer);
00187 
00188         if (iter != m_consumers.end()) {
00189                 // Remove the consumer
00190                 m_consumers.erase(iter);
00191                 success = GHOST_kSuccess;
00192         }
00193         else {
00194                 success = GHOST_kFailure;
00195         }
00196         return success;
00197 }
00198 
00199 
00200 void GHOST_EventManager::removeWindowEvents(GHOST_IWindow* window)
00201 {
00202         TEventStack::iterator iter;
00203         iter = m_events.begin();
00204         while (iter != m_events.end())
00205         {
00206                 GHOST_IEvent* event = *iter;
00207                 if (event->getWindow() == window)
00208                 {
00209             GHOST_PRINT("GHOST_EventManager::removeWindowEvents(): removing event\n");
00210                         /*
00211                          * Found an event for this window, remove it.
00212                          * The iterator will become invalid.
00213                          */
00214                         delete event;
00215                         m_events.erase(iter);
00216                         iter = m_events.begin();
00217                 }
00218                 else
00219                 {
00220                         iter++;
00221                 }
00222         }
00223 }
00224 
00225 void GHOST_EventManager::removeTypeEvents(GHOST_TEventType type, GHOST_IWindow* window)
00226 {
00227         TEventStack::iterator iter;
00228         iter = m_events.begin();
00229         while (iter != m_events.end())
00230         {
00231                 GHOST_IEvent* event = *iter;
00232                 if ((event->getType() == type) && (!window || (event->getWindow() == window)))
00233                 {
00234             GHOST_PRINT("GHOST_EventManager::removeTypeEvents(): removing event\n");
00235                         /*
00236                          * Found an event of this type for the window, remove it.
00237                          * The iterator will become invalid.
00238                          */
00239                         delete event;
00240                         m_events.erase(iter);
00241                         iter = m_events.begin();
00242                 }
00243                 else
00244                 {
00245                         iter++;
00246                 }
00247         }
00248 }
00249 
00250 
00251 GHOST_IEvent* GHOST_EventManager::popEvent()
00252 {
00253         GHOST_IEvent* event = peekEvent();
00254         if (event) {
00255                 m_events.pop_back();
00256         }
00257         return event;
00258 }
00259 
00260 
00261 void GHOST_EventManager::disposeEvents()
00262 {
00263         while (m_events.size() > 0) {
00264                 GHOST_ASSERT(m_events[0], "invalid event");
00265                 delete m_events[0];
00266                 m_events.pop_front();
00267         }
00268 }