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