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_enabled_wrap_around(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::setWrapAround(bool state)
51 m_enabled_wrap_around = state;
54 void eListbox::setContent(iListboxContent *content)
56 int oldsel = m_selected;
59 m_content->setListbox(this);
61 /* if oldsel != m_selected, selectionChanged was already
62 emitted in entryReset. we want it in any case, so otherwise,
64 if (oldsel == m_selected)
65 /* emit */ selectionChanged();
68 bool eListbox::atBegin()
70 if (m_content && !m_selected)
75 bool eListbox::atEnd()
77 if (m_content && m_content->size() == m_selected+1)
82 void eListbox::moveToEnd()
86 /* move to last existing one ("end" is already invalid) */
87 m_content->cursorEnd(); m_content->cursorMove(-1);
88 /* current selection invisible? */
89 if (m_top + m_items_per_page <= m_content->cursorGet())
91 int rest = m_content->size() % m_items_per_page;
93 m_top = m_content->cursorGet() - rest + 1;
95 m_top = m_content->cursorGet() - m_items_per_page + 1;
101 void eListbox::moveSelection(int dir)
103 /* refuse to do anything without a valid list. */
106 /* if our list does not have one entry, don't do anything. */
107 if (!m_items_per_page)
109 /* we need the old top/sel to see what we have to redraw */
111 int oldsel = m_selected;
112 /* first, move cursor */
117 m_content->cursorMove(-1);
118 if (m_enabled_wrap_around && oldsel == m_content->cursorGet()) // must wrap around ?
123 m_content->cursorMove(1);
124 /* ok - we could have reached the end. So we do wrap around. */
125 if (!m_content->cursorValid())
127 if (m_enabled_wrap_around)
130 m_content->cursorHome();
133 m_content->cursorMove(-1);
137 if (m_content->cursorGet() >= m_items_per_page)
139 m_content->cursorMove(-m_items_per_page);
140 m_top -= m_items_per_page;
146 m_content->cursorHome();
150 m_content->cursorHome();
151 m_top = 0; /* align with top, speeds up process */
154 m_content->cursorMove(m_items_per_page);
155 if (m_content->cursorValid())
165 if (m_content->cursorValid() && !m_content->currentCursorSelectable())
167 /* ok, our cursor position is valid (i.e. in list), but not selectable. */
169 /* when moving up, continue until we found a valid position. */
170 if ((dir == moveUp) || (dir == pageDown))
172 while (m_content->cursorGet())
174 m_content->cursorMove(-1);
175 if (m_content->currentCursorSelectable())
183 while (m_content->cursorValid())
185 m_content->cursorMove(+1);
186 if (m_content->currentCursorSelectable())
192 if (!m_content->cursorValid())
193 m_content->cursorMove(-1);
196 if (!m_content->currentCursorSelectable())
197 m_content->cursorSet(oldsel);
200 /* note that we could be on an invalid cursor position, but we don't
201 care. this only happens on empty lists, and should have almost no
204 /* now, look wether the current selection is out of screen */
205 m_selected = m_content->cursorGet();
206 while (m_selected < m_top)
208 m_top -= m_items_per_page;
212 while (m_selected >= m_top + m_items_per_page)
213 /* m_top should be always valid here as it's selected */
214 m_top += m_items_per_page;
216 if (oldsel != m_selected)
217 /* emit */ selectionChanged();
223 else if (m_selected != oldsel)
225 /* redraw the old and newly selected */
226 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
227 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
233 void eListbox::moveSelectionTo(int index)
237 m_content->cursorHome();
238 m_content->cursorMove(index);
239 moveSelection(justCheck);
243 int eListbox::getCurrentIndex()
245 if (m_content && m_content->cursorValid())
246 return m_content->cursorGet();
250 void eListbox::updateScrollBar()
252 if (!m_content || m_scrollbar_mode == showNever )
254 int entries = m_content->size();
255 if (m_content_changed)
257 int width = size().width();
258 int height = size().height();
259 m_content_changed = false;
260 if (entries > m_items_per_page || m_scrollbar_mode == showAlways)
262 int sbarwidth=width/16;
267 m_scrollbar->move(ePoint(width-sbarwidth, 0));
268 m_scrollbar->resize(eSize(sbarwidth, height));
269 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
274 m_content->setSize(eSize(width, m_itemheight));
278 if (m_items_per_page && entries)
280 int curVisiblePage = m_top / m_items_per_page;
281 if (m_prev_scrollbar_page != curVisiblePage)
283 m_prev_scrollbar_page = curVisiblePage;
284 int pages = entries / m_items_per_page;
285 if ((pages*m_items_per_page) < entries)
287 int start=(m_top*100)/(pages*m_items_per_page);
288 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
291 m_scrollbar->setStartEnd(start,start+vis);
296 int eListbox::event(int event, void *data, void *data2)
302 ePtr<eWindowStyle> style;
305 return eWidget::event(event, data, data2);
313 gPainter &painter = *(gPainter*)data2;
315 m_content->cursorSave();
316 m_content->cursorMove(m_top - m_selected);
318 gRegion entryrect = eRect(0, 0, size().width(), m_itemheight);
319 const gRegion &paint_region = *(gRegion*)data;
321 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
323 gRegion entry_clip_rect = paint_region & entryrect;
325 if (!entry_clip_rect.empty())
326 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
328 /* (we could clip with entry_clip_rect, but
329 this shouldn't change the behaviour of any
330 well behaving content, so it would just
331 degrade performance without any gain.) */
333 m_content->cursorMove(+1);
334 entryrect.moveBy(ePoint(0, m_itemheight));
337 // clear/repaint empty/unused space between scrollbar and listboxentrys
338 if (m_scrollbar && m_scrollbar->isVisible())
340 style->setStyle(painter, eWindowStyle::styleListboxNormal);
341 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
346 m_content->cursorRestore();
353 return eWidget::event(event, data, data2);
358 moveSelection((int)data2);
363 return eWidget::event(event, data, data2);
367 void eListbox::recalcSize()
369 m_content_changed=true;
370 m_prev_scrollbar_page=-1;
372 m_content->setSize(eSize(size().width(), m_itemheight));
373 m_items_per_page = size().height() / m_itemheight;
375 if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
376 m_items_per_page = 0;
378 moveSelection(justCheck);
381 void eListbox::setItemHeight(int h)
390 void eListbox::setSelectionEnable(int en)
392 if (m_selection_enabled == en)
394 m_selection_enabled = en;
395 entryChanged(m_selected); /* redraw current entry */
398 void eListbox::entryAdded(int index)
400 if (m_content && (m_content->size() % m_items_per_page) == 1)
401 m_content_changed=true;
402 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
404 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
405 if (index <= m_selected)
410 /* we have to check wether our current cursor is gone out of the screen. */
411 /* moveSelection will check for this case */
412 moveSelection(justCheck);
414 /* now, check if the new index is visible. */
415 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
417 /* todo, calc exact invalidation... */
422 void eListbox::entryRemoved(int index)
424 if (m_content && !(m_content->size() % m_items_per_page))
425 m_content_changed=true;
427 if (index == m_selected && m_content)
428 m_selected = m_content->cursorGet();
430 moveSelection(justCheck);
432 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
434 /* todo, calc exact invalidation... */
439 void eListbox::entryChanged(int index)
441 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
443 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
448 void eListbox::entryReset(bool selectionHome)
450 m_content_changed = true;
451 m_prev_scrollbar_page = -1;
456 m_content->cursorHome();
461 if (m_content && (m_selected >= m_content->size()))
463 if (m_content->size())
464 m_selected = m_content->size() - 1;
467 m_content->cursorSet(m_selected);
471 moveSelection(justCheck);
475 void eListbox::setBackgroundColor(gRGB &col)
477 m_style.m_background_color = col;
478 m_style.m_background_color_set = 1;
481 void eListbox::setForegroundColor(gRGB &col)
483 m_style.m_foreground_color = col;
484 m_style.m_foreground_color_set = 1;
487 void eListbox::setBackgroundPicture(gPixmap *pm)
489 m_style.m_background = pm;
492 void eListbox::setSelectionPicture(gPixmap *pm)
494 m_style.m_selection = pm;
497 struct eListboxStyle *eListbox::getLocalStyle(void)
499 /* transparency is set directly in the widget */
500 m_style.m_transparent_background = isTransparent();