- split einput contents to own files
authorFelix Domke <tmbinc@elitedvb.net>
Fri, 20 May 2005 19:34:06 +0000 (19:34 +0000)
committerFelix Domke <tmbinc@elitedvb.net>
Fri, 20 May 2005 19:34:06 +0000 (19:34 +0000)
 - add string input

lib/gui/Makefile.am
lib/gui/einput.cpp
lib/gui/einput.h
lib/gui/einputnumber.cpp [new file with mode: 0644]
lib/gui/einputnumber.h [new file with mode: 0644]
lib/gui/einputstring.cpp [new file with mode: 0644]
lib/gui/einputstring.h [new file with mode: 0644]

index 24f70676ec9c90bb5f25daef7707332e05c5cc76..951cc4f554cb2fcaa57defcd91856b2bb3599001 100644 (file)
@@ -7,7 +7,7 @@ noinst_LIBRARIES = libenigma_gui.a
 libenigma_gui_a_SOURCES = \
        ebutton.cpp elabel.cpp eslider.cpp ewidget.cpp ewidgetdesktop.cpp  \
        ewindow.cpp ewindowstyle.cpp elistbox.cpp elistboxcontent.cpp \
-       epixmap.cpp ewindowstyleskinned.cpp einput.cpp
+       epixmap.cpp ewindowstyleskinned.cpp einput.cpp einputstring.cpp einputnumber.cpp
 
 
 
index 7f37c4dbf5040b0eafaa8bed5cca48d2483e1560..7d411f9e4a1de67a91745af8ddb5cab87b2c73ca 100644 (file)
@@ -1,15 +1,13 @@
 #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)
-{
-               /* default to center alignment */
-       m_valign = alignCenter;
-       m_halign = alignCenter;
+#include <lib/driver/rc.h>
 
-       m_mode = 0;
+eInput::eInput(eWidget *parent): eWidget(parent)
+{
+       m_mode = 1;
+       m_have_focus = 0;
 }
 
 eInput::~eInput()
@@ -60,27 +58,33 @@ int eInput::event(int event, void *data, void *data2)
                para->renderString(text, 0);
                int glyphs = para->size();
                
-               if (m_mode && cursor < glyphs)
+               if (m_have_focus)
                {
-                               /* 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
-               {
-                               /* otherwise, insert small cursor */
-                       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(), 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 if (cursor)
+                               {
+                                       bbox = para->getGlyphBBox(cursor - 1);
+                                       bbox = eRect(bbox.right(), 0, 2, size().height());
+                               } else
+                               {
+                                       bbox = eRect(0, 0, 2, size().height());
+                               }
+                               painter.fill(bbox);
                        }
-                       painter.fill(bbox);
                }
                
                painter.renderPara(para, ePoint(0, 0));
@@ -113,6 +117,9 @@ int eInput::event(int event, void *data, void *data2)
                        case toggleOverwrite:
                                setOverwriteMode(!m_mode);
                                break;
+                       case accept:
+                               changed();
+                               mayKillFocus();
                        }
                        return 1;
                }
@@ -133,6 +140,11 @@ int eInput::event(int event, void *data, void *data2)
                ptr->bindAction("InputActions", 0, 0, this);
                        // bind all keys
                ptr->bindAction("", 0, 1, this);
+               m_have_focus = 1;
+               eRCInput::getInstance()->setKeyboardMode(eRCInput::kmAscii);
+                       // fixme. we should use a style for this.
+               setBackgroundColor(gRGB(64, 64, 128));
+               invalidate();
                break;
        }
        case evtFocusLost:
@@ -142,182 +154,28 @@ int eInput::event(int event, void *data, void *data2)
                eActionMap::getInstance(ptr);
                ptr->unbindAction(this, 0);
                ptr->unbindAction(this, 1);
+               m_have_focus = 0;
+               if (m_content)
+                       m_content->validate();
+               eRCInput::getInstance()->setKeyboardMode(eRCInput::kmNone);
+               clearBackgroundColor();
+               invalidate();
                break;
        }
        default:
                break;
        }
-       return eLabel::event(event, data, data2);
+       return eWidget::event(event, data, data2);
 }
 
