The SimpleCxxLib package


#include "gevents.h"

class GEvent

This class is the root of the hierarchy for all events. The structure of the GEvent hierarchy looks like this:

GEventHierarchy
GWindowEvent GActionEvent GMouseEvent GKeyEvent GTimerEvent

The standard paradigm for using GEvent is illustrated by the following program, which allows the user to draw lines on the graphics window:

   int main() {
      GWindow gw;
      GLine *line;
      cout << "This program lets the user draw lines by dragging." << endl;
      while (true) {
         GMouseEvent e = waitForEvent(MOUSE_EVENT);
         if (e.getEventType() == MOUSE_PRESSED) {
            line = new GLine(e.getX(), e.getY(), e.getX(), e.getY());
            gw.add(line);
         } else if (e.getEventType() == MOUSE_DRAGGED) {
            line->setEndPoint(e.getX(), e.getY());
         }
      }
   }
Methods
getEventClass() Returns the enumerated type constant indicating the class of the event.
getEventTime() Returns the system time in milliseconds at which the event occurred.
getEventType() Returns the enumerated type constant corresponding to the specific event type.
getModifiers() Returns an integer whose bits indicate what modifiers are in effect.
isValid() Returns true if the event is valid.
setEventTime(time) Sets the event time field for this event.
setModifiers(modifiers) Sets the modifiers field for this event.
toString() Converts the event to a human-readable representation of the event.
Functions
getNextEvent(mask) Checks to see if there are any events of the desired type waiting on the event queue.
waitForClick() Waits for a mouse click in any window, discarding any other events.
waitForEvent(mask) Dismisses the process until an event occurs whose type is covered by the event mask.

Method detail


EventClassType getEventClass() const;
Returns the enumerated type constant indicating the class of the event.

Usage:

EventClassType eventClass = e.getEventClass();

EventType getEventType() const;
Returns the enumerated type constant corresponding to the specific event type.

Usage:

EventType type = e.getEventType();

double getEventTime() const;
Returns the system time in milliseconds at which the event occurred. To ensure portability among systems that represent time in different ways, the SimpleCxxLib packages use type double to represent time, which is always encoded as the number of milliseconds that have elapsed since 00:00:00 UTC on January 1, 1970, which is the conventional zero point for computer-based time systems.

Usage:

double time = e.getEventTime();

int getModifiers() const;
Returns an integer whose bits indicate what modifiers are in effect. To check whether the shift key is down, for example, one could use the following code:
   if (e.getModifiers() & SHIFT_DOWN) ...

Usage:

int modifiers = e.getModifiers();

string toString() const;
Converts the event to a human-readable representation of the event.

Usage:

string str = e.toString();

bool isValid();
Returns true if the event is valid.

Usage:

if (e.isValid()) ...

void setEventTime(double time);
Sets the event time field for this event. The event system needs access to this method, but conventional clients don't.

Usage:

e.setEventTime(time);

void setModifiers(int modifiers);
Sets the modifiers field for this event. The event system needs access to this method, but conventional clients don't.

Usage:

e.setModifiers(modifiers);

Function detail


void waitForClick();
Waits for a mouse click in any window, discarding any other events.

Usage:

waitForClick();

GEvent waitForEvent(int mask = ANY_EVENT);
Dismisses the process until an event occurs whose type is covered by the event mask. The mask parameter is a combination of the events of interest. For example, to wait for a mouse event or an action event, clients can use the following call:
   e = waitForEvent(MOUSE_EVENT + ACTION_EVENT);
The mask parameter is optional. If it is missing, waitForEvent accepts any event.

As a more sophisticated example, the following code is the canonical event loop for an animated application that needs to respond to mouse, key, and timer events:

   GTimer timer(ANIMATION_DELAY_IN_MILLISECONDS);
   timer.start();
   while (true) {
      GEvent e = waitForEvent(TIMER_EVENT + MOUSE_EVENT + KEY_EVENT);
      switch (e.getEventClass()) {
       case TIMER_EVENT:
         takeAnimationStep();
         break;
       case MOUSE_EVENT:
         handleMouseEvent(GMouseEvent(e));
         break;
       case KEY_EVENT:
         handleKeyEvent(GKeyEvent(e));
         break;
      }
   }

Usage:

GEvent e = waitForEvent(mask);

GEvent getNextEvent(int mask = ANY_EVENT);
Checks to see if there are any events of the desired type waiting on the event queue. If so, this function returns the event in exactly the same fashion as waitForEvent; if not, getNextEvent returns an invalid event. The mask parameter is optional. If it is missing, getNextEvent accepts any event.

Usage:

GEvent e = getNextEvent(mask);