1 #include <lib/gui/elistbox.h>
2 #include <lib/gui/elistboxcontent.h>
3 #include <lib/gui/eslider.h>
4 #include <lib/actions/action.h>
6 eListbox::eListbox(eWidget *parent)
7 :eWidget(parent), m_scrollbar_mode(showNever), m_prev_scrollbar_page(-1)
8 ,m_content_changed(false), m_top(0), m_selected(0), m_itemheight(25)
9 ,m_items_per_page(0), m_selection_enabled(1), m_scrollbar(NULL)
11 setContent(new eListboxStringContent());
14 eActionMap::getInstance(ptr);
15 ptr->bindAction("ListboxActions", 0, 0, this);
24 eActionMap::getInstance(ptr);
25 ptr->unbindAction(this, 0);
28 void eListbox::setScrollbarMode(int mode)
30 m_scrollbar_mode = mode;
33 if ( m_scrollbar_mode == showNever )
41 m_scrollbar = new eSlider(this);
43 m_scrollbar->setBorderWidth(1);
44 m_scrollbar->setOrientation(eSlider::orVertical);
45 m_scrollbar->setRange(0,100);
49 void eListbox::setContent(iListboxContent *content)
53 m_content->setListbox(this);
57 void eListbox::moveSelection(int dir)
59 /* refuse to do anything without a valid list. */
63 /* if our list does not have one entry, don't do anything. */
64 if (!m_items_per_page)
67 /* we need the old top/sel to see what we have to redraw */
69 int oldsel = m_selected;
71 /* first, move cursor */
75 m_content->cursorMove(-1);
78 m_content->cursorMove(1);
79 /* ok - we could have reached the end. we just go one back then. */
80 if (!m_content->cursorValid())
81 m_content->cursorMove(-1);
84 if (m_content->cursorGet() >= m_items_per_page)
86 m_content->cursorMove(-m_items_per_page);
87 m_top -= m_items_per_page;
93 m_content->cursorHome();
97 m_content->cursorHome();
98 m_top = 0; /* align with top, speeds up process */
102 m_content->cursorMove(m_items_per_page);
103 if (m_content->cursorValid())
107 /* move to last existing one ("end" is already invalid) */
108 m_content->cursorEnd(); m_content->cursorMove(-1);
109 /* current selection invisible? */
110 if (m_top + m_items_per_page <= m_content->cursorGet())
112 int rest = m_content->size() % m_items_per_page;
114 m_top = m_content->cursorGet() - rest + 1;
116 m_top = m_content->cursorGet() - m_items_per_page + 1;
125 /* note that we could be on an invalid cursor position, but we don't
126 care. this only happens on empty lists, and should have almost no
129 /* now, look wether the current selection is out of screen */
130 m_selected = m_content->cursorGet();
132 while (m_selected < m_top)
134 m_top -= m_items_per_page;
138 while (m_selected >= m_top + m_items_per_page)
139 /* m_top should be always valid here as it's selected */
140 m_top += m_items_per_page;
146 else if (m_selected != oldsel)
149 /* redraw the old and newly selected */
150 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
151 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
157 void eListbox::moveSelectionTo(int index)
161 m_content->cursorHome();
162 m_content->cursorMove(index);
163 moveSelection(justCheck);
167 int eListbox::getCurrentIndex()
169 if ( m_content && m_content->cursorValid() )
170 return m_content->cursorGet();
174 void eListbox::updateScrollBar()
176 if (!m_content || m_scrollbar_mode == showNever )
178 int entries = m_content->size();
179 if ( m_content_changed )
181 int width = size().width();
182 int height = size().height();
183 m_content_changed = false;
184 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
186 int sbarwidth=width/16;
187 if ( sbarwidth < 18 )
189 if ( sbarwidth > 22 )
191 m_scrollbar->move(ePoint(width-sbarwidth, 0));
192 m_scrollbar->resize(eSize(sbarwidth, height));
193 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
198 m_content->setSize(eSize(width, m_itemheight));
202 if ( m_items_per_page && entries )
204 int curVisiblePage = m_top / m_items_per_page;
205 if (m_prev_scrollbar_page != curVisiblePage)
207 m_prev_scrollbar_page = curVisiblePage;
208 int pages = entries / m_items_per_page;
209 if ( (pages*m_items_per_page) < entries )
211 int start=(m_top*100)/(pages*m_items_per_page);
212 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
215 m_scrollbar->setStartEnd(start,start+vis);
220 int eListbox::event(int event, void *data, void *data2)
226 ePtr<eWindowStyle> style;
229 return eWidget::event(event, data, data2);
237 gPainter &painter = *(gPainter*)data2;
239 m_content->cursorSave();
240 m_content->cursorMove(m_top - m_selected);
242 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
244 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
245 m_content->cursorMove(+1);
248 if ( m_scrollbar && m_scrollbar->isVisible() )
250 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
255 m_content->cursorRestore();
261 return eWidget::event(event, data, data2);
266 moveSelection((int)data2);
271 return eWidget::event(event, data, data2);
275 void eListbox::recalcSize()
277 m_content_changed=true;
278 m_prev_scrollbar_page=-1;
279 m_content->setSize(eSize(size().width(), m_itemheight));
280 m_items_per_page = size().height() / m_itemheight;
282 if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
283 m_items_per_page = 0;
285 moveSelection(justCheck);
288 void eListbox::setItemHeight(int h)
297 void eListbox::setSelectionEnable(int en)
299 if (m_selection_enabled == en)
301 m_selection_enabled = en;
302 entryChanged(m_selected); /* redraw current entry */
305 void eListbox::entryAdded(int index)
307 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
309 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
310 if (index <= m_selected)
315 /* we have to check wether our current cursor is gone out of the screen. */
316 /* moveSelection will check for this case */
317 moveSelection(justCheck);
319 /* now, check if the new index is visible. */
320 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
322 /* todo, calc exact invalidation... */
327 void eListbox::entryRemoved(int index)
329 if (index == m_selected)
330 m_selected = m_content->cursorGet();
332 moveSelection(justCheck);
334 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
336 /* todo, calc exact invalidation... */
341 void eListbox::entryChanged(int index)
343 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
345 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
350 void eListbox::entryReset(bool cursorHome)
352 m_content_changed=true;
353 m_prev_scrollbar_page=-1;
357 m_content->cursorHome();
361 moveSelection(justCheck);