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