remove test cruft, add possibility for a seperator
[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
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         /* 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(int 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                 eDebug("position not selectable");
163                         /* ok, our cursor position is valid (i.e. in list), but not selectable. */
164                         
165                         /* when moving up, continue until we found a valid position. */
166                 if ((dir == moveUp) || (dir == pageDown))
167                 {
168                         eDebug("moving up");
169                         while (m_content->cursorGet())
170                         {
171                                 eDebug("tick");
172                                 m_content->cursorMove(-1);
173                                 if (m_content->currentCursorSelectable())
174                                 {
175                                         eDebug("now ok");
176                                         break;
177                                 }
178                         }
179                 } else
180                 {
181                         eDebug("moving down");
182                         while (m_content->cursorValid())
183                         {
184                                 eDebug("TICK");
185                                 m_content->cursorMove(+1);
186                                 if (m_content->currentCursorSelectable())
187                                 {
188                                         eDebug("NOW ok");
189                                         break;
190                                 }
191                         }
192                         
193                         if (!m_content->cursorValid())
194                         {
195                                 eDebug("not valid (i.e.: end)");
196                                 m_content->cursorMove(-1);
197                         }
198                 }
199                 
200                 if (!m_content->currentCursorSelectable())
201                 {
202                         eDebug("can't move!");
203                         m_content->cursorSet(oldsel);
204                 }
205         }
206         
207                 /* note that we could be on an invalid cursor position, but we don't
208                    care. this only happens on empty lists, and should have almost no
209                    side effects. */
210         
211                 /* now, look wether the current selection is out of screen */
212         m_selected = m_content->cursorGet();
213         while (m_selected < m_top)
214         {
215                 m_top -= m_items_per_page;
216                 if (m_top < 0)
217                         m_top = 0;
218         }
219         while (m_selected >= m_top + m_items_per_page)
220                 /* m_top should be always valid here as it's selected */
221                 m_top += m_items_per_page;
222
223         if (oldsel != m_selected)
224                 /* emit */ selectionChanged();
225
226         updateScrollBar();
227
228         if (m_top != oldtop)
229                 invalidate();
230         else if (m_selected != oldsel)
231         {
232    /* redraw the old and newly selected */
233                 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
234                 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
235                 
236                 invalidate(inv);
237         }
238 }
239
240 void eListbox::moveSelectionTo(int index)
241 {
242         if (m_content)
243         {
244                 m_content->cursorHome();
245                 m_content->cursorMove(index);
246                 moveSelection(justCheck);
247         }
248 }
249
250 int eListbox::getCurrentIndex()
251 {
252         if (m_content && m_content->cursorValid())
253                 return m_content->cursorGet();
254         return 0;
255 }
256
257 void eListbox::updateScrollBar()
258 {
259         if (!m_content || m_scrollbar_mode == showNever )
260                 return;
261         int entries = m_content->size();
262         if (m_content_changed)
263         {
264                 int width = size().width();
265                 int height = size().height();
266                 m_content_changed = false;
267                 if (entries > m_items_per_page || m_scrollbar_mode == showAlways)
268                 {
269                         int sbarwidth=width/16;
270                         if (sbarwidth < 18)
271                                 sbarwidth = 18;
272                         if (sbarwidth > 22)
273                                 sbarwidth = 22;
274                         m_scrollbar->move(ePoint(width-sbarwidth, 0));
275                         m_scrollbar->resize(eSize(sbarwidth, height));
276                         m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
277                         m_scrollbar->show();
278                 }
279                 else
280                 {
281                         m_content->setSize(eSize(width, m_itemheight));
282                         m_scrollbar->hide();
283                 }
284         }
285         if (m_items_per_page && entries)
286         {
287                 int curVisiblePage = m_top / m_items_per_page;
288                 if (m_prev_scrollbar_page != curVisiblePage)
289                 {
290                         m_prev_scrollbar_page = curVisiblePage;
291                         int pages = entries / m_items_per_page;
292                         if ((pages*m_items_per_page) < entries)
293                                 ++pages;
294                         int start=(m_top*100)/(pages*m_items_per_page);
295                         int vis=(m_items_per_page*100)/(pages*m_items_per_page);
296                         if (vis < 3)
297                                 vis=3;
298                         m_scrollbar->setStartEnd(start,start+vis);
299                 }
300         }
301 }
302
303 int eListbox::event(int event, void *data, void *data2)
304 {
305         switch (event)
306         {
307         case evtPaint:
308         {
309                 ePtr<eWindowStyle> style;
310                 
311                 if (!m_content)
312                         return eWidget::event(event, data, data2);
313                 assert(m_content);
314                 
315                 getStyle(style);
316                 
317                 if (!m_content)
318                         return 0;
319                 
320                 gPainter &painter = *(gPainter*)data2;
321                 
322                 m_content->cursorSave();
323                 m_content->cursorMove(m_top - m_selected);
324                 
325                 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
326                 {
327                         m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
328                         m_content->cursorMove(+1);
329                 }
330
331                 if (m_scrollbar && m_scrollbar->isVisible())
332                 {
333                         painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
334                         painter.clear();
335                         painter.clippop();
336                 }
337
338                 m_content->cursorRestore();
339
340                 return 0;
341         }
342         case evtChangedSize:
343                 recalcSize();
344                 return eWidget::event(event, data, data2);
345                 
346         case evtAction:
347                 if (isVisible())
348                 {
349                         moveSelection((int)data2);
350                         return 1;
351                 }
352                 return 0;
353         default:
354                 return eWidget::event(event, data, data2);
355         }
356 }
357
358 void eListbox::recalcSize()
359 {
360         m_content_changed=true;
361         m_prev_scrollbar_page=-1;
362         m_content->setSize(eSize(size().width(), m_itemheight));
363         m_items_per_page = size().height() / m_itemheight;
364
365         if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
366                 m_items_per_page = 0;
367
368         moveSelection(justCheck);
369 }
370
371 void eListbox::setItemHeight(int h)
372 {
373         if (h)
374                 m_itemheight = h;
375         else
376                 m_itemheight = 20;
377         recalcSize();
378 }
379
380 void eListbox::setSelectionEnable(int en)
381 {
382         if (m_selection_enabled == en)
383                 return;
384         m_selection_enabled = en;
385         entryChanged(m_selected); /* redraw current entry */
386 }
387
388 void eListbox::entryAdded(int index)
389 {
390                 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
391                 
392                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
393         if (index <= m_selected)
394                 ++m_selected;
395         if (index <= m_top)
396                 ++m_top;
397                 
398                 /* we have to check wether our current cursor is gone out of the screen. */
399                 /* moveSelection will check for this case */
400         moveSelection(justCheck);
401         
402                 /* now, check if the new index is visible. */
403         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
404         {
405                         /* todo, calc exact invalidation... */
406                 invalidate();
407         }
408 }
409
410 void eListbox::entryRemoved(int index)
411 {
412         if (index == m_selected)
413                 m_selected = m_content->cursorGet();
414
415         moveSelection(justCheck);
416
417         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
418         {
419                         /* todo, calc exact invalidation... */
420                 invalidate();
421         }
422 }
423
424 void eListbox::entryChanged(int index)
425 {
426         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
427         {
428                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
429                 invalidate(inv);
430         }
431 }
432
433 void eListbox::entryReset(bool selectionHome)
434 {
435         m_content_changed = true;
436         m_prev_scrollbar_page = -1;
437
438         if (selectionHome)
439         {
440                 if (m_content)
441                         m_content->cursorHome();
442                 m_top = 0;
443                 m_selected = 0;
444         }
445         
446         if (m_content && (m_selected >= m_content->size()))
447         {
448                 if (m_content->size())
449                         m_selected = m_content->size() - 1;
450                 else
451                         m_selected = 0;
452                 m_content->cursorSet(m_selected);
453                 selectionChanged();
454         }
455         
456         moveSelection(justCheck);
457         invalidate();
458 }