OSDN Git Service

first
[psychlops/cpp.git] / psychlops / extension / standard / widgets / psychlops_widgets_event.h
1 /*
2  *  psychlops_widgets_event.h
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2011/02/05 by Kenchi HOSOKAWA
6  *  (C) 2011 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9 #ifndef HEADER_PSYCHLOPS_WIDGET_EVENT
10 #define HEADER_PSYCHLOPS_WIDGET_EVENT
11
12
13 #include <typeinfo>
14 #include <vector>
15 #include <list>
16 #include <map>
17 //#include "../../../psychlops_core.h"
18 #include "../../../core/graphic/psychlops_g_shape.h"
19
20 namespace Psychlops
21 {
22         class Canvas;
23
24
25         class Event
26         {
27                 public:
28                 virtual const char* getName() const = 0;
29         };
30
31 namespace Events
32 {
33         class Emittable;
34         class Container;
35         class RootContainer;
36
37
38         class PointerDeviceEvent : public Event
39         {
40                 public:
41                 long time;
42                 Point place;
43
44                 long getTime();
45                 Point getPlace();
46
47                 PointerDeviceEvent();
48                 static const char* name; virtual const char* getName() const;
49         };
50
51         class MouseLeftDown : public PointerDeviceEvent { public:
52                 static const char* name; virtual const char* getName() const;
53                 MouseLeftDown();
54                 MouseLeftDown(long when, Point where);
55         };
56         class MouseLeftUp : public PointerDeviceEvent { public:
57                 static const char* name; virtual const char* getName() const;
58                 MouseLeftUp();
59                 MouseLeftUp(long when, Point where);
60         };
61         class MouseRightDown : public PointerDeviceEvent { public:
62                 static const char* name; virtual const char* getName() const;
63                 MouseRightDown();
64                 MouseRightDown(long when, Point where);
65         };
66         class MouseRightUp : public PointerDeviceEvent { public:
67                 static const char* name; virtual const char* getName() const;
68                 MouseRightUp();
69                 MouseRightUp(long when, Point where);
70         };
71
72
73         class KeyEvent : public Event
74         {
75                 public:
76                 long time;
77                 unsigned int code;
78
79                 KeyEvent();
80                 static const char* name; virtual const char* getName() const;
81         };
82         class KeyDown : public KeyEvent { public:
83                 static const char* name; virtual const char* getName() const;
84                 KeyDown();
85                 KeyDown(long when, unsigned int where);
86         };
87         class KeyUp : public KeyEvent { public:
88                 static const char* name; virtual const char* getName() const;
89                 KeyUp();
90                 KeyUp(long when, unsigned int where);
91         };
92
93         class Focus : public Event { public:
94                 static const char* name; virtual const char* getName() const;
95                 Focus();
96         };
97
98         class Blur : public Event { public:
99                 static const char* name; virtual const char* getName() const;
100                 Blur();
101         };
102         class Evoke : public Event { public:
103                 static const char* name; virtual const char* getName() const;
104                 Evoke();
105         };
106
107         class AddToTabstopList : public Event { public:
108                 static const char* name; virtual const char* getName() const;
109
110                 Emittable* target;
111                 AddToTabstopList(Emittable *e);
112         };
113
114
115
116
117         class Callable__ { public: virtual void call(Event &event) = 0; };
118         template <typename T> class ActionDelegate__ : public Callable__
119         {
120                 public: typedef void (T::*Fn)();
121                 private: T* obj; Fn fn;
122                 public:
123                 ActionDelegate__(T* o, Fn f) : obj(o), fn(f) { }
124                 virtual void call(Event &event) { (obj->*fn)(); }
125         };
126         template <typename T> Callable__* createActionDelegate(T* obj, void (T::*fn)()) { return new ActionDelegate__<T>(obj, fn); }
127
128         class ActionFunction__ : public Callable__
129         {
130                 public: typedef void (*Fn)();
131                 private: Fn fn;
132                 public:
133                 ActionFunction__(Fn f);
134                 virtual void call(Event &event);
135         };
136         Callable__* createActionFunction(void (*fn)());
137
138         template <typename T, typename U> class EventDelegate__ : public Callable__
139         {
140                 public: typedef void (T::*Fn)(U &);
141                 private: T* obj; Fn fn;
142                 public:
143                 EventDelegate__(T* o, Fn f) : obj(o), fn(f) { }
144                 virtual void call(Event &event) { U* u = (U*)(&event); (obj->*fn)(*u); }
145         };
146         template <typename T, typename U> Callable__* createEventDelegate(T* obj, void (T::*fn)(U &)) { return new EventDelegate__<T,U>(obj, fn); }
147
148         template <typename U> class EventFunction__ : public Callable__
149         {
150                 public: typedef void (*Fn)(U &);
151                 private: Fn fn;
152                 public:
153                 EventFunction__(Fn f) : fn(f) { }
154                 virtual void call(Event &event) { U* u = (U*)(&event); (*fn)(*u); }
155         };
156         template <typename U> Callable__* createEventFunction(void (*fn)(U &)) { return new EventFunction__<U>(fn); }
157
158
159         typedef Callable__* DG;
160         //typedef void (*DG)(Event&);
161         typedef std::map<const char*, std::list<DG> > Slots;
162         class Dispatcher {
163                 private:
164                 Slots slot;
165
166                 public:
167                 ~Dispatcher();
168                 template<class U, class T> void connectActionDelegate(T* obj, void (T::*cb)()) {
169                         U u;
170                         const char* info = u.getName();
171                         if(slot.count(info) == 0) {
172                                 slot[info] = std::list<DG>();
173                         }
174                         slot[info].push_back(createActionDelegate(obj, cb));
175                 }
176                 template<class U> void connectActionFunction(void (*cb)()) {
177                         U u;
178                         const char* info = u.getName();
179                         if(slot.count(info) == 0) {
180                                 slot[info] = std::list<DG>();
181                         }
182                         slot[info].push_back(createActionFunction(cb));
183                 }
184                 template<class T, class U> void connectEventDelegate(T* obj, void (T::*cb)(U&)) {
185                         U u;
186                         const char* info = u.getName();
187                         if(slot.count(info) == 0) {
188                                 slot[info] = std::list<DG>();
189                         }
190                         slot[info].push_back(createEventDelegate(obj, cb));
191                 }
192                 template<class U> void connectEventFunction(void (*cb)(U&)) {
193                         U u;
194                         const char* info = u.getName();
195                         if(slot.count(info) == 0) {
196                                 slot[info] = std::list<DG>();
197                         }
198                         slot[info].push_back(createEventFunction(cb));
199                 }
200
201                 template<class T> void disconnect(void (*cb)(T&)) {
202                         T t;
203                         //const char* info = t.getName();
204                         //if(slot.count(info) == 1)
205                         //      slot[info].remove(cb);
206                 }
207
208                 void emit(Event &event);
209                 //void emit(Events::PointerDeviceEvent &event);
210         };
211
212
213
214
215
216         class Emittable
217         {
218                 public:
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;
226
227                 void bubble(Event &ev);
228         };
229
230         class EmittableBase : public Emittable
231         {
232                 protected:
233                 Rectangle area_;
234                 Container *parent_;
235                 Dispatcher slots_;
236
237                 public:
238                 EmittableBase();
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);
246         };
247
248         class Container : public Emittable
249         {
250                 public:
251                 virtual void append(Emittable &target) = 0;
252                 virtual void remove(Emittable &target) = 0;
253                 virtual void getBubble(Event &ev) = 0;
254         };
255
256
257         class ContainerBase : public Container
258         {
259                 protected:
260                 Rectangle area_;
261                 Container *parent_;
262                 Dispatcher slots_;
263                 std::vector<Emittable *> children_;
264
265                 public:
266                 ContainerBase();
267                 virtual void append(Emittable &target);
268                 virtual void remove(Emittable &target);
269                 virtual void getBubble(Event &ev);
270
271
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);
279         };
280
281
282         class RootContainer : public ContainerBase
283         {
284                 friend class ::Psychlops::Canvas;
285                 public:
286                 Canvas *root;
287                 protected:
288                 std::vector<Emittable *> tabstop_list;
289                 std::vector<void *> tabstop_list__;
290                 int focused, focused__;
291
292                 public:
293                 RootContainer();
294                 virtual void distributeToFocus(Event &ev);
295                 virtual void getBubble(Event &ev);
296
297                 void appendTabStop__(void *target);
298                 bool isFocused__(void *target);
299                 void nextTabStop();
300                 void prevTabStop();
301         };
302
303
304
305 }
306 }
307
308 #endif