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 memset(&m_style, 0, sizeof(m_style));
12 // setContent(new eListboxStringContent());
15 eActionMap::getInstance(ptr);
16 ptr->bindAction("ListboxActions", 0, 0, this);
25 eActionMap::getInstance(ptr);
26 ptr->unbindAction(this, 0);
29 void eListbox::setScrollbarMode(int mode)
31 m_scrollbar_mode = mode;
34 if (m_scrollbar_mode == showNever)
42 m_scrollbar = new eSlider(this);
44 m_scrollbar->setBorderWidth(1);
45 m_scrollbar->setOrientation(eSlider::orVertical);
46 m_scrollbar->setRange(0,100);
50 void eListbox::setWrapAround(bool state)
52 m_enabled_wrap_around = state;
55 void eListbox::setContent(iListboxContent *content)
57 int oldsel = m_selected;
60 m_content->setListbox(this);
62 /* if oldsel != m_selected, selectionChanged was already
63 emitted in entryReset. we want it in any case, so otherwise,
65 if (oldsel == m_selected)
66 /* emit */ selectionChanged();
69 bool eListbox::atBegin()
71 if (m_content && !m_selected)
76 bool eListbox::atEnd()
78 if (m_content && m_content->size() == m_selected+1)
83 void eListbox::moveToEnd()
87 /* move to last existing one ("end" is already invalid) */
88 m_content->cursorEnd(); m_content->cursorMove(-1);
89 /* current selection invisible? */
90 if (m_top + m_items_per_page <= m_content->cursorGet())
92 int rest = m_content->size() % m_items_per_page;
94 m_top = m_content->cursorGet() - rest + 1;
96 m_top = m_content->cursorGet() - m_items_per_page + 1;
102 void eListbox::moveSelection(int dir)
104 /* refuse to do anything without a valid list. */
107 /* if our list does not have one entry, don't do anything. */
108 if (!m_items_per_page)
110 /* we need the old top/sel to see what we have to redraw */
112 int oldsel = m_selected;
113 /* first, move cursor */
118 m_content->cursorMove(-1);
119 if (m_enabled_wrap_around && oldsel == m_content->cursorGet()) // must wrap around ?
124 m_content->cursorMove(1);
125 /* ok - we could have reached the end. So we do wrap around. */
126 if (!m_content->cursorValid())
128 if (m_enabled_wrap_around)
131 m_content->cursorHome();
134 m_content->cursorMove(-1);
138 if (m_content->cursorGet() >= m_items_per_page)
140 m_content->cursorMove(-m_items_per_page);
141 m_top -= m_items_per_page;
147 m_content->cursorHome();
151 m_content->cursorHome();
152 m_top = 0; /* align with top, speeds up process */
155 m_content->cursorMove(m_items_per_page);
156 if (m_content->cursorValid())
166 if (m_content->cursorValid() && !m_content->currentCursorSelectable())
168 /* ok, our cursor position is valid (i.e. in list), but not selectable. */
170 /* when moving up, continue until we found a valid position. */
171 if ((dir == moveUp) || (dir == pageDown))
173 while (m_content->cursorGet())
175 m_content->cursorMove(-1);
176 if (m_content->currentCursorSelectable())
184 while (m_content->cursorValid())
186 m_content->cursorMove(+1);
187 if (m_content->currentCursorSelectable())
193 if (!m_content->cursorValid())
194 m_content->cursorMove(-1);
197 if (!m_content->currentCursorSelectable())
198 m_content->cursorSet(oldsel);
201 /* note that we could be on an invalid cursor position, but we don't
202 care. this only happens on empty lists, and should have almost no
205 /* now, look wether the current selection is out of screen */
206 m_selected = m_content->cursorGet();
207 while (m_selected < m_top)
209 m_top -= m_items_per_page;
213 while (m_selected >= m_top + m_items_per_page)
214 /* m_top should be always valid here as it's selected */
215 m_top += m_items_per_page;
217 if (oldsel != m_selected)
218 /* emit */ selectionChanged();
224 else if (m_selected != oldsel)
226 /* redraw the old and newly selected */
227 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
228 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::getEntryTop()
298 return (m_selected - m_top) * m_itemheight;
301 int eListbox::event(int event, void *data, void *data2)
307 ePtr<eWindowStyle> style;
310 return eWidget::event(event, data, data2);
318 gPainter &painter = *(gPainter*)data2;
320 m_content->cursorSave();
321 m_content->cursorMove(m_top - m_selected);
323 gRegion entryrect = eRect(0, 0, size().width(), m_itemheight);
324 const gRegion &paint_region = *(gRegion*)data;
326 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
328 gRegion entry_clip_rect = paint_region & entryrect;
330 if (!entry_clip_rect.empty())
331 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
333 /* (we could clip with entry_clip_rect, but
334 this shouldn't change the behaviour of any
335 well behaving content, so it would just
336 degrade performance without any gain.) */
338 m_content->cursorMove(+1);
339 entryrect.moveBy(ePoint(0, m_itemheight));
342 // clear/repaint empty/unused space between scrollbar and listboxentrys
343 if (m_scrollbar && m_scrollbar->isVisible())
345 style->setStyle(painter, eWindowStyle::styleListboxNormal);
346 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
351 m_content->cursorRestore();
358 return eWidget::event(event, data, data2);
363 moveSelection((int)data2);
368 return eWidget::event(event, data, data2);
372 void eListbox::recalcSize()
374 m_content_changed=true;
375 m_prev_scrollbar_page=-1;
377 m_content->setSize(eSize(size().width(), m_itemheight));
378 m_items_per_page = size().height() / m_itemheight;
380 if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
381 m_items_per_page = 0;
383 moveSelection(justCheck);
386 void eListbox::setItemHeight(int h)
395 void eListbox::setSelectionEnable(int en)
397 if (m_selection_enabled == en)
399 m_selection_enabled = en;
400 entryChanged(m_selected); /* redraw current entry */
403 void eListbox::entryAdded(int index)
405 if (m_content && (m_content->size() % m_items_per_page) == 1)
406 m_content_changed=true;
407 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
409 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
410 if (index <= m_selected)
415 /* we have to check wether our current cursor is gone out of the screen. */
416 /* moveSelection will check for this case */
417 moveSelection(justCheck);
419 /* now, check if the new index is visible. */
420 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
422 /* todo, calc exact invalidation... */
427 void eListbox::entryRemoved(int index)
429 if (m_content && !(m_content->size() % m_items_per_page))
430 m_content_changed=true;
432 if (index == m_selected && m_content)
433 m_selected = m_content->cursorGet();
435 if (m_content && m_content->cursorGet() >= m_content->size())
436 moveSelection(moveUp);
438 moveSelection(justCheck);
440 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
442 /* todo, calc exact invalidation... */
447 void eListbox::entryChanged(int index)
449 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
451 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
456 void eListbox::entryReset(bool selectionHome)
458 m_content_changed = true;
459 m_prev_scrollbar_page = -1;
464 m_content->cursorHome();
469 if (m_content && (m_selected >= m_content->size()))
471 if (m_content->size())
472 m_selected = m_content->size() - 1;
475 m_content->cursorSet(m_selected);
479 moveSelection(justCheck);
483 void eListbox::setBackgroundColor(gRGB &col)
485 m_style.m_background_color = col;
486 m_style.m_background_color_set = 1;
489 void eListbox::setBackgroundColorSelected(gRGB &col)
491 m_style.m_background_color_selected = col;
492 m_style.m_background_color_selected_set = 1;
495 void eListbox::setForegroundColor(gRGB &col)
497 m_style.m_foreground_color = col;
498 m_style.m_foreground_color_set = 1;
501 void eListbox::setForegroundColorSelected(gRGB &col)
503 m_style.m_foreground_color_selected = col;
504 m_style.m_foreground_color_selected_set = 1;
507 void eListbox::setBackgroundPicture(ePtr<gPixmap> &pm)
509 m_style.m_background = pm;
512 void eListbox::setSelectionPicture(ePtr<gPixmap> &pm)
514 m_style.m_selection = pm;
517 void eListbox::invalidate(const gRegion ®ion)
521 m_content->updateClip(tmp);
522 eWidget::invalidate(tmp);
525 struct eListboxStyle *eListbox::getLocalStyle(void)
527 /* transparency is set directly in the widget */
528 m_style.m_transparent_background = isTransparent();