]>
git.ipfire.org Git - thirdparty/newt.git/blob - entry.c
2 #include <slang/slang.h>
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(struct newtComponent
* co
,
24 static struct eventResult
entryHandleKey(struct newtComponent
* co
, int key
);
26 static struct componentOps entryOps
= {
32 void newtEntrySet(newtComponent co
, char * value
, int cursorAtEnd
) {
33 struct entry
* en
= co
->data
;
35 if ((strlen(value
) + 1) > en
->bufAlloced
) {
37 en
->bufAlloced
= strlen(value
) + 1;
38 en
->buf
= malloc(en
->bufAlloced
);
39 *en
->resultPtr
= en
->buf
;
41 memset(en
->buf
, 0, en
->bufAlloced
); /* clear the buffer */
42 strcpy(en
->buf
, value
);
43 en
->bufUsed
= strlen(value
);
46 en
->cursorPosition
= en
->bufUsed
;
48 en
->cursorPosition
= 0;
53 newtComponent
newtEntry(int left
, int top
, char * initialValue
, int width
,
54 char ** resultPtr
, int flags
) {
58 co
= malloc(sizeof(*co
));
59 en
= malloc(sizeof(struct entry
));
72 en
->cursorPosition
= 0;
75 en
->bufAlloced
= width
+ 1;
77 if (initialValue
&& strlen(initialValue
) > width
) {
78 en
->bufAlloced
= strlen(initialValue
) + 1;
80 en
->buf
= malloc(en
->bufAlloced
);
82 en
->resultPtr
= resultPtr
;
84 memset(en
->buf
, 0, en
->bufAlloced
);
86 strcpy(en
->buf
, initialValue
);
87 en
->bufUsed
= strlen(initialValue
);
88 en
->cursorPosition
= en
->bufUsed
;
94 static void entryDraw(newtComponent co
) {
95 struct entry
* en
= co
->data
;
100 if (co
->top
== -1) return;
102 if (en
->flags
& NEWT_ENTRY_HIDDEN
) {
103 newtGotorc(co
->top
, co
->left
);
104 SLsmg_set_color(COLORSET_ENTRY
);
105 for (i
= 0; i
< co
->width
; i
++)
106 SLsmg_write_char('_');
107 newtGotorc(co
->top
, co
->left
);
112 newtGotorc(co
->top
, co
->left
);
113 SLsmg_set_color(COLORSET_ENTRY
);
115 if (en
->cursorPosition
< en
->firstChar
) {
116 /* scroll to the left */
117 en
->firstChar
= en
->cursorPosition
;
118 } else if ((en
->firstChar
+ co
->width
) <= en
->cursorPosition
) {
119 /* scroll to the right */
120 en
->firstChar
= en
->cursorPosition
- co
->width
+ 1;
123 chptr
= en
->buf
+ en
->firstChar
;
126 if (len
<= co
->width
) {
128 SLsmg_write_string(chptr
);
129 while (i
< co
->width
) {
130 SLsmg_write_char('_');
134 SLsmg_write_nstring(chptr
, co
->width
);
137 if (en
->flags
& NEWT_ENTRY_HIDDEN
)
138 newtGotorc(co
->top
, co
->left
);
140 newtGotorc(co
->top
, co
->left
+ (en
->cursorPosition
- en
->firstChar
));
143 static void entryDestroy(newtComponent co
) {
144 struct entry
* en
= co
->data
;
151 static struct eventResult
entryEvent(struct newtComponent
* co
,
153 struct entry
* en
= co
->data
;
154 struct eventResult er
;
156 if (ev
.when
== EV_NORMAL
) {
159 /*SLtt_set_cursor_visibility(0);*/
160 if (en
->flags
& NEWT_ENTRY_HIDDEN
)
161 newtGotorc(co
->top
, co
->left
);
163 newtGotorc(co
->top
, co
->left
+
164 (en
->cursorPosition
- en
->firstChar
));
165 er
.result
= ER_SWALLOWED
;
169 /*SLtt_set_cursor_visibility(1);*/
171 er
.result
= ER_SWALLOWED
;
172 if (co
->callback
) co
->callback(co
, co
->callbackData
);
176 er
= entryHandleKey(co
, ev
.u
.key
);
180 er
.result
= ER_IGNORED
;
185 static struct eventResult
entryHandleKey(struct newtComponent
* co
, int key
) {
186 struct entry
* en
= co
->data
;
187 struct eventResult er
;
188 char * chptr
, * insPoint
;
190 er
.result
= ER_SWALLOWED
;
192 case '\r': /* Return */
193 if (en
->flags
& NEWT_ENTRY_RETURNEXIT
) {
194 er
.result
= ER_EXITFORM
;
196 er
.result
= ER_NEXTCOMP
;
200 case '\001': /* ^A */
202 en
->cursorPosition
= 0;
205 case '\005': /* ^E */
207 en
->cursorPosition
= en
->bufUsed
;
210 case '\013': /* ^K */
211 en
->bufUsed
= en
->cursorPosition
;
212 memset(en
->buf
+ en
->bufUsed
, 0, en
->bufAlloced
- en
->bufUsed
);
215 case '\002': /* ^B */
217 if (en
->cursorPosition
)
218 en
->cursorPosition
--;
222 case NEWT_KEY_DELETE
:
223 chptr
= en
->buf
+ en
->cursorPosition
;
227 *(chptr
- 1) = *chptr
;
236 if (en
->cursorPosition
) {
237 /* if this isn't true, there's nothing to erase */
238 chptr
= en
->buf
+ en
->cursorPosition
;
240 en
->cursorPosition
--;
242 *(chptr
- 1) = *chptr
;
249 case '\006': /* ^B */
251 if (en
->cursorPosition
< en
->bufUsed
)
252 en
->cursorPosition
++;
256 if ((key
>= 0x20 && key
<= 0x7e) || (key
>= 0xa0 && key
<= 0xff)) {
257 if (!(en
->flags
& NEWT_ENTRY_SCROLL
) && en
->bufUsed
== co
->width
) {
262 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
263 en
->bufAlloced
+= 20;
264 en
->buf
= realloc(en
->buf
, en
->bufAlloced
);
265 *en
->resultPtr
= en
->buf
;
266 memset(en
->buf
+ en
->bufUsed
+ 1, 0, 20);
269 if (en
->cursorPosition
== en
->bufUsed
) {
272 /* insert the new character */
274 /* chptr is the last character in the string */
275 chptr
= (en
->buf
+ en
->bufUsed
) - 1;
276 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
277 /* this string fills the buffer, so clip it */
282 insPoint
= en
->buf
+ en
->cursorPosition
;
284 while (chptr
>= insPoint
) {
285 *(chptr
+ 1) = *chptr
;
291 en
->buf
[en
->cursorPosition
++] = key
;
293 er
.result
= ER_IGNORED
;