The SimpleCxxLib package


#include "gevents.h"

This file defines the event types used in the SimpleCxxLib graphics libraries. The structure of this package is adapted from the Java event model. The GEvent hierarchy looks like this:

GEventHierarchy
GEvent GWindowEvent GActionEvent GMouseEvent GKeyEvent GTimerEvent
Classes
GActionEvent This event subclass represents an action event.
GEvent This class is the root of the hierarchy for all events.
GKeyEvent This event subclass represents a key event.
GMouseEvent This event subclass represents a mouse event.
GTimerEvent This event subclass represents a timer event.
GWindowEvent This event subclass represents a window event.
Types
EventClassType This enumeration type defines the event classes.
EventType This enumeration type defines the event types for all events.
KeyCodes This type defines the names of the key codes returned in a key event.
ModifierCodes This enumeration type defines a set of constants used to check whether modifiers are in effect.
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.

Type detail


enum EventClassType {
   NULL_EVENT  x000,
   ACTION_EVENTx010,
   KEY_EVENT   x020,
   TIMER_EVENT x040,
   WINDOW_EVENTx080,
   MOUSE_EVENT x100,
   CLICK_EVENT x200,
   ANY_EVENT   x3F0
};
This enumeration type defines the event classes. The element values are each a single bit and can be added or ORed together to generate an event mask. The CLICK_EVENT class responds only to the MOUSE_CLICKED event type. The ANY_EVENT class selects any event.

typedef enum {
   WINDOW_CLOSED    = WINDOW_EVENT + 1,
   WINDOW_RESIZED   = WINDOW_EVENT + 2,
   ACTION_PERFORMED = ACTION_EVENT + 1,
   MOUSE_CLICKED    = MOUSE_EVENT + 1,
   MOUSE_PRESSED    = MOUSE_EVENT + 2,
   MOUSE_RELEASED   = MOUSE_EVENT + 3,
   MOUSE_MOVED      = MOUSE_EVENT + 4,
   MOUSE_DRAGGED    = MOUSE_EVENT + 5,
   KEY_PRESSED      = KEY_EVENT + 1,
   KEY_RELEASED     = KEY_EVENT + 2,
   KEY_TYPED        = KEY_EVENT + 3,
   TIMER_TICKED     = TIMER_EVENT + 1,
} EventType;
This enumeration type defines the event types for all events.

enum ModifierCodes {
   SHIFT_DOWN     = 1 << 0,
   CTRL_DOWN      = 1 << 1,
   META_DOWN      = 1 << 2,
   ALT_DOWN       = 1 << 3,
   ALT_GRAPH_DOWN = 1 << 4,
   BUTTON1_DOWN   = 1 << 5,
   BUTTON2_DOWN   = 1 << 6,
   BUTTON3_DOWN   = 1 << 7
};
This enumeration type defines a set of constants used to check whether modifiers are in effect.

enum KeyCodes {
   BACKSPACE_KEY = 8,
   TAB_KEY = 9,
   ENTER_KEY = 10,
   CLEAR_KEY = 12,
   ESCAPE_KEY = 27,
   PAGE_UP_KEY = 33,
   PAGE_DOWN_KEY = 34,
   END_KEY = 35,
   HOME_KEY = 36,
   LEFT_ARROW_KEY = 37,
   UP_ARROW_KEY = 38,
   RIGHT_ARROW_KEY = 39,
   DOWN_ARROW_KEY = 40,
   F1_KEY = 112,
   F2_KEY = 113,
   F3_KEY = 114,
   F4_KEY = 115,
   F5_KEY = 116,
   F6_KEY = 117,
   F7_KEY = 118,
   F8_KEY = 119,
   F9_KEY = 120,
   F10_KEY = 121,
   F11_KEY = 122,
   F12_KEY = 123,
   DELETE_KEY = 127,
   HELP_KEY = 156
};
This type defines the names of the key codes returned in a key event.

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);