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