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.16 2007/06/23 21:24:23 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;
68 WINDOW *result = stdscr;
70 wmove(result, LINES - 1, 0);
83 saywhat(NCURSES_CONST char *text)
85 WINDOW *win = statusline();
86 if (text != 0 && *text != '\0') {
90 waddstr(win, "press any key to continue");
94 show_position(NCURSES_CONST char *text,
95 NCURSES_CONST char *also,
100 WINDOW *win = statusline();
102 wprintw(win, "%s for panel %d now %d,%d%s", text, which, ypos, xpos, also);
103 wmove(stdscr, ypos, xpos);
107 get_position(NCURSES_CONST char *text,
108 NCURSES_CONST char *also,
117 getyx(stdscr, y1, x1);
120 show_position(text, also, which, y1, x1);
123 (void) wgetch(stdscr);
124 if (fscanf(log_in, "@%d,%d\n", &y1, &x1) == 2) {
131 switch (wgetch(stdscr)) {
148 if (y1 < getmaxy(stdscr)) {
162 if (x1 < getmaxx(stdscr)) {
171 wmove(stdscr, y1, x1);
174 fprintf(log_out, "@%d,%d\n", y1, x1);
182 mkpanel(short color, int rows, int cols, int tly, int tlx)
186 char *userdata = malloc(3);
188 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
190 if ((pan = new_panel(win)) == 0) {
192 } else if (use_colors) {
193 short fg = ((color == COLOR_BLUE)
198 init_pair(color, fg, bg);
199 wbkgdset(win, (chtype) (COLOR_PAIR(color) | ' '));
201 wbkgdset(win, A_BOLD | ' ');
204 sprintf(userdata, "p%d", color % 8);
205 set_panel_userptr(pan, (NCURSES_CONST void *) userdata);
210 remove_panel(PANEL ** pans, int which)
212 if (pans[which] != 0) {
213 PANEL *pan = pans[which];
214 WINDOW *win = panel_window(pan);
215 char *user = panel_userptr(pan);
225 #define MIN(a,b) ((a) < (b) ? (a) : (b))
226 #define ABS(a) ((a) < 0 ? -(a) : (a))
229 create_panel(PANEL ** pans, int which, FillPanel myFill)
234 short fg = (pair == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
238 init_pair(pair, fg, bg);
240 /* remove the old panel, if any */
241 remove_panel(pans, which);
243 /* get the position of one corner */
244 wmove(stdscr, getmaxy(stdscr) / 2, getmaxx(stdscr) / 2);
245 getyx(stdscr, y0, x0);
246 while ((code = get_position("First corner", "", which, &x0, &y0)) == 0) {
252 sprintf(also, " (first %d,%d)", y0, x0);
253 /* get the position of the opposite corner */
254 while ((code = get_position("Opposite corner",
255 also, which, &x1, &y1)) == 0) {
260 int tly = MIN(y0, y1);
261 int tlx = MIN(x0, x1);
262 pan = mkpanel(pair, ABS(y1 - y0) + 1, ABS(x1 - x0) + 1, tly, tlx);
267 wmove(stdscr, y1, x1);
273 my_move_panel(PANEL ** pans, int which)
278 WINDOW *win = panel_window(pans[which]);
281 getbegyx(win, y0, x0);
282 sprintf(also, " (start %d,%d)", y0, x0);
283 wmove(stdscr, y0, x0);
284 while ((code = get_position("Move panel", also, which, &x1, &y1)) == 0) {
288 move_panel(pans[which], y1, x1);
293 resize_panel(PANEL ** pans, int which, FillPanel myFill)
298 WINDOW *win = panel_window(pans[which]);
301 getbegyx(win, y0, x0);
302 sprintf(also, " (start %d,%d)", y0, x0);
303 wmove(stdscr, y0, x0);
304 while ((code = get_position("Resize panel", also, which, &x1, &y1)) == 0) {
308 WINDOW *next = newwin(ABS(y1 - y0) + 1,
315 wbkgdset(next, (chtype) (COLOR_PAIR(which) | ' '));
317 wbkgdset(next, A_BOLD | ' ');
319 replace_panel(pans[which], next);
331 for (y = 0; y < LINES - 1; y++) {
332 for (x = 0; x < COLS; x++)
333 wprintw(stdscr, "%d", (y + x) % 10);
338 fill_panel(PANEL * pan)
340 WINDOW *win = panel_window(pan);
341 int num = ((const char *) panel_userptr(pan))[1];
345 wprintw(win, "-pan%c-", num);
348 for (y = 2; y < getmaxy(win) - 1; y++) {
349 for (x = 1; x < getmaxx(win) - 1; x++) {
351 waddch(win, UChar(num));
356 #if USE_WIDEC_SUPPORT
358 make_fullwidth_digit(cchar_t *target, int digit)
362 source[0] = digit + 0xff10;
364 setcchar(target, source, A_NORMAL, 0, 0);
368 init_wide_panel(void)
373 for (digit = 0; digit < 10; ++digit)
374 make_fullwidth_digit(&temp[digit], digit);
379 digit = (y + x / 2) % 10;
380 } while (add_wch(&temp[digit]) != ERR);
384 fill_wide_panel(PANEL * pan)
386 WINDOW *win = panel_window(pan);
387 int num = ((const char *) panel_userptr(pan))[1];
391 wprintw(win, "-pan%c-", num);
394 for (y = 2; y < getmaxy(win) - 1; y++) {
395 for (x = 1; x < getmaxx(win) - 1; x++) {
397 waddch(win, UChar(num));
406 which_panel(PANEL * px[MAX_PANELS + 1], PANEL * pan)
411 for (j = 1; j <= MAX_PANELS; ++j) {
421 show_panels(PANEL * px[MAX_PANELS + 1])
423 static const char *help[] =
426 "Commands are letter/digit pairs. Digits are the panel number.",
428 " b - put the panel on the bottom of the stack",
429 " c - create the panel",
430 " d - delete the panel",
431 " h - hide the panel",
432 " m - move the panel",
433 " r - resize the panel",
434 " s - show the panel",
435 " b - put the panel on the top of the stack"
443 } table[MAX_PANELS + 1];
449 for (j = 1; j <= MAX_PANELS; ++j) {
450 table[j].valid = (px[j] != 0);
451 if (table[j].valid) {
452 table[j].hidden = panel_hidden(px[j]);
453 table[j].above = panel_above(px[j]);
454 table[j].below = panel_below(px[j]);
458 if ((win = newwin(LINES - 1, COLS, 0, 0)) != 0) {
460 if ((pan = new_panel(win)) != 0) {
462 mvwprintw(win, 0, 0, "Panels:\n");
463 for (j = 1; j <= MAX_PANELS; ++j) {
464 if (table[j].valid) {
465 wprintw(win, " %d:", j);
466 if (table[j].hidden) {
467 waddstr(win, " hidden");
469 if (table[j].above) {
470 wprintw(win, " above %d",
471 which_panel(px, table[j].above));
473 if (table[j].below) {
474 wprintw(win, "%s below %d",
475 table[j].above ? "," : "",
476 which_panel(px, table[j].below));
482 for (j = 0; j < (int) SIZEOF(help); ++j) {
483 if (wprintw(win, "%s\n", help[j]) == ERR)
495 do_panel(PANEL * px[MAX_PANELS + 1],
496 NCURSES_CONST char *cmd,
499 int which = cmd[1] - '0';
503 (void) wgetch(stdscr);
509 bottom_panel(px[which]);
512 create_panel(px, which, myFill);
515 remove_panel(px, which);
518 hide_panel(px[which]);
521 my_move_panel(px, which);
524 resize_panel(px, which, myFill);
527 show_panel(px[which]);
530 top_panel(px[which]);
538 return isalpha(UChar(ch)) && strchr("bcdhmrst", ch) != 0;
544 return isdigit(UChar(ch)) && (ch >= '1') && (ch - '0' <= MAX_PANELS);
548 * A command consists of one or more letter/digit pairs separated by a space.
549 * Digits are limited to 1..MAX_PANELS.
551 * End the command with a newline. Reject other characters.
554 get_command(PANEL * px[MAX_PANELS + 1], char *buffer, int limit)
561 getyx(stdscr, y0, x0);
563 waddstr(win, "Command:");
564 buffer[length = 0] = '\0';
568 if (fgets(buffer, limit - 3, log_in) != 0) {
569 length = strlen(buffer);
570 while (length > 0 && isspace(buffer[length - 1]))
571 buffer[--length] = '\0';
579 if (ch == ERR || ch == QUIT || ch == ESCAPE) {
582 } else if (ch == '\n' || ch == KEY_ENTER) {
584 } else if (ch == '?') {
586 } else if (length + 3 < limit) {
589 } else if (ok_letter(UChar(ch))) {
590 if (isalpha(UChar(c0))) {
592 } else if (isdigit(UChar(c0))) {
593 wprintw(win, " %c", ch);
594 buffer[length++] = ' ';
595 buffer[length++] = c0 = ch;
597 wprintw(win, "%c", ch);
598 buffer[length++] = c0 = ch;
600 } else if (ok_digit(ch)) {
601 if (isalpha(UChar(c0))) {
602 wprintw(win, "%c", ch);
603 buffer[length++] = c0 = ch;
607 } else if (ch == ' ') {
608 if (isdigit(UChar(c0))) {
609 wprintw(win, "%c", ch);
610 buffer[length++] = c0 = ch;
623 wmove(stdscr, y0, x0);
625 buffer[length] = '\0';
626 if (log_out && length) {
627 fprintf(log_out, "%s\n", buffer);
629 return (length != 0);
633 demo_panels(InitPanel myInit, FillPanel myFill)
636 PANEL *px[MAX_PANELS + 1];
639 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
643 memset(px, 0, sizeof(px));
645 while (get_command(px, buffer, sizeof(buffer))) {
646 int limit = strlen(buffer);
647 for (itmp = 0; itmp < limit; itmp += 3) {
648 do_panel(px, buffer + itmp, myFill);
653 for (itmp = 1; itmp <= MAX_PANELS; ++itmp) {
654 remove_panel(px, itmp);
662 static const char *const tbl[] =
664 "Usage: demo_panels [options]"
667 ," -i file read commands from file"
668 ," -o file record commands in file"
669 ," -m do not use colors"
670 #if USE_WIDEC_SUPPORT
671 ," -w use wide-characters in panels and background"
675 for (n = 0; n < SIZEOF(tbl); n++)
676 fprintf(stderr, "%s\n", tbl[n]);
677 ExitProgram(EXIT_FAILURE);
681 main(int argc, char *argv[])
684 bool monochrome = FALSE;
685 InitPanel myInit = init_panel;
686 FillPanel myFill = fill_panel;
688 while ((c = getopt(argc, argv, "i:o:mw")) != EOF) {
691 log_in = fopen(optarg, "r");
694 log_out = fopen(optarg, "w");
699 #if USE_WIDEC_SUPPORT
701 myInit = init_wide_panel;
702 myFill = fill_wide_panel;
713 keypad(stdscr, TRUE);
715 use_colors = monochrome ? FALSE : has_colors();
719 demo_panels(myInit, myFill);
725 ExitProgram(EXIT_SUCCESS);
731 printf("This program requires the curses panel library\n");
732 ExitProgram(EXIT_FAILURE);