paint background using eWidget::event function, to support transparency
[enigma2.git] / lib / gui / epositiongauge.cpp
1 #include <lib/gui/epositiongauge.h>
2 #include <lib/gui/epixmap.h>
3
4 ePositionGauge::ePositionGauge(eWidget *parent)
5         : eWidget(parent)
6 {
7         m_point_widget = new ePixmap(this);
8         m_point_widget->setAlphatest(1);
9         m_position = 0;
10         m_length = 0;
11 }
12
13 ePositionGauge::~ePositionGauge()
14 {
15         delete m_point_widget;
16 }
17
18 void ePositionGauge::setLength(const pts_t &len)
19 {
20         if (m_length == len)
21                 return;
22         m_length = len;
23         updatePosition();
24         invalidate();
25 }
26
27 void ePositionGauge::setPosition(const pts_t &pos)
28 {
29         if (m_position == pos)
30                 return;
31         m_position = pos;
32         updatePosition();
33 }
34
35 void ePositionGauge::setInColor(const gRGB &color)
36 {
37         invalidate();
38 }
39
40 void ePositionGauge::setPointer(gPixmap *pixmap, const ePoint &center)
41 {
42         m_point_center = center;
43         m_point_widget->setPixmap(pixmap);
44         m_point_widget->resize(pixmap->size());
45         updatePosition();
46 }
47
48 void ePositionGauge::setInOutList(PyObject *list)
49 {
50         if (!PyList_Check(list))
51                 return;
52         int size = PyList_Size(list);
53         int i;
54         
55         m_cue_entries.clear();
56         
57         for (i=0; i<size; ++i)
58         {
59                 PyObject *tuple = PyList_GetItem(list, i);
60                 if (!PyTuple_Check(tuple))
61                         continue;
62
63                 if (PyTuple_Size(tuple) != 2)
64                         continue;
65
66                 PyObject *ppts = PyTuple_GetItem(tuple, 0), *ptype = PyTuple_GetItem(tuple, 1);
67                 if (!(PyLong_Check(ppts) && PyInt_Check(ptype)))
68                         continue;
69
70                 pts_t pts = PyLong_AsLongLong(ppts);
71                 int type = PyInt_AsLong(ptype);
72                 m_cue_entries.insert(cueEntry(pts, type));
73         }
74         invalidate();
75 }
76
77 int ePositionGauge::event(int event, void *data, void *data2)
78 {
79         switch (event)
80         {
81         case evtPaint:
82         {
83                 ePtr<eWindowStyle> style;
84                 gPainter &painter = *(gPainter*)data2;
85
86                 eSize s(size());
87
88                 getStyle(style);
89                 
90                 eWidget::event(evtPaint, data, data2);
91
92                 style->setStyle(painter, eWindowStyle::styleLabel); // TODO - own style
93 //              painter.fill(eRect(0, 10, s.width(), s.height()-20));
94                 
95                 pts_t in = 0, out = 0;
96                 
97                 std::multiset<cueEntry>::iterator i(m_cue_entries.begin());
98                 
99                 while (1)
100                 {
101                         if (i == m_cue_entries.end())
102                                 out = m_length;
103                         else {
104                                 if (i->what == 0) /* in */
105                                 {
106                                         in = i++->where;
107                                         continue;
108                                 } else if (i->what == 1) /* out */
109                                         out = i++->where;
110                                 else /* mark */
111                                 {
112                                         int xm = scale(i->where);
113                                         painter.setForegroundColor(gRGB(0xFF8080));
114                                         painter.fill(eRect(xm - 2, 0, 4, s.height()));
115                                         i++;
116                                         continue;
117                                 }
118                         }
119                         
120                         painter.setForegroundColor(gRGB(0x225b7395));
121                         int xi = scale(in), xo = scale(out);
122                         painter.fill(eRect(xi, 10, xo-xi, s.height()-14));
123                         in = m_length;
124                         
125                         if (i == m_cue_entries.end())
126                                 break;
127                 }
128 //              painter.setForegroundColor(gRGB(0x00000000));
129                 painter.setForegroundColor(gRGB(0x225b7395));
130                 painter.fill(eRect(s.width() - 2, 2, s.width() - 1, s.height() - 4));
131                 painter.fill(eRect(0, 2, 2, s.height() - 4));
132                 
133 #if 0
134 // border
135                 if (m_have_border_color)
136                         painter.setForegroundColor(m_border_color);
137                 painter.fill(eRect(0, 0, s.width(), m_border_width));
138                 painter.fill(eRect(0, m_border_width, m_border_width, s.height()-m_border_width));
139                 painter.fill(eRect(m_border_width, s.height()-m_border_width, s.width()-m_border_width, m_border_width));
140                 painter.fill(eRect(s.width()-m_border_width, m_border_width, m_border_width, s.height()-m_border_width));
141 #endif
142
143                 return 0;
144         }
145         case evtChangedPosition:
146                 return 0;
147         default:
148                 return eWidget::event(event, data, data2);
149         }
150 }
151
152 void ePositionGauge::updatePosition()
153 {
154         m_pos = scale(m_position);
155         int base = (size().height() - 10) / 2;
156         
157         m_point_widget->move(ePoint(m_pos - m_point_center.x(), base - m_point_center.y()));
158 }
159
160 int ePositionGauge::scale(const pts_t &val)
161 {
162         if (!m_length)
163                 return 0;
164
165         int width = size().width();
166
167         return width * val / m_length;
168 }