1 #include <lib/gui/elistbox.h>
2 #include <lib/gui/elistboxcontent.h>
3 #include <lib/gui/eslider.h>
4 #include <lib/actions/action.h>
6 eListbox::eListbox(eWidget *parent) :
7 eWidget(parent), m_scrollbar_mode(showNever), m_prev_scrollbar_page(-1),
8 m_content_changed(false), m_enabled_wrap_around(false), m_top(0), m_selected(0), m_itemheight(25),
9 m_items_per_page(0), m_selection_enabled(1), m_scrollbar(NULL)
11 // setContent(new eListboxStringContent());
14 eActionMap::getInstance(ptr);
15 ptr->bindAction("ListboxActions", 0, 0, this);
24 eActionMap::getInstance(ptr);
25 ptr->unbindAction(this, 0);
28 void eListbox::setScrollbarMode(int mode)
30 m_scrollbar_mode = mode;
33 if (m_scrollbar_mode == showNever)
41 m_scrollbar = new eSlider(this);
43 m_scrollbar->setBorderWidth(1);
44 m_scrollbar->setOrientation(eSlider::orVertical);
45 m_scrollbar->setRange(0,100);
49 void eListbox::setWrapAround(bool state)
51 m_enabled_wrap_around = state;
54 void eListbox::setContent(iListboxContent *content)
56 int oldsel = m_selected;
59 m_content->setListbox(this);
61 if (oldsel == m_selected)
62 /* emit */ selectionChanged();
65 bool eListbox::atBegin()
67 if (m_content && !m_selected)
72 bool eListbox::atEnd()
74 if (m_content && m_content->size() == m_selected+1)
79 void eListbox::moveToEnd()
81 /* move to last existing one ("end" is already invalid) */
82 m_content->cursorEnd(); m_content->cursorMove(-1);
83 /* current selection invisible? */
84 if (m_top + m_items_per_page <= m_content->cursorGet())
86 int rest = m_content->size() % m_items_per_page;
88 m_top = m_content->cursorGet() - rest + 1;
90 m_top = m_content->cursorGet() - m_items_per_page + 1;
96 void eListbox::moveSelection(int dir)
98 /* refuse to do anything without a valid list. */
101 /* if our list does not have one entry, don't do anything. */
102 if (!m_items_per_page)
104 /* we need the old top/sel to see what we have to redraw */
106 int oldsel = m_selected;
107 /* first, move cursor */
112 m_content->cursorMove(-1);
113 if (m_enabled_wrap_around && oldsel == m_content->cursorGet()) // must wrap around ?
118 m_content->cursorMove(1);
119 /* ok - we could have reached the end. So we do wrap around. */
120 if (!m_content->cursorValid())
122 if (m_enabled_wrap_around)
125 m_content->cursorHome();
128 m_content->cursorMove(-1);
132 if (m_content->cursorGet() >= m_items_per_page)
134 m_content->cursorMove(-m_items_per_page);
135 m_top -= m_items_per_page;
141 m_content->cursorHome();
145 m_content->cursorHome();
146 m_top = 0; /* align with top, speeds up process */
149 m_content->cursorMove(m_items_per_page);
150 if (m_content->cursorValid())
160 if (m_content->cursorValid() && !m_content->currentCursorSelectable())
162 /* ok, our cursor position is valid (i.e. in list), but not selectable. */
164 /* when moving up, continue until we found a valid position. */
165 if ((dir == moveUp) || (dir == pageDown))
167 while (m_content->cursorGet())
169 m_content->cursorMove(-1);
170 if (m_content->currentCursorSelectable())
178 while (m_content->cursorValid())
180 m_content->cursorMove(+1);
181 if (m_content->currentCursorSelectable())
187 if (!m_content->cursorValid())
188 m_content->cursorMove(-1);
191 if (!m_content->currentCursorSelectable())
192 m_content->cursorSet(oldsel);
195 /* note that we could be on an invalid cursor position, but we don't
196 care. this only happens on empty lists, and should have almost no
199 /* now, look wether the current selection is out of screen */
200 m_selected = m_content->cursorGet();
201 while (m_selected < m_top)
203 m_top -= m_items_per_page;
207 while (m_selected >= m_top + m_items_per_page)
208 /* m_top should be always valid here as it's selected */
209 m_top += m_items_per_page;
211 if (oldsel != m_selected)
212 /* emit */ selectionChanged();
218 else if (m_selected != oldsel)
220 /* redraw the old and newly selected */
221 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
222 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
228 void eListbox::moveSelectionTo(int index)
232 m_content->cursorHome();
233 m_content->cursorMove(index);
234 moveSelection(justCheck);
238 int eListbox::getCurrentIndex()
240 if (m_content && m_content->cursorValid())
241 return m_content->cursorGet();
245 void eListbox::updateScrollBar()
247 if (!m_content || m_scrollbar_mode == showNever )
249 int entries = m_content->size();
250 if (m_content_changed)
252 int width = size().width();
253 int height = size().height();
254 m_content_changed = false;
255 if (entries > m_items_per_page || m_scrollbar_mode == showAlways)
257 int sbarwidth=width/16;
262 m_scrollbar->move(ePoint(width-sbarwidth, 0));
263 m_scrollbar->resize(eSize(sbarwidth, height));
264 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
269 m_content->setSize(eSize(width, m_itemheight));
273 if (m_items_per_page && entries)
275 int curVisiblePage = m_top / m_items_per_page;
276 if (m_prev_scrollbar_page != curVisiblePage)
278 m_prev_scrollbar_page = curVisiblePage;
279 int pages = entries / m_items_per_page;
280 if ((pages*m_items_per_page) < entries)
282 int start=(m_top*100)/(pages*m_items_per_page);
283 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
286 m_scrollbar->setStartEnd(start,start+vis);
291 int eListbox::event(int event, void *data, void *data2)
297 ePtr<eWindowStyle> style;
300 return eWidget::event(event, data, data2);
308 gPainter &painter = *(gPainter*)data2;
310 m_content->cursorSave();
311 m_content->cursorMove(m_top - m_selected);
313 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
315 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
316 m_content->cursorMove(+1);
319 if (m_scrollbar && m_scrollbar->isVisible())
321 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
326 m_content->cursorRestore();
332 return eWidget::event(event, data, data2);
337 moveSelection((int)data2);
342 return eWidget::event(event, data, data2);
346 void eListbox::recalcSize()
348 m_content_changed=true;
349 m_prev_scrollbar_page=-1;
350 m_content->setSize(eSize(size().width(), m_itemheight));
351 m_items_per_page = size().height() / m_itemheight;
353 if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
354 m_items_per_page = 0;
356 moveSelection(justCheck);
359 void eListbox::setItemHeight(int h)
368 void eListbox::setSelectionEnable(int en)
370 if (m_selection_enabled == en)
372 m_selection_enabled = en;
373 entryChanged(m_selected); /* redraw current entry */
376 void eListbox::entryAdded(int index)
378 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
380 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
381 if (index <= m_selected)
386 /* we have to check wether our current cursor is gone out of the screen. */
387 /* moveSelection will check for this case */
388 moveSelection(justCheck);
390 /* now, check if the new index is visible. */
391 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
393 /* todo, calc exact invalidation... */
398 void eListbox::entryRemoved(int index)
400 if (index == m_selected)
401 m_selected = m_content->cursorGet();
403 moveSelection(justCheck);
405 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
407 /* todo, calc exact invalidation... */
412 void eListbox::entryChanged(int index)
414 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
416 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
421 void eListbox::entryReset(bool selectionHome)
423 m_content_changed = true;
424 m_prev_scrollbar_page = -1;
429 m_content->cursorHome();
434 if (m_content && (m_selected >= m_content->size()))
436 if (m_content->size())
437 m_selected = m_content->size() - 1;
440 m_content->cursorSet(m_selected);
444 moveSelection(justCheck);