Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (c) 2006-2010,2012 Free Software Foundation, Inc. * |
| 3 | * * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a * |
| 5 | * copy of this software and associated documentation files (the * |
| 6 | * "Software"), to deal in the Software without restriction, including * |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, * |
| 8 | * distribute, distribute with modifications, sublicense, and/or sell * |
| 9 | * copies of the Software, and to permit persons to whom the Software is * |
| 10 | * furnished to do so, subject to the following conditions: * |
| 11 | * * |
| 12 | * The above copyright notice and this permission notice shall be included * |
| 13 | * in all copies or substantial portions of the Software. * |
| 14 | * * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
| 16 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
| 18 | * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
| 19 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
| 20 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
| 21 | * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
| 22 | * * |
| 23 | * Except as contained in this notice, the name(s) of the above copyright * |
| 24 | * holders shall not be used in advertising or otherwise to promote the * |
| 25 | * sale, use or other dealings in this Software without prior written * |
| 26 | * authorization. * |
| 27 | ****************************************************************************/ |
| 28 | /* |
| 29 | * $Id: chgat.c,v 1.12 2012/11/18 01:55:35 tom Exp $ |
| 30 | * |
| 31 | * test-driver for chgat/wchgat/mvchgat/mvwchgat |
| 32 | */ |
| 33 | |
| 34 | #include <test.priv.h> |
| 35 | |
| 36 | #if HAVE_CHGAT |
| 37 | |
| 38 | #define SHOW(n) ((n) == ERR ? "ERR" : "OK") |
| 39 | #define COLOR_DEFAULT (-1) |
| 40 | |
| 41 | #if defined(NCURSES_VERSION_PATCH) && NCURSES_VERSION_PATCH < 20060715 |
| 42 | #define touch_if_needed(win, row) touchline(win, row, 1) |
| 43 | #else |
| 44 | #define touch_if_needed(win, row) /* nothing */ |
| 45 | #endif |
| 46 | |
| 47 | typedef struct { |
| 48 | unsigned c; |
| 49 | unsigned v; |
| 50 | short pair; |
| 51 | unsigned attr; |
| 52 | int count; |
| 53 | int ch; |
| 54 | const char *c_msg; |
| 55 | const char *v_msg; |
| 56 | int y_val; |
| 57 | int x_val; |
| 58 | int y_beg, x_beg; |
| 59 | int y_max, x_max; |
| 60 | } STATUS; |
| 61 | |
| 62 | static const char * |
| 63 | color_params(unsigned state, short *pair) |
| 64 | { |
| 65 | /* *INDENT-OFF* */ |
| 66 | static struct { |
| 67 | short pair; |
| 68 | short fg, bg; |
| 69 | const char *msg; |
| 70 | } table[] = { |
| 71 | { 0, COLOR_DEFAULT, COLOR_DEFAULT, "default" }, |
| 72 | { 1, COLOR_RED, COLOR_BLACK, "red/black" }, |
| 73 | { 2, COLOR_WHITE, COLOR_BLUE, "white/blue" }, |
| 74 | }; |
| 75 | /* *INDENT-ON* */ |
| 76 | |
| 77 | static bool first = TRUE; |
| 78 | const char *result = 0; |
| 79 | |
| 80 | if (has_colors()) { |
| 81 | if (first) { |
| 82 | unsigned n; |
| 83 | |
| 84 | start_color(); |
| 85 | for (n = 0; n < SIZEOF(table); ++n) { |
| 86 | init_pair(table[n].pair, table[n].fg, table[n].bg); |
| 87 | } |
| 88 | } |
| 89 | if (state < SIZEOF(table)) { |
| 90 | *pair = table[state].pair; |
| 91 | result = table[state].msg; |
| 92 | } |
| 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | static const char * |
| 98 | video_params(unsigned state, unsigned *attr) |
| 99 | { |
| 100 | /* *INDENT-OFF* */ |
| 101 | static struct { |
| 102 | unsigned attr; |
| 103 | const char *msg; |
| 104 | } table[] = { |
| 105 | { A_NORMAL, "normal" }, |
| 106 | { A_BOLD, "bold" }, |
| 107 | { A_REVERSE, "reverse" }, |
| 108 | { A_UNDERLINE, "underline" }, |
| 109 | { A_BLINK, "blink" }, |
| 110 | }; |
| 111 | /* *INDENT-ON* */ |
| 112 | |
| 113 | const char *result = 0; |
| 114 | |
| 115 | if (state < SIZEOF(table)) { |
| 116 | *attr = table[state].attr; |
| 117 | result = table[state].msg; |
| 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | /* fill the window with a test-pattern */ |
| 123 | static void |
| 124 | fill_window(WINDOW *win) |
| 125 | { |
| 126 | int y, x; |
| 127 | int y0 = -1, x0 = -1; |
| 128 | |
| 129 | getyx(win, y, x); |
| 130 | wmove(win, 0, 0); |
| 131 | while (waddstr(win, "0123456789 abcdefghijklmnopqrstuvwxyz ") != ERR) { |
| 132 | int y1, x1; |
| 133 | getyx(win, y1, x1); |
| 134 | if (y1 == y0 && x1 == x0) |
| 135 | break; |
| 136 | x0 = x1; |
| 137 | y0 = y1; |
| 138 | } |
| 139 | wmove(win, y, x); |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | show_status(WINDOW *win, STATUS * sp) |
| 144 | { |
| 145 | int y, x; |
| 146 | |
| 147 | getyx(win, y, x); |
| 148 | wmove(win, 0, 0); |
| 149 | wprintw(win, "Count %d", sp->count); |
| 150 | if (sp->v_msg != 0) |
| 151 | wprintw(win, " Video %s", sp->v_msg); |
| 152 | if (sp->c_msg != 0) |
| 153 | wprintw(win, " Color %s", sp->c_msg); |
| 154 | wclrtoeol(win); |
| 155 | wmove(win, y, x); |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | do_subwindow(WINDOW *win, STATUS * sp, void func(WINDOW *)) |
| 160 | { |
| 161 | WINDOW *win1 = newwin(sp->y_max - 2, sp->x_max - 2, |
| 162 | sp->y_beg + 1, sp->x_beg + 1); |
| 163 | |
| 164 | if (win1 != 0 && sp->y_max > 4 && sp->x_max > 4) { |
| 165 | WINDOW *win2 = derwin(win1, sp->y_max - 4, sp->x_max - 4, 1, 1); |
| 166 | |
| 167 | if (win2 != 0) { |
| 168 | box(win1, 0, 0); |
| 169 | wrefresh(win1); |
| 170 | func(win2); |
| 171 | |
| 172 | delwin(win2); |
| 173 | } else { |
| 174 | beep(); |
| 175 | } |
| 176 | delwin(win1); |
| 177 | touchwin(win); |
| 178 | } else { |
| 179 | if (win1 != 0) |
| 180 | delwin(win1); |
| 181 | beep(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static void |
| 186 | init_status(WINDOW *win, STATUS * sp) |
| 187 | { |
| 188 | memset(sp, 0, sizeof(*sp)); |
| 189 | sp->c = 99; |
| 190 | sp->v = 99; |
| 191 | sp->ch = ' '; |
| 192 | |
| 193 | keypad(win, TRUE); |
| 194 | fill_window(win); |
| 195 | |
| 196 | getbegyx(win, sp->y_beg, sp->x_beg); |
| 197 | getmaxyx(win, sp->y_max, sp->x_max); |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | show_help(WINDOW *win) |
| 202 | { |
| 203 | static const char *table[] = |
| 204 | { |
| 205 | "Basic commands:" |
| 206 | ,"Use h/j/k/l or arrow keys to move the cursor." |
| 207 | ,"Set the count parameter for chgat by entering digits 0-9." |
| 208 | ,"" |
| 209 | ,"Other commands:" |
| 210 | ,"space toggles through the set of video attributes and colors." |
| 211 | ,"t touches (forces repaint) of the current line." |
| 212 | ,". calls *chgat at the current position with the given count." |
| 213 | ,", calls *chgat at the window beginning with the given count." |
| 214 | ,"= resets count to zero." |
| 215 | ,"- negates count." |
| 216 | ,"? shows this help-window" |
| 217 | ,"" |
| 218 | ,"" |
| 219 | }; |
| 220 | |
| 221 | int y_max, x_max; |
| 222 | int row; |
| 223 | |
| 224 | getmaxyx(win, y_max, x_max); |
| 225 | for (row = 0; row < (int) SIZEOF(table) && row < y_max; ++row) { |
| 226 | MvWPrintw(win, row, 0, "%.*s", x_max, table[row]); |
| 227 | } |
| 228 | while (wgetch(win) != 'q') |
| 229 | beep(); |
| 230 | } |
| 231 | |
| 232 | static void |
| 233 | update_status(WINDOW *win, STATUS * sp) |
| 234 | { |
| 235 | switch (sp->ch) { |
| 236 | case ' ': /* next test-iteration */ |
| 237 | if (has_colors()) { |
| 238 | if ((sp->c_msg = color_params(++(sp->c), &(sp->pair))) == 0) { |
| 239 | sp->c_msg = color_params(sp->c = 0, &(sp->pair)); |
| 240 | if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) { |
| 241 | sp->v_msg = video_params(sp->v = 0, &(sp->attr)); |
| 242 | } |
| 243 | } |
| 244 | } else { |
| 245 | if ((sp->v_msg = video_params(++(sp->v), &(sp->attr))) == 0) { |
| 246 | sp->v_msg = video_params(sp->v = 0, &(sp->attr)); |
| 247 | } |
| 248 | } |
| 249 | sp->count = 0; |
| 250 | show_status(win, sp); |
| 251 | break; |
| 252 | case KEY_LEFT: |
| 253 | case 'h': |
| 254 | if (sp->x_val > 0) |
| 255 | wmove(win, sp->y_val, --(sp->x_val)); |
| 256 | break; |
| 257 | case KEY_DOWN: |
| 258 | case 'j': |
| 259 | if (sp->y_val < sp->y_max) |
| 260 | wmove(win, ++(sp->y_val), sp->x_val); |
| 261 | break; |
| 262 | case KEY_UP: |
| 263 | case 'k': |
| 264 | if (sp->y_val > 0) |
| 265 | wmove(win, --(sp->y_val), sp->x_val); |
| 266 | break; |
| 267 | case KEY_RIGHT: |
| 268 | case 'l': |
| 269 | if (sp->x_val < sp->x_max) |
| 270 | wmove(win, sp->y_val, ++(sp->x_val)); |
| 271 | break; |
| 272 | case 't': |
| 273 | touchline(win, sp->y_val, 1); |
| 274 | break; |
| 275 | case '=': |
| 276 | sp->count = 0; |
| 277 | show_status(win, sp); |
| 278 | break; |
| 279 | case '-': |
| 280 | sp->count = -(sp->count); |
| 281 | show_status(win, sp); |
| 282 | break; |
| 283 | case '?': |
| 284 | do_subwindow(win, sp, show_help); |
| 285 | break; |
| 286 | default: |
| 287 | if (isdigit(sp->ch)) { |
| 288 | sp->count = (sp->count * 10) + (sp->ch - '0'); |
| 289 | show_status(win, sp); |
| 290 | } else { |
| 291 | beep(); |
| 292 | } |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static void |
| 298 | test_wchgat(WINDOW *win) |
| 299 | { |
| 300 | STATUS st; |
| 301 | |
| 302 | init_status(win, &st); |
| 303 | |
| 304 | do { |
| 305 | switch (st.ch) { |
| 306 | case '.': /* change from current position */ |
| 307 | wchgat(win, st.count, st.attr, st.pair, (void *) 0); |
| 308 | touch_if_needed(win, st.y_val); |
| 309 | break; |
| 310 | case ',': /* change from beginning of window */ |
| 311 | mvwchgat(win, 0, 0, st.count, st.attr, st.pair, (void *) 0); |
| 312 | touch_if_needed(win, 0); |
| 313 | wmove(win, st.y_val, st.x_val); |
| 314 | break; |
| 315 | case 'w': |
| 316 | do_subwindow(win, &st, test_wchgat); |
| 317 | break; |
| 318 | case 'q': |
| 319 | return; |
| 320 | default: |
| 321 | update_status(win, &st); |
| 322 | break; |
| 323 | } |
| 324 | } while ((st.ch = wgetch(win)) != ERR); |
| 325 | } |
| 326 | |
| 327 | static void |
| 328 | test_chgat(void) |
| 329 | { |
| 330 | STATUS st; |
| 331 | |
| 332 | init_status(stdscr, &st); |
| 333 | |
| 334 | do { |
| 335 | switch (st.ch) { |
| 336 | case '.': /* change from current position */ |
| 337 | chgat(st.count, st.attr, st.pair, (void *) 0); |
| 338 | touch_if_needed(stdscr, st.y_val); |
| 339 | break; |
| 340 | case ',': /* change from beginning of window */ |
| 341 | mvchgat(0, 0, st.count, st.attr, st.pair, (void *) 0); |
| 342 | touch_if_needed(stdscr, 0); |
| 343 | move(st.y_val, st.x_val); |
| 344 | break; |
| 345 | case 'w': |
| 346 | do_subwindow(stdscr, &st, test_wchgat); |
| 347 | break; |
| 348 | case 'q': |
| 349 | return; |
| 350 | default: |
| 351 | update_status(stdscr, &st); |
| 352 | break; |
| 353 | } |
| 354 | } while ((st.ch = getch()) != ERR); |
| 355 | } |
| 356 | |
| 357 | int |
| 358 | main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) |
| 359 | { |
| 360 | initscr(); |
| 361 | cbreak(); |
| 362 | noecho(); |
| 363 | |
| 364 | test_chgat(); |
| 365 | endwin(); |
| 366 | |
| 367 | ExitProgram(EXIT_SUCCESS); |
| 368 | } |
| 369 | |
| 370 | #else |
| 371 | int |
| 372 | main(void) |
| 373 | { |
| 374 | printf("This program requires the curses chgat function\n"); |
| 375 | ExitProgram(EXIT_FAILURE); |
| 376 | } |
| 377 | #endif |