-int eInput::getNumber()
+void eInput::setFont(gFont *fnt)
 {
-       return atoi(m_text.c_str());
+       m_font = fnt;
+       invalidate();
 }
 
-DEFINE_REF(eInputContentNumber);
-
 void eInputContent::setInput(eInput *widget)
 {
        m_input = widget;
 }
 
-eInputContentNumber::eInputContentNumber(int cur, int min, int max)
-{
-       m_min = min;
-       m_max = max;
-       m_value = cur;
-       m_cursor = 0;
-       m_input = 0;
-       recalcLen();
-}
-
-void eInputContentNumber::getDisplay(std::string &res, int &cursor)
-{
-       // TODO
-       char r[128];
-       sprintf(r, "%d", m_value);
-       res = r;
-       cursor = m_cursor;
-}
-
-void eInputContentNumber::moveCursor(int dir)
-{
-       eDebug("move cursor..");
-       int old_cursor = m_cursor;
-       
-       switch (dir)
-       {
-       case dirLeft:
-               --m_cursor;
-               break;
-       case dirRight:
-               ++m_cursor;
-               break;
-       case dirHome:
-               m_cursor = 0;
-               break;
-       case dirEnd:
-               m_cursor = m_len;
-               break;
-       }
-       
-       if (m_cursor < 0)
-               m_cursor = 0;
-       if (m_cursor > m_len)
-               m_cursor = m_len;
-       
-       if (m_cursor != old_cursor)
-               if (m_input)
-                       m_input->invalidate();
-}
-
-int eInputContentNumber::haveKey(int code, int overwrite)
-{
-       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;
-}
-
-void eInputContentNumber::recalcLen()
-{
-       int v = m_value;
-       m_len = 0;
-       while (v)
-       {
-               ++m_len;
-               v /= 10;
-       }
-       
-       if (!m_len) /* zero */
-               m_len = 1;
-}
-
-void eInputContentNumber::insertDigit(int pos, int dig)
-{
-               /* get stuff left from cursor */
-       int exp = 1;
-       int 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;
-       
-       if (dig >= 0)
-       {
-               left *= 10;
-               left += dig;
-       } else if (dig == -1) /* delete */
-       {
-               left /= 10;
-       }
-       
-       left *= exp;
-       left += right;
-       m_value = left;
-}
index 76342df6d02b0c24d725fc1dc4c553a5d745b150..7c65cc13ec6b5dd65d714d1a914339b979702e4e 100644 (file)
@@ -1,12 +1,12 @@
 #ifndef __lib_gui_einput_h
 #define __lib_gui_einput_h
 
-#include <lib/gui/elabel.h>
+#include <lib/gui/ewidget.h>
 #include <lib/python/connections.h>
 
 class eInputContent;
 
-class eInput: public eLabel
+class eInput: public eWidget
 {
 public:
        eInput(eWidget *parent);
@@ -22,16 +22,18 @@ public:
                moveEnd,
                deleteForward,
                deleteBackward,
-               toggleOverwrite
+               toggleOverwrite,
+               accept
        };
        
        void setContent(eInputContent *cnt);
        
        void setOverwriteMode(int o);
        
-       int getNumber();
+       void setFont(gFont *font);
 protected:
-       int m_mode;
+       ePtr<gFont> m_font;
+       int m_mode, m_have_focus;
        ePtr<eInputContent> m_content;
        int event(int event, void *data=0, void *data2=0);
 };
@@ -62,31 +64,9 @@ public:
        virtual int haveKey(int code, int overwrite)=0;
        
        virtual int isValid()=0;
+       virtual void validate()=0;
 protected:
        eInput *m_input;
 };
 
-class eInputContentNumber: public eInputContent
-{
-       DECLARE_REF(eInputContentNumber);
-public:
-       eInputContentNumber(int cur, int min, int max);
-
-       void getDisplay(std::string &res, int &cursor);
-       void moveCursor(int dir);
-       int haveKey(int code, int overwrite);
-       void deleteChar(int dir);
-       int isValid();
-       
-private:
-       void recalcLen();
-       
-       void insertDigit(int pos, int dig);
-       
-       int m_value;
-       int m_cursor, m_len;
-       
-       int m_min, m_max;
-};
-
 #endif
