]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/movewindow.c
ncurses 6.1 - patch 20180623
[ncurses.git] / test / movewindow.c
index b855942832c7ed97ab19f688e181b19a7042dc51..dca14d44b9d34c09c03671d47946fd155d364192 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2006-2010,2012 Free Software Foundation, Inc.              *
+ * Copyright (c) 2006-2017,2018 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,7 @@
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: movewindow.c,v 1.36 2012/09/01 23:54:00 tom Exp $
+ * $Id: movewindow.c,v 1.47 2018/06/09 17:35:50 tom Exp $
  *
  * Demonstrate move functions for windows and derived windows from the curses
  * library.
@@ -45,13 +45,16 @@ TODO:
  */
 
 #include <test.priv.h>
-#include <stdarg.h>
+
+#if HAVE_MVDERWIN && HAVE_MVWIN
+
+#include <popup_msg.h>
 
 #ifdef HAVE_XCURSES
 #undef derwin
 #endif
 
-#ifdef NCURSES_VERSION
+#if defined(NCURSES_VERSION) || defined(PDCURSES)
 #define CONST_FMT const
 #else
 #define CONST_FMT              /* nothing */
@@ -79,6 +82,14 @@ static void tail_line(CONST_FMT char *fmt,...) GCC_PRINTFLIKE(1, 2);
 static unsigned num_windows;
 static FRAME *all_windows;
 
+static void
+failed(const char *s)
+{
+    perror(s);
+    endwin();
+    ExitProgram(EXIT_FAILURE);
+}
+
 static void
 message(int lineno, CONST_FMT char *fmt, va_list argp)
 {
@@ -95,7 +106,7 @@ message(int lineno, CONST_FMT char *fmt, va_list argp)
        addstr(buffer);
     }
 #else
-    vwprintw(stdscr, fmt, argp);
+    vw_printw(stdscr, fmt, argp);
 #endif
 
     move(y, x);
@@ -182,6 +193,7 @@ selectcell(WINDOW *parent,
            moved = TRUE;
            break;
        case QUIT:
+           /* FALLTHRU */
        case ESCAPE:
            return ((PAIR *) 0);
 #ifdef NCURSES_MOUSE_VERSION
@@ -203,15 +215,23 @@ selectcell(WINDOW *parent,
                    break;
                }
            }
-           /* FALLTHRU */
 #endif
+           /* FALLTHRU */
        default:
            res.y = uli + i;
            res.x = ulj + j;
            return (&res);
        }
-       i %= si;
-       j %= sj;
+
+       if (si <= 0)
+           i = 0;
+       else
+           i %= si;
+
+       if (sj <= 0)
+           j = 0;
+       else
+           j %= sj;
 
        /*
         * If the caller can handle continuous movement, return the result.
@@ -305,6 +325,8 @@ add_window(WINDOW *parent, WINDOW *child)
     keypad(child, TRUE);
     if (need > have) {
        all_windows = typeRealloc(FRAME, need, all_windows);
+       if (!all_windows)
+           failed("add_window");
     }
     all_windows[num_windows].parent = parent;
     all_windows[num_windows].child = child;
@@ -476,34 +498,32 @@ move_derwin(WINDOW *win)
 
     if (parent != 0) {
        bool top = (parent == stdscr);
-       if (!top) {
-           int min_col = top ? COL_MIN : 0;
-           int max_col = top ? COL_MAX : getmaxx(parent);
-           int min_line = top ? LINE_MIN : 0;
-           int max_line = top ? LINE_MAX : getmaxy(parent);
-           PAIR *tmp;
-           bool more;
-
-           show_derwin(win);
-           while ((tmp = selectcell(parent,
-                                    win,
-                                    min_line, min_col,
-                                    max_line, max_col,
-                                    TRUE,
-                                    &more)) != 0) {
-               if (mvderwin(win, tmp->y, tmp->x) != ERR) {
-                   refresh_all(win);
-                   doupdate();
-                   repaint_one(win);
-                   doupdate();
-                   result = TRUE;
-                   show_derwin(win);
-               } else {
-                   flash();
-               }
-               if (!more)
-                   break;
+       int min_col = top ? COL_MIN : 0;
+       int max_col = top ? COL_MAX : getmaxx(parent);
+       int min_line = top ? LINE_MIN : 0;
+       int max_line = top ? LINE_MAX : getmaxy(parent);
+       PAIR *tmp;
+       bool more;
+
+       show_derwin(win);
+       while ((tmp = selectcell(parent,
+                                win,
+                                min_line, min_col,
+                                max_line, max_col,
+                                TRUE,
+                                &more)) != 0) {
+           if (mvderwin(win, tmp->y, tmp->x) != ERR) {
+               refresh_all(win);
+               doupdate();
+               repaint_one(win);
+               doupdate();
+               result = TRUE;
+               show_derwin(win);
+           } else {
+               flash();
            }
+           if (!more)
+               break;
        }
     }
     head_line("done");
@@ -621,7 +641,7 @@ show_help(WINDOW *current)
        int     key;
        CONST_FMT char * msg;
     } help[] = {
-       { '?',          "Show this screen" },
+       { HELP_KEY_1,   "Show this screen" },
        { 'b',          "Draw a box inside the current window" },
        { 'c',          "Create a new window" },
        { 'd',          "Create a new derived window" },
@@ -638,20 +658,18 @@ show_help(WINDOW *current)
     };
     /* *INDENT-ON* */
 
-    WINDOW *mywin = newwin(LINES, COLS, 0, 0);
-    int row;
+    char **msgs = typeCalloc(char *, SIZEOF(help) + 1);
+    size_t n;
 
-    for (row = 0; row < LINES - 2 && row < (int) SIZEOF(help); ++row) {
-       wmove(mywin, row + 1, 1);
-       wprintw(mywin, "%s", keyname(help[row].key));
-       wmove(mywin, row + 1, 20);
-       wprintw(mywin, "%s", help[row].msg);
+    for (n = 0; n < SIZEOF(help); ++n) {
+       msgs[n] = typeMalloc(char, 21 + strlen(help[n].msg));
+       sprintf(msgs[n], "%-20s%s", keyname(help[n].key), help[n].msg);
     }
-    box_inside(mywin);
-    wmove(mywin, 1, 1);
-    wgetch(mywin);
-    delwin(mywin);
-    refresh_all(current);
+    popup_msg2(current, msgs);
+    for (n = 0; n < SIZEOF(help); ++n) {
+       free(msgs[n]);
+    }
+    free(msgs);
 }
 
 int
@@ -679,7 +697,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
        getyx(current_win, y, x);
 
        switch (ch) {
-       case '?':
+       case HELP_KEY_1:
            show_help(current_win);
            break;
        case 'b':
@@ -746,5 +764,16 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
        wmove(current_win, 0, 0);
     }
     endwin();
+#if NO_LEAKS
+    free(all_windows);
+#endif
     ExitProgram(EXIT_SUCCESS);
 }
+#else
+int
+main(void)
+{
+    printf("This program requires the curses mvderwin and mvwin functions\n");
+    ExitProgram(EXIT_FAILURE);
+}
+#endif