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 #include "vuplus_gles.h"
9 eListbox::eListbox(eWidget *parent) :
10 eWidget(parent), m_scrollbar_mode(showNever), m_prev_scrollbar_page(-1),
11 m_content_changed(false), m_enabled_wrap_around(false), m_top(0), m_selected(0), m_itemheight(25),
12 m_items_per_page(0), m_selection_enabled(1), m_scrollbar(NULL)
14 memset(&m_style, 0, sizeof(m_style));
15 // setContent(new eListboxStringContent());
18 eActionMap::getInstance(ptr);
19 ptr->bindAction("ListboxActions", 0, 0, this);
28 eActionMap::getInstance(ptr);
29 ptr->unbindAction(this, 0);
32 void eListbox::setScrollbarMode(int mode)
34 m_scrollbar_mode = mode;
37 if (m_scrollbar_mode == showNever)
45 m_scrollbar = new eSlider(this);
47 m_scrollbar->setBorderWidth(1);
48 m_scrollbar->setOrientation(eSlider::orVertical);
49 m_scrollbar->setRange(0,100);
53 void eListbox::setWrapAround(bool state)
55 m_enabled_wrap_around = state;
58 void eListbox::setContent(iListboxContent *content)
62 m_content->setListbox(this);
66 bool eListbox::atBegin()
68 if (m_content && !m_selected)
73 bool eListbox::atEnd()
75 if (m_content && m_content->size() == m_selected+1)
80 void eListbox::moveToEnd()
84 /* move to last existing one ("end" is already invalid) */
85 m_content->cursorEnd(); m_content->cursorMove(-1);
86 /* current selection invisible? */
87 if (m_top + m_items_per_page <= m_content->cursorGet())
89 int rest = m_content->size() % m_items_per_page;
91 m_top = m_content->cursorGet() - rest + 1;
93 m_top = m_content->cursorGet() - m_items_per_page + 1;
99 void eListbox::moveSelection(long dir)
101 /* refuse to do anything without a valid list. */
104 /* if our list does not have one entry, don't do anything. */
105 if (!m_items_per_page)
107 /* we need the old top/sel to see what we have to redraw */
109 int oldsel = m_selected;
110 /* first, move cursor */
111 #ifdef USE_LIBVUGLES2
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-1)/(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);
332 #ifdef USE_LIBVUGLES2
333 if (m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled) {
334 ePoint pos = getAbsolutePosition();
335 painter.sendShowItem(m_dir, ePoint(pos.x(), pos.y() + y), eSize(m_scrollbar && m_scrollbar->isVisible() ? size().width() - m_scrollbar->size().width() : size().width(), m_itemheight));
336 gles_set_animation_listbox_current(pos.x(), pos.y() + y, m_scrollbar && m_scrollbar->isVisible() ? size().width() - m_scrollbar->size().width() : size().width(), m_itemheight);
341 /* (we could clip with entry_clip_rect, but
342 this shouldn't change the behavior of any
343 well behaving content, so it would just
344 degrade performance without any gain.) */
346 m_content->cursorMove(+1);
347 entryrect.moveBy(ePoint(0, m_itemheight));
350 // clear/repaint empty/unused space between scrollbar and listboxentrys
351 if (m_scrollbar && m_scrollbar->isVisible())
353 style->setStyle(painter, eWindowStyle::styleListboxNormal);
354 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
359 m_content->cursorRestore();
366 return eWidget::event(event, data, data2);
371 moveSelection((long)data2);
376 return eWidget::event(event, data, data2);
380 void eListbox::recalcSize()
382 m_content_changed=true;
383 m_prev_scrollbar_page=-1;
385 m_content->setSize(eSize(size().width(), m_itemheight));
386 m_items_per_page = size().height() / m_itemheight;
388 if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
389 m_items_per_page = 0;
391 moveSelection(justCheck);
394 void eListbox::setItemHeight(int h)
403 void eListbox::setSelectionEnable(int en)
405 if (m_selection_enabled == en)
407 m_selection_enabled = en;
408 entryChanged(m_selected); /* redraw current entry */
411 void eListbox::entryAdded(int index)
413 if (m_content && (m_content->size() % m_items_per_page) == 1)
414 m_content_changed=true;
415 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
417 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
418 if (index <= m_selected)
423 /* we have to check wether our current cursor is gone out of the screen. */
424 /* moveSelection will check for this case */
425 moveSelection(justCheck);
427 /* now, check if the new index is visible. */
428 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
430 /* todo, calc exact invalidation... */
435 void eListbox::entryRemoved(int index)
437 if (m_content && !(m_content->size() % m_items_per_page))
438 m_content_changed=true;
440 if (index == m_selected && m_content)
441 m_selected = m_content->cursorGet();
443 if (m_content && m_content->cursorGet() >= m_content->size())
444 moveSelection(moveUp);
446 moveSelection(justCheck);
448 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
450 /* todo, calc exact invalidation... */
455 void eListbox::entryChanged(int index)
457 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
459 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
464 void eListbox::entryReset(bool selectionHome)
466 m_content_changed = true;
467 m_prev_scrollbar_page = -1;
473 m_content->cursorHome();
478 if (m_content && (m_selected >= m_content->size()))
480 if (m_content->size())
481 m_selected = m_content->size() - 1;
484 m_content->cursorSet(m_selected);
488 moveSelection(justCheck);
489 /* if oldsel != m_selected, selectionChanged was already
490 emitted in moveSelection. we want it in any case, so otherwise,
492 if (oldsel == m_selected)
493 /* emit */ selectionChanged();
497 void eListbox::setFont(gFont *font)
499 m_style.m_font = font;
502 void eListbox::setBackgroundColor(gRGB &col)
504 m_style.m_background_color = col;
505 m_style.m_background_color_set = 1;
508 void eListbox::setBackgroundColorSelected(gRGB &col)
510 m_style.m_background_color_selected = col;
511 m_style.m_background_color_selected_set = 1;
514 void eListbox::setForegroundColor(gRGB &col)
516 m_style.m_foreground_color = col;
517 m_style.m_foreground_color_set = 1;
520 void eListbox::setForegroundColorSelected(gRGB &col)
522 m_style.m_foreground_color_selected = col;
523 m_style.m_foreground_color_selected_set = 1;
526 void eListbox::setBackgroundPicture(ePtr<gPixmap> &pm)
528 m_style.m_background = pm;
531 void eListbox::setSelectionPicture(ePtr<gPixmap> &pm)
533 m_style.m_selection = pm;
536 void eListbox::invalidate(const gRegion ®ion)
540 m_content->updateClip(tmp);
541 eWidget::invalidate(tmp);
544 struct eListboxStyle *eListbox::getLocalStyle(void)
546 /* transparency is set directly in the widget */
547 m_style.m_transparent_background = isTransparent();