Index: trunk/src/lib/event/event_handler.cc
===================================================================
--- trunk/src/lib/event/event_handler.cc	(revision 7868)
+++ trunk/src/lib/event/event_handler.cc	(revision 7919)
@@ -29,6 +29,6 @@
 
 /**
- *  standard constructor
-*/
+ * @brief standard constructor
+ */
 EventHandler::EventHandler ()
 {
@@ -42,5 +42,6 @@
 
   /* now initialize them all to zero */
-  this->withUNICODE(false);
+  for (unsigned int i = 0; i < ES_NUMBER; i++)
+    this->bUNICODE[i] = false;
   this->grabEvents(false);
 
@@ -51,5 +52,5 @@
 
 /**
- *  the singleton reference to this class
+ * @brief the singleton reference to this class
 */
 EventHandler* EventHandler::singletonRef = NULL;
@@ -57,7 +58,6 @@
 
 /**
- *  standard deconstructor
-
-*/
+ * @brief standard deconstructor
+ */
 EventHandler::~EventHandler ()
 {
@@ -79,5 +79,5 @@
 
 /**
- *  initializes the event handler
+ * @brief initializes the event handler
  *
  * this has to be called before the use of the event handler
@@ -89,5 +89,44 @@
 
 /**
- * pushes the current State in the State-stack, and selects state
+ * @param state: to which the event handler shall change
+ */
+void EventHandler::setState(elState state)
+{
+  if (state == this->state)
+    return;
+
+  /// When Changing the State, all the keys will be released.
+  /// This is done by sending each eventListener, that still
+  /// has an Event subscribed, a Release Event.
+  int keyCount;
+  Uint8 * pressedKeys = SDL_GetKeyState(&keyCount);
+  for (unsigned int i = 0; i < SDLK_LAST; i++)
+  {
+    if (pressedKeys[i])
+    {
+      Event ev;
+      ev.bPressed = false;
+      ev.type = i;
+      if (unlikely(this->bUNICODE[this->state]))
+        ev.x = i;
+      this->dispachEvent(this->state, ev );
+    }
+  }
+
+  // switching to the new State.
+  elState oldState = this->state;
+  this->state = state;
+
+  // in the End the Corresponding handler will be notified.
+  Event stateSwitchEvent;
+  stateSwitchEvent.type = EV_LEAVE_STATE;
+  this->dispachEvent(oldState, stateSwitchEvent);
+
+  SDL_EnableUNICODE(this->bUNICODE[state]);
+};
+
+
+/**
+ * @brief pushes the current State in the State-stack, and selects state
  * @param state the new State to set
  */
@@ -106,5 +145,5 @@
 
 /**
- * this removes the topmost stack-entry and select the underlying one
+ * @brief this removes the topmost stack-entry and select the underlying one
  * @returns the next stack-entry
  */
@@ -144,19 +183,19 @@
   {
     for(unsigned int i = 0; i < ES_NUMBER; i++)
-    {
-      if( !likely(this->listeners[i][eventType].empty()))
+      if (!this->findListener( NULL, (elState)i, eventType, el))
+        this->listeners[i][eventType].push_back(el);
+      else
       {
-        PRINTF(2)("'%s' of class '%s' tried to subscribe to event %i @ state %i but this event has already been subscribed\n", el->getName(), el->getClassName(), eventType, state);
+        PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassName(), el->getName(), i, eventType);
       }
-      this->listeners[i][eventType].push_back(el);
-    }
-  }
-  else
-  {
-    if( likely(!this->listeners[state][eventType].empty()))
-    {
-      PRINTF(2)("%s of class %s tried to subscribe to event %i @ state %i but this event has already been subscribed\n", el->getName(), el->getClassName(), eventType, state);
-    }
-    this->listeners[state][eventType].push_back(el);
+  }
+  else
+  {
+    if (!this->findListener( NULL, state, eventType, el))
+      this->listeners[state][eventType].push_back(el);
+    else
+    {
+      PRINTF(2)("%s::%s was already subscribed to state %d event %d\n", el->getClassName(), el->getName(), state, eventType);
+    }
   }
 }
