use green to add a timer directly from the epgSelection
[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         :m_visual_mode(visModeSimple), m_size(0), m_current_marked(false), m_swap(m_list.end())
128 {
129         cursorHome();
130         eServiceCenter::getInstance(m_service_center);
131 }
132
133 void eListboxServiceContent::cursorHome()
134 {
135         list::iterator old = m_cursor;
136
137         m_cursor = m_list.begin();
138         m_cursor_number = 0;
139
140         if ( m_current_marked && m_saved_cursor == m_list.end() )
141                 std::iter_swap( old, m_cursor );
142 }
143
144 void eListboxServiceContent::cursorEnd()
145 {
146         if ( m_current_marked && m_saved_cursor == m_list.end() && m_cursor != m_list.end() )
147                 m_swap = m_cursor;
148         m_cursor = m_list.end();
149         m_cursor_number = m_size;
150 }
151
152 int eListboxServiceContent::setCurrentMarked(bool state)
153 {
154         bool prev = m_current_marked;
155         m_current_marked = state;
156
157         if (state != prev && m_listbox)
158                 m_listbox->entryChanged(m_cursor_number);
159
160         return 0;
161 }
162
163 int eListboxServiceContent::cursorMove(int count)
164 {
165         list::iterator old = m_cursor;
166
167         if (count > 0)
168         {
169                 while(count && (m_cursor != m_list.end()))
170                 {
171                         ++m_cursor;
172                         ++m_cursor_number;
173                         --count;
174                 }
175         } else if (count < 0)
176         {
177                 while (count && (m_cursor != m_list.begin()))
178                 {
179                         --m_cursor;
180                         --m_cursor_number;
181                         ++count;
182                 }
183         }
184
185         if ( m_current_marked && m_saved_cursor == m_list.end() )
186         {
187                 if ( m_cursor == m_list.end() )
188                         m_swap = old;
189                 else if ( old == m_list.end() )
190                 {
191                         std::iter_swap( m_swap, m_cursor );
192                         m_swap = m_list.end();
193                 }
194                 else
195                         std::iter_swap( old, m_cursor );
196         }
197
198         return 0;
199 }
200
201 int eListboxServiceContent::cursorValid()
202 {
203         return m_cursor != m_list.end();
204 }
205
206 int eListboxServiceContent::cursorSet(int n)
207 {
208         cursorHome();
209         cursorMove(n);
210         
211         return 0;
212 }
213
214 int eListboxServiceContent::cursorGet()
215 {
216         return m_cursor_number;
217 }
218
219 void eListboxServiceContent::cursorSave()
220 {
221         m_saved_cursor = m_cursor;
222         m_saved_cursor_number = m_cursor_number;
223 }
224
225 void eListboxServiceContent::cursorRestore()
226 {
227         m_cursor = m_saved_cursor;
228         m_cursor_number = m_saved_cursor_number;
229         m_saved_cursor = m_list.end();
230 }
231
232 int eListboxServiceContent::size()
233 {
234         return m_size;
235 }
236         
237 void eListboxServiceContent::setSize(const eSize &size)
238 {
239         m_itemsize = size;
240         setVisualMode(m_visual_mode);
241 }
242
243 void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
244 {
245         painter.clip(eRect(offset, m_itemsize));
246
247         if (m_current_marked && selected)
248                 style.setStyle(painter, eWindowStyle::styleListboxMarked);
249         else if (cursorValid() && isMarked(*m_cursor))
250                 style.setStyle(painter, eWindowStyle::styleListboxMarked);
251         else
252                 style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
253         painter.clear();
254         
255         if (cursorValid())
256         {
257                         /* get service information */
258                 ePtr<iStaticServiceInformation> service_info;
259                 m_service_center->info(*m_cursor, service_info);
260                 
261                 for (int e = 0; e < celElements; ++e)
262                 {
263                         if (!m_element_font[e])
264                                 continue;
265                         painter.setFont(m_element_font[e]);
266                         
267                         std::string text = "<n/a>";
268                         
269                         switch (e)
270                         {
271                         case celServiceName:
272                         {
273                                 if (service_info)
274                                         service_info->getName(*m_cursor, text);
275                                 break;
276                         }
277                         case celServiceNumber:
278                         {
279                                 char bla[10];
280                                 sprintf(bla, "%d", m_cursor_number + 1);
281                                 text = bla;
282                                 break;
283                         }
284                         case celServiceInfo:
285                         {
286                                 text = "now&next";
287                                 break;
288                         }
289                         case celIcon:
290                                 continue;
291                         }
292                         
293                         eRect area = m_element_position[e];
294                         area.moveBy(offset.x(), offset.y());
295                         
296                         painter.renderText(area, text);
297                 }
298                 
299                 if (selected)
300                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
301         }
302         
303         painter.clippop();
304 }
305