c41721ceb586098b4682dc5e6d3c2d7ed56e7908
[enigma2.git] / lib / gui / echeckbox.cpp
1 #include <lib/gui/echeckbox.h>
2
3 #include <lib/gdi/font.h>
4 #include <lib/base/init.h>
5 #include <lib/base/init_num.h>
6 #include <lib/gui/eskin.h>
7
8 eCheckbox::eCheckbox(eWidget *parent, int checked, int takefocus, bool swapTxtPixmap, const char *deco)
9         :eButton(parent, 0, takefocus, deco), swapTxtPixmap(swapTxtPixmap)
10 {
11         align=eTextPara::dirLeft;
12         ischecked = -1;
13         setCheck(checked);
14         CONNECT(selected, eCheckbox::sel);
15 }
16
17 eCheckbox::~eCheckbox()
18 {
19 }
20
21 void eCheckbox::sel()
22 {
23         setCheck(ischecked?0:1);
24         /*emit*/ checked(ischecked);
25 }
26
27 void eCheckbox::gotFocus()
28 {
29 #ifndef DISABLE_LCD
30         if (parent && parent->LCDElement)
31         {
32                 LCDTmp = new eLabel(parent->LCDElement);
33                 LCDTmp->hide();
34                 eSize s = parent->LCDElement->getSize();
35                 LCDTmp->move(ePoint(0,0));
36                 LCDTmp->resize(eSize(s.width(), s.height()));
37                 ((eLabel*)LCDTmp)->setFlags(RS_WRAP);
38                 ePtr<gPixmap> pm;
39                 eSkin::getActive()->queryImage(pm, ischecked?"eCheckboxLCD.checked":"eCheckboxLCD.unchecked");
40                 LCDTmp->setPixmap(pm);
41                 ((eLabel*)LCDTmp)->pixmap_position=ePoint(0, (size.height()-15)/2);
42                 ((eLabel*)LCDTmp)->text_position=ePoint(21, 0);
43                 LCDTmp->setText(text);
44                 LCDTmp->show();
45         }
46 #endif
47         setForegroundColor(focusF, false);
48         setBackgroundColor(focusB);
49 //      invalidate();
50 }
51
52 void eCheckbox::lostFocus()
53 {
54 #ifndef DISABLE_LCD
55         if (LCDTmp)
56         {
57                 delete LCDTmp;
58                 LCDTmp = 0;
59         }
60 #endif
61         eButton::lostFocus();
62 }
63
64
65 void eCheckbox::setCheck(int c)
66 {
67         if (ischecked != -1 && ischecked == c)
68                 return;
69
70         ischecked=c;
71
72         ePtr<gPixmap> pixmap;
73         eSkin::getActive()->queryImage(pixmap, ischecked?"eCheckbox.checked":"eCheckbox.unchecked");
74         setPixmap(pixmap);
75 #ifndef DISABLE_LCD
76         eSkin::getActive()->queryImage(pixmap, ischecked?"eCheckboxLCD.checked":"eCheckboxLCD.unchecked");
77         if (LCDTmp)
78                 LCDTmp->setPixmap(pixmap);
79 #endif
80 }
81
82 int eCheckbox::setProperty(const eString &prop, const eString &value)
83 {
84         if (prop=="swaptxtpixmap")      
85         {
86                 swapTxtPixmap = (value != "off");
87                 event( eWidgetEvent::changedSize );
88         }
89         else
90                 return eButton::setProperty(prop, value);
91         return 0;
92 }
93
94 int eCheckbox::eventHandler(const eWidgetEvent &event)
95 {
96         switch (event.type)
97         {
98         case eWidgetEvent::changedSize:
99                 if (swapTxtPixmap)
100                 {
101                         text_position=ePoint(0,0);
102                         eLabel::invalidate();
103                         validate();
104                         pixmap_position=ePoint( para->getBoundBox().right()+5, (size.height()-pixmap->y) / 2 );
105                 }
106                 else
107                 {
108                         pixmap_position=ePoint(0, (size.height()-pixmap->y)/2);
109                         text_position=ePoint((int)(pixmap->x*1.25), 0);
110                 }
111                 //return eButton::eventHandler(event); // changed Size must seen by eLabel...
112                 break;
113
114         default:
115                 return eButton::eventHandler(event);
116         }
117         return 1;
118 }
119
120 static eWidget *create_eCheckbox(eWidget *parent)
121 {
122         return new eCheckbox(parent);
123 }
124
125 class eCheckboxSkinInit
126 {
127 public:
128         eCheckboxSkinInit()
129         {
130                 eSkin::addWidgetCreator("eCheckbox", create_eCheckbox);
131         }
132         ~eCheckboxSkinInit()
133         {
134                 eSkin::removeWidgetCreator("eCheckbox", create_eCheckbox);
135         }
136 };
137
138 eAutoInitP0<eCheckboxSkinInit> init_eCheckboxSkinInit(eAutoInitNumbers::guiobject, "eCheckbox");