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)
14 m_content->cursorHome();
19 void eListbox::moveSelection(int dir)
21 /* we need the old top/sel to see what we have to redraw */
23 int oldsel = m_selected;
25 /* first, move cursor */
29 m_content->cursorMove(-1);
32 m_content->cursorMove(1);
33 /* ok - we could have reached the end. we just go one back then. */
34 if (!m_content->cursorValid())
35 m_content->cursorMove(-1);
38 m_content->cursorHome();
39 m_top = 0; /* align with top, speeds up process */
42 /* move to last existing one ("end" is already invalid) */
43 m_content->cursorEnd(); m_content->cursorMove(-1);
45 m_top = m_content->cursorGet() - m_items_per_page + 1;
51 /* note that we could be on an invalid cursor position, but we don't
52 care. this only happens on empty lists, and should have almost no
55 /* now, look wether the current selection is out of screen */
56 m_selected = m_content->cursorGet();
57 if (m_selected < m_top)
59 m_top -= m_items_per_page;
62 } else if (m_selected >= m_top + m_items_per_page)
64 /* m_top should be always valid here as it's selected */
65 m_top += m_items_per_page;
72 /* redraw the old and newly selected */
73 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
74 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
80 int eListbox::event(int event, void *data, void *data2)
86 ePtr<eWindowStyle> style;
89 recalcSize(); // move to event
96 gPainter &painter = *(gPainter*)data2;
98 m_content->cursorSave();
99 m_content->cursorMove(m_top - m_selected);
101 for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
103 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet());
104 m_content->cursorMove(+1);
107 m_content->cursorRestore();
112 return eWidget::event(event, data, data2);
116 void eListbox::recalcSize()
119 m_content->setSize(eSize(size().width(), m_itemheight));
120 m_items_per_page = size().height() / m_itemheight;