d4cbff208a0da0f1f8077a9762156710d58dcb2a
[enigma2.git] / lib / service / listboxservice.cpp
1 #include <lib/service/listboxservice.h>
2 #include <lib/service/service.h>
3
4 void eListboxServiceContent::setRoot(const eServiceReference &root)
5 {
6         m_list.clear();
7         m_root = root;
8         
9         assert(m_service_center);
10         
11         ePtr<iListableService> lst;
12         if (m_service_center->list(m_root, lst))
13                 eDebug("no list available!");
14         else
15                 if (lst->getContent(m_list))
16                         eDebug("getContent failed");
17
18         m_size = m_list.size();
19         cursorHome();
20         
21         if (m_listbox)
22                 m_listbox->entryReset();
23 }
24
25 void eListboxServiceContent::getCurrent(eServiceReference &ref)
26 {
27         if (cursorValid())
28                 ref = *m_cursor;
29         else
30                 ref = eServiceReference();
31 }
32
33 void eListboxServiceContent::initMarked()
34 {
35         m_marked.clear();
36 }
37
38 void eListboxServiceContent::addMarked(const eServiceReference &ref)
39 {
40         m_marked.insert(ref);
41         if (m_listbox)
42                 m_listbox->entryChanged(lookupService(ref));
43 }
44
45 void eListboxServiceContent::removeMarked(const eServiceReference &ref)
46 {
47         m_marked.erase(ref);
48         if (m_listbox)
49                 m_listbox->entryChanged(lookupService(ref));
50 }
51
52 int eListboxServiceContent::isMarked(const eServiceReference &ref)
53 {
54         return m_marked.find(ref) != m_marked.end();
55 }
56
57 void eListboxServiceContent::markedQueryStart()
58 {
59         m_marked_iterator = m_marked.begin();
60 }
61
62 int eListboxServiceContent::markedQueryNext(eServiceReference &ref)
63 {
64         if (m_marked_iterator == m_marked.end())
65                 return -1;
66         ref = *m_marked_iterator++;
67         return 0;
68 }
69
70 int eListboxServiceContent::lookupService(const eServiceReference &ref)
71 {
72                 /* shortcut for cursor */
73         if (ref == *m_cursor)
74                 return m_cursor_number;
75                 /* otherwise, search in the list.. */
76         int index = 0;
77         for (list::const_iterator i(m_list.begin()); i != m_list.end(); ++i, ++index);
78         
79                 /* this is ok even when the index was not found. */
80         return index;
81 }
82
83 void eListboxServiceContent::setVisualMode(int mode)
84 {
85         m_visual_mode = mode;
86         
87         if (m_visual_mode == visModeSimple)
88         {
89                 m_element_position[celServiceName] = eRect(ePoint(0, 0), m_itemsize);
90                 m_element_font[celServiceName] = new gFont("Arial", 14);
91                 m_element_position[celServiceNumber] = eRect();
92                 m_element_font[celServiceNumber] = 0;
93                 m_element_position[celIcon] = eRect();
94                 m_element_position[celServiceInfo] = eRect();
95                 m_element_font[celServiceInfo] = 0;
96         }
97 }
98
99 void eListboxServiceContent::setElementPosition(int element, eRect where)
100 {
101         if ((element >= 0) && (element < celElements))
102                 m_element_position[element] = where;
103 }
104
105 void eListboxServiceContent::setElementFont(int element, gFont *font)
106 {
107         if ((element >= 0) && (element < celElements))
108                 m_element_font[element] = font;
109 }
110
111 void eListboxServiceContent::sort()
112 {
113         ePtr<iListableService> lst;
114   if (!m_service_center->list(m_root, lst))
115   {
116                 m_list.sort(iListableServiceCompare(lst));
117                         /* FIXME: is this really required or can we somehow keep the current entry? */
118                 cursorHome();
119                 if (m_listbox)
120                         m_listbox->entryReset();
121         }
122 }
123
124 DEFINE_REF(eListboxServiceContent);
125
126 eListboxServiceContent::eListboxServiceContent()
127 {
128         m_visual_mode = visModeSimple;
129         m_size = 0;
130         cursorHome();
131         eServiceCenter::getInstance(m_service_center);
132 }
133
134 void eListboxServiceContent::cursorHome()
135 {
136         m_cursor = m_list.begin();
137         m_cursor_number = 0;
138 }
139
140 void eListboxServiceContent::cursorEnd()
141 {
142         m_cursor = m_list.end();
143         m_cursor_number = m_size;
144 }
145
146 int eListboxServiceContent::cursorMove(int count)
147 {
148         if (count > 0)
149         {
150                 while (count && (m_cursor != m_list.end()))
151                 {
152                         ++m_cursor;
153                         ++m_cursor_number;
154                         --count;
155                 }
156         } else if (count < 0)
157         {
158                 while (count && (m_cursor != m_list.begin()))
159                 {
160                         --m_cursor;
161                         --m_cursor_number;
162                         ++count;
163                 }
164         }
165         
166         return 0;
167 }
168
169 int eListboxServiceContent::cursorValid()
170 {
171         return m_cursor != m_list.end();
172 }
173
174 int eListboxServiceContent::cursorSet(int n)
175 {
176         cursorHome();
177         cursorMove(n);
178         
179         return 0;
180 }
181
182 int eListboxServiceContent::cursorGet()
183 {
184         return m_cursor_number;
185 }
186
187 void eListboxServiceContent::cursorSave()
188 {
189         m_saved_cursor = m_cursor;
190         m_saved_cursor_number = m_cursor_number;
191 }
192
193 void eListboxServiceContent::cursorRestore()
194 {
195         m_cursor = m_saved_cursor;
196         m_cursor_number = m_saved_cursor_number;
197 }
198
199 int eListboxServiceContent::size()
200 {
201         return m_size;
202 }
203         
204 void eListboxServiceContent::setSize(const eSize &size)
205 {
206         m_itemsize = size;
207         setVisualMode(m_visual_mode);
208 }
209
210 void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
211 {
212         painter.clip(eRect(offset, m_itemsize));
213         if (cursorValid() && isMarked(*m_cursor))
214                 style.setStyle(painter, eWindowStyle::styleListboxMarked);
215         else
216                 style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
217         painter.clear();
218         
219         if (cursorValid())
220         {
221                         /* get service information */
222                 ePtr<iStaticServiceInformation> service_info;
223                 m_service_center->info(*m_cursor, service_info);
224                 
225                 for (int e = 0; e < celElements; ++e)
226                 {
227                         if (!m_element_font[e])
228                                 continue;
229                         painter.setFont(m_element_font[e]);
230                         
231                         std::string text = "<n/a>";
232                         
233                         switch (e)
234                         {
235                         case celServiceName:
236                         {
237                                 if (service_info)
238                                         service_info->getName(*m_cursor, text);
239                                 break;
240                         }
241                         case celServiceNumber:
242                         {
243                                 char bla[10];
244                                 sprintf(bla, "%d", m_cursor_number + 1);
245                                 text = bla;
246                                 break;
247                         }
248                         case celServiceInfo:
249                         {
250                                 text = "now&next";
251                                 break;
252                         }
253                         case celIcon:
254                                 continue;
255                         }
256                         
257                         eRect area = m_element_position[e];
258                         area.moveBy(offset.x(), offset.y());
259                         
260                         painter.renderText(area, text);
261                 }
262                 
263                 if (selected)
264                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
265         }
266         
267         painter.clippop();
268 }
269