listbox: move checkScrollbar to the proper place
[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(NULL), m_scrollbar_mode(showNever)
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                         m_top = m_content->cursorGet() - m_items_per_page + 1;
110                         if (m_top < 0)
111                                 m_top = 0;
112                 }
113                 break;
114         case justCheck:
115                 break;
116         }
117         
118                 /* note that we could be on an invalid cursor position, but we don't
119                    care. this only happens on empty lists, and should have almost no
120                    side effects. */
121         
122                 /* now, look wether the current selection is out of screen */
123         m_selected = m_content->cursorGet();
124
125         while (m_selected < m_top)
126         {
127                 eDebug("%d < %d", m_selected, m_top);
128                 m_top -= m_items_per_page;
129                 if (m_top < 0)
130                         m_top = 0;
131         }
132         
133         while (m_selected >= m_top + m_items_per_page)
134         {
135                 eDebug("%d >= %d + %d", 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
140         if (m_top != oldtop)
141                 invalidate();
142         else if (m_selected != oldsel)
143         {
144                 
145                         /* redraw the old and newly selected */
146                 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
147                 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
148                 
149                 invalidate(inv);
150         }
151
152         if (m_scrollbar_mode != showNever)
153                 updateScrollBar();
154 }
155
156 void eListbox::moveSelectionTo(int index)
157 {
158         m_content->cursorHome();
159         m_content->cursorMove(index);
160         moveSelection(justCheck);
161 }
162
163 void eListbox::updateScrollBar()
164 {
165         int entries = m_content->size();
166         if ( m_content_changed )
167         {
168                 int width = size().width();
169                 int height = size().height();
170                 m_content_changed = false;
171                 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
172                 {
173                         int sbarwidth=width/16;
174                         if ( sbarwidth < 18 )
175                                 sbarwidth=18;
176                         if ( sbarwidth > 22 )
177                                 sbarwidth=22;
178                         m_scrollbar->move(ePoint(width-sbarwidth, 0));
179                         m_scrollbar->resize(eSize(sbarwidth, height));
180                         m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
181                         if ( !m_scrollbar->isVisible() )
182                                 m_scrollbar->show();
183                 }
184                 else if ( m_scrollbar_mode != showAlways )
185                 {
186                         if ( m_scrollbar->isVisible() )
187                         {
188                                 m_content->setSize(eSize(width, m_itemheight));
189                                 m_scrollbar->hide(); // why this hide dont work???
190                         }
191                 }
192         }
193         int curVisiblePage = m_top / m_items_per_page;
194         if ( m_scrollbar->isVisible() &&
195                 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 int eListbox::event(int event, void *data, void *data2)
210 {
211         switch (event)
212         {
213         case evtPaint:
214         {
215                 ePtr<eWindowStyle> style;
216                 
217                 if (!m_content)
218                         return eWidget::event(event, data, data2);
219                 assert(m_content);
220                 
221                 getStyle(style);
222                 
223                 if (!m_content)
224                         return 0;
225                 
226                 gPainter &painter = *(gPainter*)data2;
227                 
228                 m_content->cursorSave();
229                 m_content->cursorMove(m_top - m_selected);
230                 
231                 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
232                 {
233                         m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
234                         m_content->cursorMove(+1);
235                 }
236
237                 if ( m_scrollbar && m_scrollbar->isVisible() )
238                 {
239                         painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
240                         painter.clear();
241                         painter.clippop();
242                 }
243
244                 m_content->cursorRestore();
245
246                 return 0;
247         }
248         case evtChangedSize:
249                 recalcSize();
250                 return eWidget::event(event, data, data2);
251                 
252         case evtAction:
253                 if (isVisible())
254                 {
255                         moveSelection((int)data2);
256                         return 1;
257                 }
258                 return 0;
259         default:
260                 return eWidget::event(event, data, data2);
261         }
262 }
263
264 void eListbox::recalcSize()
265 {
266         m_content_changed=true;
267         m_content->setSize(eSize(size().width(), m_itemheight));
268         m_items_per_page = size().height() / m_itemheight;
269 }
270
271 void eListbox::setItemHeight(int h)
272 {
273         if (h)
274                 m_itemheight = h;
275         else
276                 m_itemheight = 20;
277         recalcSize();
278 }
279
280 void eListbox::setSelectionEnable(int en)
281 {
282         if (m_selection_enabled == en)
283                 return;
284         m_selection_enabled = en;
285         entryChanged(m_selected); /* redraw current entry */
286 }
287
288 void eListbox::entryAdded(int index)
289 {
290                 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
291                 
292                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
293         if (index <= m_selected)
294                 ++m_selected;
295         if (index <= m_top)
296                 ++m_top;
297                 
298                 /* we have to check wether our current cursor is gone out of the screen. */
299                 /* moveSelection will check for this case */
300         moveSelection(justCheck);
301         
302                 /* now, check if the new index is visible. */
303         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
304         {
305                         /* todo, calc exact invalidation... */
306                 invalidate();
307         }
308 }
309
310 void eListbox::entryRemoved(int index)
311 {
312         if (index == m_selected)
313                 m_selected = m_content->cursorGet();
314
315         moveSelection(justCheck);
316         
317         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
318         {
319                         /* todo, calc exact invalidation... */
320                 invalidate();
321         }
322 }
323
324 void eListbox::entryChanged(int index)
325 {
326         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
327         {
328                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
329                 invalidate(inv);
330         }
331 }
332
333 void eListbox::entryReset()
334 {
335         m_content_changed=true;
336         m_prev_scrollbar_page=-1;
337         if (m_content)
338                 m_content->cursorHome();
339         m_top = 0;
340         m_selected = 0;
341         moveSelection(justCheck);
342         invalidate();
343 }