|
|
1.1 root 1: /*
2: * Editor for input fields
3: *
4: * Copyright (c) 1990-93 by Udo Munk
5: */
6:
7: #ifdef AIX
8: #define NLS
9: #endif
10:
11: #include <curses.h>
12: #include <ctype.h>
13: #include <string.h>
14: #include "winfun.h"
15:
16: extern int tab_active;
17:
18: edit_field(w, y, x, s, l, e, fn)
19: WINDOW *w;
20: char *s;
21: int x, y, l, e;
22: int (*fn) ();
23: {
24: register char *p;
25: register int c;
26: int cur_x;
27:
28: cursoron();
29: redraw_field(w, y, x, s, l, e);
30: cur_x = x + strlen(s);
31: wmove(w, y, cur_x);
32: wrefresh(w);
33: while ((c = wgetch(w)) != '\r' && c != KEY_ENTER) {
34: switch (c) {
35: case KEY_LEFT: /* one character left */
36: if (cur_x > x) {
37: cur_x--;
38: wmove(w, y, cur_x);
39: }
40: break;
41: case KEY_RIGHT: /* one character right */
42: if ((cur_x < x + l) && (*(s + cur_x - x) != '\0')) {
43: cur_x++;
44: wmove(w, y, cur_x);
45: }
46: break;
47: case KEY_UP: /* get previous entry of a list */
48: if (fn != NULL) {
49: (*fn) (1, s);
50: redraw_field(w, y, x, s, l, e);
51: cur_x = x + strlen(s);
52: wmove(w, y, cur_x);
53: }
54: break;
55: case KEY_DOWN: /* get next entry of a list */
56: if (fn != NULL) {
57: (*fn) (-1, s);
58: redraw_field(w, y, x, s, l, e);
59: cur_x = x + strlen(s);
60: wmove(w, y, cur_x);
61: }
62: break;
63: case KEY_DC: /* remove character under cursor */
64: case W_KEY_DC:
65: if (cur_x < x + l) {
66: p = s + cur_x - x;
67: while (*p) {
68: *p = *(p + 1);
69: p++;
70: }
71: *p = '\0';
72: redraw_field(w, y, x, s, l, e);
73: wmove(w, y, cur_x);
74: }
75: break;
76: case 0177: /* remove character left from cursor */
77: case 010:
78: #ifdef KEY_BACKSPACE
79: #if KEY_BACKSPACE != 010
80: case KEY_BACKSPACE:
81: #endif
82: #endif
83: if (cur_x == x)
84: break;
85: cur_x--;
86: p = s + cur_x - x;
87: while (*p) {
88: *p = *(p + 1);
89: p++;
90: }
91: *p = '\0';
92: redraw_field(w, y, x, s, l, e);
93: wmove(w, y, cur_x);
94: break;
95: case W_KEY_DL: /* remove whole contents of field */
96: case KEY_DL:
97: cur_x = x;
98: p = s;
99: *p = '\0';
100: redraw_field(w, y, x, s, l, e);
101: wmove(w, y, cur_x);
102: break;
103: case KEY_PPAGE: /* move cursor to begin of field */
104: case W_KEY_PPAGE:
105: case KEY_HOME:
106: case W_KEY_HOME:
107: cur_x = x;
108: p = s;
109: wmove(w, y, cur_x);
110: break;
111: case KEY_NPAGE: /* move cursor to end of input */
112: case W_KEY_NPAGE:
113: case KEY_LL:
114: case W_KEY_LL:
115: cur_x = x + strlen(s);
116: p = s + strlen(s);
117: wmove(w, y, cur_x);
118: break;
119: case W_KEY_REFR:/* refresh screen */
120: #ifdef KEY_REFR
121: case KEY_REFR:
122: #endif
123: wrefresh(curscr);
124: break;
125: case 033: /* ESC, abort input */
126: cursoroff();
127: return(WIN_ABORT);
128: case W_KEY_TAB: /* go to next input field */
129: if (tab_active)
130: return(WIN_NEXTFIE);
131: break;
132: case W_KEY_BTAB:/* go to previous input field */
133: if (tab_active)
134: return(WIN_PREVFIE);
135: break;
136: default: /* compute input */
137: if ((c < ' ') || (c > 0176))
138: break;
139: if (cur_x == x + l)
140: break;
141: if (strlen(s) == l)
142: break;
143: if (*(s + cur_x - x) == '\0') {
144: *(s + cur_x - x) = c;
145: cur_x++;
146: *(s + cur_x - x) = '\0';
147: if (e)
148: waddch(w, c);
149: else
150: waddch(w, '#');
151: break;
152: }
153: p = s + strlen(s);
154: while (p != s + cur_x - x - 1) {
155: *(p + 1) = *p;
156: p--;
157: }
158: *++p = c;
159: redraw_field(w, y, x, s, l, e);
160: cur_x++;
161: wmove(w, y, cur_x);
162: break;
163: }
164: wrefresh(w);
165: }
166: if (strlen(s) > 1) {
167: p = s + strlen(s) - 1;
168: while (isspace(*p))
169: *p-- = '\0';
170: }
171: cursoroff();
172: return(WIN_OK);
173: }
174:
175: /*
176: * redraw input
177: */
178: static redraw_field(w, y, x, s, l, e)
179: WINDOW *w;
180: char *s;
181: int x, y, l, e;
182: {
183: wmove(w, y, x);
184: while (l--) {
185: if (*s) {
186: if (e)
187: waddch(w, *s);
188: else
189: waddch(w, '#');
190: s++;
191: } else
192: waddch(w, ' ');
193: }
194: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.