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.5 2007/05/26 22:35:46 tom Exp $
31 * Demonstrate a variety of functions from the panel library.
32 * Thomas Dickey - 2003/4/26
41 #include <test.priv.h>
47 typedef void (*InitPanel) (void);
48 typedef void (*FillPanel) (PANEL *);
50 static bool use_colors;
52 static NCURSES_CONST char *mod[] =
62 /*+-------------------------------------------------------------------------
64 --------------------------------------------------------------------------*/
66 saywhat(NCURSES_CONST char *text)
68 wmove(stdscr, LINES - 1, 0);
70 if (text != 0 && *text != '\0') {
71 waddstr(stdscr, text);
72 waddstr(stdscr, "; ");
74 waddstr(stdscr, "press any key to continue");
75 } /* end of saywhat */
78 mkpanel(short color, int rows, int cols, int tly, int tlx)
82 char *userdata = malloc(3);
84 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
85 if ((pan = new_panel(win)) == 0) {
87 } else if (use_colors) {
88 short fg = (color == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
91 init_pair(color, fg, bg);
92 wbkgdset(win, (chtype) (COLOR_PAIR(color) | ' '));
94 wbkgdset(win, A_BOLD | ' ');
97 sprintf(userdata, "p%d", color % 8);
98 set_panel_userptr(pan, (NCURSES_CONST void *) userdata);
102 /*+-------------------------------------------------------------------------
104 --------------------------------------------------------------------------*/
108 WINDOW *win = panel_window(pan);
111 } /* end of rmpanel */
113 /*+-------------------------------------------------------------------------
115 --------------------------------------------------------------------------*/
121 } /* end of pflush */
123 /*+-------------------------------------------------------------------------
125 --------------------------------------------------------------------------*/
131 for (y = 0; y < LINES - 1; y++) {
132 for (x = 0; x < COLS; x++)
133 wprintw(stdscr, "%d", (y + x) % 10);
138 fill_panel(PANEL * pan)
140 WINDOW *win = panel_window(pan);
141 int num = ((const char *) panel_userptr(pan))[1];
145 wprintw(win, "-pan%c-", num);
148 for (y = 2; y < getmaxy(win) - 1; y++) {
149 for (x = 1; x < getmaxx(win) - 1; x++) {
151 waddch(win, UChar(num));
156 #if USE_WIDEC_SUPPORT
158 make_fullwidth_digit(cchar_t *target, int digit)
162 source[0] = digit + 0xff10;
164 setcchar(target, source, A_NORMAL, 0, 0);
168 init_wide_panel(void)
173 for (digit = 0; digit < 10; ++digit)
174 make_fullwidth_digit(&temp[digit], digit);
179 digit = (y + x / 2) % 10;
180 } while (add_wch(&temp[digit]) != ERR);
184 fill_wide_panel(PANEL * pan)
186 WINDOW *win = panel_window(pan);
187 int num = ((const char *) panel_userptr(pan))[1];
191 wprintw(win, "-pan%c-", num);
194 for (y = 2; y < getmaxy(win) - 1; y++) {
195 for (x = 1; x < getmaxx(win) - 1; x++) {
197 waddch(win, UChar(num));
206 canned_panel(PANEL * px[MAX_PANELS + 1], NCURSES_CONST char *cmd)
208 int which = cmd[1] - '0';
213 hide_panel(px[which]);
216 show_panel(px[which]);
219 top_panel(px[which]);
222 bottom_panel(px[which]);
233 demo_panels(void (*myInit) (void), void (*myFill) (PANEL *))
236 PANEL *px[MAX_PANELS + 1];
238 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
242 for (itmp = 1; itmp <= MAX_PANELS; ++itmp)
245 px[1] = mkpanel(COLOR_RED,
251 px[2] = mkpanel(COLOR_GREEN,
257 px[3] = mkpanel(COLOR_YELLOW,
263 px[4] = mkpanel(COLOR_BLUE,
269 px[5] = mkpanel(COLOR_MAGENTA,
287 saywhat("h3 s1 s2 s4 s5");
288 move_panel(px[1], 0, 0);
297 canned_panel(px, "s1");
298 canned_panel(px, "s2");
301 move_panel(px[2], LINES / 3 + 1, COLS / 8);
305 canned_panel(px, "s3");
308 move_panel(px[3], LINES / 4 + 1, COLS / 15);
312 canned_panel(px, "b3");
313 canned_panel(px, "s4");
314 canned_panel(px, "s5");
315 canned_panel(px, "t3");
316 canned_panel(px, "t1");
317 canned_panel(px, "t2");
318 canned_panel(px, "t3");
319 canned_panel(px, "t4");
321 for (itmp = 0; itmp < 6; itmp++) {
322 WINDOW *w4 = panel_window(px[4]);
323 WINDOW *w5 = panel_window(px[5]);
326 wmove(w4, LINES / 8, 1);
327 waddstr(w4, mod[itmp]);
328 move_panel(px[4], LINES / 6, itmp * (COLS / 8));
329 wmove(w5, LINES / 6, 1);
330 waddstr(w5, mod[itmp]);
335 wmove(w4, LINES / 6, 1);
336 waddstr(w4, mod[itmp]);
337 move_panel(px[5], LINES / 3 - 1, (itmp * 10) + 6);
338 wmove(w5, LINES / 8, 1);
339 waddstr(w5, mod[itmp]);
345 move_panel(px[4], LINES / 6, itmp * (COLS / 8));
349 canned_panel(px, "t5");
350 canned_panel(px, "t2");
351 canned_panel(px, "t1");
352 canned_panel(px, "d2");
353 canned_panel(px, "h3");
354 canned_panel(px, "d1");
355 canned_panel(px, "d4");
356 canned_panel(px, "d5");
357 canned_panel(px, "d3");
368 static const char *const tbl[] =
370 "Usage: demo_panels [options]"
373 ," -m do not use colors"
374 #if USE_WIDEC_SUPPORT
375 ," -w use wide-characters in panels and background"
379 for (n = 0; n < SIZEOF(tbl); n++)
380 fprintf(stderr, "%s\n", tbl[n]);
381 ExitProgram(EXIT_FAILURE);
385 main(int argc, char *argv[])
388 bool monochrome = FALSE;
389 InitPanel myInit = init_panel;
390 FillPanel myFill = fill_panel;
392 while ((c = getopt(argc, argv, "mw")) != EOF) {
397 #if USE_WIDEC_SUPPORT
399 myInit = init_wide_panel;
400 myFill = fill_wide_panel;
410 use_colors = monochrome ? FALSE : has_colors();
414 demo_panels(myInit, myFill);
423 printf("This program requires the curses panel library\n");
424 ExitProgram(EXIT_FAILURE);