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