647dbdb9d1400c3147234926304d9af1da6ddf9d
[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 DEFINE_REF(eListboxServiceContent);
34
35 eListboxServiceContent::eListboxServiceContent()
36 {
37         m_size = 0;
38         cursorHome();
39         eServiceCenter::getInstance(m_service_center);
40 }
41
42 void eListboxServiceContent::cursorHome()
43 {
44         m_cursor = m_list.begin();
45         m_cursor_number = 0;
46 }
47
48 void eListboxServiceContent::cursorEnd()
49 {
50         m_cursor = m_list.end();
51         m_cursor_number = m_size;
52 }
53
54 int eListboxServiceContent::cursorMove(int count)
55 {
56         if (count > 0)
57         {
58                 while (count && (m_cursor != m_list.end()))
59                 {
60                         ++m_cursor;
61                         ++m_cursor_number;
62                         --count;
63                 }
64         } else if (count < 0)
65         {
66                 while (count && (m_cursor != m_list.begin()))
67                 {
68                         --m_cursor;
69                         --m_cursor_number;
70                         ++count;
71                 }
72         }
73         
74         return 0;
75 }
76
77 int eListboxServiceContent::cursorValid()
78 {
79         return m_cursor != m_list.end();
80 }
81
82 int eListboxServiceContent::cursorSet(int n)
83 {
84         cursorHome();
85         cursorMove(n);
86         
87         return 0;
88 }
89
90 int eListboxServiceContent::cursorGet()
91 {
92         return m_cursor_number;
93 }
94
95 void eListboxServiceContent::cursorSave()
96 {
97         m_saved_cursor = m_cursor;
98         m_saved_cursor_number = m_cursor_number;
99 }
100
101 void eListboxServiceContent::cursorRestore()
102 {
103         m_cursor = m_saved_cursor;
104         m_cursor_number = m_saved_cursor_number;
105 }
106
107 int eListboxServiceContent::size()
108 {
109         return m_size;
110 }
111         
112 void eListboxServiceContent::setSize(const eSize &size)
113 {
114         m_itemsize = size;
115 }
116
117 void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
118 {
119         ePtr<gFont> fnt = new gFont("Arial", 14);
120         painter.clip(eRect(offset, m_itemsize));
121         style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
122         painter.clear();
123         
124         if (cursorValid())
125         {
126                 painter.setFont(fnt);
127                 
128                 ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
129                 
130                         /* get name of service */
131                 ePtr<iStaticServiceInformation> service_info;
132                 m_service_center->info(*m_cursor, service_info);
133                 std::string name = "<n/a>";
134                 
135                 if (service_info)
136                         service_info->getName(*m_cursor, name);
137                 
138                 painter.renderText(eRect(text_offset, m_itemsize), name);
139                 
140                 if (selected)
141                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
142         }
143         
144         painter.clippop();
145 }
146