]>
git.ipfire.org Git - thirdparty/newt.git/blob - entry.c
2 #include <slang/slang.h>
13 int bufUsed
; /* amount of the buffer that's been used */
14 int cursorPosition
; /* cursor *in the string* on on screen */
15 int firstChar
; /* first character position being shown */
17 newtCallback callback
;
20 static void entryDraw(newtComponent co
);
21 static void entryDestroy(newtComponent co
);
22 static struct eventResult
entryEvent(struct newtComponent
* co
,
25 static struct eventResult
entryHandleKey(struct newtComponent
* co
, int key
);
27 static struct componentOps entryOps
= {
33 void newtEntrySet(newtComponent co
, char * value
, int cursorAtEnd
) {
34 struct entry
* en
= co
->data
;
36 if ((strlen(value
) + 1) > en
->bufAlloced
) {
38 en
->bufAlloced
= strlen(value
) + 1;
39 en
->buf
= malloc(en
->bufAlloced
);
40 *en
->resultPtr
= en
->buf
;
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
));
71 en
->cursorPosition
= 0;
74 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 void newtEntryAddCallback(newtComponent co
, newtCallback f
, void * data
) {
95 struct entry
* en
= co
->data
;
98 en
->callbackData
= data
;
101 static void entryDraw(newtComponent co
) {
102 struct entry
* en
= co
->data
;
107 if (co
->top
== -1) return;
109 if (en
->flags
& NEWT_ENTRY_HIDDEN
) {
110 newtGotorc(co
->top
, co
->left
);
111 SLsmg_set_color(COLORSET_ENTRY
);
112 for (i
= 0; i
< co
->width
; i
++)
113 SLsmg_write_char('_');
114 newtGotorc(co
->top
, co
->left
);
119 newtGotorc(co
->top
, co
->left
);
120 SLsmg_set_color(COLORSET_ENTRY
);
122 if (en
->cursorPosition
< en
->firstChar
) {
123 /* scroll to the left */
124 en
->firstChar
= en
->cursorPosition
;
125 } else if ((en
->firstChar
+ co
->width
) <= en
->cursorPosition
) {
126 /* scroll to the right */
127 en
->firstChar
= en
->cursorPosition
- co
->width
+ 1;
130 chptr
= en
->buf
+ en
->firstChar
;
133 if (len
<= co
->width
) {
135 SLsmg_write_string(chptr
);
136 while (i
< co
->width
) {
137 SLsmg_write_char('_');
141 SLsmg_write_nstring(chptr
, co
->width
);
144 newtGotorc(co
->top
, co
->left
+ (en
->cursorPosition
- en
->firstChar
));
147 static void entryDestroy(newtComponent co
) {
148 struct entry
* en
= co
->data
;
155 static struct eventResult
entryEvent(struct newtComponent
* co
,
157 struct entry
* en
= co
->data
;
158 struct eventResult er
;
160 if (ev
.when
== EV_NORMAL
) {
163 /*SLtt_set_cursor_visibility(0);*/
164 newtGotorc(co
->top
, co
->left
+
165 (en
->cursorPosition
- en
->firstChar
));
166 er
.result
= ER_SWALLOWED
;
170 /*SLtt_set_cursor_visibility(1);*/
172 er
.result
= ER_SWALLOWED
;
173 if (en
->callback
) en
->callback(co
, en
->callbackData
);
177 er
= entryHandleKey(co
, ev
.u
.key
);
181 er
.result
= ER_IGNORED
;
186 static struct eventResult
entryHandleKey(struct newtComponent
* co
, int key
) {
187 struct entry
* en
= co
->data
;
188 struct eventResult er
;
189 char * chptr
, * insPoint
;
191 er
.result
= ER_SWALLOWED
;
193 case '\r': /* Return */
194 if (en
->flags
& NEWT_ENTRY_RETURNEXIT
) {
195 er
.result
= ER_EXITFORM
;
197 er
.result
= ER_NEXTCOMP
;
201 case '\001': /* ^A */
203 en
->cursorPosition
= 0;
206 case '\005': /* ^E */
208 en
->cursorPosition
= en
->bufUsed
;
211 case '\013': /* ^K */
212 en
->bufUsed
= en
->cursorPosition
;
213 memset(en
->buf
+ en
->bufUsed
, 0, en
->bufAlloced
- en
->bufUsed
);
216 case '\002': /* ^B */
218 if (en
->cursorPosition
)
219 en
->cursorPosition
--;
223 case NEWT_KEY_DELETE
:
224 chptr
= en
->buf
+ en
->cursorPosition
;
228 *(chptr
- 1) = *chptr
;
237 if (en
->cursorPosition
) {
238 /* if this isn't true, there's nothing to erase */
239 chptr
= en
->buf
+ en
->cursorPosition
;
241 en
->cursorPosition
--;
243 *(chptr
- 1) = *chptr
;
250 case '\006': /* ^B */
252 if (en
->cursorPosition
< en
->bufUsed
)
253 en
->cursorPosition
++;
257 if ((key
>= 0x20 && key
<= 0x7e) || (key
>= 0xa0 && key
<= 0xff)) {
258 if (!(en
->flags
& NEWT_ENTRY_SCROLL
) && en
->bufUsed
== co
->width
) {
263 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
264 en
->bufAlloced
+= 20;
265 en
->buf
= realloc(en
->buf
, en
->bufAlloced
);
266 *en
->resultPtr
= en
->buf
;
267 memset(en
->buf
+ en
->bufUsed
+ 1, 0, 20);
270 if (en
->cursorPosition
== en
->bufUsed
) {
273 /* insert the new character */
275 /* chptr is the last character in the string */
276 chptr
= (en
->buf
+ en
->bufUsed
) - 1;
277 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
278 /* this string fills the buffer, so clip it */
283 insPoint
= en
->buf
+ en
->cursorPosition
;
285 while (chptr
>= insPoint
) {
286 *(chptr
+ 1) = *chptr
;
292 en
->buf
[en
->cursorPosition
++] = key
;
294 er
.result
= ER_IGNORED
;