- listbox size gets recalculated at the correct position
[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                 
111                 getStyle(style);
112                 
113                 if (!m_content)
114                         return 0;
115                 
116                 gPainter &painter = *(gPainter*)data2;
117                 
118                 m_content->cursorSave();
119                 m_content->cursorMove(m_top - m_selected);
120                 
121                 for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
122                 {
123                         m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet());
124                         m_content->cursorMove(+1);
125                 }
126                 
127                 m_content->cursorRestore();
128                 
129                 return 0;
130         }
131         case evtChangedSize:
132                 recalcSize();
133                 return eWidget::event(event, data, data2);
134                 
135         case evtAction:
136                 if (isVisible())
137                 {
138                         moveSelection((int)data2);
139                         return 1;
140                 }
141                 return 0;
142         default:
143                 return eWidget::event(event, data, data2);
144         }
145 }
146
147 void eListbox::recalcSize()
148 {
149         m_itemheight = 20;
150         m_content->setSize(eSize(size().width(), m_itemheight));
151         m_items_per_page = size().height() / m_itemheight;
152 }
153
154 void eListbox::entryAdded(int index)
155 {
156                 /* manage our local pointers. when the entry was added before the current position, we have to advance. */
157                 
158                 /* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
159         if (index <= m_selected)
160                 ++m_selected;
161         if (index <= m_top)
162                 ++m_top;
163                 
164                 /* we have to check wether our current cursor is gone out of the screen. */
165                 /* moveSelection will check for this case */
166         moveSelection(justCheck);
167         
168                 /* now, check if the new index is visible. */
169         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
170         {
171                         /* todo, calc exact invalidation... */
172                 invalidate();
173         }
174 }
175
176 void eListbox::entryRemoved(int index)
177 {
178         if (index == m_selected)
179                 m_selected = m_content->cursorGet();
180
181         moveSelection(justCheck);
182         
183         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
184         {
185                         /* todo, calc exact invalidation... */
186                 invalidate();
187         }
188 }
189
190 void eListbox::entryChanged(int index)
191 {
192         if ((m_top <= index) && (index < (m_top + m_items_per_page)))
193         {
194                 gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
195                 invalidate(inv);
196         }
197 }
198
199 void eListbox::entryReset()
200 {
201         if (m_content)
202                 m_content->cursorHome();
203         m_top = 0;
204         m_selected = 0;
205         invalidate();
206 }