]>
git.ipfire.org Git - thirdparty/newt.git/blob - entry.c
18 const char ** resultPtr
;
20 int bufUsed
; /* amount of the buffer that's been used */
21 int cursorPosition
; /* cursor *in the string* on on screen */
22 int firstChar
; /* first character position being shown */
23 newtEntryFilter filter
;
27 static void entryDraw(newtComponent co
);
28 static void entryDestroy(newtComponent co
);
29 static struct eventResult
entryEvent(newtComponent co
,
32 static struct eventResult
entryHandleKey(newtComponent co
, int key
);
34 static struct componentOps entryOps
= {
38 newtDefaultPlaceHandler
,
39 newtDefaultMappedHandler
,
42 void newtEntrySet(newtComponent co
, const char * value
, int cursorAtEnd
) {
43 struct entry
* en
= co
->data
;
45 if ((strlen(value
) + 1) > (unsigned int)en
->bufAlloced
) {
47 en
->bufAlloced
= strlen(value
) + 1;
48 en
->buf
= malloc(en
->bufAlloced
);
49 if (en
->resultPtr
) *en
->resultPtr
= en
->buf
;
51 memset(en
->buf
, 0, en
->bufAlloced
); /* clear the buffer */
52 strcpy(en
->buf
, value
);
53 en
->bufUsed
= strlen(value
);
56 en
->cursorPosition
= en
->bufUsed
;
58 en
->cursorPosition
= 0;
63 newtComponent
newtEntry(int left
, int top
, const char * initialValue
, int width
,
64 const char ** resultPtr
, int flags
) {
68 co
= malloc(sizeof(*co
));
69 en
= malloc(sizeof(struct entry
));
82 en
->cursorPosition
= 0;
85 en
->bufAlloced
= width
+ 1;
88 if (!(en
->flags
& NEWT_FLAG_DISABLED
))
93 if (initialValue
&& wstrlen(initialValue
,-1) > (unsigned int)width
) {
94 en
->bufAlloced
= wstrlen(initialValue
,-1) + 1;
96 en
->buf
= malloc(en
->bufAlloced
);
97 en
->resultPtr
= resultPtr
;
98 if (en
->resultPtr
) *en
->resultPtr
= en
->buf
;
100 memset(en
->buf
, 0, en
->bufAlloced
);
102 strcpy(en
->buf
, initialValue
);
103 en
->bufUsed
= strlen(initialValue
);
104 en
->cursorPosition
= en
->bufUsed
;
108 en
->cursorPosition
= 0;
114 static void entryDraw(newtComponent co
) {
115 struct entry
* en
= co
->data
;
120 if (!co
->isMapped
) return;
122 if (en
->flags
& NEWT_FLAG_DISABLED
)
123 SLsmg_set_color(NEWT_COLORSET_DISENTRY
);
125 SLsmg_set_color(NEWT_COLORSET_ENTRY
);
127 if (en
->flags
& NEWT_FLAG_HIDDEN
) {
128 newtGotorc(co
->top
, co
->left
);
129 for (i
= 0; i
< co
->width
; i
++)
130 SLsmg_write_char('_');
131 newtGotorc(co
->top
, co
->left
);
136 newtGotorc(co
->top
, co
->left
);
138 if (en
->cursorPosition
< en
->firstChar
) {
139 /* scroll to the left */
140 en
->firstChar
= en
->cursorPosition
;
141 } else if ((en
->firstChar
+ co
->width
) <= en
->cursorPosition
) {
142 /* scroll to the right */
143 en
->firstChar
= en
->cursorPosition
- co
->width
+ 1;
146 chptr
= en
->buf
+ en
->firstChar
;
148 if (en
->flags
& NEWT_FLAG_PASSWORD
) {
151 tmpptr
= alloca(strlen(chptr
)+2);
152 strcpy(tmpptr
, chptr
);
153 for (p
= tmpptr
; *p
; p
++)
158 len
= wstrlen(chptr
, -1);
160 if (len
<= co
->width
) {
162 SLsmg_write_string(chptr
);
163 while (i
< co
->width
) {
164 SLsmg_write_char('_');
168 SLsmg_write_nstring(chptr
, co
->width
);
171 if (en
->flags
& NEWT_FLAG_HIDDEN
)
172 newtGotorc(co
->top
, co
->left
);
174 newtGotorc(co
->top
, co
->left
+ (en
->cursorPosition
- en
->firstChar
));
177 void newtEntrySetFlags(newtComponent co
, int flags
, enum newtFlagsSense sense
) {
178 struct entry
* en
= co
->data
;
181 en
->flags
= newtSetFlags(en
->flags
, flags
, sense
);
183 if (!(en
->flags
& NEWT_FLAG_DISABLED
))
188 newtGetrc(&row
, &col
);
190 newtGotorc(row
, col
);
193 static void entryDestroy(newtComponent co
) {
194 struct entry
* en
= co
->data
;
201 static struct eventResult
entryEvent(newtComponent co
,
203 struct entry
* en
= co
->data
;
204 struct eventResult er
;
207 if (ev
.when
== EV_NORMAL
) {
211 if (en
->flags
& NEWT_FLAG_HIDDEN
)
212 newtGotorc(co
->top
, co
->left
);
214 newtGotorc(co
->top
, co
->left
+
215 (en
->cursorPosition
- en
->firstChar
));
216 er
.result
= ER_SWALLOWED
;
222 er
.result
= ER_SWALLOWED
;
224 co
->callback(co
, co
->callbackData
);
230 ch
= en
->filter(co
, en
->filterData
, ch
, en
->cursorPosition
);
231 if (ch
) er
= entryHandleKey(co
, ch
);
235 if ((ev
.u
.mouse
.type
== MOUSE_BUTTON_DOWN
) &&
236 (en
->flags
^ NEWT_FLAG_HIDDEN
)) {
237 if (strlen(en
->buf
) >= ev
.u
.mouse
.x
- co
->left
) {
238 en
->cursorPosition
= ev
.u
.mouse
.x
- co
->left
;
240 co
->left
+(en
->cursorPosition
- en
->firstChar
));
242 en
->cursorPosition
= strlen(en
->buf
);
244 co
->left
+(en
->cursorPosition
- en
->firstChar
));
250 er
.result
= ER_IGNORED
;
255 static struct eventResult
entryHandleKey(newtComponent co
, int key
) {
256 struct entry
* en
= co
->data
;
257 struct eventResult er
;
258 char * chptr
, * insPoint
;
260 er
.result
= ER_SWALLOWED
;
262 case '\r': /* Return */
263 if (en
->flags
& NEWT_FLAG_RETURNEXIT
) {
264 er
.result
= ER_EXITFORM
;
266 er
.result
= ER_NEXTCOMP
;
270 case '\001': /* ^A */
272 en
->cursorPosition
= 0;
275 case '\005': /* ^E */
277 en
->cursorPosition
= en
->bufUsed
;
280 case '\013': /* ^K */
281 en
->bufUsed
= en
->cursorPosition
;
282 memset(en
->buf
+ en
->bufUsed
, 0, en
->bufAlloced
- en
->bufUsed
);
285 case '\002': /* ^B */
287 if (en
->cursorPosition
)
288 en
->cursorPosition
--;
292 case NEWT_KEY_DELETE
:
293 chptr
= en
->buf
+ en
->cursorPosition
;
297 *(chptr
- 1) = *chptr
;
306 if (en
->cursorPosition
) {
307 /* if this isn't true, there's nothing to erase */
308 chptr
= en
->buf
+ en
->cursorPosition
;
310 en
->cursorPosition
--;
312 *(chptr
- 1) = *chptr
;
319 case '\006': /* ^B */
321 if (en
->cursorPosition
< en
->bufUsed
)
322 en
->cursorPosition
++;
326 if ((key
>= 0x20 && key
<= 0x7e) || (key
>= 0xa0 && key
<= 0xff)) {
327 if (!(en
->flags
& NEWT_FLAG_SCROLL
) && en
->bufUsed
>= co
->width
) {
332 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
333 en
->bufAlloced
+= 20;
334 en
->buf
= realloc(en
->buf
, en
->bufAlloced
);
335 if (en
->resultPtr
) *en
->resultPtr
= en
->buf
;
336 memset(en
->buf
+ en
->bufUsed
+ 1, 0, 20);
339 if (en
->cursorPosition
== en
->bufUsed
) {
342 /* insert the new character */
344 /* chptr is the last character in the string */
345 chptr
= (en
->buf
+ en
->bufUsed
) - 1;
346 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
347 /* this string fills the buffer, so clip it */
352 insPoint
= en
->buf
+ en
->cursorPosition
;
354 while (chptr
>= insPoint
) {
355 *(chptr
+ 1) = *chptr
;
361 en
->buf
[en
->cursorPosition
++] = key
;
363 er
.result
= ER_IGNORED
;
372 char * newtEntryGetValue(newtComponent co
) {
373 struct entry
* en
= co
->data
;
378 void newtEntrySetFilter(newtComponent co
, newtEntryFilter filter
, void * data
) {
379 struct entry
* en
= co
->data
;
381 en
->filterData
= data
;