fix
[enigma2.git] / lib / service / listboxservice.cpp
index f35c00a584e1d397e7ba4fe3fc951e40f0e1fba2..9eafc853cc1d212c47e1ef0a3380da8fdcd4c8ea 100644 (file)
@@ -1,5 +1,7 @@
 #include <lib/service/listboxservice.h>
 #include <lib/service/service.h>
+#include <lib/gdi/font.h>
+#include <lib/dvb/epgcache.h>
 
 void eListboxServiceContent::setRoot(const eServiceReference &root)
 {
@@ -22,6 +24,18 @@ void eListboxServiceContent::setRoot(const eServiceReference &root)
                m_listbox->entryReset();
 }
 
+void eListboxServiceContent::setCurrent(const eServiceReference &ref)
+{
+       int index=0;
+       for (list::iterator i(m_list.begin()); i != m_list.end(); ++i, ++index)
+               if ( *i == ref )
+               {
+                       m_cursor = i;
+                       m_cursor_number = index;
+                       break;
+               }
+}
+
 void eListboxServiceContent::getCurrent(eServiceReference &ref)
 {
        if (cursorValid())
@@ -87,7 +101,7 @@ void eListboxServiceContent::setVisualMode(int mode)
        if (m_visual_mode == visModeSimple)
        {
                m_element_position[celServiceName] = eRect(ePoint(0, 0), m_itemsize);
-               m_element_font[celServiceName] = new gFont("Arial", 14);
+               m_element_font[celServiceName] = new gFont("Arial", 23);
                m_element_position[celServiceNumber] = eRect();
                m_element_font[celServiceNumber] = 0;
                m_element_position[celIcon] = eRect();
@@ -124,7 +138,7 @@ void eListboxServiceContent::sort()
 DEFINE_REF(eListboxServiceContent);
 
 eListboxServiceContent::eListboxServiceContent()
-       :m_visual_mode(visModeSimple), m_size(0), m_current_marked(false), m_swap(m_list.end())
+       :epgcache(eEPGCache::getInstance()), m_visual_mode(visModeSimple), m_size(0), m_current_marked(false), m_numberoffset(0)
 {
        cursorHome();
        eServiceCenter::getInstance(m_service_center);
@@ -132,21 +146,44 @@ eListboxServiceContent::eListboxServiceContent()
 
 void eListboxServiceContent::cursorHome()
 {
-       list::iterator old = m_cursor;
-
-       m_cursor = m_list.begin();
-       m_cursor_number = 0;
-
-       if ( m_current_marked && m_saved_cursor == m_list.end() )
-               std::iter_swap( old, m_cursor );
+       if (m_current_marked && m_saved_cursor == m_list.end())
+       {
+               while (m_cursor_number)
+               {
+                       std::iter_swap(m_cursor--, m_cursor);
+                       --m_cursor_number;
+                       if (m_listbox && m_cursor_number)
+                               m_listbox->entryChanged(m_cursor_number);
+               }
+       }
+       else
+       {
+               m_cursor = m_list.begin();
+               m_cursor_number = 0;
+       }
 }
 
 void eListboxServiceContent::cursorEnd()
 {
-       if ( m_current_marked && m_saved_cursor == m_list.end() && m_cursor != m_list.end() )
-               m_swap = m_cursor;
-       m_cursor = m_list.end();
-       m_cursor_number = m_size;
+       if (m_current_marked && m_saved_cursor == m_list.end())
+       {
+               while (m_cursor != m_list.end())
+               {
+                       list::iterator prev = m_cursor++;
+                       ++m_cursor_number;
+                       if ( prev != m_list.end() && m_cursor != m_list.end() )
+                       {
+                               std::iter_swap(m_cursor, prev);
+                               if ( m_listbox )
+                                       m_listbox->entryChanged(m_cursor_number);
+                       }
+               }
+       }
+       else
+       {
+               m_cursor = m_list.end();
+               m_cursor_number = m_size;
+       }
 }
 
 int eListboxServiceContent::setCurrentMarked(bool state)
@@ -155,46 +192,73 @@ int eListboxServiceContent::setCurrentMarked(bool state)
        m_current_marked = state;
 
        if (state != prev && m_listbox)
+       {
                m_listbox->entryChanged(m_cursor_number);
+               if (!state)
+               {
+                       ePtr<iListableService> lst;
+                       if (m_service_center->list(m_root, lst))
+                               eDebug("no list available!");
+                       else
+                       {
+                               ePtr<iMutableServiceList> list;
+                               if (lst->startEdit(list))
+                                       eDebug("no editable list");
+                               else
+                               {
+                                       eServiceReference ref;
+                                       getCurrent(ref);
+                                       if(!ref)
+                                               eDebug("no valid service selected");
+                                       else
+                                       {
+                                               int pos = cursorGet();
+                                               eDebugNoNewLine("move %s to %d ", ref.toString().c_str(), pos);
+                                               if (list->moveService(ref, cursorGet()))
+                                                       eDebug("failed");
+                                               else
+                                                       eDebug("ok");
+                                       }
+                               }
+                       }
+               }
+       }
 
        return 0;
 }
 
 int eListboxServiceContent::cursorMove(int count)
 {
-       list::iterator old = m_cursor;
-
+       int prev = m_cursor_number, last = m_cursor_number + count;
        if (count > 0)
        {
-               while(count && (m_cursor != m_list.end()))
+               while(count && m_cursor != m_list.end())
                {
-                       ++m_cursor;
+                       list::iterator prev_it = m_cursor++;
+                       if ( m_current_marked && m_cursor != m_list.end() && m_saved_cursor == m_list.end() )
+                       {
+                               std::iter_swap(prev_it, m_cursor);
+                               if ( m_listbox && prev != m_cursor_number && last != m_cursor_number )
+                                       m_listbox->entryChanged(m_cursor_number);
+                       }
                        ++m_cursor_number;
                        --count;
-               }
+       }
        } else if (count < 0)
        {
-               while (count && (m_cursor != m_list.begin()))
+               while (count && m_cursor != m_list.begin())
                {
-                       --m_cursor;
+                       list::iterator prev_it = m_cursor--;
+                       if ( m_current_marked && m_cursor != m_list.end() && prev_it != m_list.end() && m_saved_cursor == m_list.end() )
+                       {
+                               std::iter_swap(prev_it, m_cursor);
+                               if ( m_listbox && prev != m_cursor_number && last != m_cursor_number )
+                                       m_listbox->entryChanged(m_cursor_number);
+                       }
                        --m_cursor_number;
                        ++count;
                }
        }
-
-       if ( m_current_marked && m_saved_cursor == m_list.end() )
-       {
-               if ( m_cursor == m_list.end() )
-                       m_swap = old;
-               else if ( old == m_list.end() )
-               {
-                       std::iter_swap( m_swap, m_cursor );
-                       m_swap = m_list.end();
-               }
-               else
-                       std::iter_swap( old, m_cursor );
-       }
-
        return 0;
 }
 
@@ -262,38 +326,76 @@ void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const
                {
                        if (!m_element_font[e])
                                continue;
-                       painter.setFont(m_element_font[e]);
-                       
+                       int flags=gPainter::RT_VALIGN_CENTER;
+
+                       eRect area = m_element_position[e];
+
                        std::string text = "<n/a>";
-                       
+
                        switch (e)
                        {
+                       case celIcon:
+                               // render icon here...
+                               continue;
+                       case celServiceNumber:
+                       {
+                               char bla[10];
+                               sprintf(bla, "%d", m_numberoffset + m_cursor_number + 1);
+                               text = bla;
+                               flags|=gPainter::RT_HALIGN_RIGHT;
+                               break;
+                       }
                        case celServiceName:
                        {
                                if (service_info)
                                        service_info->getName(*m_cursor, text);
                                break;
                        }
-                       case celServiceNumber:
+                       case celServiceInfo:
                        {
-                               char bla[10];
-                               sprintf(bla, "%d", m_cursor_number + 1);
-                               text = bla;
+                               ePtr<eServiceEvent> evt;
+                               time_t t=-1;
+                               if ( !epgcache->lookupEventTime(*m_cursor, t, evt) )
+                                       text = '(' + evt->getEventName() + ')';
+                               else
+                                       continue;
                                break;
                        }
-                       case celServiceInfo:
+                       }
+
+                       eTextPara *para = new eTextPara(area);
+
+                       para->setFont(m_element_font[e]);
+                       para->renderString(text);
+
+                       if (e == celServiceName)
                        {
-                               text = "now&next";
-                               break;
+                               eRect bbox = para->getBoundBox();
+                               int name_width = bbox.width()+10;
+                               m_element_position[celServiceInfo].setLeft(area.left()+name_width);
+                               m_element_position[celServiceInfo].setTop(area.top());
+                               m_element_position[celServiceInfo].setWidth(area.width()-name_width);
+                               m_element_position[celServiceInfo].setHeight(area.height());
                        }
-                       case celIcon:
-                               continue;
+
+                       if (flags & gPainter::RT_HALIGN_RIGHT)
+                               para->realign(eTextPara::dirRight);
+                       else if (flags & gPainter::RT_HALIGN_CENTER)
+                               para->realign(eTextPara::dirCenter);
+                       else if (flags & gPainter::RT_HALIGN_BLOCK)
+                               para->realign(eTextPara::dirBlock);
+
+                       ePoint offs = offset;
+
+                       if (flags & gPainter::RT_VALIGN_CENTER)
+                       {
+                               eRect bbox = para->getBoundBox();
+                               int vcentered_top = (area.height() - bbox.height()) / 2;
+                               int correction = vcentered_top - bbox.top();
+                               offs += ePoint(0, correction);
                        }
-                       
-                       eRect area = m_element_position[e];
-                       area.moveBy(offset.x(), offset.y());
-                       
-                       painter.renderText(area, text);
+
+                       painter.renderPara(para, offs);
                }
                
                if (selected)