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