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