1 #include <lib/gui/einput.h>
2 #include <lib/gdi/font.h>
3 #include <lib/actions/action.h>
4 #include <linux/input.h>
6 eInput::eInput(eWidget *parent): eLabel(parent)
8 /* default to center alignment */
9 m_valign = alignCenter;
10 m_halign = alignCenter;
20 void eInput::setOverwriteMode(int m)
28 void eInput::setContent(eInputContent *content)
31 m_content->setInput(0);
34 m_content->setInput(this);
37 int eInput::event(int event, void *data, void *data2)
43 gPainter &painter = *(gPainter*)data2;
44 ePtr<eWindowStyle> style;
48 eWidget::event(event, data, data2);
50 ePtr<eTextPara> para = new eTextPara(eRect(0, 0, size().width(), size().height()));
56 m_content->getDisplay(text, cursor);
58 eDebug("cursor is %d", cursor);
59 para->setFont(m_font);
60 para->renderString(text, 0);
61 int glyphs = para->size();
63 if (m_mode && cursor < glyphs)
65 /* in overwrite mode, when not at end of line, invert the current cursor position. */
66 para->setGlyphFlag(cursor, GS_INVERT);
67 eRect bbox = para->getGlyphBBox(cursor);
68 bbox = eRect(bbox.left(), 0, bbox.width() + 2, size().height());
72 /* otherwise, insert small cursor */
76 bbox = para->getGlyphBBox(cursor);
77 bbox = eRect(bbox.left()-1, 0, 2, size().height());
80 bbox = para->getGlyphBBox(cursor - 1);
81 bbox = eRect(bbox.right(), 0, 2, size().height());
86 painter.renderPara(para, ePoint(0, 0));
96 m_content->moveCursor(eInputContent::dirLeft);
99 m_content->moveCursor(eInputContent::dirRight);
102 m_content->moveCursor(eInputContent::dirHome);
105 m_content->moveCursor(eInputContent::dirEnd);
108 m_content->deleteChar(eInputContent::deleteForward);
111 m_content->deleteChar(eInputContent::deleteBackward);
113 case toggleOverwrite:
114 setOverwriteMode(!m_mode);
123 int flags = (int)data2;
124 if (m_content && !(flags & 1)) // only make/repeat, no break
125 return m_content->haveKey(key, m_mode);
130 eDebug("focus got in %p", this);
131 ePtr<eActionMap> ptr;
132 eActionMap::getInstance(ptr);
133 ptr->bindAction("InputActions", 0, 0, this);
135 ptr->bindAction("", 0, 1, this);
140 eDebug("focus lostin %p", this);
141 ePtr<eActionMap> ptr;
142 eActionMap::getInstance(ptr);
143 ptr->unbindAction(this, 0);
144 ptr->unbindAction(this, 1);
150 return eLabel::event(event, data, data2);
153 int eInput::getNumber()
155 return atoi(m_text.c_str());
158 DEFINE_REF(eInputContentNumber);
160 void eInputContent::setInput(eInput *widget)
165 eInputContentNumber::eInputContentNumber(int cur, int min, int max)
175 void eInputContentNumber::getDisplay(std::string &res, int &cursor)
179 sprintf(r, "%d", m_value);
184 void eInputContentNumber::moveCursor(int dir)
186 eDebug("move cursor..");
187 int old_cursor = m_cursor;
207 if (m_cursor > m_len)
210 if (m_cursor != old_cursor)
212 m_input->invalidate();
215 int eInputContentNumber::haveKey(int code, int overwrite)
219 #define ASCII(x) (x | 0x8000)
220 #define DIGIT(x) case KEY_##x: case KEY_KP##x: case ASCII(x|0x30): have_digit=x; break;
237 if (have_digit != -1)
239 insertDigit(m_cursor, have_digit);
240 /* if overwrite and not end of line, erase char first. */
241 if (overwrite && m_cursor < m_len)
242 insertDigit(m_cursor + 1, -1);
247 // can happen when 0 -> x
248 if (m_cursor > m_len)
252 m_input->invalidate();
258 void eInputContentNumber::deleteChar(int dir)
260 if (dir == deleteForward)
263 if (m_cursor != m_len)
268 /* backward delete at begin */
271 insertDigit(m_cursor, -1);
277 m_input->invalidate();
280 int eInputContentNumber::isValid()
282 return m_value >= m_min && m_value <= m_max;
285 void eInputContentNumber::recalcLen()
295 if (!m_len) /* zero */
299 void eInputContentNumber::insertDigit(int pos, int dig)
301 /* get stuff left from cursor */
304 for (i = 0; i < (m_len - pos); ++i)
307 /* now it's 1...max */
308 int left = m_value / exp;
309 int right = m_value % exp;
315 } else if (dig == -1) /* delete */