X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Finchs.c;h=2e05ccbc80f52bec5282604e864eccb2ecf6aee9;hp=f49e72542271346d55fba5051b5dd91e65e06de2;hb=07e31b3b587a07281ff7c71e5c13248a31048257;hpb=614ef82f41eda084798068d715ef91b072014ca9 diff --git a/test/inchs.c b/test/inchs.c index f49e7254..2e05ccbc 100644 --- a/test/inchs.c +++ b/test/inchs.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2007 Free Software Foundation, Inc. * + * Copyright (c) 2007-2012,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -26,7 +26,9 @@ * authorization. * ****************************************************************************/ /* - * $Id: inchs.c,v 1.4 2007/02/11 00:27:12 tom Exp $ + * $Id: inchs.c,v 1.14 2017/04/29 22:03:26 tom Exp $ + * + * Author: Thomas E Dickey */ /* chtype inch(void); @@ -44,70 +46,93 @@ */ #include +#include +#define BASE_Y 7 #define MAX_COLS 1024 +static void +failed(const char *s) +{ + int save = errno; + endwin(); + errno = save; + perror(s); + ExitProgram(EXIT_FAILURE); +} + static bool Quit(int ch) { - return (ch == ERR || ch == QUIT || ch == ESCAPE); + return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE); } -int -main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) +static int +test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin) { - WINDOW *txtbox; - WINDOW *txtwin; - WINDOW *chrbox; - WINDOW *chrwin; - WINDOW *strwin; + static const char *help[] = + { + "Test input from screen using inch(), etc., in a moveable viewport.", + "", + "Commands:", + " ESC/^Q - quit", + " h,j,k,l (and arrow-keys) - move viewport", + " w - recur to new window", + " for next input file", + 0 + }; + WINDOW *txtbox = 0; + WINDOW *txtwin = 0; FILE *fp; int ch, j; int txt_x = 0, txt_y = 0; + int base_y; int limit; chtype text[MAX_COLS]; - setlocale(LC_ALL, ""); - - if (argc != 2) { - fprintf(stderr, "usage: %s file\n", argv[0]); - return EXIT_FAILURE; + if (argv[level] == 0) { + beep(); + return FALSE; } - initscr(); - - chrbox = newwin(7, COLS, 0, 0); - box(chrbox, 0, 0); - wnoutrefresh(chrbox); - - chrwin = derwin(chrbox, 1, COLS - 2, 1, 1); - strwin = derwin(chrbox, 4, COLS - 2, 2, 1); - - txtbox = newwin(LINES - 7, COLS, 7, 0); - box(txtbox, 0, 0); - wnoutrefresh(txtbox); + if (level > 1) { + txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level); + box(txtbox, 0, 0); + wnoutrefresh(txtbox); - txtwin = derwin(txtbox, - getmaxy(txtbox) - 2, - getmaxx(txtbox) - 2, - 1, 1); + txtwin = derwin(txtbox, + getmaxy(txtbox) - 2, + getmaxx(txtbox) - 2, + 1, 1); + base_y = 0; + } else { + txtwin = stdscr; + base_y = BASE_Y; + } + if (txtwin == 0) + failed("cannot create txtwin"); keypad(txtwin, TRUE); /* enable keyboard mapping */ (void) cbreak(); /* take input chars one at a time, no wait for \n */ (void) noecho(); /* don't echo input */ - if ((fp = fopen(argv[1], "r")) != 0) { - while ((ch = fgetc(fp)) != EOF) { - if (waddch(txtwin, ch) != OK) { + txt_y = base_y; + txt_x = 0; + wmove(txtwin, txt_y, txt_x); + + if ((fp = fopen(argv[level], "r")) != 0) { + while ((j = fgetc(fp)) != EOF) { + if (waddch(txtwin, UChar(j)) != OK) { break; } } + fclose(fp); } else { wprintw(txtwin, "Cannot open:\n%s", argv[1]); } - fclose(fp); - while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) { - switch (ch) { + + while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) { + switch (j) { case KEY_DOWN: case 'j': if (txt_y < getmaxy(txtwin) - 1) @@ -117,7 +142,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) break; case KEY_UP: case 'k': - if (txt_y > 0) + if (txt_y > base_y) txt_y--; else beep(); @@ -136,54 +161,139 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) else beep(); break; + case 'w': + test_inchs(level + 1, argv, chrwin, strwin); + if (txtbox != 0) { + touchwin(txtbox); + wnoutrefresh(txtbox); + } else { + touchwin(txtwin); + wnoutrefresh(txtwin); + } + break; + case HELP_KEY_1: + popup_msg(txtwin, help); + break; + default: + beep(); + break; } - wmove(txtwin, txt_y, txt_x); - - mvwprintw(chrwin, 0, 0, "char:"); + MvWPrintw(chrwin, 0, 0, "char:"); wclrtoeol(chrwin); - if ((ch = winch(txtwin)) != ERR) { - if (waddch(chrwin, (chtype) ch) != ERR) { - for (j = txt_x + 1; j < getmaxx(txtwin); ++j) { - if ((ch = mvwinch(txtwin, txt_y, j)) != ERR) { - if (waddch(chrwin, (chtype) ch) == ERR) { + + if (txtwin != stdscr) { + wmove(txtwin, txt_y, txt_x); + + if ((ch = (int) winch(txtwin)) != ERR) { + if (waddch(chrwin, (chtype) ch) != ERR) { + for (j = txt_x + 1; j < getmaxx(txtwin); ++j) { + if ((ch = (int) mvwinch(txtwin, txt_y, j)) != ERR) { + if (waddch(chrwin, (chtype) ch) == ERR) { + break; + } + } else { + break; + } + } + } + } + } else { + move(txt_y, txt_x); + + if ((ch = (int) inch()) != ERR) { + if (waddch(chrwin, (chtype) ch) != ERR) { + for (j = txt_x + 1; j < getmaxx(txtwin); ++j) { + if ((ch = (int) mvinch(txt_y, j)) != ERR) { + if (waddch(chrwin, (chtype) ch) == ERR) { + break; + } + } else { break; } - } else { - break; } } } } wnoutrefresh(chrwin); - mvwprintw(strwin, 0, 0, "text:"); + MvWPrintw(strwin, 0, 0, "text:"); wclrtobot(strwin); limit = getmaxx(strwin) - 5; - wmove(txtwin, txt_y, txt_x); - if (winchstr(txtwin, text) != ERR) { - mvwaddchstr(strwin, 0, 5, text); - } + if (txtwin != stdscr) { + wmove(txtwin, txt_y, txt_x); + if (winchstr(txtwin, text) != ERR) { + MvWAddChStr(strwin, 0, 5, text); + } - wmove(txtwin, txt_y, txt_x); - if (winchnstr(txtwin, text, limit) != ERR) { - mvwaddchstr(strwin, 1, 5, text); - } + wmove(txtwin, txt_y, txt_x); + if (winchnstr(txtwin, text, limit) != ERR) { + MvWAddChStr(strwin, 1, 5, text); + } - if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) { - mvwaddchstr(strwin, 2, 5, text); - } + if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) { + MvWAddChStr(strwin, 2, 5, text); + } + + if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) { + MvWAddChStr(strwin, 3, 5, text); + } + } else { + move(txt_y, txt_x); + if (inchstr(text) != ERR) { + MvWAddChStr(strwin, 0, 5, text); + } - if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) { - mvwaddchstr(strwin, 3, 5, text); + move(txt_y, txt_x); + if (inchnstr(text, limit) != ERR) { + MvWAddChStr(strwin, 1, 5, text); + } + + if (mvinchstr(txt_y, txt_x, text) != ERR) { + MvWAddChStr(strwin, 2, 5, text); + } + + if (mvinchnstr(txt_y, txt_x, text, limit) != ERR) { + MvWAddChStr(strwin, 3, 5, text); + } } wnoutrefresh(strwin); + } + if (level > 1) { + delwin(txtwin); + delwin(txtbox); + } + return TRUE; +} + +int +main(int argc, char *argv[]) +{ + WINDOW *chrbox; + WINDOW *chrwin; + WINDOW *strwin; - /* FIXME: want stdscr tests also, but must be separate program */ + setlocale(LC_ALL, ""); + + if (argc < 2) { + fprintf(stderr, "usage: %s file1 [file2 [...]]\n", argv[0]); + return EXIT_FAILURE; } + + initscr(); + + chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0); + box(chrbox, 0, 0); + wnoutrefresh(chrbox); + + chrwin = derwin(chrbox, 1, COLS - 2, 1, 1); + strwin = derwin(chrbox, 4, COLS - 2, 2, 1); + + test_inchs(1, argv, chrwin, strwin); + endwin(); - return EXIT_SUCCESS; + ExitProgram(EXIT_SUCCESS); }