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)
15 void eListbox::moveSelection(int dir)
17 /* we need the old top/sel to see what we have to redraw */
19 int oldsel = m_selected;
21 /* first, move cursor */
25 m_content->cursorMove(-1);
28 m_content->cursorMove(1);
29 /* ok - we could have reached the end. we just go one back then. */
30 if (!m_content->cursorValid())
31 m_content->cursorMove(-1);
34 m_content->cursorHome();
35 m_top = 0; /* align with top, speeds up process */
38 /* move to last existing one ("end" is already invalid) */
39 m_content->cursorEnd(); m_content->cursorMove(-1);
41 m_top = m_content->cursorGet() - m_items_per_page + 1;
49 /* note that we could be on an invalid cursor position, but we don't
50 care. this only happens on empty lists, and should have almost no
53 /* now, look wether the current selection is out of screen */
54 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;
69 else if (m_selected != oldsel)
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;
123 void eListbox::entryAdded(int index)
125 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
127 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
128 if (index <= m_selected)
133 /* we have to check wether our current cursor is gone out of the screen. */
134 /* moveSelection will check for this case */
135 moveSelection(justCheck);
137 /* now, check if the new index is visible. */
138 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
140 /* todo, calc exact invalidation... */
145 void eListbox::entryRemoved(int index)
147 if (index == m_selected)
148 m_selected = m_content->cursorGet();
150 moveSelection(justCheck);
152 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
154 /* todo, calc exact invalidation... */
159 void eListbox::entryChanged(int index)
161 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
163 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
168 void eListbox::entryReset()
172 m_content->cursorHome();