]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/savescreen.c
ncurses 6.0 - patch 20171223
[ncurses.git] / test / savescreen.c
index 111882d0b55888fb9f5fe6a1dee604f16fce6ea0..4be1bef5eaab462f941b3ec9e3e3e7ffbcd2ecec 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2007-2010,2011 Free Software Foundation, Inc.              *
+ * Copyright (c) 2007-2015,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            *
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: savescreen.c,v 1.15 2011/01/15 18:15:11 tom Exp $
+ * $Id: savescreen.c,v 1.36 2017/10/20 21:20:34 tom Exp $
  *
  * Demonstrate save/restore functions from the curses library.
  * Thomas Dickey - 2007/7/14
  */
 
 #include <test.priv.h>
+#include <popup_msg.h>
 
 #if HAVE_SCR_DUMP
 
 # endif
 #endif
 
+#if defined(__hpux)
+#define MyMarker 'X'
+#else
+#define MyMarker ACS_DIAMOND
+#endif
+
 static bool use_init = FALSE;
+static bool keep_dumps = FALSE;
+
+#if USE_WIDEC_SUPPORT
+/* In HPUX curses, cchar_t is opaque; other implementations are not */
+static wchar_t
+BaseChar(cchar_t data)
+{
+    wchar_t my_wchar[CCHARW_MAX];
+    attr_t my_attr;
+    short my_pair;
+    getcchar(&data, my_wchar, &my_attr, &my_pair, NULL);
+    return my_wchar[0];
+}
+#endif
 
 static int
 fexists(const char *name)
