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);
24 eActionMap::getInstance(ptr);
25 ptr->unbindAction(this, 0);
28 void eListbox::setScrollbarMode(int mode)
30 m_scrollbar_mode = mode;
31 if ( m_scrollbar_mode == showNever && m_scrollbar )
36 else if (!m_scrollbar)
38 m_scrollbar = new eSlider(this);
40 m_scrollbar->setBorderWidth(1);
41 m_scrollbar->setOrientation(eSlider::orVertical);
42 m_scrollbar->setRange(0,100);
46 void eListbox::setContent(iListboxContent *content)
50 m_content->setListbox(this);
54 void eListbox::moveSelection(int dir)
56 /* refuse to do anything without a valid list. */
60 /* if our list does not have one entry, don't do anything. */
61 if (!m_items_per_page)
64 /* we need the old top/sel to see what we have to redraw */
66 int oldsel = m_selected;
68 /* first, move cursor */
72 m_content->cursorMove(-1);
75 m_content->cursorMove(1);
76 /* ok - we could have reached the end. we just go one back then. */
77 if (!m_content->cursorValid())
78 m_content->cursorMove(-1);
81 if (m_content->cursorGet() >= m_items_per_page)
83 m_content->cursorMove(-m_items_per_page);
84 m_top -= m_items_per_page;
90 m_content->cursorHome();
94 m_content->cursorHome();
95 m_top = 0; /* align with top, speeds up process */
99 m_content->cursorMove(m_items_per_page);
100 if (m_content->cursorValid())
104 /* move to last existing one ("end" is already invalid) */
105 m_content->cursorEnd(); m_content->cursorMove(-1);
106 /* current selection invisible? */
107 if (m_top + m_items_per_page <= m_content->cursorGet())
109 int rest = m_content->size() % m_items_per_page;
111 m_top = m_content->cursorGet() - rest + 1;
113 m_top = m_content->cursorGet() - m_items_per_page + 1;
122 /* note that we could be on an invalid cursor position, but we don't
123 care. this only happens on empty lists, and should have almost no
126 /* now, look wether the current selection is out of screen */
127 m_selected = m_content->cursorGet();
129 while (m_selected < m_top)
131 m_top -= m_items_per_page;
135 while (m_selected >= m_top + m_items_per_page)
136 /* m_top should be always valid here as it's selected */
137 m_top += m_items_per_page;
141 else if (m_selected != oldsel)
144 /* redraw the old and newly selected */
145 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
146 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
151 if (m_scrollbar_mode != showNever)
155 void eListbox::moveSelectionTo(int index)
157 m_content->cursorHome();
158 m_content->cursorMove(index);
159 moveSelection(justCheck);
162 void eListbox::updateScrollBar()
164 int entries = m_content->size();
165 if ( m_content_changed )
167 int width = size().width();
168 int height = size().height();
169 m_content_changed = false;
170 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
172 int sbarwidth=width/16;
173 if ( sbarwidth < 18 )
175 if ( sbarwidth > 22 )
177 m_scrollbar->move(ePoint(width-sbarwidth, 0));
178 m_scrollbar->resize(eSize(sbarwidth, height));
179 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
180 if ( !m_scrollbar->isVisible() )
183 else if ( m_scrollbar_mode != showAlways )
185 if ( m_scrollbar->isVisible() )
187 m_content->setSize(eSize(width, m_itemheight));
188 m_scrollbar->hide(); // why this hide dont work???
192 int curVisiblePage = m_top / m_items_per_page;
193 if ( m_scrollbar->isVisible() &&
194 m_prev_scrollbar_page != curVisiblePage)
196 m_prev_scrollbar_page = curVisiblePage;
197 int pages = entries / m_items_per_page;
198 if ( (pages*m_items_per_page) < entries )
200 int start=(m_top*100)/(pages*m_items_per_page);
201 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
204 m_scrollbar->setStartEnd(start,start+vis);
208 int eListbox::event(int event, void *data, void *data2)
214 ePtr<eWindowStyle> style;
217 return eWidget::event(event, data, data2);
225 gPainter &painter = *(gPainter*)data2;
227 m_content->cursorSave();
228 m_content->cursorMove(m_top - m_selected);
230 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
232 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
233 m_content->cursorMove(+1);
236 if ( m_scrollbar && m_scrollbar->isVisible() )
238 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
243 m_content->cursorRestore();
249 return eWidget::event(event, data, data2);
253 moveSelection((int)data2);
258 return eWidget::event(event, data, data2);
262 void eListbox::recalcSize()
264 m_content_changed=true;
265 m_content->setSize(eSize(size().width(), m_itemheight));
266 m_items_per_page = size().height() / m_itemheight;
267 moveSelection(justCheck);
270 void eListbox::setItemHeight(int h)
279 void eListbox::setSelectionEnable(int en)
281 if (m_selection_enabled == en)
283 m_selection_enabled = en;
284 entryChanged(m_selected); /* redraw current entry */
287 void eListbox::entryAdded(int index)
289 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
291 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
292 if (index <= m_selected)
297 /* we have to check wether our current cursor is gone out of the screen. */
298 /* moveSelection will check for this case */
299 moveSelection(justCheck);
301 /* now, check if the new index is visible. */
302 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
304 /* todo, calc exact invalidation... */
309 void eListbox::entryRemoved(int index)
311 if (index == m_selected)
312 m_selected = m_content->cursorGet();
314 moveSelection(justCheck);
316 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
318 /* todo, calc exact invalidation... */
323 void eListbox::entryChanged(int index)
325 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
327 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
332 void eListbox::entryReset()
334 m_content_changed=true;
335 m_prev_scrollbar_page=-1;
337 m_content->cursorHome();
340 moveSelection(justCheck);