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 2019-2020,2022 Thomas E. Dickey * |
| 3 | * Copyright 2007-2010,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: inch_wide.c,v 1.13 2022/12/10 23:55:34 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 31 | */ |
| 32 | /* |
| 33 | int in_wch(cchar_t *wcval); |
| 34 | int mvin_wch(int y, int x, cchar_t *wcval); |
| 35 | int mvwin_wch(WINDOW *win, int y, int x, cchar_t *wcval); |
| 36 | int win_wch(WINDOW *win, cchar_t *wcval); |
| 37 | int in_wchstr(cchar_t *wchstr); |
| 38 | int in_wchnstr(cchar_t *wchstr, int n); |
| 39 | int win_wchstr(WINDOW *win, cchar_t *wchstr); |
| 40 | int win_wchnstr(WINDOW *win, cchar_t *wchstr, int n); |
| 41 | int mvin_wchstr(int y, int x, cchar_t *wchstr); |
| 42 | int mvin_wchnstr(int y, int x, cchar_t *wchstr, int n); |
| 43 | int mvwin_wchstr(WINDOW *win, int y, int x, cchar_t *wchstr); |
| 44 | int mvwin_wchnstr(WINDOW *win, int y, int x, cchar_t *wchstr, int n); |
| 45 | */ |
| 46 | |
| 47 | #include <test.priv.h> |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 48 | #include <popup_msg.h> |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 49 | |
| 50 | #if USE_WIDEC_SUPPORT |
| 51 | |
| 52 | #define BASE_Y 7 |
| 53 | #define MAX_COLS 1024 |
| 54 | |
| 55 | static bool |
| 56 | Quit(int ch) |
| 57 | { |
| 58 | return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE); |
| 59 | } |
| 60 | |
| 61 | static int |
| 62 | test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin) |
| 63 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 64 | static const char *help[] = |
| 65 | { |
| 66 | "Test input from screen using inch(), etc., in a moveable viewport.", |
| 67 | "", |
| 68 | "Commands:", |
| 69 | " ESC/^Q - quit", |
| 70 | " h,j,k,l (and arrow-keys) - move viewport", |
| 71 | " w - recur to new window", |
| 72 | " for next input file", |
| 73 | 0 |
| 74 | }; |
| 75 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 76 | WINDOW *txtbox = 0; |
| 77 | WINDOW *txtwin = 0; |
| 78 | FILE *fp; |
| 79 | int j; |
| 80 | int txt_x = 0, txt_y = 0; |
| 81 | int base_y; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 82 | cchar_t ch; |
| 83 | cchar_t text[MAX_COLS]; |
| 84 | |
| 85 | if (argv[level] == 0) { |
| 86 | beep(); |
| 87 | return FALSE; |
| 88 | } |
| 89 | |
| 90 | if (level > 1) { |
| 91 | txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level); |
| 92 | box(txtbox, 0, 0); |
| 93 | wnoutrefresh(txtbox); |
| 94 | |
| 95 | txtwin = derwin(txtbox, |
| 96 | getmaxy(txtbox) - 2, |
| 97 | getmaxx(txtbox) - 2, |
| 98 | 1, 1); |
| 99 | base_y = 0; |
| 100 | } else { |
| 101 | txtwin = stdscr; |
| 102 | base_y = BASE_Y; |
| 103 | } |
| 104 | |
| 105 | keypad(txtwin, TRUE); /* enable keyboard mapping */ |
| 106 | (void) cbreak(); /* take input chars one at a time, no wait for \n */ |
| 107 | (void) noecho(); /* don't echo input */ |
| 108 | |
| 109 | txt_y = base_y; |
| 110 | txt_x = 0; |
| 111 | wmove(txtwin, txt_y, txt_x); |
| 112 | |
| 113 | if ((fp = fopen(argv[level], "r")) != 0) { |
| 114 | while ((j = fgetc(fp)) != EOF) { |
| 115 | if (waddch(txtwin, UChar(j)) != OK) { |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | fclose(fp); |
| 120 | } else { |
| 121 | wprintw(txtwin, "Cannot open:\n%s", argv[1]); |
| 122 | } |
| 123 | |
| 124 | while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 125 | int limit; |
| 126 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 127 | switch (j) { |
| 128 | case KEY_DOWN: |
| 129 | case 'j': |
| 130 | if (txt_y < getmaxy(txtwin) - 1) |
| 131 | txt_y++; |
| 132 | else |
| 133 | beep(); |
| 134 | break; |
| 135 | case KEY_UP: |
| 136 | case 'k': |
| 137 | if (txt_y > base_y) |
| 138 | txt_y--; |
| 139 | else |
| 140 | beep(); |
| 141 | break; |
| 142 | case KEY_LEFT: |
| 143 | case 'h': |
| 144 | if (txt_x > 0) |
| 145 | txt_x--; |
| 146 | else |
| 147 | beep(); |
| 148 | break; |
| 149 | case KEY_RIGHT: |
| 150 | case 'l': |
| 151 | if (txt_x < getmaxx(txtwin) - 1) |
| 152 | txt_x++; |
| 153 | else |
| 154 | beep(); |
| 155 | break; |
| 156 | case 'w': |
| 157 | test_inchs(level + 1, argv, chrwin, strwin); |
| 158 | if (txtbox != 0) { |
| 159 | touchwin(txtbox); |
| 160 | wnoutrefresh(txtbox); |
| 161 | } else { |
| 162 | touchwin(txtwin); |
| 163 | wnoutrefresh(txtwin); |
| 164 | } |
| 165 | break; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 166 | case HELP_KEY_1: |
| 167 | popup_msg(txtwin, help); |
| 168 | break; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 169 | default: |
| 170 | beep(); |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | MvWPrintw(chrwin, 0, 0, "char:"); |
| 175 | wclrtoeol(chrwin); |
| 176 | |
| 177 | if (txtwin != stdscr) { |
| 178 | wmove(txtwin, txt_y, txt_x); |
| 179 | if (win_wch(txtwin, &ch) != ERR) { |
| 180 | if (wadd_wch(chrwin, &ch) != ERR) { |
| 181 | for (j = txt_x + 1; j < getmaxx(txtwin); ++j) { |
| 182 | if (mvwin_wch(txtwin, txt_y, j, &ch) != ERR) { |
| 183 | if (wadd_wch(chrwin, &ch) == ERR) { |
| 184 | break; |
| 185 | } |
| 186 | } else { |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } else { |
| 193 | move(txt_y, txt_x); |
| 194 | if (in_wch(&ch) != ERR) { |
| 195 | if (wadd_wch(chrwin, &ch) != ERR) { |
| 196 | for (j = txt_x + 1; j < getmaxx(txtwin); ++j) { |
| 197 | if (mvin_wch(txt_y, j, &ch) != ERR) { |
| 198 | if (wadd_wch(chrwin, &ch) == ERR) { |
| 199 | break; |
| 200 | } |
| 201 | } else { |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | wnoutrefresh(chrwin); |
| 209 | |
| 210 | MvWPrintw(strwin, 0, 0, "text:"); |
| 211 | wclrtobot(strwin); |
| 212 | |
| 213 | limit = getmaxx(strwin) - 5; |
| 214 | |
| 215 | if (txtwin != stdscr) { |
| 216 | wmove(txtwin, txt_y, txt_x); |
| 217 | if (win_wchstr(txtwin, text) != ERR) { |
| 218 | (void) mvwadd_wchstr(strwin, 0, 5, text); |
| 219 | } |
| 220 | |
| 221 | wmove(txtwin, txt_y, txt_x); |
| 222 | if (win_wchnstr(txtwin, text, limit) != ERR) { |
| 223 | (void) mvwadd_wchstr(strwin, 1, 5, text); |
| 224 | } |
| 225 | |
| 226 | if (mvwin_wchstr(txtwin, txt_y, txt_x, text) != ERR) { |
| 227 | (void) mvwadd_wchstr(strwin, 2, 5, text); |
| 228 | } |
| 229 | |
| 230 | if (mvwin_wchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) { |
| 231 | (void) mvwadd_wchstr(strwin, 3, 5, text); |
| 232 | } |
| 233 | } else { |
| 234 | move(txt_y, txt_x); |
| 235 | if (in_wchstr(text) != ERR) { |
| 236 | (void) mvwadd_wchstr(strwin, 0, 5, text); |
| 237 | } |
| 238 | |
| 239 | move(txt_y, txt_x); |
| 240 | if (in_wchnstr(text, limit) != ERR) { |
| 241 | (void) mvwadd_wchstr(strwin, 1, 5, text); |
| 242 | } |
| 243 | |
| 244 | if (mvin_wchstr(txt_y, txt_x, text) != ERR) { |
| 245 | (void) mvwadd_wchstr(strwin, 2, 5, text); |
| 246 | } |
| 247 | |
| 248 | if (mvin_wchnstr(txt_y, txt_x, text, limit) != ERR) { |
| 249 | (void) mvwadd_wchstr(strwin, 3, 5, text); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | wnoutrefresh(strwin); |
| 254 | } |
| 255 | if (level > 1) { |
| 256 | delwin(txtwin); |
| 257 | delwin(txtbox); |
| 258 | } |
| 259 | return TRUE; |
| 260 | } |
| 261 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 262 | static void |
| 263 | usage(int ok) |
| 264 | { |
| 265 | static const char *msg[] = |
| 266 | { |
| 267 | "Usage: inch_wide [options] [file1 [...]]" |
| 268 | ,"" |
| 269 | ,USAGE_COMMON |
| 270 | }; |
| 271 | size_t n; |
| 272 | |
| 273 | for (n = 0; n < SIZEOF(msg); n++) |
| 274 | fprintf(stderr, "%s\n", msg[n]); |
| 275 | |
| 276 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 277 | } |
| 278 | /* *INDENT-OFF* */ |
| 279 | VERSION_COMMON() |
| 280 | /* *INDENT-ON* */ |
| 281 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 282 | int |
| 283 | main(int argc, char *argv[]) |
| 284 | { |
| 285 | WINDOW *chrbox; |
| 286 | WINDOW *chrwin; |
| 287 | WINDOW *strwin; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 288 | int ch; |
| 289 | |
| 290 | while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) { |
| 291 | switch (ch) { |
| 292 | case OPTS_VERSION: |
| 293 | show_version(argv); |
| 294 | ExitProgram(EXIT_SUCCESS); |
| 295 | default: |
| 296 | usage(ch == OPTS_USAGE); |
| 297 | /* NOTREACHED */ |
| 298 | } |
| 299 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 300 | |
| 301 | setlocale(LC_ALL, ""); |
| 302 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 303 | if (optind + 1 > argc) |
| 304 | usage(FALSE); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 305 | |
| 306 | initscr(); |
| 307 | |
| 308 | chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0); |
| 309 | box(chrbox, 0, 0); |
| 310 | wnoutrefresh(chrbox); |
| 311 | |
| 312 | chrwin = derwin(chrbox, 1, COLS - 2, 1, 1); |
| 313 | strwin = derwin(chrbox, 4, COLS - 2, 2, 1); |
| 314 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 315 | test_inchs(optind, argv, chrwin, strwin); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 316 | |
| 317 | endwin(); |
| 318 | ExitProgram(EXIT_SUCCESS); |
| 319 | } |
| 320 | #else |
| 321 | int |
| 322 | main(void) |
| 323 | { |
| 324 | printf("This program requires the wide-ncurses library\n"); |
| 325 | ExitProgram(EXIT_FAILURE); |
| 326 | } |
| 327 | #endif |