1 #include <lib/gui/elistbox.h>
2 #include <lib/gui/elistboxcontent.h>
3 #include <lib/actions/action.h>
5 eListbox::eListbox(eWidget *parent): eWidget(parent)
7 setContent(new eListboxStringContent());
10 eActionMap::getInstance(ptr);
14 ptr->bindAction("ListboxActions", 0, 0, this);
20 eActionMap::getInstance(ptr);
21 ptr->unbindAction(this, 0);
24 void eListbox::setContent(iListboxContent *content)
28 m_content->setListbox(this);
32 void eListbox::moveSelection(int dir)
34 /* refuse to do anything without a valid list. */
38 /* we need the old top/sel to see what we have to redraw */
40 int oldsel = m_selected;
42 /* first, move cursor */
46 m_content->cursorMove(-1);
49 m_content->cursorMove(1);
50 /* ok - we could have reached the end. we just go one back then. */
51 if (!m_content->cursorValid())
52 m_content->cursorMove(-1);
55 if (m_content->cursorGet() >= m_items_per_page)
57 m_content->cursorMove(-m_items_per_page);
58 m_top -= m_items_per_page;
64 m_content->cursorHome();
68 m_content->cursorHome();
69 m_top = 0; /* align with top, speeds up process */
73 m_content->cursorMove(m_items_per_page);
74 if (m_content->cursorValid())
78 /* move to last existing one ("end" is already invalid) */
79 m_content->cursorEnd(); m_content->cursorMove(-1);
80 /* current selection invisible? */
81 if (m_top + m_items_per_page <= m_content->cursorGet())
83 m_top = m_content->cursorGet() - m_items_per_page + 1;
92 /* note that we could be on an invalid cursor position, but we don't
93 care. this only happens on empty lists, and should have almost no
96 /* now, look wether the current selection is out of screen */
97 m_selected = m_content->cursorGet();
99 if (m_selected < m_top)
101 m_top -= m_items_per_page;
104 } else if (m_selected >= m_top + m_items_per_page)
106 /* m_top should be always valid here as it's selected */
107 m_top += m_items_per_page;
112 else if (m_selected != oldsel)
115 /* redraw the old and newly selected */
116 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
117 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
123 int eListbox::event(int event, void *data, void *data2)
129 ePtr<eWindowStyle> style;
132 return eWidget::event(event, data, data2);
140 gPainter &painter = *(gPainter*)data2;
142 m_content->cursorSave();
143 m_content->cursorMove(m_top - m_selected);
145 for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
147 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size());
148 m_content->cursorMove(+1);
151 m_content->cursorRestore();
157 return eWidget::event(event, data, data2);
162 moveSelection((int)data2);
167 return eWidget::event(event, data, data2);
171 void eListbox::recalcSize()
173 m_content->setSize(eSize(size().width(), m_itemheight));
174 m_items_per_page = size().height() / m_itemheight;
177 void eListbox::setItemHeight(int h)
186 void eListbox::entryAdded(int index)
188 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
190 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
191 if (index <= m_selected)
196 /* we have to check wether our current cursor is gone out of the screen. */
197 /* moveSelection will check for this case */
198 moveSelection(justCheck);
200 /* now, check if the new index is visible. */
201 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
203 /* todo, calc exact invalidation... */
208 void eListbox::entryRemoved(int index)
210 if (index == m_selected)
211 m_selected = m_content->cursorGet();
213 moveSelection(justCheck);
215 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
217 /* todo, calc exact invalidation... */
222 void eListbox::entryChanged(int index)
224 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
226 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
231 void eListbox::entryReset()
234 m_content->cursorHome();