1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 2003-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 * $Id: demo_panels.c,v 1.45 2020/02/02 23:34:34 tom Exp $
32 * Demonstrate a variety of functions from the panel library.
35 #include <test.priv.h>
44 typedef void (*InitPanel) (void);
45 typedef void (*FillPanel) (PANEL *);
47 static bool use_colors = FALSE;
48 static bool unboxed = FALSE;
73 WINDOW *result = stdscr;
75 wmove(result, LINES - 1, 0);
88 saywhat(NCURSES_CONST char *text)
90 WINDOW *win = statusline();
91 if (text != 0 && *text != '\0') {
95 waddstr(win, "press any key to continue");
99 show_position(NCURSES_CONST char *text,
100 NCURSES_CONST char *also,
105 WINDOW *win = statusline();
107 wprintw(win, "%s for panel %d now %d,%d%s", text, which, ypos, xpos, also);
108 wmove(stdscr, ypos, xpos);
112 get_position(NCURSES_CONST char *text,
113 NCURSES_CONST char *also,
122 getyx(stdscr, y1, x1);
125 show_position(text, also, which, y1, x1);
128 if (fscanf(log_in, "%c%d,%d\n", &cmd, &y1, &x1) == 3) {
132 (void) wgetch(stdscr);
148 switch (wgetch(stdscr)) {
165 if (y1 < getmaxy(stdscr)) {
179 if (x1 < getmaxx(stdscr)) {
188 wmove(stdscr, y1, x1);
194 fprintf(log_out, "%c%d,%d\n",
204 mkpanel(short color, int rows, int cols, int tly, int tlx)
208 char *userdata = typeMalloc(char, 6);
210 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
212 if ((pan = new_panel(win)) == 0) {
214 } else if (use_colors) {
215 short fg = (short) ((color == COLOR_BLUE)
220 init_pair(color, fg, bg);
221 wbkgdset(win, (chtype) (COLOR_PAIR(color) | ' '));
222 } else if (!unboxed) {
223 wbkgdset(win, A_BOLD | ' ');
226 _nc_SPRINTF(userdata, _nc_SLIMIT(4) "p%d", color % 8);
227 set_panel_userptr(pan, (NCURSES_CONST void *) userdata);
232 my_remove_panel(PANEL **pans, int which)
234 if (pans[which] != 0) {
235 PANEL *pan = pans[which];
236 WINDOW *win = panel_window(pan);
237 char *user = (char *) panel_userptr(pan);
248 #define MIN(a,b) ((a) < (b) ? (a) : (b))
249 #define ABS(a) ((a) < 0 ? -(a) : (a))
252 my_create_panel(PANEL **pans, int which, FillPanel myFill)
255 short pair = (short) which;
256 short fg = (short) ((pair == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK);
260 init_pair(pair, fg, bg);
262 /* remove the old panel, if any */
263 my_remove_panel(pans, which);
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) {
274 _nc_SPRINTF(also, _nc_SLIMIT(sizeof(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) {
282 int tly = MIN(y0, y1);
283 int tlx = MIN(x0, x1);
284 PANEL *pan = mkpanel(pair,
292 wmove(stdscr, y1, x1);
298 my_move_panel(PANEL **pans, int which, bool continuous)
300 if (pans[which] != 0) {
304 WINDOW *win = panel_window(pans[which]);
307 getbegyx(win, y0, x0);
308 _nc_SPRINTF(also, _nc_SLIMIT(sizeof(also)) " (start %d,%d)", y0, x0);
309 wmove(stdscr, y0, x0);
310 while ((code = get_position("Move panel", also, which, &x1, &y1)) == 0) {
312 move_panel(pans[which], y1, x1);
317 move_panel(pans[which], y1, x1);
323 my_resize_panel(PANEL **pans, int which, FillPanel myFill)
325 if (pans[which] != 0) {
329 WINDOW *win = panel_window(pans[which]);
332 getbegyx(win, y0, x0);
333 _nc_SPRINTF(also, _nc_SLIMIT(sizeof(also)) " (start %d,%d)", y0, x0);
334 wmove(stdscr, y0, x0);
335 while ((code = get_position("Resize panel",
336 also, which, &x1, &y1)) == 0) {
340 WINDOW *next = newwin(ABS(y1 - y0) + 1,
347 wbkgdset(next, (chtype) (COLOR_PAIR(which) | ' '));
348 } else if (!unboxed) {
349 wbkgdset(next, A_BOLD | ' ');
351 replace_panel(pans[which], next);
364 for (y = 0; y < LINES - 1; y++) {
365 for (x = 0; x < COLS; x++)
366 wprintw(stdscr, "%d", (y + x) % 10);
371 fill_panel(PANEL *pan)
373 WINDOW *win = panel_window(pan);
374 const char *userptr = (const char *) panel_userptr(pan);
375 int num = (userptr && *userptr) ? userptr[1] : '?';
379 wprintw(win, "-pan%c-", num);
382 for (y = 2; y < getmaxy(win) - 1; y++) {
383 for (x = 1; x < getmaxx(win) - 1; x++) {
385 waddch(win, UChar(num));
391 fill_unboxed(PANEL *pan)
393 WINDOW *win = panel_window(pan);
394 const char *userptr = (const char *) panel_userptr(pan);
395 int num = (userptr && *userptr) ? userptr[1] : '?';
398 for (y = 0; y < getmaxy(win); y++) {
399 for (x = 0; x < getmaxx(win); x++) {
401 waddch(win, UChar(num));
406 #if USE_WIDEC_SUPPORT
408 make_fullwidth_digit(cchar_t *target, int digit)
412 source[0] = (wchar_t) (digit + 0xff10);
414 setcchar(target, source, A_NORMAL, 0, 0);
418 init_wide_panel(void)
423 for (digit = 0; digit < 10; ++digit)
424 make_fullwidth_digit(&temp[digit], digit);
429 digit = (y + x / 2) % 10;
430 } while (add_wch(&temp[digit]) != ERR);
434 fill_wide_panel(PANEL *pan)
436 WINDOW *win = panel_window(pan);
437 int num = ((const char *) panel_userptr(pan))[1];
441 wprintw(win, "-pan%c-", num);
444 for (y = 2; y < getmaxy(win) - 1; y++) {
445 for (x = 1; x < getmaxx(win) - 1; x++) {
447 waddch(win, UChar(num));
456 which_panel(PANEL *px[MAX_PANELS + 1], PANEL *pan)
461 for (j = 1; j <= MAX_PANELS; ++j) {
471 show_help(WINDOW *win)
473 static const char *help[] =
476 "Commands are letter/digit pairs. Digits are the panel number.",
478 " b - put the panel on the bottom of the stack",
479 " c - create the panel",
480 " d - delete the panel",
481 " h - hide the panel",
482 " m - move the panel (M for continuous move)",
483 " r - resize the panel",
484 " s - show the panel",
485 " t - put the panel on the top of the stack"
489 for (j = 0; j < (int) SIZEOF(help); ++j) {
490 if (wprintw(win, "%s\n", help[j]) == ERR)
496 show_panels(PANEL *px[MAX_PANELS + 1])
503 } table[MAX_PANELS + 1];
508 memset(table, 0, sizeof(table));
509 for (j = 1; j <= MAX_PANELS; ++j) {
510 table[j].valid = (px[j] != 0);
511 if (table[j].valid) {
512 table[j].hidden = panel_hidden(px[j]);
513 table[j].above = panel_above(px[j]);
514 table[j].below = panel_below(px[j]);
518 if ((win = newwin(LINES - 1, COLS, 0, 0)) != 0) {
522 if ((pan = new_panel(win)) != 0) {
524 MvWPrintw(win, 0, 0, "Panels:\n");
525 for (j = 1; j <= MAX_PANELS; ++j) {
526 if (table[j].valid) {
527 wprintw(win, " %d:", j);
528 if (table[j].hidden) {
529 waddstr(win, " hidden");
531 if (table[j].above) {
532 wprintw(win, " above %d",
533 which_panel(px, table[j].above));
535 if (table[j].below) {
536 wprintw(win, "%s below %d",
537 table[j].above ? "," : "",
538 which_panel(px, table[j].below));
553 #define wrapper(func) \
554 static int my_##func(PANEL *pan) \
563 wrapper(bottom_panel)
570 do_panel(PANEL *px[MAX_PANELS + 1],
571 NCURSES_CONST char *cmd,
574 int which = cmd[1] - '0';
576 if (which < 1 || which > MAX_PANELS) {
588 my_bottom_panel(px[which]);
591 my_create_panel(px, which, myFill);
594 my_remove_panel(px, which);
597 my_hide_panel(px[which]);
600 my_move_panel(px, which, FALSE);
603 my_move_panel(px, which, TRUE);
606 my_resize_panel(px, which, myFill);
609 my_show_panel(px[which]);
612 my_top_panel(px[which]);
620 return isalpha(UChar(ch)) && strchr("bcdhmMrst", ch) != 0;
626 return isdigit(UChar(ch)) && (ch >= '1') && (ch - '0' <= MAX_PANELS);
630 * A command consists of one or more letter/digit pairs separated by a space.
631 * Digits are limited to 1..MAX_PANELS.
633 * End the command with a newline. Reject other characters.
636 get_command(PANEL *px[MAX_PANELS + 1], char *buffer, int limit)
642 getyx(stdscr, y0, x0);
644 waddstr(win, "Command:");
645 buffer[length = 0] = '\0';
648 if (fgets(buffer, limit - 3, log_in) != 0) {
649 length = (int) strlen(buffer);
650 while (length > 0 && isspace(UChar(buffer[length - 1])))
651 buffer[--length] = '\0';
652 waddstr(win, buffer);
660 int ch = wgetch(win);
661 if (ch == ERR || ch == QUIT || ch == ESCAPE) {
664 } else if (ch == CTRL('L')) {
666 } else if (ch == '\n' || ch == KEY_ENTER) {
668 } else if (ch == HELP_KEY_1) {
670 } else if (length + 3 < limit) {
673 } else if (ok_letter(UChar(ch))) {
674 if (isalpha(UChar(c0))) {
676 } else if (isdigit(UChar(c0))) {
677 wprintw(win, " %c", ch);
678 buffer[length++] = ' ';
679 buffer[length++] = (char) (c0 = ch);
681 wprintw(win, "%c", ch);
682 buffer[length++] = (char) (c0 = ch);
684 } else if (ok_digit(ch)) {
685 if (isalpha(UChar(c0))) {
686 wprintw(win, "%c", ch);
687 buffer[length++] = (char) (c0 = ch);
691 } else if (ch == ' ') {
692 if (isdigit(UChar(c0))) {
693 wprintw(win, "%c", ch);
694 buffer[length++] = (char) (c0 = ch);
707 wmove(stdscr, y0, x0);
709 buffer[length] = '\0';
710 if (log_out && length) {
711 fprintf(log_out, "%s\n", buffer);
713 return (length != 0);
717 demo_panels(InitPanel myInit, FillPanel myFill)
720 PANEL *px[MAX_PANELS + 1];
723 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
727 memset(px, 0, sizeof(px));
729 while (get_command(px, buffer, sizeof(buffer))) {
730 int limit = (int) strlen(buffer);
731 for (itmp = 0; itmp < limit; itmp += 3) {
732 do_panel(px, buffer + itmp, myFill);
737 for (itmp = 1; itmp <= MAX_PANELS; ++itmp) {
738 my_remove_panel(px, itmp);
746 static const char *const tbl[] =
748 "Usage: demo_panels [options]"
751 ," -i file read commands from file"
752 ," -o file record commands in file"
753 ," -m do not use colors"
754 #if USE_WIDEC_SUPPORT
755 ," -w use wide-characters in panels and background"
757 ," -x do not enclose panels in boxes"
760 for (n = 0; n < SIZEOF(tbl); n++)
761 fprintf(stderr, "%s\n", tbl[n]);
762 ExitProgram(EXIT_FAILURE);
766 main(int argc, char *argv[])
769 bool monochrome = FALSE;
770 InitPanel myInit = init_panel;
771 FillPanel myFill = fill_panel;
773 setlocale(LC_ALL, "");
775 while ((c = getopt(argc, argv, "i:o:mwx")) != -1) {
778 log_in = fopen(optarg, "r");
781 log_out = fopen(optarg, "w");
786 #if USE_WIDEC_SUPPORT
788 myInit = init_wide_panel;
789 myFill = fill_wide_panel;
800 myFill = fill_unboxed;
805 keypad(stdscr, TRUE);
807 use_colors = monochrome ? FALSE : has_colors();
811 demo_panels(myInit, myFill);
817 ExitProgram(EXIT_SUCCESS);
823 printf("This program requires the curses panel library\n");
824 ExitProgram(EXIT_FAILURE);