938b1795035b1b7911078099dfc8112c3b55e88a
[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::setCurrent(const eServiceReference &ref)
26 {
27         int index=0;
28         for (list::iterator i(m_list.begin()); i != m_list.end(); ++i, ++index)
29                 if ( *i == ref )
30                 {
31                         m_cursor = i;
32                         m_cursor_number = index;
33                         break;
34                 }
35 }
36
37 void eListboxServiceContent::getCurrent(eServiceReference &ref)
38 {
39         if (cursorValid())
40                 ref = *m_cursor;
41         else
42                 ref = eServiceReference();
43 }
44
45 void eListboxServiceContent::initMarked()
46 {
47         m_marked.clear();
48 }
49
50 void eListboxServiceContent::addMarked(const eServiceReference &ref)
51 {
52         m_marked.insert(ref);
53         if (m_listbox)
54                 m_listbox->entryChanged(lookupService(ref));
55 }
56
57 void eListboxServiceContent::removeMarked(const eServiceReference &ref)
58 {
59         m_marked.erase(ref);
60         if (m_listbox)
61                 m_listbox->entryChanged(lookupService(ref));
62 }
63
64 int eListboxServiceContent::isMarked(const eServiceReference &ref)
65 {
66         return m_marked.find(ref) != m_marked.end();
67 }
68
69 void eListboxServiceContent::markedQueryStart()
70 {
71         m_marked_iterator = m_marked.begin();
72 }
73
74 int eListboxServiceContent::markedQueryNext(eServiceReference &ref)
75 {
76         if (m_marked_iterator == m_marked.end())
77                 return -1;
78         ref = *m_marked_iterator++;
79         return 0;
80 }
81
82 int eListboxServiceContent::lookupService(const eServiceReference &ref)
83 {
84                 /* shortcut for cursor */
85         if (ref == *m_cursor)
86                 return m_cursor_number;
87                 /* otherwise, search in the list.. */
88         int index = 0;
89         for (list::const_iterator i(m_list.begin()); i != m_list.end(); ++i, ++index);
90         
91                 /* this is ok even when the index was not found. */
92         return index;
93 }
94
95 void eListboxServiceContent::setVisualMode(int mode)
96 {
97         m_visual_mode = mode;
98         
99         if (m_visual_mode == visModeSimple)
100         {
101                 m_element_position[celServiceName] = eRect(ePoint(0, 0), m_itemsize);
102                 m_element_font[celServiceName] = new gFont("Arial", 23);
103                 m_element_position[celServiceNumber] = eRect();
104                 m_element_font[celServiceNumber] = 0;
105                 m_element_position[celIcon] = eRect();
106                 m_element_position[celServiceInfo] = eRect();
107                 m_element_font[celServiceInfo] = 0;
108         }
109 }
110
111 void eListboxServiceContent::setElementPosition(int element, eRect where)
112 {
113         if ((element >= 0) && (element < celElements))
114                 m_element_position[element] = where;
115 }
116
117 void eListboxServiceContent::setElementFont(int element, gFont *font)
118 {
119         if ((element >= 0) && (element < celElements))
120                 m_element_font[element] = font;
121 }
122
123 void eListboxServiceContent::sort()
124 {
125         ePtr<iListableService> lst;
126   if (!m_service_center->list(m_root, lst))
127   {
128                 m_list.sort(iListableServiceCompare(lst));
129                         /* FIXME: is this really required or can we somehow keep the current entry? */
130                 cursorHome();
131                 if (m_listbox)
132                         m_listbox->entryReset();
133         }
134 }
135
136 DEFINE_REF(eListboxServiceContent);
137
138 eListboxServiceContent::eListboxServiceContent()
139         :m_visual_mode(visModeSimple), m_size(0), m_current_marked(false), m_swap(m_list.end()), m_numberoffset(0)
140 {
141         cursorHome();
142         eServiceCenter::getInstance(m_service_center);
143 }
144
145 void eListboxServiceContent::cursorHome()
146 {
147         list::iterator old = m_cursor;
148
149         m_cursor = m_list.begin();
150         m_cursor_number = 0;
151
152         if ( m_current_marked && m_saved_cursor == m_list.end() )
153                 std::iter_swap( old, m_cursor );
154 }
155
156 void eListboxServiceContent::cursorEnd()
157 {
158         if ( m_current_marked && m_saved_cursor == m_list.end() && m_cursor != m_list.end() )
159                 m_swap = m_cursor;
160         m_cursor = m_list.end();
161         m_cursor_number = m_size;
162 }
163
164 int eListboxServiceContent::setCurrentMarked(bool state)
165 {
166         bool prev = m_current_marked;
167         m_current_marked = state;
168
169         if (state != prev && m_listbox)
170         {
171                 m_listbox->entryChanged(m_cursor_number);
172                 if (!state)
173                 {
174                         ePtr<iListableService> lst;
175                         if (m_service_center->list(m_root, lst))
176                                 eDebug("no list available!");
177                         else
178                         {
179                                 ePtr<iMutableServiceList> list;
180                                 if (lst->startEdit(list))
181                                         eDebug("no editable list");
182                                 else
183                                 {
184                                         eServiceReference ref;
185                                         getCurrent(ref);
186                                         if(!ref)
187                                                 eDebug("no valid service selected");
188                                         else
189                                         {
190                                                 int pos = cursorGet();
191                                                 eDebugNoNewLine("move %s to %d ", ref.toString().c_str(), pos);
192                                                 if (list->moveService(ref, cursorGet()))
193                                                         eDebug("failed");
194                                                 else
195                                                         eDebug("ok");
196                                         }
197                                 }
198                         }
199                 }
200         }
201
202         return 0;
203 }
204
205 int eListboxServiceContent::cursorMove(int count)
206 {
207         list::iterator old = m_cursor;
208
209         if (count > 0)
210         {
211                 while(count && (m_cursor != m_list.end()))
212                 {
213                         ++m_cursor;
214                         ++m_cursor_number;
215                         --count;
216                 }
217         } else if (count < 0)
218         {
219                 while (count && (m_cursor != m_list.begin()))
220                 {
221                         --m_cursor;
222                         --m_cursor_number;
223                         ++count;
224                 }
225         }
226
227         if ( m_current_marked && m_saved_cursor == m_list.end() )
228         {
229                 if ( m_cursor == m_list.end() )
230                         m_swap = old;
231                 else if ( old == m_list.end() )
232                 {
233                         std::iter_swap( m_swap, m_cursor );
234                         m_swap = m_list.end();
235                 }
236                 else
237                         std::iter_swap( old, m_cursor );
238         }
239
240         return 0;
241 }
242
243 int eListboxServiceContent::cursorValid()
244 {
245         return m_cursor != m_list.end();
246 }
247
248 int eListboxServiceContent::cursorSet(int n)
249 {
250         cursorHome();
251         cursorMove(n);
252         
253         return 0;
254 }
255
256 int eListboxServiceContent::cursorGet()
257 {
258         return m_cursor_number;
259 }
260
261 void eListboxServiceContent::cursorSave()
262 {
263         m_saved_cursor = m_cursor;
264         m_saved_cursor_number = m_cursor_number;
265 }
266
267 void eListboxServiceContent::cursorRestore()
268 {
269         m_cursor = m_saved_cursor;
270         m_cursor_number = m_saved_cursor_number;
271         m_saved_cursor = m_list.end();
272 }
273
274 int eListboxServiceContent::size()
275 {
276         return m_size;
277 }
278         
279 void eListboxServiceContent::setSize(const eSize &size)
280 {
281         m_itemsize = size;
282         setVisualMode(m_visual_mode);
283 }
284
285 void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
286 {
287         painter.clip(eRect(offset, m_itemsize));
288
289         if (m_current_marked && selected)
290                 style.setStyle(painter, eWindowStyle::styleListboxMarked);
291         else if (cursorValid() && isMarked(*m_cursor))
292                 style.setStyle(painter, eWindowStyle::styleListboxMarked);
293         else
294                 style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
295         painter.clear();
296         
297         if (cursorValid())
298         {
299                         /* get service information */
300                 ePtr<iStaticServiceInformation> service_info;
301                 m_service_center->info(*m_cursor, service_info);
302                 
303                 for (int e = 0; e < celElements; ++e)
304                 {
305                         if (!m_element_font[e])
306                                 continue;
307                         painter.setFont(m_element_font[e]);
308                         
309                         std::string text = "<n/a>";
310                         
311                         switch (e)
312                         {
313                         case celServiceName:
314                         {
315                                 if (service_info)
316                                         service_info->getName(*m_cursor, text);
317                                 break;
318                         }
319                         case celServiceNumber:
320                         {
321                                 char bla[10];
322                                 sprintf(bla, "%d", m_numberoffset + m_cursor_number + 1);
323                                 text = bla;
324                                 break;
325                         }
326                         case celServiceInfo:
327                         {
328                                 text = "now&next";
329                                 break;
330                         }
331                         case celIcon:
332                                 continue;
333                         }
334                         
335                         eRect area = m_element_position[e];
336                         area.moveBy(offset.x(), offset.y());
337                         
338                         painter.renderText(area, text);
339                 }
340                 
341                 if (selected)
342                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
343         }
344         
345         painter.clippop();
346 }
347