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());
11 setScrollbarMode(showOnDemand); // Default show scrollbar on demand
14 eActionMap::getInstance(ptr);
17 m_selection_enabled = 1;
19 ptr->bindAction("ListboxActions", 0, 0, this);
25 eActionMap::getInstance(ptr);
26 ptr->unbindAction(this, 0);
29 void eListbox::setScrollbarMode(int mode)
31 m_scrollbar_mode = mode;
32 if ( m_scrollbar_mode == showNever && m_scrollbar )
37 else if (!m_scrollbar)
39 m_scrollbar = new eSlider(this);
41 m_scrollbar->setBorderWidth(1);
42 m_scrollbar->setOrientation(eSlider::orVertical);
43 m_scrollbar->setRange(0,100);
47 void eListbox::setContent(iListboxContent *content)
51 m_content->setListbox(this);
55 void eListbox::moveSelection(int dir)
57 /* refuse to do anything without a valid list. */
61 /* we need the old top/sel to see what we have to redraw */
63 int oldsel = m_selected;
65 /* first, move cursor */
69 m_content->cursorMove(-1);
72 m_content->cursorMove(1);
73 /* ok - we could have reached the end. we just go one back then. */
74 if (!m_content->cursorValid())
75 m_content->cursorMove(-1);
78 if (m_content->cursorGet() >= m_items_per_page)
80 m_content->cursorMove(-m_items_per_page);
81 m_top -= m_items_per_page;
87 m_content->cursorHome();
91 m_content->cursorHome();
92 m_top = 0; /* align with top, speeds up process */
96 m_content->cursorMove(m_items_per_page);
97 if (m_content->cursorValid())
101 /* move to last existing one ("end" is already invalid) */
102 m_content->cursorEnd(); m_content->cursorMove(-1);
103 /* current selection invisible? */
104 if (m_top + m_items_per_page <= m_content->cursorGet())
106 m_top = m_content->cursorGet() - m_items_per_page + 1;
115 /* note that we could be on an invalid cursor position, but we don't
116 care. this only happens on empty lists, and should have almost no
119 /* now, look wether the current selection is out of screen */
120 m_selected = m_content->cursorGet();
122 while (m_selected < m_top)
124 m_top -= m_items_per_page;
128 while (m_selected >= m_top + m_items_per_page)
129 /* m_top should be always valid here as it's selected */
130 m_top += m_items_per_page;
134 else if (m_selected != oldsel)
137 /* redraw the old and newly selected */
138 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
139 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
145 void eListbox::moveSelectionTo(int index)
147 m_content->cursorHome();
148 m_content->cursorMove(index);
149 moveSelection(justCheck);
152 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();