add some more hacks
[enigma2.git] / lib / gui / elistboxcontent.cpp
index fdb270f80b6a0c8e89c5121ae203aba80b88907e..621fa5cc907b0a33549d414c7891a9ede769f084 100644 (file)
@@ -1,7 +1,7 @@
 #include <lib/gui/elistbox.h>
 #include <lib/gui/elistboxcontent.h>
 #include <lib/gdi/font.h>
-#include <Python.h>
+#include <lib/python/python.h>
 
 /*
     The basic idea is to have an interface which gives all relevant list
@@ -50,7 +50,6 @@ DEFINE_REF(eListboxPythonStringContent);
 
 eListboxPythonStringContent::eListboxPythonStringContent()
 {
-       m_list = 0;
 }
 
 eListboxPythonStringContent::~eListboxPythonStringContent()
@@ -104,11 +103,10 @@ int eListboxPythonStringContent::currentCursorSelectable()
 {
        if (m_list && cursorValid())
        {
-               PyObject *item = PyList_GET_ITEM(m_list, m_cursor);
-               if (PyTuple_Check(item))
-                       item = PyTuple_GET_ITEM(item, 0);
-               
-               if (item != Py_None)
+               ePyObject item = PyList_GET_ITEM(m_list, m_cursor);
+               if (!PyTuple_Check(item))
+                       return 1;
+               if (PyTuple_Size(item) >= 2)
                        return 1;
        }
        return 0;
@@ -145,12 +143,17 @@ void eListboxPythonStringContent::paint(gPainter &painter, eWindowStyle &style,
 
        if (m_list && cursorValid())
        {
-               PyObject *item = PyList_GET_ITEM(m_list, m_cursor); // borrowed reference!
+               int gray = 0;
+               ePyObject item = PyList_GET_ITEM(m_list, m_cursor); // borrowed reference!
                painter.setFont(fnt);
 
                        /* the user can supply tuples, in this case the first one will be displayed. */         
                if (PyTuple_Check(item))
