don't merge pixmaps when it's not required
[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_enabled_wrap_around(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::setWrapAround(bool state)
50 {
51         m_enabled_wrap_around = state;
52 }
53
54 void eListbox::setContent(iListboxContent *content)
55 {
56         int oldsel = m_selected;
57         m_content = content;
58         if (content)
59                 m_content->setListbox(this);
60         entryReset();
61         if (oldsel == m_selected)
62                 /* emit */ selectionChanged();
63 }
64
65 bool eListbox::atBegin()
66 {
67         if (m_content && !m_selected)
68                 return true;
69         return false;
70 }
71
72 bool eListbox::atEnd()
73 {
74         if (m_content && m_content->size() == m_selected+1)
75                 return true;
76         return false;
77 }
78
79 void eListbox::moveToEnd()
80 {
81         if (!m_content)
82                 return;
83         /* move to last existing one ("end" is already invalid) */
84         m_content->cursorEnd(); m_content->cursorMove(-1);
85         /* current selection invisible? */
86         if (m_top + m_items_per_page <= m_content->cursorGet())
87         {
88                 int rest = m_content->size() % m_items_per_page;
89                 if (rest)
90                         m_top = m_content->cursorGet() - rest + 1;
91                 else
92                         m_top = m_content->cursorGet() - m_items_per_page + 1;
93                 if (m_top < 0)
94                         m_top = 0;
95         }
96 }
97
98 void eListbox::moveSelection(int dir)
99 {
100                 /* refuse to do anything without a valid list. */
101         if (!m_content)
102                 return;
103                 /* if our list does not have one entry, don't do anything. */
104         if (!m_items_per_page)
105                 return;
106                 /* we need the old top/sel to see what we have to redraw */
107         int oldtop = m_top;
108         int oldsel = m_selected;
109                 /* first, move cursor */
110         switch (dir)
111         {
112         case moveUp:
113         {
114                 m_content->cursorMove(-1);
115                 if (m_enabled_wrap_around && oldsel == m_content->cursorGet())  // must wrap around ?
116                         moveToEnd();
117                 break;
118         }
119         case moveDown:
120                 m_content->cursorMove(1);
121                         /* ok - we could have reached the end. So we do wrap around. */
122                 if (!m_content->cursorValid())
123                 {
124                         if (m_enabled_wrap_around)
125                         {
126                                 m_top = 0;
127                                 m_content->cursorHome();
128                         }
129                         else
130                                 m_content->cursorMove(-1);
131                 }
132                 break;
133         case pageUp:
134                 if (m_content->cursorGet() >= m_items_per_page)
135                 {
136                         m_content->cursorMove(-m_items_per_page);
137                         m_top -= m_items_per_page;
138                         if (m_top < 0)
139                                 m_top = 0;
140                 } else
141                 {
142                         m_top = 0;
143                         m_content->cursorHome();
144                 }
145                 break;
146         case moveTop:
147                 m_content->cursorHome();
148                 m_top = 0; /* align with top, speeds up process */
149                 break;
150         case pageDown:
151                 m_content->cursorMove(m_items_per_page);
152                 if (m_content->cursorValid())
153                         break;
154                                 /* fall through */
155         case moveEnd:
156                 moveToEnd();
157                 break;
158         case justCheck:
159                 break;
160         }
161         
162         if (m_content->cursorValid() && !m_content->currentCursorSelectable())
163         {
164                         /* ok, our cursor position is valid (i.e. in list), but not selectable. */
165                         
166                         /* when moving up, continue until we found a valid position. */
167                 if ((dir == moveUp) || (dir == pageDown))
168                 {
169                         while (m_content->cursorGet())
170                         {
171                                 m_content->cursorMove(-1);
172                                 if (m_content->currentCursorSelectable())
173                                 {
174                                         break;
175                                 }
176                         }
177                 } else
178                 {
179                                 /* else move down */
180                         while (m_content->cursorValid())
181                         {
182                                 m_content->cursorMove(+1);
183                                 if (m_content->currentCursorSelectable())
184                                 {
185                                         break;
186                                 }
187                         }
188                         
189                         if (!m_content->cursorValid())
190                                 m_content->cursorMove(-1);
191                 }
192                 
193                 if (!m_content->currentCursorSelectable())
194                         m_content->cursorSet(oldsel);
195         }
196         
197                 /* note that we could be on an invalid cursor position, but we don't
198                    care. this only happens on empty lists, and should have almost no
199                    side effects. */
200         
201                 /* now, look wether the current selection is out of screen */
202         m_selected = m_content->cursorGet();
203         while (m_selected < m_top)
204         {
205                 m_top -= m_items_per_page;
206                 if (m_top < 0)
207                         m_top = 0;
208         }
209         while (m_selected >= m_top + m_items_per_page)
210                 /* m_top should be always valid here as it's selected */
211                 m_top += m_items_per_page;
212
213         if (oldsel != m_selected)
214                 /* emit */ selectionChanged();
215
216         updateScrollBar();
217
218         if (m_top != oldtop)
219                 invalidate();
220         else if (m_selected != oldsel)
221         {
222    /* redraw the old and newly selected */
223                 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
224                 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
225                 
226                 invalidate(inv);
227         }
228 }
229
230 void eListbox::moveSelectionTo(int index)
231 {
232         if (m_content)
233         {
234                 m_content->cursorHome();
235                 m_content->cursorMove(index);
236                 moveSelection(justCheck);
237         }
238 }
239
240 int eListbox::getCurrentIndex()
241 {
242         if (m_content && m_content->cursorValid())
243                 return m_content->cursorGet();
244         return 0;
245 }
246
247 void eListbox::updateScrollBar()
248 {
249         if (!m_content || m_scrollbar_mode == showNever )
250                 return;
251         int entries = m_content->size();
252         if (m_content_changed)
253         {
254                 int width = size().width();
255                 int height = size().height();
256                 m_content_changed = false;
257                 if (entries > m_items_per_page || m_scrollbar_mode == showAlways)
258                 {
259                         int sbarwidth=width/16;
260                         if (sbarwidth < 18)
261                                 sbarwidth = 18;
262                         if (sbarwidth > 22)
263                                 sbarwidth = 22;
264                         m_scrollbar->move(ePoint(width-sbarwidth, 0));
265                         m_scrollbar->resize(eSize(sbarwidth, height));
266                         m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
267                         m_scrollbar->show();
268                 }
269                 else
270                 {
271                         m_content->setSize(eSize(width, m_itemheight));
272                         m_scrollbar->hide();
273                 }
274         }
275         if (m_items_per_page && entries)
276         {
277                 int curVisiblePage = m_top / m_items_per_page;
278                 if (m_prev_scrollbar_page != curVisiblePage)
279                 {
280                         m_prev_scrollbar_page = curVisiblePage;
281                         int pages = entries / m_items_per_page;
282                         if ((pages*m_items_per_page) < entries)
283                                 ++pages;
284                         int start=(m_top*100)/(pages*m_items_per_page);
285                         int vis=(m_items_per_page*100)/(pages*m_items_per_page);
286                         if (vis < 3)
287                                 vis=3;
288                         m_scrollbar->setStartEnd(start,start+vis);
289                 }
290         }
291 }
292
293 int eListbox::event(int event, void *data, void *data2)
294 {
295         switch (event)
296         {
297         case evtPaint:
298         {
299                 ePtr<eWindowStyle> style;
300                 
301                 if (!m_content)
302                         return eWidget::event(event, data, data2);
303                 assert(m_content);
304                 
305                 getStyle(style);
306                 
307                 if (!m_content)
308                         return 0;
309                 
310                 gPainter &painter = *(gPainter*)data2;
311                 
312                 m_content->cursorSave();
313                 m_content->cursorMove(m_top - m_selected);
314                 
315                 gRegion entryrect = eRect(0, 0, size().width(), m_itemheight);
316                 const gRegion &paint_region = *(gRegion*)data;
317                 
318                 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
319                 {
320                         gRegion entry_clip_rect = paint_region & entryrect;
321
322                         if (!entry_clip_rect.empty())
323                                 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
324
325                                 /* (we could clip with entry_clip_rect, but 
326                                    this shouldn't change the behaviour of any
327                                    well behaving content, so it would just
328                                    degrade performance without any gain.) */
329
330                         m_content->cursorMove(+1);
331                         entryrect.moveBy(ePoint(0, m_itemheight));
332                 }
333
334                 // clear/repaint empty/unused space between scrollbar and listboxentrys
335                 if (m_scrollbar && m_scrollbar->isVisible())
336                 {
337                         style->setStyle(painter, eWindowStyle::styleListboxNormal);
338                         painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
339                         painter.clear();
340                         painter.clippop();
341                 }
342
343                 m_content->cursorRestore();
344
345                 return 0;
346         }
347
348         case evtChangedSize:
349                 recalcSize();
350                 return eWidget::event(event, data, data2);
351                 
352         case evtAction:
353                 if (isVisible())
354                 {
355                         moveSelection((int)data2);
356                         return 1;
357                 }
358                 return 0;
359         default:
360                 return eWidget::event(event, data, data2);
361         }
362 }
363
364 void eListbox::recalcSize()
365 {
366         m_content_changed=true;
367         m_prev_scrollbar_page=-1;
368         if (m_content)
369                 m_content->setSize(eSize(size().width(), m_itemheight));
370         m_items_per_page = size().height() / m_itemheight;
371
372         if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
373                 m_items_per_page = 0;
374
375         moveSelection(justCheck);
376 }
377
378 void eListbox::setItemHeight(int h)
379 {
380         if (h)
381                 m_itemheight = h;
382         else
383                 m_itemheight = 20;
384         recalcSize();
385 }
386
387 void eListbox::setSelectionEnable(int en)
388 {
389         if (m_selection_enabled == en)
390                 return;
391         m_selection_enabled = en;
392         entryChanged(m_selected); /* redraw current entry */
393 }
394
395 void eListbox::entryAdded(int index)
396 {
397                 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
398                 
399                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
400         if (index <= m_selected)
401                 ++m_selected;
402         if (index <= m_top)
403                 ++m_top;
404                 
405                 /* we have to check wether our current cursor is gone out of the screen. */
406                 /* moveSelection will check for this case */
407         moveSelection(justCheck);
408         
409                 /* now, check if the new index is visible. */
410         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
411         {
412                         /* todo, calc exact invalidation... */
413                 invalidate();
414         }
415 }
416
417 void eListbox::entryRemoved(int index)
418 {
419          if (index == m_selected && m_content)
420                 m_selected = m_content->cursorGet();
421
422         moveSelection(justCheck);
423
424         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
425         {
426                         /* todo, calc exact invalidation... */
427                 invalidate();
428         }
429 }
430
431 void eListbox::entryChanged(int index)
432 {
433         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
434         {
435                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
436                 invalidate(inv);
437         }
438 }
439
440 void eListbox::entryReset(bool selectionHome)
441 {
442         m_content_changed = true;
443         m_prev_scrollbar_page = -1;
444
445         if (selectionHome)
446         {
447                 if (m_content)
448                         m_content->cursorHome();
449                 m_top = 0;
450                 m_selected = 0;
451         }
452         
453         if (m_content && (m_selected >= m_content->size()))
454         {
455                 if (m_content->size())
456                         m_selected = m_content->size() - 1;
457                 else
458                         m_selected = 0;
459                 m_content->cursorSet(m_selected);
460                 selectionChanged();
461         }
462         
463         moveSelection(justCheck);
464         invalidate();
465 }