aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/EventManager.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/EventManager.cc')
-rw-r--r--src/FbTk/EventManager.cc187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/FbTk/EventManager.cc b/src/FbTk/EventManager.cc
new file mode 100644
index 0000000..6c697e6
--- /dev/null
+++ b/src/FbTk/EventManager.cc
@@ -0,0 +1,187 @@
1// EventManager.cc
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: EventManager.cc,v 1.10 2003/10/14 16:23:16 rathnor Exp $
23
24#include "EventManager.hh"
25#include "FbWindow.hh"
26#include "App.hh"
27
28#include <iostream>
29using namespace std;
30
31namespace FbTk {
32
33EventManager *EventManager::instance() {
34 static EventManager ev;
35 return &ev;
36}
37
38EventManager::~EventManager() {
39 if (m_eventhandlers.size() != 0)
40 cerr<<"FbTk::EventManager: Warning: unregistered eventhandlers!"<<endl;
41}
42
43void EventManager::handleEvent(XEvent &ev) {
44 dispatch(ev.xany.window, ev);
45}
46
47void EventManager::add(EventHandler &ev, const FbWindow &win) {
48 registerEventHandler(ev, win.window());
49}
50
51void EventManager::addParent(EventHandler &ev, const FbWindow &win) {
52 if (win.window() != 0)
53 m_parent[win.window()] = &ev;
54}
55
56void EventManager::remove(const FbWindow &win) {
57 unregisterEventHandler(win.window());
58}
59
60Window EventManager::getEventWindow(XEvent &ev) {
61 // we only have cases for events that differ from xany
62 switch (ev.type) {
63 case CreateNotify:
64 // XCreateWindowEvent
65 return ev.xcreatewindow.window;
66 break;
67 case DestroyNotify:
68 // XDestroyWindowEvent
69 return ev.xdestroywindow.window;
70 break;
71 case UnmapNotify:
72 // XUnmapEvent
73 return ev.xunmap.window;
74 break;
75 case MapNotify:
76 // XMapEvent
77 return ev.xmap.window;
78 break;
79 case MapRequest:
80 // XMapRequestEvent
81 return ev.xmaprequest.window;
82 break;
83 case ReparentNotify:
84 // XReparentEvent
85 return ev.xreparent.window;
86 break;
87 case ConfigureNotify:
88 // XConfigureNotify
89 return ev.xconfigure.window;
90 break;
91 case GravityNotify:
92 // XGravityNotify
93 return ev.xgravity.window;
94 break;
95 case ConfigureRequest:
96 // XConfigureRequestEvent
97 return ev.xconfigurerequest.window;
98 break;
99 case CirculateNotify:
100 // XCirculateEvent
101 return ev.xcirculate.window;
102 break;
103 case CirculateRequest:
104 // XCirculateRequestEvent
105 return ev.xcirculaterequest.window;
106 break;
107 default:
108 return ev.xany.window;
109 }
110}
111
112void EventManager::registerEventHandler(EventHandler &ev, Window win) {
113 if (win != None)
114 m_eventhandlers[win] = &ev;
115}
116
117void EventManager::unregisterEventHandler(Window win) {
118 if (win != None) {
119 m_eventhandlers.erase(win);
120 m_parent.erase(win);
121 }
122}
123
124void EventManager::dispatch(Window win, XEvent &ev, bool parent) {
125 EventHandler *evhand = 0;
126 if (parent)
127 evhand = m_parent[win];
128 else {
129 win = getEventWindow(ev);
130 evhand = m_eventhandlers[win];
131 }
132
133 if (evhand == 0)
134 return;
135
136 switch (ev.type) {
137 case KeyPress:
138 evhand->keyPressEvent(ev.xkey);
139 break;
140 case KeyRelease:
141 evhand->keyReleaseEvent(ev.xkey);
142 break;
143 case ButtonPress:
144 evhand->buttonPressEvent(ev.xbutton);
145 break;
146 case ButtonRelease:
147 evhand->buttonReleaseEvent(ev.xbutton);
148 break;
149 case MotionNotify:
150 evhand->motionNotifyEvent(ev.xmotion);
151 break;
152 case Expose:
153 evhand->exposeEvent(ev.xexpose);
154 break;
155 case EnterNotify:
156 evhand->enterNotifyEvent(ev.xcrossing);
157 break;
158 case LeaveNotify:
159 evhand->leaveNotifyEvent(ev.xcrossing);
160 break;
161 default:
162 evhand->handleEvent(ev);
163 break;
164 };
165
166 // find out which window is the parent and
167 // dispatch event
168 Window root, parent_win, *children = 0;
169 unsigned int num_children;
170 if (XQueryTree(FbTk::App::instance()->display(), win,
171 &root, &parent_win, &children, &num_children) != 0) {
172 if (children != 0)
173 XFree(children);
174
175 if (parent_win != 0 &&
176 parent_win != root) {
177 if (m_parent[parent_win] == 0)
178 return;
179
180 // dispatch event to parent
181 dispatch(parent_win, ev, true);
182 }
183 }
184
185}
186
187}; // end namespace FbTk