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);
13 m_selection_enabled = 1;
15 ptr->bindAction("ListboxActions", 0, 0, this);
21 eActionMap::getInstance(ptr);
22 ptr->unbindAction(this, 0);
25 void eListbox::setContent(iListboxContent *content)
29 m_content->setListbox(this);
33 void eListbox::moveSelection(int dir)
35 /* refuse to do anything without a valid list. */
39 /* we need the old top/sel to see what we have to redraw */
41 int oldsel = m_selected;
43 /* first, move cursor */
47 m_content->cursorMove(-1);
50 m_content->cursorMove(1);
51 /* ok - we could have reached the end. we just go one back then. */
52 if (!m_content->cursorValid())
53 m_content->cursorMove(-1);
56 if (m_content->cursorGet() >= m_items_per_page)
58 m_content->cursorMove(-m_items_per_page);
59 m_top -= m_items_per_page;
65 m_content->cursorHome();
69 m_content->cursorHome();
70 m_top = 0; /* align with top, speeds up process */
74 m_content->cursorMove(m_items_per_page);
75 if (m_content->cursorValid())
79 /* move to last existing one ("end" is already invalid) */
80 m_content->cursorEnd(); m_content->cursorMove(-1);
81 /* current selection invisible? */
82 if (m_top + m_items_per_page <= m_content->cursorGet())
84 m_top = m_content->cursorGet() - m_items_per_page + 1;
93 /* note that we could be on an invalid cursor position, but we don't
94 care. this only happens on empty lists, and should have almost no
97 /* now, look wether the current selection is out of screen */
98 m_selected = m_content->cursorGet();
100 if (m_selected < m_top)
102 m_top -= m_items_per_page;
105 } else if (m_selected >= m_top + m_items_per_page)
107 /* m_top should be always valid here as it's selected */
108 m_top += m_items_per_page;
113 else if (m_selected != oldsel)
116 /* redraw the old and newly selected */
117 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
118 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
124 void eListbox::moveSelectionTo(int index)
126 printf("Moving to listbox-entry with index %d\n", index);
127 m_content->cursorHome();
128 m_content->cursorMove(index);
129 moveSelection(justCheck);
132 int eListbox::event(int event, void *data, void *data2)
138 ePtr<eWindowStyle> style;
141 return eWidget::event(event, data, data2);
149 gPainter &painter = *(gPainter*)data2;
151 m_content->cursorSave();
152 m_content->cursorMove(m_top - m_selected);
154 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
156 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
157 m_content->cursorMove(+1);
160 m_content->cursorRestore();
166 return eWidget::event(event, data, data2);
171 moveSelection((int)data2);
176 return eWidget::event(event, data, data2);
180 void eListbox::recalcSize()
182 m_content->setSize(eSize(size().width(), m_itemheight));
183 m_items_per_page = size().height() / m_itemheight;
186 void eListbox::setItemHeight(int h)
195 void eListbox::setSelectionEnable(int en)
197 if (m_selection_enabled == en)
199 m_selection_enabled = en;
200 entryChanged(m_selected); /* redraw current entry */
203 void eListbox::entryAdded(int index)
205 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
207 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
208 if (index <= m_selected)
213 /* we have to check wether our current cursor is gone out of the screen. */
214 /* moveSelection will check for this case */
215 moveSelection(justCheck);
217 /* now, check if the new index is visible. */
218 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
220 /* todo, calc exact invalidation... */
225 void eListbox::entryRemoved(int index)
227 if (index == m_selected)
228 m_selected = m_content->cursorGet();
230 moveSelection(justCheck);
232 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
234 /* todo, calc exact invalidation... */
239 void eListbox::entryChanged(int index)
241 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
243 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
248 void eListbox::entryReset()
251 m_content->cursorHome();