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