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