e2f1c26f878691063247a066fe25aa18b36a2d7f
[enigma2.git] / lib / gui / elistbox.cpp
1 #include <lib/gui/elistbox.h>
2 #include <lib/gui/elistboxcontent.h>
3 #include <lib/actions/action.h>
4
5 eListbox::eListbox(eWidget *parent): eWidget(parent)
6 {
7         setContent(new eListboxStringContent());
8
9         ePtr<eActionMap> ptr;
10         eActionMap::getInstance(ptr);
11         
12         m_itemheight = 25;
13         m_selection_enabled = 1;
14         
15         ptr->bindAction("ListboxActions", 0, 0, this);
16 }
17
18 eListbox::~eListbox()
19 {
20         ePtr<eActionMap> ptr;
21         eActionMap::getInstance(ptr);
22         ptr->unbindAction(this, 0);
23 }
24
25 void eListbox::setContent(iListboxContent *content)
26 {
27         m_content = content;
28         if (content)
29                 m_content->setListbox(this);
30         entryReset();
31 }
32
33 void eListbox::moveSelection(int dir)
34 {
35                 /* refuse to do anything without a valid list. */
36         if (!m_content)
37                 return;
38                 
39                 /* we need the old top/sel to see what we have to redraw */
40         int oldtop = m_top;
41         int oldsel = m_selected;
42         
43                 /* first, move cursor */
44         switch (dir)
45         {
46         case moveUp:
47                 m_content->cursorMove(-1);
48                 break;
49         case moveDown:
50                 m_content->cursorMove(1);
51                         /* ok - we could have reached the end. we just go one back then. */
52                 if (!m_content->cursorValid())
53                         m_content->cursorMove(-1);
54                 break;
55         case pageUp:
56                 if (m_content->cursorGet() >= m_items_per_page)
57                 {
58                         m_content->cursorMove(-m_items_per_page);
59                         m_top -= m_items_per_page;
60                         if (m_top < 0)
61                                 m_top = 0;
62                 } else
63                 {
64                         m_top = 0;
65                         m_content->cursorHome();
66                 }
67                 break;
68         case moveTop:
69                 m_content->cursorHome();
70                 m_top = 0; /* align with top, speeds up process */
71                 break;
72
73         case pageDown:
74                 m_content->cursorMove(m_items_per_page);
75                 if (m_content->cursorValid())
76                         break;
77                                 /* fall through */
78         case moveEnd:
79                         /* move to last existing one ("end" is already invalid) */
80                 m_content->cursorEnd(); m_content->cursorMove(-1); 
81                         /* current selection invisible? */
82                 if (m_top + m_items_per_page <= m_content->cursorGet())
83                 {
84                         m_top = m_content->cursorGet() - m_items_per_page + 1;
85                         if (m_top < 0)
86                                 m_top = 0;
87                 }
88                 break;
89         case justCheck:
90                 break;
91         }
92         
93                 /* note that we could be on an invalid cursor position, but we don't
94                    care. this only happens on empty lists, and should have almost no
95                    side effects. */
96         
97                 /* now, look wether the current selection is out of screen */
98         m_selected = m_content->cursorGet();
99
100         while (m_selected < m_top)
101         {
102                 m_top -= m_items_per_page;
103                 if (m_top < 0)
104                         m_top = 0;
105         }
106         while (m_selected >= m_top + m_items_per_page)
107                 /* m_top should be always valid here as it's selected */
108                 m_top += m_items_per_page;
109
110         if (m_top != oldtop)
111                 invalidate();
112         else if (m_selected != oldsel)
113         {
114                 
115                         /* redraw the old and newly selected */
116                 gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
117                 inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
118                 
119                 invalidate(inv);
120         }
121 }
122
123 void eListbox::moveSelectionTo(int index)
124 {
125         printf("Moving to listbox-entry with index %d\n", index);
126         m_content->cursorHome();
127         m_content->cursorMove(index);
128         moveSelection(justCheck);
129 }
130
131 int eListbox::event(int event, void *data, void *data2)
132 {
133         switch (event)
134         {
135         case evtPaint:
136         {
137                 ePtr<eWindowStyle> style;
138                 
139                 if (!m_content)
140                         return eWidget::event(event, data, data2);
141                 assert(m_content);
142                 
143                 getStyle(style);
144                 
145                 if (!m_content)
146                         return 0;
147                 
148                 gPainter &painter = *(gPainter*)data2;
149                 
150                 m_content->cursorSave();
151                 m_content->cursorMove(m_top - m_selected);
152                 
153                 for (int y = 0, i = 0; i <= m_items_per_page; y += m_itemheight, ++i)
154                 {
155                         m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet() && m_content->size() && m_selection_enabled);
156                         m_content->cursorMove(+1);
157                 }
158                 
159                 m_content->cursorRestore();
160                 
161                 return 0;
162         }
163         case evtChangedSize:
164                 recalcSize();
165                 return eWidget::event(event, data, data2);
166                 
167         case evtAction:
168                 if (isVisible())
169                 {
170                         moveSelection((int)data2);
171                         return 1;
172                 }
173                 return 0;
174         default:
175                 return eWidget::event(event, data, data2);
176         }
177 }
178
179 void eListbox::recalcSize()
180 {
181         m_content->setSize(eSize(size().width(), m_itemheight));
182         m_items_per_page = size().height() / m_itemheight;
183 }
184
185 void eListbox::setItemHeight(int h)
186 {
187         if (h)
188                 m_itemheight = h;
189         else
190                 m_itemheight = 20;
191         recalcSize();
192 }
193
194 void eListbox::setSelectionEnable(int en)
195 {
196         if (m_selection_enabled == en)
197                 return;
198         m_selection_enabled = en;
199         entryChanged(m_selected); /* redraw current entry */
200 }
201
202 void eListbox::entryAdded(int index)
203 {
204                 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
205                 
206                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
207         if (index <= m_selected)
208                 ++m_selected;
209         if (index <= m_top)
210                 ++m_top;
211                 
212                 /* we have to check wether our current cursor is gone out of the screen. */
213                 /* moveSelection will check for this case */
214         moveSelection(justCheck);
215         
216                 /* now, check if the new index is visible. */
217         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
218         {
219                         /* todo, calc exact invalidation... */
220                 invalidate();
221         }
222 }
223
224 void eListbox::entryRemoved(int index)
225 {
226         if (index == m_selected)
227                 m_selected = m_content->cursorGet();
228
229         moveSelection(justCheck);
230         
231         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
232         {
233                         /* todo, calc exact invalidation... */
234                 invalidate();
235         }
236 }
237
238 void eListbox::entryChanged(int index)
239 {
240         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
241         {
242                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
243                 invalidate(inv);
244         }
245 }
246
247 void eListbox::entryReset()
248 {
249         if (m_content)
250                 m_content->cursorHome();
251         m_top = 0;
252         m_selected = 0;
253         invalidate();
254 }