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 m_content->cursorHome();
56 m_top = 0; /* align with top, speeds up process */
59 /* move to last existing one ("end" is already invalid) */
60 m_content->cursorEnd(); m_content->cursorMove(-1);
62 m_top = m_content->cursorGet() - m_items_per_page + 1;
70 /* note that we could be on an invalid cursor position, but we don't
71 care. this only happens on empty lists, and should have almost no
74 /* now, look wether the current selection is out of screen */
75 m_selected = m_content->cursorGet();
77 if (m_selected < m_top)
79 m_top -= m_items_per_page;
82 } else if (m_selected >= m_top + m_items_per_page)
84 /* m_top should be always valid here as it's selected */
85 m_top += m_items_per_page;
90 else if (m_selected != oldsel)
93 /* redraw the old and newly selected */
94 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
95 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
101 int eListbox::event(int event, void *data, void *data2)
107 ePtr<eWindowStyle> style;
110 return eWidget::event(event, data, data2);
118 gPainter &painter = *(gPainter*)data2;
120 m_content->cursorSave();
121 m_content->cursorMove(m_top - m_selected);
123 for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
125 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet());
126 m_content->cursorMove(+1);
129 m_content->cursorRestore();
135 return eWidget::event(event, data, data2);
140 moveSelection((int)data2);
145 return eWidget::event(event, data, data2);
149 void eListbox::recalcSize()
151 m_content->setSize(eSize(size().width(), m_itemheight));
152 m_items_per_page = size().height() / m_itemheight;
155 void eListbox::setItemHeight(int h)
164 void eListbox::entryAdded(int index)
166 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
168 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
169 if (index <= m_selected)
174 /* we have to check wether our current cursor is gone out of the screen. */
175 /* moveSelection will check for this case */
176 moveSelection(justCheck);
178 /* now, check if the new index is visible. */
179 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
181 /* todo, calc exact invalidation... */
186 void eListbox::entryRemoved(int index)
188 if (index == m_selected)
189 m_selected = m_content->cursorGet();
191 moveSelection(justCheck);
193 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
195 /* todo, calc exact invalidation... */
200 void eListbox::entryChanged(int index)
202 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
204 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
209 void eListbox::entryReset()
212 m_content->cursorHome();