1 /****************************************************************************
2 * Copyright (c) 2007-2008,2010 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.34 2010/03/06 18:14:04 tom Exp $
31 * Demonstrate a variety of functions from the panel library.
34 #include <test.priv.h>
43 typedef void (*InitPanel) (void);
44 typedef void (*FillPanel) (PANEL *);
46 static bool use_colors = FALSE;
47 static bool unboxed = FALSE;
72 WINDOW *result = stdscr;
74 wmove(result, LINES - 1, 0);
87 saywhat(NCURSES_CONST char *text)
89 WINDOW *win = statusline();
90 if (text != 0 && *text != '\0') {
94 waddstr(win, "press any key to continue");
98 show_position(NCURSES_CONST char *text,
99 NCURSES_CONST char *also,
104 WINDOW *win = statusline();
106 wprintw(win, "%s for panel %d now %d,%d%s", text, which, ypos, xpos, also);
107 wmove(stdscr, ypos, xpos);
111 get_position(NCURSES_CONST char *text,
112 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, 3);
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 sprintf(userdata, "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)
256 short pair = (short) which;
257 short fg = (short) ((pair == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK);
261 init_pair(pair, fg, bg);
263 /* remove the old panel, if any */
264 my_remove_panel(pans, which);
266 /* get the position of one corner */
267 wmove(stdscr, getmaxy(stdscr) / 2, getmaxx(stdscr) / 2);
268 getyx(stdscr, y0, x0);
269 while ((code = get_position("First corner", "", which, &x0, &y0)) == 0) {
275 sprintf(also, " (first %d,%d)", y0, x0);
276 /* get the position of the opposite corner */
277 while ((code = get_position("Opposite corner",
278 also, which, &x1, &y1)) == 0) {
283 int tly = MIN(y0, y1);
284 int tlx = MIN(x0, x1);
285 pan = mkpanel(pair, ABS(y1 - y0) + 1, ABS(x1 - x0) + 1, tly, tlx);
290 wmove(stdscr, y1, x1);
296 my_move_panel(PANEL ** pans, int which, bool continuous)
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("Move panel", also, which, &x1, &y1)) == 0) {
310 move_panel(pans[which], y1, x1);
315 move_panel(pans[which], y1, x1);
321 my_resize_panel(PANEL ** pans, int which, FillPanel myFill)
323 if (pans[which] != 0) {
327 WINDOW *win = panel_window(pans[which]);
330 getbegyx(win, y0, x0);
331 sprintf(also, " (start %d,%d)", y0, x0);
332 wmove(stdscr, y0, x0);
333 while ((code = get_position("Resize panel",
334 also, which, &x1, &y1)) == 0) {
338 WINDOW *next = newwin(ABS(y1 - y0) + 1,
345 wbkgdset(next, (chtype) (COLOR_PAIR(which) | ' '));
346 } else if (!unboxed) {
347 wbkgdset(next, A_BOLD | ' ');
349 replace_panel(pans[which], next);
362 for (y = 0; y < LINES - 1; y++) {
363 for (x = 0; x < COLS; x++)
364 wprintw(stdscr, "%d", (y + x) % 10);
369 fill_panel(PANEL * pan)
371 WINDOW *win = panel_window(pan);
372 const char *userptr = (const char *) panel_userptr(pan);
373 int num = (userptr && *userptr) ? userptr[1] : '?';
377 wprintw(win, "-pan%c-", num);
380 for (y = 2; y < getmaxy(win) - 1; y++) {
381 for (x = 1; x < getmaxx(win) - 1; x++) {
383 waddch(win, UChar(num));
389 fill_unboxed(PANEL * pan)
391 WINDOW *win = panel_window(pan);
392 const char *userptr = (const char *) panel_userptr(pan);
393 int num = (userptr && *userptr) ? userptr[1] : '?';
396 for (y = 0; y < getmaxy(win); y++) {
397 for (x = 0; x < getmaxx(win); x++) {
399 waddch(win, UChar(num));
404 #if USE_WIDEC_SUPPORT
406 make_fullwidth_digit(cchar_t *target, int digit)
410 source[0] = digit + 0xff10;
412 setcchar(target, source, A_NORMAL, 0, 0);
416 init_wide_panel(void)
421 for (digit = 0; digit < 10; ++digit)
422 make_fullwidth_digit(&temp[digit], digit);
427 digit = (y + x / 2) % 10;
428 } while (add_wch(&temp[digit]) != ERR);
432 fill_wide_panel(PANEL * pan)
434 WINDOW *win = panel_window(pan);
435 int num = ((const char *) panel_userptr(pan))[1];
439 wprintw(win, "-pan%c-", num);
442 for (y = 2; y < getmaxy(win) - 1; y++) {
443 for (x = 1; x < getmaxx(win) - 1; x++) {
445 waddch(win, UChar(num));
454 which_panel(PANEL * px[MAX_PANELS + 1], PANEL * pan)
459 for (j = 1; j <= MAX_PANELS; ++j) {
469 show_panels(PANEL * px[MAX_PANELS + 1])
471 static const char *help[] =
474 "Commands are letter/digit pairs. Digits are the panel number.",
476 " b - put the panel on the bottom of the stack",
477 " c - create the panel",
478 " d - delete the panel",
479 " h - hide the panel",
480 " m - move the panel (M for continuous move)",
481 " r - resize the panel",
482 " s - show the panel",
483 " b - put the panel on the top of the stack"
491 } table[MAX_PANELS + 1];
497 memset(table, 0, sizeof(table));
498 for (j = 1; j <= MAX_PANELS; ++j) {
499 table[j].valid = (px[j] != 0);
500 if (table[j].valid) {
501 table[j].hidden = panel_hidden(px[j]);
502 table[j].above = panel_above(px[j]);
503 table[j].below = panel_below(px[j]);
507 if ((win = newwin(LINES - 1, COLS, 0, 0)) != 0) {
509 if ((pan = new_panel(win)) != 0) {
511 mvwprintw(win, 0, 0, "Panels:\n");
512 for (j = 1; j <= MAX_PANELS; ++j) {
513 if (table[j].valid) {
514 wprintw(win, " %d:", j);
515 if (table[j].hidden) {
516 waddstr(win, " hidden");
518 if (table[j].above) {
519 wprintw(win, " above %d",
520 which_panel(px, table[j].above));
522 if (table[j].below) {
523 wprintw(win, "%s below %d",
524 table[j].above ? "," : "",
525 which_panel(px, table[j].below));
531 for (j = 0; j < (int) SIZEOF(help); ++j) {
532 if (wprintw(win, "%s\n", help[j]) == ERR)
543 #define wrapper(func) \
544 static int my_##func(PANEL *pan) \
553 wrapper(bottom_panel)
560 do_panel(PANEL * px[MAX_PANELS + 1],
561 NCURSES_CONST char *cmd,
564 int which = cmd[1] - '0';
566 if (which < 1 || which > MAX_PANELS) {
578 my_bottom_panel(px[which]);
581 my_create_panel(px, which, myFill);
584 my_remove_panel(px, which);
587 my_hide_panel(px[which]);
590 my_move_panel(px, which, FALSE);
593 my_move_panel(px, which, TRUE);
596 my_resize_panel(px, which, myFill);
599 my_show_panel(px[which]);
602 my_top_panel(px[which]);
610 return isalpha(UChar(ch)) && strchr("bcdhmMrst", ch) != 0;
616 return isdigit(UChar(ch)) && (ch >= '1') && (ch - '0' <= MAX_PANELS);
620 * A command consists of one or more letter/digit pairs separated by a space.
621 * Digits are limited to 1..MAX_PANELS.
623 * End the command with a newline. Reject other characters.
626 get_command(PANEL * px[MAX_PANELS + 1], char *buffer, int limit)
633 getyx(stdscr, y0, x0);
635 waddstr(win, "Command:");
636 buffer[length = 0] = '\0';
639 if (fgets(buffer, limit - 3, log_in) != 0) {
640 length = (int) strlen(buffer);
641 while (length > 0 && isspace(UChar(buffer[length - 1])))
642 buffer[--length] = '\0';
643 waddstr(win, buffer);
652 if (ch == ERR || ch == QUIT || ch == ESCAPE) {
655 } else if (ch == CTRL('L')) {
657 } else if (ch == '\n' || ch == KEY_ENTER) {
659 } else if (ch == '?') {
661 } else if (length + 3 < limit) {
664 } else if (ok_letter(UChar(ch))) {
665 if (isalpha(UChar(c0))) {
667 } else if (isdigit(UChar(c0))) {
668 wprintw(win, " %c", ch);
669 buffer[length++] = ' ';
670 buffer[length++] = (char) (c0 = ch);
672 wprintw(win, "%c", ch);
673 buffer[length++] = (char) (c0 = ch);
675 } else if (ok_digit(ch)) {
676 if (isalpha(UChar(c0))) {
677 wprintw(win, "%c", ch);
678 buffer[length++] = (char) (c0 = ch);
682 } else if (ch == ' ') {
683 if (isdigit(UChar(c0))) {
684 wprintw(win, "%c", ch);
685 buffer[length++] = (char) (c0 = ch);
698 wmove(stdscr, y0, x0);
700 buffer[length] = '\0';
701 if (log_out && length) {
702 fprintf(log_out, "%s\n", buffer);
704 return (length != 0);
708 demo_panels(InitPanel myInit, FillPanel myFill)
711 PANEL *px[MAX_PANELS + 1];
714 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
718 memset(px, 0, sizeof(px));
720 while (get_command(px, buffer, sizeof(buffer))) {
721 int limit = (int) strlen(buffer);
722 for (itmp = 0; itmp < limit; itmp += 3) {
723 do_panel(px, buffer + itmp, myFill);
728 for (itmp = 1; itmp <= MAX_PANELS; ++itmp) {
729 my_remove_panel(px, itmp);
737 static const char *const tbl[] =
739 "Usage: demo_panels [options]"
742 ," -i file read commands from file"
743 ," -o file record commands in file"
744 ," -m do not use colors"
745 #if USE_WIDEC_SUPPORT
746 ," -w use wide-characters in panels and background"
748 ," -x do not enclose panels in boxes"
751 for (n = 0; n < SIZEOF(tbl); n++)
752 fprintf(stderr, "%s\n", tbl[n]);
753 ExitProgram(EXIT_FAILURE);
757 main(int argc, char *argv[])
760 bool monochrome = FALSE;
761 InitPanel myInit = init_panel;
762 FillPanel myFill = fill_panel;
764 setlocale(LC_ALL, "");
766 while ((c = getopt(argc, argv, "i:o:mwx")) != -1) {
769 log_in = fopen(optarg, "r");
772 log_out = fopen(optarg, "w");
777 #if USE_WIDEC_SUPPORT
779 myInit = init_wide_panel;
780 myFill = fill_wide_panel;
791 myFill = fill_unboxed;
796 keypad(stdscr, TRUE);
798 use_colors = monochrome ? FALSE : has_colors();
802 demo_panels(myInit, myFill);
808 ExitProgram(EXIT_SUCCESS);
814 printf("This program requires the curses panel library\n");
815 ExitProgram(EXIT_FAILURE);