Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (c) 2007-2010,2014 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: demo_panels.c,v 1.38 2014/08/02 17:24:07 tom Exp $ |
| 30 | * |
| 31 | * Demonstrate a variety of functions from the panel library. |
| 32 | */ |
| 33 | |
| 34 | #include <test.priv.h> |
| 35 | |
| 36 | #if USE_LIBPANEL |
| 37 | |
| 38 | #include <panel.h> |
| 39 | |
| 40 | #define LAST_POS '@' |
| 41 | #define TEMP_POS '>' |
| 42 | |
| 43 | typedef void (*InitPanel) (void); |
| 44 | typedef void (*FillPanel) (PANEL *); |
| 45 | |
| 46 | static bool use_colors = FALSE; |
| 47 | static bool unboxed = FALSE; |
| 48 | static FILE *log_in; |
| 49 | static FILE *log_out; |
| 50 | |
| 51 | static void |
| 52 | close_input(void) |
| 53 | { |
| 54 | if (log_in != 0) { |
| 55 | fclose(log_in); |
| 56 | log_in = 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void |
| 61 | close_output(void) |
| 62 | { |
| 63 | if (log_out != 0) { |
| 64 | fclose(log_out); |
| 65 | log_out = 0; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static WINDOW * |
| 70 | statusline(void) |
| 71 | { |
| 72 | WINDOW *result = stdscr; |
| 73 | |
| 74 | wmove(result, LINES - 1, 0); |
| 75 | wclrtoeol(result); |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | pflush(void) |
| 81 | { |
| 82 | update_panels(); |
| 83 | doupdate(); |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | saywhat(NCURSES_CONST char *text) |
| 88 | { |
| 89 | WINDOW *win = statusline(); |
| 90 | if (text != 0 && *text != '\0') { |
| 91 | waddstr(win, text); |
| 92 | waddstr(win, "; "); |
| 93 | } |
| 94 | waddstr(win, "press any key to continue"); |
| 95 | } |
| 96 | |
| 97 | static void |
| 98 | show_position(NCURSES_CONST char *text, |
| 99 | NCURSES_CONST char *also, |
| 100 | int which, |
| 101 | int ypos, |
| 102 | int xpos) |
| 103 | { |
| 104 | WINDOW *win = statusline(); |
| 105 | |
| 106 | wprintw(win, "%s for panel %d now %d,%d%s", text, which, ypos, xpos, also); |
| 107 | wmove(stdscr, ypos, xpos); |
| 108 | } |
| 109 | |
| 110 | static int |
| 111 | get_position(NCURSES_CONST char *text, |
| 112 | NCURSES_CONST char *also, |
| 113 | int which, |
| 114 | int *xpos, |
| 115 | int *ypos) |
| 116 | { |
| 117 | int result = 0; |
| 118 | int x1, y1; |
| 119 | char cmd; |
| 120 | |
| 121 | getyx(stdscr, y1, x1); |
| 122 | (void) statusline(); |
| 123 | |
| 124 | show_position(text, also, which, y1, x1); |
| 125 | |
| 126 | if (log_in != 0) { |
| 127 | if (fscanf(log_in, "%c%d,%d\n", &cmd, &y1, &x1) == 3) { |
| 128 | switch (cmd) { |
| 129 | case LAST_POS: |
| 130 | result = 1; |
| 131 | (void) wgetch(stdscr); |
| 132 | break; |
| 133 | case TEMP_POS: |
| 134 | result = 0; |
| 135 | wrefresh(stdscr); |
| 136 | napms(100); |
| 137 | break; |
| 138 | default: |
| 139 | result = -1; |
| 140 | break; |
| 141 | } |
| 142 | } else { |
| 143 | result = -1; |
| 144 | } |
| 145 | } else { |
| 146 | |
| 147 | switch (wgetch(stdscr)) { |
| 148 | case QUIT: |
| 149 | case ESCAPE: |
| 150 | case ERR: |
| 151 | result = -1; |
| 152 | break; |
| 153 | case ' ': |
| 154 | result = 1; |
| 155 | break; |
| 156 | case KEY_UP: |
| 157 | if (y1 > 0) { |
| 158 | --y1; |
| 159 | } else { |
| 160 | beep(); |
| 161 | } |
| 162 | break; |
| 163 | case KEY_DOWN: |
| 164 | if (y1 < getmaxy(stdscr)) { |
| 165 | ++y1; |
| 166 | } else { |
| 167 | beep(); |
| 168 | } |
| 169 | break; |
| 170 | case KEY_LEFT: |
| 171 | if (x1 > 0) { |
| 172 | --x1; |
| 173 | } else { |
| 174 | beep(); |
| 175 | } |
| 176 | break; |
| 177 | case KEY_RIGHT: |
| 178 | if (x1 < getmaxx(stdscr)) { |
| 179 | ++x1; |
| 180 | } else { |
| 181 | beep(); |
| 182 | } |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | wmove(stdscr, y1, x1); |
| 188 | *ypos = y1; |
| 189 | *xpos = x1; |
| 190 | |
| 191 | if (result >= 0) { |
| 192 | if (log_out) |
| 193 | fprintf(log_out, "%c%d,%d\n", |
| 194 | ((result > 0) |
| 195 | ? LAST_POS |
| 196 | : TEMP_POS), |
| 197 | y1, x1); |
| 198 | } |
| 199 | return result; |
| 200 | } |
| 201 | |
| 202 | static PANEL * |
| 203 | mkpanel(short color, int rows, int cols, int tly, int tlx) |
| 204 | { |
| 205 | WINDOW *win; |
| 206 | PANEL *pan = 0; |
| 207 | char *userdata = typeMalloc(char, 3); |
| 208 | |
| 209 | if ((win = newwin(rows, cols, tly, tlx)) != 0) { |
| 210 | keypad(win, TRUE); |
| 211 | if ((pan = new_panel(win)) == 0) { |
| 212 | delwin(win); |
| 213 | } else if (use_colors) { |
| 214 | short fg = (short) ((color == COLOR_BLUE) |
| 215 | ? COLOR_WHITE |
| 216 | : COLOR_BLACK); |
| 217 | short bg = color; |
| 218 | |
| 219 | init_pair(color, fg, bg); |
| 220 | wbkgdset(win, (chtype) (COLOR_PAIR(color) | ' ')); |
| 221 | } else if (!unboxed) { |
| 222 | wbkgdset(win, A_BOLD | ' '); |
| 223 | } |
| 224 | } |
| 225 | sprintf(userdata, "p%d", color % 8); |
| 226 | set_panel_userptr(pan, (NCURSES_CONST void *) userdata); |
| 227 | return pan; |
| 228 | } |
| 229 | |
| 230 | static void |
| 231 | my_remove_panel(PANEL ** pans, int which) |
| 232 | { |
| 233 | if (pans[which] != 0) { |
| 234 | PANEL *pan = pans[which]; |
| 235 | WINDOW *win = panel_window(pan); |
| 236 | char *user = (char *) panel_userptr(pan); |
| 237 | |
| 238 | free(user); |
| 239 | del_panel(pan); |
| 240 | delwin(win); |
| 241 | |
| 242 | pans[which] = 0; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | #undef MIN |
| 247 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| 248 | #define ABS(a) ((a) < 0 ? -(a) : (a)) |
| 249 | |
| 250 | static void |
| 251 | my_create_panel(PANEL ** pans, int which, FillPanel myFill) |
| 252 | { |
| 253 | PANEL *pan = 0; |
| 254 | int code; |
| 255 | short pair = (short) which; |
| 256 | short fg = (short) ((pair == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK); |
| 257 | short bg = pair; |
| 258 | int x0, y0, x1, y1; |
| 259 | |
| 260 | init_pair(pair, fg, bg); |
| 261 | |
| 262 | /* remove the old panel, if any */ |
| 263 | my_remove_panel(pans, which); |
| 264 | |
| 265 | /* get the position of one corner */ |
| 266 | wmove(stdscr, getmaxy(stdscr) / 2, getmaxx(stdscr) / 2); |
| 267 | getyx(stdscr, y0, x0); |
| 268 | while ((code = get_position("First corner", "", which, &x0, &y0)) == 0) { |
| 269 | ; |
| 270 | } |
| 271 | |
| 272 | if (code > 0) { |
| 273 | char also[80]; |
| 274 | sprintf(also, " (first %d,%d)", y0, x0); |
| 275 | /* get the position of the opposite corner */ |
| 276 | while ((code = get_position("Opposite corner", |
| 277 | also, which, &x1, &y1)) == 0) { |
| 278 | ; |
| 279 | } |
| 280 | |
| 281 | if (code > 0) { |
| 282 | int tly = MIN(y0, y1); |
| 283 | int tlx = MIN(x0, x1); |
| 284 | pan = mkpanel(pair, ABS(y1 - y0) + 1, ABS(x1 - x0) + 1, tly, tlx); |
| 285 | /* finish */ |
| 286 | myFill(pan); |
| 287 | pans[which] = pan; |
| 288 | pflush(); |
| 289 | wmove(stdscr, y1, x1); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void |
| 295 | my_move_panel(PANEL ** pans, int which, bool continuous) |
| 296 | { |
| 297 | if (pans[which] != 0) { |
| 298 | int code; |
| 299 | int y0, x0; |
| 300 | int y1, x1; |
| 301 | WINDOW *win = panel_window(pans[which]); |
| 302 | char also[80]; |
| 303 | |
| 304 | getbegyx(win, y0, x0); |
| 305 | sprintf(also, " (start %d,%d)", y0, x0); |
| 306 | wmove(stdscr, y0, x0); |
| 307 | while ((code = get_position("Move panel", also, which, &x1, &y1)) == 0) { |
| 308 | if (continuous) { |
| 309 | move_panel(pans[which], y1, x1); |
| 310 | pflush(); |
| 311 | } |
| 312 | } |
| 313 | if (code > 0) { |
| 314 | move_panel(pans[which], y1, x1); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | static void |
| 320 | my_resize_panel(PANEL ** pans, int which, FillPanel myFill) |
| 321 | { |
| 322 | if (pans[which] != 0) { |
| 323 | int code; |
| 324 | int y0, x0; |
| 325 | int y1, x1; |
| 326 | WINDOW *win = panel_window(pans[which]); |
| 327 | char also[80]; |
| 328 | |
| 329 | getbegyx(win, y0, x0); |
| 330 | sprintf(also, " (start %d,%d)", y0, x0); |
| 331 | wmove(stdscr, y0, x0); |
| 332 | while ((code = get_position("Resize panel", |
| 333 | also, which, &x1, &y1)) == 0) { |
| 334 | ; |
| 335 | } |
| 336 | if (code > 0) { |
| 337 | WINDOW *next = newwin(ABS(y1 - y0) + 1, |
| 338 | ABS(x1 - x0) + 1, |
| 339 | MIN(y0, y1), |
| 340 | MIN(x0, x1)); |
| 341 | if (next != 0) { |
| 342 | keypad(next, TRUE); |
| 343 | if (use_colors) { |
| 344 | wbkgdset(next, (chtype) (COLOR_PAIR(which) | ' ')); |
| 345 | } else if (!unboxed) { |
| 346 | wbkgdset(next, A_BOLD | ' '); |
| 347 | } |
| 348 | replace_panel(pans[which], next); |
| 349 | myFill(pans[which]); |
| 350 | delwin(win); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static void |
| 357 | init_panel(void) |
| 358 | { |
| 359 | register int y, x; |
| 360 | |
| 361 | for (y = 0; y < LINES - 1; y++) { |
| 362 | for (x = 0; x < COLS; x++) |
| 363 | wprintw(stdscr, "%d", (y + x) % 10); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | static void |
| 368 | fill_panel(PANEL * pan) |
| 369 | { |
| 370 | WINDOW *win = panel_window(pan); |
| 371 | const char *userptr = (const char *) panel_userptr(pan); |
| 372 | int num = (userptr && *userptr) ? userptr[1] : '?'; |
| 373 | int y, x; |
| 374 | |
| 375 | wmove(win, 1, 1); |
| 376 | wprintw(win, "-pan%c-", num); |
| 377 | wclrtoeol(win); |
| 378 | box(win, 0, 0); |
| 379 | for (y = 2; y < getmaxy(win) - 1; y++) { |
| 380 | for (x = 1; x < getmaxx(win) - 1; x++) { |
| 381 | wmove(win, y, x); |
| 382 | waddch(win, UChar(num)); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static void |
| 388 | fill_unboxed(PANEL * pan) |
| 389 | { |
| 390 | WINDOW *win = panel_window(pan); |
| 391 | const char *userptr = (const char *) panel_userptr(pan); |
| 392 | int num = (userptr && *userptr) ? userptr[1] : '?'; |
| 393 | int y, x; |
| 394 | |
| 395 | for (y = 0; y < getmaxy(win); y++) { |
| 396 | for (x = 0; x < getmaxx(win); x++) { |
| 397 | wmove(win, y, x); |
| 398 | waddch(win, UChar(num)); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | #if USE_WIDEC_SUPPORT |
| 404 | static void |
| 405 | make_fullwidth_digit(cchar_t *target, int digit) |
| 406 | { |
| 407 | wchar_t source[2]; |
| 408 | |
| 409 | source[0] = (wchar_t) (digit + 0xff10); |
| 410 | source[1] = 0; |
| 411 | setcchar(target, source, A_NORMAL, 0, 0); |
| 412 | } |
| 413 | |
| 414 | static void |
| 415 | init_wide_panel(void) |
| 416 | { |
| 417 | int digit; |
| 418 | cchar_t temp[10]; |
| 419 | |
| 420 | for (digit = 0; digit < 10; ++digit) |
| 421 | make_fullwidth_digit(&temp[digit], digit); |
| 422 | |
| 423 | do { |
| 424 | int y, x; |
| 425 | getyx(stdscr, y, x); |
| 426 | digit = (y + x / 2) % 10; |
| 427 | } while (add_wch(&temp[digit]) != ERR); |
| 428 | } |
| 429 | |
| 430 | static void |
| 431 | fill_wide_panel(PANEL * pan) |
| 432 | { |
| 433 | WINDOW *win = panel_window(pan); |
| 434 | int num = ((const char *) panel_userptr(pan))[1]; |
| 435 | int y, x; |
| 436 | |
| 437 | wmove(win, 1, 1); |
| 438 | wprintw(win, "-pan%c-", num); |
| 439 | wclrtoeol(win); |
| 440 | box(win, 0, 0); |
| 441 | for (y = 2; y < getmaxy(win) - 1; y++) { |
| 442 | for (x = 1; x < getmaxx(win) - 1; x++) { |
| 443 | wmove(win, y, x); |
| 444 | waddch(win, UChar(num)); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | #endif |
| 449 | |
| 450 | #define MAX_PANELS 5 |
| 451 | |
| 452 | static int |
| 453 | which_panel(PANEL * px[MAX_PANELS + 1], PANEL * pan) |
| 454 | { |
| 455 | int result = 0; |
| 456 | int j; |
| 457 | |
| 458 | for (j = 1; j <= MAX_PANELS; ++j) { |
| 459 | if (px[j] == pan) { |
| 460 | result = j; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | return result; |
| 465 | } |
| 466 | |
| 467 | static void |
| 468 | show_panels(PANEL * px[MAX_PANELS + 1]) |
| 469 | { |
| 470 | static const char *help[] = |
| 471 | { |
| 472 | "", |
| 473 | "Commands are letter/digit pairs. Digits are the panel number.", |
| 474 | "", |
| 475 | " b - put the panel on the bottom of the stack", |
| 476 | " c - create the panel", |
| 477 | " d - delete the panel", |
| 478 | " h - hide the panel", |
| 479 | " m - move the panel (M for continuous move)", |
| 480 | " r - resize the panel", |
| 481 | " s - show the panel", |
| 482 | " b - put the panel on the top of the stack" |
| 483 | }; |
| 484 | |
| 485 | struct { |
| 486 | bool valid; |
| 487 | bool hidden; |
| 488 | PANEL *above; |
| 489 | PANEL *below; |
| 490 | } table[MAX_PANELS + 1]; |
| 491 | |
| 492 | WINDOW *win; |
| 493 | PANEL *pan; |
| 494 | int j; |
| 495 | |
| 496 | memset(table, 0, sizeof(table)); |
| 497 | for (j = 1; j <= MAX_PANELS; ++j) { |
| 498 | table[j].valid = (px[j] != 0); |
| 499 | if (table[j].valid) { |
| 500 | table[j].hidden = panel_hidden(px[j]); |
| 501 | table[j].above = panel_above(px[j]); |
| 502 | table[j].below = panel_below(px[j]); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | if ((win = newwin(LINES - 1, COLS, 0, 0)) != 0) { |
| 507 | keypad(win, TRUE); |
| 508 | if ((pan = new_panel(win)) != 0) { |
| 509 | werase(win); |
| 510 | MvWPrintw(win, 0, 0, "Panels:\n"); |
| 511 | for (j = 1; j <= MAX_PANELS; ++j) { |
| 512 | if (table[j].valid) { |
| 513 | wprintw(win, " %d:", j); |
| 514 | if (table[j].hidden) { |
| 515 | waddstr(win, " hidden"); |
| 516 | } else { |
| 517 | if (table[j].above) { |
| 518 | wprintw(win, " above %d", |
| 519 | which_panel(px, table[j].above)); |
| 520 | } |
| 521 | if (table[j].below) { |
| 522 | wprintw(win, "%s below %d", |
| 523 | table[j].above ? "," : "", |
| 524 | which_panel(px, table[j].below)); |
| 525 | } |
| 526 | } |
| 527 | waddch(win, '\n'); |
| 528 | } |
| 529 | } |
| 530 | for (j = 0; j < (int) SIZEOF(help); ++j) { |
| 531 | if (wprintw(win, "%s\n", help[j]) == ERR) |
| 532 | break; |
| 533 | } |
| 534 | wgetch(win); |
| 535 | del_panel(pan); |
| 536 | pflush(); |
| 537 | } |
| 538 | delwin(win); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | #define wrapper(func) \ |
| 543 | static int my_##func(PANEL *pan) \ |
| 544 | { \ |
| 545 | int code = ERR; \ |
| 546 | if (pan != 0) { \ |
| 547 | code = func(pan); \ |
| 548 | } \ |
| 549 | return code; \ |
| 550 | } |
| 551 | /* *INDENT-OFF* */ |
| 552 | wrapper(bottom_panel) |
| 553 | wrapper(hide_panel) |
| 554 | wrapper(show_panel) |
| 555 | wrapper(top_panel) |
| 556 | /* *INDENT-ON* */ |
| 557 | |
| 558 | static void |
| 559 | do_panel(PANEL * px[MAX_PANELS + 1], |
| 560 | NCURSES_CONST char *cmd, |
| 561 | FillPanel myFill) |
| 562 | { |
| 563 | int which = cmd[1] - '0'; |
| 564 | |
| 565 | if (which < 1 || which > MAX_PANELS) { |
| 566 | beep(); |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | if (log_in != 0) { |
| 571 | pflush(); |
| 572 | } |
| 573 | |
| 574 | saywhat(cmd); |
| 575 | switch (*cmd) { |
| 576 | case 'b': |
| 577 | my_bottom_panel(px[which]); |
| 578 | break; |
| 579 | case 'c': |
| 580 | my_create_panel(px, which, myFill); |
| 581 | break; |
| 582 | case 'd': |
| 583 | my_remove_panel(px, which); |
| 584 | break; |
| 585 | case 'h': |
| 586 | my_hide_panel(px[which]); |
| 587 | break; |
| 588 | case 'm': |
| 589 | my_move_panel(px, which, FALSE); |
| 590 | break; |
| 591 | case 'M': |
| 592 | my_move_panel(px, which, TRUE); |
| 593 | break; |
| 594 | case 'r': |
| 595 | my_resize_panel(px, which, myFill); |
| 596 | break; |
| 597 | case 's': |
| 598 | my_show_panel(px[which]); |
| 599 | break; |
| 600 | case 't': |
| 601 | my_top_panel(px[which]); |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | static bool |
| 607 | ok_letter(int ch) |
| 608 | { |
| 609 | return isalpha(UChar(ch)) && strchr("bcdhmMrst", ch) != 0; |
| 610 | } |
| 611 | |
| 612 | static bool |
| 613 | ok_digit(int ch) |
| 614 | { |
| 615 | return isdigit(UChar(ch)) && (ch >= '1') && (ch - '0' <= MAX_PANELS); |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * A command consists of one or more letter/digit pairs separated by a space. |
| 620 | * Digits are limited to 1..MAX_PANELS. |
| 621 | * |
| 622 | * End the command with a newline. Reject other characters. |
| 623 | */ |
| 624 | static bool |
| 625 | get_command(PANEL * px[MAX_PANELS + 1], char *buffer, int limit) |
| 626 | { |
| 627 | int length = 0; |
| 628 | int y0, x0; |
| 629 | int c0, ch; |
| 630 | WINDOW *win; |
| 631 | |
| 632 | getyx(stdscr, y0, x0); |
| 633 | win = statusline(); |
| 634 | waddstr(win, "Command:"); |
| 635 | buffer[length = 0] = '\0'; |
| 636 | |
| 637 | if (log_in != 0) { |
| 638 | if (fgets(buffer, limit - 3, log_in) != 0) { |
| 639 | length = (int) strlen(buffer); |
| 640 | while (length > 0 && isspace(UChar(buffer[length - 1]))) |
| 641 | buffer[--length] = '\0'; |
| 642 | waddstr(win, buffer); |
| 643 | } else { |
| 644 | close_input(); |
| 645 | } |
| 646 | (void) wgetch(win); |
| 647 | } else { |
| 648 | c0 = 0; |
| 649 | for (;;) { |
| 650 | ch = wgetch(win); |
| 651 | if (ch == ERR || ch == QUIT || ch == ESCAPE) { |
| 652 | buffer[0] = '\0'; |
| 653 | break; |
| 654 | } else if (ch == CTRL('L')) { |
| 655 | wrefresh(curscr); |
| 656 | } else if (ch == '\n' || ch == KEY_ENTER) { |
| 657 | break; |
| 658 | } else if (ch == '?') { |
| 659 | show_panels(px); |
| 660 | } else if (length + 3 < limit) { |
| 661 | if (ch >= KEY_MIN) { |
| 662 | beep(); |
| 663 | } else if (ok_letter(UChar(ch))) { |
| 664 | if (isalpha(UChar(c0))) { |
| 665 | beep(); |
| 666 | } else if (isdigit(UChar(c0))) { |
| 667 | wprintw(win, " %c", ch); |
| 668 | buffer[length++] = ' '; |
| 669 | buffer[length++] = (char) (c0 = ch); |
| 670 | } else { |
| 671 | wprintw(win, "%c", ch); |
| 672 | buffer[length++] = (char) (c0 = ch); |
| 673 | } |
| 674 | } else if (ok_digit(ch)) { |
| 675 | if (isalpha(UChar(c0))) { |
| 676 | wprintw(win, "%c", ch); |
| 677 | buffer[length++] = (char) (c0 = ch); |
| 678 | } else { |
| 679 | beep(); |
| 680 | } |
| 681 | } else if (ch == ' ') { |
| 682 | if (isdigit(UChar(c0))) { |
| 683 | wprintw(win, "%c", ch); |
| 684 | buffer[length++] = (char) (c0 = ch); |
| 685 | } else { |
| 686 | beep(); |
| 687 | } |
| 688 | } else { |
| 689 | beep(); |
| 690 | } |
| 691 | } else { |
| 692 | beep(); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | wmove(stdscr, y0, x0); |
| 698 | |
| 699 | buffer[length] = '\0'; |
| 700 | if (log_out && length) { |
| 701 | fprintf(log_out, "%s\n", buffer); |
| 702 | } |
| 703 | return (length != 0); |
| 704 | } |
| 705 | |
| 706 | static void |
| 707 | demo_panels(InitPanel myInit, FillPanel myFill) |
| 708 | { |
| 709 | int itmp; |
| 710 | PANEL *px[MAX_PANELS + 1]; |
| 711 | char buffer[BUFSIZ]; |
| 712 | |
| 713 | scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */ |
| 714 | refresh(); |
| 715 | |
| 716 | myInit(); |
| 717 | memset(px, 0, sizeof(px)); |
| 718 | |
| 719 | while (get_command(px, buffer, sizeof(buffer))) { |
| 720 | int limit = (int) strlen(buffer); |
| 721 | for (itmp = 0; itmp < limit; itmp += 3) { |
| 722 | do_panel(px, buffer + itmp, myFill); |
| 723 | } |
| 724 | pflush(); |
| 725 | } |
| 726 | #if NO_LEAKS |
| 727 | for (itmp = 1; itmp <= MAX_PANELS; ++itmp) { |
| 728 | my_remove_panel(px, itmp); |
| 729 | } |
| 730 | #endif |
| 731 | } |
| 732 | |
| 733 | static void |
| 734 | usage(void) |
| 735 | { |
| 736 | static const char *const tbl[] = |
| 737 | { |
| 738 | "Usage: demo_panels [options]" |
| 739 | ,"" |
| 740 | ,"Options:" |
| 741 | ," -i file read commands from file" |
| 742 | ," -o file record commands in file" |
| 743 | ," -m do not use colors" |
| 744 | #if USE_WIDEC_SUPPORT |
| 745 | ," -w use wide-characters in panels and background" |
| 746 | #endif |
| 747 | ," -x do not enclose panels in boxes" |
| 748 | }; |
| 749 | size_t n; |
| 750 | for (n = 0; n < SIZEOF(tbl); n++) |
| 751 | fprintf(stderr, "%s\n", tbl[n]); |
| 752 | ExitProgram(EXIT_FAILURE); |
| 753 | } |
| 754 | |
| 755 | int |
| 756 | main(int argc, char *argv[]) |
| 757 | { |
| 758 | int c; |
| 759 | bool monochrome = FALSE; |
| 760 | InitPanel myInit = init_panel; |
| 761 | FillPanel myFill = fill_panel; |
| 762 | |
| 763 | setlocale(LC_ALL, ""); |
| 764 | |
| 765 | while ((c = getopt(argc, argv, "i:o:mwx")) != -1) { |
| 766 | switch (c) { |
| 767 | case 'i': |
| 768 | log_in = fopen(optarg, "r"); |
| 769 | break; |
| 770 | case 'o': |
| 771 | log_out = fopen(optarg, "w"); |
| 772 | break; |
| 773 | case 'm': |
| 774 | monochrome = TRUE; |
| 775 | break; |
| 776 | #if USE_WIDEC_SUPPORT |
| 777 | case 'w': |
| 778 | myInit = init_wide_panel; |
| 779 | myFill = fill_wide_panel; |
| 780 | break; |
| 781 | #endif |
| 782 | case 'x': |
| 783 | unboxed = TRUE; |
| 784 | break; |
| 785 | default: |
| 786 | usage(); |
| 787 | } |
| 788 | } |
| 789 | if (unboxed) |
| 790 | myFill = fill_unboxed; |
| 791 | |
| 792 | initscr(); |
| 793 | cbreak(); |
| 794 | noecho(); |
| 795 | keypad(stdscr, TRUE); |
| 796 | |
| 797 | use_colors = monochrome ? FALSE : has_colors(); |
| 798 | if (use_colors) |
| 799 | start_color(); |
| 800 | |
| 801 | demo_panels(myInit, myFill); |
| 802 | endwin(); |
| 803 | |
| 804 | close_input(); |
| 805 | close_output(); |
| 806 | |
| 807 | ExitProgram(EXIT_SUCCESS); |
| 808 | } |
| 809 | #else |
| 810 | int |
| 811 | main(void) |
| 812 | { |
| 813 | printf("This program requires the curses panel library\n"); |
| 814 | ExitProgram(EXIT_FAILURE); |
| 815 | } |
| 816 | #endif |