@@ -71,8 +92,10 @@ cleanup(char *files[])
 {
     int n;
 
-    for (n = 0; files[n] != 0; ++n) {
-       unlink(files[n]);
+    if (!keep_dumps) {
+       for (n = 0; files[n] != 0; ++n) {
+           unlink(files[n]);
+       }
     }
 }
 
@@ -105,13 +128,25 @@ after_load(void)
 static void
 show_what(int which, int last)
 {
-    int y, x;
-    time_t now = time((time_t *) 0);
+    int y, x, n;
+    time_t now;
+    char *mytime;
 
     getyx(stdscr, y, x);
 
     move(0, 0);
-    printw("Saved %d of %d - %s", which, last + 1, ctime(&now));
+    printw("Saved %d of %d (? for help)", which, last + 1);
+
+    now = time((time_t *) 0);
+    mytime = ctime(&now);
+    for (n = (int) strlen(mytime) - 1; n >= 0; --n) {
+       if (isspace(UChar(mytime[n]))) {
+           mytime[n] = '\0';
+       } else {
+           break;
+       }
+    }
+    mvprintw(0, (COLS - n - 2), " %s", mytime);
 
     move(y, x);
 
@@ -123,7 +158,7 @@ get_command(int which, int last)
 {
     int ch;
 
-    timeout(100);
+    timeout(50);
 
     do {
        show_what(which, last);
@@ -133,6 +168,45 @@ get_command(int which, int last)
     return ch;
 }
 
+static void
+editor_help(void)
+{
+    static const char *msgs[] =
+    {
+       "You are now in the screen-editor, which allows you to make some",
+       "lines on the screen, as well as save copies of the screen to a",
+       "temporary file",
+       "",
+       "Keys:",
+       "   q           quit",
+       "   n           run the screen-loader to show the saved screens",
+       "   <space>     dump a screen",
+       "",
+       "   a           toggle between '#' and graphic symbol for drawing",
+       "   c           change color drawn by line to next in palette",
+       "   h,j,k,l or arrows to move around the screen, drawing",
+       0
+    };
+    popup_msg(stdscr, msgs);
+}
+
+static void
+replay_help(void)
+{
+    static const char *msgs[] =
+    {
+       "You are now in the screen-loader, which allows you to view",
+       "the dumped/restored screens.",
+       "",
+       "Keys:",
+       "   q           quit",
+       "   <space>     load the next screen",
+       "   <backspace> load the previous screen",
+       0
+    };
+    popup_msg(stdscr, msgs);
+}
+
 static void
 usage(void)
 {
@@ -141,8 +215,10 @@ usage(void)
        "Usage: savescreen [-r] files",
        "",
        "Options:",
-       " -i  use scr_init/scr_restore rather than scr_set",
-       " -r  replay the screen-dump files"
+       " -f file  fill/initialize screen using text from this file",
+       " -i       use scr_init/scr_restore rather than scr_set",
+       " -k       keep the restored dump-files rather than removing them",
+       " -r       replay the screen-dump files"
     };
     unsigned n;
     for (n = 0; n < SIZEOF(msg); ++n) {
@@ -160,12 +236,25 @@ main(int argc, char *argv[])
     bool replaying = FALSE;
     bool done = FALSE;
     char **files;
+    char *fill_by = 0;
+#if USE_WIDEC_SUPPORT
+    cchar_t mycc;
+    int myxx;
+#endif
+
+    setlocale(LC_ALL, "");
 
-    while ((ch = getopt(argc, argv, "ir")) != -1) {
+    while ((ch = getopt(argc, argv, "f:ikr")) != -1) {
        switch (ch) {
+       case 'f':
+           fill_by = optarg;
+           break;
        case 'i':
            use_init = TRUE;
            break;
+       case 'k':
+           keep_dumps = TRUE;
+           break;
        case 'r':
            replaying = TRUE;
            break;
@@ -189,10 +278,44 @@ main(int argc, char *argv[])
     keypad(stdscr, TRUE);
     curs_set(0);
     if (has_colors()) {
+       short pair;
+       short color;
+
        start_color();
-       for (ch = 0; ch < COLOR_PAIRS; ++ch) {
-           short pair = (short) (ch % COLOR_PAIRS);
-           init_pair(pair, COLOR_WHITE, (short) (ch % COLORS));
+       /*
+        * Assume pairs is the square of colors, and assign pairs going down
+        * so that there is minimal conflict with the background color (which
+        * counts up).  The intent is just to show how color pair values are
+        * saved and restored.
+        */
+       for (pair = 0; pair < COLOR_PAIRS; ++pair) {
+           color = (short) (pair % (COLORS - 1));
+           init_pair(pair, (short) (COLOR_WHITE - color), color);
+       }
+    }
+
+    if (fill_by != 0) {
+       FILE *fp = fopen(fill_by, "r");
+       if (fp != 0) {
+           bool filled = FALSE;
+           move(1, 0);
+           while ((ch = fgetc(fp)) != EOF) {
+               if (addch(UChar(ch)) == ERR) {
+                   filled = TRUE;
+                   break;
+               }
+           }
+           fclose(fp);
+           if (!filled) {
+               while (addch(' ') != ERR) {
+                   ;
+               }
+           }
+           move(0, 0);
+       } else {
+           exit_curses();
+           fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
+           ExitProgram(EXIT_FAILURE);
        }
     }
 
@@ -202,14 +325,14 @@ main(int argc, char *argv[])
         * Use the last file as the initial/current screen.
         */
        if (last < 0) {
-           endwin();
+           exit_curses();
            printf("No screen-dumps given\n");
            ExitProgram(EXIT_FAILURE);
        }
 
        which = last;
        if (load_screen(files[which]) == ERR) {
-           endwin();
+           exit_curses();
            printf("Cannot load screen-dump %s\n", files[which]);
            ExitProgram(EXIT_FAILURE);
        }
@@ -227,7 +350,6 @@ main(int argc, char *argv[])
                done = TRUE;
                break;
            case 'q':
-               endwin();
                cleanup(files);
                done = TRUE;
                break;
@@ -240,6 +362,9 @@ main(int argc, char *argv[])
                if (++which > last)
                    which = 0;
                break;
+           case HELP_KEY_1:
+               replay_help();
+               break;
            default:
                beep();
                continue;
@@ -256,17 +381,12 @@ main(int argc, char *argv[])
                wrefresh(curscr);
            }
        }
+       endwin();
     } else {
-       int y;
-       int x;
-
-       move(2, 0);
-       printw("Use h,j,k,l or arrows to move around the screen\n");
-       printw("Press 'q' to quit, ' ' to dump a screen\n");
-       printw("When the last screen has been dumped, press 'n' to run the\n");
-       printw("screen-loader.  That allows only 'q', backspace and ' ' for\n");
-       printw("stepping through the dumped/restored screens.\n");
-       getyx(stdscr, y, x);
+       int y = 0;
+       int x = 0;
+       int color = 0;
+       int altchars = 0;
 
        while (!done) {
            switch (get_command(which, last)) {
@@ -275,7 +395,6 @@ main(int argc, char *argv[])
                done = TRUE;
                break;
            case 'q':
-               endwin();
                cleanup(files);
                done = TRUE;
                break;
@@ -291,8 +410,32 @@ main(int argc, char *argv[])
                    }
                    ++which;
                    if (has_colors()) {
+                       int cx, cy;
                        short pair = (short) (which % COLOR_PAIRS);
+                       /*
+                        * Change the background color, to make it more
+                        * obvious.  But that changes the existing text-color. 
+                        * Copy the old values from the currently displayed
+                        * screen.
+                        */
                        bkgd((chtype) COLOR_PAIR(pair));
+                       for (cy = 1; cy < LINES; ++cy) {
+                           for (cx = 0; cx < COLS; ++cx) {
+                               wmove(curscr, cy, cx);
+                               wmove(stdscr, cy, cx);
+#if USE_WIDEC_SUPPORT
+                               if (win_wch(curscr, &mycc) != ERR) {
+                                   myxx = wcwidth(BaseChar(mycc));
+                                   if (myxx > 0) {
+                                       wadd_wchnstr(stdscr, &mycc, 1);
+                                       cx += (myxx - 1);
+                                   }
+                               }
+#else
+                               waddch(stdscr, winch(curscr));
+#endif
+                           }
+                       }
                    }
                } else {
                    beep();
@@ -318,23 +461,35 @@ main(int argc, char *argv[])
                if (++x >= COLS)
                    x = 0;
                break;
+           case 'a':
+               altchars = !altchars;
+               break;
+           case 'c':
+               color = (color + 1) % COLORS;
+               break;
+           case HELP_KEY_1:
+               editor_help();
+               break;
+           default:
+               beep();
+               continue;
            }
            if (!done) {
-               time_t now = time((time_t *) 0);
-
-               move(0, 0);
-               addstr(ctime(&now));
+               chtype attr = (A_REVERSE | (chtype) COLOR_PAIR(color * COLORS));
+               chtype ch2 = (altchars ? MyMarker : '#');
                move(y, x);
-               addch('#' | A_REVERSE);
+               AddCh(ch2 | attr);
                move(y, x);
            }
        }
+       endwin();
     }
     ExitProgram(EXIT_SUCCESS);
 }
+
 #else
 int
-main(int argc, char *argv[])
+main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
 {
     printf("This program requires the screen-dump functions\n");
     ExitProgram(EXIT_FAILURE);