diff --git a/lib/gui/einputnumber.cpp b/lib/gui/einputnumber.cpp
new file mode 100644 (file)
index 0000000..29c7880
--- /dev/null
@@ -0,0 +1,207 @@
+#include <lib/gui/einputnumber.h>
+#include <linux/input.h>
+
+DEFINE_REF(eInputContentNumber);
+
+eInputContentNumber::eInputContentNumber(int cur, int min, int max)
+{
+       m_min = min;
+       m_max = max;
+       m_value = cur;
+       m_cursor = 0;
+       m_input = 0;
+       recalcLen();
+}
+
+void eInputContentNumber::getDisplay(std::string &res, int &cursor)
+{
+       // TODO
+       char r[128];
+       
+       int e = 1;
+       for (int i = 1; i < m_len; ++i)
+               e *= 10;
+
+       int v = m_value;
+       
+       int i;
+       for (i = 0; i < m_len; ++i)
+       {
+               int rem = v / e;
+               r[i] = '0' + rem;
+               v %= e;
+               
+               e /= 10;
+       }
+       
+       r[i] = 0;
+       
+       res = r;
+       cursor = m_cursor;
+}
+
+void eInputContentNumber::moveCursor(int dir)
+{
+       eDebug("move cursor..");
+       int old_cursor = m_cursor;
+       
+       switch (dir)
+       {
+       case dirLeft:
+               --m_cursor;
+               break;
+       case dirRight:
+               ++m_cursor;
+               break;
+       case dirHome:
+               m_cursor = 0;
+               break;
+       case dirEnd:
+               m_cursor = m_len;
+               break;
+       }
+       
+       if (m_cursor < 0)
+               m_cursor = 0;
+       if (m_cursor > m_len)
+               m_cursor = m_len;
+       
+       if (m_cursor != old_cursor)
+               if (m_input)
+                       m_input->invalidate();
+}
+
+int eInputContentNumber::haveKey(int code, int overwrite)
+{
+       int have_digit = -1;
+
+               /* we handle KEY_KPx, but not KEY_x. otherwise we would get stuff twice. */
+#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);
+               else
+                       ++m_len;
+               
+               m_cursor++;
+               
+               assert(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;
+       
+       if (!m_len)
+               return;
+       
+       insertDigit(m_cursor, -1);
+       
+       m_len--;
+       m_cursor--;
+
+       if (m_input)
+               m_input->invalidate();
+}
+
+int eInputContentNumber::isValid()
+{
+       return m_value >= m_min && m_value <= m_max;
+}
+
+void eInputContentNumber::validate()
+{
+       recalcLen();
+}
+
+void eInputContentNumber::setValue(int val)
+{
+       m_value = val;
+       recalcLen();
+       if (m_cursor > m_len)
+               m_cursor = m_len;
+       if (m_input)
+               m_input->invalidate();
+}
+
+int eInputContentNumber::getValue()
+{
+       return m_value;
+}
+
+void eInputContentNumber::recalcLen()
+{
+       int v = m_value;
+       m_len = 0;
+       while (v)
+       {
+               ++m_len;
+               v /= 10;
+       }
+       
+       if (!m_len) /* zero */
+               m_len = 1;
+}
+
+void eInputContentNumber::insertDigit(int pos, int dig)
+{
+               /* get stuff left from cursor */
+       int exp = 1;
+       int 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;
+       
+       if (dig >= 0)
+       {
+               left *= 10;
+               left += dig;
+       } else if (dig == -1) /* delete */
+       {
+               left /= 10;
+       }
+       
+       left *= exp;
+       left += right;
+       m_value = left;
+}
+
diff --git a/lib/gui/einputnumber.h b/lib/gui/einputnumber.h
new file mode 100644 (file)
index 0000000..c4f3013
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef __lib_gui_einputnumber_h
+#define __lib_gui_einputnumber_h
+
+#include <lib/gui/einput.h>
+
+class eInputContentNumber: public eInputContent
+{
+       DECLARE_REF(eInputContentNumber);
+public:
+       eInputContentNumber(int cur, int min, int max);
+
+       void getDisplay(std::string &res, int &cursor);
+       void moveCursor(int dir);
+       int haveKey(int code, int overwrite);
+       void deleteChar(int dir);
+       int isValid();
+       
+       void validate();
+       
+       void setValue(int num);
+       int getValue();
+       
+private:
+       void recalcLen();
+       
+       void insertDigit(int pos, int dig);
+       
+       int m_value;
+       int m_cursor, m_len;
+       
+       int m_min, m_max;
+};
+
+
+#endif
diff --git a/lib/gui/einputstring.cpp b/lib/gui/einputstring.cpp
new file mode 100644 (file)
index 0000000..9ead4fa
--- /dev/null
@@ -0,0 +1,126 @@
+#include <lib/gui/einputstring.h>
+
+DEFINE_REF(eInputContentString);
+
+eInputContentString::eInputContentString()
+{
+       m_string = "bla";
+       m_cursor = 0;
+       m_input = 0;
+       m_len = m_string.size();
+}
+
+void eInputContentString::getDisplay(std::string &res, int &cursor)
+{
+       res = m_string;
+       cursor = m_cursor;
+}
+
+void eInputContentString::moveCursor(int dir)
+{
+       int old_cursor = m_cursor;
+       
+       switch (dir)
+       {
+       case dirLeft:
+               --m_cursor;
+               break;
+       case dirRight:
+               ++m_cursor;
+               break;
+       case dirHome:
+               m_cursor = 0;
+               break;
+       case dirEnd:
+               m_cursor = m_len;
+               break;
+       }
+       
+       if (m_cursor < 0)
+               m_cursor = 0;
+       if (m_cursor > m_len)
+               m_cursor = m_len;
+       
+       if (m_cursor != old_cursor)
+               if (m_input)
+                       m_input->invalidate();
+}
+
+int eInputContentString::haveKey(int code, int overwrite)
+{
+       int have_char = -1;
+
+       if (code >= 0x8020)
+               have_char = code &~ 0x8000;
+       
+       if (have_char != -1)
+       {
+               if (overwrite && m_cursor < m_len)
+                       m_string[m_cursor] = have_char;
+               else
+               {
+                       m_string.insert(m_cursor, 1, have_char);
+                       ++m_len;
+               }
+               
+               m_cursor++;
+               
+               assert(m_cursor <= m_len);
+               if (m_input)
+                       m_input->invalidate();
+               return 1;
+       }
+       return 0;
+}
+
+void eInputContentString::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;
+       
+       if (!m_len)
+               return;
+       
+       m_string.erase(m_cursor - 1, m_cursor);
+       
+       m_len--;
+       m_cursor--;
+
+       if (m_input)
+               m_input->invalidate();
+}
+
+int eInputContentString::isValid()
+{
+       return 1;
+}
+
+void eInputContentString::validate()
+{
+}
+
+void eInputContentString::setText(const std::string &str)
+{
+       m_string = str;
+       m_len = m_string.size();
+       if (m_cursor > m_len)
+               m_cursor = m_len;
+       
+       if (m_input)
+               m_input->invalidate();
+}
+
+std::string eInputContentString::getText()
+{
+       return m_string;
+}
diff --git a/lib/gui/einputstring.h b/lib/gui/einputstring.h
new file mode 100644 (file)
index 0000000..a4eb52a
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef __lib_gui_einputstring_h
+#define __lib_gui_einputstring_h
+
+#include <lib/gui/einput.h>
+
+class eInputContentString: public eInputContent
+{
+       DECLARE_REF(eInputContentString);
+public:
+       eInputContentString();
+
+       void getDisplay(std::string &res, int &cursor);
+       void moveCursor(int dir);
+       int haveKey(int code, int overwrite);
+       void deleteChar(int dir);
+       int isValid();
+       
+       void validate();
+       
+       void setText(const std::string &text);
+       std::string getText();
+       
+private:
+       void insertChar(int pos, int ch);
+       
+       std::string m_string;
+       
+       int m_cursor, m_len;
+};
+
+#endif