1 /* written by: Felix Domke <tmbinc@elitedvb.net> */
2 #include <lib/gui/elistbox.h>
3 #include <lib/gui/elistboxcontent.h>
5 eListbox::eListbox(eWidget *parent): eWidget(parent)
7 setContent(new eListboxStringContent());
10 void eListbox::setContent(iListboxContent *content)
16 void eListbox::moveSelection(int dir)
18 /* we need the old top/sel to see what we have to redraw */
20 int oldsel = m_selected;
22 /* first, move cursor */
26 m_content->cursorMove(-1);
29 m_content->cursorMove(1);
30 /* ok - we could have reached the end. we just go one back then. */
31 if (!m_content->cursorValid())
32 m_content->cursorMove(-1);
35 m_content->cursorHome();
36 m_top = 0; /* align with top, speeds up process */
39 /* move to last existing one ("end" is already invalid) */
40 m_content->cursorEnd(); m_content->cursorMove(-1);
42 m_top = m_content->cursorGet() - m_items_per_page + 1;
50 /* note that we could be on an invalid cursor position, but we don't
51 care. this only happens on empty lists, and should have almost no
54 /* now, look wether the current selection is out of screen */
55 m_selected = m_content->cursorGet();
57 if (m_selected < m_top)
59 m_top -= m_items_per_page;
62 } else if (m_selected >= m_top + m_items_per_page)
64 /* m_top should be always valid here as it's selected */
65 m_top += m_items_per_page;
70 else if (m_selected != oldsel)
73 /* redraw the old and newly selected */
74 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
75 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
81 int eListbox::event(int event, void *data, void *data2)
87 ePtr<eWindowStyle> style;
90 recalcSize(); // move to event
97 gPainter &painter = *(gPainter*)data2;
99 m_content->cursorSave();
100 m_content->cursorMove(m_top - m_selected);
102 for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
104 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet());
105 m_content->cursorMove(+1);
108 m_content->cursorRestore();
113 return eWidget::event(event, data, data2);
117 void eListbox::recalcSize()
120 m_content->setSize(eSize(size().width(), m_itemheight));
121 m_items_per_page = size().height() / m_itemheight;
124 void eListbox::entryAdded(int index)
126 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
128 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
129 if (index <= m_selected)
134 /* we have to check wether our current cursor is gone out of the screen. */
135 /* moveSelection will check for this case */
136 moveSelection(justCheck);
138 /* now, check if the new index is visible. */
139 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
141 /* todo, calc exact invalidation... */
146 void eListbox::entryRemoved(int index)
148 if (index == m_selected)
149 m_selected = m_content->cursorGet();
151 moveSelection(justCheck);
153 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
155 /* todo, calc exact invalidation... */
160 void eListbox::entryChanged(int index)
162 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
164 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
169 void eListbox::entryReset()
173 m_content->cursorHome();