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