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)
164 m_content->cursorHome();
165 m_content->cursorMove(index);
166 moveSelection(justCheck);
170 int eListbox::getCurrentIndex()
172 if ( m_content && m_content->cursorValid() )
173 return m_content->cursorGet();
177 void eListbox::updateScrollBar()
179 if (!m_content || m_scrollbar_mode == showNever )
181 int entries = m_content->size();
182 if ( m_content_changed )
184 int width = size().width();
185 int height = size().height();
186 m_content_changed = false;
187 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
189 int sbarwidth=width/16;
190 if ( sbarwidth < 18 )
192 if ( sbarwidth > 22 )
194 m_scrollbar->move(ePoint(width-sbarwidth, 0));
195 m_scrollbar->resize(eSize(sbarwidth, height));
196 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
201 m_content->setSize(eSize(width, m_itemheight));
205 if ( m_items_per_page && entries )
207 int curVisiblePage = m_top / m_items_per_page;
208 if (m_prev_scrollbar_page != curVisiblePage)
210 m_prev_scrollbar_page = curVisiblePage;
211 int pages = entries / m_items_per_page;
212 if ( (pages*m_items_per_page) < entries )
214 int start=(m_top*100)/(pages*m_items_per_page);
215 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
218 m_scrollbar->setStartEnd(start,start+vis);
223 int eListbox::event(int event, void *data, void *data2)
229 ePtr<eWindowStyle> style;
232 return eWidget::event(event, data, data2);
240 gPainter &painter = *(gPainter*)data2;
242 m_content->cursorSave();
243 m_content->cursorMove(m_top - m_selected);
245 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
247 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
248 m_content->cursorMove(+1);
251 if ( m_scrollbar && m_scrollbar->isVisible() )
253 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
258 m_content->cursorRestore();
264 return eWidget::event(event, data, data2);
269 moveSelection((int)data2);
274 return eWidget::event(event, data, data2);
278 void eListbox::recalcSize()
280 m_content_changed=true;
281 m_prev_scrollbar_page=-1;
282 m_content->setSize(eSize(size().width(), m_itemheight));
283 m_items_per_page = size().height() / m_itemheight;
284 moveSelection(justCheck);
287 void eListbox::setItemHeight(int h)
296 void eListbox::setSelectionEnable(int en)
298 if (m_selection_enabled == en)
300 m_selection_enabled = en;
301 entryChanged(m_selected); /* redraw current entry */
304 void eListbox::entryAdded(int index)
306 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
308 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
309 if (index <= m_selected)
314 /* we have to check wether our current cursor is gone out of the screen. */
315 /* moveSelection will check for this case */
316 moveSelection(justCheck);
318 /* now, check if the new index is visible. */
319 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
321 /* todo, calc exact invalidation... */
326 void eListbox::entryRemoved(int index)
328 if (index == m_selected)
329 m_selected = m_content->cursorGet();
331 moveSelection(justCheck);
333 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
335 /* todo, calc exact invalidation... */
340 void eListbox::entryChanged(int index)
342 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
344 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
349 void eListbox::entryReset(bool cursorHome)
351 m_content_changed=true;
352 m_prev_scrollbar_page=-1;
356 m_content->cursorHome();
360 moveSelection(justCheck);