1 /****************************************************************************
2 * Copyright (c) 2007 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: demo_panels.c,v 1.22 2007/06/30 21:38:08 tom Exp $
31 * Demonstrate a variety of functions from the panel library.
34 #include <test.priv.h>
40 typedef void (*InitPanel) (void);
41 typedef void (*FillPanel) (PANEL *);
43 static bool use_colors = FALSE;
44 static bool unboxed = FALSE;
69 WINDOW *result = stdscr;
71 wmove(result, LINES - 1, 0);
84 saywhat(NCURSES_CONST char *text)
86 WINDOW *win = statusline();
87 if (text != 0 && *text != '\0') {
91 waddstr(win, "press any key to continue");
95 show_position(NCURSES_CONST char *text,
96 NCURSES_CONST char *also,
101 WINDOW *win = statusline();
103 wprintw(win, "%s for panel %d now %d,%d%s", text, which, ypos, xpos, also);
104 wmove(stdscr, ypos, xpos);
108 get_position(NCURSES_CONST char *text,
109 NCURSES_CONST char *also,
118 getyx(stdscr, y1, x1);
121 show_position(text, also, which, y1, x1);
124 (void) wgetch(stdscr);
125 if (fscanf(log_in, "@%d,%d\n", &y1, &x1) == 2) {
132 switch (wgetch(stdscr)) {
149 if (y1 < getmaxy(stdscr)) {
163 if (x1 < getmaxx(stdscr)) {
172 wmove(stdscr, y1, x1);
175 fprintf(log_out, "@%d,%d\n", y1, x1);
183 mkpanel(short color, int rows, int cols, int tly, int tlx)
187 char *userdata = malloc(3);
189 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
191 if ((pan = new_panel(win)) == 0) {
193 } else if (use_colors) {
194 short fg = ((color == COLOR_BLUE)
199 init_pair(color, fg, bg);
200 wbkgdset(win, (chtype) (COLOR_PAIR(color) | ' '));
201 } else if (!unboxed) {
202 wbkgdset(win, A_BOLD | ' ');
205 sprintf(userdata, "p%d", color % 8);
206 set_panel_userptr(pan, (NCURSES_CONST void *) userdata);
211 my_remove_panel(PANEL ** pans, int which)
213 if (pans[which] != 0) {
214 PANEL *pan = pans[which];
215 WINDOW *win = panel_window(pan);
216 char *user = panel_userptr(pan);
226 #define MIN(a,b) ((a) < (b) ? (a) : (b))
227 #define ABS(a) ((a) < 0 ? -(a) : (a))
230 my_create_panel(PANEL ** pans, int which, FillPanel myFill)
235 short fg = (pair == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
239 init_pair(pair, fg, bg);
241 /* remove the old panel, if any */
242 my_remove_panel(pans, which);
244 /* get the position of one corner */
245 wmove(stdscr, getmaxy(stdscr) / 2, getmaxx(stdscr) / 2);
246 getyx(stdscr, y0, x0);
247 while ((code = get_position("First corner", "", which, &x0, &y0)) == 0) {
253 sprintf(also, " (first %d,%d)", y0, x0);
254 /* get the position of the opposite corner */
255 while ((code = get_position("Opposite corner",
256 also, which, &x1, &y1)) == 0) {
261 int tly = MIN(y0, y1);
262 int tlx = MIN(x0, x1);
263 pan = mkpanel(pair, ABS(y1 - y0) + 1, ABS(x1 - x0) + 1, tly, tlx);
268 wmove(stdscr, y1, x1);
274 my_move_panel(PANEL ** pans, int which)
276 if (pans[which] != 0) {
280 WINDOW *win = panel_window(pans[which]);
283 getbegyx(win, y0, x0);
284 sprintf(also, " (start %d,%d)", y0, x0);
285 wmove(stdscr, y0, x0);
286 while ((code = get_position("Move panel", also, which, &x1, &y1)) == 0) {
290 move_panel(pans[which], y1, x1);
296 my_resize_panel(PANEL ** pans, int which, FillPanel myFill)
298 if (pans[which] != 0) {
302 WINDOW *win = panel_window(pans[which]);
305 getbegyx(win, y0, x0);
306 sprintf(also, " (start %d,%d)", y0, x0);
307 wmove(stdscr, y0, x0);
308 while ((code = get_position("Resize panel",
309 also, which, &x1, &y1)) == 0) {
313 WINDOW *next = newwin(ABS(y1 - y0) + 1,
320 wbkgdset(next, (chtype) (COLOR_PAIR(which) | ' '));
321 } else if (!unboxed) {
322 wbkgdset(next, A_BOLD | ' ');
324 replace_panel(pans[which], next);
337 for (y = 0; y < LINES - 1; y++) {
338 for (x = 0; x < COLS; x++)
339 wprintw(stdscr, "%d", (y + x) % 10);
344 fill_panel(PANEL * pan)
346 WINDOW *win = panel_window(pan);
347 int num = ((const char *) panel_userptr(pan))[1];
351 wprintw(win, "-pan%c-", num);
354 for (y = 2; y < getmaxy(win) - 1; y++) {
355 for (x = 1; x < getmaxx(win) - 1; x++) {
357 waddch(win, UChar(num));
363 fill_unboxed(PANEL * pan)
365 WINDOW *win = panel_window(pan);
366 int num = ((const char *) panel_userptr(pan))[1];
369 for (y = 0; y < getmaxy(win); y++) {
370 for (x = 0; x < getmaxx(win); x++) {
372 waddch(win, UChar(num));
377 #if USE_WIDEC_SUPPORT
379 make_fullwidth_digit(cchar_t *target, int digit)
383 source[0] = digit + 0xff10;
385 setcchar(target, source, A_NORMAL, 0, 0);
389 init_wide_panel(void)
394 for (digit = 0; digit < 10; ++digit)
395 make_fullwidth_digit(&temp[digit], digit);
400 digit = (y + x / 2) % 10;
401 } while (add_wch(&temp[digit]) != ERR);
405 fill_wide_panel(PANEL * pan)
407 WINDOW *win = panel_window(pan);
408 int num = ((const char *) panel_userptr(pan))[1];
412 wprintw(win, "-pan%c-", num);
415 for (y = 2; y < getmaxy(win) - 1; y++) {
416 for (x = 1; x < getmaxx(win) - 1; x++) {
418 waddch(win, UChar(num));
427 which_panel(PANEL * px[MAX_PANELS + 1], PANEL * pan)
432 for (j = 1; j <= MAX_PANELS; ++j) {
442 show_panels(PANEL * px[MAX_PANELS + 1])
444 static const char *help[] =
447 "Commands are letter/digit pairs. Digits are the panel number.",
449 " b - put the panel on the bottom of the stack",
450 " c - create the panel",
451 " d - delete the panel",
452 " h - hide the panel",
453 " m - move the panel",
454 " r - resize the panel",
455 " s - show the panel",
456 " b - put the panel on the top of the stack"
464 } table[MAX_PANELS + 1];
470 for (j = 1; j <= MAX_PANELS; ++j) {
471 table[j].valid = (px[j] != 0);
472 if (table[j].valid) {
473 table[j].hidden = panel_hidden(px[j]);
474 table[j].above = panel_above(px[j]);
475 table[j].below = panel_below(px[j]);
479 if ((win = newwin(LINES - 1, COLS, 0, 0)) != 0) {
481 if ((pan = new_panel(win)) != 0) {
483 mvwprintw(win, 0, 0, "Panels:\n");
484 for (j = 1; j <= MAX_PANELS; ++j) {
485 if (table[j].valid) {
486 wprintw(win, " %d:", j);
487 if (table[j].hidden) {
488 waddstr(win, " hidden");
490 if (table[j].above) {
491 wprintw(win, " above %d",
492 which_panel(px, table[j].above));
494 if (table[j].below) {
495 wprintw(win, "%s below %d",
496 table[j].above ? "," : "",
497 which_panel(px, table[j].below));
503 for (j = 0; j < (int) SIZEOF(help); ++j) {
504 if (wprintw(win, "%s\n", help[j]) == ERR)
515 #define wrapper(func) \
516 static int my_##func(PANEL *pan) \
525 wrapper(bottom_panel)
532 do_panel(PANEL * px[MAX_PANELS + 1],
533 NCURSES_CONST char *cmd,
536 int which = cmd[1] - '0';
540 (void) wgetch(stdscr);
546 my_bottom_panel(px[which]);
549 my_create_panel(px, which, myFill);
552 my_remove_panel(px, which);
555 my_hide_panel(px[which]);
558 my_move_panel(px, which);
561 my_resize_panel(px, which, myFill);
564 my_show_panel(px[which]);
567 my_top_panel(px[which]);
575 return isalpha(UChar(ch)) && strchr("bcdhmrst", ch) != 0;
581 return isdigit(UChar(ch)) && (ch >= '1') && (ch - '0' <= MAX_PANELS);
585 * A command consists of one or more letter/digit pairs separated by a space.
586 * Digits are limited to 1..MAX_PANELS.
588 * End the command with a newline. Reject other characters.
591 get_command(PANEL * px[MAX_PANELS + 1], char *buffer, int limit)
598 getyx(stdscr, y0, x0);
600 waddstr(win, "Command:");
601 buffer[length = 0] = '\0';
605 if (fgets(buffer, limit - 3, log_in) != 0) {
606 length = strlen(buffer);
607 while (length > 0 && isspace(buffer[length - 1]))
608 buffer[--length] = '\0';
616 if (ch == ERR || ch == QUIT || ch == ESCAPE) {
619 } else if (ch == CTRL('L')) {
621 } else if (ch == '\n' || ch == KEY_ENTER) {
623 } else if (ch == '?') {
625 } else if (length + 3 < limit) {
628 } else if (ok_letter(UChar(ch))) {
629 if (isalpha(UChar(c0))) {
631 } else if (isdigit(UChar(c0))) {
632 wprintw(win, " %c", ch);
633 buffer[length++] = ' ';
634 buffer[length++] = c0 = ch;
636 wprintw(win, "%c", ch);
637 buffer[length++] = c0 = ch;
639 } else if (ok_digit(ch)) {
640 if (isalpha(UChar(c0))) {
641 wprintw(win, "%c", ch);
642 buffer[length++] = c0 = ch;
646 } else if (ch == ' ') {
647 if (isdigit(UChar(c0))) {
648 wprintw(win, "%c", ch);
649 buffer[length++] = c0 = ch;
662 wmove(stdscr, y0, x0);
664 buffer[length] = '\0';
665 if (log_out && length) {
666 fprintf(log_out, "%s\n", buffer);
668 return (length != 0);
672 demo_panels(InitPanel myInit, FillPanel myFill)
675 PANEL *px[MAX_PANELS + 1];
678 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
682 memset(px, 0, sizeof(px));
684 while (get_command(px, buffer, sizeof(buffer))) {
685 int limit = strlen(buffer);
686 for (itmp = 0; itmp < limit; itmp += 3) {
687 do_panel(px, buffer + itmp, myFill);
692 for (itmp = 1; itmp <= MAX_PANELS; ++itmp) {
693 my_remove_panel(px, itmp);
701 static const char *const tbl[] =
703 "Usage: demo_panels [options]"
706 ," -i file read commands from file"
707 ," -o file record commands in file"
708 ," -m do not use colors"
709 #if USE_WIDEC_SUPPORT
710 ," -w use wide-characters in panels and background"
712 ," -x do not enclose panels in boxes"
715 for (n = 0; n < SIZEOF(tbl); n++)
716 fprintf(stderr, "%s\n", tbl[n]);
717 ExitProgram(EXIT_FAILURE);
721 main(int argc, char *argv[])
724 bool monochrome = FALSE;
725 InitPanel myInit = init_panel;
726 FillPanel myFill = fill_panel;
728 setlocale(LC_ALL, "");
730 while ((c = getopt(argc, argv, "i:o:mwx")) != EOF) {
733 log_in = fopen(optarg, "r");
736 log_out = fopen(optarg, "w");
741 #if USE_WIDEC_SUPPORT
743 myInit = init_wide_panel;
744 myFill = fill_wide_panel;
755 myFill = fill_unboxed;
760 keypad(stdscr, TRUE);
762 use_colors = monochrome ? FALSE : has_colors();
766 demo_panels(myInit, myFill);
772 ExitProgram(EXIT_SUCCESS);
778 printf("This program requires the curses panel library\n");
779 ExitProgram(EXIT_FAILURE);