OSDN Git Service

first
[psychlops/cpp.git] / psychlops / extension / standard / widgets / psychlops_widgets_event.cpp
1 /*
2  *  psychlops_widgets_base.cpp
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2011/01/19 by Kenchi HOSOKAWA
6  *  (C) 2011 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
7  */
8
9
10 #include <iostream>\r
11 #include "psychlops_widgets_event.h"\r
12 #include "../../../core/graphic/psychlops_g_canvas.h"
13
14
15 namespace Psychlops
16 {
17
18 namespace Events
19 {
20         long PointerDeviceEvent::getTime() { return time; }
21         Point PointerDeviceEvent::getPlace() { return place; }\r
22         PointerDeviceEvent::PointerDeviceEvent() {}\r
23         const char* PointerDeviceEvent::name = "PointerDeviceEvent"; const char* PointerDeviceEvent::getName() const { return name; };\r
24 \r
25         MouseLeftDown::MouseLeftDown() {}
26         MouseLeftDown::MouseLeftDown(long when, Point where) { time = when; place = where; }\r
27         const char* MouseLeftDown::name = "MouseLeftDown"; const char* MouseLeftDown::getName() const { return name; };\r
28 \r
29         MouseLeftUp::MouseLeftUp() {}
30         MouseLeftUp::MouseLeftUp(long when, Point where) { time = when; place = where; }\r
31         const char* MouseLeftUp::name = "MouseLeftUp"; const char* MouseLeftUp::getName() const { return name; };\r
32 \r
33         MouseRightDown::MouseRightDown() {}
34         MouseRightDown::MouseRightDown(long when, Point where) { time = when; place = where; }\r
35         const char* MouseRightDown::name = "MouseRightDown"; const char* MouseRightDown::getName() const { return name; };\r
36 \r
37         MouseRightUp::MouseRightUp() {}
38         MouseRightUp::MouseRightUp(long when, Point where) { time = when; place = where; }\r
39         const char* MouseRightUp::name = "MouseRightUp"; const char* MouseRightUp::getName() const { return name; };
40 \r
41 \r
42         KeyEvent::KeyEvent() {}\r
43         const char* KeyEvent::name = "KeyEvent"; const char* KeyEvent::getName() const { return name; };\r
44 \r
45         KeyDown::KeyDown() {}\r
46         KeyDown::KeyDown(long when, unsigned int where) { time = when; code = where; }\r
47         const char* KeyDown::name = "KeyDown"; const char* KeyDown::getName() const { return name; };\r
48 \r
49         KeyUp::KeyUp() {}\r
50         KeyUp::KeyUp(long when, unsigned int where) { time = when; code = where; }\r
51         const char* KeyUp::name = "KeyUp"; const char* KeyUp::getName() const { return name; };\r
52 \r
53 \r
54 \r
55         Focus::Focus() {}\r
56         const char* Focus::name = "Focus"; const char* Focus::getName() const { return name; };\r
57 \r
58         Blur::Blur() {}\r
59         const char* Blur::name = "Blur"; const char* Blur::getName() const { return name; };\r
60 \r
61         Evoke::Evoke() {}\r
62         const char* Evoke::name = "Evoke"; const char* Evoke::getName() const { return name; };\r
63 \r
64         AddToTabstopList::AddToTabstopList(Emittable *e) { target = e; }\r
65         const char* AddToTabstopList::name = "AddToTabstopList"; const char* AddToTabstopList::getName() const { return name; };
66 \r
67
68 \r
69 \r
70 \r
71         ActionFunction__::ActionFunction__(Fn f) : fn(f) { }\r
72         void ActionFunction__::call(Event &event) { (*fn)(); }\r
73         Callable__* createActionFunction(void (*fn)()) { return new ActionFunction__(fn); }\r
74 \r
75 \r
76 \r
77         Dispatcher::~Dispatcher()\r
78         {\r
79                 std::map<const char*, std::list<DG> >::iterator s;\r
80                 std::list<DG>::iterator c;\r
81                 if(!slot.empty())\r
82                 {\r
83                         s = slot.begin();\r
84                         while( s != slot.end() )\r
85                         {\r
86                                 if(!((*s).second.empty()))\r
87                                 {\r
88                                         c = (*s).second.begin();\r
89                                         while( c != (*s).second.end() )\r
90                                         {\r
91                                                 delete (*c);\r
92                                                 c++;\r
93                                         }\r
94                                 }\r
95                                 ++s;\r
96                         }\r
97                 }\r
98         }\r
99 \r
100         void Dispatcher::emit(Event &event)\r
101         {\r
102                 std::list<DG>::iterator c;\r
103                 const char* info = event.getName();\r
104                 if(slot.count(info) == 1)\r
105                 {\r
106                         std::list<DG> *it = &(slot[info]);\r
107                         c = (*it).begin();\r
108                         while( c != (*it).end() )\r
109                         {\r
110                                 (*c)->call(event);\r
111                                 ++c;\r
112                         }\r
113                         ++it;\r
114                 }\r
115         }
116         void Emittable::bubble(Event &ev) { if(&(getParent()) != 0) getParent().getBubble(ev); }
117
118
119         EmittableBase::EmittableBase() { parent_ = 0; }
120 \r
121         Point EmittableBase::getHitDatum() { const Point p(0,0,0); return p; }
122         Rectangle EmittableBase::getArea() { return area_; }
123         Container& EmittableBase::getParent() { return *parent_; }
124         Container& EmittableBase::setParent(Container &par) { parent_ = &par; return *parent_; }
125         Dispatcher& EmittableBase::getSlots() { return slots_; }
126
127         void EmittableBase::distribute(Event &ev) { getSlots().emit(ev); }
128         void EmittableBase::distribute(Events::PointerDeviceEvent &ev) { getSlots().emit(ev); }
129
130
131
132
133         ContainerBase::ContainerBase() { }\r
134         Point ContainerBase::getHitDatum() { const Point p(0,0,0); return p; }
135         void ContainerBase::append(Emittable &target)
136         {
137                 children_.push_back(&target);
138                 target.setParent(*this);
139         }
140         void ContainerBase::remove(Emittable &target)
141         {
142         }
143         void ContainerBase::getBubble(Event &ev)
144         {
145                 slots_.emit(ev);
146                 bubble(ev);
147         }
148
149         void ContainerBase::distribute(Event &ev)
150         {\r
151                 std::vector<Emittable *>::iterator w = children_.begin();
152                 while(w != children_.end() )
153                 {
154                         (**w).distribute(ev);
155                         w++;
156                 }
157         }
158
159         void ContainerBase::distribute(Events::PointerDeviceEvent &ev)
160         {
161                 std::vector<Emittable *>::iterator w = children_.begin();
162                 while(w != children_.end() )
163                 {\r
164                         Point p = (**w).getHitDatum();
165                         if((**w).getArea().include(ev.getPlace().shift(-p.x, -p.y))) (**w).distribute(ev);
166                         w++;
167                 }
168         }
169
170         Rectangle ContainerBase::getArea() { return area_; }
171         Container& ContainerBase::getParent() { return *parent_; }
172         Container& ContainerBase::setParent(Container &par) { parent_ = &par; return *parent_; }
173         Dispatcher& ContainerBase::getSlots() { return slots_; }
174
175
176 \r
177         RootContainer::RootContainer() { root = 0; focused = 0; focused__ = 0; }\r
178         void RootContainer::distributeToFocus(Event &ev)\r
179         {\r
180                 if(focused<tabstop_list.size())\r
181                         tabstop_list[focused]->distribute(ev);\r
182         }
183         void RootContainer::getBubble(Event &ev)
184         {\r
185                 if(ev.getName() == AddToTabstopList::name)\r
186                 {\r
187                         AddToTabstopList *addToTabstopList = (AddToTabstopList*)(&ev);\r
188                         tabstop_list.push_back(addToTabstopList->target);\r
189                         focused = 0;\r
190                 }\r
191                 if(root!=0)\r
192                 {\r
193                         root->getBubble(ev.getName());\r
194                 }
195         }
196 \r
197         void RootContainer::appendTabStop__(void *target)\r
198         {\r
199                 bool on_off = true;\r
200                 if(on_off) {\r
201                         for(std::vector<void*>::iterator i=tabstop_list__.begin(); i!=tabstop_list__.end(); i++) {\r
202                                 if(*i==target) { return; }\r
203                         }\r
204                         tabstop_list__.push_back(target);\r
205                 } else {\r
206                         for(std::vector<void*>::iterator i=tabstop_list__.begin(); i!=tabstop_list__.end(); i++) {\r
207                                 if(*i==target) { tabstop_list__.erase(i); return; }\r
208                         }\r
209                 }\r
210         }\r
211         bool RootContainer::isFocused__(void *target)\r
212         {\r
213                 if(tabstop_list__.size()>focused__) {\r
214                         return tabstop_list__[focused__]==target ? true : false;\r
215                 } else {\r
216                         return false;\r
217                 }\r
218         }\r
219         void RootContainer::nextTabStop()\r
220         {\r
221                 ++focused %= tabstop_list__.size();\r
222                 ++focused__ %= tabstop_list__.size();\r
223         }\r
224         void RootContainer::prevTabStop()\r
225         {\r
226                 --focused %= tabstop_list__.size();\r
227                 --focused__ %= tabstop_list__.size();\r
228         }
229 \r
230
231 }
232 }