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);
27 eActionMap::getInstance(ptr);
28 ptr->unbindAction(this, 0);
31 void eListbox::setScrollbarMode(int mode)
33 m_scrollbar_mode = mode;
36 if ( m_scrollbar_mode == showNever )
44 m_scrollbar = new eSlider(this);
46 m_scrollbar->setBorderWidth(1);
47 m_scrollbar->setOrientation(eSlider::orVertical);
48 m_scrollbar->setRange(0,100);
52 void eListbox::setContent(iListboxContent *content)
56 m_content->setListbox(this);
60 void eListbox::moveSelection(int dir)
62 /* refuse to do anything without a valid list. */
66 /* if our list does not have one entry, don't do anything. */
67 if (!m_items_per_page)
70 /* we need the old top/sel to see what we have to redraw */
72 int oldsel = m_selected;
74 /* first, move cursor */
78 m_content->cursorMove(-1);
81 m_content->cursorMove(1);
82 /* ok - we could have reached the end. we just go one back then. */
83 if (!m_content->cursorValid())
84 m_content->cursorMove(-1);
87 if (m_content->cursorGet() >= m_items_per_page)
89 m_content->cursorMove(-m_items_per_page);
90 m_top -= m_items_per_page;
96 m_content->cursorHome();
100 m_content->cursorHome();
101 m_top = 0; /* align with top, speeds up process */
105 m_content->cursorMove(m_items_per_page);
106 if (m_content->cursorValid())
110 /* move to last existing one ("end" is already invalid) */
111 m_content->cursorEnd(); m_content->cursorMove(-1);
112 /* current selection invisible? */
113 if (m_top + m_items_per_page <= m_content->cursorGet())
115 int rest = m_content->size() % m_items_per_page;
117 m_top = m_content->cursorGet() - rest + 1;
119 m_top = m_content->cursorGet() - m_items_per_page + 1;
128 /* note that we could be on an invalid cursor position, but we don't
129 care. this only happens on empty lists, and should have almost no
132 /* now, look wether the current selection is out of screen */
133 m_selected = m_content->cursorGet();
135 while (m_selected < m_top)
137 m_top -= m_items_per_page;
141 while (m_selected >= m_top + m_items_per_page)
142 /* m_top should be always valid here as it's selected */
143 m_top += m_items_per_page;
149 else if (m_selected != oldsel)
152 /* redraw the old and newly selected */
153 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
154 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
160 void eListbox::moveSelectionTo(int index)
162 m_content->cursorHome();
163 m_content->cursorMove(index);
164 moveSelection(justCheck);
167 void eListbox::updateScrollBar()
169 if (!m_content || m_scrollbar_mode == showNever )
171 int entries = m_content->size();
172 if ( m_content_changed )
174 int width = size().width();
175 int height = size().height();
176 m_content_changed = false;
177 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
179 int sbarwidth=width/16;
180 if ( sbarwidth < 18 )
182 if ( sbarwidth > 22 )
184 m_scrollbar->move(ePoint(width-sbarwidth, 0));
185 m_scrollbar->resize(eSize(sbarwidth, height));
186 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
191 m_content->setSize(eSize(width, m_itemheight));
195 if ( m_items_per_page && entries )
197 int curVisiblePage = m_top / m_items_per_page;
198 if (m_prev_scrollbar_page != curVisiblePage)
200 m_prev_scrollbar_page = curVisiblePage;
201 int pages = entries / m_items_per_page;
202 if ( (pages*m_items_per_page) < entries )
204 int start=(m_top*100)/(pages*m_items_per_page);
205 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
208 m_scrollbar->setStartEnd(start,start+vis);
213 int eListbox::event(int event, void *data, void *data2)
219 ePtr<eWindowStyle> style;
222 return eWidget::event(event, data, data2);
230 gPainter &painter = *(gPainter*)data2;
232 m_content->cursorSave();
233 m_content->cursorMove(m_top - m_selected);
235 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
237 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
238 m_content->cursorMove(+1);
241 if ( m_scrollbar && m_scrollbar->isVisible() )
243 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
248 m_content->cursorRestore();
254 return eWidget::event(event, data, data2);
259 moveSelection((int)data2);
264 return eWidget::event(event, data, data2);
268 void eListbox::recalcSize()
270 m_content_changed=true;
271 m_prev_scrollbar_page=-1;
272 m_content->setSize(eSize(size().width(), m_itemheight));
273 m_items_per_page = size().height() / m_itemheight;
274 moveSelection(justCheck);
277 void eListbox::setItemHeight(int h)
286 void eListbox::setSelectionEnable(int en)
288 if (m_selection_enabled == en)
290 m_selection_enabled = en;
291 entryChanged(m_selected); /* redraw current entry */
294 void eListbox::entryAdded(int index)
296 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
298 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
299 if (index <= m_selected)
304 /* we have to check wether our current cursor is gone out of the screen. */
305 /* moveSelection will check for this case */
306 moveSelection(justCheck);
308 /* now, check if the new index is visible. */
309 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
311 /* todo, calc exact invalidation... */
316 void eListbox::entryRemoved(int index)
318 if (index == m_selected)
319 m_selected = m_content->cursorGet();
321 moveSelection(justCheck);
323 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
325 /* todo, calc exact invalidation... */
330 void eListbox::entryChanged(int index)
332 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
334 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
339 void eListbox::entryReset(bool cursorHome)
341 m_content_changed=true;
342 m_prev_scrollbar_page=-1;
346 m_content->cursorHome();
350 moveSelection(justCheck);