2 * psychlops_widgets_event.h
3 * Psychlops Standard Library (Universal)
5 * Last Modified 2011/02/05 by Kenchi HOSOKAWA
6 * (C) 2011 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
9 #ifndef HEADER_PSYCHLOPS_WIDGET_EVENT
10 #define HEADER_PSYCHLOPS_WIDGET_EVENT
17 //#include "../../../psychlops_core.h"
18 #include "../../../core/graphic/psychlops_g_shape.h"
28 virtual const char* getName() const = 0;
38 class PointerDeviceEvent : public Event
48 static const char* name; virtual const char* getName() const;
51 class MouseLeftDown : public PointerDeviceEvent { public:
52 static const char* name; virtual const char* getName() const;
54 MouseLeftDown(long when, Point where);
56 class MouseLeftUp : public PointerDeviceEvent { public:
57 static const char* name; virtual const char* getName() const;
59 MouseLeftUp(long when, Point where);
61 class MouseRightDown : public PointerDeviceEvent { public:
62 static const char* name; virtual const char* getName() const;
64 MouseRightDown(long when, Point where);
66 class MouseRightUp : public PointerDeviceEvent { public:
67 static const char* name; virtual const char* getName() const;
69 MouseRightUp(long when, Point where);
73 class KeyEvent : public Event
80 static const char* name; virtual const char* getName() const;
82 class KeyDown : public KeyEvent { public:
83 static const char* name; virtual const char* getName() const;
85 KeyDown(long when, unsigned int where);
87 class KeyUp : public KeyEvent { public:
88 static const char* name; virtual const char* getName() const;
90 KeyUp(long when, unsigned int where);
93 class Focus : public Event { public:
94 static const char* name; virtual const char* getName() const;
98 class Blur : public Event { public:
99 static const char* name; virtual const char* getName() const;
102 class Evoke : public Event { public:
103 static const char* name; virtual const char* getName() const;
107 class AddToTabstopList : public Event { public:
108 static const char* name; virtual const char* getName() const;
111 AddToTabstopList(Emittable *e);
117 class Callable__ { public: virtual void call(Event &event) = 0; };
118 template <typename T> class ActionDelegate__ : public Callable__
120 public: typedef void (T::*Fn)();
121 private: T* obj; Fn fn;
123 ActionDelegate__(T* o, Fn f) : obj(o), fn(f) { }
124 virtual void call(Event &event) { (obj->*fn)(); }
126 template <typename T> Callable__* createActionDelegate(T* obj, void (T::*fn)()) { return new ActionDelegate__<T>(obj, fn); }
128 class ActionFunction__ : public Callable__
130 public: typedef void (*Fn)();
133 ActionFunction__(Fn f);
134 virtual void call(Event &event);
136 Callable__* createActionFunction(void (*fn)());
138 template <typename T, typename U> class EventDelegate__ : public Callable__
140 public: typedef void (T::*Fn)(U &);
141 private: T* obj; Fn fn;
143 EventDelegate__(T* o, Fn f) : obj(o), fn(f) { }
144 virtual void call(Event &event) { U* u = (U*)(&event); (obj->*fn)(*u); }
146 template <typename T, typename U> Callable__* createEventDelegate(T* obj, void (T::*fn)(U &)) { return new EventDelegate__<T,U>(obj, fn); }
148 template <typename U> class EventFunction__ : public Callable__
150 public: typedef void (*Fn)(U &);
153 EventFunction__(Fn f) : fn(f) { }
154 virtual void call(Event &event) { U* u = (U*)(&event); (*fn)(*u); }
156 template <typename U> Callable__* createEventFunction(void (*fn)(U &)) { return new EventFunction__<U>(fn); }
159 typedef Callable__* DG;
160 //typedef void (*DG)(Event&);
161 typedef std::map<const char*, std::list<DG> > Slots;
168 template<class U, class T> void connectActionDelegate(T* obj, void (T::*cb)()) {
170 const char* info = u.getName();
171 if(slot.count(info) == 0) {
172 slot[info] = std::list<DG>();
174 slot[info].push_back(createActionDelegate(obj, cb));
176 template<class U> void connectActionFunction(void (*cb)()) {
178 const char* info = u.getName();
179 if(slot.count(info) == 0) {
180 slot[info] = std::list<DG>();
182 slot[info].push_back(createActionFunction(cb));
184 template<class T, class U> void connectEventDelegate(T* obj, void (T::*cb)(U&)) {
186 const char* info = u.getName();
187 if(slot.count(info) == 0) {
188 slot[info] = std::list<DG>();
190 slot[info].push_back(createEventDelegate(obj, cb));
192 template<class U> void connectEventFunction(void (*cb)(U&)) {
194 const char* info = u.getName();
195 if(slot.count(info) == 0) {
196 slot[info] = std::list<DG>();
198 slot[info].push_back(createEventFunction(cb));
201 template<class T> void disconnect(void (*cb)(T&)) {
203 //const char* info = t.getName();
204 //if(slot.count(info) == 1)
205 // slot[info].remove(cb);
208 void emit(Event &event);
209 //void emit(Events::PointerDeviceEvent &event);
219 virtual Point getHitDatum() = 0;
220 virtual Rectangle getArea() = 0;
221 virtual Container& getParent() = 0;
222 virtual Container& setParent(Container&) = 0;
223 virtual Dispatcher& getSlots() = 0;
224 virtual void distribute(Event &ev) = 0;
225 virtual void distribute(Events::PointerDeviceEvent &ev) = 0;
227 void bubble(Event &ev);
230 class EmittableBase : public Emittable
239 virtual Point getHitDatum();
240 virtual Rectangle getArea();
241 virtual Container& getParent();
242 virtual Container& setParent(Container &par);
243 virtual Dispatcher& getSlots();
244 virtual void distribute(Event &ev);
245 virtual void distribute(Events::PointerDeviceEvent &event);
248 class Container : public Emittable
251 virtual void append(Emittable &target) = 0;
252 virtual void remove(Emittable &target) = 0;
253 virtual void getBubble(Event &ev) = 0;
257 class ContainerBase : public Container
263 std::vector<Emittable *> children_;
267 virtual void append(Emittable &target);
268 virtual void remove(Emittable &target);
269 virtual void getBubble(Event &ev);
272 virtual Point getHitDatum();
273 virtual Rectangle getArea();
274 virtual Container& getParent();
275 virtual Container& setParent(Container &par);
276 virtual Dispatcher& getSlots();
277 virtual void distribute(Event &ev);
278 virtual void distribute(Events::PointerDeviceEvent &event);
282 class RootContainer : public ContainerBase
284 friend class ::Psychlops::Canvas;
288 std::vector<Emittable *> tabstop_list;
289 std::vector<void *> tabstop_list__;
290 int focused, focused__;
294 virtual void distributeToFocus(Event &ev);
295 virtual void getBubble(Event &ev);
297 void appendTabStop__(void *target);
298 bool isFocused__(void *target);