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), m_scrollbar(NULL)
9 setContent(new eListboxStringContent());
12 eActionMap::getInstance(ptr);
15 m_selection_enabled = 1;
17 ptr->bindAction("ListboxActions", 0, 0, this);
23 eActionMap::getInstance(ptr);
24 ptr->unbindAction(this, 0);
27 void eListbox::setScrollbarMode(int mode)
29 m_scrollbar_mode = mode;
30 if ( m_scrollbar_mode == showNever && m_scrollbar )
35 else if (!m_scrollbar)
37 m_scrollbar = new eSlider(this);
39 m_scrollbar->setBorderWidth(1);
40 m_scrollbar->setOrientation(eSlider::orVertical);
41 m_scrollbar->setRange(0,100);
45 void eListbox::setContent(iListboxContent *content)
49 m_content->setListbox(this);
53 void eListbox::moveSelection(int dir)
55 /* refuse to do anything without a valid list. */
59 /* we need the old top/sel to see what we have to redraw */
61 int oldsel = m_selected;
63 /* first, move cursor */
67 m_content->cursorMove(-1);
70 m_content->cursorMove(1);
71 /* ok - we could have reached the end. we just go one back then. */
72 if (!m_content->cursorValid())
73 m_content->cursorMove(-1);
76 if (m_content->cursorGet() >= m_items_per_page)
78 m_content->cursorMove(-m_items_per_page);
79 m_top -= m_items_per_page;
85 m_content->cursorHome();
89 m_content->cursorHome();
90 m_top = 0; /* align with top, speeds up process */
94 m_content->cursorMove(m_items_per_page);
95 if (m_content->cursorValid())
99 /* move to last existing one ("end" is already invalid) */
100 m_content->cursorEnd(); m_content->cursorMove(-1);
101 /* current selection invisible? */
102 if (m_top + m_items_per_page <= m_content->cursorGet())
104 m_top = m_content->cursorGet() - m_items_per_page + 1;
113 /* note that we could be on an invalid cursor position, but we don't
114 care. this only happens on empty lists, and should have almost no
117 /* now, look wether the current selection is out of screen */
118 m_selected = m_content->cursorGet();
120 while (m_selected < m_top)
122 m_top -= m_items_per_page;
126 while (m_selected >= m_top + m_items_per_page)
127 /* m_top should be always valid here as it's selected */
128 m_top += m_items_per_page;
132 else if (m_selected != oldsel)
135 /* redraw the old and newly selected */
136 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
137 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
143 void eListbox::moveSelectionTo(int index)
145 m_content->cursorHome();
146 m_content->cursorMove(index);
147 moveSelection(justCheck);
150 void eListbox::updateScrollBar()
154 int entries = m_content->size();
155 if ( m_content_changed )
157 int width = size().width();
158 int height = size().height();
159 m_content_changed = false;
160 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
162 int sbarwidth=width/16;
163 if ( sbarwidth < 18 )
165 if ( sbarwidth > 22 )
167 m_scrollbar->move(ePoint(width-sbarwidth, 0));
168 m_scrollbar->resize(eSize(sbarwidth, height));
169 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
170 if ( !m_scrollbar->isVisible() )
173 else if ( m_scrollbar_mode != showAlways )
175 if ( m_scrollbar->isVisible() )
177 m_content->setSize(eSize(width, m_itemheight));
178 m_scrollbar->hide(); // why this hide dont work???
182 int curVisiblePage = m_top / m_items_per_page;
183 if ( m_scrollbar->isVisible() &&
184 m_prev_scrollbar_page != curVisiblePage)
186 m_prev_scrollbar_page = curVisiblePage;
187 int pages = entries / m_items_per_page;
188 if ( (pages*m_items_per_page) < entries )
190 int start=(m_top*100)/(pages*m_items_per_page);
191 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
194 m_scrollbar->setStartEnd(start,start+vis);
198 int eListbox::event(int event, void *data, void *data2)
204 ePtr<eWindowStyle> style;
207 return eWidget::event(event, data, data2);
215 gPainter &painter = *(gPainter*)data2;
217 if (m_scrollbar_mode != showNever)
220 m_content->cursorSave();
221 m_content->cursorMove(m_top - m_selected);
223 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
225 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
226 m_content->cursorMove(+1);
229 if ( m_scrollbar && m_scrollbar->isVisible() )
231 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
236 m_content->cursorRestore();
242 return eWidget::event(event, data, data2);
247 moveSelection((int)data2);
252 return eWidget::event(event, data, data2);
256 void eListbox::recalcSize()
258 m_content_changed=true;
259 m_content->setSize(eSize(size().width(), m_itemheight));
260 m_items_per_page = size().height() / m_itemheight;
263 void eListbox::setItemHeight(int h)
272 void eListbox::setSelectionEnable(int en)
274 if (m_selection_enabled == en)
276 m_selection_enabled = en;
277 entryChanged(m_selected); /* redraw current entry */
280 void eListbox::entryAdded(int index)
282 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
284 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
285 if (index <= m_selected)
290 /* we have to check wether our current cursor is gone out of the screen. */
291 /* moveSelection will check for this case */
292 moveSelection(justCheck);
294 /* now, check if the new index is visible. */
295 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
297 /* todo, calc exact invalidation... */
302 void eListbox::entryRemoved(int index)
304 if (index == m_selected)
305 m_selected = m_content->cursorGet();
307 moveSelection(justCheck);
309 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
311 /* todo, calc exact invalidation... */
316 void eListbox::entryChanged(int index)
318 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
320 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
325 void eListbox::entryReset()
327 m_content_changed=true;
328 m_prev_scrollbar_page=-1;
330 m_content->cursorHome();