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