1 #include <lib/gui/elistbox.h>
2 #include <lib/gui/elistboxcontent.h>
4 eListbox::eListbox(eWidget *parent): eWidget(parent)
6 setContent(new eListboxStringContent());
9 void eListbox::setContent(iListboxContent *content)
13 m_content->cursorHome();
18 void eListbox::moveSelection(int dir)
20 /* we need the old top/sel to see what we have to redraw */
22 int oldsel = m_selected;
24 /* first, move cursor */
28 m_content->cursorMove(-1);
31 m_content->cursorMove(1);
32 /* ok - we could have reached the end. we just go one back then. */
33 if (!m_content->cursorValid())
34 m_content->cursorMove(-1);
37 m_content->cursorHome();
38 m_top = 0; /* align with top, speeds up process */
41 /* move to last existing one ("end" is already invalid) */
42 m_content->cursorEnd(); m_content->cursorMove(-1);
44 m_top = m_content->cursorGet() - m_items_per_page + 1;
50 /* note that we could be on an invalid cursor position, but we don't
51 care. this only happens on empty lists, and should have almost no
54 /* now, look wether the current selection is out of screen */
55 m_selected = m_content->cursorGet();
56 if (m_selected < m_top)
58 m_top -= m_items_per_page;
61 } else if (m_selected >= m_top + m_items_per_page)
63 /* m_top should be always valid here as it's selected */
64 m_top += m_items_per_page;
71 /* redraw the old and newly selected */
72 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
73 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
79 int eListbox::event(int event, void *data, void *data2)
85 ePtr<eWindowStyle> style;
88 recalcSize(); // move to event
95 gPainter &painter = *(gPainter*)data2;
97 m_content->cursorSave();
98 m_content->cursorMove(m_top - m_selected);
100 for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
102 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet());
103 m_content->cursorMove(+1);
106 m_content->cursorRestore();
111 return eWidget::event(event, data, data2);
115 void eListbox::recalcSize()
118 m_content->setSize(eSize(size().width(), m_itemheight));
119 m_items_per_page = size().height() / m_itemheight;