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);
12 ptr->bindAction("ListboxActions", 0, 0, this);
18 eActionMap::getInstance(ptr);
19 ptr->unbindAction(this, 0);
22 void eListbox::setContent(iListboxContent *content)
26 m_content->setListbox(this);
30 void eListbox::moveSelection(int dir)
32 /* refuse to do anything without a valid list. */
36 /* we need the old top/sel to see what we have to redraw */
38 int oldsel = m_selected;
40 /* first, move cursor */
44 m_content->cursorMove(-1);
47 m_content->cursorMove(1);
48 /* ok - we could have reached the end. we just go one back then. */
49 if (!m_content->cursorValid())
50 m_content->cursorMove(-1);
53 m_content->cursorHome();
54 m_top = 0; /* align with top, speeds up process */
57 /* move to last existing one ("end" is already invalid) */
58 m_content->cursorEnd(); m_content->cursorMove(-1);
60 m_top = m_content->cursorGet() - m_items_per_page + 1;
68 /* note that we could be on an invalid cursor position, but we don't
69 care. this only happens on empty lists, and should have almost no
72 /* now, look wether the current selection is out of screen */
73 m_selected = m_content->cursorGet();
75 if (m_selected < m_top)
77 m_top -= m_items_per_page;
80 } else if (m_selected >= m_top + m_items_per_page)
82 /* m_top should be always valid here as it's selected */
83 m_top += m_items_per_page;
88 else if (m_selected != oldsel)
91 /* redraw the old and newly selected */
92 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
93 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
99 int eListbox::event(int event, void *data, void *data2)
105 ePtr<eWindowStyle> style;
108 return eWidget::event(event, data, data2);
116 gPainter &painter = *(gPainter*)data2;
118 m_content->cursorSave();
119 m_content->cursorMove(m_top - m_selected);
121 for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
123 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet());
124 m_content->cursorMove(+1);
127 m_content->cursorRestore();
133 return eWidget::event(event, data, data2);
138 moveSelection((int)data2);
143 return eWidget::event(event, data, data2);
147 void eListbox::recalcSize()
150 m_content->setSize(eSize(size().width(), m_itemheight));
151 m_items_per_page = size().height() / m_itemheight;
154 void eListbox::entryAdded(int index)
156 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
158 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
159 if (index <= m_selected)
164 /* we have to check wether our current cursor is gone out of the screen. */
165 /* moveSelection will check for this case */
166 moveSelection(justCheck);
168 /* now, check if the new index is visible. */
169 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
171 /* todo, calc exact invalidation... */
176 void eListbox::entryRemoved(int index)
178 if (index == m_selected)
179 m_selected = m_content->cursorGet();
181 moveSelection(justCheck);
183 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
185 /* todo, calc exact invalidation... */
190 void eListbox::entryChanged(int index)
192 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
194 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
199 void eListbox::entryReset()
202 m_content->cursorHome();