]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_panels.c
ncurses 6.1 - patch 20191207
[ncurses.git] / test / demo_panels.c
1 /****************************************************************************
2  * Copyright (c) 2007-2018,2019 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.44 2019/08/31 23:24:11 tom Exp $
30  *
31  * Demonstrate a variety of functions from the panel library.
32  */
33
34 #include <test.priv.h>
35
36 #if USE_LIBPANEL
37
38 #include <panel.h>
39
40 #define LAST_POS '@'
41 #define TEMP_POS '>'
42
43 typedef void (*InitPanel) (void);
44 typedef void (*FillPanel) (PANEL *);
45
46 static bool use_colors = FALSE;
47 static bool unboxed = FALSE;
48 static FILE *log_in;
49 static FILE *log_out;
50
51 static void
52 close_input(void)
53 {
54     if (log_in != 0) {
55         fclose(log_in);
56         log_in = 0;
57     }
58 }
59
60 static void
61 close_output(void)
62 {
63     if (log_out != 0) {
64         fclose(log_out);
65         log_out = 0;
66     }
67 }
68
69 static WINDOW *
70 statusline(void)
71 {
72     WINDOW *result = stdscr;
73
74     wmove(result, LINES - 1, 0);
75     wclrtoeol(result);
76     return result;
77 }
78
79 static void
80 pflush(void)
81 {
82     update_panels();
83     doupdate();
84 }
85
86 static void
87 saywhat(NCURSES_CONST char *text)
88 {
89     WINDOW *win = statusline();
90     if (text != 0 && *text != '\0') {
91         waddstr(win, text);
92         waddstr(win, "; ");
93     }
94     waddstr(win, "press any key to continue");
95 }
96
97 static void
98 show_position(NCURSES_CONST char *text,
99               NCURSES_CONST char *also,
100               int which,
101               int ypos,
102               int xpos)
103 {
104     WINDOW *win = statusline();
105
106     wprintw(win, "%s for panel %d now %d,%d%s", text, which, ypos, xpos, also);
107     wmove(stdscr, ypos, xpos);
108 }
109
110 static int
111 get_position(NCURSES_CONST char *text,
112              NCURSES_CONST char *also,
113              int which,
114              int *xpos,
115              int *ypos)
116 {
117     int result = 0;
118     int x1, y1;
119     char cmd;
120
121     getyx(stdscr, y1, x1);
122     (void) statusline();
123
124     show_position(text, also, which, y1, x1);
125
126     if (log_in != 0) {
127         if (fscanf(log_in, "%c%d,%d\n", &cmd, &y1, &x1) == 3) {
128             switch (cmd) {
129             case LAST_POS:
130                 result = 1;
131                 (void) wgetch(stdscr);
132                 break;
133             case TEMP_POS:
134                 result = 0;
135                 wrefresh(stdscr);
136                 napms(100);
137                 break;
138             default:
139                 result = -1;
140                 break;
141             }
142         } else {
143             result = -1;
144         }
145     } else {
146
147         switch (wgetch(stdscr)) {
148         case QUIT:
149         case ESCAPE:
150         case ERR:
151             result = -1;
152             break;
153         case ' ':
154             result = 1;
155             break;
156         case KEY_UP:
157             if (y1 > 0) {
158                 --y1;
159             } else {
160                 beep();
161             }
162             break;
163         case KEY_DOWN:
164             if (y1 < getmaxy(stdscr)) {
165                 ++y1;
166             } else {
167                 beep();
168             }
169             break;
170         case KEY_LEFT:
171             if (x1 > 0) {
172                 --x1;
173             } else {
174                 beep();
175             }
176             break;
177         case KEY_RIGHT:
178             if (x1 < getmaxx(stdscr)) {
179                 ++x1;
180             } else {
181                 beep();
182             }
183             break;
184         }
185     }
186
187     wmove(stdscr, y1, x1);
188     *ypos = y1;
189     *xpos = x1;
190
191     if (result >= 0) {
192         if (log_out)
193             fprintf(log_out, "%c%d,%d\n",
194                     ((result > 0)
195                      ? LAST_POS
196                      : TEMP_POS),
197                     y1, x1);
198     }
199     return result;
200 }
201
202 static PANEL *
203 mkpanel(short color, int rows, int cols, int tly, int tlx)
204 {
205     WINDOW *win;
206     PANEL *pan = 0;
207     char *userdata = typeMalloc(char, 6);
208
209     if ((win = newwin(rows, cols, tly, tlx)) != 0) {
210         keypad(win, TRUE);
211         if ((pan = new_panel(win)) == 0) {
212             delwin(win);
213         } else if (use_colors) {
214             short fg = (short) ((color == COLOR_BLUE)
215                                 ? COLOR_WHITE
216                                 : COLOR_BLACK);
217             short bg = color;
218
219             init_pair(color, fg, bg);
220             wbkgdset(win, (chtype) (COLOR_PAIR(color) | ' '));
221         } else if (!unboxed) {
222             wbkgdset(win, A_BOLD | ' ');
223         }
224     }
225     _nc_SPRINTF(userdata, _nc_SLIMIT(4) "p%d", color % 8);
226     set_panel_userptr(pan, (NCURSES_CONST void *) userdata);
227     return pan;
228 }
229
230 static void
231 my_remove_panel(PANEL **pans, int which)
232 {
233     if (pans[which] != 0) {
234         PANEL *pan = pans[which];
235         WINDOW *win = panel_window(pan);
236         char *user = (char *) panel_userptr(pan);
237
238         free(user);
239         del_panel(pan);
240         delwin(win);
241
242         pans[which] = 0;
243     }
244 }
245
246 #undef MIN
247 #define MIN(a,b) ((a) < (b) ? (a) : (b))
248 #define ABS(a)   ((a) < 0 ? -(a) : (a))
249
250 static void
251 my_create_panel(PANEL **pans, int which, FillPanel myFill)
252 {
253     int code;
254     short pair = (short) which;
255     short fg = (short) ((pair == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK);
256     short bg = pair;
257     int x0, y0, x1, y1;
258
259     init_pair(pair, fg, bg);
260
261     /* remove the old panel, if any */
262     my_remove_panel(pans, which);
263
264     /* get the position of one corner */
265     wmove(stdscr, getmaxy(stdscr) / 2, getmaxx(stdscr) / 2);
266     getyx(stdscr, y0, x0);
267     while ((code = get_position("First corner", "", which, &x0, &y0)) == 0) {
268         ;
269     }
270
271     if (code > 0) {
272         char also[80];
273         _nc_SPRINTF(also, _nc_SLIMIT(sizeof(also)) " (first %d,%d)", y0, x0);
274         /* get the position of the opposite corner */
275         while ((code = get_position("Opposite corner",
276                                     also, which, &x1, &y1)) == 0) {
277             ;
278         }
279
280         if (code > 0) {
281             int tly = MIN(y0, y1);
282             int tlx = MIN(x0, x1);
283             PANEL *pan = mkpanel(pair,
284                                  ABS(y1 - y0) + 1,
285                                  ABS(x1 - x0) + 1,
286                                  tly, tlx);
287             /* finish */
288             myFill(pan);
289             pans[which] = pan;
290             pflush();
291             wmove(stdscr, y1, x1);
292         }
293     }
294 }
295
296 static void
297 my_move_panel(PANEL **pans, int which, bool continuous)
298 {
299     if (pans[which] != 0) {
300         int code;
301         int y0, x0;
302         int y1, x1;
303         WINDOW *win = panel_window(pans[which]);
304         char also[80];
305
306         getbegyx(win, y0, x0);
307         _nc_SPRINTF(also, _nc_SLIMIT(sizeof(also)) " (start %d,%d)", y0, x0);
308         wmove(stdscr, y0, x0);
309         while ((code = get_position("Move panel", also, which, &x1, &y1)) == 0) {
310             if (continuous) {
311                 move_panel(pans[which], y1, x1);
312                 pflush();
313             }
314         }
315         if (code > 0) {
316             move_panel(pans[which], y1, x1);
317         }
318     }
319 }
320
321 static void
322 my_resize_panel(PANEL **pans, int which, FillPanel myFill)
323 {
324     if (pans[which] != 0) {
325         int code;
326         int y0, x0;
327         int y1, x1;
328         WINDOW *win = panel_window(pans[which]);
329         char also[80];
330
331         getbegyx(win, y0, x0);
332         _nc_SPRINTF(also, _nc_SLIMIT(sizeof(also)) " (start %d,%d)", y0, x0);
333         wmove(stdscr, y0, x0);
334         while ((code = get_position("Resize panel",
335                                     also, which, &x1, &y1)) == 0) {
336             ;
337         }
338         if (code > 0) {
339             WINDOW *next = newwin(ABS(y1 - y0) + 1,
340                                   ABS(x1 - x0) + 1,
341                                   MIN(y0, y1),
342                                   MIN(x0, x1));
343             if (next != 0) {
344                 keypad(next, TRUE);
345                 if (use_colors) {
346                     wbkgdset(next, (chtype) (COLOR_PAIR(which) | ' '));
347                 } else if (!unboxed) {
348                     wbkgdset(next, A_BOLD | ' ');
349                 }
350                 replace_panel(pans[which], next);
351                 myFill(pans[which]);
352                 delwin(win);
353             }
354         }
355     }
356 }
357
358 static void
359 init_panel(void)
360 {
361     register int y, x;
362
363     for (y = 0; y < LINES - 1; y++) {
364         for (x = 0; x < COLS; x++)
365             wprintw(stdscr, "%d", (y + x) % 10);
366     }
367 }
368
369 static void
370 fill_panel(PANEL *pan)
371 {
372     WINDOW *win = panel_window(pan);
373     const char *userptr = (const char *) panel_userptr(pan);
374     int num = (userptr && *userptr) ? userptr[1] : '?';
375     int y, x;
376
377     wmove(win, 1, 1);
378     wprintw(win, "-pan%c-", num);
379     wclrtoeol(win);
380     box(win, 0, 0);
381     for (y = 2; y < getmaxy(win) - 1; y++) {
382         for (x = 1; x < getmaxx(win) - 1; x++) {
383             wmove(win, y, x);
384             waddch(win, UChar(num));
385         }
386     }
387 }
388
389 static void
390 fill_unboxed(PANEL *pan)
391 {
392     WINDOW *win = panel_window(pan);
393     const char *userptr = (const char *) panel_userptr(pan);
394     int num = (userptr && *userptr) ? userptr[1] : '?';
395     int y, x;
396
397     for (y = 0; y < getmaxy(win); y++) {
398         for (x = 0; x < getmaxx(win); x++) {
399             wmove(win, y, x);
400             waddch(win, UChar(num));
401         }
402     }
403 }
404
405 #if USE_WIDEC_SUPPORT
406 static void
407 make_fullwidth_digit(cchar_t *target, int digit)
408 {
409     wchar_t source[2];
410
411     source[0] = (wchar_t) (digit + 0xff10);
412     source[1] = 0;
413     setcchar(target, source, A_NORMAL, 0, 0);
414 }
415
416 static void
417 init_wide_panel(void)
418 {
419     int digit;
420     cchar_t temp[10];
421
422     for (digit = 0; digit < 10; ++digit)
423         make_fullwidth_digit(&temp[digit], digit);
424
425     do {
426         int y, x;
427         getyx(stdscr, y, x);
428         digit = (y + x / 2) % 10;
429     } while (add_wch(&temp[digit]) != ERR);
430 }
431
432 static void
433 fill_wide_panel(PANEL *pan)
434 {
435     WINDOW *win = panel_window(pan);
436     int num = ((const char *) panel_userptr(pan))[1];
437     int y, x;
438
439     wmove(win, 1, 1);
440     wprintw(win, "-pan%c-", num);
441     wclrtoeol(win);
442     box(win, 0, 0);
443     for (y = 2; y < getmaxy(win) - 1; y++) {
444         for (x = 1; x < getmaxx(win) - 1; x++) {
445             wmove(win, y, x);
446             waddch(win, UChar(num));
447         }
448     }
449 }
450 #endif
451
452 #define MAX_PANELS 5
453
454 static int
455 which_panel(PANEL *px[MAX_PANELS + 1], PANEL *pan)
456 {
457     int result = 0;
458     int j;
459
460     for (j = 1; j <= MAX_PANELS; ++j) {
461         if (px[j] == pan) {
462             result = j;
463             break;
464         }
465     }
466     return result;
467 }
468
469 static void
470 show_help(WINDOW *win)
471 {
472     static const char *help[] =
473     {
474         "",
475         "Commands are letter/digit pairs.  Digits are the panel number.",
476         "",
477         "  b - put the panel on the bottom of the stack",
478         "  c - create the panel",
479         "  d - delete the panel",
480         "  h - hide the panel",
481         "  m - move the panel (M for continuous move)",
482         "  r - resize the panel",
483         "  s - show the panel",
484         "  t - put the panel on the top of the stack"
485     };
486     int j;
487
488     for (j = 0; j < (int) SIZEOF(help); ++j) {
489         if (wprintw(win, "%s\n", help[j]) == ERR)
490             break;
491     }
492 }
493
494 static void
495 show_panels(PANEL *px[MAX_PANELS + 1])
496 {
497     struct {
498         bool valid;
499         bool hidden;
500         PANEL *above;
501         PANEL *below;
502     } table[MAX_PANELS + 1];
503
504     WINDOW *win;
505     int j;
506
507     memset(table, 0, sizeof(table));
508     for (j = 1; j <= MAX_PANELS; ++j) {
509         table[j].valid = (px[j] != 0);
510         if (table[j].valid) {
511             table[j].hidden = panel_hidden(px[j]);
512             table[j].above = panel_above(px[j]);
513             table[j].below = panel_below(px[j]);
514         }
515     }
516
517     if ((win = newwin(LINES - 1, COLS, 0, 0)) != 0) {
518         PANEL *pan;
519
520         keypad(win, TRUE);
521         if ((pan = new_panel(win)) != 0) {
522             werase(win);
523             MvWPrintw(win, 0, 0, "Panels:\n");
524             for (j = 1; j <= MAX_PANELS; ++j) {
525                 if (table[j].valid) {
526                     wprintw(win, " %d:", j);
527                     if (table[j].hidden) {
528                         waddstr(win, " hidden");
529                     } else {
530                         if (table[j].above) {
531                             wprintw(win, " above %d",
532                                     which_panel(px, table[j].above));
533                         }
534                         if (table[j].below) {
535                             wprintw(win, "%s below %d",
536                                     table[j].above ? "," : "",
537                                     which_panel(px, table[j].below));
538                         }
539                     }
540                     waddch(win, '\n');
541                 }
542             }
543             show_help(win);
544             wgetch(win);
545             del_panel(pan);
546             pflush();
547         }
548         delwin(win);
549     }
550 }
551
552 #define wrapper(func) \
553 static int my_##func(PANEL *pan) \
554 { \
555     int code = ERR; \
556     if (pan != 0) { \
557         code = func(pan); \
558     } \
559     return code; \
560 }
561 /* *INDENT-OFF* */
562 wrapper(bottom_panel)
563 wrapper(hide_panel)
564 wrapper(show_panel)
565 wrapper(top_panel)
566 /* *INDENT-ON* */
567
568 static void
569 do_panel(PANEL *px[MAX_PANELS + 1],
570          NCURSES_CONST char *cmd,
571          FillPanel myFill)
572 {
573     int which = cmd[1] - '0';
574
575     if (which < 1 || which > MAX_PANELS) {
576         beep();
577         return;
578     }
579
580     if (log_in != 0) {
581         pflush();
582     }
583
584     saywhat(cmd);
585     switch (*cmd) {
586     case 'b':
587         my_bottom_panel(px[which]);
588         break;
589     case 'c':
590         my_create_panel(px, which, myFill);
591         break;
592     case 'd':
593         my_remove_panel(px, which);
594         break;
595     case 'h':
596         my_hide_panel(px[which]);
597         break;
598     case 'm':
599         my_move_panel(px, which, FALSE);
600         break;
601     case 'M':
602         my_move_panel(px, which, TRUE);
603         break;
604     case 'r':
605         my_resize_panel(px, which, myFill);
606         break;
607     case 's':
608         my_show_panel(px[which]);
609         break;
610     case 't':
611         my_top_panel(px[which]);
612         break;
613     }
614 }
615
616 static bool
617 ok_letter(int ch)
618 {
619     return isalpha(UChar(ch)) && strchr("bcdhmMrst", ch) != 0;
620 }
621
622 static bool
623 ok_digit(int ch)
624 {
625     return isdigit(UChar(ch)) && (ch >= '1') && (ch - '0' <= MAX_PANELS);
626 }
627
628 /*
629  * A command consists of one or more letter/digit pairs separated by a space.
630  * Digits are limited to 1..MAX_PANELS.
631  *
632  * End the command with a newline.  Reject other characters.
633  */
634 static bool
635 get_command(PANEL *px[MAX_PANELS + 1], char *buffer, int limit)
636 {
637     int length = 0;
638     int y0, x0;
639     WINDOW *win;
640
641     getyx(stdscr, y0, x0);
642     win = statusline();
643     waddstr(win, "Command:");
644     buffer[length = 0] = '\0';
645
646     if (log_in != 0) {
647         if (fgets(buffer, limit - 3, log_in) != 0) {
648             length = (int) strlen(buffer);
649             while (length > 0 && isspace(UChar(buffer[length - 1])))
650                 buffer[--length] = '\0';
651             waddstr(win, buffer);
652         } else {
653             close_input();
654         }
655         (void) wgetch(win);
656     } else {
657         int c0 = 0;
658         for (;;) {
659             int ch = wgetch(win);
660             if (ch == ERR || ch == QUIT || ch == ESCAPE) {
661                 buffer[0] = '\0';
662                 break;
663             } else if (ch == CTRL('L')) {
664                 wrefresh(curscr);
665             } else if (ch == '\n' || ch == KEY_ENTER) {
666                 break;
667             } else if (ch == HELP_KEY_1) {
668                 show_panels(px);
669             } else if (length + 3 < limit) {
670                 if (ch >= KEY_MIN) {
671                     beep();
672                 } else if (ok_letter(UChar(ch))) {
673                     if (isalpha(UChar(c0))) {
674                         beep();
675                     } else if (isdigit(UChar(c0))) {
676                         wprintw(win, " %c", ch);
677                         buffer[length++] = ' ';
678                         buffer[length++] = (char) (c0 = ch);
679                     } else {
680                         wprintw(win, "%c", ch);
681                         buffer[length++] = (char) (c0 = ch);
682                     }
683                 } else if (ok_digit(ch)) {
684                     if (isalpha(UChar(c0))) {
685                         wprintw(win, "%c", ch);
686                         buffer[length++] = (char) (c0 = ch);
687                     } else {
688                         beep();
689                     }
690                 } else if (ch == ' ') {
691                     if (isdigit(UChar(c0))) {
692                         wprintw(win, "%c", ch);
693                         buffer[length++] = (char) (c0 = ch);
694                     } else {
695                         beep();
696                     }
697                 } else {
698                     beep();
699                 }
700             } else {
701                 beep();
702             }
703         }
704     }
705
706     wmove(stdscr, y0, x0);
707
708     buffer[length] = '\0';
709     if (log_out && length) {
710         fprintf(log_out, "%s\n", buffer);
711     }
712     return (length != 0);
713 }
714
715 static void
716 demo_panels(InitPanel myInit, FillPanel myFill)
717 {
718     int itmp;
719     PANEL *px[MAX_PANELS + 1];
720     char buffer[BUFSIZ];
721
722     scrollok(stdscr, FALSE);    /* we don't want stdscr to scroll! */
723     refresh();
724
725     myInit();
726     memset(px, 0, sizeof(px));
727
728     while (get_command(px, buffer, sizeof(buffer))) {
729         int limit = (int) strlen(buffer);
730         for (itmp = 0; itmp < limit; itmp += 3) {
731             do_panel(px, buffer + itmp, myFill);
732         }
733         pflush();
734     }
735 #if NO_LEAKS
736     for (itmp = 1; itmp <= MAX_PANELS; ++itmp) {
737         my_remove_panel(px, itmp);
738     }
739 #endif
740 }
741
742 static void
743 usage(void)
744 {
745     static const char *const tbl[] =
746     {
747         "Usage: demo_panels [options]"
748         ,""
749         ,"Options:"
750         ,"  -i file  read commands from file"
751         ,"  -o file  record commands in file"
752         ,"  -m       do not use colors"
753 #if USE_WIDEC_SUPPORT
754         ,"  -w       use wide-characters in panels and background"
755 #endif
756         ,"  -x       do not enclose panels in boxes"
757     };
758     size_t n;
759     for (n = 0; n < SIZEOF(tbl); n++)
760         fprintf(stderr, "%s\n", tbl[n]);
761     ExitProgram(EXIT_FAILURE);
762 }
763
764 int
765 main(int argc, char *argv[])
766 {
767     int c;
768     bool monochrome = FALSE;
769     InitPanel myInit = init_panel;
770     FillPanel myFill = fill_panel;
771
772     setlocale(LC_ALL, "");
773
774     while ((c = getopt(argc, argv, "i:o:mwx")) != -1) {
775         switch (c) {
776         case 'i':
777             log_in = fopen(optarg, "r");
778             break;
779         case 'o':
780             log_out = fopen(optarg, "w");
781             break;
782         case 'm':
783             monochrome = TRUE;
784             break;
785 #if USE_WIDEC_SUPPORT
786         case 'w':
787             myInit = init_wide_panel;
788             myFill = fill_wide_panel;
789             break;
790 #endif
791         case 'x':
792             unboxed = TRUE;
793             break;
794         default:
795             usage();
796         }
797     }
798     if (unboxed)
799         myFill = fill_unboxed;
800
801     initscr();
802     cbreak();
803     noecho();
804     keypad(stdscr, TRUE);
805
806     use_colors = monochrome ? FALSE : has_colors();
807     if (use_colors)
808         start_color();
809
810     demo_panels(myInit, myFill);
811     endwin();
812
813     close_input();
814     close_output();
815
816     ExitProgram(EXIT_SUCCESS);
817 }
818 #else
819 int
820 main(void)
821 {
822     printf("This program requires the curses panel library\n");
823     ExitProgram(EXIT_FAILURE);
824 }
825 #endif