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 int m_scrollbar_mode, m_prev_scrollbar_page;
7 bool m_content_changed;
12 int m_selection_enabled;
13 ePtr<iListboxContent> m_content;
16 eListbox::eListbox(eWidget *parent)
17 :eWidget(parent), m_scrollbar_mode(showNever), m_prev_scrollbar_page(-1)
18 ,m_content_changed(false), m_top(0), m_selected(0), m_itemheight(25)
19 ,m_items_per_page(0), m_selection_enabled(1), m_scrollbar(NULL)
21 setContent(new eListboxStringContent());
24 eActionMap::getInstance(ptr);
25 ptr->bindAction("ListboxActions", 0, 0, this);
34 eActionMap::getInstance(ptr);
35 ptr->unbindAction(this, 0);
38 void eListbox::setScrollbarMode(int mode)
40 m_scrollbar_mode = mode;
43 if ( m_scrollbar_mode == showNever )
51 m_scrollbar = new eSlider(this);
53 m_scrollbar->setBorderWidth(1);
54 m_scrollbar->setOrientation(eSlider::orVertical);
55 m_scrollbar->setRange(0,100);
59 void eListbox::setContent(iListboxContent *content)
63 m_content->setListbox(this);
67 void eListbox::moveSelection(int dir)
69 /* refuse to do anything without a valid list. */
73 /* if our list does not have one entry, don't do anything. */
74 if (!m_items_per_page)
77 /* we need the old top/sel to see what we have to redraw */
79 int oldsel = m_selected;
81 /* first, move cursor */
85 m_content->cursorMove(-1);
88 m_content->cursorMove(1);
89 /* ok - we could have reached the end. we just go one back then. */
90 if (!m_content->cursorValid())
91 m_content->cursorMove(-1);
94 if (m_content->cursorGet() >= m_items_per_page)
96 m_content->cursorMove(-m_items_per_page);
97 m_top -= m_items_per_page;
103 m_content->cursorHome();
107 m_content->cursorHome();
108 m_top = 0; /* align with top, speeds up process */
112 m_content->cursorMove(m_items_per_page);
113 if (m_content->cursorValid())
117 /* move to last existing one ("end" is already invalid) */
118 m_content->cursorEnd(); m_content->cursorMove(-1);
119 /* current selection invisible? */
120 if (m_top + m_items_per_page <= m_content->cursorGet())
122 int rest = m_content->size() % m_items_per_page;
124 m_top = m_content->cursorGet() - rest + 1;
126 m_top = m_content->cursorGet() - m_items_per_page + 1;
135 /* note that we could be on an invalid cursor position, but we don't
136 care. this only happens on empty lists, and should have almost no
139 /* now, look wether the current selection is out of screen */
140 m_selected = m_content->cursorGet();
142 while (m_selected < m_top)
144 m_top -= m_items_per_page;
148 while (m_selected >= m_top + m_items_per_page)
149 /* m_top should be always valid here as it's selected */
150 m_top += m_items_per_page;
156 else if (m_selected != oldsel)
159 /* redraw the old and newly selected */
160 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
161 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
167 void eListbox::moveSelectionTo(int index)
171 m_content->cursorHome();
172 m_content->cursorMove(index);
173 moveSelection(justCheck);
177 int eListbox::getCurrentIndex()
179 if ( m_content && m_content->cursorValid() )
180 return m_content->cursorGet();
184 void eListbox::updateScrollBar()
186 if (!m_content || m_scrollbar_mode == showNever )
188 int entries = m_content->size();
189 if ( m_content_changed )
191 int width = size().width();
192 int height = size().height();
193 m_content_changed = false;
194 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
196 int sbarwidth=width/16;
197 if ( sbarwidth < 18 )
199 if ( sbarwidth > 22 )
201 m_scrollbar->move(ePoint(width-sbarwidth, 0));
202 m_scrollbar->resize(eSize(sbarwidth, height));
203 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
208 m_content->setSize(eSize(width, m_itemheight));
212 if ( m_items_per_page && entries )
214 int curVisiblePage = m_top / m_items_per_page;
215 if (m_prev_scrollbar_page != curVisiblePage)
217 m_prev_scrollbar_page = curVisiblePage;
218 int pages = entries / m_items_per_page;
219 if ( (pages*m_items_per_page) < entries )
221 int start=(m_top*100)/(pages*m_items_per_page);
222 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
225 m_scrollbar->setStartEnd(start,start+vis);
230 int eListbox::event(int event, void *data, void *data2)
239 ePtr<eWindowStyle> style;
242 return eWidget::event(event, data, data2);
250 gPainter &painter = *(gPainter*)data2;
252 m_content->cursorSave();
253 m_content->cursorMove(m_top - m_selected);
255 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
257 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
258 m_content->cursorMove(+1);
261 if ( m_scrollbar && m_scrollbar->isVisible() )
263 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
268 m_content->cursorRestore();
270 gettimeofday(&t2, 0);
272 eDebug("draw %d:%d", t2.tv_sec, t2.tv_usec);
277 return eWidget::event(event, data, data2);
282 moveSelection((int)data2);
287 return eWidget::event(event, data, data2);
291 void eListbox::recalcSize()
293 m_content_changed=true;
294 m_prev_scrollbar_page=-1;
295 m_content->setSize(eSize(size().width(), m_itemheight));
296 m_items_per_page = size().height() / m_itemheight;
298 if (m_items_per_page > 20)
299 eDebug("eListbox::recalcSize() m_items_per_page %d", m_items_per_page);
301 if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
302 m_items_per_page = 0;
304 moveSelection(justCheck);
307 void eListbox::setItemHeight(int h)
316 void eListbox::setSelectionEnable(int en)
318 if (m_selection_enabled == en)
320 m_selection_enabled = en;
321 entryChanged(m_selected); /* redraw current entry */
324 void eListbox::entryAdded(int index)
326 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
328 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
329 if (index <= m_selected)
334 /* we have to check wether our current cursor is gone out of the screen. */
335 /* moveSelection will check for this case */
336 moveSelection(justCheck);
338 /* now, check if the new index is visible. */
339 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
341 /* todo, calc exact invalidation... */
346 void eListbox::entryRemoved(int index)
348 if (index == m_selected)
349 m_selected = m_content->cursorGet();
351 moveSelection(justCheck);
353 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
355 /* todo, calc exact invalidation... */
360 void eListbox::entryChanged(int index)
362 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
364 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
369 void eListbox::entryReset(bool cursorHome)
371 m_content_changed=true;
372 m_prev_scrollbar_page=-1;
376 m_content->cursorHome();
380 moveSelection(justCheck);