db4f6b86d990734f81a1f538944abb364b8eb58e
[openblackhole/openblackhole-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 int eListboxServiceContent::lookupService(const eServiceReference &ref)
58 {
59                 /* shortcut for cursor */
60         if (ref == *m_cursor)
61                 return m_cursor_number;
62                 /* otherwise, search in the list.. */
63         int index = 0;
64         for (list::const_iterator i(m_list.begin()); i != m_list.end(); ++i, ++index);
65         
66                 /* this is ok even when the index was not found. */
67         return index;
68 }
69
70 DEFINE_REF(eListboxServiceContent);
71
72 eListboxServiceContent::eListboxServiceContent()
73 {
74         m_size = 0;
75         cursorHome();
76         eServiceCenter::getInstance(m_service_center);
77 }
78
79 void eListboxServiceContent::cursorHome()
80 {
81         m_cursor = m_list.begin();
82         m_cursor_number = 0;
83 }
84
85 void eListboxServiceContent::cursorEnd()
86 {
87         m_cursor = m_list.end();
88         m_cursor_number = m_size;
89 }
90
91 int eListboxServiceContent::cursorMove(int count)
92 {
93         if (count > 0)
94         {
95                 while (count && (m_cursor != m_list.end()))
96                 {
97                         ++m_cursor;
98                         ++m_cursor_number;
99                         --count;
100                 }
101         } else if (count < 0)
102         {
103                 while (count && (m_cursor != m_list.begin()))
104                 {
105                         --m_cursor;
106                         --m_cursor_number;
107                         ++count;
108                 }
109         }
110         
111         return 0;
112 }
113
114 int eListboxServiceContent::cursorValid()
115 {
116         return m_cursor != m_list.end();
117 }
118
119 int eListboxServiceContent::cursorSet(int n)
120 {
121         cursorHome();
122         cursorMove(n);
123         
124         return 0;
125 }
126
127 int eListboxServiceContent::cursorGet()
128 {
129         return m_cursor_number;
130 }
131
132 void eListboxServiceContent::cursorSave()
133 {
134         m_saved_cursor = m_cursor;
135         m_saved_cursor_number = m_cursor_number;
136 }
137
138 void eListboxServiceContent::cursorRestore()
139 {
140         m_cursor = m_saved_cursor;
141         m_cursor_number = m_saved_cursor_number;
142 }
143
144 int eListboxServiceContent::size()
145 {
146         return m_size;
147 }
148         
149 void eListboxServiceContent::setSize(const eSize &size)
150 {
151         m_itemsize = size;
152 }
153
154 void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
155 {
156         ePtr<gFont> fnt = new gFont("Arial", 14);
157         painter.clip(eRect(offset, m_itemsize));
158         if (cursorValid() && isMarked(*m_cursor))
159                 style.setStyle(painter, eWindowStyle::styleListboxMarked);
160         else
161                 style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
162         painter.clear();
163         
164         if (cursorValid())
165         {
166                 painter.setFont(fnt);
167                 
168                 ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
169                 
170                         /* get name of service */
171                 ePtr<iStaticServiceInformation> service_info;
172                 m_service_center->info(*m_cursor, service_info);
173                 std::string name = "<n/a>";
174                 
175                 if (service_info)
176                         service_info->getName(*m_cursor, name);
177                 
178                 painter.renderText(eRect(text_offset, m_itemsize), name);
179                 
180                 if (selected)
181                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
182         }
183         
184         painter.clippop();
185 }
186