added a sanity check for timer editor (no functionality until now except building...
[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_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::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         if ( m_content )
160         {
161                 m_content->cursorHome();
162                 m_content->cursorMove(index);
163                 moveSelection(justCheck);
164         }
165 }
166
167 int eListbox::getCurrentIndex()
168 {
169         if ( m_content && m_content->cursorValid() )
170                 return m_content->cursorGet();
171         return 0;
172 }
173
174 void eListbox::updateScrollBar()
175 {
176         if (!m_content || m_scrollbar_mode == showNever )
177                 return;
178         int entries = m_content->size();
179         if ( m_content_changed )
180         {
181                 int width = size().width();
182                 int height = size().height();
183                 m_content_changed = false;
184                 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
185                 {
186                         int sbarwidth=width/16;
187                         if ( sbarwidth < 18 )
188                                 sbarwidth=18;
189                         if ( sbarwidth > 22 )
190                                 sbarwidth=22;
191                         m_scrollbar->move(ePoint(width-sbarwidth, 0));
192                         m_scrollbar->resize(eSize(sbarwidth, height));
193                         m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
194                         m_scrollbar->show();
195                 }
196                 else
197                 {
198                         m_content->setSize(eSize(width, m_itemheight));
199                         m_scrollbar->hide();
200                 }
201         }
202         if ( m_items_per_page && entries )
203         {
204                 int curVisiblePage = m_top / m_items_per_page;
205                 if (m_prev_scrollbar_page != curVisiblePage)
206                 {
207                         m_prev_scrollbar_page = curVisiblePage;
208                         int pages = entries / m_items_per_page;
209                         if ( (pages*m_items_per_page) < entries )
210                                 ++pages;
211                         int start=(m_top*100)/(pages*m_items_per_page);
212                         int vis=(m_items_per_page*100)/(pages*m_items_per_page);
213                         if (vis < 3)
214                                 vis=3;
215                         m_scrollbar->setStartEnd(start,start+vis);
216                 }
217         }
218 }
219
220 int eListbox::event(int event, void *data, void *data2)
221 {
222         switch (event)
223         {
224         case evtPaint:
225         {
226                 ePtr<eWindowStyle> style;
227                 
228                 if (!m_content)
229                         return eWidget::event(event, data, data2);
230                 assert(m_content);
231                 
232                 getStyle(style);
233                 
234                 if (!m_content)
235                         return 0;
236                 
237                 gPainter &painter = *(gPainter*)data2;
238                 
239                 m_content->cursorSave();
240                 m_content->cursorMove(m_top - m_selected);
241                 
242                 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
243                 {
244                         m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
245                         m_content->cursorMove(+1);
246                 }
247
248                 if ( m_scrollbar && m_scrollbar->isVisible() )
249                 {
250                         painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
251                         painter.clear();
252                         painter.clippop();
253                 }
254
255                 m_content->cursorRestore();
256
257                 return 0;
258         }
259         case evtChangedSize:
260                 recalcSize();
261                 return eWidget::event(event, data, data2);
262                 
263         case evtAction:
264                 if (isVisible())
265                 {
266                         moveSelection((int)data2);
267                         return 1;
268                 }
269                 return 0;
270         default:
271                 return eWidget::event(event, data, data2);
272         }
273 }
274
275 void eListbox::recalcSize()
276 {
277         m_content_changed=true;
278         m_prev_scrollbar_page=-1;
279         m_content->setSize(eSize(size().width(), m_itemheight));
280         m_items_per_page = size().height() / m_itemheight;
281
282         if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
283                 m_items_per_page = 0;
284
285         moveSelection(justCheck);
286 }
287
288 void eListbox::setItemHeight(int h)
289 {
290         if (h)
291                 m_itemheight = h;
292         else
293                 m_itemheight = 20;
294         recalcSize();
295 }
296
297 void eListbox::setSelectionEnable(int en)
298 {
299         if (m_selection_enabled == en)
300                 return;
301         m_selection_enabled = en;
302         entryChanged(m_selected); /* redraw current entry */
303 }
304
305 void eListbox::entryAdded(int index)
306 {
307                 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
308                 
309                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
310         if (index <= m_selected)
311                 ++m_selected;
312         if (index <= m_top)
313                 ++m_top;
314                 
315                 /* we have to check wether our current cursor is gone out of the screen. */
316                 /* moveSelection will check for this case */
317         moveSelection(justCheck);
318         
319                 /* now, check if the new index is visible. */
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::entryRemoved(int index)
328 {
329         if (index == m_selected)
330                 m_selected = m_content->cursorGet();
331
332         moveSelection(justCheck);
333
334         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
335         {
336                         /* todo, calc exact invalidation... */
337                 invalidate();
338         }
339 }
340
341 void eListbox::entryChanged(int index)
342 {
343         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
344         {
345                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
346                 invalidate(inv);
347         }
348 }
349
350 void eListbox::entryReset(bool cursorHome)
351 {
352         m_content_changed=true;
353         m_prev_scrollbar_page=-1;
354         if ( cursorHome )
355         {
356                 if (m_content)
357                         m_content->cursorHome();
358                 m_top = 0;
359                 m_selected = 0;
360         }
361         moveSelection(justCheck);
362         invalidate();
363 }