- fixed lib/Makefile.am for "components" directory
[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_root = root;
7         
8         assert(m_service_center);
9         
10         ePtr<iListableService> lst;
11         if (m_service_center->list(m_root, lst))
12                 eDebug("no list available!");
13         else
14                 if (lst->getContent(m_list))
15                         eDebug("getContent failed");
16
17         m_size = m_list.size();
18         cursorHome();
19 }
20
21 DEFINE_REF(eListboxServiceContent);
22
23 eListboxServiceContent::eListboxServiceContent()
24 {
25         m_size = 0;
26         cursorHome();
27         eServiceCenter::getInstance(m_service_center);
28 }
29
30 void eListboxServiceContent::cursorHome()
31 {
32         m_cursor = m_list.begin();
33         m_cursor_number = 0;
34 }
35
36 void eListboxServiceContent::cursorEnd()
37 {
38         m_cursor = m_list.end();
39         m_cursor_number = m_size;
40 }
41
42 int eListboxServiceContent::cursorMove(int count)
43 {
44         if (count > 0)
45         {
46                 while (count && (m_cursor != m_list.end()))
47                 {
48                         ++m_cursor;
49                         ++m_cursor_number;
50                         --count;
51                 }
52         } else if (count < 0)
53         {
54                 while (count && (m_cursor != m_list.begin()))
55                 {
56                         --m_cursor;
57                         --m_cursor_number;
58                         ++count;
59                 }
60         }
61         
62         return 0;
63 }
64
65 int eListboxServiceContent::cursorValid()
66 {
67         return m_cursor != m_list.end();
68 }
69
70 int eListboxServiceContent::cursorSet(int n)
71 {
72         cursorHome();
73         cursorMove(n);
74         
75         return 0;
76 }
77
78 int eListboxServiceContent::cursorGet()
79 {
80         return m_cursor_number;
81 }
82
83 void eListboxServiceContent::cursorSave()
84 {
85         m_saved_cursor = m_cursor;
86         m_saved_cursor_number = m_cursor_number;
87 }
88
89 void eListboxServiceContent::cursorRestore()
90 {
91         m_cursor = m_saved_cursor;
92         m_cursor_number = m_saved_cursor_number;
93 }
94
95 int eListboxServiceContent::size()
96 {
97         return m_size;
98 }
99         
100 void eListboxServiceContent::setSize(const eSize &size)
101 {
102         m_itemsize = size;
103 }
104
105 void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
106 {
107         ePtr<gFont> fnt = new gFont("Arial", 14);
108         painter.clip(eRect(offset, m_itemsize));
109         style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
110         painter.clear();
111         
112         if (cursorValid())
113         {
114                 painter.setFont(fnt);
115                 
116                 ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
117                 
118                         /* get name of service */
119                 ePtr<iStaticServiceInformation> service_info;
120                 m_service_center->info(*m_cursor, service_info);
121                 std::string name = "<n/a>";
122                 
123                 if (service_info)
124                         service_info->getName(*m_cursor, name);
125                 
126                 painter.renderText(eRect(text_offset, m_itemsize), name);
127                 
128                 if (selected)
129                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
130         }
131         
132         painter.clippop();
133 }
134