]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/inchs.c
ncurses 6.1 - patch 20191102
[ncurses.git] / test / inchs.c
index 38747a2f39bd2c70162b0b16831a420b99374f3a..e9d53702f7903f7fd0c92cc346fbb3f1a4e7de8e 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2007 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2007-2017,2019 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.6 2007/06/09 21:25:06 tom Exp $
+ * $Id: inchs.c,v 1.17 2019/08/24 23:11:01 tom Exp $
+ *
+ * Author: Thomas E Dickey
  */
 /*
        chtype inch(void);
 */
 
 #include <test.priv.h>
+#include <popup_msg.h>
 
 #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)
 {
@@ -57,13 +70,23 @@ Quit(int ch)
 static int
 test_inchs(int level, char **argv, 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];
 
     if (argv[level] == 0) {
@@ -85,6 +108,8 @@ test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
        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 */
@@ -96,16 +121,18 @@ test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
 
     if ((fp = fopen(argv[level], "r")) != 0) {
        while ((j = fgetc(fp)) != EOF) {
-           if (waddch(txtwin, j) != OK) {
+           if (waddch(txtwin, UChar(j)) != OK) {
                break;
            }
        }
+       fclose(fp);
     } else {
        wprintw(txtwin, "Cannot open:\n%s", argv[1]);
     }
-    fclose(fp);
 
     while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) {
+       int limit;
+
        switch (j) {
        case KEY_DOWN:
        case 'j':
@@ -145,21 +172,24 @@ test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
                wnoutrefresh(txtwin);
            }
            break;
+       case HELP_KEY_1:
+           popup_msg(txtwin, help);
+           break;
        default:
            beep();
            break;
        }
 
-       mvwprintw(chrwin, 0, 0, "char:");
+       MvWPrintw(chrwin, 0, 0, "char:");
        wclrtoeol(chrwin);
 
        if (txtwin != stdscr) {
            wmove(txtwin, txt_y, txt_x);
 
-           if ((ch = winch(txtwin)) != ERR) {
+           if ((ch = (int) 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 ((ch = (int) mvwinch(txtwin, txt_y, j)) != ERR) {
                            if (waddch(chrwin, (chtype) ch) == ERR) {
                                break;
                            }
@@ -172,10 +202,10 @@ test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
        } else {
            move(txt_y, txt_x);
 
-           if ((ch = inch()) != ERR) {
+           if ((ch = (int) inch()) != ERR) {
                if (waddch(chrwin, (chtype) ch) != ERR) {
                    for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
-                       if ((ch = mvinch(txt_y, j)) != ERR) {
+                       if ((ch = (int) mvinch(txt_y, j)) != ERR) {
                            if (waddch(chrwin, (chtype) ch) == ERR) {
                                break;
                            }
@@ -188,7 +218,7 @@ test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
        }
        wnoutrefresh(chrwin);
 
-       mvwprintw(strwin, 0, 0, "text:");
+       MvWPrintw(strwin, 0, 0, "text:");
        wclrtobot(strwin);
 
        limit = getmaxx(strwin) - 5;
@@ -196,38 +226,38 @@ test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
        if (txtwin != stdscr) {
            wmove(txtwin, txt_y, txt_x);
            if (winchstr(txtwin, text) != ERR) {
-               mvwaddchstr(strwin, 0, 5, text);
+               MvWAddChStr(strwin, 0, 5, text);
            }
 
            wmove(txtwin, txt_y, txt_x);
            if (winchnstr(txtwin, text, limit) != ERR) {
-               mvwaddchstr(strwin, 1, 5, text);
+               MvWAddChStr(strwin, 1, 5, text);
            }
 
            if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) {
-               mvwaddchstr(strwin, 2, 5, text);
+               MvWAddChStr(strwin, 2, 5, text);
            }
 
            if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) {
-               mvwaddchstr(strwin, 3, 5, text);
+               MvWAddChStr(strwin, 3, 5, text);
            }
        } else {
            move(txt_y, txt_x);
            if (inchstr(text) != ERR) {
-               mvwaddchstr(strwin, 0, 5, text);
+               MvWAddChStr(strwin, 0, 5, text);
            }
 
            move(txt_y, txt_x);
            if (inchnstr(text, limit) != ERR) {
-               mvwaddchstr(strwin, 1, 5, text);
+               MvWAddChStr(strwin, 1, 5, text);
            }
 
            if (mvinchstr(txt_y, txt_x, text) != ERR) {
-               mvwaddchstr(strwin, 2, 5, text);
+               MvWAddChStr(strwin, 2, 5, text);
            }
 
            if (mvinchnstr(txt_y, txt_x, text, limit) != ERR) {
-               mvwaddchstr(strwin, 3, 5, text);
+               MvWAddChStr(strwin, 3, 5, text);
            }
        }
 
@@ -250,7 +280,7 @@ main(int argc, char *argv[])
     setlocale(LC_ALL, "");
 
     if (argc < 2) {
-       fprintf(stderr, "usage: %s file\n", argv[0]);
+       fprintf(stderr, "usage: %s file1 [file2 [...]]\n", argv[0]);
        return EXIT_FAILURE;
     }
 
@@ -266,5 +296,5 @@ main(int argc, char *argv[])
     test_inchs(1, argv, chrwin, strwin);
 
     endwin();
-    return EXIT_SUCCESS;
+    ExitProgram(EXIT_SUCCESS);
 }