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(NULL), m_scrollbar_mode(showNever)
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 /* we need the old top/sel to see what we have to redraw */
62 int oldsel = m_selected;
64 /* first, move cursor */
68 m_content->cursorMove(-1);
71 m_content->cursorMove(1);
72 /* ok - we could have reached the end. we just go one back then. */
73 if (!m_content->cursorValid())
74 m_content->cursorMove(-1);
77 if (m_content->cursorGet() >= m_items_per_page)
79 m_content->cursorMove(-m_items_per_page);
80 m_top -= m_items_per_page;
86 m_content->cursorHome();
90 m_content->cursorHome();
91 m_top = 0; /* align with top, speeds up process */
95 m_content->cursorMove(m_items_per_page);
96 if (m_content->cursorValid())
100 /* move to last existing one ("end" is already invalid) */
101 m_content->cursorEnd(); m_content->cursorMove(-1);
102 /* current selection invisible? */
103 if (m_top + m_items_per_page <= m_content->cursorGet())
105 m_top = m_content->cursorGet() - m_items_per_page + 1;
114 /* note that we could be on an invalid cursor position, but we don't
115 care. this only happens on empty lists, and should have almost no
118 /* now, look wether the current selection is out of screen */
119 m_selected = m_content->cursorGet();
121 while (m_selected < m_top)
123 m_top -= m_items_per_page;
127 while (m_selected >= m_top + m_items_per_page)
128 /* m_top should be always valid here as it's selected */
129 m_top += m_items_per_page;
133 else if (m_selected != oldsel)
136 /* redraw the old and newly selected */
137 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
138 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
144 void eListbox::moveSelectionTo(int index)
146 m_content->cursorHome();
147 m_content->cursorMove(index);
148 moveSelection(justCheck);
151 void eListbox::updateScrollBar()
153 int entries = m_content->size();
154 if ( m_content_changed )
156 int width = size().width();
157 int height = size().height();
158 m_content_changed = false;
159 if ( entries > m_items_per_page || m_scrollbar_mode == showAlways )
161 int sbarwidth=width/16;
162 if ( sbarwidth < 18 )
164 if ( sbarwidth > 22 )
166 m_scrollbar->move(ePoint(width-sbarwidth, 0));
167 m_scrollbar->resize(eSize(sbarwidth, height));
168 m_content->setSize(eSize(width-sbarwidth-5, m_itemheight));
169 if ( !m_scrollbar->isVisible() )
172 else if ( m_scrollbar_mode != showAlways )
174 if ( m_scrollbar->isVisible() )
176 m_content->setSize(eSize(width, m_itemheight));
177 m_scrollbar->hide(); // why this hide dont work???
181 int curVisiblePage = m_top / m_items_per_page;
182 if ( m_scrollbar->isVisible() &&
183 m_prev_scrollbar_page != curVisiblePage)
185 m_prev_scrollbar_page = curVisiblePage;
186 int pages = entries / m_items_per_page;
187 if ( (pages*m_items_per_page) < entries )
189 int start=(m_top*100)/(pages*m_items_per_page);
190 int vis=(m_items_per_page*100)/(pages*m_items_per_page);
193 m_scrollbar->setStartEnd(start,start+vis);
197 int eListbox::event(int event, void *data, void *data2)
203 ePtr<eWindowStyle> style;
206 return eWidget::event(event, data, data2);
214 gPainter &painter = *(gPainter*)data2;
216 if (m_scrollbar_mode != showNever)
219 m_content->cursorSave();
220 m_content->cursorMove(m_top - m_selected);
222 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
224 m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
225 m_content->cursorMove(+1);
228 if ( m_scrollbar && m_scrollbar->isVisible() )
230 painter.clip(eRect(m_scrollbar->position() - ePoint(5,0), eSize(5,m_scrollbar->size().height())));
235 m_content->cursorRestore();
241 return eWidget::event(event, data, data2);
246 moveSelection((int)data2);
251 return eWidget::event(event, data, data2);
255 void eListbox::recalcSize()
257 m_content_changed=true;
258 m_content->setSize(eSize(size().width(), m_itemheight));
259 m_items_per_page = size().height() / m_itemheight;
262 void eListbox::setItemHeight(int h)
271 void eListbox::setSelectionEnable(int en)
273 if (m_selection_enabled == en)
275 m_selection_enabled = en;
276 entryChanged(m_selected); /* redraw current entry */
279 void eListbox::entryAdded(int index)
281 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
283 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
284 if (index <= m_selected)
289 /* we have to check wether our current cursor is gone out of the screen. */
290 /* moveSelection will check for this case */
291 moveSelection(justCheck);
293 /* now, check if the new index is visible. */
294 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
296 /* todo, calc exact invalidation... */
301 void eListbox::entryRemoved(int index)
303 if (index == m_selected)
304 m_selected = m_content->cursorGet();
306 moveSelection(justCheck);
308 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
310 /* todo, calc exact invalidation... */
315 void eListbox::entryChanged(int index)
317 if ((m_top <= index) && (index < (m_top + m_items_per_page)))
319 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
324 void eListbox::entryReset()
326 m_content_changed=true;
327 m_prev_scrollbar_page=-1;
329 m_content->cursorHome();