]>
git.ipfire.org Git - thirdparty/newt.git/blob - entry.c
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 */
19 static void entryDraw(newtComponent co
);
20 static void entryDestroy(newtComponent co
);
21 static struct eventResult
entryEvent(newtComponent co
,
24 static struct eventResult
entryHandleKey(newtComponent co
, int key
);
26 static struct componentOps entryOps
= {
33 void newtEntrySet(newtComponent co
, const char * value
, int cursorAtEnd
) {
34 struct entry
* en
= co
->data
;
36 if ((strlen(value
) + 1) > (unsigned int)en
->bufAlloced
) {
38 en
->bufAlloced
= strlen(value
) + 1;
39 en
->buf
= malloc(en
->bufAlloced
);
40 *en
->resultPtr
= en
->buf
;
42 memset(en
->buf
, 0, en
->bufAlloced
); /* clear the buffer */
43 strcpy(en
->buf
, value
);
44 en
->bufUsed
= strlen(value
);
47 en
->cursorPosition
= en
->bufUsed
;
49 en
->cursorPosition
= 0;
54 newtComponent
newtEntry(int left
, int top
, const char * initialValue
, int width
,
55 char ** resultPtr
, int flags
) {
59 co
= malloc(sizeof(*co
));
60 en
= malloc(sizeof(struct entry
));
72 en
->cursorPosition
= 0;
75 en
->bufAlloced
= width
+ 1;
77 if (!(en
->flags
& NEWT_FLAG_DISABLED
))
82 if (initialValue
&& strlen(initialValue
) > (unsigned int)width
) {
83 en
->bufAlloced
= strlen(initialValue
) + 1;
85 en
->buf
= malloc(en
->bufAlloced
);
87 en
->resultPtr
= resultPtr
;
89 memset(en
->buf
, 0, en
->bufAlloced
);
91 strcpy(en
->buf
, initialValue
);
92 en
->bufUsed
= strlen(initialValue
);
93 en
->cursorPosition
= en
->bufUsed
;
97 en
->cursorPosition
= 0;
103 static void entryDraw(newtComponent co
) {
104 struct entry
* en
= co
->data
;
109 if (co
->top
== -1) return;
111 if (en
->flags
& NEWT_FLAG_DISABLED
)
112 SLsmg_set_color(NEWT_COLORSET_DISENTRY
);
114 SLsmg_set_color(NEWT_COLORSET_ENTRY
);
116 if (en
->flags
& NEWT_FLAG_HIDDEN
) {
117 newtGotorc(co
->top
, co
->left
);
118 for (i
= 0; i
< co
->width
; i
++)
119 SLsmg_write_char('_');
120 newtGotorc(co
->top
, co
->left
);
125 newtGotorc(co
->top
, co
->left
);
127 if (en
->cursorPosition
< en
->firstChar
) {
128 /* scroll to the left */
129 en
->firstChar
= en
->cursorPosition
;
130 } else if ((en
->firstChar
+ co
->width
) <= en
->cursorPosition
) {
131 /* scroll to the right */
132 en
->firstChar
= en
->cursorPosition
- co
->width
+ 1;
135 chptr
= en
->buf
+ en
->firstChar
;
138 if (len
<= co
->width
) {
140 SLsmg_write_string(chptr
);
141 while (i
< co
->width
) {
142 SLsmg_write_char('_');
146 SLsmg_write_nstring(chptr
, co
->width
);
149 if (en
->flags
& NEWT_FLAG_HIDDEN
)
150 newtGotorc(co
->top
, co
->left
);
152 newtGotorc(co
->top
, co
->left
+ (en
->cursorPosition
- en
->firstChar
));
155 void newtEntrySetFlags(newtComponent co
, int flags
, enum newtFlagsSense sense
) {
156 struct entry
* en
= co
->data
;
159 en
->flags
= newtSetFlags(en
->flags
, flags
, sense
);
161 if (!(en
->flags
& NEWT_FLAG_DISABLED
))
166 newtGetrc(&row
, &col
);
168 newtGotorc(row
, col
);
171 static void entryDestroy(newtComponent co
) {
172 struct entry
* en
= co
->data
;
179 static struct eventResult
entryEvent(newtComponent co
,
181 struct entry
* en
= co
->data
;
182 struct eventResult er
;
184 if (ev
.when
== EV_NORMAL
) {
187 /*SLtt_set_cursor_visibility(0);*/
188 if (en
->flags
& NEWT_FLAG_HIDDEN
)
189 newtGotorc(co
->top
, co
->left
);
191 newtGotorc(co
->top
, co
->left
+
192 (en
->cursorPosition
- en
->firstChar
));
193 er
.result
= ER_SWALLOWED
;
197 /*SLtt_set_cursor_visibility(1);*/
199 er
.result
= ER_SWALLOWED
;
200 if (co
->callback
) co
->callback(co
, co
->callbackData
);
204 er
= entryHandleKey(co
, ev
.u
.key
);
208 er
.result
= ER_IGNORED
;
213 static struct eventResult
entryHandleKey(newtComponent co
, int key
) {
214 struct entry
* en
= co
->data
;
215 struct eventResult er
;
216 char * chptr
, * insPoint
;
218 er
.result
= ER_SWALLOWED
;
220 case '\r': /* Return */
221 if (en
->flags
& NEWT_FLAG_RETURNEXIT
) {
222 er
.result
= ER_EXITFORM
;
224 er
.result
= ER_NEXTCOMP
;
228 case '\001': /* ^A */
230 en
->cursorPosition
= 0;
233 case '\005': /* ^E */
235 en
->cursorPosition
= en
->bufUsed
;
238 case '\013': /* ^K */
239 en
->bufUsed
= en
->cursorPosition
;
240 memset(en
->buf
+ en
->bufUsed
, 0, en
->bufAlloced
- en
->bufUsed
);
243 case '\002': /* ^B */
245 if (en
->cursorPosition
)
246 en
->cursorPosition
--;
250 case NEWT_KEY_DELETE
:
251 chptr
= en
->buf
+ en
->cursorPosition
;
255 *(chptr
- 1) = *chptr
;
264 if (en
->cursorPosition
) {
265 /* if this isn't true, there's nothing to erase */
266 chptr
= en
->buf
+ en
->cursorPosition
;
268 en
->cursorPosition
--;
270 *(chptr
- 1) = *chptr
;
277 case '\006': /* ^B */
279 if (en
->cursorPosition
< en
->bufUsed
)
280 en
->cursorPosition
++;
284 if ((key
>= 0x20 && key
<= 0x7e) || (key
>= 0xa0 && key
<= 0xff)) {
285 if (!(en
->flags
& NEWT_FLAG_SCROLL
) && en
->bufUsed
== co
->width
) {
290 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
291 en
->bufAlloced
+= 20;
292 en
->buf
= realloc(en
->buf
, en
->bufAlloced
);
293 *en
->resultPtr
= en
->buf
;
294 memset(en
->buf
+ en
->bufUsed
+ 1, 0, 20);
297 if (en
->cursorPosition
== en
->bufUsed
) {
300 /* insert the new character */
302 /* chptr is the last character in the string */
303 chptr
= (en
->buf
+ en
->bufUsed
) - 1;
304 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
305 /* this string fills the buffer, so clip it */
310 insPoint
= en
->buf
+ en
->cursorPosition
;
312 while (chptr
>= insPoint
) {
313 *(chptr
+ 1) = *chptr
;
319 en
->buf
[en
->cursorPosition
++] = key
;
321 er
.result
= ER_IGNORED
;