14
int bufUsed; /* amount of the buffer that's been used */
15
int cursorPosition; /* cursor *in the string* on on screen */
16
int firstChar; /* first character position being shown */
17
newtEntryFilter filter;
21
static void entryDraw(newtComponent co);
22
static void entryDestroy(newtComponent co);
23
static struct eventResult entryEvent(newtComponent co,
26
static struct eventResult entryHandleKey(newtComponent co, int key);
28
static struct componentOps entryOps = {
32
newtDefaultPlaceHandler,
33
newtDefaultMappedHandler,
36
void newtEntrySet(newtComponent co, const char * value, int cursorAtEnd) {
37
struct entry * en = co->data;
39
if ((strlen(value) + 1) > (unsigned int)en->bufAlloced) {
41
en->bufAlloced = strlen(value) + 1;
42
en->buf = malloc(en->bufAlloced);
43
if (en->resultPtr) *en->resultPtr = en->buf;
45
memset(en->buf, 0, en->bufAlloced); /* clear the buffer */
46
strcpy(en->buf, value);
47
en->bufUsed = strlen(value);
50
en->cursorPosition = en->bufUsed;
52
en->cursorPosition = 0;
57
newtComponent newtEntry(int left, int top, const char * initialValue, int width,
58
char ** resultPtr, int flags) {
62
co = malloc(sizeof(*co));
63
en = malloc(sizeof(struct entry));
76
en->cursorPosition = 0;
79
en->bufAlloced = width + 1;
82
if (!(en->flags & NEWT_FLAG_DISABLED))
87
if (initialValue && strlen(initialValue) > (unsigned int)width) {
88
en->bufAlloced = strlen(initialValue) + 1;
90
en->buf = malloc(en->bufAlloced);
91
en->resultPtr = resultPtr;
92
if (en->resultPtr) *en->resultPtr = en->buf;
94
memset(en->buf, 0, en->bufAlloced);
96
strcpy(en->buf, initialValue);
97
en->bufUsed = strlen(initialValue);
98
en->cursorPosition = en->bufUsed;
102
en->cursorPosition = 0;
108
static void entryDraw(newtComponent co) {
109
struct entry * en = co->data;
114
if (!co->isMapped) return;
116
if (en->flags & NEWT_FLAG_DISABLED)
117
SLsmg_set_color(NEWT_COLORSET_DISENTRY);
119
SLsmg_set_color(NEWT_COLORSET_ENTRY);
121
if (en->flags & NEWT_FLAG_HIDDEN) {
122
newtGotorc(co->top, co->left);
123
for (i = 0; i < co->width; i++)
124
SLsmg_write_char('_');
125
newtGotorc(co->top, co->left);
130
newtGotorc(co->top, co->left);
132
if (en->cursorPosition < en->firstChar) {
133
/* scroll to the left */
134
en->firstChar = en->cursorPosition;
135
} else if ((en->firstChar + co->width) <= en->cursorPosition) {
136
/* scroll to the right */
137
en->firstChar = en->cursorPosition - co->width + 1;
140
chptr = en->buf + en->firstChar;
142
if (en->flags & NEWT_FLAG_PASSWORD) {
145
tmpptr = alloca(strlen(chptr+2));
146
strcpy(tmpptr, chptr);
147
for (p = tmpptr; *p; p++)
154
if (len <= co->width) {
156
SLsmg_write_string(chptr);
157
while (i < co->width) {
158
SLsmg_write_char('_');
162
SLsmg_write_nstring(chptr, co->width);
165
if (en->flags & NEWT_FLAG_HIDDEN)
166
newtGotorc(co->top, co->left);
168
newtGotorc(co->top, co->left + (en->cursorPosition - en->firstChar));
171
void newtEntrySetFlags(newtComponent co, int flags, enum newtFlagsSense sense) {
172
struct entry * en = co->data;
175
en->flags = newtSetFlags(en->flags, flags, sense);
177
if (!(en->flags & NEWT_FLAG_DISABLED))
182
newtGetrc(&row, &col);
184
newtGotorc(row, col);
187
static void entryDestroy(newtComponent co) {
188
struct entry * en = co->data;
195
static struct eventResult entryEvent(newtComponent co,
197
struct entry * en = co->data;
198
struct eventResult er;
201
if (ev.when == EV_NORMAL) {
205
if (en->flags & NEWT_FLAG_HIDDEN)
206
newtGotorc(co->top, co->left);
208
newtGotorc(co->top, co->left +
209
(en->cursorPosition - en->firstChar));
210
er.result = ER_SWALLOWED;
216
er.result = ER_SWALLOWED;
218
co->callback(co, co->callbackData);
224
ch = en->filter(co, en->filterData, ch, en->cursorPosition);
225
if (ch) er = entryHandleKey(co, ch);
229
if ((ev.u.mouse.type == MOUSE_BUTTON_DOWN) &&
230
(en->flags ^ NEWT_FLAG_HIDDEN)) {
231
if (strlen(en->buf) >= ev.u.mouse.x - co->left) {
232
en->cursorPosition = ev.u.mouse.x - co->left;
234
co->left +(en->cursorPosition - en->firstChar));
236
en->cursorPosition = strlen(en->buf);
238
co->left +(en->cursorPosition - en->firstChar));
244
er.result = ER_IGNORED;
249
static struct eventResult entryHandleKey(newtComponent co, int key) {
250
struct entry * en = co->data;
251
struct eventResult er;
252
char * chptr, * insPoint;
254
er.result = ER_SWALLOWED;
256
case '\r': /* Return */
257
if (en->flags & NEWT_FLAG_RETURNEXIT) {
258
er.result = ER_EXITFORM;
260
er.result = ER_NEXTCOMP;
264
case '\001': /* ^A */
266
en->cursorPosition = 0;
269
case '\005': /* ^E */
271
en->cursorPosition = en->bufUsed;
274
case '\013': /* ^K */
275
en->bufUsed = en->cursorPosition;
276
memset(en->buf + en->bufUsed, 0, en->bufAlloced - en->bufUsed);
279
case '\002': /* ^B */
281
if (en->cursorPosition)
282
en->cursorPosition--;
286
case NEWT_KEY_DELETE:
287
chptr = en->buf + en->cursorPosition;
291
*(chptr - 1) = *chptr;
300
if (en->cursorPosition) {
301
/* if this isn't true, there's nothing to erase */
302
chptr = en->buf + en->cursorPosition;
304
en->cursorPosition--;
306
*(chptr - 1) = *chptr;
313
case '\006': /* ^B */
315
if (en->cursorPosition < en->bufUsed)
316
en->cursorPosition++;
320
if ((key >= 0x20 && key <= 0x7e) || (key >= 0xa0 && key <= 0xff)) {
321
if (!(en->flags & NEWT_FLAG_SCROLL) && en->bufUsed >= co->width) {
326
if ((en->bufUsed + 1) == en->bufAlloced) {
327
en->bufAlloced += 20;
328
en->buf = realloc(en->buf, en->bufAlloced);
329
if (en->resultPtr) *en->resultPtr = en->buf;
330
memset(en->buf + en->bufUsed + 1, 0, 20);
333
if (en->cursorPosition == en->bufUsed) {
336
/* insert the new character */
338
/* chptr is the last character in the string */
339
chptr = (en->buf + en->bufUsed) - 1;
340
if ((en->bufUsed + 1) == en->bufAlloced) {
341
/* this string fills the buffer, so clip it */
346
insPoint = en->buf + en->cursorPosition;
348
while (chptr >= insPoint) {
349
*(chptr + 1) = *chptr;
355
en->buf[en->cursorPosition++] = key;
357
er.result = ER_IGNORED;
366
char * newtEntryGetValue(newtComponent co) {
367
struct entry * en = co->data;
372
void newtEntrySetFilter(newtComponent co, newtEntryFilter filter, void * data) {
373
struct entry * en = co->data;
375
en->filterData = data;