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.5 2020/03/21 22:04:03 tom Exp $
31 * Demonstrate move_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];
53 { CTRL('A'), REQ_BEG_FIELD, "go to beginning of field" },
54 { CTRL('D'), REQ_DOWN_FIELD, "move downward to field" },
55 { CTRL('E'), REQ_END_FIELD, "go to end of field" },
56 { CTRL('H'), REQ_DEL_PREV, "delete previous character" },
57 { CTRL('I'), REQ_NEXT_FIELD, "go to next field" },
58 { CTRL('K'), REQ_CLR_EOF, "clear to end of field" },
59 { CTRL('N'), REQ_NEXT_FIELD, "go to next field" },
60 { CTRL('P'), REQ_PREV_FIELD, "go to previous field" },
61 { CTRL('Q'), MY_QUIT, "exit form" },
62 { CTRL('U'), REQ_UP_FIELD, "move upward to field" },
63 { CTRL('W'), REQ_NEXT_WORD, "go to next word" },
64 { CTRL('X'), REQ_CLR_FIELD, "clear field" },
65 { CTRL('['), MY_QUIT, "exit form" },
66 { KEY_F(1), MY_HELP, "show this screen", },
67 { KEY_BACKSPACE, REQ_DEL_PREV, "delete previous character" },
68 { KEY_BTAB, REQ_PREV_FIELD, "go to previous field" },
69 { KEY_DOWN, REQ_DOWN_CHAR, "move down 1 character" },
70 { KEY_END, REQ_LAST_FIELD, "go to last field" },
71 { KEY_HOME, REQ_FIRST_FIELD, "go to first field" },
72 { KEY_LEFT, REQ_LEFT_CHAR, "move left 1 character" },
73 { KEY_NEXT, REQ_NEXT_FIELD, "go to next field" },
74 { KEY_PREVIOUS, REQ_PREV_FIELD, "go to previous field" },
75 { KEY_RIGHT, REQ_RIGHT_CHAR, "move right 1 character" },
76 { KEY_UP, REQ_UP_CHAR, "move up 1 character" },
77 { DO_DEMO, MY_DEMO, "move current field with cursor keys" }
82 my_help_edit_field(void)
86 char **msgs = typeCalloc(char *, 3 + SIZEOF(commands));
88 msgs[used++] = strdup("Defined form edit/traversal keys:");
89 for (n = 0; n < SIZEOF(commands); ++n) {
92 const char *code = keyname(commands[n].code);
94 #ifdef NCURSES_VERSION
95 if ((name = form_request_name(commands[n].result)) == 0)
97 name = commands[n].help;
98 need = 5 + strlen(code) + strlen(name);
99 msg = typeMalloc(char, need);
100 _nc_SPRINTF(msg, _nc_SLIMIT(need) "%s -- %s", code, name);
104 strdup("Arrow keys move within a field as you would expect.");
106 popup_msg2(stdscr, msgs);
107 for (n = 0; msgs[n] != 0; ++n) {
114 make_label(const char *label, int frow, int fcol)
116 FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
119 set_field_buffer(f, 0, label);
120 set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE));
126 make_field(int frow, int fcol, int rows, int cols)
128 FIELD *f = new_field(rows, cols, frow, fcol, 0, 1);
131 set_field_back(f, A_UNDERLINE);
132 init_edit_field(f, empty);
140 WINDOW *w = form_win(f);
141 WINDOW *s = form_sub(f);
151 my_field_attrs(FIELD *f)
153 return (FieldAttrs *) field_userptr(f);
157 buffer_length(FIELD *f)
159 return my_field_attrs(f)->row_lengths[0];
163 set_buffer_length(FIELD *f, int length)
165 my_field_attrs(f)->row_lengths[0] = length;
169 offset_in_field(FORM *form)
171 FIELD *field = current_field(form);
174 form_getyx(form, currow, curcol);
175 return curcol + currow * (int) field->dcols;
179 inactive_field(FIELD *f)
181 set_field_back(f, my_field_attrs(f)->background);
185 my_edit_field(FORM *form, int *result)
187 int ch = wgetch(form_win(form));
194 int before_off = offset_in_field(form);
196 form_getyx(form, before_row, before_col);
197 before = current_field(form);
198 set_field_back(before, A_NORMAL);
200 set_field_back(before, A_REVERSE);
201 } else if (ch <= MAX_FORM_COMMAND) {
202 inactive_field(before);
206 for (n = 0; n < SIZEOF(commands); ++n) {
207 if (commands[n].code == ch) {
208 *result = commands[n].result;
213 status = form_driver(form, *result);
215 if (status == E_OK) {
216 bool modified = TRUE;
218 length = buffer_length(before);
219 if (length < before_off)
226 if ((int) (before_row + 1) == (int) (before->rows))
233 if (length > before_off)
238 if (before_col > 0) {
240 } else if (before_row > 0) {
241 length -= (int) before->cols + before_col;
246 length += (int) before->cols;
250 modified = (ch < MIN_FORM_COMMAND
256 * If we do not force a re-validation, then field_buffer 0 will
257 * be lagging by one character.
259 if (modified && form_driver(form, REQ_VALIDATION) == E_OK && *result
263 set_buffer_length(before, length);
266 if (current_field(form) != before)
267 inactive_field(before);
272 copy_fields(FIELD **source, int length)
274 FIELD **target = calloc(length + 1, sizeof(FIELD *));
275 memcpy(target, source, length * sizeof(FIELD *));
279 /* display a status message to show what's happening */
281 show_status(FORM *form, FIELD *field)
283 WINDOW *sub = form_sub(form);
286 getyx(stdscr, currow, curcol);
287 mvprintw(LINES - 1, 0,
288 "Field at [%d,%d]. Press %s to quit moving.",
289 getbegy(sub) + field->frow,
290 getbegx(sub) + field->fcol,
293 move(currow, curcol);
298 * Move the current label+field in response to cursor-keys (or h,j,k,l) until
299 * a control/F is read.
304 int count = field_count(form);
305 FIELD *my_field = current_field(form);
307 if (count > 0 && my_field != NULL) {
308 FIELD **old_fields = copy_fields(form_fields(form), count);
309 FIELD **new_fields = copy_fields(form_fields(form), count);
312 if (old_fields != NULL && new_fields != NULL) {
315 /* TODO: move the label too, in parallel with the editing field */
317 /* remove the current field from the newer list */
318 for (ch = 0; ch <= count; ++ch) {
320 new_fields[ch - 1] = new_fields[ch];
321 } else if (new_fields[ch] == my_field) {
329 getyx(stdscr, currow, curcol);
331 show_status(form, my_field);
333 while ((ch = wgetch(form_win(form))) != DO_DEMO) {
334 int field_y = my_field->frow;
335 int field_x = my_field->fcol;
369 /* alter connected fields temporarily to move the field */
371 set_form_fields(form, new_fields);
374 /* TODO: update screen position on success */
375 move_field(my_field, field_y, field_x);
377 /* restore the form's list of fields */
379 set_form_fields(form, old_fields);
382 show_status(form, my_field);
388 move(currow, curcol);
397 my_form_driver(FORM *form, int c)
401 if (form_driver(form, REQ_VALIDATION) == E_OK)
405 my_help_edit_field();
425 /* describe the form */
426 all_fields[n++] = make_label("Sample Form", 0, 15);
429 all_fields[n++] = make_label(fname, 2, 0);
430 all_fields[n++] = make_field(3, 0, 1, 18);
431 set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
433 fname = "First Name";
434 all_fields[n++] = make_label(fname, 2, 20);
435 all_fields[n++] = make_field(3, 20, 1, 12);
436 set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
438 fname = "Middle Name";
439 all_fields[n++] = make_label(fname, 2, 34);
440 all_fields[n++] = make_field(3, 34, 1, 12);
441 set_field_type(all_fields[n - 1], TYPE_ALPHA, 1);
444 all_fields[n++] = make_label(fname, 5, 0);
445 all_fields[n++] = make_field(6, 0, 4, 46);
446 init_edit_field(all_fields[n - 1], empty);
448 all_fields[n] = (FIELD *) 0;
450 if ((form = new_form(all_fields)) != 0) {
456 switch (my_edit_field(form, &c)) {
459 case E_UNKNOWN_COMMAND:
460 finished = my_form_driver(form, c);
472 for (c = 0; all_fields[c] != 0; c++) {
473 free_edit_field(all_fields[c]);
474 free_field(all_fields[c]);
483 setlocale(LC_ALL, "");
489 nonl(); /* lets us read ^M's */
490 intrflush(stdscr, FALSE);
491 keypad(stdscr, TRUE);
495 init_pair(1, COLOR_WHITE, COLOR_BLUE);
496 init_pair(2, COLOR_GREEN, COLOR_BLACK);
497 init_pair(3, COLOR_CYAN, COLOR_BLACK);
498 bkgd((chtype) COLOR_PAIR(1));
505 ExitProgram(EXIT_SUCCESS);
512 printf("This program requires the curses form library\n");
513 ExitProgram(EXIT_FAILURE);