+               {
+                       if (PyTuple_Size(item) == 1)
+                               gray = 1;
                        item = PyTuple_GET_ITEM(item, 0);
+               }
                
                if (item == Py_None)
                {
@@ -161,6 +164,8 @@ void eListboxPythonStringContent::paint(gPainter &painter, eWindowStyle &style,
                {
                        const char *string = PyString_Check(item) ? PyString_AsString(item) : "<not-a-string>";
                        ePoint text_offset = offset + (selected ? ePoint(2, 2) : ePoint(1, 1));
+                       if (gray)
+                               painter.setForegroundColor(gRGB(0x808080));
                        painter.renderText(eRect(text_offset, m_itemsize), string);
                }
                
@@ -171,12 +176,12 @@ void eListboxPythonStringContent::paint(gPainter &painter, eWindowStyle &style,
        painter.clippop();
 }
 
-void eListboxPythonStringContent::setList(PyObject *list)
+void eListboxPythonStringContent::setList(ePyObject list)
 {
        Py_XDECREF(m_list);
        if (!PyList_Check(list))
        {
-               m_list = 0;
+               m_list = ePyObject();
        } else
        {
                m_list = list;
@@ -190,11 +195,9 @@ void eListboxPythonStringContent::setList(PyObject *list)
 PyObject *eListboxPythonStringContent::getCurrentSelection()
 {
        if (!(m_list && cursorValid()))
-       {
-               Py_INCREF(Py_None);
-               return Py_None;
-       }
-       PyObject *r = PyList_GET_ITEM(m_list, m_cursor);
+               Py_RETURN_NONE;
+
+       ePyObject r = PyList_GET_ITEM(m_list, m_cursor);
        Py_XINCREF(r);
        return r;
 }
@@ -208,7 +211,12 @@ void eListboxPythonStringContent::invalidateEntry(int index)
 void eListboxPythonStringContent::invalidate()
 {
        if (m_listbox)
+       {
+               int s = size();
+               if ( m_cursor >= s )
+                       m_listbox->moveSelectionTo(s?s-1:0);
                m_listbox->invalidate();
+       }
 }
 
 //////////////////////////////////////
@@ -225,8 +233,8 @@ void eListboxPythonConfigContent::paint(gPainter &painter, eWindowStyle &style,
        if (m_list && cursorValid())
        {
                        /* get current list item */
-               PyObject *item = PyList_GET_ITEM(m_list, m_cursor); // borrowed reference!
-               PyObject *text = 0, *value = 0;
+               ePyObject item = PyList_GET_ITEM(m_list, m_cursor); // borrowed reference!
+               ePyObject text, value;
                painter.setFont(fnt);
 
                        /* the first tuple element is a string for the left side.
@@ -255,7 +263,7 @@ void eListboxPythonConfigContent::paint(gPainter &painter, eWindowStyle &style,
                        value = PyTuple_GET_ITEM(item, 1);
                        if (value)
                        {
-                               PyObject *args = PyTuple_New(1);
+                               ePyObject args = PyTuple_New(1);
                                PyTuple_SET_ITEM(args, 0, PyInt_FromLong(selected));
                                
                                        /* CallObject will call __call__ which should return the value tuple */
@@ -272,14 +280,14 @@ void eListboxPythonConfigContent::paint(gPainter &painter, eWindowStyle &style,
                        if (value && PyTuple_Check(value))
                        {
                                        /* convert type to string */
-                               PyObject *type = PyTuple_GET_ITEM(value, 0);
+                               ePyObject type = PyTuple_GET_ITEM(value, 0);
                                const char *atype = (type && PyString_Check(type)) ? PyString_AsString(type) : 0;
                                
                                if (atype)
                                {
                                        if (!strcmp(atype, "text"))
                                        {
-                                               PyObject *pvalue = PyTuple_GET_ITEM(value, 1);
+                                               ePyObject pvalue = PyTuple_GET_ITEM(value, 1);
                                                const char *value = (pvalue && PyString_Check(pvalue)) ? PyString_AsString(pvalue) : "<not-a-string>";
                                                painter.setFont(fnt2);
                                                if (value_alignment_left)
@@ -290,8 +298,8 @@ void eListboxPythonConfigContent::paint(gPainter &painter, eWindowStyle &style,
                                                        /* pvalue is borrowed */
                                        } else if (!strcmp(atype, "slider"))
                                        {
-                                               PyObject *pvalue = PyTuple_GET_ITEM(value, 1);
-                                               PyObject *psize = PyTuple_GET_ITEM(value, 2);
+                                               ePyObject pvalue = PyTuple_GET_ITEM(value, 1);
+                                               ePyObject psize = PyTuple_GET_ITEM(value, 2);
                                                
                                                        /* convert value to Long. fallback to -1 on error. */
                                                int value = (pvalue && PyInt_Check(pvalue)) ? PyInt_AsLong(pvalue) : -1;
@@ -310,7 +318,7 @@ void eListboxPythonConfigContent::paint(gPainter &painter, eWindowStyle &style,
                                                        /* pvalue is borrowed */
                                        } else if (!strcmp(atype, "mtext"))
                                        {
-                                               PyObject *pvalue = PyTuple_GET_ITEM(value, 1);
+                                               ePyObject pvalue = PyTuple_GET_ITEM(value, 1);
                                                const char *text = (pvalue && PyString_Check(pvalue)) ? PyString_AsString(pvalue) : "<not-a-string>";
                                                int xoffs = value_alignment_left ? 0 : m_seperation;
                                                ePtr<eTextPara> para = new eTextPara(eRect(offset + eSize(xoffs, 0), item_right));
@@ -319,7 +327,7 @@ void eListboxPythonConfigContent::paint(gPainter &painter, eWindowStyle &style,
                                                para->realign(value_alignment_left ? eTextPara::dirLeft : eTextPara::dirRight);
                                                int glyphs = para->size();
                                                
-                                               PyObject *plist = 0;
+                                               ePyObject plist;
                                                
                                                if (PyTuple_Size(value) >= 3)
                                                        plist = PyTuple_GET_ITEM(value, 2);
@@ -331,7 +339,7 @@ void eListboxPythonConfigContent::paint(gPainter &painter, eWindowStyle &style,
                                                
                                                for (int i = 0; i < entries; ++i)
                                                {
-                                                       PyObject *entry = PyList_GET_ITEM(plist, i);
+                                                       ePyObject entry = PyList_GET_ITEM(plist, i);
                                                        int num = PyInt_Check(entry) ? PyInt_AsLong(entry) : -1;
                                                        
                                                        if ((num < 0) || (num >= glyphs))
@@ -375,6 +383,16 @@ int eListboxPythonConfigContent::currentCursorSelectable()
        /* todo: make a real infrastructure here! */
 RESULT SwigFromPython(ePtr<gPixmap> &res, PyObject *obj);
 
+eListboxPythonMultiContent::eListboxPythonMultiContent()
+{
+}
+
+eListboxPythonMultiContent::~eListboxPythonMultiContent()
+{
+       if (m_buildFunc)
+               Py_DECREF(m_buildFunc);
+}
+
 void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
 {
        eRect itemrect(offset, m_itemsize);
@@ -382,10 +400,25 @@ void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, c
        style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
        painter.clear();
 
+       ePyObject items;
+
        if (m_list && cursorValid())
        {
-               PyObject *items = PyList_GET_ITEM(m_list, m_cursor); // borrowed reference!
-               
+               items = PyList_GET_ITEM(m_list, m_cursor); // borrowed reference!
+
+               if (m_buildFunc)
+               {
+                       if (PyCallable_Check(m_buildFunc))  // when we have a buildFunc then call it
+                       {
+                               if (PyTuple_Check(items))
+                                       items = PyObject_CallObject(m_buildFunc, items);
+                               else
+                                       eDebug("items is no tuple");
+                       }
+                       else
+                               eDebug("buildfunc is not callable");
+               }
+
                if (!items)
                {
                        eDebug("eListboxPythonMultiContent: error getting item %d", m_cursor);
@@ -401,7 +434,7 @@ void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, c
                int size = PyList_Size(items);
                for (int i = 1; i < size; ++i)
                {
-                       PyObject *item = PyList_GET_ITEM(items, i); // borrowed reference!
+                       ePyObject item = PyList_GET_ITEM(items, i); // borrowed reference!
                        
                        if (!item)
                        {
@@ -409,12 +442,12 @@ void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, c
                                goto error_out;
                        }
                        
-                       PyObject *px = 0, *py = 0, *pwidth = 0, *pheight = 0, *pfnt = 0, *pstring = 0, *pflags = 0;
+                       ePyObject px, py, pwidth, pheight, pfnt, pstring, pflags, pcolor;
                
                        /*
                                we have a list of tuples:
                                
-                               (0, x, y, width, height, fnt, flags, "bla" ),
+                               (0, x, y, width, height, fnt, flags, "bla"[, color] ),
 
                                or, for a progress:
                                (1, x, y, width, height, filled_percent )
@@ -453,6 +486,8 @@ void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, c
                                        pflags = PyTuple_GET_ITEM(item, 6);
                                        pstring = PyTuple_GET_ITEM(item, 7);
                                }
+                               if (size > 8)
+                                       pcolor = PyTuple_GET_ITEM(item, 8);
                        }
                        
                        switch (type)
@@ -461,7 +496,7 @@ void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, c
                        {
                                if (!(px && py && pwidth && pheight && pfnt && pstring))
                                {
-                                       eDebug("eListboxPythonMultiContent received too small tuple (must be (TYPE_TEXT, x, y, width, height, fnt, flags, string[, ...])");
+                                       eDebug("eListboxPythonMultiContent received too small tuple (must be (TYPE_TEXT, x, y, width, height, fnt, flags, string, [color, ]...])");
                                        goto error_out;
                                }
                                
@@ -473,6 +508,12 @@ void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, c
                                int flags = PyInt_AsLong(pflags);
                                int fnt = PyInt_AsLong(pfnt);
                                
+                               if (pcolor)
+                               {
+                                       int color = PyInt_AsLong(pcolor);
+                                       painter.setForegroundColor(gRGB(color));
+                               }
+                               
                                if (m_font.find(fnt) == m_font.end())
                                {
                                        eDebug("eListboxPythonMultiContent: specified font %d was not found!", fnt);
@@ -569,6 +610,9 @@ void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, c
                                eWarning("eListboxPythonMultiContent received unknown type (%d)", type);
                                goto error_out;
                        }
+                       
+                       if (pcolor)
+                               style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
                }
        }
        
@@ -576,12 +620,38 @@ void eListboxPythonMultiContent::paint(gPainter &painter, eWindowStyle &style, c
                style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
 
 error_out:
+       if (m_buildFunc && PyCallable_Check(m_buildFunc) && items)
+               Py_DECREF(items);
+
        painter.clippop();
 }
 
+void eListboxPythonMultiContent::setBuildFunc(ePyObject cb)
+{
+       if (m_buildFunc)
+               Py_DECREF(m_buildFunc);
+       m_buildFunc=cb;
+       if (cb)
+               Py_INCREF(m_buildFunc);
+}
+
 int eListboxPythonMultiContent::currentCursorSelectable()
 {
-       return eListboxPythonStringContent::currentCursorSelectable();
+       /* each list-entry is a list of tuples. if the first of these is none, it's not selectable */
+       if (m_list && cursorValid())
+       {
+               ePyObject item = PyList_GET_ITEM(m_list, m_cursor);
+               if (PyList_Check(item))
+               {
+                       item = PyList_GET_ITEM(item, 0);
+                       if (item != Py_None)
+                               return 1;
+               }
+               else if (m_buildFunc && PyCallable_Check(m_buildFunc))
+               // FIXME .. how we can detect non selectable entrys when we have a buildFunc callback
+                       return 1;
+       }
+       return 0;
 }
 
 void eListboxPythonMultiContent::setFont(int fnt, gFont *font)