hopefully fix the 99% systemload bug when opening a new 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
6         int m_scrollbar_mode, m_prev_scrollbar_page;
7         bool m_content_changed;
8
9         int m_top, m_selected;
10         int m_itemheight;
11         int m_items_per_page;
12         int m_selection_enabled;
13         ePtr<iListboxContent> m_content;
14         eSlider *m_scrollbar;
15
16 eListbox::eListbox(eWidget *parent)
17         :eWidget(parent), m_scrollbar_mode(showNever), m_prev_scrollbar_page(-1)
18         ,m_content_changed(false), m_top(0), m_selected(0), m_itemheight(25)
19         ,m_items_per_page(0), m_selection_enabled(1), m_scrollbar(NULL)
20 {
21         setContent(new eListboxStringContent());
22
23         ePtr<eActionMap> ptr;
24         eActionMap::getInstance(ptr);
25         ptr->bindAction("ListboxActions", 0, 0, this);
26 }
27
28 eListbox::~eListbox()
29 {
30         if (m_scrollbar)
31                 delete m_scrollbar;
32         
33         ePtr<eActionMap> ptr;
34         eActionMap::getInstance(ptr);
35         ptr->unbindAction(this, 0);
36 }
37
38 void eListbox::setScrollbarMode(int mode)
39 {
40         m_scrollbar_mode = mode;
41         if ( m_scrollbar )
42         {
43                 if ( m_scrollbar_mode == showNever )
44                 {
45                         delete m_scrollbar;
46                         m_scrollbar=0;
47                 }
48         }
49         else
50         {
51                 m_scrollbar = new eSlider(this);
52                 m_scrollbar->hide();
53                 m_scrollbar->setBorderWidth(1);
54                 m_scrollbar->setOrientation(eSlider::orVertical);
55                 m_scrollbar->setRange(0,100);
56         }
57 }
58
59 void eListbox::setContent(iListboxContent *content)
60 {
61         m_content = content;
62         if (content)
63                 m_content->setListbox(this);
64         entryReset();
65 }
66
67 void eListbox::moveSelection(int dir)
68 {
69                 /* refuse to do anything without a valid list. */
70         if (!m_content)
71                 return;
72         
73                 /* if our list does not have one entry, don't do anything. */
74         if (!m_items_per_page)
75                 return;
76                 
77                 /* we need the old top/sel to see what we have to redraw */
78         int oldtop = m_top;
79         int oldsel = m_selected;
80         
81                 /* first, move cursor */
82         switch (dir)
83         {
84         case moveUp:
85                 m_content->cursorMove(-1);
86                 break;
87         case moveDown:
88                 m_content->cursorMove(1);
89                         /* ok - we could have reached the end. we just go one back then. */
90                 if (!m_content->cursorValid())
91                         m_content->cursorMove(-1);
92                 break;
93         case pageUp:
94                 if (m_content->cursorGet() >= m_items_per_page)
95                 {
96                         m_content->cursorMove(-m_items_per_page);
97                         m_top -= m_items_per_page;
98                         if (m_top < 0)
99                                 m_top = 0;
100                 } else
101                 {
102                         m_top = 0;
103                         m_content->cursorHome();
104                 }
105                 break;
106         case moveTop:
107                 m_content->cursorHome();
108                 m_top = 0; /* align with top, speeds up process */
109                 break;
110
111         case pageDown:
112                 m_content->cursorMove(m_items_per_page);
113                 if (m_content->cursorValid())
114                         break;
115                                 /* fall through */
116         case moveEnd:
117                         /* move to last existing one ("end" is already invalid) */
118                 m_content->cursorEnd(); m_content->cursorMove(-1); 
119                         /* current selection invisible? */
120                 if (m_top + m_items_per_page <= m_content->cursorGet())
121                 {
122                         int rest = m_content->size() % m_items_per_page;
123                         if ( rest )
124                                 m_top = m_content->cursorGet() - rest + 1;
125                         else
126                                 m_top = m_content->cursorGet() - m_items_per_page + 1;
127                         if (m_top < 0)
128                                 m_top = 0;
129                 }
130                 break;
131         case justCheck:
132                 break;
133         }
134         
135                 /* note that we could be on an invalid cursor position, but we don't
136                    care. this only happens on empty lists, and should have almost no
137                    side effects. */
138         
139                 /* now, look wether the current selection is out of screen */
140         m_selected = m_content->cursorGet();
141
142         while (m_selected < m_top)
143         {
144                 m_top -= m_items_per_page;
145                 if (m_top < 0)
146                         m_top = 0;
147         }
148         while (m_selected >= m_top + m_items_per_page)
149                 /* m_top should be always valid here as it's selected */
150                 m_top += m_items_per_page;
151
152         updateScrollBar();
153
154         if (m_top != oldtop)
155                 invalidate();
156         else if (m_selected != oldsel)
157         {
158                 
159                         /* redraw the old and newly selected */
160                 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
161                 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
162                 
163                 invalidate(inv);
164         }
165 }
166
167 void eListbox::moveSelectionTo(int index)
168 {
169         if ( m_content )
170         {
171                 m_content->cursorHome();
172                 m_content->cursorMove(index);
173                 moveSelection(justCheck);
174         }
175 }
176
177 int eListbox::getCurrentIndex()
178 {
179         if ( m_content && m_content->cursorValid() )
180                 return m_content->cursorGet();
181         return 0;
182 }
183
184 void eListbox::updateScrollBar()
185 {
186         if (!m_content || m_scrollbar_mode == showNever )
187                 return;
188         int entries = m_content->size();
189         if ( m_content_changed )
190         {
191                 int width = size().width();
192                 int height = size().height();
193                 m_content_changed = false;
194                 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
195                 {
196                         int sbarwidth=width/16;
197                         if ( sbarwidth < 18 )
198                                 sbarwidth=18;
199                         if ( sbarwidth > 22 )
200                                 sbarwidth=22;
201                         m_scrollbar->move(ePoint(width-sbarwidth, 0));
202                         m_scrollbar->resize(eSize(sbarwidth, height));
203                         m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
204                         m_scrollbar->show();
205                 }
206                 else
207                 {
208                         m_content->setSize(eSize(width, m_itemheight));
209                         m_scrollbar->hide();
210                 }
211         }
212         if ( m_items_per_page && entries )
213         {
214                 int curVisiblePage = m_top / m_items_per_page;
215                 if (m_prev_scrollbar_page != curVisiblePage)
216                 {
217                         m_prev_scrollbar_page = curVisiblePage;
218                         int pages = entries / m_items_per_page;
219                         if ( (pages*m_items_per_page) < entries )
220                                 ++pages;
221                         int start=(m_top*100)/(pages*m_items_per_page);
222                         int vis=(m_items_per_page*100)/(pages*m_items_per_page);
223                         if (vis < 3)
224                                 vis=3;
225                         m_scrollbar->setStartEnd(start,start+vis);
226                 }
227         }
228 }
229
230 int eListbox::event(int event, void *data, void *data2)
231 {
232         switch (event)
233         {
234         case evtPaint:
235         {
236                 timeval t, t2;
237                 gettimeofday(&t, 0);
238
239                 ePtr<eWindowStyle> style;
240                 
241                 if (!m_content)
242                         return eWidget::event(event, data, data2);
243                 assert(m_content);
244                 
245                 getStyle(style);
246                 
247                 if (!m_content)
248                         return 0;
249                 
250                 gPainter &painter = *(gPainter*)data2;
251                 
252                 m_content->cursorSave();
253                 m_content->cursorMove(m_top - m_selected);
254                 
255                 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
256                 {
257                         m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
258                         m_content->cursorMove(+1);
259                 }
260
261                 if ( m_scrollbar && m_scrollbar->isVisible() )
262                 {
263                         painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
264                         painter.clear();
265                         painter.clippop();
266                 }
267
268                 m_content->cursorRestore();
269
270                 gettimeofday(&t2, 0);
271                 t2 -= t;
272                 eDebug("draw %d:%d", t2.tv_sec, t2.tv_usec);
273                 return 0;
274         }
275         case evtChangedSize:
276                 recalcSize();
277                 return eWidget::event(event, data, data2);
278                 
279         case evtAction:
280                 if (isVisible())
281                 {
282                         moveSelection((int)data2);
283                         return 1;
284                 }
285                 return 0;
286         default:
287                 return eWidget::event(event, data, data2);
288         }
289 }
290
291 void eListbox::recalcSize()
292 {
293         m_content_changed=true;
294         m_prev_scrollbar_page=-1;
295         m_content->setSize(eSize(size().width(), m_itemheight));
296         m_items_per_page = size().height() / m_itemheight;
297
298         if (m_items_per_page > 20)
299                 eDebug("eListbox::recalcSize() m_items_per_page %d", m_items_per_page);
300
301         if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
302                 m_items_per_page = 0;
303
304         moveSelection(justCheck);
305 }
306
307 void eListbox::setItemHeight(int h)
308 {
309         if (h)
310                 m_itemheight = h;
311         else
312                 m_itemheight = 20;
313         recalcSize();
314 }
315
316 void eListbox::setSelectionEnable(int en)
317 {
318         if (m_selection_enabled == en)
319                 return;
320         m_selection_enabled = en;
321         entryChanged(m_selected); /* redraw current entry */
322 }
323
324 void eListbox::entryAdded(int index)
325 {
326                 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
327                 
328                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
329         if (index <= m_selected)
330                 ++m_selected;
331         if (index <= m_top)
332                 ++m_top;
333                 
334                 /* we have to check wether our current cursor is gone out of the screen. */
335                 /* moveSelection will check for this case */
336         moveSelection(justCheck);
337         
338                 /* now, check if the new index is visible. */
339         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
340         {
341                         /* todo, calc exact invalidation... */
342                 invalidate();
343         }
344 }
345
346 void eListbox::entryRemoved(int index)
347 {
348         if (index == m_selected)
349                 m_selected = m_content->cursorGet();
350
351         moveSelection(justCheck);
352
353         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
354         {
355                         /* todo, calc exact invalidation... */
356                 invalidate();
357         }
358 }
359
360 void eListbox::entryChanged(int index)
361 {
362         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
363         {
364                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
365                 invalidate(inv);
366         }
367 }
368
369 void eListbox::entryReset(bool cursorHome)
370 {
371         m_content_changed=true;
372         m_prev_scrollbar_page=-1;
373         if ( cursorHome )
374         {
375                 if (m_content)
376                         m_content->cursorHome();
377                 m_top = 0;
378                 m_selected = 0;
379         }
380         moveSelection(justCheck);
381         invalidate();
382 }