Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2020-2021,2022 Thomas E. Dickey * |
| 3 | * Copyright 2006-2012,2017 Free Software Foundation, Inc. * |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4 | * * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a * |
| 6 | * copy of this software and associated documentation files (the * |
| 7 | * "Software"), to deal in the Software without restriction, including * |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, * |
| 9 | * distribute, distribute with modifications, sublicense, and/or sell * |
| 10 | * copies of the Software, and to permit persons to whom the Software is * |
| 11 | * furnished to do so, subject to the following conditions: * |
| 12 | * * |
| 13 | * The above copyright notice and this permission notice shall be included * |
| 14 | * in all copies or substantial portions of the Software. * |
| 15 | * * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
| 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
| 19 | * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
| 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
| 21 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
| 22 | * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
| 23 | * * |
| 24 | * Except as contained in this notice, the name(s) of the above copyright * |
| 25 | * holders shall not be used in advertising or otherwise to promote the * |
| 26 | * sale, use or other dealings in this Software without prior written * |
| 27 | * authorization. * |
| 28 | ****************************************************************************/ |
| 29 | /* |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 30 | * $Id: redraw.c,v 1.17 2022/12/10 22:28:50 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 31 | * |
| 32 | * Demonstrate the redrawwin() and wredrawln() functions. |
| 33 | * Thomas Dickey - 2006/11/4 |
| 34 | */ |
| 35 | |
| 36 | #include <test.priv.h> |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 37 | #include <popup_msg.h> |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 38 | |
| 39 | static void |
| 40 | trash(int beg_x, int max_x, int cur_x) |
| 41 | { |
| 42 | int x; |
| 43 | |
| 44 | for (x = cur_x; x > beg_x; --x) { |
| 45 | putchar('\b'); |
| 46 | } |
| 47 | for (x = beg_x; x < max_x; ++x) { |
| 48 | if (x < cur_x) |
| 49 | putchar('<'); |
| 50 | else if (x == cur_x) |
| 51 | putchar('='); |
| 52 | else if (x > cur_x) |
| 53 | putchar('>'); |
| 54 | } |
| 55 | for (x = max_x; x > cur_x; --x) { |
| 56 | putchar('\b'); |
| 57 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 58 | fflush(stdout); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static void |
| 62 | test_redraw(WINDOW *win) |
| 63 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 64 | static const char *help[] = |
| 65 | { |
| 66 | "Commands:", |
| 67 | " ^Q/ESC/q - quit", |
| 68 | " w - recur in a new window", |
| 69 | " ! - overwrite current line using stdio outside curses.", |
| 70 | #ifdef NCURSES_VERSION |
| 71 | " @ - run \"date\" command, to put its output on screen.", |
| 72 | #endif |
| 73 | " ^L - call redrawwin() for current window.", |
| 74 | " ^W - call wredrawln() for current line/current window.", |
| 75 | " arrow-keys - move cursor on the screen", |
| 76 | "", |
| 77 | "Other control characters are added to the screen in printable form.", |
| 78 | "Other printable characters are added to the screen as is.", |
| 79 | 0 |
| 80 | }; |
| 81 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 82 | WINDOW *win1; |
| 83 | WINDOW *win2; |
| 84 | bool done = FALSE; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 85 | int max_y, max_x; |
| 86 | int beg_y, beg_x; |
| 87 | |
| 88 | assert(win != 0); |
| 89 | |
| 90 | scrollok(win, TRUE); |
| 91 | keypad(win, TRUE); |
| 92 | getmaxyx(win, max_y, max_x); |
| 93 | getbegyx(win, beg_y, beg_x); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 94 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 95 | while (!done) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 96 | int ch = wgetch(win); |
| 97 | int y, x; |
| 98 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 99 | getyx(win, y, x); |
| 100 | switch (ch) { |
| 101 | case 'q': |
| 102 | /* FALLTHRU */ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 103 | case QUIT: |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 104 | case ESCAPE: |
| 105 | done = TRUE; |
| 106 | break; |
| 107 | case 'w': |
| 108 | win1 = newwin(max_y, max_x, |
| 109 | beg_y, beg_x); |
| 110 | win2 = newwin(max_y - 2, max_x - 2, |
| 111 | beg_y + 1, beg_x + 1); |
| 112 | box(win1, 0, 0); |
| 113 | wrefresh(win1); |
| 114 | |
| 115 | test_redraw(win2); |
| 116 | |
| 117 | delwin(win2); |
| 118 | delwin(win1); |
| 119 | |
| 120 | touchwin(win); |
| 121 | break; |
| 122 | |
| 123 | case '!': |
| 124 | /* |
| 125 | * redrawwin() and wredrawln() do not take into account the |
| 126 | * possibility that the cursor may have moved. That makes them |
| 127 | * cumbersome for using with a shell command. So we simply |
| 128 | * trash the current line of the window using backspace/overwrite. |
| 129 | */ |
| 130 | trash(beg_x, max_x, x + beg_x); |
| 131 | break; |
| 132 | |
| 133 | #ifdef NCURSES_VERSION |
| 134 | case '@': |
| 135 | /* |
| 136 | * For a shell command, we can work around the problem noted above |
| 137 | * using mvcur(). It is ifdef'd for NCURSES, since X/Open does |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 138 | * not define the case where the old location is unknown. |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 139 | */ |
| 140 | IGNORE_RC(system("date")); |
| 141 | mvcur(-1, -1, y, x); |
| 142 | break; |
| 143 | #endif |
| 144 | |
| 145 | case CTRL('W'): |
| 146 | redrawwin(win); |
| 147 | break; |
| 148 | |
| 149 | case CTRL('L'): |
| 150 | wredrawln(win, y, 1); |
| 151 | break; |
| 152 | |
| 153 | case KEY_UP: |
| 154 | if (y > 0) |
| 155 | wmove(win, y - 1, x); |
| 156 | break; |
| 157 | |
| 158 | case KEY_DOWN: |
| 159 | if (y < max_y) |
| 160 | wmove(win, y + 1, x); |
| 161 | break; |
| 162 | |
| 163 | case KEY_LEFT: |
| 164 | if (x > 0) |
| 165 | wmove(win, y, x - 1); |
| 166 | break; |
| 167 | |
| 168 | case KEY_RIGHT: |
| 169 | if (x < max_x) |
| 170 | wmove(win, y, x + 1); |
| 171 | break; |
| 172 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 173 | case HELP_KEY_1: |
| 174 | popup_msg(win, help); |
| 175 | break; |
| 176 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 177 | default: |
| 178 | if (ch > KEY_MIN) { |
| 179 | waddstr(win, keyname(ch)); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 180 | waddch(win, '\n'); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 181 | } else { |
| 182 | waddstr(win, unctrl(UChar(ch))); |
| 183 | } |
| 184 | break; |
| 185 | } |
| 186 | wnoutrefresh(win); |
| 187 | doupdate(); |
| 188 | } |
| 189 | } |
| 190 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 191 | static void |
| 192 | usage(int ok) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 193 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 194 | static const char *tbl[] = |
| 195 | { |
| 196 | "Usage: redraw [options]" |
| 197 | ,"" |
| 198 | ,USAGE_COMMON |
| 199 | ,"Options:" |
| 200 | ," -e use stderr (default stdout)" |
| 201 | ," -n do not initialize terminal" |
| 202 | }; |
| 203 | unsigned n; |
| 204 | for (n = 0; n < SIZEOF(tbl); ++n) |
| 205 | fprintf(stderr, "%s\n", tbl[n]); |
| 206 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 207 | } |
| 208 | /* *INDENT-OFF* */ |
| 209 | VERSION_COMMON() |
| 210 | /* *INDENT-ON* */ |
| 211 | |
| 212 | int |
| 213 | main(int argc, char *argv[]) |
| 214 | { |
| 215 | int ch; |
| 216 | bool no_init = FALSE; |
| 217 | FILE *my_fp = stdout; |
| 218 | |
| 219 | while ((ch = getopt(argc, argv, OPTS_COMMON "en")) != -1) { |
| 220 | switch (ch) { |
| 221 | case 'e': |
| 222 | my_fp = stderr; |
| 223 | break; |
| 224 | case 'n': |
| 225 | no_init = TRUE; |
| 226 | break; |
| 227 | case OPTS_VERSION: |
| 228 | show_version(argv); |
| 229 | ExitProgram(EXIT_SUCCESS); |
| 230 | default: |
| 231 | usage(ch == OPTS_USAGE); |
| 232 | /* NOTREACHED */ |
| 233 | } |
| 234 | } |
| 235 | if (optind < argc) |
| 236 | usage(FALSE); |
| 237 | |
| 238 | if (no_init) { |
| 239 | START_TRACE(); |
| 240 | } else { |
| 241 | newterm((char *) 0, my_fp, stdin); |
| 242 | } |
| 243 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 244 | raw(); |
| 245 | noecho(); |
| 246 | test_redraw(stdscr); |
| 247 | endwin(); |
| 248 | ExitProgram(EXIT_SUCCESS); |
| 249 | } |