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