add requester slider capabilities: vertical orientation and start offset (untested)
[enigma2.git] / lib / components / listboxepg.cpp
1 #include <lib/components/listboxepg.h>
2 #include <lib/dvb/epgcache.h>
3 #include <lib/service/service.h>
4
5 void eListboxEPGContent::setRoot(const eServiceReference &root)
6 {
7         eEPGCache *epg=NULL;
8         if ( !eEPGCache::getInstance(epg) )
9         {
10                 m_list.clear();
11                 m_root = root;
12
13                 epg->Lock();
14                 if (!epg->startTimeQuery(root))
15                 {
16                         ePtr<eServiceEvent> ptr;
17                         while( !epg->getNextTimeEntry(ptr) )
18                                 m_list.push_back(ptr);
19                 }
20                 else
21                         eDebug("startTimeQuery failed %s", root.toString().c_str());
22                 epg->Unlock();
23
24                 m_size = m_list.size();
25                 cursorHome();
26
27                 if (m_listbox)
28                         m_listbox->entryReset();
29         }
30 }
31
32 void eListboxEPGContent::getCurrent(ePtr<eServiceEvent>& evt)
33 {
34         if (cursorValid())
35                 evt = *m_cursor;
36         else
37                 evt = 0;
38 }
39
40 void eListboxEPGContent::setElementPosition(int element, eRect where)
41 {
42         if ((element >= 0) && (element < celElements))
43                 m_element_position[element] = where;
44 }
45
46 void eListboxEPGContent::setElementFont(int element, gFont *font)
47 {
48         if ((element >= 0) && (element < celElements))
49                 m_element_font[element] = font;
50 }
51
52 void eListboxEPGContent::sort()
53 {
54 #if 0
55         ePtr<iListableService> lst;
56         if (!m_service_center->list(m_root, lst))
57         {
58                 m_list.sort(iListableServiceCompare(lst));
59                         /* FIXME: is this really required or can we somehow keep the current entry? */
60                 cursorHome();
61                 if (m_listbox)
62                         m_listbox->entryReset();
63         }
64 #endif
65 }
66
67 DEFINE_REF(eListboxEPGContent);
68
69 eListboxEPGContent::eListboxEPGContent()
70         :m_size(0)
71 {
72         cursorHome();
73 }
74
75 void eListboxEPGContent::cursorHome()
76 {
77         m_cursor = m_list.begin();
78         m_cursor_number = 0;
79 }
80
81 void eListboxEPGContent::cursorEnd()
82 {
83         m_cursor = m_list.end();
84         m_cursor_number = m_size;
85 }
86
87 int eListboxEPGContent::cursorMove(int count)
88 {
89         list::iterator old = m_cursor;
90
91         if (count > 0)
92         {
93                 while(count && (m_cursor != m_list.end()))
94                 {
95                         ++m_cursor;
96                         ++m_cursor_number;
97                         --count;
98                 }
99         } else if (count < 0)
100         {
101                 while (count && (m_cursor != m_list.begin()))
102                 {
103                         --m_cursor;
104                         --m_cursor_number;
105                         ++count;
106                 }
107         }
108
109         return 0;
110 }
111
112 int eListboxEPGContent::cursorValid()
113 {
114         return m_cursor != m_list.end();
115 }
116
117 int eListboxEPGContent::cursorSet(int n)
118 {
119         cursorHome();
120         cursorMove(n);
121
122         return 0;
123 }
124
125 int eListboxEPGContent::cursorGet()
126 {
127         return m_cursor_number;
128 }
129
130 void eListboxEPGContent::cursorSave()
131 {
132         m_saved_cursor = m_cursor;
133         m_saved_cursor_number = m_cursor_number;
134 }
135
136 void eListboxEPGContent::cursorRestore()
137 {
138         m_cursor = m_saved_cursor;
139         m_cursor_number = m_saved_cursor_number;
140         m_saved_cursor = m_list.end();
141 }
142
143 int eListboxEPGContent::size()
144 {
145         return m_size;
146 }
147
148 void eListboxEPGContent::setSize(const eSize &size)
149 {
150         m_itemsize = size;
151         eSize s = m_itemsize;
152         s.setWidth(size.width()/20*5);
153         m_element_position[celBeginTime] = eRect(ePoint(0, 0), s);
154         m_element_font[celBeginTime] = new gFont("Arial", 20);
155         s.setWidth(size.width()/20*15);
156         m_element_position[celTitle] = eRect(ePoint(size.width()/20*5, 0), s);
157         m_element_font[celTitle] = new gFont("Arial", 20);
158 }
159
160 void eListboxEPGContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
161 {
162         painter.clip(eRect(offset, m_itemsize));
163         style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
164         painter.clear();
165
166         if (cursorValid())
167         {
168                 for (int e = 0; e < celElements; ++e)
169                 {
170                         if (!m_element_font[e])
171                                 continue;
172
173                         painter.setFont(m_element_font[e]);
174
175                         std::string text = "<n/a>";
176
177                         switch (e)
178                         {
179                         case celBeginTime:
180                         {
181                                 tm t;
182                                 localtime_r(&(*m_cursor)->m_begin, &t);
183                                 char tmp[13];
184                                 snprintf(tmp, 13, "%02d.%02d, %02d:%02d",
185                                         t.tm_mday, t.tm_mon+1,
186                                         t.tm_hour, t.tm_min);
187                                 text=tmp;
188                                 break;
189                         }
190                         case celTitle:
191                         {
192                                 text = (*m_cursor)->m_event_name;
193                                 break;
194                         }
195                         }
196                         
197                         eRect area = m_element_position[e];
198                         area.moveBy(offset.x(), offset.y());
199                         
200                         painter.renderText(area, text);
201                 }
202                 
203                 if (selected)
204                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
205         }
206         painter.clippop();
207 }
208