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;
20 ptr->bindAction("ListboxActions", 0, 0, this);
29 eActionMap::getInstance(ptr);
30 ptr->unbindAction(this, 0);
33 void eListbox::setScrollbarMode(int mode)
35 m_scrollbar_mode = mode;
38 if ( m_scrollbar_mode == showNever )
46 m_scrollbar = new eSlider(this);
48 m_scrollbar->setBorderWidth(1);
49 m_scrollbar->setOrientation(eSlider::orVertical);
50 m_scrollbar->setRange(0,100);
54 void eListbox::setContent(iListboxContent *content)
58 m_content->setListbox(this);
62 void eListbox::moveSelection(int dir)
64 /* refuse to do anything without a valid list. */
68 /* if our list does not have one entry, don't do anything. */
69 if (!m_items_per_page)
72 /* we need the old top/sel to see what we have to redraw */
74 int oldsel = m_selected;
76 /* first, move cursor */
80 m_content->cursorMove(-1);
83 m_content->cursorMove(1);
84 /* ok - we could have reached the end. we just go one back then. */
85 if (!m_content->cursorValid())
86 m_content->cursorMove(-1);
89 if (m_content->cursorGet() >= m_items_per_page)
91 m_content->cursorMove(-m_items_per_page);
92 m_top -= m_items_per_page;
98 m_content->cursorHome();
102 m_content->cursorHome();
103 m_top = 0; /* align with top, speeds up process */
107 m_content->cursorMove(m_items_per_page);
108 if (m_content->cursorValid())
112 /* move to last existing one ("end" is already invalid) */
113 m_content->cursorEnd(); m_content->cursorMove(-1);
114 /* current selection invisible? */
115 if (m_top + m_items_per_page <= m_content->cursorGet())
117 int rest = m_content->size() % m_items_per_page;
119 m_top = m_content->cursorGet() - rest + 1;
121 m_top = m_content->cursorGet() - m_items_per_page + 1;
130 /* note that we could be on an invalid cursor position, but we don't
131 care. this only happens on empty lists, and should have almost no
134 /* now, look wether the current selection is out of screen */
135 m_selected = m_content->cursorGet();
137 while (m_selected < m_top)
139 m_top -= m_items_per_page;
143 while (m_selected >= m_top + m_items_per_page)
144 /* m_top should be always valid here as it's selected */
145 m_top += m_items_per_page;
151 else if (m_selected != oldsel)
154 /* redraw the old and newly selected */
155 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
156 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
162 void eListbox::moveSelectionTo(int index)
166 m_content->cursorHome();
167 m_content->cursorMove(index);
168 moveSelection(justCheck);
172 int eListbox::getCurrentIndex()
174 if ( m_content && m_content->cursorValid() )
175 return m_content->cursorGet();
179 void eListbox::updateScrollBar()
181 if (!m_content || m_scrollbar_mode == showNever )
183 int entries = m_content->size();
184 if ( m_content_changed )
186 int width = size().width();
187 int height = size().height();
188 m_content_changed = false;
189 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
191 int sbarwidth=width/16;
192 if ( sbarwidth < 18 )
194 if ( sbarwidth > 22 )
196 m_scrollbar->move(ePoint(width-sbarwidth, 0));
197 m_scrollbar->resize(eSize(sbarwidth, height));
198 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
203 m_content->setSize(eSize(width, m_itemheight));
207 if ( m_items_per_page && entries )
209 int curVisiblePage = m_top / m_items_per_page;
210 if (m_prev_scrollbar_page != curVisiblePage)
212 m_prev_scrollbar_page = curVisiblePage;
213 int pages = entries / m_items_per_page;
214 if ( (pages*m_items_per_page) < entries )
216 int start=(m_top*100)/(pages*m_items_per_page);
217 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
220 m_scrollbar->setStartEnd(start,start+vis);
225 int eListbox::event(int event, void *data, void *data2)
231 ePtr<eWindowStyle> style;
234 return eWidget::event(event, data, data2);
242 gPainter &painter = *(gPainter*)data2;
244 m_content->cursorSave();
245 m_content->cursorMove(m_top - m_selected);
247 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
249 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
250 m_content->cursorMove(+1);
253 if ( m_scrollbar && m_scrollbar->isVisible() )
255 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
260 m_content->cursorRestore();
266 return eWidget::event(event, data, data2);
271 moveSelection((int)data2);
276 return eWidget::event(event, data, data2);
280 void eListbox::recalcSize()
282 m_content_changed=true;
283 m_prev_scrollbar_page=-1;
284 m_content->setSize(eSize(size().width(), m_itemheight));
285 m_items_per_page = size().height() / m_itemheight;
287 if (m_items_per_page < 0) /* TODO: whyever - our size could be invalid, or itemheigh could be wrongly specified. */
288 m_items_per_page = 0;
290 moveSelection(justCheck);
293 void eListbox::setItemHeight(int h)
302 void eListbox::setSelectionEnable(int en)
304 if (m_selection_enabled == en)
306 m_selection_enabled = en;
307 entryChanged(m_selected); /* redraw current entry */
310 void eListbox::entryAdded(int index)
312 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
314 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
315 if (index <= m_selected)
320 /* we have to check wether our current cursor is gone out of the screen. */
321 /* moveSelection will check for this case */
322 moveSelection(justCheck);
324 /* now, check if the new index is visible. */
325 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
327 /* todo, calc exact invalidation... */
332 void eListbox::entryRemoved(int index)
334 if (index == m_selected)
335 m_selected = m_content->cursorGet();
337 moveSelection(justCheck);
339 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
341 /* todo, calc exact invalidation... */
346 void eListbox::entryChanged(int index)
348 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
350 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
355 void eListbox::entryReset(bool cursorHome)
357 m_content_changed=true;
358 m_prev_scrollbar_page=-1;
362 m_content->cursorHome();
366 moveSelection(justCheck);