]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/newdemo.c
ncurses 6.2 - patch 20200627
[ncurses.git] / test / newdemo.c
index 7f583389c321bfa83b517f67cf96e5c0e7c56eb7..d76523ab6ffda5c7930f8f224fdc21fdbd820d4d 100644 (file)
@@ -2,19 +2,17 @@
  *  newdemo.c  -       A demo program using PDCurses. The program illustrate
  *                     the use of colours for text output.
  *
  *  newdemo.c  -       A demo program using PDCurses. The program illustrate
  *                     the use of colours for text output.
  *
- * $Id: newdemo.c,v 1.24 2002/06/29 23:32:18 tom Exp $
+ * $Id: newdemo.c,v 1.47 2019/12/14 23:25:29 tom Exp $
  */
 
  */
 
-#include <time.h>
-
 #include <test.priv.h>
 
 #include <test.priv.h>
 
-#define delay_output(x) napms(x)
+#include <time.h>
 
 /*
  *  The Australian map
  */
 
 /*
  *  The Australian map
  */
-const char *AusMap[16] =
+static CONST_MENUS char *AusMap[16] =
 {
     "           A           A ",
     "    N.T. AAAAA       AAAA ",
 {
     "           A           A ",
     "    N.T. AAAAA       AAAA ",
@@ -35,7 +33,7 @@ const char *AusMap[16] =
  */
 #define NMESSAGES   6
 
  */
 #define NMESSAGES   6
 
-NCURSES_CONST char *messages[] =
+static const char *messages[] =
 {
     "Hello from the Land Down Under",
     "The Land of crocs. and a big Red Rock",
 {
     "Hello from the Land Down Under",
     "The Land of crocs. and a big Red Rock",
@@ -49,10 +47,10 @@ NCURSES_CONST char *messages[] =
 /*
  *  Trap interrupt
  */
 /*
  *  Trap interrupt
  */
-static RETSIGTYPE
+static void
 trap(int sig GCC_UNUSED)
 {
 trap(int sig GCC_UNUSED)
 {
-    endwin();
+    stop_curses();
     ExitProgram(EXIT_FAILURE);
 }
 
     ExitProgram(EXIT_FAILURE);
 }
 
@@ -63,12 +61,13 @@ static int
 WaitForUser(WINDOW *win)
 {
     time_t t;
 WaitForUser(WINDOW *win)
 {
     time_t t;
-    chtype key;
 
     nodelay(win, TRUE);
     t = time((time_t *) 0);
 
     nodelay(win, TRUE);
     t = time((time_t *) 0);
+
     while (1) {
     while (1) {
-       if ((int) (key = wgetch(win)) != ERR) {
+       chtype key;
+       if ((int) (key = (chtype) wgetch(win)) != ERR) {
            if (key == 'q' || key == 'Q')
                return 1;
            else
            if (key == 'q' || key == 'Q')
                return 1;
            else
@@ -85,20 +84,20 @@ set_colors(WINDOW *win, int pair, int foreground, int background)
     if (has_colors()) {
        if (pair > COLOR_PAIRS)
            pair = COLOR_PAIRS;
     if (has_colors()) {
        if (pair > COLOR_PAIRS)
            pair = COLOR_PAIRS;
-       init_pair(pair, foreground, background);
-       wattrset(win, COLOR_PAIR(pair));
+       init_pair((short) pair, (short) foreground, (short) background);
+       (void) wattrset(win, AttrArg(COLOR_PAIR(pair), 0));
     }
 }
 
     }
 }
 
-static int
-use_colors(WINDOW *win, int pair, int attrs)
+static chtype
+use_colors(WINDOW *win, int pair, chtype attrs)
 {
     if (has_colors()) {
        if (pair > COLOR_PAIRS)
            pair = COLOR_PAIRS;
 {
     if (has_colors()) {
        if (pair > COLOR_PAIRS)
            pair = COLOR_PAIRS;
-       attrs |= COLOR_PAIR(pair);
+       attrs |= (chtype) COLOR_PAIR(pair);
     }
     }
-    wattrset(win, attrs);
+    (void) wattrset(win, AttrArg(attrs, 0));
     return attrs;
 }
 
     return attrs;
 }
 
@@ -115,26 +114,33 @@ SubWinTest(WINDOW *win)
     getbegyx(win, by, bx);
     sw = w / 3;
     sh = h / 3;
     getbegyx(win, by, bx);
     sw = w / 3;
     sh = h / 3;
-    if ((swin1 = subwin(win, sh, sw, by + 3, bx + 5)) == NULL)
+
+    if ((swin1 = subwin(win, sh, sw, by + 3, bx + 5)) == NULL) {
        return 1;
        return 1;
-    if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL)
+    }
+    if ((swin2 = subwin(win, sh, sw, by + 4, bx + 8)) == NULL) {
+       delwin(swin1);
        return 1;
        return 1;
-    if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL)
+    }
+    if ((swin3 = subwin(win, sh, sw, by + 5, bx + 11)) == NULL) {
+       delwin(swin1);
+       delwin(swin2);
        return 1;
        return 1;
+    }
 
     set_colors(swin1, 8, COLOR_RED, COLOR_BLUE);
     werase(swin1);
 
     set_colors(swin1, 8, COLOR_RED, COLOR_BLUE);
     werase(swin1);
-    mvwaddstr(swin1, 0, 3, "Sub-window 1");
+    MvWAddStr(swin1, 0, 3, "Sub-window 1");
     wrefresh(swin1);
 
     set_colors(swin2, 9, COLOR_CYAN, COLOR_MAGENTA);
     werase(swin2);
     wrefresh(swin1);
 
     set_colors(swin2, 9, COLOR_CYAN, COLOR_MAGENTA);
     werase(swin2);
-    mvwaddstr(swin2, 0, 3, "Sub-window 2");
+    MvWAddStr(swin2, 0, 3, "Sub-window 2");
     wrefresh(swin2);
 
     set_colors(swin3, 10, COLOR_YELLOW, COLOR_GREEN);
     werase(swin3);
     wrefresh(swin2);
 
     set_colors(swin3, 10, COLOR_YELLOW, COLOR_GREEN);
     werase(swin3);
-    mvwaddstr(swin3, 0, 3, "Sub-window 3");
+    MvWAddStr(swin3, 0, 3, "Sub-window 3");
     wrefresh(swin3);
 
     delwin(swin1);
     wrefresh(swin3);
 
     delwin(swin1);
@@ -194,13 +200,13 @@ BouncingBalls(WINDOW *win)
        y3 = bounce(y3, &yd3, h);
 
        set_colors(win, 11, COLOR_RED, COLOR_BLUE);
        y3 = bounce(y3, &yd3, h);
 
        set_colors(win, 11, COLOR_RED, COLOR_BLUE);
-       mvwaddch(win, y1, x1, 'O');
+       MvWAddCh(win, y1, x1, 'O');
 
        set_colors(win, 12, COLOR_BLUE, COLOR_RED);
 
        set_colors(win, 12, COLOR_BLUE, COLOR_RED);
-       mvwaddch(win, y2, x2, '*');
+       MvWAddCh(win, y2, x2, '*');
 
        set_colors(win, 13, COLOR_YELLOW, COLOR_WHITE);
 
        set_colors(win, 13, COLOR_YELLOW, COLOR_WHITE);
-       mvwaddch(win, y3, x3, '@');
+       MvWAddCh(win, y3, x3, '@');
 
        wmove(win, 0, 0);
        wrefresh(win);
 
        wmove(win, 0, 0);
        wrefresh(win);
@@ -216,30 +222,32 @@ int
 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
 {
     WINDOW *win;
 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
 {
     WINDOW *win;
-    int w, x, y, i, j, k;
-    char buffer[200];
-    const char *message;
+    int x, y, i, k;
+    char buffer[SIZEOF(messages) * 80];
     int width, height;
     chtype save[80];
     int width, height;
     chtype save[80];
-    chtype c;
 
     setlocale(LC_ALL, "");
 
 
     setlocale(LC_ALL, "");
 
-    initscr();
+    InitAndCatch(initscr(), trap);
     if (has_colors())
        start_color();
     cbreak();
     curs_set(0);
     if (has_colors())
        start_color();
     cbreak();
     curs_set(0);
-    signal(SIGINT, trap);
     width = 48;
     height = 14;               /* Create a drawing window */
     win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
     if (win == NULL) {
     width = 48;
     height = 14;               /* Create a drawing window */
     win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
     if (win == NULL) {
-       endwin();
+       stop_curses();
        ExitProgram(EXIT_FAILURE);
     }
 
     while (1) {
        ExitProgram(EXIT_FAILURE);
     }
 
     while (1) {
+       int w;
+       int j;
+       chtype c;
+       const char *message;
+
        set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
        werase(win);
 
        set_colors(win, 1, COLOR_WHITE, COLOR_BLUE);
        werase(win);
 
@@ -252,7 +260,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
        for (i = 0; i < 5000; ++i) {
            x = rand() % (width - 2) + 1;
            y = rand() % (height - 2) + 1;
        for (i = 0; i < 5000; ++i) {
            x = rand() % (width - 2) + 1;
            y = rand() % (height - 2) + 1;
-           mvwaddch(win, y, x, c);
+           MvWAddCh(win, y, x, c);
            wrefresh(win);
            nodelay(win, TRUE);
            if (wgetch(win) != ERR)
            wrefresh(win);
            nodelay(win, TRUE);
            if (wgetch(win) != ERR)
@@ -277,7 +285,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
        use_colors(win, 4, A_BOLD);
        i = 0;
        while (*AusMap[i]) {
        use_colors(win, 4, A_BOLD);
        i = 0;
        while (*AusMap[i]) {
-           mvwaddstr(win, i + 1, 8, AusMap[i]);
+           MvWAddStr(win, i + 1, 8, AusMap[i]);
            wrefresh(win);
            delay_output(50);
            ++i;
            wrefresh(win);
            delay_output(50);
            ++i;
@@ -285,7 +293,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
 
        set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
        use_colors(win, 5, A_BLINK);
 
        set_colors(win, 5, COLOR_BLUE, COLOR_WHITE);
        use_colors(win, 5, A_BLINK);
-       mvwaddstr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
+       MvWAddStr(win, height - 2, 6, " PDCurses 2.1 for DOS, OS/2 and Unix");
        wrefresh(win);
 
        /* Draw running messages */
        wrefresh(win);
 
        /* Draw running messages */
@@ -293,17 +301,17 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
        message = messages[j = 0];
        i = 1;
        w = width - 2;
        message = messages[j = 0];
        i = 1;
        w = width - 2;
-       strcpy(buffer, message);
+       _nc_STRCPY(buffer, message, sizeof(buffer));
        while (j < NMESSAGES) {
            while ((int) strlen(buffer) < w) {
        while (j < NMESSAGES) {
            while ((int) strlen(buffer) < w) {
-               strcat(buffer, " ... ");
-               strcat(buffer, messages[++j % NMESSAGES]);
+               _nc_STRCAT(buffer, " ... ", sizeof(buffer));
+               _nc_STRCAT(buffer, messages[++j % NMESSAGES], sizeof(buffer));
            }
 
            if (i < w)
            }
 
            if (i < w)
-               mvwaddnstr(win, height / 2, w - i, buffer, i);
+               (void) mvwaddnstr(win, height / 2, w - i, buffer, i);
            else
            else
-               mvwaddnstr(win, height / 2, 1, buffer, w);
+               (void) mvwaddnstr(win, height / 2, 1, buffer, w);
 
            wrefresh(win);
            nodelay(win, TRUE);
 
            wrefresh(win);
            nodelay(win, TRUE);
@@ -320,20 +328,21 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
        j = 0;
        /*  Draw running As across in RED */
        set_colors(win, 7, COLOR_RED, COLOR_GREEN);
        j = 0;
        /*  Draw running As across in RED */
        set_colors(win, 7, COLOR_RED, COLOR_GREEN);
+       memset(save, ' ', sizeof(save));
        for (i = 2; i < width - 4; ++i) {
        for (i = 2; i < width - 4; ++i) {
-           k = mvwinch(win, 4, i);
+           k = (int) mvwinch(win, 4, i);
            if (k == ERR)
                break;
            if (k == ERR)
                break;
-           save[j++] = c = k;
+           save[j++] = c = (chtype) k;
            c &= A_CHARTEXT;
            c &= A_CHARTEXT;
-           mvwaddch(win, 4, i, c);
+           MvWAddCh(win, 4, i, c);
        }
        wrefresh(win);
 
        /* Put a message up wait for a key */
        i = height - 2;
        use_colors(win, 5, A_NORMAL);
        }
        wrefresh(win);
 
        /* Put a message up wait for a key */
        i = height - 2;
        use_colors(win, 5, A_NORMAL);
-       mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
+       MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
        wrefresh(win);
 
        if (WaitForUser(win) == 1)
        wrefresh(win);
 
        if (WaitForUser(win) == 1)
@@ -341,18 +350,18 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
 
        j = 0;                  /* Restore the old line */
        for (i = 2; i < width - 4; ++i)
 
        j = 0;                  /* Restore the old line */
        for (i = 2; i < width - 4; ++i)
-           mvwaddch(win, 4, i, save[j++]);
+           MvWAddCh(win, 4, i, save[j++]);
        wrefresh(win);
 
        BouncingBalls(win);
        /* Put a message up wait for a key */
        i = height - 2;
        use_colors(win, 5, A_NORMAL);
        wrefresh(win);
 
        BouncingBalls(win);
        /* Put a message up wait for a key */
        i = height - 2;
        use_colors(win, 5, A_NORMAL);
-       mvwaddstr(win, i, 5, " Type a key to continue or 'Q' to quit ");
+       MvWAddStr(win, i, 5, " Type a key to continue or 'Q' to quit ");
        wrefresh(win);
        if (WaitForUser(win) == 1)
            break;
     }
        wrefresh(win);
        if (WaitForUser(win) == 1)
            break;
     }
-    endwin();
+    stop_curses();
     ExitProgram(EXIT_SUCCESS);
 }
     ExitProgram(EXIT_SUCCESS);
 }