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)
28 void eListbox::moveSelection(int dir)
30 /* we need the old top/sel to see what we have to redraw */
32 int oldsel = m_selected;
34 /* first, move cursor */
38 m_content->cursorMove(-1);
41 m_content->cursorMove(1);
42 /* ok - we could have reached the end. we just go one back then. */
43 if (!m_content->cursorValid())
44 m_content->cursorMove(-1);
47 m_content->cursorHome();
48 m_top = 0; /* align with top, speeds up process */
51 /* move to last existing one ("end" is already invalid) */
52 m_content->cursorEnd(); m_content->cursorMove(-1);
54 m_top = m_content->cursorGet() - m_items_per_page + 1;
62 /* note that we could be on an invalid cursor position, but we don't
63 care. this only happens on empty lists, and should have almost no
66 /* now, look wether the current selection is out of screen */
67 m_selected = m_content->cursorGet();
69 if (m_selected < m_top)
71 m_top -= m_items_per_page;
74 } else if (m_selected >= m_top + m_items_per_page)
76 /* m_top should be always valid here as it's selected */
77 m_top += m_items_per_page;
82 else if (m_selected != oldsel)
85 /* redraw the old and newly selected */
86 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
87 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
93 int eListbox::event(int event, void *data, void *data2)
99 ePtr<eWindowStyle> style;
102 recalcSize(); // move to event
109 gPainter &painter = *(gPainter*)data2;
111 m_content->cursorSave();
112 m_content->cursorMove(m_top - m_selected);
114 for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
116 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet());
117 m_content->cursorMove(+1);
120 m_content->cursorRestore();
125 moveSelection((int)data2);
128 return eWidget::event(event, data, data2);
132 void eListbox::recalcSize()
135 m_content->setSize(eSize(size().width(), m_itemheight));
136 m_items_per_page = size().height() / m_itemheight;
139 void eListbox::entryAdded(int index)
141 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
143 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
144 if (index <= m_selected)
149 /* we have to check wether our current cursor is gone out of the screen. */
150 /* moveSelection will check for this case */
151 moveSelection(justCheck);
153 /* now, check if the new index is visible. */
154 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
156 /* todo, calc exact invalidation... */
161 void eListbox::entryRemoved(int index)
163 if (index == m_selected)
164 m_selected = m_content->cursorGet();
166 moveSelection(justCheck);
168 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
170 /* todo, calc exact invalidation... */
175 void eListbox::entryChanged(int index)
177 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
179 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
184 void eListbox::entryReset()
188 m_content->cursorHome();