event handling classes
This commit is contained in:
parent
1210e6d688
commit
e53fbcf092
4 changed files with 294 additions and 0 deletions
49
otk/eventdispatcher.cc
Normal file
49
otk/eventdispatcher.cc
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
#include "eventdispatcher.hh"
|
||||||
|
#include "display.hh"
|
||||||
|
|
||||||
|
namespace otk {
|
||||||
|
|
||||||
|
OtkEventDispatcher::OtkEventDispatcher()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
OtkEventDispatcher::~OtkEventDispatcher()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void OtkEventDispatcher::clearAllHandlers(void)
|
||||||
|
{
|
||||||
|
_map.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OtkEventDispatcher::registerHandler(Window id, OtkEventHandler *handler)
|
||||||
|
{
|
||||||
|
_map.insert(std::pair<Window, OtkEventHandler*>(id, handler));
|
||||||
|
}
|
||||||
|
|
||||||
|
void OtkEventDispatcher::clearHandler(Window id)
|
||||||
|
{
|
||||||
|
_map.erase(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OtkEventDispatcher::dispatchEvents(void)
|
||||||
|
{
|
||||||
|
XEvent e;
|
||||||
|
OtkEventHandler *handler;
|
||||||
|
OtkEventMap::iterator it;
|
||||||
|
|
||||||
|
while (XPending(OBDisplay::display)) {
|
||||||
|
XNextEvent(OBDisplay::display, &e);
|
||||||
|
it = _map.find(e.xany.window);
|
||||||
|
|
||||||
|
if (it == _map.end())
|
||||||
|
handler = _fallback;
|
||||||
|
else
|
||||||
|
handler = it->second;
|
||||||
|
|
||||||
|
if (handler)
|
||||||
|
handler->handle(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
otk/eventdispatcher.hh
Normal file
35
otk/eventdispatcher.hh
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#ifndef __eventdispatcher
|
||||||
|
#define __eventdispatcher
|
||||||
|
|
||||||
|
#include "eventhandler.hh"
|
||||||
|
#include <map>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace otk {
|
||||||
|
|
||||||
|
typedef std::map<unsigned int, OtkEventHandler *> OtkEventMap;
|
||||||
|
|
||||||
|
class OtkEventDispatcher {
|
||||||
|
public:
|
||||||
|
|
||||||
|
OtkEventDispatcher();
|
||||||
|
virtual ~OtkEventDispatcher();
|
||||||
|
|
||||||
|
virtual void clearAllHandlers(void);
|
||||||
|
virtual void registerHandler(Window id, OtkEventHandler *handler);
|
||||||
|
virtual void clearHandler(Window id);
|
||||||
|
virtual void dispatchEvents(void);
|
||||||
|
|
||||||
|
inline void setFallbackHandler(OtkEventHandler *fallback)
|
||||||
|
{ _fallback = fallback; }
|
||||||
|
OtkEventHandler *getFallbackHandler(void) const { return _fallback; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
OtkEventMap _map;
|
||||||
|
OtkEventHandler *_fallback;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
84
otk/eventhandler.cc
Normal file
84
otk/eventhandler.cc
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
#include "eventhandler.hh"
|
||||||
|
|
||||||
|
namespace otk {
|
||||||
|
|
||||||
|
OtkEventHandler::OtkEventHandler()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
OtkEventHandler::~OtkEventHandler()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int OtkEventHandler::handle(const XEvent &e)
|
||||||
|
{
|
||||||
|
switch(e.type){
|
||||||
|
case KeyPress:
|
||||||
|
return keyPressHandler(e.xkey);
|
||||||
|
case KeyRelease:
|
||||||
|
return keyReleaseHandler(e.xkey);
|
||||||
|
case ButtonPress:
|
||||||
|
return buttonPressHandler(e.xbutton);
|
||||||
|
case ButtonRelease:
|
||||||
|
return buttonReleaseHandler(e.xbutton);
|
||||||
|
case EnterNotify:
|
||||||
|
return enterHandler(e.xcrossing);
|
||||||
|
case LeaveNotify:
|
||||||
|
return leaveHandler(e.xcrossing);
|
||||||
|
case FocusIn:
|
||||||
|
return focusHandler(e.xfocus);
|
||||||
|
case FocusOut:
|
||||||
|
return unfocusHandler(e.xfocus);
|
||||||
|
case Expose:
|
||||||
|
return exposeHandler(e.xexpose);
|
||||||
|
case GraphicsExpose:
|
||||||
|
return graphicsExposeHandler(e.xgraphicsexpose);
|
||||||
|
case NoExpose:
|
||||||
|
return noExposeEventHandler(e.xnoexpose);
|
||||||
|
case CirculateRequest:
|
||||||
|
return circulateRequestHandler(e.xcirculaterequest);
|
||||||
|
case ConfigureRequest:
|
||||||
|
return configureRequestHandler(e.xconfigurerequest);
|
||||||
|
case MapRequest:
|
||||||
|
return mapRequestHandler(e.xmaprequest);
|
||||||
|
case ResizeRequest:
|
||||||
|
return resizeRequestHandler(e.xresizerequest);
|
||||||
|
case CirculateNotify:
|
||||||
|
return circulateHandler(e.xcirculate);
|
||||||
|
case ConfigureNotify:
|
||||||
|
return configureHandler(e.xconfigure);
|
||||||
|
case CreateNotify:
|
||||||
|
return createHandler(e.xcreatewindow);
|
||||||
|
case DestroyNotify:
|
||||||
|
return destroyHandler(e.xdestroywindow);
|
||||||
|
case GravityNotify:
|
||||||
|
return gravityHandler(e.xgravity);
|
||||||
|
case MapNotify:
|
||||||
|
return mapHandler(e.xmap);
|
||||||
|
case MappingNotify:
|
||||||
|
return mappingHandler(e.xmapping);
|
||||||
|
case ReparentNotify:
|
||||||
|
return reparentHandler(e.xreparent);
|
||||||
|
case UnmapNotify:
|
||||||
|
return unmapHandler(e.xunmap);
|
||||||
|
case VisibilityNotify:
|
||||||
|
return visibilityHandler(e.xvisibility);
|
||||||
|
case ColormapNotify:
|
||||||
|
return colorMapHandler(e.xcolormap);
|
||||||
|
case ClientMessage:
|
||||||
|
return clientMessageHandler(e.xclient);
|
||||||
|
case PropertyNotify:
|
||||||
|
return propertyHandler(e.xproperty);
|
||||||
|
case SelectionClear:
|
||||||
|
return selectionClearHandler(e.xselectionclear);
|
||||||
|
case SelectionNotify:
|
||||||
|
return selectionHandler(e.xselection);
|
||||||
|
case SelectionRequest:
|
||||||
|
return selectionRequestHandler(e.xselectionrequest);
|
||||||
|
};
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
126
otk/eventhandler.hh
Normal file
126
otk/eventhandler.hh
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
#ifndef __eventhandler__hh
|
||||||
|
#define __eventhandler__hh
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace otk {
|
||||||
|
|
||||||
|
class OtkEventHandler{
|
||||||
|
public:
|
||||||
|
//! Dispatches events to one of the other handlers based on their type.
|
||||||
|
int handle(const XEvent &e);
|
||||||
|
|
||||||
|
//! Called whenever any key is pressed.
|
||||||
|
virtual int keyPressHandler(const XKeyEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called whenever any key is released.
|
||||||
|
virtual int keyReleaseHandler(const XKeyEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called whenever a button of the pointer is pressed.
|
||||||
|
virtual int buttonPressHandler(const XButtonEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called whenever a button of the pointer is released.
|
||||||
|
virtual int buttonReleaseHandler(const XButtonEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called whenever the pointer enters a window.
|
||||||
|
virtual int enterHandler(const XCrossingEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called whenever the pointer leaves a window.
|
||||||
|
virtual int leaveHandler(const XCrossingEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a window gains focus.
|
||||||
|
virtual int focusHandler(const XFocusChangeEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a window looses focus.
|
||||||
|
virtual int unfocusHandler(const XFocusChangeEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a window becomes visible to the user.
|
||||||
|
virtual int exposeHandler(const XExposeEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called to handle GraphicsExpose events.
|
||||||
|
virtual int graphicsExposeHandler(const XGraphicsExposeEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called to handle NoExpose events.
|
||||||
|
virtual int noExposeEventHandler(const XNoExposeEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when the window requests a change in its z-order.
|
||||||
|
virtual int circulateRequestHandler(const XCirculateRequestEvent &)
|
||||||
|
{return 1;}
|
||||||
|
|
||||||
|
//! Called when a different client initiates a configure window request.
|
||||||
|
virtual int configureRequestHandler(const XConfigureRequestEvent &)
|
||||||
|
{return 1;}
|
||||||
|
|
||||||
|
//! Called when a different client tries to map a window.
|
||||||
|
virtual int mapRequestHandler(const XMapRequestEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when another client attemps to change the size of a window.
|
||||||
|
virtual int resizeRequestHandler(const XResizeRequestEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when the z-order of the window has changed.
|
||||||
|
virtual int circulateHandler(const XCirculateEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when the window as been reconfigured.
|
||||||
|
virtual int configureHandler(const XConfigureEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a window is created.
|
||||||
|
virtual int createHandler(const XCreateWindowEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a window is destroyed.
|
||||||
|
virtual int destroyHandler(const XDestroyWindowEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a window is moved because of a change in the size of its
|
||||||
|
//! parent.
|
||||||
|
virtual int gravityHandler(const XGravityEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a window is mapped.
|
||||||
|
virtual int mapHandler(const XMapEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when the server generats a MappingNotify event
|
||||||
|
virtual int mappingHandler(const XMappingEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a window is reparented
|
||||||
|
virtual int reparentHandler(const XReparentEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a window is unmapped
|
||||||
|
virtual int unmapHandler(const XUnmapEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a the visibilty of a window changes
|
||||||
|
virtual int visibilityHandler(const XVisibilityEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when the colormap changes, or is installed or unistalled
|
||||||
|
virtual int colorMapHandler(const XColormapEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a client calls XSendEvent
|
||||||
|
virtual int clientMessageHandler(const XClientMessageEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a property of a window changes
|
||||||
|
virtual int propertyHandler(const XPropertyEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when the client loses ownership of a selection
|
||||||
|
virtual int selectionClearHandler(const XSelectionClearEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a ConvertSelection protocol request is sent
|
||||||
|
virtual int selectionHandler(const XSelectionEvent &) {return 1;}
|
||||||
|
|
||||||
|
//! Called when a SelectionEvent occurs
|
||||||
|
virtual int selectionRequestHandler(const XSelectionRequestEvent &)
|
||||||
|
{return 1;}
|
||||||
|
|
||||||
|
virtual ~OtkEventHandler();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/*! Constructor for the XEventHandler class.
|
||||||
|
This is protected so that XEventHandlers can't be instantiated on their
|
||||||
|
own.
|
||||||
|
*/
|
||||||
|
OtkEventHandler();
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue