]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dup_field.c
ncurses 6.2 - patch 20210925
[ncurses.git] / test / dup_field.c
1 /****************************************************************************
2  * Copyright 2020,2021 Thomas E. Dickey                                     *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: dup_field.c,v 1.2 2021/03/27 23:41:57 tom Exp $
30  *
31  * Demonstrate move_field().
32  */
33
34 #include <test.priv.h>
35
36 #if USE_LIBFORM
37
38 #include <edit_field.h>
39 #include <popup_msg.h>
40
41 #define MY_DEMO         EDIT_FIELD('f')
42
43 static char empty[] = "";
44 static FIELD *all_fields[100];
45 /* *INDENT-OFF* */
46 static struct {
47     int code;
48     int result;
49     const char *help;
50 } commands[] = {
51     { CTRL('A'),     REQ_BEG_FIELD,   "go to beginning of field" },
52     { CTRL('D'),     REQ_DOWN_FIELD,  "move downward to field" },
53     { CTRL('E'),     REQ_END_FIELD,   "go to end of field" },
54     { CTRL('G'),     MY_DEMO,         "move current field with cursor keys" },
55     { CTRL('H'),     REQ_DEL_PREV,    "delete previous character" },
56     { CTRL('I'),     REQ_NEXT_FIELD,  "go to next field" },
57     { CTRL('K'),     REQ_CLR_EOF,     "clear to end of field" },
58     { CTRL('N'),     REQ_NEXT_FIELD,  "go to next field" },
59     { CTRL('P'),     REQ_PREV_FIELD,  "go to previous field" },
60     { CTRL('Q'),     MY_QUIT,         "exit form" },
61     { CTRL('U'),     REQ_UP_FIELD,    "move upward to field" },
62     { CTRL('W'),     REQ_NEXT_WORD,   "go to next word" },
63     { CTRL('X'),     REQ_CLR_FIELD,   "clear field" },
64     { CTRL('['),     MY_QUIT,         "exit form" },
65     { KEY_F(1),      MY_HELP,         "show this screen", },
66     { KEY_BACKSPACE, REQ_DEL_PREV,    "delete previous character" },
67     { KEY_BTAB,      REQ_PREV_FIELD,  "go to previous field" },
68     { KEY_DOWN,      REQ_DOWN_CHAR,   "move down 1 character" },
69     { KEY_END,       REQ_LAST_FIELD,  "go to last field" },
70     { KEY_HOME,      REQ_FIRST_FIELD, "go to first field" },
71     { KEY_LEFT,      REQ_LEFT_CHAR,   "move left 1 character" },
72     { KEY_NEXT,      REQ_NEXT_FIELD,  "go to next field" },
73     { KEY_PREVIOUS,  REQ_PREV_FIELD,  "go to previous field" },
74     { KEY_RIGHT,     REQ_RIGHT_CHAR,  "move right 1 character" },
75     { KEY_UP,        REQ_UP_CHAR,     "move up 1 character" }
76 };
77 /* *INDENT-ON* */
78
79 static void
80 my_help_edit_field(void)
81 {
82     int used = 0;
83     unsigned n;
84     char **msgs = typeCalloc(char *, 3 + SIZEOF(commands));
85
86     msgs[used++] = strdup("Defined form edit/traversal keys:");
87     for (n = 0; n < SIZEOF(commands); ++n) {
88         char *msg;
89         const char *name;
90         const char *code = keyname(commands[n].code);
91         size_t need = 5;
92 #ifdef NCURSES_VERSION
93         if ((name = form_request_name(commands[n].result)) == 0)
94 #endif
95             name = commands[n].help;
96         need = 5 + strlen(code) + strlen(name);
97         msg = typeMalloc(char, need);
98         _nc_SPRINTF(msg, _nc_SLIMIT(need) "%s -- %s", code, name);
99         msgs[used++] = msg;
100     }
101     msgs[used++] =
102         strdup("Arrow keys move within a field as you would expect.");
103     msgs[used] = 0;
104     popup_msg2(stdscr, msgs);
105     for (n = 0; msgs[n] != 0; ++n) {
106         free(msgs[n]);
107     }
108     free(msgs);
109 }
110
111 static void
112 do_demo(FORM *form)
113 {
114 }
115
116 static FIELD *
117 make_label(const char *label, int frow, int fcol)
118 {
119     FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
120
121     if (f) {
122         set_field_buffer(f, 0, label);
123         set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
124     }
125     return (f);
126 }
127
128 static FIELD *
129 make_field(int frow, int fcol, int rows, int cols)
130 {
131     FIELD *f = new_field(rows, cols, frow, fcol, 0, 1);
132
133     if (f) {
134         set_field_back(f, A_UNDERLINE);
135         init_edit_field(f, empty);
136     }
137     return (f);
138 }
139
140 static void
141 erase_form(FORM *f)
142 {
143     WINDOW *w = form_win(f);
144     WINDOW *s = form_sub(f);
145
146     unpost_form(f);
147     werase(w);
148     wrefresh(w);
149     delwin(s);
150     delwin(w);
151 }
152
153 static int
154 my_form_driver(FORM *form, int c)
155 {
156     switch (c) {
157     case MY_QUIT:
158         if (form_driver(form, REQ_VALIDATION) == E_OK)
159             return (TRUE);
160         break;
161     case MY_HELP:
162         my_help_edit_field();
163         break;
164     case MY_DEMO:
165         do_demo(form);
166         break;
167     default:
168         beep();
169         break;
170     }
171     return (FALSE);
172 }
173
174 static FieldAttrs *
175 my_field_attrs(FIELD *f)
176 {
177     return (FieldAttrs *) field_userptr(f);
178 }
179
180 static int
181 buffer_length(FIELD *f)
182 {
183     return my_field_attrs(f)->row_lengths[0];
184 }
185
186 static void
187 set_buffer_length(FIELD *f, int length)
188 {
189     my_field_attrs(f)->row_lengths[0] = length;
190 }
191
192 static int
193 offset_in_field(FORM *form)
194 {
195     FIELD *field = current_field(form);
196     int currow, curcol;
197
198     form_getyx(form, currow, curcol);
199     return curcol + currow * (int) field->dcols;
200 }
201
202 static void
203 inactive_field(FIELD *f)
204 {
205     set_field_back(f, my_field_attrs(f)->background);
206 }
207
208 static int
209 my_edit_field(FORM *form, int *result)
210 {
211     int ch = wgetch(form_win(form));
212     int status;
213     FIELD *before;
214     unsigned n;
215     int before_row;
216     int before_col;
217     int before_off = offset_in_field(form);
218
219     form_getyx(form, before_row, before_col);
220     before = current_field(form);
221     set_field_back(before, A_NORMAL);
222     if (ch <= KEY_MAX) {
223         set_field_back(before, A_REVERSE);
224     } else if (ch <= MAX_FORM_COMMAND) {
225         inactive_field(before);
226     }
227
228     *result = ch;
229     for (n = 0; n < SIZEOF(commands); ++n) {
230         if (commands[n].code == ch) {
231             *result = commands[n].result;
232             break;
233         }
234     }
235
236     status = form_driver(form, *result);
237
238     if (status == E_OK) {
239         bool modified = TRUE;
240         int length = buffer_length(before);
241
242         if (length < before_off)
243             length = before_off;
244         switch (*result) {
245         case REQ_CLR_EOF:
246             length = before_off;
247             break;
248         case REQ_CLR_EOL:
249             if ((int) (before_row + 1) == (int) (before->rows))
250                 length = before_off;
251             break;
252         case REQ_CLR_FIELD:
253             length = 0;
254             break;
255         case REQ_DEL_CHAR:
256             if (length > before_off)
257                 --length;
258             break;
259         case REQ_DEL_PREV:
260             if (length > 0) {
261                 if (before_col > 0) {
262                     --length;
263                 } else if (before_row > 0) {
264                     length -= (int) before->cols + before_col;
265                 }
266             }
267             break;
268         case REQ_NEW_LINE:
269             length += (int) before->cols;
270             break;
271
272         default:
273             modified = (ch < MIN_FORM_COMMAND
274                         && isprint(ch));
275             break;
276         }
277
278         /*
279          * If we do not force a re-validation, then field_buffer 0 will
280          * be lagging by one character.
281          */
282         if (modified && form_driver(form, REQ_VALIDATION) == E_OK && *result
283             < MIN_FORM_COMMAND)
284             ++length;
285
286         set_buffer_length(before, length);
287     }
288
289     if (current_field(form) != before)
290         inactive_field(before);
291     return status;
292 }
293
294 static void
295 demo_forms(void)
296 {
297     FORM *form;
298     int c;
299     unsigned n = 0;
300     const char *fname;
301
302     /* describe the form */
303     all_fields[n++] = make_label("Sample Form", 0, 15);
304
305     fname = "Last Name";
306     all_fields[n++] = make_label(fname, 2, 0);
307     all_fields[n++] = make_field(3, 0, 1, 18);
308     set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
309
310     fname = "First Name";
311     all_fields[n++] = make_label(fname, 2, 20);
312     all_fields[n++] = make_field(3, 20, 1, 12);
313     set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
314
315     fname = "Middle Name";
316     all_fields[n++] = make_label(fname, 2, 34);
317     all_fields[n++] = make_field(3, 34, 1, 12);
318     set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
319
320     fname = "Comments";
321     all_fields[n++] = make_label(fname, 5, 0);
322     all_fields[n++] = make_field(6, 0, 4, 46);
323     init_edit_field(all_fields[n - 1], empty);
324
325     all_fields[n] = (FIELD *) 0;
326
327     if ((form = new_form(all_fields)) != 0) {
328         int finished = 0;
329
330         post_form(form);
331
332         while (!finished) {
333             switch (my_edit_field(form, &c)) {
334             case E_OK:
335                 break;
336             case E_UNKNOWN_COMMAND:
337                 finished = my_form_driver(form, c);
338                 break;
339             default:
340                 beep();
341                 break;
342             }
343         }
344
345         erase_form(form);
346
347         free_form(form);
348     }
349     for (c = 0; all_fields[c] != 0; c++) {
350         free_edit_field(all_fields[c]);
351         free_field(all_fields[c]);
352     }
353     noraw();
354     nl();
355 }
356
357 int
358 main(void)
359 {
360     setlocale(LC_ALL, "");
361
362     initscr();
363     cbreak();
364     noecho();
365     raw();
366     nonl();                     /* lets us read ^M's */
367     intrflush(stdscr, FALSE);
368     keypad(stdscr, TRUE);
369
370     if (has_colors()) {
371         start_color();
372         init_pair(1, COLOR_WHITE, COLOR_BLUE);
373         init_pair(2, COLOR_GREEN, COLOR_BLACK);
374         init_pair(3, COLOR_CYAN, COLOR_BLACK);
375         bkgd((chtype) COLOR_PAIR(1));
376         refresh();
377     }
378
379     demo_forms();
380
381     endwin();
382     ExitProgram(EXIT_SUCCESS);
383 }
384
385 #else
386 int
387 main(void)
388 {
389     printf("This program requires the curses form library\n");
390     ExitProgram(EXIT_FAILURE);
391 }
392 #endif