- add overwrite support
[enigma2.git] / lib / gui / einput.cpp
index b2c8ad14af39c9a18389d5802c558d7941c30ef5..7f37c4dbf5040b0eafaa8bed5cca48d2483e1560 100644 (file)
@@ -1,6 +1,7 @@
 #include <lib/gui/einput.h>
 #include <lib/gdi/font.h>
 #include <lib/actions/action.h>
+#include <linux/input.h>
 
 eInput::eInput(eWidget *parent): eLabel(parent)
 {
@@ -8,16 +9,20 @@ eInput::eInput(eWidget *parent): eLabel(parent)
        m_valign = alignCenter;
        m_halign = alignCenter;
 
-       ePtr<eActionMap> ptr;
-       eActionMap::getInstance(ptr);
-       ptr->bindAction("InputActions", 0, 0, this);
+       m_mode = 0;
 }
 
 eInput::~eInput()
 {
-       ePtr<eActionMap> ptr;
-       eActionMap::getInstance(ptr);
-       ptr->unbindAction(this, 0);
+       mayKillFocus();
+}
+
+void eInput::setOverwriteMode(int m)
+{
+       int om = m_mode;
+       m_mode = m;
+       if (om != m_mode)
+               invalidate();
 }
 
 void eInput::setContent(eInputContent *content)
@@ -53,19 +58,30 @@ int eInput::event(int event, void *data, void *data2)
                eDebug("cursor is %d", cursor);
                para->setFont(m_font);
                para->renderString(text, 0);
-               
                int glyphs = para->size();
-               eRect bbox;
-               if (cursor < glyphs)
+               
+               if (m_mode && cursor < glyphs)
                {
-                       bbox = para->getGlyphBBox(cursor);
-                       bbox = eRect(bbox.left()-1, 0, 2, size().height());
+                               /* in overwrite mode, when not at end of line, invert the current cursor position. */
+                       para->setGlyphFlag(cursor, GS_INVERT);
+                       eRect bbox = para->getGlyphBBox(cursor);
+                       bbox = eRect(bbox.left(), 0, bbox.width() + 2, size().height());
+                       painter.fill(bbox);
                } else
                {
-                       bbox = para->getGlyphBBox(cursor - 1);
-                       bbox = eRect(bbox.right(), 0, 2, size().height());
+                               /* otherwise, insert small cursor */
+                       eRect bbox;
+                       if (cursor < glyphs)
+                       {
+                               bbox = para->getGlyphBBox(cursor);
+                               bbox = eRect(bbox.left()-1, 0, 2, size().height());
+                       } else
+                       {
+                               bbox = para->getGlyphBBox(cursor - 1);
+                               bbox = eRect(bbox.right(), 0, 2, size().height());
+                       }
+                       painter.fill(bbox);
                }
-               painter.fill(bbox);
                
                painter.renderPara(para, ePoint(0, 0));
                
@@ -88,13 +104,46 @@ int eInput::event(int event, void *data, void *data2)
                        case moveEnd:
                                m_content->moveCursor(eInputContent::dirEnd);
                                break;
-                       case deleteChar:
-                               // not yet
+                       case deleteForward:
+                               m_content->deleteChar(eInputContent::deleteForward);
+                               break;
+                       case deleteBackward:
+                               m_content->deleteChar(eInputContent::deleteBackward);
+                               break;
+                       case toggleOverwrite:
+                               setOverwriteMode(!m_mode);
                                break;
                        }
                        return 1;
                }
                return 0;
+       case evtKey:
+       {
+               int key = (int)data;
+               int flags = (int)data2;
+               if (m_content && !(flags & 1)) // only make/repeat, no break
+                       return m_content->haveKey(key, m_mode);
+               break;
+       }
+       case evtFocusGot:
+       {
+               eDebug("focus got in %p", this);
+               ePtr<eActionMap> ptr;
+               eActionMap::getInstance(ptr);
+               ptr->bindAction("InputActions", 0, 0, this);
+                       // bind all keys
+               ptr->bindAction("", 0, 1, this);
+               break;
+       }
+       case evtFocusLost:
+       {
+               eDebug("focus lostin %p", this);
+               ePtr<eActionMap> ptr;
+               eActionMap::getInstance(ptr);
+               ptr->unbindAction(this, 0);
+               ptr->unbindAction(this, 1);
+               break;
+       }
        default:
                break;
        }
@@ -163,13 +212,71 @@ void eInputContentNumber::moveCursor(int dir)
                        m_input->invalidate();
 }
 
-int eInputContentNumber::haveKey(int code)
+int eInputContentNumber::haveKey(int code, int overwrite)
 {
-       insertDigit(m_cursor, code);
-       recalcLen();
+       int have_digit = -1;
+
+#define ASCII(x) (x | 0x8000)
+#define DIGIT(x) case KEY_##x: case KEY_KP##x: case ASCII(x|0x30): have_digit=x; break;
+       switch (code)
+       {
+       DIGIT(0);
+       DIGIT(1);
+       DIGIT(2);
+       DIGIT(3);
+       DIGIT(4);
+       DIGIT(5);
+       DIGIT(6);
+       DIGIT(7);
+       DIGIT(8);
+       DIGIT(9);
+       default:
+               return 0;
+       }
+       
+       if (have_digit != -1)
+       {
+               insertDigit(m_cursor, have_digit);
+                       /* if overwrite and not end of line, erase char first. */
+               if (overwrite && m_cursor < m_len)
+                       insertDigit(m_cursor + 1, -1);
+               m_cursor++;
+               
+               recalcLen();
+               
+                               // can happen when 0 -> x
+               if (m_cursor > m_len)
+                       m_cursor = m_len;
+               if (m_input)
+                       m_input->invalidate();
+               return 1;
+       }
        return 0;
 }
 
+void eInputContentNumber::deleteChar(int dir)
+{
+       if (dir == deleteForward)
+       {
+               eDebug("forward");
+               if (m_cursor != m_len)
+                       ++m_cursor;
+               else
+                       return;
+       }
+               /* backward delete at begin */
+       if (!m_cursor)
+               return;
+       insertDigit(m_cursor, -1);
+       
+       if (m_len > 1)
+               m_cursor--;
+       recalcLen();
+       if (m_input)
+               m_input->invalidate();
+}
+
 int eInputContentNumber::isValid()
 {
        return m_value >= m_min && m_value <= m_max;
@@ -194,14 +301,22 @@ void eInputContentNumber::insertDigit(int pos, int dig)
                /* get stuff left from cursor */
        int exp = 1;
        int i;
-       for (i = 0; i < (m_len - pos - 1); ++i)
+       for (i = 0; i < (m_len - pos); ++i)
                exp *= 10;
        
                /* now it's 1...max */
        int left = m_value / exp;
        int right = m_value % exp;
-       left *= 10;
-       left += dig;
+       
+       if (dig >= 0)
+       {
+               left *= 10;
+               left += dig;
+       } else if (dig == -1) /* delete */
+       {
+               left /= 10;
+       }
+       
        left *= exp;
        left += right;
        m_value = left;