use long instead of int where pointers are casted
[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         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(long 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                 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::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
333                                 /* (we could clip with entry_clip_rect, but 
334                                    this shouldn't change the behaviour of any
335                                    well behaving content, so it would just
336                                    degrade performance without any gain.) */
337
338                         m_content->cursorMove(+1);
339                         entryrect.moveBy(ePoint(0, m_itemheight));
340                 }
341
342                 // clear/repaint empty/unused space between scrollbar and listboxentrys
343                 if (m_scrollbar && m_scrollbar->isVisible())
344                 {
345                         style->setStyle(painter, eWindowStyle::styleListboxNormal);
346                         painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
347                         painter.clear();
348                         painter.clippop();
349                 }
350
351                 m_content->cursorRestore();
352
353                 return 0;
354         }
355
356         case evtChangedSize:
357                 recalcSize();
358                 return eWidget::event(event, data, data2);
359                 
360         case evtAction:
361                 if (isVisible())
362                 {
363                         moveSelection((long)data2);
364                         return 1;
365                 }
366                 return 0;
367         default:
368                 return eWidget::event(event, data, data2);
369         }
370 }
371
372 void eListbox::recalcSize()
373 {
374         m_content_changed=true;
375         m_prev_scrollbar_page=-1;
376         if (m_content)
377                 m_content->setSize(eSize(size().width(), m_itemheight));
378         m_items_per_page = size().height() / m_itemheight;
379
380         if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
381                 m_items_per_page = 0;
382
383         moveSelection(justCheck);
384 }
385
386 void eListbox::setItemHeight(int h)
387 {
388         if (h)
389                 m_itemheight = h;
390         else
391                 m_itemheight = 20;
392         recalcSize();
393 }
394
395 void eListbox::setSelectionEnable(int en)
396 {
397         if (m_selection_enabled == en)
398                 return;
399         m_selection_enabled = en;
400         entryChanged(m_selected); /* redraw current entry */
401 }
402
403 void eListbox::entryAdded(int index)
404 {
405         if (m_content && (m_content->size() % m_items_per_page) == 1)
406                 m_content_changed=true;
407         /* manage our local pointers. when the entry was added before the current position, we have to advance. */
408                 
409                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
410         if (index <= m_selected)
411                 ++m_selected;
412         if (index <= m_top)
413                 ++m_top;
414                 
415                 /* we have to check wether our current cursor is gone out of the screen. */
416                 /* moveSelection will check for this case */
417         moveSelection(justCheck);
418         
419                 /* now, check if the new index is visible. */
420         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
421         {
422                         /* todo, calc exact invalidation... */
423                 invalidate();
424         }
425 }
426
427 void eListbox::entryRemoved(int index)
428 {
429         if (m_content && !(m_content->size() % m_items_per_page))
430                 m_content_changed=true;
431
432         if (index == m_selected && m_content)
433                 m_selected = m_content->cursorGet();
434
435         if (m_content && m_content->cursorGet() >= m_content->size())
436                 moveSelection(moveUp);
437         else
438                 moveSelection(justCheck);
439
440         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
441         {
442                         /* todo, calc exact invalidation... */
443                 invalidate();
444         }
445 }
446
447 void eListbox::entryChanged(int index)
448 {
449         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
450         {
451                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
452                 invalidate(inv);
453         }
454 }
455
456 void eListbox::entryReset(bool selectionHome)
457 {
458         m_content_changed = true;
459         m_prev_scrollbar_page = -1;
460
461         if (selectionHome)
462         {
463                 if (m_content)
464                         m_content->cursorHome();
465                 m_top = 0;
466                 m_selected = 0;
467         }
468         
469         if (m_content && (m_selected >= m_content->size()))
470         {
471                 if (m_content->size())
472                         m_selected = m_content->size() - 1;
473                 else
474                         m_selected = 0;
475                 m_content->cursorSet(m_selected);
476                 selectionChanged();
477         }
478         
479         moveSelection(justCheck);
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 }