]>
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 */
18 static void entryDraw(newtComponent co
);
19 static void entryDestroy(newtComponent co
);
20 static struct eventResult
entryEvent(struct newtComponent
* co
,
23 static struct eventResult
entryHandleKey(struct newtComponent
* co
, int key
);
25 static struct componentOps entryOps
= {
31 newtComponent
newtEntry(int left
, int top
, char * initialValue
, int width
,
32 char ** resultPtr
, int flags
) {
36 co
= malloc(sizeof(*co
));
37 en
= malloc(sizeof(struct entry
));
49 en
->cursorPosition
= 0;
52 en
->bufAlloced
= width
+ 1;
54 if (initialValue
&& strlen(initialValue
) > width
) {
55 en
->bufAlloced
= strlen(initialValue
) + 1;
57 en
->buf
= malloc(en
->bufAlloced
);
59 en
->resultPtr
= resultPtr
;
61 memset(en
->buf
, 0, en
->bufAlloced
);
63 strcpy(en
->buf
, initialValue
);
64 en
->bufUsed
= strlen(initialValue
);
70 static void entryDraw(newtComponent co
) {
71 struct entry
* en
= co
->data
;
76 if (co
->top
== -1) return;
78 if (en
->flags
& NEWT_ENTRY_HIDDEN
) {
79 newtGotorc(co
->top
, co
->left
);
80 SLsmg_set_color(COLORSET_ENTRY
);
81 for (i
= 0; i
< co
->width
; i
++)
82 SLsmg_write_char('_');
83 newtGotorc(co
->top
, co
->left
);
88 newtGotorc(co
->top
, co
->left
);
89 SLsmg_set_color(COLORSET_ENTRY
);
91 if (en
->cursorPosition
< en
->firstChar
) {
92 /* scroll to the left */
93 en
->firstChar
= en
->cursorPosition
;
94 } else if ((en
->firstChar
+ co
->width
) <= en
->cursorPosition
) {
95 /* scroll to the right */
96 en
->firstChar
= en
->cursorPosition
- co
->width
+ 1;
99 chptr
= en
->buf
+ en
->firstChar
;
102 if (len
<= co
->width
) {
104 SLsmg_write_string(chptr
);
105 while (i
< co
->width
) {
106 SLsmg_write_char('_');
110 SLsmg_write_nstring(chptr
, co
->width
);
113 newtGotorc(co
->top
, co
->left
+ (en
->cursorPosition
- en
->firstChar
));
116 static void entryDestroy(newtComponent co
) {
117 struct entry
* en
= co
->data
;
124 static struct eventResult
entryEvent(struct newtComponent
* co
,
126 struct entry
* en
= co
->data
;
127 struct eventResult er
;
131 /*SLtt_set_cursor_visibility(0);*/
132 newtGotorc(co
->top
, co
->left
+ (en
->cursorPosition
- en
->firstChar
));
133 er
.result
= ER_SWALLOWED
;
137 /*SLtt_set_cursor_visibility(1);*/
139 er
.result
= ER_SWALLOWED
;
143 er
= entryHandleKey(co
, ev
.u
.key
);
150 static struct eventResult
entryHandleKey(struct newtComponent
* co
, int key
) {
151 struct entry
* en
= co
->data
;
152 struct eventResult er
;
153 char * chptr
, * insPoint
;
155 er
.result
= ER_SWALLOWED
;
157 case '\001': /* ^A */
159 en
->cursorPosition
= 0;
162 case '\005': /* ^E */
164 en
->cursorPosition
= en
->bufUsed
;
167 case '\013': /* ^K */
168 en
->bufUsed
= en
->cursorPosition
;
169 memset(en
->buf
+ en
->bufUsed
, 0, en
->bufAlloced
- en
->bufUsed
);
172 case '\002': /* ^B */
174 if (en
->cursorPosition
)
175 en
->cursorPosition
--;
179 case NEWT_KEY_DELETE
:
180 chptr
= en
->buf
+ en
->cursorPosition
;
184 *(chptr
- 1) = *chptr
;
193 if (en
->cursorPosition
) {
194 /* if this isn't true, there's nothing to erase */
195 chptr
= en
->buf
+ en
->cursorPosition
;
197 en
->cursorPosition
--;
199 *(chptr
- 1) = *chptr
;
206 case '\006': /* ^B */
208 if (en
->cursorPosition
< en
->bufUsed
)
209 en
->cursorPosition
++;
213 if ((key
>= 0x20 && key
<= 0x7e) || (key
>= 0xa0 && key
<= 0xff)) {
214 if (!(en
->flags
& NEWT_ENTRY_SCROLL
) && en
->bufUsed
== co
->width
) {
219 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
220 en
->bufAlloced
+= 20;
221 en
->buf
= realloc(en
->buf
, en
->bufAlloced
);
222 *en
->resultPtr
= en
->buf
;
223 memset(en
->buf
+ en
->bufUsed
+ 1, 0, 20);
226 if (en
->cursorPosition
== en
->bufUsed
) {
229 /* insert the new character */
231 /* chptr is the last character in the string */
232 chptr
= (en
->buf
+ en
->bufUsed
) - 1;
233 if ((en
->bufUsed
+ 1) == en
->bufAlloced
) {
234 /* this string fills the buffer, so clip it */
239 insPoint
= en
->buf
+ en
->cursorPosition
;
241 while (chptr
>= insPoint
) {
242 *(chptr
+ 1) = *chptr
;
248 en
->buf
[en
->cursorPosition
++] = key
;
250 er
.result
= ER_IGNORED
;