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_prev_scrollbar_page(-1), m_content_changed(false)
8 , m_scrollbar_mode(showNever), m_scrollbar(NULL)
10 setContent(new eListboxStringContent());
13 eActionMap::getInstance(ptr);
16 m_selection_enabled = 1;
18 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)
159 m_content->cursorHome();
160 m_content->cursorMove(index);
161 moveSelection(justCheck);
164 void eListbox::updateScrollBar()
166 if (!m_content || m_scrollbar_mode == showNever )
168 int entries = m_content->size();
169 if ( m_content_changed )
171 int width = size().width();
172 int height = size().height();
173 m_content_changed = false;
174 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
176 int sbarwidth=width/16;
177 if ( sbarwidth < 18 )
179 if ( sbarwidth > 22 )
181 m_scrollbar->move(ePoint(width-sbarwidth, 0));
182 m_scrollbar->resize(eSize(sbarwidth, height));
183 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
188 m_content->setSize(eSize(width, m_itemheight));
192 if ( m_items_per_page && entries )
194 int curVisiblePage = m_top / m_items_per_page;
195 if (m_prev_scrollbar_page != curVisiblePage)
197 m_prev_scrollbar_page = curVisiblePage;
198 int pages = entries / m_items_per_page;
199 if ( (pages*m_items_per_page) < entries )
201 int start=(m_top*100)/(pages*m_items_per_page);
202 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
205 m_scrollbar->setStartEnd(start,start+vis);
210 int eListbox::event(int event, void *data, void *data2)
216 ePtr<eWindowStyle> style;
219 return eWidget::event(event, data, data2);
227 gPainter &painter = *(gPainter*)data2;
229 m_content->cursorSave();
230 m_content->cursorMove(m_top - m_selected);
232 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
234 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
235 m_content->cursorMove(+1);
238 if ( m_scrollbar && m_scrollbar->isVisible() )
240 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
245 m_content->cursorRestore();
251 return eWidget::event(event, data, data2);
256 moveSelection((int)data2);
261 return eWidget::event(event, data, data2);
265 void eListbox::recalcSize()
267 m_content_changed=true;
268 m_prev_scrollbar_page=-1;
269 m_content->setSize(eSize(size().width(), m_itemheight));
270 m_items_per_page = size().height() / m_itemheight;
274 void eListbox::setItemHeight(int h)
283 void eListbox::setSelectionEnable(int en)
285 if (m_selection_enabled == en)
287 m_selection_enabled = en;
288 entryChanged(m_selected); /* redraw current entry */
291 void eListbox::entryAdded(int index)
293 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
295 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
296 if (index <= m_selected)
301 /* we have to check wether our current cursor is gone out of the screen. */
302 /* moveSelection will check for this case */
303 moveSelection(justCheck);
305 /* now, check if the new index is visible. */
306 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
308 /* todo, calc exact invalidation... */
313 void eListbox::entryRemoved(int index)
315 if (index == m_selected)
316 m_selected = m_content->cursorGet();
318 moveSelection(justCheck);
320 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
322 /* todo, calc exact invalidation... */
327 void eListbox::entryChanged(int index)
329 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
331 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
336 void eListbox::entryReset()
338 m_content_changed=true;
339 m_prev_scrollbar_page=-1;
341 m_content->cursorHome();
344 moveSelection(justCheck);