trunk 20080912
[gitenigma.git] / lib / gui / slider.cpp
blob54332b20991e4392ed20927bf966cefa4ec39cb7
1 #include <lib/gui/slider.h>
3 #include <lib/gui/eskin.h>
4 #include <lib/gui/elabel.h>
5 #include <lib/gui/guiactions.h>
6 #include <lib/system/init.h>
7 #include <lib/system/init_num.h>
9 inline void swap( gColor& a, gColor& b )
11 gColor tmp = a;
12 a = b;
13 b = tmp;
16 eSlider::eSlider( eWidget* parent, const eWidget *descr, int min, int max )
17 :eProgress( parent, 1), descr(descr)
19 activated_left = eSkin::getActive()->queryScheme( "eSlider.activated.left" );
20 activated_right = eSkin::getActive()->queryScheme( "eSlider.activated.right" );
21 setMin(min);
22 setMax(max);
23 incrementation = 4; // in Percent
24 addActionMap(&i_cursorActions->map);
27 void eSlider::setMin( int i )
29 min = i;
32 void eSlider::setMax( int i )
34 max = (i <= min) ? 99 : i;
37 void eSlider::setValue( int i )
39 if ( i >= min && i <= max )
40 // setPerc( (int) round( i * (double)100/((max-min)+1) ) );
41 setPerc((i-min) * 100 / (max-min));
42 else
43 setPerc( 0 );
46 int eSlider::getValue()
48 int ret = (int) ( (double) perc / 100 * ( (max-min)+1));
49 return (ret > max ? max : ret);
52 void eSlider::setIncrement( int i )
54 if (!i)
55 incrementation = 4;
56 else
57 incrementation = i;
60 int eSlider::setProperty( const eString &prop, const eString &val)
62 if (prop == "leftColorActive")
63 activated_left=eSkin::getActive()->queryColor( prop );
64 else if (prop == "rightColorActive")
65 activated_right=eSkin::getActive()->queryColor( prop );
66 else if (prop == "incrementation")
67 setIncrement( atoi( val.c_str() ) );
68 else if (prop == "min")
69 setMin( atoi(val.c_str()) );
70 else if (prop == "max")
71 setMax( atoi(val.c_str()) );
72 else
73 return eProgress::setProperty( prop, val );
74 return 0;
77 void eSlider::gotFocus()
79 have_focus++;
80 swap( left, activated_left );
81 swap( right, activated_right );
83 #ifndef DISABLE_LCD
84 if (parent && parent->LCDElement) // detect if LCD Avail
86 LCDTmp = new eProgress( parent->LCDElement );
87 LCDTmp->hide();
88 LCDTmp->setForegroundColor( 255 );
89 ((eSlider*)LCDTmp)->left=255;
90 ((eSlider*)LCDTmp)->right=0;
91 ((eProgress*)LCDTmp)->setPerc( perc );
92 eSize s = parent->LCDElement->getSize();
94 if (descr)
96 LCDTmp->move(ePoint(0, s.height()/2 + s.height()/4 - 6 ));
97 LCDTmp->resize( eSize(s.width(), 12) );
98 tmpDescr = new eLabel(parent->LCDElement);
99 tmpDescr->hide();
100 tmpDescr->move(ePoint(0,0));
101 tmpDescr->resize(eSize(s.width(), s.height()/2));
102 tmpDescr->setText(descr->getText());
103 tmpDescr->show();
105 else
107 LCDTmp->resize( eSize(s.width(), 8) );
108 LCDTmp->move(ePoint(0, s.height() / 2 - 6));
110 ((eProgress*)LCDTmp)->setPerc( perc );
111 LCDTmp->show();
113 #endif // DISABLE_LCD
114 redraw();
117 void eSlider::lostFocus()
119 // swap back;
120 swap( left, activated_left );
121 swap( right, activated_right );
123 #ifndef DISABLE_LCD
124 if (LCDTmp)
126 delete LCDTmp;
127 LCDTmp=0;
128 if (tmpDescr)
130 delete tmpDescr;
131 tmpDescr=0;
134 #endif
135 have_focus--;
136 invalidate();
139 int eSlider::eventHandler( const eWidgetEvent& event )
141 switch (event.type)
143 case eWidgetEvent::evtAction:
145 if(event.action == &i_cursorActions->right)
147 if ( (perc += incrementation) > 100 )
148 perc = 100;
150 else if(event.action == &i_cursorActions->left)
152 if ( (perc-=incrementation) < 0 )
153 perc = 0;
155 else
156 break;
157 redraw();
158 /* emit */ changed( getValue() );
159 #ifndef DISABLE_LCD
160 if (LCDTmp)
161 ((eProgress*)LCDTmp)->setPerc( perc );
162 #endif
163 return 1;
165 default:
166 break;
168 return eProgress::eventHandler( event );
171 static eWidget *create_eSlider(eWidget *parent)
173 return new eSlider(parent);
176 class eSliderSkinInit
178 public:
179 eSliderSkinInit()
181 eSkin::addWidgetCreator("eSlider", create_eSlider);
183 ~eSliderSkinInit()
185 eSkin::removeWidgetCreator("eSlider", create_eSlider);
189 eAutoInitP0<eSliderSkinInit> init_eSliderSkinInit(eAutoInitNumbers::guiobject, "eSlider");