1 /****************************************************************************
2 * Copyright 2020-2021,2022 Thomas E. Dickey *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: dup_field.c,v 1.7 2022/12/10 23:31:31 tom Exp $
31 * Demonstrate dup_field().
34 #include <test.priv.h>
38 #include <edit_field.h>
39 #include <popup_msg.h>
41 #define DO_DEMO CTRL('F') /* actual key for toggling demo-mode */
42 #define MY_DEMO EDIT_FIELD('f') /* internal request-code */
44 static char empty[] = "";
45 static FIELD *all_fields[100];
52 { CTRL('A'), REQ_BEG_FIELD, "go to beginning of field" },
53 { CTRL('D'), REQ_DOWN_FIELD, "move downward to field" },
54 { CTRL('E'), REQ_END_FIELD, "go to end of field" },
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 { DO_DEMO, MY_DEMO, "duplicate current field" }
81 my_help_edit_field(void)
85 char **msgs = typeCalloc(char *, 3 + SIZEOF(commands));
87 msgs[used++] = strdup("Defined form edit/traversal keys:");
88 for (n = 0; n < SIZEOF(commands); ++n) {
91 const char *code = keyname(commands[n].code);
93 #ifdef NCURSES_VERSION
94 if ((name = form_request_name(commands[n].result)) == 0)
96 name = commands[n].help;
97 need = 5 + strlen(code) + strlen(name);
98 msg = typeMalloc(char, need);
99 _nc_SPRINTF(msg, _nc_SLIMIT(need) "%s -- %s", code, name);
103 strdup("Arrow keys move within a field as you would expect.");
105 popup_msg2(stdscr, msgs);
106 for (n = 0; msgs[n] != 0; ++n) {
113 make_label(const char *label, int frow, int fcol)
115 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
118 set_field_buffer(f, 0, label);
119 set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
125 make_field(int frow, int fcol, int rows, int cols)
127 FIELD *f = new_field(rows, cols, frow, fcol, 0, 1);
130 set_field_back(f, A_UNDERLINE);
131 init_edit_field(f, empty);
139 WINDOW *w = form_win(f);
140 WINDOW *s = form_sub(f);
149 my_field_attrs(FIELD *f)
151 return (FieldAttrs *) field_userptr(f);
155 buffer_length(FIELD *f)
157 return my_field_attrs(f)->row_lengths[0];
161 set_buffer_length(FIELD *f, int length)
163 my_field_attrs(f)->row_lengths[0] = length;
167 offset_in_field(FORM *form)
169 FIELD *field = current_field(form);
172 form_getyx(form, currow, curcol);
173 return curcol + currow * (int) field->dcols;
177 inactive_field(FIELD *f)
179 set_field_back(f, my_field_attrs(f)->background);
183 my_edit_field(FORM *form, int *result)
185 int ch = wgetch(form_win(form));
191 int before_off = offset_in_field(form);
193 form_getyx(form, before_row, before_col);
194 before = current_field(form);
195 set_field_back(before, A_NORMAL);
197 set_field_back(before, A_REVERSE);
198 } else if (ch <= MAX_FORM_COMMAND) {
199 inactive_field(before);
203 for (n = 0; n < SIZEOF(commands); ++n) {
204 if (commands[n].code == ch) {
205 *result = commands[n].result;
210 status = form_driver(form, *result);
212 if (status == E_OK) {
213 bool modified = TRUE;
214 int length = buffer_length(before);
216 if (length < before_off)
223 if ((int) (before_row + 1) == (int) (before->rows))
230 if (length > before_off)
235 if (before_col > 0) {
237 } else if (before_row > 0) {
238 length -= (int) before->cols + before_col;
243 length += (int) before->cols;
247 modified = (ch < MIN_FORM_COMMAND
253 * If we do not force a re-validation, then field_buffer 0 will
254 * be lagging by one character.
256 if (modified && form_driver(form, REQ_VALIDATION) == E_OK && *result
260 set_buffer_length(before, length);
263 if (current_field(form) != before)
264 inactive_field(before);
269 copy_fields(FIELD **source, FIELD *extra, size_t length)
271 FIELD **target = typeCalloc(FIELD *, length + 1);
272 memcpy(target, source, length * sizeof(FIELD *));
273 target[length] = extra;
280 int count = field_count(form);
281 FIELD *my_field = current_field(form);
282 FIELD **old_fields = form_fields(form);
284 if (count > 0 && old_fields != NULL && my_field != NULL) {
285 FIELD **new_fields = copy_fields(old_fields,
287 form_field_row(my_field)
289 form_field_col(my_field)),
291 if (new_fields != NULL)
292 set_form_fields(form, new_fields);
297 my_form_driver(FORM *form, int c)
301 if (form_driver(form, REQ_VALIDATION) == E_OK)
305 my_help_edit_field();
325 /* describe the form */
326 all_fields[n++] = make_label("Sample Form", 0, 15);
329 all_fields[n++] = make_label(fname, 2, 0);
330 all_fields[n++] = make_field(3, 0, 1, 18);
331 set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
333 fname = "First Name";
334 all_fields[n++] = make_label(fname, 2, 20);
335 all_fields[n++] = make_field(3, 20, 1, 12);
336 set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
338 fname = "Middle Name";
339 all_fields[n++] = make_label(fname, 2, 34);
340 all_fields[n++] = make_field(3, 34, 1, 12);
341 set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
344 all_fields[n++] = make_label(fname, 5, 0);
345 all_fields[n++] = make_field(6, 0, 4, 46);
346 init_edit_field(all_fields[n - 1], empty);
348 all_fields[n] = (FIELD *) 0;
350 if ((form = new_form(all_fields)) != 0) {
356 switch (my_edit_field(form, &c)) {
359 case E_UNKNOWN_COMMAND:
360 finished = my_form_driver(form, c);
372 for (c = 0; all_fields[c] != 0; c++) {
373 free_edit_field(all_fields[c]);
374 free_field(all_fields[c]);
383 static const char *msg[] =
385 "Usage: dup_field [options]"
391 for (n = 0; n < SIZEOF(msg); n++)
392 fprintf(stderr, "%s\n", msg[n]);
394 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
401 main(int argc, char *argv[])
405 while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
409 ExitProgram(EXIT_SUCCESS);
411 usage(ch == OPTS_USAGE);
418 setlocale(LC_ALL, "");
424 nonl(); /* lets us read ^M's */
425 intrflush(stdscr, FALSE);
426 keypad(stdscr, TRUE);
430 init_pair(1, COLOR_WHITE, COLOR_BLUE);
431 init_pair(2, COLOR_GREEN, COLOR_BLACK);
432 init_pair(3, COLOR_CYAN, COLOR_BLACK);
433 bkgd((chtype) COLOR_PAIR(1));
440 ExitProgram(EXIT_SUCCESS);
447 printf("This program requires the curses form library\n");
448 ExitProgram(EXIT_FAILURE);