TYPE_PIXMAP_ALPHATEST in eListboxMultiContent, fix alphatest
[enigma2.git] / lib / gui / elistbox.cpp
1 #include <lib/gui/elistbox.h>
2 #include <lib/gui/elistboxcontent.h>
3 #include <lib/gui/eslider.h>
4 #include <lib/actions/action.h>
5
6 eListbox::eListbox(eWidget *parent)
7         :eWidget(parent), m_scrollbar_mode(showNever), m_prev_scrollbar_page(-1)
8         ,m_content_changed(false), m_top(0), m_selected(0), m_itemheight(25)
9         ,m_items_per_page(0), m_selection_enabled(1), m_scrollbar(NULL)
10 {
11 //      setContent(new eListboxStringContent());
12
13         ePtr<eActionMap> ptr;
14         eActionMap::getInstance(ptr);
15         ptr->bindAction("ListboxActions", 0, 0, this);
16 }
17
18 eListbox::~eListbox()
19 {
20         if (m_scrollbar)
21                 delete m_scrollbar;
22         
23         ePtr<eActionMap> ptr;
24         eActionMap::getInstance(ptr);
25         ptr->unbindAction(this, 0);
26 }
27
28 void eListbox::setScrollbarMode(int mode)
29 {
30         m_scrollbar_mode = mode;
31         if ( m_scrollbar )
32         {
33                 if ( m_scrollbar_mode == showNever )
34                 {
35                         delete m_scrollbar;
36                         m_scrollbar=0;
37                 }
38         }
39         else
40         {
41                 m_scrollbar = new eSlider(this);
42                 m_scrollbar->hide();
43                 m_scrollbar->setBorderWidth(1);
44                 m_scrollbar->setOrientation(eSlider::orVertical);
45                 m_scrollbar->setRange(0,100);
46         }
47 }
48
49 void eListbox::setContent(iListboxContent *content)
50 {
51         int oldsel = m_selected;
52         m_content = content;
53         if (content)
54                 m_content->setListbox(this);
55         entryReset();
56         if (oldsel == m_selected)
57                 /* emit */ selectionChanged();
58 }
59
60 void eListbox::moveSelection(int dir)
61 {
62                 /* refuse to do anything without a valid list. */
63         if (!m_content)
64                 return;
65                 /* if our list does not have one entry, don't do anything. */
66         if (!m_items_per_page)
67                 return;
68                 /* we need the old top/sel to see what we have to redraw */
69         int oldtop = m_top;
70         int oldsel = m_selected;
71                 /* first, move cursor */
72         switch (dir)
73         {
74         case moveUp:
75                 m_content->cursorMove(-1);
76                 break;
77         case moveDown:
78                 m_content->cursorMove(1);
79                         /* ok - we could have reached the end. we just go one back then. */
80                 if (!m_content->cursorValid())
81                         m_content->cursorMove(-1);
82                 break;
83         case pageUp:
84                 if (m_content->cursorGet() >= m_items_per_page)
85                 {
86                         m_content->cursorMove(-m_items_per_page);
87                         m_top -= m_items_per_page;
88                         if (m_top < 0)
89                                 m_top = 0;
90                 } else
91                 {
92                         m_top = 0;
93                         m_content->cursorHome();
94                 }
95                 break;
96         case moveTop:
97                 m_content->cursorHome();
98                 m_top = 0; /* align with top, speeds up process */
99                 break;
100
101         case pageDown:
102                 m_content->cursorMove(m_items_per_page);
103                 if (m_content->cursorValid())
104                         break;
105                                 /* fall through */
106         case moveEnd:
107                         /* move to last existing one ("end" is already invalid) */
108                 m_content->cursorEnd(); m_content->cursorMove(-1); 
109                         /* current selection invisible? */
110                 if (m_top + m_items_per_page <= m_content->cursorGet())
111                 {
112                         int rest = m_content->size() % m_items_per_page;
113                         if ( rest )
114                                 m_top = m_content->cursorGet() - rest + 1;
115                         else
116                                 m_top = m_content->cursorGet() - m_items_per_page + 1;
117                         if (m_top < 0)
118                                 m_top = 0;
119                 }
120                 break;
121         case justCheck:
122                 break;
123         }
124         
125                 /* note that we could be on an invalid cursor position, but we don't
126                    care. this only happens on empty lists, and should have almost no
127                    side effects. */
128         
129                 /* now, look wether the current selection is out of screen */
130         m_selected = m_content->cursorGet();
131         while (m_selected < m_top)
132         {
133                 m_top -= m_items_per_page;
134                 if (m_top < 0)
135                         m_top = 0;
136         }
137         while (m_selected >= m_top + m_items_per_page)
138                 /* m_top should be always valid here as it's selected */
139                 m_top += m_items_per_page;
140
141         if (oldsel != m_selected)
142                 /* emit */ selectionChanged();
143
144         updateScrollBar();
145
146         if (m_top != oldtop)
147                 invalidate();
148         else if (m_selected != oldsel)
149         {
150    /* redraw the old and newly selected */
151                 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
152                 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
153                 
154                 invalidate(inv);
155         }
156 }
157
158 void eListbox::moveSelectionTo(int index)
159 {
160         if ( m_content )
161         {
162                 m_content->cursorHome();
163                 m_content->cursorMove(index);
164                 moveSelection(justCheck);
165         }
166 }
167
168 int eListbox::getCurrentIndex()
169 {
170         if ( m_content && m_content->cursorValid() )
171                 return m_content->cursorGet();
172         return 0;
173 }
174
175 void eListbox::updateScrollBar()
176 {
177         if (!m_content || m_scrollbar_mode == showNever )
178                 return;
179         int entries = m_content->size();
180         if ( m_content_changed )
181         {
182                 int width = size().width();
183                 int height = size().height();
184                 m_content_changed = false;
185                 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
186                 {
187                         int sbarwidth=width/16;
188                         if ( sbarwidth < 18 )
189                                 sbarwidth=18;
190                         if ( sbarwidth > 22 )
191                                 sbarwidth=22;
192                         m_scrollbar->move(ePoint(width-sbarwidth, 0));
193                         m_scrollbar->resize(eSize(sbarwidth, height));
194                         m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
195                         m_scrollbar->show();
196                 }
197                 else
198                 {
199                         m_content->setSize(eSize(width, m_itemheight));
200                         m_scrollbar->hide();
201                 }
202         }
203         if ( m_items_per_page && entries )
204         {
205                 int curVisiblePage = m_top / m_items_per_page;
206                 if (m_prev_scrollbar_page != curVisiblePage)
207                 {
208                         m_prev_scrollbar_page = curVisiblePage;
209                         int pages = entries / m_items_per_page;
210                         if ( (pages*m_items_per_page) < entries )
211                                 ++pages;
212                         int start=(m_top*100)/(pages*m_items_per_page);
213                         int vis=(m_items_per_page*100)/(pages*m_items_per_page);
214                         if (vis < 3)
215                                 vis=3;
216                         m_scrollbar->setStartEnd(start,start+vis);
217                 }
218         }
219 }
220
221 int eListbox::event(int event, void *data, void *data2)
222 {
223         switch (event)
224         {
225         case evtPaint:
226         {
227                 ePtr<eWindowStyle> style;
228                 
229                 if (!m_content)
230                         return eWidget::event(event, data, data2);
231                 assert(m_content);
232                 
233                 getStyle(style);
234                 
235                 if (!m_content)
236                         return 0;
237                 
238                 gPainter &painter = *(gPainter*)data2;
239                 
240                 m_content->cursorSave();
241                 m_content->cursorMove(m_top - m_selected);
242                 
243                 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
244                 {
245                         m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
246                         m_content->cursorMove(+1);
247                 }
248
249                 if ( m_scrollbar && m_scrollbar->isVisible() )
250                 {
251                         painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
252                         painter.clear();
253                         painter.clippop();
254                 }
255
256                 m_content->cursorRestore();
257
258                 return 0;
259         }
260         case evtChangedSize:
261                 recalcSize();
262                 return eWidget::event(event, data, data2);
263                 
264         case evtAction:
265                 if (isVisible())
266                 {
267                         moveSelection((int)data2);
268                         return 1;
269                 }
270                 return 0;
271         default:
272                 return eWidget::event(event, data, data2);
273         }
274 }
275
276 void eListbox::recalcSize()
277 {
278         m_content_changed=true;
279         m_prev_scrollbar_page=-1;
280         m_content->setSize(eSize(size().width(), m_itemheight));
281         m_items_per_page = size().height() / m_itemheight;
282
283         if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
284                 m_items_per_page = 0;
285
286         moveSelection(justCheck);
287 }
288
289 void eListbox::setItemHeight(int h)
290 {
291         if (h)
292                 m_itemheight = h;
293         else
294                 m_itemheight = 20;
295         recalcSize();
296 }
297
298 void eListbox::setSelectionEnable(int en)
299 {
300         if (m_selection_enabled == en)
301                 return;
302         m_selection_enabled = en;
303         entryChanged(m_selected); /* redraw current entry */
304 }
305
306 void eListbox::entryAdded(int index)
307 {
308                 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
309                 
310                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
311         if (index <= m_selected)
312                 ++m_selected;
313         if (index <= m_top)
314                 ++m_top;
315                 
316                 /* we have to check wether our current cursor is gone out of the screen. */
317                 /* moveSelection will check for this case */
318         moveSelection(justCheck);
319         
320                 /* now, check if the new index is visible. */
321         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
322         {
323                         /* todo, calc exact invalidation... */
324                 invalidate();
325         }
326 }
327
328 void eListbox::entryRemoved(int index)
329 {
330         if (index == m_selected)
331                 m_selected = m_content->cursorGet();
332
333         moveSelection(justCheck);
334
335         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
336         {
337                         /* todo, calc exact invalidation... */
338                 invalidate();
339         }
340 }
341
342 void eListbox::entryChanged(int index)
343 {
344         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
345         {
346                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
347                 invalidate(inv);
348         }
349 }
350
351 void eListbox::entryReset(bool cursorHome)
352 {
353         m_content_changed=true;
354         m_prev_scrollbar_page=-1;
355         if ( cursorHome )
356         {
357                 if (m_content)
358                         m_content->cursorHome();
359                 m_top = 0;
360                 m_selected = 0;
361         }
362         moveSelection(justCheck);
363         invalidate();
364 }