blob: 6c8258915713c5fe5d60140d6f360a6b4b7ce8fd [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2020,2022 Thomas E. Dickey *
3 * Copyright 2003-2016,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29/*
micky3879b9f5e72025-07-08 18:04:53 -040030 * $Id: demo_panels.c,v 1.48 2022/12/04 00:40:11 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010031 *
32 * Demonstrate a variety of functions from the panel library.
33 */
34
35#include <test.priv.h>
36
37#if USE_LIBPANEL
38
39#include <panel.h>
40
41#define LAST_POS '@'
42#define TEMP_POS '>'
43
44typedef void (*InitPanel) (void);
45typedef void (*FillPanel) (PANEL *);
46
47static bool use_colors = FALSE;
48static bool unboxed = FALSE;
49static FILE *log_in;
50static FILE *log_out;
51
52static void
53close_input(void)
54{
55 if (log_in != 0) {
56 fclose(log_in);
57 log_in = 0;
58 }
59}
60
61static void
62close_output(void)
63{
64 if (log_out != 0) {
65 fclose(log_out);
66 log_out = 0;
67 }
68}
69
70static WINDOW *
71statusline(void)
72{
73 WINDOW *result = stdscr;
74
75 wmove(result, LINES - 1, 0);
76 wclrtoeol(result);
77 return result;
78}
79
80static void
81pflush(void)
82{
83 update_panels();
84 doupdate();
85}
86
87static void
88saywhat(NCURSES_CONST char *text)
89{
90 WINDOW *win = statusline();
91 if (text != 0 && *text != '\0') {
92 waddstr(win, text);
93 waddstr(win, "; ");
94 }
95 waddstr(win, "press any key to continue");
96}
97
98static void
99show_position(NCURSES_CONST char *text,
100 NCURSES_CONST char *also,
101 int which,
102 int ypos,
103 int xpos)
104{
105 WINDOW *win = statusline();
106
107 wprintw(win, "%s for panel %d now %d,%d%s", text, which, ypos, xpos, also);
108 wmove(stdscr, ypos, xpos);
109}
110
111static int
112get_position(NCURSES_CONST char *text,
113 NCURSES_CONST char *also,
114 int which,
115 int *xpos,
116 int *ypos)
117{
118 int result = 0;
119 int x1, y1;
120 char cmd;
121
122 getyx(stdscr, y1, x1);
123 (void) statusline();
124
125 show_position(text, also, which, y1, x1);
126
127 if (log_in != 0) {
128 if (fscanf(log_in, "%c%d,%d\n", &cmd, &y1, &x1) == 3) {
129 switch (cmd) {
130 case LAST_POS:
131 result = 1;
132 (void) wgetch(stdscr);
133 break;
134 case TEMP_POS:
135 result = 0;
136 wrefresh(stdscr);
137 napms(100);
138 break;
139 default:
140 result = -1;
141 break;
142 }
143 } else {
144 result = -1;
145 }
146 } else {
147
148 switch (wgetch(stdscr)) {
149 case QUIT:
150 case ESCAPE:
151 case ERR:
152 result = -1;
153 break;
154 case ' ':
155 result = 1;
156 break;
157 case KEY_UP:
158 if (y1 > 0) {
159 --y1;
160 } else {
161 beep();
162 }
163 break;
164 case KEY_DOWN:
165 if (y1 < getmaxy(stdscr)) {
166 ++y1;
167 } else {
168 beep();
169 }
170 break;
171 case KEY_LEFT:
172 if (x1 > 0) {
173 --x1;
174 } else {
175 beep();
176 }
177 break;
178 case KEY_RIGHT:
179 if (x1 < getmaxx(stdscr)) {
180 ++x1;
181 } else {
182 beep();
183 }
184 break;
185 }
186 }
187
188 wmove(stdscr, y1, x1);
189 *ypos = y1;
190 *xpos = x1;
191
192 if (result >= 0) {
193 if (log_out)
194 fprintf(log_out, "%c%d,%d\n",
195 ((result > 0)
196 ? LAST_POS
197 : TEMP_POS),
198 y1, x1);
199 }
200 return result;
201}
202
203static PANEL *
204mkpanel(short color, int rows, int cols, int tly, int tlx)
205{
206 WINDOW *win;
207 PANEL *pan = 0;
micky3879b9f5e72025-07-08 18:04:53 -0400208 char *userdata = typeMalloc(char, 6);
Steve Kondikae271bc2015-11-15 02:50:53 +0100209
210 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
211 keypad(win, TRUE);
212 if ((pan = new_panel(win)) == 0) {
213 delwin(win);
214 } else if (use_colors) {
215 short fg = (short) ((color == COLOR_BLUE)
216 ? COLOR_WHITE
217 : COLOR_BLACK);
218 short bg = color;
219
220 init_pair(color, fg, bg);
221 wbkgdset(win, (chtype) (COLOR_PAIR(color) | ' '));
222 } else if (!unboxed) {
223 wbkgdset(win, A_BOLD | ' ');
224 }
225 }
micky3879b9f5e72025-07-08 18:04:53 -0400226 _nc_SPRINTF(userdata, _nc_SLIMIT(4) "p%d", color % 8);
Steve Kondikae271bc2015-11-15 02:50:53 +0100227 set_panel_userptr(pan, (NCURSES_CONST void *) userdata);
228 return pan;
229}
230
231static void
micky3879b9f5e72025-07-08 18:04:53 -0400232my_remove_panel(PANEL **pans, int which)
Steve Kondikae271bc2015-11-15 02:50:53 +0100233{
234 if (pans[which] != 0) {
235 PANEL *pan = pans[which];
236 WINDOW *win = panel_window(pan);
237 char *user = (char *) panel_userptr(pan);
238
239 free(user);
240 del_panel(pan);
241 delwin(win);
242
243 pans[which] = 0;
244 }
245}
246
247#undef MIN
248#define MIN(a,b) ((a) < (b) ? (a) : (b))
249#define ABS(a) ((a) < 0 ? -(a) : (a))
250
251static void
micky3879b9f5e72025-07-08 18:04:53 -0400252my_create_panel(PANEL **pans, int which, FillPanel myFill)
Steve Kondikae271bc2015-11-15 02:50:53 +0100253{
Steve Kondikae271bc2015-11-15 02:50:53 +0100254 int code;
255 short pair = (short) which;
256 short fg = (short) ((pair == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK);
257 short bg = pair;
258 int x0, y0, x1, y1;
259
260 init_pair(pair, fg, bg);
261
262 /* remove the old panel, if any */
263 my_remove_panel(pans, which);
264
265 /* get the position of one corner */
266 wmove(stdscr, getmaxy(stdscr) / 2, getmaxx(stdscr) / 2);
267 getyx(stdscr, y0, x0);
268 while ((code = get_position("First corner", "", which, &x0, &y0)) == 0) {
269 ;
270 }
271
272 if (code > 0) {
273 char also[80];
micky3879b9f5e72025-07-08 18:04:53 -0400274 _nc_SPRINTF(also, _nc_SLIMIT(sizeof(also)) " (first %d,%d)", y0, x0);
Steve Kondikae271bc2015-11-15 02:50:53 +0100275 /* get the position of the opposite corner */
276 while ((code = get_position("Opposite corner",
277 also, which, &x1, &y1)) == 0) {
278 ;
279 }
280
281 if (code > 0) {
282 int tly = MIN(y0, y1);
283 int tlx = MIN(x0, x1);
micky3879b9f5e72025-07-08 18:04:53 -0400284 PANEL *pan = mkpanel(pair,
285 ABS(y1 - y0) + 1,
286 ABS(x1 - x0) + 1,
287 tly, tlx);
Steve Kondikae271bc2015-11-15 02:50:53 +0100288 /* finish */
289 myFill(pan);
290 pans[which] = pan;
291 pflush();
292 wmove(stdscr, y1, x1);
293 }
294 }
295}
296
297static void
micky3879b9f5e72025-07-08 18:04:53 -0400298my_move_panel(PANEL **pans, int which, bool continuous)
Steve Kondikae271bc2015-11-15 02:50:53 +0100299{
300 if (pans[which] != 0) {
301 int code;
302 int y0, x0;
303 int y1, x1;
304 WINDOW *win = panel_window(pans[which]);
305 char also[80];
306
307 getbegyx(win, y0, x0);
micky3879b9f5e72025-07-08 18:04:53 -0400308 _nc_SPRINTF(also, _nc_SLIMIT(sizeof(also)) " (start %d,%d)", y0, x0);
Steve Kondikae271bc2015-11-15 02:50:53 +0100309 wmove(stdscr, y0, x0);
310 while ((code = get_position("Move panel", also, which, &x1, &y1)) == 0) {
311 if (continuous) {
312 move_panel(pans[which], y1, x1);
313 pflush();
314 }
315 }
316 if (code > 0) {
317 move_panel(pans[which], y1, x1);
318 }
319 }
320}
321
322static void
micky3879b9f5e72025-07-08 18:04:53 -0400323my_resize_panel(PANEL **pans, int which, FillPanel myFill)
Steve Kondikae271bc2015-11-15 02:50:53 +0100324{
325 if (pans[which] != 0) {
326 int code;
327 int y0, x0;
328 int y1, x1;
329 WINDOW *win = panel_window(pans[which]);
330 char also[80];
331
332 getbegyx(win, y0, x0);
micky3879b9f5e72025-07-08 18:04:53 -0400333 _nc_SPRINTF(also, _nc_SLIMIT(sizeof(also)) " (start %d,%d)", y0, x0);
Steve Kondikae271bc2015-11-15 02:50:53 +0100334 wmove(stdscr, y0, x0);
335 while ((code = get_position("Resize panel",
336 also, which, &x1, &y1)) == 0) {
337 ;
338 }
339 if (code > 0) {
340 WINDOW *next = newwin(ABS(y1 - y0) + 1,
341 ABS(x1 - x0) + 1,
342 MIN(y0, y1),
343 MIN(x0, x1));
344 if (next != 0) {
345 keypad(next, TRUE);
346 if (use_colors) {
347 wbkgdset(next, (chtype) (COLOR_PAIR(which) | ' '));
348 } else if (!unboxed) {
349 wbkgdset(next, A_BOLD | ' ');
350 }
351 replace_panel(pans[which], next);
352 myFill(pans[which]);
353 delwin(win);
354 }
355 }
356 }
357}
358
359static void
360init_panel(void)
361{
362 register int y, x;
363
364 for (y = 0; y < LINES - 1; y++) {
365 for (x = 0; x < COLS; x++)
366 wprintw(stdscr, "%d", (y + x) % 10);
367 }
368}
369
370static void
micky3879b9f5e72025-07-08 18:04:53 -0400371fill_panel(PANEL *pan)
Steve Kondikae271bc2015-11-15 02:50:53 +0100372{
373 WINDOW *win = panel_window(pan);
374 const char *userptr = (const char *) panel_userptr(pan);
375 int num = (userptr && *userptr) ? userptr[1] : '?';
376 int y, x;
377
378 wmove(win, 1, 1);
379 wprintw(win, "-pan%c-", num);
380 wclrtoeol(win);
381 box(win, 0, 0);
382 for (y = 2; y < getmaxy(win) - 1; y++) {
383 for (x = 1; x < getmaxx(win) - 1; x++) {
384 wmove(win, y, x);
385 waddch(win, UChar(num));
386 }
387 }
388}
389
390static void
micky3879b9f5e72025-07-08 18:04:53 -0400391fill_unboxed(PANEL *pan)
Steve Kondikae271bc2015-11-15 02:50:53 +0100392{
393 WINDOW *win = panel_window(pan);
394 const char *userptr = (const char *) panel_userptr(pan);
395 int num = (userptr && *userptr) ? userptr[1] : '?';
396 int y, x;
397
398 for (y = 0; y < getmaxy(win); y++) {
399 for (x = 0; x < getmaxx(win); x++) {
400 wmove(win, y, x);
401 waddch(win, UChar(num));
402 }
403 }
404}
405
406#if USE_WIDEC_SUPPORT
407static void
408make_fullwidth_digit(cchar_t *target, int digit)
409{
410 wchar_t source[2];
411
412 source[0] = (wchar_t) (digit + 0xff10);
413 source[1] = 0;
414 setcchar(target, source, A_NORMAL, 0, 0);
415}
416
417static void
418init_wide_panel(void)
419{
420 int digit;
421 cchar_t temp[10];
422
423 for (digit = 0; digit < 10; ++digit)
424 make_fullwidth_digit(&temp[digit], digit);
425
426 do {
427 int y, x;
428 getyx(stdscr, y, x);
429 digit = (y + x / 2) % 10;
430 } while (add_wch(&temp[digit]) != ERR);
431}
432
433static void
micky3879b9f5e72025-07-08 18:04:53 -0400434fill_wide_panel(PANEL *pan)
Steve Kondikae271bc2015-11-15 02:50:53 +0100435{
436 WINDOW *win = panel_window(pan);
437 int num = ((const char *) panel_userptr(pan))[1];
438 int y, x;
439
440 wmove(win, 1, 1);
441 wprintw(win, "-pan%c-", num);
442 wclrtoeol(win);
443 box(win, 0, 0);
444 for (y = 2; y < getmaxy(win) - 1; y++) {
445 for (x = 1; x < getmaxx(win) - 1; x++) {
446 wmove(win, y, x);
447 waddch(win, UChar(num));
448 }
449 }
450}
451#endif
452
453#define MAX_PANELS 5
454
455static int
micky3879b9f5e72025-07-08 18:04:53 -0400456which_panel(PANEL *px[MAX_PANELS + 1], PANEL *pan)
Steve Kondikae271bc2015-11-15 02:50:53 +0100457{
458 int result = 0;
459 int j;
460
461 for (j = 1; j <= MAX_PANELS; ++j) {
462 if (px[j] == pan) {
463 result = j;
464 break;
465 }
466 }
467 return result;
468}
469
470static void
micky3879b9f5e72025-07-08 18:04:53 -0400471show_help(WINDOW *win)
Steve Kondikae271bc2015-11-15 02:50:53 +0100472{
473 static const char *help[] =
474 {
475 "",
476 "Commands are letter/digit pairs. Digits are the panel number.",
477 "",
478 " b - put the panel on the bottom of the stack",
479 " c - create the panel",
480 " d - delete the panel",
481 " h - hide the panel",
482 " m - move the panel (M for continuous move)",
483 " r - resize the panel",
484 " s - show the panel",
micky3879b9f5e72025-07-08 18:04:53 -0400485 " t - put the panel on the top of the stack"
Steve Kondikae271bc2015-11-15 02:50:53 +0100486 };
micky3879b9f5e72025-07-08 18:04:53 -0400487 int j;
Steve Kondikae271bc2015-11-15 02:50:53 +0100488
micky3879b9f5e72025-07-08 18:04:53 -0400489 for (j = 0; j < (int) SIZEOF(help); ++j) {
490 if (wprintw(win, "%s\n", help[j]) == ERR)
491 break;
492 }
493}
494
495static void
496show_panels(PANEL *px[MAX_PANELS + 1])
497{
Steve Kondikae271bc2015-11-15 02:50:53 +0100498 struct {
499 bool valid;
500 bool hidden;
501 PANEL *above;
502 PANEL *below;
503 } table[MAX_PANELS + 1];
504
505 WINDOW *win;
Steve Kondikae271bc2015-11-15 02:50:53 +0100506 int j;
507
508 memset(table, 0, sizeof(table));
509 for (j = 1; j <= MAX_PANELS; ++j) {
510 table[j].valid = (px[j] != 0);
511 if (table[j].valid) {
512 table[j].hidden = panel_hidden(px[j]);
513 table[j].above = panel_above(px[j]);
514 table[j].below = panel_below(px[j]);
515 }
516 }
517
518 if ((win = newwin(LINES - 1, COLS, 0, 0)) != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400519 PANEL *pan;
520
Steve Kondikae271bc2015-11-15 02:50:53 +0100521 keypad(win, TRUE);
522 if ((pan = new_panel(win)) != 0) {
523 werase(win);
524 MvWPrintw(win, 0, 0, "Panels:\n");
525 for (j = 1; j <= MAX_PANELS; ++j) {
526 if (table[j].valid) {
527 wprintw(win, " %d:", j);
528 if (table[j].hidden) {
529 waddstr(win, " hidden");
530 } else {
531 if (table[j].above) {
532 wprintw(win, " above %d",
533 which_panel(px, table[j].above));
534 }
535 if (table[j].below) {
536 wprintw(win, "%s below %d",
537 table[j].above ? "," : "",
538 which_panel(px, table[j].below));
539 }
540 }
541 waddch(win, '\n');
542 }
543 }
micky3879b9f5e72025-07-08 18:04:53 -0400544 show_help(win);
Steve Kondikae271bc2015-11-15 02:50:53 +0100545 wgetch(win);
546 del_panel(pan);
547 pflush();
548 }
549 delwin(win);
550 }
551}
552
553#define wrapper(func) \
554static int my_##func(PANEL *pan) \
555{ \
556 int code = ERR; \
557 if (pan != 0) { \
558 code = func(pan); \
559 } \
560 return code; \
561}
562/* *INDENT-OFF* */
563wrapper(bottom_panel)
564wrapper(hide_panel)
565wrapper(show_panel)
566wrapper(top_panel)
567/* *INDENT-ON* */
568
569static void
micky3879b9f5e72025-07-08 18:04:53 -0400570do_panel(PANEL *px[MAX_PANELS + 1],
Steve Kondikae271bc2015-11-15 02:50:53 +0100571 NCURSES_CONST char *cmd,
572 FillPanel myFill)
573{
574 int which = cmd[1] - '0';
575
576 if (which < 1 || which > MAX_PANELS) {
577 beep();
578 return;
579 }
580
581 if (log_in != 0) {
582 pflush();
583 }
584
585 saywhat(cmd);
586 switch (*cmd) {
587 case 'b':
588 my_bottom_panel(px[which]);
589 break;
590 case 'c':
591 my_create_panel(px, which, myFill);
592 break;
593 case 'd':
594 my_remove_panel(px, which);
595 break;
596 case 'h':
597 my_hide_panel(px[which]);
598 break;
599 case 'm':
600 my_move_panel(px, which, FALSE);
601 break;
602 case 'M':
603 my_move_panel(px, which, TRUE);
604 break;
605 case 'r':
606 my_resize_panel(px, which, myFill);
607 break;
608 case 's':
609 my_show_panel(px[which]);
610 break;
611 case 't':
612 my_top_panel(px[which]);
613 break;
614 }
615}
616
617static bool
618ok_letter(int ch)
619{
620 return isalpha(UChar(ch)) && strchr("bcdhmMrst", ch) != 0;
621}
622
623static bool
624ok_digit(int ch)
625{
626 return isdigit(UChar(ch)) && (ch >= '1') && (ch - '0' <= MAX_PANELS);
627}
628
629/*
630 * A command consists of one or more letter/digit pairs separated by a space.
631 * Digits are limited to 1..MAX_PANELS.
632 *
633 * End the command with a newline. Reject other characters.
634 */
635static bool
micky3879b9f5e72025-07-08 18:04:53 -0400636get_command(PANEL *px[MAX_PANELS + 1], char *buffer, int limit)
Steve Kondikae271bc2015-11-15 02:50:53 +0100637{
638 int length = 0;
639 int y0, x0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100640 WINDOW *win;
641
642 getyx(stdscr, y0, x0);
643 win = statusline();
644 waddstr(win, "Command:");
645 buffer[length = 0] = '\0';
646
647 if (log_in != 0) {
648 if (fgets(buffer, limit - 3, log_in) != 0) {
649 length = (int) strlen(buffer);
650 while (length > 0 && isspace(UChar(buffer[length - 1])))
651 buffer[--length] = '\0';
652 waddstr(win, buffer);
653 } else {
654 close_input();
655 }
656 (void) wgetch(win);
657 } else {
micky3879b9f5e72025-07-08 18:04:53 -0400658 int c0 = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100659 for (;;) {
micky3879b9f5e72025-07-08 18:04:53 -0400660 int ch = wgetch(win);
Steve Kondikae271bc2015-11-15 02:50:53 +0100661 if (ch == ERR || ch == QUIT || ch == ESCAPE) {
662 buffer[0] = '\0';
663 break;
664 } else if (ch == CTRL('L')) {
665 wrefresh(curscr);
666 } else if (ch == '\n' || ch == KEY_ENTER) {
667 break;
micky3879b9f5e72025-07-08 18:04:53 -0400668 } else if (ch == HELP_KEY_1) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100669 show_panels(px);
670 } else if (length + 3 < limit) {
671 if (ch >= KEY_MIN) {
672 beep();
673 } else if (ok_letter(UChar(ch))) {
674 if (isalpha(UChar(c0))) {
675 beep();
676 } else if (isdigit(UChar(c0))) {
677 wprintw(win, " %c", ch);
678 buffer[length++] = ' ';
679 buffer[length++] = (char) (c0 = ch);
680 } else {
681 wprintw(win, "%c", ch);
682 buffer[length++] = (char) (c0 = ch);
683 }
684 } else if (ok_digit(ch)) {
685 if (isalpha(UChar(c0))) {
686 wprintw(win, "%c", ch);
687 buffer[length++] = (char) (c0 = ch);
688 } else {
689 beep();
690 }
691 } else if (ch == ' ') {
692 if (isdigit(UChar(c0))) {
693 wprintw(win, "%c", ch);
694 buffer[length++] = (char) (c0 = ch);
695 } else {
696 beep();
697 }
698 } else {
699 beep();
700 }
701 } else {
702 beep();
703 }
704 }
705 }
706
707 wmove(stdscr, y0, x0);
708
709 buffer[length] = '\0';
710 if (log_out && length) {
711 fprintf(log_out, "%s\n", buffer);
712 }
713 return (length != 0);
714}
715
716static void
717demo_panels(InitPanel myInit, FillPanel myFill)
718{
719 int itmp;
720 PANEL *px[MAX_PANELS + 1];
721 char buffer[BUFSIZ];
722
723 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
724 refresh();
725
726 myInit();
727 memset(px, 0, sizeof(px));
728
729 while (get_command(px, buffer, sizeof(buffer))) {
730 int limit = (int) strlen(buffer);
731 for (itmp = 0; itmp < limit; itmp += 3) {
732 do_panel(px, buffer + itmp, myFill);
733 }
734 pflush();
735 }
736#if NO_LEAKS
737 for (itmp = 1; itmp <= MAX_PANELS; ++itmp) {
738 my_remove_panel(px, itmp);
739 }
740#endif
741}
742
743static void
micky3879b9f5e72025-07-08 18:04:53 -0400744usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +0100745{
746 static const char *const tbl[] =
747 {
748 "Usage: demo_panels [options]"
749 ,""
micky3879b9f5e72025-07-08 18:04:53 -0400750 ,USAGE_COMMON
Steve Kondikae271bc2015-11-15 02:50:53 +0100751 ,"Options:"
micky3879b9f5e72025-07-08 18:04:53 -0400752 ," -i file read commands from file"
753 ," -o file record commands in file"
754 ," -m do not use colors"
Steve Kondikae271bc2015-11-15 02:50:53 +0100755#if USE_WIDEC_SUPPORT
micky3879b9f5e72025-07-08 18:04:53 -0400756 ," -w use wide-characters in panels and background"
Steve Kondikae271bc2015-11-15 02:50:53 +0100757#endif
micky3879b9f5e72025-07-08 18:04:53 -0400758 ," -x do not enclose panels in boxes"
Steve Kondikae271bc2015-11-15 02:50:53 +0100759 };
760 size_t n;
761 for (n = 0; n < SIZEOF(tbl); n++)
762 fprintf(stderr, "%s\n", tbl[n]);
micky3879b9f5e72025-07-08 18:04:53 -0400763 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100764}
micky3879b9f5e72025-07-08 18:04:53 -0400765/* *INDENT-OFF* */
766VERSION_COMMON()
767/* *INDENT-ON* */
Steve Kondikae271bc2015-11-15 02:50:53 +0100768
769int
770main(int argc, char *argv[])
771{
micky3879b9f5e72025-07-08 18:04:53 -0400772 int ch;
Steve Kondikae271bc2015-11-15 02:50:53 +0100773 bool monochrome = FALSE;
774 InitPanel myInit = init_panel;
775 FillPanel myFill = fill_panel;
776
777 setlocale(LC_ALL, "");
778
micky3879b9f5e72025-07-08 18:04:53 -0400779 while ((ch = getopt(argc, argv, OPTS_COMMON "i:o:mwx")) != -1) {
780 switch (ch) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100781 case 'i':
782 log_in = fopen(optarg, "r");
783 break;
784 case 'o':
785 log_out = fopen(optarg, "w");
786 break;
787 case 'm':
788 monochrome = TRUE;
789 break;
790#if USE_WIDEC_SUPPORT
791 case 'w':
792 myInit = init_wide_panel;
793 myFill = fill_wide_panel;
794 break;
795#endif
796 case 'x':
797 unboxed = TRUE;
798 break;
micky3879b9f5e72025-07-08 18:04:53 -0400799 case OPTS_VERSION:
800 show_version(argv);
801 ExitProgram(EXIT_SUCCESS);
Steve Kondikae271bc2015-11-15 02:50:53 +0100802 default:
micky3879b9f5e72025-07-08 18:04:53 -0400803 usage(ch == OPTS_USAGE);
804 /* NOTREACHED */
Steve Kondikae271bc2015-11-15 02:50:53 +0100805 }
806 }
807 if (unboxed)
808 myFill = fill_unboxed;
809
810 initscr();
811 cbreak();
812 noecho();
813 keypad(stdscr, TRUE);
814
815 use_colors = monochrome ? FALSE : has_colors();
816 if (use_colors)
817 start_color();
818
819 demo_panels(myInit, myFill);
820 endwin();
821
822 close_input();
823 close_output();
824
825 ExitProgram(EXIT_SUCCESS);
826}
827#else
828int
829main(void)
830{
831 printf("This program requires the curses panel library\n");
832 ExitProgram(EXIT_FAILURE);
833}
834#endif