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