@@ -167,8 +206,8 @@
  * @param state: the stat in which it has been subscribed
  * @param eventType: the event, that shall be unsubscribed
-
-   if you want to unsubscribe an event listener from all subscribed events, just use the
-   unsubscribe(EventListener* el, elState state) function
-*/
+ *
+ * if you want to unsubscribe an event listener from all subscribed events, just use the
+ * unsubscribe(EventListener* el, elState state) function
+ */
 void EventHandler::unsubscribe(EventListener* el, elState state, int eventType)
 {
@@ -177,18 +216,12 @@
     for (unsigned int i = 0; i < ES_NUMBER; i++)
     {
-      std::vector<EventListener*>::iterator listener =
-         std::find(this->listeners[i][eventType].begin(),
-                   this->listeners[i][eventType].end(),
-                   el);
-      if (listener != this->listeners[i][eventType].end())
+      std::vector<EventListener*>::iterator listener;
+      if (this->findListener(&listener, (elState)i, eventType, el))
         this->listeners[i][eventType].erase(listener);
     }
   else
   {
-    std::vector<EventListener*>::iterator listener =
-        std::find(this->listeners[state][eventType].begin(),
-                  this->listeners[state][eventType].end(),
-                  el);
-    if (listener != this->listeners[state][eventType].end())
+    std::vector<EventListener*>::iterator listener;
+    if (this->findListener(&listener, state, eventType, el))
       this->listeners[state][eventType].erase(listener);
   }
@@ -203,6 +236,5 @@
 void EventHandler::unsubscribe(EventListener* el, elState state)
 {
-  if( el == NULL || state >= ES_NUMBER)
-    return;
+  assert( el != NULL && state < ES_NUMBER);
   if( state == ES_ALL)
   {
@@ -228,4 +260,10 @@
 }
 
+/**
+ * @brief returns true if at state and eventType there is something subscribed.
+ * @param state the state to check in.
+ * @param eventType the eventtype to check.
+ * @returns true if a event is subscibed.
+ */
 bool EventHandler::isSubscribed(elState state, int eventType)
 {
@@ -236,5 +274,5 @@
 
 /**
- * flush all registered events
+ * @brief flush all registered events
  * @param state: a specific state
 */
@@ -261,10 +299,32 @@
 
 
-void EventHandler::withUNICODE(bool enableUNICODE)
-{
-  SDL_EnableUNICODE(enableUNICODE);
-  this->bUNICODE = enableUNICODE;
-}
-
+bool EventHandler::findListener(std::vector<EventListener*>::iterator* it, elState state, int eventType, EventListener* listener)
+{
+  std::vector<EventListener*>::iterator findIterator =
+    std::find(this->listeners[state][eventType].begin(), this->listeners[state][eventType].end(), listener);
+  if (it != NULL)
+    *it = findIterator;
+  return ( findIterator != this->listeners[state][eventType].end());
+
+}
+
+
+
+/**
+ * @brief if the unicode characters should be recorded.
+ * @param state the State in whitch to set the new Value.
+ * @param enableUNICODE: enabled, or disabled.
+ */
+void EventHandler::withUNICODE(elState state, bool enableUNICODE)
+{
+  this->bUNICODE[state] = enableUNICODE;
+  if (this->state == state)
+    SDL_EnableUNICODE(enableUNICODE);
+}
+
+/**
+ * @brief grabs InputEvents.
+ * @param grabEvents if the Events should be grabbed(true) or released(false)
+ */
 void EventHandler::grabEvents(bool grabEvents)
 {
@@ -282,10 +342,12 @@
 }
 
-/**
- *  core function of event handler: receives all events from SDL
-
-   The event from the SDL framework are collected here and distributed to all listeners.
-*/
-void EventHandler::process()
+
+
+/**
+ * @brief core function of event handler: receives all events from SDL
+ *
+ * The event from the SDL framework are collected here and distributed to all listeners.
+ */
+void EventHandler::process() const
 {
   SDL_Event event;
@@ -299,5 +361,5 @@
         ev.bPressed = true;
         ev.type = event.key.keysym.sym;
-        if (unlikely(this->bUNICODE))
+        if (unlikely(this->bUNICODE[this->state]))
           ev.x = event.key.keysym.unicode;
         break;
@@ -305,5 +367,5 @@
         ev.bPressed = false;
         ev.type = event.key.keysym.sym;
-        if (unlikely(this->bUNICODE))
+        if (unlikely(this->bUNICODE[this->state]))
           ev.x = event.key.keysym.unicode;
         break;
@@ -344,4 +406,8 @@
         ev.type = EV_JOY_BUTTON;
         break;
+      case SDL_ACTIVEEVENT:
+        ev.type = EV_WINDOW_FOCUS;
+        ev.bPressed = (event.active.gain != 0);
+        break;
       case SDL_VIDEORESIZE:
         ev.resize = event.resize;
@@ -355,25 +421,41 @@
         break;
     }
-
-    /* small debug routine: shows all events dispatched by the event handler */
-    PRINT(4)("\n==========================| EventHandler::process () |===\n");
-    PRINT(4)("=  Got Event nr %i, for state %i", ev.type, this->state);
-
-
-    for (unsigned int i = 0; i < this->listeners[this->state][ev.type].size(); i++)
-    {
-      PRINT(4)("=  Event dispatcher msg: This event has been consumed\n");
-      PRINT(4)("=======================================================\n");
-      listeners[this->state][ev.type][i]->process(ev);
-    }
-    /*    else
-        {
-          PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
-          PRINT(4)("=======================================================\n");
-        }*/
-  }
-}
-
-
+    this->dispachEvent(this->state, ev);
+  }
+}
+
+
+/**
+ * @brief dispaches an Event.
+ * @param event the Event to dispach.
+ */
+void EventHandler::dispachEvent(elState state, const Event& event) const
+{
+  /* small debug routine: shows all events dispatched by the event handler */
+  PRINT(4)("\n==========================| EventHandler::process () |===\n");
+  PRINT(4)("=  Got Event nr %i, for state %i\n", event.type, state);
+
+  /// setting a temporary state in case of an EventListener's process changes the state.
+  for (unsigned int i = 0; i < this->listeners[state][event.type].size(); i++)
+  {
+    PRINT(4)("=  Event dispatcher msg: This event has been consumed\n");
+    PRINT(4)("=  Got Event nr %i, for state %i (%d registered) to %s::%s(%p)\n", event.type, i, state, this->listeners[state][event.type][i]->getClassName(), this->listeners[state][event.type][i]->getName(), this->listeners[state][event.type][i]);
+    PRINT(4)("=======================================================\n");
+    this->listeners[state][event.type][i]->process(event);
+  }
+  /*    else
+  {
+        PRINT(4)("=  Event dispatcher msg: This event has NOT been consumed\n");
+        PRINT(4)("=======================================================\n");
+  }*/
+}
+
+
+
+/**
+ * @brief An eventFilter.
+ * @param event the Event to be filtered.
+ * @returns 0 on filtered Event. 1 Otherwise.
+ */
 int EventHandler::eventFilter(const SDL_Event *event)
 {
@@ -400,5 +482,5 @@
 
 /**
- * outputs some nice information about the EventHandler
+ * @brief outputs some nice information about the EventHandler
  */
 void EventHandler::debug() const
@@ -408,7 +490,9 @@
   PRINT(0)("===============================\n");
   for(int i = 0; i < ES_NUMBER; ++i)
+  {
     for(int j = 0; j < EV_NUMBER; ++j)
       for (unsigned int evl = 0; evl < this->listeners[i][j].size(); evl++)
         PRINT(0)("Event %d of State %d subscribed to %s (%p)\n", j, i, this->listeners[i][j][evl]->getName(), this->listeners[i][j][evl]);
+  }
   PRINT(0)("============================EH=\n");
 }
