2 * psychlops_widgets_base.cpp
3 * Psychlops Standard Library (Universal)
5 * Last Modified 2011/01/19 by Kenchi HOSOKAWA
6 * (C) 2011 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
11 #include "psychlops_widgets_event.h"
\r
12 #include "../../../core/graphic/psychlops_g_canvas.h"
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
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
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
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
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; };
42 KeyEvent::KeyEvent() {}
\r
43 const char* KeyEvent::name = "KeyEvent"; const char* KeyEvent::getName() const { return name; };
\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
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
56 const char* Focus::name = "Focus"; const char* Focus::getName() const { return name; };
\r
59 const char* Blur::name = "Blur"; const char* Blur::getName() const { return name; };
\r
62 const char* Evoke::name = "Evoke"; const char* Evoke::getName() const { return name; };
\r
64 AddToTabstopList::AddToTabstopList(Emittable *e) { target = e; }
\r
65 const char* AddToTabstopList::name = "AddToTabstopList"; const char* AddToTabstopList::getName() const { return name; };
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
77 Dispatcher::~Dispatcher()
\r
79 std::map<const char*, std::list<DG> >::iterator s;
\r
80 std::list<DG>::iterator c;
\r
84 while( s != slot.end() )
\r
86 if(!((*s).second.empty()))
\r
88 c = (*s).second.begin();
\r
89 while( c != (*s).second.end() )
\r
100 void Dispatcher::emit(Event &event)
\r
102 std::list<DG>::iterator c;
\r
103 const char* info = event.getName();
\r
104 if(slot.count(info) == 1)
\r
106 std::list<DG> *it = &(slot[info]);
\r
108 while( c != (*it).end() )
\r
116 void Emittable::bubble(Event &ev) { if(&(getParent()) != 0) getParent().getBubble(ev); }
119 EmittableBase::EmittableBase() { parent_ = 0; }
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_ = ∥ return *parent_; }
125 Dispatcher& EmittableBase::getSlots() { return slots_; }
127 void EmittableBase::distribute(Event &ev) { getSlots().emit(ev); }
128 void EmittableBase::distribute(Events::PointerDeviceEvent &ev) { getSlots().emit(ev); }
133 ContainerBase::ContainerBase() { }
\r
134 Point ContainerBase::getHitDatum() { const Point p(0,0,0); return p; }
135 void ContainerBase::append(Emittable &target)
137 children_.push_back(&target);
138 target.setParent(*this);
140 void ContainerBase::remove(Emittable &target)
143 void ContainerBase::getBubble(Event &ev)
149 void ContainerBase::distribute(Event &ev)
151 std::vector<Emittable *>::iterator w = children_.begin();
152 while(w != children_.end() )
154 (**w).distribute(ev);
159 void ContainerBase::distribute(Events::PointerDeviceEvent &ev)
161 std::vector<Emittable *>::iterator w = children_.begin();
162 while(w != children_.end() )
164 Point p = (**w).getHitDatum();
165 if((**w).getArea().include(ev.getPlace().shift(-p.x, -p.y))) (**w).distribute(ev);
170 Rectangle ContainerBase::getArea() { return area_; }
171 Container& ContainerBase::getParent() { return *parent_; }
172 Container& ContainerBase::setParent(Container &par) { parent_ = ∥ return *parent_; }
173 Dispatcher& ContainerBase::getSlots() { return slots_; }
177 RootContainer::RootContainer() { root = 0; focused = 0; focused__ = 0; }
\r
178 void RootContainer::distributeToFocus(Event &ev)
\r
180 if(focused<tabstop_list.size())
\r
181 tabstop_list[focused]->distribute(ev);
\r
183 void RootContainer::getBubble(Event &ev)
185 if(ev.getName() == AddToTabstopList::name)
\r
187 AddToTabstopList *addToTabstopList = (AddToTabstopList*)(&ev);
\r
188 tabstop_list.push_back(addToTabstopList->target);
\r
193 root->getBubble(ev.getName());
\r
197 void RootContainer::appendTabStop__(void *target)
\r
199 bool on_off = true;
\r
201 for(std::vector<void*>::iterator i=tabstop_list__.begin(); i!=tabstop_list__.end(); i++) {
\r
202 if(*i==target) { return; }
\r
204 tabstop_list__.push_back(target);
\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
211 bool RootContainer::isFocused__(void *target)
\r
213 if(tabstop_list__.size()>focused__) {
\r
214 return tabstop_list__[focused__]==target ? true : false;
\r
219 void RootContainer::nextTabStop()
\r
221 ++focused %= tabstop_list__.size();
\r
222 ++focused__ %= tabstop_list__.size();
\r
224 void RootContainer::prevTabStop()
\r
226 --focused %= tabstop_list__.size();
\r
227 --focused__ %= tabstop_list__.size();
\r