1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
#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;
ePtr<eActionMap> ptr;
eActionMap::getInstance(ptr);
ptr->bindAction("InputActions", 0, 0, this);
// bind all keys
ptr->bindAction("", 0, 1, this);
}
eInput::~eInput()
{
ePtr<eActionMap> ptr;
eActionMap::getInstance(ptr);
ptr->unbindAction(this, 0);
ptr->unbindAction(this, 1);
}
void eInput::setContent(eInputContent *content)
{
if (m_content)
m_content->setInput(0);
m_content = content;
if (m_content)
m_content->setInput(this);
}
int eInput::event(int event, void *data, void *data2)
{
switch (event)
{
case evtPaint:
{
gPainter &painter = *(gPainter*)data2;
ePtr<eWindowStyle> style;
getStyle(style);
eWidget::event(event, data, data2);
ePtr<eTextPara> para = new eTextPara(eRect(0, 0, size().width(), size().height()));
std::string text;
int cursor = -1;
if (m_content)
m_content->getDisplay(text, cursor);
eDebug("cursor is %d", cursor);
para->setFont(m_font);
para->renderString(text, 0);
int glyphs = para->size();
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.renderPara(para, ePoint(0, 0));
return 0;
}
case evtAction:
if (isVisible())
{
switch((int)data2)
{
case moveLeft:
m_content->moveCursor(eInputContent::dirLeft);
break;
case moveRight:
m_content->moveCursor(eInputContent::dirRight);
break;
case moveHome:
m_content->moveCursor(eInputContent::dirHome);
break;
case moveEnd:
m_content->moveCursor(eInputContent::dirEnd);
break;
case deleteForward:
m_content->deleteChar(eInputContent::deleteForward);
break;
case deleteBackward:
m_content->deleteChar(eInputContent::deleteBackward);
break;
}
return 1;
}
return 0;
case evtKey:
{
int key = (int)data;
int flags = (int)data2;
if (m_content && !(flags & 1))
m_content->haveKey(key);
break;
}
default:
break;
}
return eLabel::event(event, data, data2);
}
int eInput::getNumber()
{
return atoi(m_text.c_str());
}
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 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);
}
if (have_digit != -1)
{
insertDigit(m_cursor, have_digit);
m_cursor++;
recalcLen();
// can happen when 0 -> x
if (m_cursor > m_len)
m_cursor = m_len;
if (m_input)
m_input->invalidate();
}
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;
}
|