OSDN Git Service

first
[psychlops/cpp.git] / psychlops / extension / standard / widgets / psychlops_widget.cpp
1 /*
2  *  psychlops_figure_prototype.cpp
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2009/02/15 by Kenchi HOSOKAWA
6  *  (C) 2009 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9 #include "psychlops_widget.h"
10
11 namespace Psychlops {
12
13
14 namespace Widgets {
15
16
17
18         // WidgetBase
19         WidgetBase::WidgetBase() { parent_ = 0; }
20         Rectangle WidgetBase::getArea() { return area; }
21         Events::Container& WidgetBase::getParent() { return *parent_; }
22         Events::Container& WidgetBase::setParent(Events::Container &par) { parent_ = ∥ return *parent_; }
23         Events::Dispatcher& WidgetBase::getSlots() { return slots_; }
24         void WidgetBase::distribute(Event &ev) { getSlots().emit(ev); }
25         void WidgetBase::distribute(Events::PointerDeviceEvent &ev) { getSlots().emit(ev); }
26         void WidgetBase::tabStop(bool on_off)
27         {
28                 Events::AddToTabstopList list(this);
29                 bubble(list);
30         }
31
32
33
34
35
36         // Deplecated Widgets
37
38         Theme Theme::current;
39         bool Theme::initialized__ = false;
40         Image Theme::default_button, Theme::default_horiz_grad;
41         /*
42         Theme::Theme(Color c1, Color c2, Stroke s1, Stroke s2)
43         {
44                 normal_background = c1;
45                 over_background = c2;
46                 normal_stroke = s1;
47                 active_stroke = s2;
48         }
49         */
50         void Theme::initialize()
51         {
52                 if(!initialized__) {
53                         for(int i=0; i<2; i++) {
54                                 Theme::current.normal_foreground[i] = Color::black;
55                                 Theme::current.active_foreground[i] = Color::black;
56                                 Theme::current.normal_background[i] = Color(.85,.85,.85,.5);
57                                 Theme::current.active_background[i] = Color(.70,1.0,.85,.65);
58                                 Theme::current.over_background[i] = Color(.5,.5,1.0,.5);
59                                 Theme::current.normal_stroke[i] = Stroke(Color(.35,.35,.35,.75), 1, Stroke::SOLID);
60                                 Theme::current.active_stroke[i] = Stroke(Color(.35,.35,.35,.75), 2, Stroke::SOLID);
61                         }
62                         Theme::current.normal_background[ALERT] = Color(.8,.2,.1,.5);
63                         Theme::current.over_background[ALERT] = Color(1,.25,0,.5);
64
65                         default_button.set(1,20, Image::RGBA);
66                         for(int y= 0; y< 5; y++) { double level = 0.5-y*0.1; default_button.pix(0,y,Color::white); default_button.alpha(0,y,level); }
67                         for(int y= 5; y<15; y++) { default_button.pix(0,y,Color::white); default_button.alpha(0,y,0.1); }
68                         for(int y=15; y<20; y++) { double level = (y-14)*0.075; default_button.pix(0,y,Color(.8,.8,.8)); default_button.alpha(0,y,level); }
69                         Theme::current.button_back = &Theme::default_button;
70                         default_horiz_grad.set(40,1, Image::RGBA);
71                         for(int x= 0; x<15; x++) { double level = 0.25-x*0.01; default_horiz_grad.pix(x,0,Color::white); default_horiz_grad.alpha(x,0,level); }
72                         for(int x=15; x<35; x++) { default_horiz_grad.pix(x,0,Color::white); default_horiz_grad.alpha(x,0,0.1); }
73                         for(int x=35; x<40; x++) { double level = (x-33)*0.075; default_horiz_grad.pix(x,0,Color(.8,.8,.8)); default_horiz_grad.alpha(x,0,level); }
74                         Theme::current.horiz_grad = &Theme::default_horiz_grad;
75                         initialized__ = true;
76                 }
77         }
78
79         Point drawableMouse(Drawable &d){
80                 Canvas *cnvs = dynamic_cast<Canvas*>(&d);
81                 return (cnvs!=0) ? cnvs->mouse() : (Point)Mouse::uniPosition;
82         }
83
84
85
86         WidgetDatum::WidgetDatum() : datum(0,0,0) {}
87         WidgetDatum::~WidgetDatum() {}
88         const Point WidgetDatum::getDatum() const {
89                 return datum;
90         }
91         WidgetDatum& WidgetDatum::setDatum(const Point& p) {
92                 datum = p;
93                 return *this;
94         }
95         WidgetDatum& WidgetDatum::centering(const Point & p) {
96                 datum = p;
97                 return *this;
98         }
99
100         WidgetRect::WidgetRect()
101         : mleft(Mouse::left), pressFrame(-1), tabStopRegistered(false)
102         {
103 Theme::initialize();
104                 theme = &Theme::current;
105                 theme_type = Theme::NORMAL;
106                 pushed_ = false;
107         }
108         const Point WidgetRect::getDatum() const { return area.getDatum(); }
109         WidgetRect& WidgetRect::setDatum(const Point& p) { area.setDatum(p); return *this; }
110         WidgetRect& WidgetRect::centering(const Point& p) { area.centering(p); return *this; }
111
112         double WidgetRect::getWidth() const { return area.getWidth(); }
113         double WidgetRect::getHeight() const { return area.getHeight(); }
114         double WidgetRect::getLeft() const { return area.getLeft(); }
115         double WidgetRect::getTop() const { return area.getTop(); }
116         double WidgetRect::getRight() const { return area.getRight(); }
117         double WidgetRect::getBottom() const { return area.getBottom(); }
118         WidgetRect& WidgetRect::alignLeft(const double lef) { area.alignLeft(lef); return *this; }
119         WidgetRect& WidgetRect::alignTop(const double to_) { area.alignTop(to_); return *this; }
120         WidgetRect& WidgetRect::alignRight(const double rig) { area.alignRight(rig); return *this; }
121         WidgetRect& WidgetRect::alignBottom(const double bot) { area.alignBottom(bot); return *this; }
122
123         WidgetRect& WidgetRect::set(double wid, double hei)
124         {
125                 area.set(wid, hei);
126                 double size = hei-4;
127                 label.font.size = (size>0) ? size : Font::default_font.size;
128                 return *this;
129         }
130         WidgetRect& WidgetRect::set(std::wstring name)
131         {
132                 return set(name, Font::default_font.size);
133         }
134         WidgetRect& WidgetRect::set(std::wstring name, double hei)
135         {
136                 double size = hei-4;
137                 label.font.size = (size>0) ? size : Font::default_font.size;
138                 label.setString(name);
139                 area.set(hei*label.str.length(), hei);
140                 return *this;
141         }
142         WidgetRect& WidgetRect::setLabel(std::wstring s)
143         {
144                 double size = area.getHeight()-4;
145                 label.font.size = (size>5 && size<40) ? size : Font::default_font.size;
146                 label.setString(s);
147                 label.fill = theme->normal_foreground[theme_type];
148                 return *this;
149         }
150         WidgetRect& WidgetRect::draw(Drawable &target)
151         {
152                 return *this;
153         }
154         bool WidgetRect::pushed()
155         {
156                 bool v = pushed_;
157                 pushed_ = false;
158                 return v;
159         }
160         WidgetRect& WidgetRect::pushThis()
161         {
162                 pushed_ = true;
163                 return *this;
164         }
165         
166         int  WidgetRect::__LAST_TOTAL_REFRESH = 0;
167         double WidgetRect::__autoAlign_ = 10.0;
168         void WidgetRect::__autoAlign(WidgetRect &target)
169         {
170                 /*
171                 if(Display::the_canvas!=0) {
172                         if(__LAST_TOTAL_REFRESH!=Display::the_canvas->TOTAL_REFRESH) {
173                                 __LAST_TOTAL_REFRESH = Display::the_canvas->TOTAL_REFRESH;
174                                 __autoAlign_ = 0.0;
175                         }
176                 }*/
177                 
178                 target.area.shift(10, __autoAlign_);
179                 __autoAlign_ += target.area.getHeight() + 10;
180         }
181
182         
183
184         StackPanel::StackPanel()
185         {
186                 orientation = VERTICAL;
187                 pitch = 2;
188         }
189         StackPanel& StackPanel::setWidth(double wid)
190         {
191                 area.set(wid, area.getHeight());
192                 forceBase = true;
193                 return *this;
194         }
195         StackPanel& StackPanel::setHeight(double hei)
196         {
197                 area.set(area.getWidth(), hei);
198                 forceBase = true;
199                 return *this;
200         }
201         StackPanel& StackPanel::append(WidgetRect *target1, WidgetRect *target2, WidgetRect *target3, WidgetRect *target4, WidgetRect *target5)
202         {
203                 if(target1!=0) { content.push_back(target1); }
204                 if(target2!=0) { content.push_back(target2); }
205                 if(target3!=0) { content.push_back(target3); }
206                 if(target4!=0) { content.push_back(target4); }
207                 if(target5!=0) { content.push_back(target5); }
208                 return *this;
209         }
210         StackPanel& StackPanel::remove(WidgetRect *target)
211         {
212                 for(std::deque<WidgetRect *>::iterator i = content.begin(); i != content.end(); i++) {
213                         if( *i == target ) {
214                                 content.erase(i);
215                                 break;
216                         }
217                 }
218                 return *this;
219         }
220         void StackPanel::align(Orientation ori)
221         {
222                 orientation = ori;
223                 double dleft = getLeft(), dtop = getTop();
224                 switch(orientation) {
225                 case VERTICAL:
226                         if(!content.empty()) {
227                                 for(int i=0; i<content.size(); i++) {
228                                         content[i]->area.set(area.getWidth(), content[i]->area.getHeight());
229                                         content[i]->alignLeft(dleft);
230                                         content[i]->alignTop(dtop);
231                                         dtop += content[i]->getHeight() + pitch;
232                                 }
233                         }
234                         break;
235                 case HORIZONTAL:
236                 default:
237                         if(!content.empty()) {
238                                 for(int i=0; i<content.size(); i++) {
239                                         content[i]->alignLeft(dleft);
240                                         content[i]->alignTop(dtop);
241                                         dleft += content[i]->getWidth() + pitch;
242                                 }
243                         }
244                         break;
245                 }
246         }
247
248         StackPanel& StackPanel::draw(Drawable &target)
249         {
250                 double dleft = getLeft(), dtop = getTop();
251                 switch(orientation) {
252                 case VERTICAL:
253                         if(!content.empty()) {
254                                 for(int i=0; i<content.size(); i++) {
255                                         content[i]->area.set(area.getWidth(), content[i]->area.getHeight());
256                                         content[i]->alignLeft(dleft);
257                                         content[i]->alignTop(dtop);
258                                         content[i]->draw(target);
259                                         dtop += content[i]->getHeight() + pitch;
260                                 }
261                         }
262                         break;
263                 case HORIZONTAL:
264                 default:
265                         if(!content.empty()) {
266                                 for(int i=0; i<content.size(); i++) {
267                                         content[i]->alignLeft(dleft);
268                                         content[i]->alignTop(dtop);
269                                         content[i]->draw(target);
270                                         dleft += content[i]->getWidth() + pitch;
271                                 }
272                         }
273                         break;
274                 }
275                 return *this;
276         }
277
278
279         StackPanel StackPanel::default_stack;
280
281
282 }       /*      <- namespace Widgets    */
283 }       /*      <- namespace Psycholops         */
284
285