X-Git-Url: https://ncurses.scripts.mit.edu/?a=blobdiff_plain;f=test%2Fmove_field.c;h=84bac3f4b5659121b4dfa301cb50eb0c250ea5af;hb=122d3739b3c11c83decc625d53f26fff6e825710;hp=7681225bf9882d8c1e97c0f0ed512de14c929f98;hpb=f3ec084eb66ba14feb6357b674fb85dd474933d8;p=ncurses.git diff --git a/test/move_field.c b/test/move_field.c index 7681225b..84bac3f4 100644 --- a/test/move_field.c +++ b/test/move_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,7 +26,7 @@ * authorization. * ****************************************************************************/ /* - * $Id: move_field.c,v 1.5 2020/03/21 22:04:03 tom Exp $ + * $Id: move_field.c,v 1.15 2023/11/11 00:28:19 tom Exp $ * * Demonstrate move_field(). */ @@ -43,7 +43,6 @@ static char empty[] = ""; static FIELD *all_fields[100]; - /* *INDENT-OFF* */ static struct { int code; @@ -117,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); } @@ -144,7 +143,6 @@ erase_form(FORM *f) werase(w); wrefresh(w); delwin(s); - delwin(w); } static FieldAttrs * @@ -188,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); @@ -214,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) { @@ -269,9 +266,9 @@ my_edit_field(FORM *form, int *result) } static FIELD ** -copy_fields(FIELD **source, int length) +copy_fields(FIELD **source, size_t length) { - FIELD **target = calloc(length + 1, sizeof(FIELD *)); + FIELD **target = typeCalloc(FIELD *, length + 1); memcpy(target, source, length * sizeof(FIELD *)); return target; } @@ -286,8 +283,8 @@ show_status(FORM *form, FIELD *field) getyx(stdscr, currow, curcol); mvprintw(LINES - 1, 0, "Field at [%d,%d]. Press %s to quit moving.", - getbegy(sub) + field->frow, - getbegx(sub) + field->fcol, + getbegy(sub) + form_field_row(field), + getbegx(sub) + form_field_col(field), keyname(DO_DEMO)); clrtobot(); move(currow, curcol); @@ -303,14 +300,15 @@ do_demo(FORM *form) { int count = field_count(form); FIELD *my_field = current_field(form); + FIELD **old_fields = form_fields(form); - if (count > 0 && my_field != NULL) { - FIELD **old_fields = copy_fields(form_fields(form), count); - FIELD **new_fields = copy_fields(form_fields(form), count); - int ch; + if (count > 0 && old_fields != NULL && my_field != NULL) { + size_t needed = (size_t) count; + FIELD **new_fields = copy_fields(old_fields, needed); - if (old_fields != NULL && new_fields != NULL) { + if (new_fields != NULL) { bool found = FALSE; + int ch; /* TODO: move the label too, in parallel with the editing field */ @@ -329,10 +327,9 @@ do_demo(FORM *form) getyx(stdscr, currow, curcol); show_status(form, my_field); - ch = '?'; while ((ch = wgetch(form_win(form))) != DO_DEMO) { - int field_y = my_field->frow; - int field_x = my_field->fcol; + int field_y = form_field_row(my_field); + int field_x = form_field_col(my_field); switch (ch) { case 'h': @@ -477,9 +474,44 @@ demo_forms(void) nl(); } +static void +usage(int ok) +{ + static const char *msg[] = + { + "Usage: move_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();