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