Blender V2.61 - r43446

GHOST_EventManager.cpp

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