diff options
Diffstat (limited to 'test/dup_field.c')
-rw-r--r-- | test/dup_field.c | 132 |
1 files changed, 94 insertions, 38 deletions
diff --git a/test/dup_field.c b/test/dup_field.c index 5ca67f0b0f5a..68198ed755b3 100644 --- a/test/dup_field.c +++ b/test/dup_field.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2020 Thomas E. Dickey * + * Copyright 2020-2022,2023 Thomas E. Dickey * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -26,9 +26,9 @@ * authorization. * ****************************************************************************/ /* - * $Id: dup_field.c,v 1.1 2020/03/21 19:28:36 tom Exp $ + * $Id: dup_field.c,v 1.8 2023/11/11 00:29:10 tom Exp $ * - * Demonstrate move_field(). + * Demonstrate dup_field(). */ #include <test.priv.h> @@ -38,11 +38,11 @@ #include <edit_field.h> #include <popup_msg.h> -#define MY_DEMO EDIT_FIELD('f') +#define DO_DEMO CTRL('F') /* actual key for toggling demo-mode */ +#define MY_DEMO EDIT_FIELD('f') /* internal request-code */ static char empty[] = ""; static FIELD *all_fields[100]; - /* *INDENT-OFF* */ static struct { int code; @@ -52,7 +52,6 @@ static struct { { CTRL('A'), REQ_BEG_FIELD, "go to beginning of field" }, { CTRL('D'), REQ_DOWN_FIELD, "move downward to field" }, { CTRL('E'), REQ_END_FIELD, "go to end of field" }, - { CTRL('G'), MY_DEMO, "move current field with cursor keys" }, { CTRL('H'), REQ_DEL_PREV, "delete previous character" }, { CTRL('I'), REQ_NEXT_FIELD, "go to next field" }, { CTRL('K'), REQ_CLR_EOF, "clear to end of field" }, @@ -73,7 +72,8 @@ static struct { { KEY_NEXT, REQ_NEXT_FIELD, "go to next field" }, { KEY_PREVIOUS, REQ_PREV_FIELD, "go to previous field" }, { KEY_RIGHT, REQ_RIGHT_CHAR, "move right 1 character" }, - { KEY_UP, REQ_UP_CHAR, "move up 1 character" } + { KEY_UP, REQ_UP_CHAR, "move up 1 character" }, + { DO_DEMO, MY_DEMO, "duplicate current field" } }; /* *INDENT-ON* */ @@ -109,11 +109,6 @@ my_help_edit_field(void) free(msgs); } -static void -do_demo(FORM *form) -{ -} - static FIELD * make_label(const char *label, int frow, int fcol) { @@ -121,7 +116,7 @@ make_label(const char *label, int frow, int fcol) if (f) { set_field_buffer(f, 0, label); - set_field_opts(f, (int) ((unsigned) field_opts(f) & ~O_ACTIVE)); + set_field_opts(f, (int) ((unsigned) field_opts(f) & (unsigned) ~O_ACTIVE)); } return (f); } @@ -148,28 +143,6 @@ erase_form(FORM *f) werase(w); wrefresh(w); delwin(s); - delwin(w); -} - -static int -my_form_driver(FORM *form, int c) -{ - switch (c) { - case MY_QUIT: - if (form_driver(form, REQ_VALIDATION) == E_OK) - return (TRUE); - break; - case MY_HELP: - my_help_edit_field(); - break; - case MY_DEMO: - do_demo(form); - break; - default: - beep(); - break; - } - return (FALSE); } static FieldAttrs * @@ -213,7 +186,6 @@ my_edit_field(FORM *form, int *result) int status; FIELD *before; unsigned n; - int length; int before_row; int before_col; int before_off = offset_in_field(form); @@ -239,8 +211,8 @@ my_edit_field(FORM *form, int *result) if (status == E_OK) { bool modified = TRUE; + int length = buffer_length(before); - length = buffer_length(before); if (length < before_off) length = before_off; switch (*result) { @@ -293,6 +265,55 @@ my_edit_field(FORM *form, int *result) return status; } +static FIELD ** +copy_fields(FIELD **source, FIELD *extra, size_t length) +{ + FIELD **target = typeCalloc(FIELD *, length + 1); + memcpy(target, source, length * sizeof(FIELD *)); + target[length] = extra; + return target; +} + +static void +do_demo(FORM *form) +{ + int count = field_count(form); + FIELD *my_field = current_field(form); + FIELD **old_fields = form_fields(form); + + if (count > 0 && old_fields != NULL && my_field != NULL) { + FIELD **new_fields = copy_fields(old_fields, + dup_field(my_field, + form_field_row(my_field) + + 1, + form_field_col(my_field)), + (size_t) count); + if (new_fields != NULL) + set_form_fields(form, new_fields); + } +} + +static int +my_form_driver(FORM *form, int c) +{ + switch (c) { + case MY_QUIT: + if (form_driver(form, REQ_VALIDATION) == E_OK) + return (TRUE); + break; + case MY_HELP: + my_help_edit_field(); + break; + case MY_DEMO: + do_demo(form); + break; + default: + beep(); + break; + } + return (FALSE); +} + static void demo_forms(void) { @@ -356,9 +377,44 @@ demo_forms(void) nl(); } +static void +usage(int ok) +{ + static const char *msg[] = + { + "Usage: dup_field [options]" + ,"" + ,USAGE_COMMON + }; + size_t n; + + for (n = 0; n < SIZEOF(msg); n++) + fprintf(stderr, "%s\n", msg[n]); + + ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); +} +/* *INDENT-OFF* */ +VERSION_COMMON() +/* *INDENT-ON* */ + int -main(void) +main(int argc, char *argv[]) { + int ch; + + while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) { + switch (ch) { + case OPTS_VERSION: + show_version(argv); + ExitProgram(EXIT_SUCCESS); + default: + usage(ch == OPTS_USAGE); + /* NOTREACHED */ + } + } + if (optind < argc) + usage(FALSE); + setlocale(LC_ALL, ""); initscr(); |