1 /****************************************************************************
2 * Copyright 2020 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: move_field.c,v 1.7 2020/05/09 12:52:00 tom Exp $
31 * Demonstrate move_field().
34 #include <test.priv.h>
38 #include <edit_field.h>
39 #include <popup_msg.h>
41 #ifdef HAVE_NETBSD_FORM_H
42 #define form_field_row(field) (field)->form_row
43 #define form_field_col(field) (field)->form_col
44 #else /* e.g., SVr4, ncurses */
45 #define form_field_row(field) (field)->frow
46 #define form_field_col(field) (field)->fcol
49 #define DO_DEMO CTRL('F') /* actual key for toggling demo-mode */
50 #define MY_DEMO EDIT_FIELD('f') /* internal request-code */
52 static char empty[] = "";
53 static FIELD *all_fields[100];
61 { CTRL('A'), REQ_BEG_FIELD, "go to beginning of field" },
62 { CTRL('D'), REQ_DOWN_FIELD, "move downward to field" },
63 { CTRL('E'), REQ_END_FIELD, "go to end of field" },
64 { CTRL('H'), REQ_DEL_PREV, "delete previous character" },
65 { CTRL('I'), REQ_NEXT_FIELD, "go to next field" },
66 { CTRL('K'), REQ_CLR_EOF, "clear to end of field" },
67 { CTRL('N'), REQ_NEXT_FIELD, "go to next field" },
68 { CTRL('P'), REQ_PREV_FIELD, "go to previous field" },
69 { CTRL('Q'), MY_QUIT, "exit form" },
70 { CTRL('U'), REQ_UP_FIELD, "move upward to field" },
71 { CTRL('W'), REQ_NEXT_WORD, "go to next word" },
72 { CTRL('X'), REQ_CLR_FIELD, "clear field" },
73 { CTRL('['), MY_QUIT, "exit form" },
74 { KEY_F(1), MY_HELP, "show this screen", },
75 { KEY_BACKSPACE, REQ_DEL_PREV, "delete previous character" },
76 { KEY_BTAB, REQ_PREV_FIELD, "go to previous field" },
77 { KEY_DOWN, REQ_DOWN_CHAR, "move down 1 character" },
78 { KEY_END, REQ_LAST_FIELD, "go to last field" },
79 { KEY_HOME, REQ_FIRST_FIELD, "go to first field" },
80 { KEY_LEFT, REQ_LEFT_CHAR, "move left 1 character" },
81 { KEY_NEXT, REQ_NEXT_FIELD, "go to next field" },
82 { KEY_PREVIOUS, REQ_PREV_FIELD, "go to previous field" },
83 { KEY_RIGHT, REQ_RIGHT_CHAR, "move right 1 character" },
84 { KEY_UP, REQ_UP_CHAR, "move up 1 character" },
85 { DO_DEMO, MY_DEMO, "move current field with cursor keys" }
90 my_help_edit_field(void)
94 char **msgs = typeCalloc(char *, 3 + SIZEOF(commands));
96 msgs[used++] = strdup("Defined form edit/traversal keys:");
97 for (n = 0; n < SIZEOF(commands); ++n) {
100 const char *code = keyname(commands[n].code);
102 #ifdef NCURSES_VERSION
103 if ((name = form_request_name(commands[n].result)) == 0)
105 name = commands[n].help;
106 need = 5 + strlen(code) + strlen(name);
107 msg = typeMalloc(char, need);
108 _nc_SPRINTF(msg, _nc_SLIMIT(need) "%s -- %s", code, name);
112 strdup("Arrow keys move within a field as you would expect.");
114 popup_msg2(stdscr, msgs);
115 for (n = 0; msgs[n] != 0; ++n) {
122 make_label(const char *label, int frow, int fcol)
124 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
127 set_field_buffer(f, 0, label);
128 set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
134 make_field(int frow, int fcol, int rows, int cols)
136 FIELD *f = new_field(rows, cols, frow, fcol, 0, 1);
139 set_field_back(f, A_UNDERLINE);
140 init_edit_field(f, empty);
148 WINDOW *w = form_win(f);
149 WINDOW *s = form_sub(f);
159 my_field_attrs(FIELD *f)
161 return (FieldAttrs *) field_userptr(f);
165 buffer_length(FIELD *f)
167 return my_field_attrs(f)->row_lengths[0];
171 set_buffer_length(FIELD *f, int length)
173 my_field_attrs(f)->row_lengths[0] = length;
177 offset_in_field(FORM *form)
179 FIELD *field = current_field(form);
182 form_getyx(form, currow, curcol);
183 return curcol + currow * (int) field->dcols;
187 inactive_field(FIELD *f)
189 set_field_back(f, my_field_attrs(f)->background);
193 my_edit_field(FORM *form, int *result)
195 int ch = wgetch(form_win(form));
202 int before_off = offset_in_field(form);
204 form_getyx(form, before_row, before_col);
205 before = current_field(form);
206 set_field_back(before, A_NORMAL);
208 set_field_back(before, A_REVERSE);
209 } else if (ch <= MAX_FORM_COMMAND) {
210 inactive_field(before);
214 for (n = 0; n < SIZEOF(commands); ++n) {
215 if (commands[n].code == ch) {
216 *result = commands[n].result;
221 status = form_driver(form, *result);
223 if (status == E_OK) {
224 bool modified = TRUE;
226 length = buffer_length(before);
227 if (length < before_off)
234 if ((int) (before_row + 1) == (int) (before->rows))
241 if (length > before_off)
246 if (before_col > 0) {
248 } else if (before_row > 0) {
249 length -= (int) before->cols + before_col;
254 length += (int) before->cols;
258 modified = (ch < MIN_FORM_COMMAND
264 * If we do not force a re-validation, then field_buffer 0 will
265 * be lagging by one character.
267 if (modified && form_driver(form, REQ_VALIDATION) == E_OK && *result
271 set_buffer_length(before, length);
274 if (current_field(form) != before)
275 inactive_field(before);
280 copy_fields(FIELD **source, size_t length)
282 FIELD **target = calloc(length + 1, sizeof(FIELD *));
283 memcpy(target, source, length * sizeof(FIELD *));
287 /* display a status message to show what's happening */
289 show_status(FORM *form, FIELD *field)
291 WINDOW *sub = form_sub(form);
294 getyx(stdscr, currow, curcol);
295 mvprintw(LINES - 1, 0,
296 "Field at [%d,%d]. Press %s to quit moving.",
297 getbegy(sub) + form_field_row(field),
298 getbegx(sub) + form_field_col(field),
301 move(currow, curcol);
306 * Move the current label+field in response to cursor-keys (or h,j,k,l) until
307 * a control/F is read.
312 int count = field_count(form);
313 FIELD *my_field = current_field(form);
315 if (count > 0 && my_field != NULL) {
316 size_t needed = (size_t) count;
317 FIELD **old_fields = copy_fields(form_fields(form), needed);
318 FIELD **new_fields = copy_fields(form_fields(form), needed);
321 if (old_fields != NULL && new_fields != NULL) {
324 /* TODO: move the label too, in parallel with the editing field */
326 /* remove the current field from the newer list */
327 for (ch = 0; ch <= count; ++ch) {
329 new_fields[ch - 1] = new_fields[ch];
330 } else if (new_fields[ch] == my_field) {
338 getyx(stdscr, currow, curcol);
340 show_status(form, my_field);
342 while ((ch = wgetch(form_win(form))) != DO_DEMO) {
343 int field_y = form_field_row(my_field);
344 int field_x = form_field_col(my_field);
378 /* alter connected fields temporarily to move the field */
380 set_form_fields(form, new_fields);
383 /* TODO: update screen position on success */
384 move_field(my_field, field_y, field_x);
386 /* restore the form's list of fields */
388 set_form_fields(form, old_fields);
391 show_status(form, my_field);
397 move(currow, curcol);
406 my_form_driver(FORM *form, int c)
410 if (form_driver(form, REQ_VALIDATION) == E_OK)
414 my_help_edit_field();
434 /* describe the form */
435 all_fields[n++] = make_label("Sample Form", 0, 15);
438 all_fields[n++] = make_label(fname, 2, 0);
439 all_fields[n++] = make_field(3, 0, 1, 18);
440 set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
442 fname = "First Name";
443 all_fields[n++] = make_label(fname, 2, 20);
444 all_fields[n++] = make_field(3, 20, 1, 12);
445 set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
447 fname = "Middle Name";
448 all_fields[n++] = make_label(fname, 2, 34);
449 all_fields[n++] = make_field(3, 34, 1, 12);
450 set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
453 all_fields[n++] = make_label(fname, 5, 0);
454 all_fields[n++] = make_field(6, 0, 4, 46);
455 init_edit_field(all_fields[n - 1], empty);
457 all_fields[n] = (FIELD *) 0;
459 if ((form = new_form(all_fields)) != 0) {
465 switch (my_edit_field(form, &c)) {
468 case E_UNKNOWN_COMMAND:
469 finished = my_form_driver(form, c);
481 for (c = 0; all_fields[c] != 0; c++) {
482 free_edit_field(all_fields[c]);
483 free_field(all_fields[c]);
492 setlocale(LC_ALL, "");
498 nonl(); /* lets us read ^M's */
499 intrflush(stdscr, FALSE);
500 keypad(stdscr, TRUE);
504 init_pair(1, COLOR_WHITE, COLOR_BLUE);
505 init_pair(2, COLOR_GREEN, COLOR_BLACK);
506 init_pair(3, COLOR_CYAN, COLOR_BLACK);
507 bkgd((chtype) COLOR_PAIR(1));
514 ExitProgram(EXIT_SUCCESS);
521 printf("This program requires the curses form library\n");
522 ExitProgram(EXIT_FAILURE);