]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_panels.c
ncurses 5.6 - patch 20070526
[ncurses.git] / test / demo_panels.c
1 /****************************************************************************
2  * Copyright (c) 2007 Free Software Foundation, Inc.                        *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: demo_panels.c,v 1.5 2007/05/26 22:35:46 tom Exp $
30  *
31  * Demonstrate a variety of functions from the panel library.
32  * Thomas Dickey - 2003/4/26
33  */
34 /*
35 panel_above                     -
36 panel_below                     -
37 panel_hidden                    -
38 replace_panel                   -
39 */
40
41 #include <test.priv.h>
42
43 #if USE_LIBPANEL
44
45 #include <panel.h>
46
47 typedef void (*InitPanel) (void);
48 typedef void (*FillPanel) (PANEL *);
49
50 static bool use_colors;
51
52 static NCURSES_CONST char *mod[] =
53 {
54     "test ",
55     "TEST ",
56     "(**) ",
57     "*()* ",
58     "<--> ",
59     "LAST "
60 };
61
62 /*+-------------------------------------------------------------------------
63         saywhat(text)
64 --------------------------------------------------------------------------*/
65 static void
66 saywhat(NCURSES_CONST char *text)
67 {
68     wmove(stdscr, LINES - 1, 0);
69     wclrtoeol(stdscr);
70     if (text != 0 && *text != '\0') {
71         waddstr(stdscr, text);
72         waddstr(stdscr, "; ");
73     }
74     waddstr(stdscr, "press any key to continue");
75 }                               /* end of saywhat */
76
77 static PANEL *
78 mkpanel(short color, int rows, int cols, int tly, int tlx)
79 {
80     WINDOW *win;
81     PANEL *pan = 0;
82     char *userdata = malloc(3);
83
84     if ((win = newwin(rows, cols, tly, tlx)) != 0) {
85         if ((pan = new_panel(win)) == 0) {
86             delwin(win);
87         } else if (use_colors) {
88             short fg = (color == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
89             short bg = color;
90
91             init_pair(color, fg, bg);
92             wbkgdset(win, (chtype) (COLOR_PAIR(color) | ' '));
93         } else {
94             wbkgdset(win, A_BOLD | ' ');
95         }
96     }
97     sprintf(userdata, "p%d", color % 8);
98     set_panel_userptr(pan, (NCURSES_CONST void *) userdata);
99     return pan;
100 }
101
102 /*+-------------------------------------------------------------------------
103         rmpanel(pan)
104 --------------------------------------------------------------------------*/
105 static void
106 rmpanel(PANEL * pan)
107 {
108     WINDOW *win = panel_window(pan);
109     del_panel(pan);
110     delwin(win);
111 }                               /* end of rmpanel */
112
113 /*+-------------------------------------------------------------------------
114         pflush()
115 --------------------------------------------------------------------------*/
116 static void
117 pflush(void)
118 {
119     update_panels();
120     doupdate();
121 }                               /* end of pflush */
122
123 /*+-------------------------------------------------------------------------
124         fill_panel(win)
125 --------------------------------------------------------------------------*/
126 static void
127 init_panel(void)
128 {
129     register int y, x;
130
131     for (y = 0; y < LINES - 1; y++) {
132         for (x = 0; x < COLS; x++)
133             wprintw(stdscr, "%d", (y + x) % 10);
134     }
135 }
136
137 static void
138 fill_panel(PANEL * pan)
139 {
140     WINDOW *win = panel_window(pan);
141     int num = ((const char *) panel_userptr(pan))[1];
142     int y, x;
143
144     wmove(win, 1, 1);
145     wprintw(win, "-pan%c-", num);
146     wclrtoeol(win);
147     box(win, 0, 0);
148     for (y = 2; y < getmaxy(win) - 1; y++) {
149         for (x = 1; x < getmaxx(win) - 1; x++) {
150             wmove(win, y, x);
151             waddch(win, UChar(num));
152         }
153     }
154 }
155
156 #if USE_WIDEC_SUPPORT
157 static void
158 make_fullwidth_digit(cchar_t *target, int digit)
159 {
160     wchar_t source[2];
161
162     source[0] = digit + 0xff10;
163     source[1] = 0;
164     setcchar(target, source, A_NORMAL, 0, 0);
165 }
166
167 static void
168 init_wide_panel(void)
169 {
170     int digit;
171     cchar_t temp[10];
172
173     for (digit = 0; digit < 10; ++digit)
174         make_fullwidth_digit(&temp[digit], digit);
175
176     do {
177         int y, x;
178         getyx(stdscr, y, x);
179         digit = (y + x / 2) % 10;
180     } while (add_wch(&temp[digit]) != ERR);
181 }
182
183 static void
184 fill_wide_panel(PANEL * pan)
185 {
186     WINDOW *win = panel_window(pan);
187     int num = ((const char *) panel_userptr(pan))[1];
188     int y, x;
189
190     wmove(win, 1, 1);
191     wprintw(win, "-pan%c-", num);
192     wclrtoeol(win);
193     box(win, 0, 0);
194     for (y = 2; y < getmaxy(win) - 1; y++) {
195         for (x = 1; x < getmaxx(win) - 1; x++) {
196             wmove(win, y, x);
197             waddch(win, UChar(num));
198         }
199     }
200 }
201 #endif
202
203 #define MAX_PANELS 5
204
205 static void
206 canned_panel(PANEL * px[MAX_PANELS + 1], NCURSES_CONST char *cmd)
207 {
208     int which = cmd[1] - '0';
209
210     saywhat(cmd);
211     switch (*cmd) {
212     case 'h':
213         hide_panel(px[which]);
214         break;
215     case 's':
216         show_panel(px[which]);
217         break;
218     case 't':
219         top_panel(px[which]);
220         break;
221     case 'b':
222         bottom_panel(px[which]);
223         break;
224     case 'd':
225         rmpanel(px[which]);
226         break;
227     }
228     pflush();
229     wgetch(stdscr);
230 }
231
232 static void
233 demo_panels(void (*myInit) (void), void (*myFill) (PANEL *))
234 {
235     int itmp;
236     PANEL *px[MAX_PANELS + 1];
237
238     scrollok(stdscr, FALSE);    /* we don't want stdscr to scroll! */
239     refresh();
240
241     myInit();
242     for (itmp = 1; itmp <= MAX_PANELS; ++itmp)
243         px[itmp] = 0;
244
245     px[1] = mkpanel(COLOR_RED,
246                     LINES / 2 - 2,
247                     COLS / 8 + 1,
248                     0,
249                     0);
250
251     px[2] = mkpanel(COLOR_GREEN,
252                     LINES / 2 + 1,
253                     COLS / 7,
254                     LINES / 4,
255                     COLS / 10);
256
257     px[3] = mkpanel(COLOR_YELLOW,
258                     LINES / 4,
259                     COLS / 10,
260                     LINES / 2,
261                     COLS / 9);
262
263     px[4] = mkpanel(COLOR_BLUE,
264                     LINES / 2 - 2,
265                     COLS / 8,
266                     LINES / 2 - 2,
267                     COLS / 3);
268
269     px[5] = mkpanel(COLOR_MAGENTA,
270                     LINES / 2 - 2,
271                     COLS / 8,
272                     LINES / 2,
273                     COLS / 2 - 2);
274
275     myFill(px[1]);
276     myFill(px[2]);
277     myFill(px[3]);
278     myFill(px[4]);
279     myFill(px[5]);
280
281     hide_panel(px[4]);
282     hide_panel(px[5]);
283     pflush();
284     saywhat("");
285     wgetch(stdscr);
286
287     saywhat("h3 s1 s2 s4 s5");
288     move_panel(px[1], 0, 0);
289     hide_panel(px[3]);
290     show_panel(px[1]);
291     show_panel(px[2]);
292     show_panel(px[4]);
293     show_panel(px[5]);
294     pflush();
295     wgetch(stdscr);
296
297     canned_panel(px, "s1");
298     canned_panel(px, "s2");
299
300     saywhat("m2");
301     move_panel(px[2], LINES / 3 + 1, COLS / 8);
302     pflush();
303     wgetch(stdscr);
304
305     canned_panel(px, "s3");
306
307     saywhat("m3");
308     move_panel(px[3], LINES / 4 + 1, COLS / 15);
309     pflush();
310     wgetch(stdscr);
311
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");
320
321     for (itmp = 0; itmp < 6; itmp++) {
322         WINDOW *w4 = panel_window(px[4]);
323         WINDOW *w5 = panel_window(px[5]);
324
325         saywhat("m4");
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]);
331         pflush();
332         wgetch(stdscr);
333
334         saywhat("m5");
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]);
340         pflush();
341         wgetch(stdscr);
342     }
343
344     saywhat("m4");
345     move_panel(px[4], LINES / 6, itmp * (COLS / 8));
346     pflush();
347     wgetch(stdscr);
348
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");
358
359     wgetch(stdscr);
360
361     erase();
362     endwin();
363 }
364
365 static void
366 usage(void)
367 {
368     static const char *const tbl[] =
369     {
370         "Usage: demo_panels [options]"
371         ,""
372         ,"Options:"
373         ,"  -m       do not use colors"
374 #if USE_WIDEC_SUPPORT
375         ,"  -w       use wide-characters in panels and background"
376 #endif
377     };
378     size_t n;
379     for (n = 0; n < SIZEOF(tbl); n++)
380         fprintf(stderr, "%s\n", tbl[n]);
381     ExitProgram(EXIT_FAILURE);
382 }
383
384 int
385 main(int argc, char *argv[])
386 {
387     int c;
388     bool monochrome = FALSE;
389     InitPanel myInit = init_panel;
390     FillPanel myFill = fill_panel;
391
392     while ((c = getopt(argc, argv, "mw")) != EOF) {
393         switch (c) {
394         case 'm':
395             monochrome = TRUE;
396             break;
397 #if USE_WIDEC_SUPPORT
398         case 'w':
399             myInit = init_wide_panel;
400             myFill = fill_wide_panel;
401             break;
402 #endif
403         default:
404             usage();
405         }
406     }
407
408     initscr();
409
410     use_colors = monochrome ? FALSE : has_colors();
411     if (use_colors)
412         start_color();
413
414     demo_panels(myInit, myFill);
415     endwin();
416
417     return EXIT_SUCCESS;
418 }
419 #else
420 int
421 main(void)
422 {
423     printf("This program requires the curses panel library\n");
424     ExitProgram(EXIT_FAILURE);
425 }
426 #endif