- beautified network cursor, added 'selected' argument to __call__
[enigma2.git] / lib / gui / elistboxcontent.cpp
1 #include <lib/gui/elistbox.h>
2 #include <lib/gui/elistboxcontent.h>
3 #include <lib/gdi/font.h>
4 #include <Python.h>
5
6 /*
7     The basic idea is to have an interface which gives all relevant list
8     processing functions, and can be used by the listbox to browse trough
9     the list.
10     
11     The listbox directly uses the implemented cursor. It tries hard to avoid
12     iterating trough the (possibly very large) list, so it should be O(1),
13     i.e. the performance should not be influenced by the size of the list.
14     
15     The list interface knows how to draw the current entry to a specified 
16     offset. Different interfaces can be used to adapt different lists,
17     pre-filter lists on the fly etc.
18     
19                 cursorSave/Restore is used to avoid re-iterating the list on redraw.
20                 The current selection is always selected as cursor position, the
21     cursor is then positioned to the start, and then iterated. This gives
22     at most 2x m_items_per_page cursor movements per redraw, indepenent
23     of the size of the list.
24     
25     Although cursorSet is provided, it should be only used when there is no
26     other way, as it involves iterating trough the list.
27  */
28
29 iListboxContent::~iListboxContent()
30 {
31 }
32
33 iListboxContent::iListboxContent(): m_listbox(0)
34 {
35 }
36
37 void iListboxContent::setListbox(eListbox *lb)
38 {
39         m_listbox = lb;
40 }
41
42 DEFINE_REF(eListboxTestContent);
43
44 void eListboxTestContent::cursorHome()
45 {
46         m_cursor = 0;
47 }
48
49 void eListboxTestContent::cursorEnd()
50 {
51         m_cursor = size();
52 }
53
54 int eListboxTestContent::cursorMove(int count)
55 {
56         m_cursor += count;
57         
58         if (m_cursor < 0)
59                 cursorHome();
60         else if (m_cursor > size())
61                 cursorEnd();
62         return 0;
63 }
64
65 int eListboxTestContent::cursorValid()
66 {
67         return m_cursor < size();
68 }
69
70 int eListboxTestContent::cursorSet(int n)
71 {
72         m_cursor = n;
73         
74         if (m_cursor < 0)
75                 cursorHome();
76         else if (m_cursor > size())
77                 cursorEnd();
78         return 0;
79 }
80
81 int eListboxTestContent::cursorGet()
82 {
83         return m_cursor;
84 }
85
86 void eListboxTestContent::cursorSave()
87 {
88         m_saved_cursor = m_cursor;
89 }
90
91 void eListboxTestContent::cursorRestore()
92 {
93         m_cursor = m_saved_cursor;
94 }
95
96 int eListboxTestContent::size()
97 {
98         return 10;
99 }
100         
101 RESULT eListboxTestContent::connectItemChanged(const Slot0<void> &itemChanged, ePtr<eConnection> &connection)
102 {
103         return 0;
104 }
105
106 void eListboxTestContent::setSize(const eSize &size)
107 {
108         m_size = size;
109 }
110
111 void eListboxTestContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
112 {
113         ePtr<gFont> fnt = new gFont("Arial", 14);
114         painter.clip(eRect(offset, m_size));
115         style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
116         painter.clear();
117
118         if (cursorValid())
119         {
120                 painter.setFont(fnt);
121                 char string[10];
122                 sprintf(string, "%d.)", m_cursor);
123                 
124                 ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
125                 
126                 painter.renderText(eRect(text_offset, m_size), string);
127                 
128                 if (selected)
129                         style.drawFrame(painter, eRect(offset, m_size), eWindowStyle::frameListboxEntry);
130         }
131         
132         painter.clippop();
133 }
134
135 //////////////////////////////////////
136
137 DEFINE_REF(eListboxStringContent);
138
139 eListboxStringContent::eListboxStringContent()
140 {
141         m_size = 0;
142         cursorHome();
143 }
144
145 void eListboxStringContent::cursorHome()
146 {
147         m_cursor = m_list.begin();
148         m_cursor_number = 0;
149 }
150
151 void eListboxStringContent::cursorEnd()
152 {
153         m_cursor = m_list.end();
154         m_cursor_number = m_size;
155 }
156
157 int eListboxStringContent::cursorMove(int count)
158 {
159         if (count > 0)
160         {
161                 while (count && (m_cursor != m_list.end()))
162                 {
163                         ++m_cursor;
164                         ++m_cursor_number;
165                         --count;
166                 }
167         } else if (count < 0)
168         {
169                 while (count && (m_cursor != m_list.begin()))
170                 {
171                         --m_cursor;
172                         --m_cursor_number;
173                         ++count;
174                 }
175         }
176         
177         return 0;
178 }
179
180 int eListboxStringContent::cursorValid()
181 {
182         return m_cursor != m_list.end();
183 }
184
185 int eListboxStringContent::cursorSet(int n)
186 {
187         cursorHome();
188         cursorMove(n);
189         
190         return 0;
191 }
192
193 int eListboxStringContent::cursorGet()
194 {
195         return m_cursor_number;
196 }
197
198 void eListboxStringContent::cursorSave()
199 {
200         m_saved_cursor = m_cursor;
201         m_saved_cursor_number = m_cursor_number;
202 }
203
204 void eListboxStringContent::cursorRestore()
205 {
206         m_cursor = m_saved_cursor;
207         m_cursor_number = m_saved_cursor_number;
208 }
209
210 int eListboxStringContent::size()
211 {
212         return m_size;
213 }
214         
215 void eListboxStringContent::setSize(const eSize &size)
216 {
217         m_itemsize = size;
218 }
219
220 void eListboxStringContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
221 {
222         ePtr<gFont> fnt = new gFont("Arial", 14);
223         painter.clip(eRect(offset, m_itemsize));
224         style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
225         painter.clear();
226         
227         eDebug("item %d", m_cursor_number);
228         if (cursorValid())
229         {
230                 eDebug("is valid..");
231                 painter.setFont(fnt);
232                 
233                 ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
234                 
235                 painter.renderText(eRect(text_offset, m_itemsize), *m_cursor);
236                 
237                 if (selected)
238                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
239         }
240         
241         painter.clippop();
242 }
243
244 void eListboxStringContent::setList(std::list<std::string> &list)
245 {
246         m_list = list;
247         m_size = list.size();
248         cursorHome();
249 }
250
251 //////////////////////////////////////
252
253 DEFINE_REF(eListboxPythonStringContent);
254
255 eListboxPythonStringContent::eListboxPythonStringContent()
256 {
257         m_list = 0;
258 }
259
260 eListboxPythonStringContent::~eListboxPythonStringContent()
261 {
262 }
263
264 void eListboxPythonStringContent::cursorHome()
265 {
266         m_cursor = 0;
267 }
268
269 void eListboxPythonStringContent::cursorEnd()
270 {
271         m_cursor = size();
272 }
273
274 int eListboxPythonStringContent::cursorMove(int count)
275 {
276         m_cursor += count;
277         
278         if (m_cursor < 0)
279                 cursorHome();
280         else if (m_cursor > size())
281                 cursorEnd();
282         return 0;
283 }
284
285 int eListboxPythonStringContent::cursorValid()
286 {
287         return m_cursor < size();
288 }
289
290 int eListboxPythonStringContent::cursorSet(int n)
291 {
292         m_cursor = n;
293         
294         if (m_cursor < 0)
295                 cursorHome();
296         else if (m_cursor > size())
297                 cursorEnd();
298         return 0;
299 }
300
301 int eListboxPythonStringContent::cursorGet()
302 {
303         return m_cursor;
304 }
305
306 void eListboxPythonStringContent::cursorSave()
307 {
308         m_saved_cursor = m_cursor;
309 }
310
311 void eListboxPythonStringContent::cursorRestore()
312 {
313         m_cursor = m_saved_cursor;
314 }
315
316 int eListboxPythonStringContent::size()
317 {
318         if (!m_list)
319                 return 0;
320         return PyList_Size(m_list);
321 }
322         
323 void eListboxPythonStringContent::setSize(const eSize &size)
324 {
325         m_itemsize = size;
326 }
327
328 void eListboxPythonStringContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
329 {
330         ePtr<gFont> fnt = new gFont("Arial", 14);
331         painter.clip(eRect(offset, m_itemsize));
332         style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
333         painter.clear();
334
335         if (m_list && cursorValid())
336         {
337                 PyObject *item = PyList_GetItem(m_list, m_cursor); // borrowed reference!
338                 painter.setFont(fnt);
339
340                         /* the user can supply tuples, in this case the first one will be displayed. */         
341                 if (PyTuple_Check(item))
342                         item = PyTuple_GetItem(item, 0);
343                 
344                 const char *string = PyString_Check(item) ? PyString_AsString(item) : "<not-a-string>";
345                 
346                 ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
347                 
348                 painter.renderText(eRect(text_offset, m_itemsize), string);
349                 
350                 if (selected)
351                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
352         }
353         
354         painter.clippop();
355 }
356
357 void eListboxPythonStringContent::setList(PyObject *list)
358 {
359         Py_XDECREF(m_list);
360         if (!PyList_Check(list))
361         {
362                 m_list = 0;
363         } else
364         {
365                 m_list = list;
366                 Py_INCREF(m_list);
367         }
368 }
369
370 PyObject *eListboxPythonStringContent::getCurrentSelection()
371 {
372         if (!m_list)
373                 return 0;
374         if (!cursorValid())
375                 return 0;
376         PyObject *r = PyList_GetItem(m_list, m_cursor);
377         Py_XINCREF(r);
378         return r;
379 }
380
381 void eListboxPythonStringContent::invalidateEntry(int index)
382 {
383         if (m_listbox)
384                 m_listbox->entryChanged(index);
385 }
386
387 //////////////////////////////////////
388
389 void eListboxPythonConfigContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
390 {
391         ePtr<gFont> fnt = new gFont("Arial", 14);
392         ePtr<gFont> fnt2 = new gFont("Arial", 16);
393         painter.clip(eRect(offset, m_itemsize));
394         style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
395         painter.clear();
396
397         if (m_list && cursorValid())
398         {
399                         /* get current list item */
400                 PyObject *item = PyList_GetItem(m_list, m_cursor); // borrowed reference!
401                 PyObject *text = 0, *value = 0;
402                 painter.setFont(fnt);
403
404                         /* the first tuple element is a string for the left side.
405                            the second one will be called, and the result shall be an tuple.
406                            
407                            of this tuple,
408                            the first one is the type (string).
409                            the second one is the value. */
410                 if (PyTuple_Check(item))
411                 {
412                                 /* handle left part. get item from tuple, convert to string, display. */
413                                 
414                         text = PyTuple_GetItem(item, 0);
415                         text = PyObject_Str(text);
416                         const char *string = (text && PyString_Check(text)) ? PyString_AsString(text) : "<not-a-string>";
417                         eSize item_left = eSize(m_seperation, m_itemsize.height());
418                         eSize item_right = eSize(m_itemsize.width() - m_seperation, m_itemsize.height());
419                         painter.renderText(eRect(offset, item_left), string, gPainter::RT_HALIGN_LEFT);
420                         Py_XDECREF(text);
421                         
422                                 /* now, handle the value. get 2nd part from tuple*/
423                         value = PyTuple_GetItem(item, 1);
424                         if (value)
425                         {
426                                 PyObject *args = PyTuple_New(1);
427                                 PyTuple_SetItem(args, 0, PyInt_FromLong(selected));
428                                 
429                                         /* CallObject will call __call__ which should return the value tuple */
430                                 value = PyObject_CallObject(value, args);
431
432                                 Py_DECREF(args);
433                                         /* the PyInt was stolen. */
434                         }
435                         
436                                 /*  check if this is really a tuple */
437                         if (PyTuple_Check(value))
438                         {
439                                         /* convert type to string */
440                                 PyObject *type = PyTuple_GetItem(value, 0);
441                                 const char *atype = (type && PyString_Check(type)) ? PyString_AsString(type) : 0;
442                                 
443                                 if (atype)
444                                 {
445                                         if (!strcmp(atype, "text"))
446                                         {
447                                                 PyObject *pvalue = PyTuple_GetItem(value, 1);
448                                                 const char *value = (pvalue && PyString_Check(pvalue)) ? PyString_AsString(pvalue) : "<not-a-string>";
449                                                 painter.setFont(fnt2);
450                                                 painter.renderText(eRect(offset + eSize(m_seperation, 0), item_right), value, gPainter::RT_HALIGN_RIGHT);
451
452                                                         /* pvalue is borrowed */
453                                         } else if (!strcmp(atype, "slider"))
454                                         {
455                                                 PyObject *pvalue = PyTuple_GetItem(value, 1);
456                                                 
457                                                         /* convert value to Long. fallback to -1 on error. */
458                                                 int value = (pvalue && PyInt_Check(pvalue)) ? PyInt_AsLong(pvalue) : -1;
459                                                 
460                                                         /* calc. slider length */
461                                                 int width = item_right.width() * value / 100;
462                                                 int height = item_right.height();
463                                                 
464                                                                                                 
465                                                         /* draw slider */
466                                                 //painter.fill(eRect(offset.x() + m_seperation, offset.y(), width, height));
467                                                 //hack - make it customizable
468                                                 painter.fill(eRect(offset.x() + m_seperation, offset.y() + 5, width, height-10));
469                                                 
470                                                         /* pvalue is borrowed */
471                                         } else if (!strcmp(atype, "mtext"))
472                                         {
473                                                 PyObject *pvalue = PyTuple_GetItem(value, 1);
474                                                 const char *text = (pvalue && PyString_Check(pvalue)) ? PyString_AsString(pvalue) : "<not-a-string>";
475                                                 
476                                                 ePtr<eTextPara> para = new eTextPara(eRect(offset + eSize(m_seperation, 0), item_right));
477                                                 para->setFont(fnt2);
478                                                 para->renderString(text, 0);
479                                                 para->realign(eTextPara::dirRight);
480                                                 int glyphs = para->size();
481                                                 
482                                                 PyObject *plist = 0;
483                                                 
484                                                 if (PyTuple_Size(value) >= 3)
485                                                         plist = PyTuple_GetItem(value, 2);
486                                                 
487                                                 int entries = 0;
488
489                                                 if (plist && PyList_Check(plist))
490                                                         entries = PyList_Size(plist);
491                                                 
492                                                 for (int i = 0; i < entries; ++i)
493                                                 {
494                                                         PyObject *entry = PyList_GetItem(plist, i);
495                                                         int num = PyInt_Check(entry) ? PyInt_AsLong(entry) : -1;
496                                                         
497                                                         if ((num < 0) || (num >= glyphs))
498                                                                 eWarning("glyph index %d in PythonConfigList out of bounds!");
499                                                         else
500                                                         {
501                                                                 para->setGlyphFlag(num, GS_INVERT);
502                                                                 eRect bbox;
503                                                                 bbox = para->getGlyphBBox(num);
504                                                                 bbox = eRect(bbox.left(), offset.y(), bbox.width(), m_itemsize.height());
505                                                                 painter.fill(bbox);
506                                                         }
507                                                                 /* entry is borrowed */
508                                                 }
509                                                 
510                                                 painter.renderPara(para, ePoint(0, 0));
511                                                         /* pvalue is borrowed */
512                                                         /* plist is 0 or borrowed */
513                                         }
514                                 }
515                                 Py_XDECREF(type);
516                         }
517                                 /* value is borrowed */
518                 }
519
520                 if (selected)
521                         style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
522         }
523         
524         painter.clippop();
525 }
526
527 //////////////////////////////////////
528
529 void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
530 {
531         painter.clip(eRect(offset, m_itemsize));
532         style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
533         painter.clear();
534
535         if (m_list && cursorValid())
536         {
537                 PyObject *items = PyList_GetItem(m_list, m_cursor); // borrowed reference!
538                 
539                 if (!items)
540                 {
541                         eDebug("eListboxPythonMultiContent: error getting item %d", m_cursor);
542                         painter.clippop();
543                         return;
544                 }
545                 
546                 if (!PyList_Check(items))
547                 {
548                         eDebug("eListboxPythonMultiContent: list entry %d is not a list", m_cursor);
549                         painter.clippop();
550                         return;
551                 }
552                 
553                 int size = PyList_Size(items);
554                 for (int i = 1; i < size; ++i)
555                 {
556                         PyObject *item = PyList_GetItem(items, i); // borrowed reference!
557                         
558                         if (!item)
559                         {
560                                 eDebug("eListboxPythonMultiContent: ?");
561                                 painter.clippop();
562                                 return;
563                         }
564                         
565                         
566                         PyObject *px, *py, *pwidth, *pheight, *pfnt, *pstring, *pflags;
567                 
568                         /*
569                                 we have a list of tuples:
570                                 
571                                 (x, y, width, height, fnt, flags, "bla" ),
572                                 
573                          */
574                         
575                         if (!PyTuple_Check(item))
576                         {
577                                 eDebug("eListboxPythonMultiContent did not receive a tuple.");
578                                 painter.clippop();
579                                 return;
580                         }
581                 
582                         px = PyTuple_GetItem(item, 0);
583                         py = PyTuple_GetItem(item, 1);
584                         pwidth = PyTuple_GetItem(item, 2);
585                         pheight = PyTuple_GetItem(item, 3);
586                         pfnt = PyTuple_GetItem(item, 4);
587                         pflags = PyTuple_GetItem(item, 5);
588                         pstring = PyTuple_GetItem(item, 6);
589                         
590                         if (!(px && py && pwidth && pheight && pfnt && pstring))
591                         {
592                                 eDebug("eListboxPythonMultiContent received too small tuple (must be (x, y, width, height, fnt, flags, string[, ...])");
593                                 painter.clippop();
594                                 return;
595                         }
596         
597                         pstring = PyObject_Str(pstring);
598                         
599                         const char *string = (PyString_Check(pstring)) ? PyString_AsString(pstring) : "<not-a-string>";
600                         
601                         int x = PyInt_AsLong(px);
602                         int y = PyInt_AsLong(py);
603                         int width = PyInt_AsLong(pwidth);
604                         int height = PyInt_AsLong(pheight);
605                         int flags = PyInt_AsLong(pflags);
606                         
607                         int fnt = PyInt_AsLong(pfnt);
608                         
609                         if (m_font.find(fnt) == m_font.end())
610                         {
611                                 eDebug("eListboxPythonMultiContent: specified font %d was not found!", fnt);
612                                 Py_XDECREF(pstring);
613                                 painter.clippop();
614                                 return;
615                         }
616                         
617                         eRect r = eRect(x, y, width, height);
618                         r.moveBy(offset);
619                         
620                         painter.setFont(m_font[fnt]);
621                         
622                         painter.renderText(r, string, flags);
623         
624                         Py_XDECREF(pstring);
625                         
626                         if (selected)
627                                 style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
628                 }
629         }
630         
631         painter.clippop();
632 }
633
634 void eListboxPythonMultiContent::setFont(int fnt, gFont *font)
635 {
636         if (font)
637                 m_font[fnt] = font;
638         else
639                 m_font.erase(fnt);
640 }