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);
110 recalcSize(); // move to event
117 gPainter &painter = *(gPainter*)data2;
119 m_content->cursorSave();
120 m_content->cursorMove(m_top - m_selected);
122 for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
124 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet());
125 m_content->cursorMove(+1);
128 m_content->cursorRestore();
135 moveSelection((int)data2);
140 return eWidget::event(event, data, data2);
144 void eListbox::recalcSize()
147 m_content->setSize(eSize(size().width(), m_itemheight));
148 m_items_per_page = size().height() / m_itemheight;
151 void eListbox::entryAdded(int index)
153 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
155 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
156 if (index <= m_selected)
161 /* we have to check wether our current cursor is gone out of the screen. */
162 /* moveSelection will check for this case */
163 moveSelection(justCheck);
165 /* now, check if the new index is visible. */
166 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
168 /* todo, calc exact invalidation... */
173 void eListbox::entryRemoved(int index)
175 if (index == m_selected)
176 m_selected = m_content->cursorGet();
178 moveSelection(justCheck);
180 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
182 /* todo, calc exact invalidation... */
187 void eListbox::entryChanged(int index)
189 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
191 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
196 void eListbox::entryReset()
199 m_content->cursorHome();