Support focus animation for listbox
[vuplus_dvbapp] / 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 #ifdef USE_LIBVUGLES2
6 #include "vuplus_gles.h"
7 #endif
8
9 eListbox::eListbox(eWidget *parent) :
10         eWidget(parent), m_scrollbar_mode(showNever), m_prev_scrollbar_page(-1),
11         m_content_changed(false), m_enabled_wrap_around(false), m_top(0), m_selected(0), m_itemheight(25),
12         m_items_per_page(0), m_selection_enabled(1), m_scrollbar(NULL)
13 {
14         memset(&m_style, 0, sizeof(m_style));
15 //      setContent(new eListboxStringContent());
16
17         ePtr<eActionMap> ptr;
18         eActionMap::getInstance(ptr);
19         ptr->bindAction("ListboxActions", 0, 0, this);
20 }
21
22 eListbox::~eListbox()
23 {
24         if (m_scrollbar)
25                 delete m_scrollbar;
26         
27         ePtr<eActionMap> ptr;
28         eActionMap::getInstance(ptr);
29         ptr->unbindAction(this, 0);
30 }
31
32 void eListbox::setScrollbarMode(int mode)
33 {
34         m_scrollbar_mode = mode;
35         if (m_scrollbar)
36         {
37                 if (m_scrollbar_mode == showNever)
38                 {
39                         delete m_scrollbar;
40                         m_scrollbar=0;
41                 }
42         }
43         else
44         {
45                 m_scrollbar = new eSlider(this);
46                 m_scrollbar->hide();
47                 m_scrollbar->setBorderWidth(1);
48                 m_scrollbar->setOrientation(eSlider::orVertical);
49                 m_scrollbar->setRange(0,100);
50         }
51 }
52
53 void eListbox::setWrapAround(bool state)
54 {
55         m_enabled_wrap_around = state;
56 }
57
58 void eListbox::setContent(iListboxContent *content)
59 {
60         m_content = content;
61         if (content)
62                 m_content->setListbox(this);
63         entryReset();
64 }
65
66 bool eListbox::atBegin()
67 {
68         if (m_content && !m_selected)
69                 return true;
70         return false;
71 }
72
73 bool eListbox::atEnd()
74 {
75         if (m_content && m_content->size() == m_selected+1)
76                 return true;
77         return false;
78 }
79
80 void eListbox::moveToEnd()
81 {
82         if (!m_content)
83                 return;
84         /* move to last existing one ("end" is already invalid) */
85         m_content->cursorEnd(); m_content->cursorMove(-1);
86         /* current selection invisible? */
87         if (m_top + m_items_per_page <= m_content->cursorGet())
88         {
89                 int rest = m_content->size() % m_items_per_page;
90                 if (rest)
91                         m_top = m_content->cursorGet() - rest + 1;
92                 else
93                         m_top = m_content->cursorGet() - m_items_per_page + 1;
94                 if (m_top < 0)
95                         m_top = 0;
96         }
97 }
98
99 void eListbox::moveSelection(long dir)
100 {
101                 /* refuse to do anything without a valid list. */
102         if (!m_content)
103                 return;
104                 /* if our list does not have one entry, don't do anything. */
105         if (!m_items_per_page)
106                 return;
107                 /* we need the old top/sel to see what we have to redraw */
108         int oldtop = m_top;
109         int oldsel = m_selected;
110                 /* first, move cursor */
111 #ifdef USE_LIBVUGLES2
112         m_dir = dir;
113 #endif
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                 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-1)/(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::getEntryTop()
297 {
298         return (m_selected - m_top) * m_itemheight;
299 }
300
301 int eListbox::event(int event, void *data, void *data2)
302 {
303         switch (event)
304         {
305         case evtPaint:
306         {
307                 ePtr<eWindowStyle> style;
308                 
309                 if (!m_content)
310                         return eWidget::event(event, data, data2);
311                 ASSERT(m_content);
312                 
313                 getStyle(style);
314                 
315                 if (!m_content)
316                         return 0;
317                 
318                 gPainter &painter = *(gPainter*)data2;
319                 
320                 m_content->cursorSave();
321                 m_content->cursorMove(m_top - m_selected);
322                 
323                 gRegion entryrect = eRect(0, 0, size().width(), m_itemheight);
324                 const gRegion &paint_region = *(gRegion*)data;
325
326                 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
327                 {
328                         gRegion entry_clip_rect = paint_region & entryrect;
329
330                         if (!entry_clip_rect.empty())
331                                 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
332 #ifdef USE_LIBVUGLES2
333                         if (m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled) {
334                                 ePoint pos = getAbsolutePosition();
335                                 painter.sendShowItem(m_dir, ePoint(pos.x(), pos.y() + y), eSize(m_scrollbar && m_scrollbar->isVisible() ? size().width() - m_scrollbar->size().width() : size().width(), m_itemheight));
336                                 gles_set_animation_listbox_current(pos.x(), pos.y() + y, m_scrollbar && m_scrollbar->isVisible() ? size().width() - m_scrollbar->size().width() : size().width(), m_itemheight);
337                                 m_dir = justCheck;
338                         }
339 #endif
340
341                                 /* (we could clip with entry_clip_rect, but 
342                                    this shouldn't change the behavior of any
343                                    well behaving content, so it would just
344                                    degrade performance without any gain.) */
345
346                         m_content->cursorMove(+1);
347                         entryrect.moveBy(ePoint(0, m_itemheight));
348                 }
349
350                 // clear/repaint empty/unused space between scrollbar and listboxentrys
351                 if (m_scrollbar && m_scrollbar->isVisible())
352                 {
353                         style->setStyle(painter, eWindowStyle::styleListboxNormal);
354                         painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
355                         painter.clear();
356                         painter.clippop();
357                 }
358
359                 m_content->cursorRestore();
360
361                 return 0;
362         }
363
364         case evtChangedSize:
365                 recalcSize();
366                 return eWidget::event(event, data, data2);
367                 
368         case evtAction:
369                 if (isVisible())
370                 {
371                         moveSelection((long)data2);
372                         return 1;
373                 }
374                 return 0;
375         default:
376                 return eWidget::event(event, data, data2);
377         }
378 }
379
380 void eListbox::recalcSize()
381 {
382         m_content_changed=true;
383         m_prev_scrollbar_page=-1;
384         if (m_content)
385                 m_content->setSize(eSize(size().width(), m_itemheight));
386         m_items_per_page = size().height() / m_itemheight;
387
388         if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
389                 m_items_per_page = 0;
390
391         moveSelection(justCheck);
392 }
393
394 void eListbox::setItemHeight(int h)
395 {
396         if (h)
397                 m_itemheight = h;
398         else
399                 m_itemheight = 20;
400         recalcSize();
401 }
402
403 void eListbox::setSelectionEnable(int en)
404 {
405         if (m_selection_enabled == en)
406                 return;
407         m_selection_enabled = en;
408         entryChanged(m_selected); /* redraw current entry */
409 }
410
411 void eListbox::entryAdded(int index)
412 {
413         if (m_content && (m_content->size() % m_items_per_page) == 1)
414                 m_content_changed=true;
415         /* manage our local pointers. when the entry was added before the current position, we have to advance. */
416                 
417                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
418         if (index <= m_selected)
419                 ++m_selected;
420         if (index <= m_top)
421                 ++m_top;
422                 
423                 /* we have to check wether our current cursor is gone out of the screen. */
424                 /* moveSelection will check for this case */
425         moveSelection(justCheck);
426         
427                 /* now, check if the new index is visible. */
428         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
429         {
430                         /* todo, calc exact invalidation... */
431                 invalidate();
432         }
433 }
434
435 void eListbox::entryRemoved(int index)
436 {
437         if (m_content && !(m_content->size() % m_items_per_page))
438                 m_content_changed=true;
439
440         if (index == m_selected && m_content)
441                 m_selected = m_content->cursorGet();
442
443         if (m_content && m_content->cursorGet() >= m_content->size())
444                 moveSelection(moveUp);
445         else
446                 moveSelection(justCheck);
447
448         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
449         {
450                         /* todo, calc exact invalidation... */
451                 invalidate();
452         }
453 }
454
455 void eListbox::entryChanged(int index)
456 {
457         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
458         {
459                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
460                 invalidate(inv);
461         }
462 }
463
464 void eListbox::entryReset(bool selectionHome)
465 {
466         m_content_changed = true;
467         m_prev_scrollbar_page = -1;
468         int oldsel;
469
470         if (selectionHome)
471         {
472                 if (m_content)
473                         m_content->cursorHome();
474                 m_top = 0;
475                 m_selected = 0;
476         }
477         
478         if (m_content && (m_selected >= m_content->size()))
479         {
480                 if (m_content->size())
481                         m_selected = m_content->size() - 1;
482                 else
483                         m_selected = 0;
484                 m_content->cursorSet(m_selected);
485         }
486         
487         oldsel = m_selected;
488         moveSelection(justCheck);
489                 /* if oldsel != m_selected, selectionChanged was already 
490                    emitted in moveSelection. we want it in any case, so otherwise,
491                    emit it now. */
492         if (oldsel == m_selected)
493                 /* emit */ selectionChanged();
494         invalidate();
495 }
496
497 void eListbox::setFont(gFont *font)
498 {
499         m_style.m_font = font;
500 }
501
502 void eListbox::setBackgroundColor(gRGB &col)
503 {
504         m_style.m_background_color = col;
505         m_style.m_background_color_set = 1;
506 }
507
508 void eListbox::setBackgroundColorSelected(gRGB &col)
509 {
510         m_style.m_background_color_selected = col;
511         m_style.m_background_color_selected_set = 1;
512 }
513
514 void eListbox::setForegroundColor(gRGB &col)
515 {
516         m_style.m_foreground_color = col;
517         m_style.m_foreground_color_set = 1;
518 }
519
520 void eListbox::setForegroundColorSelected(gRGB &col)
521 {
522         m_style.m_foreground_color_selected = col;
523         m_style.m_foreground_color_selected_set = 1;
524 }
525
526 void eListbox::setBackgroundPicture(ePtr<gPixmap> &pm)
527 {
528         m_style.m_background = pm;
529 }
530
531 void eListbox::setSelectionPicture(ePtr<gPixmap> &pm)
532 {
533         m_style.m_selection = pm;
534 }
535
536 void eListbox::invalidate(const gRegion &region)
537 {
538         gRegion tmp(region);
539         if (m_content)
540                 m_content->updateClip(tmp);
541         eWidget::invalidate(tmp);
542 }
543
544 struct eListboxStyle *eListbox::getLocalStyle(void)
545 {
546                 /* transparency is set directly in the widget */
547         m_style.m_transparent_background = isTransparent();
548         return &m_style;
549 }