]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_panels.c
396d76c60d0d985720b44a2927d68274c2941fbc
[ncurses.git] / test / demo_panels.c
1 /****************************************************************************
2  * Copyright (c) 2007,2008 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.31 2008/07/05 23:13:47 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     WINDOW *win;
121
122     getyx(stdscr, y1, x1);
123     win = 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
203 static PANEL *
204 mkpanel(short color, int rows, int cols, int tly, int tlx)
205 {
206     WINDOW *win;
207     PANEL *pan = 0;
208     char *userdata = typeMalloc(char, 3);
209
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 = ((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     }
226     sprintf(userdata, "p%d", color % 8);
227     set_panel_userptr(pan, (NCURSES_CONST void *) userdata);
228     return pan;
229 }
230
231 static void
232 my_remove_panel(PANEL ** pans, int which)
233 {
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
251 static void
252 my_create_panel(PANEL ** pans, int which, FillPanel myFill)
253 {
254     PANEL *pan = 0;
255     int code;
256     int pair = which;
257     short fg = (pair == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
258     short bg = pair;
259     int x0, y0, x1, y1;
260
261     init_pair(pair, fg, bg);
262
263     /* remove the old panel, if any */
264     my_remove_panel(pans, which);
265
266     /* get the position of one corner */
267     wmove(stdscr, getmaxy(stdscr) / 2, getmaxx(stdscr) / 2);
268     getyx(stdscr, y0, x0);
269     while ((code = get_position("First corner", "", which, &x0, &y0)) == 0) {
270         ;
271     }
272
273     if (code > 0) {
274         char also[80];
275         sprintf(also, " (first %d,%d)", y0, x0);
276         /* get the position of the opposite corner */
277         while ((code = get_position("Opposite corner",
278                                     also, which, &x1, &y1)) == 0) {
279             ;
280         }
281
282         if (code > 0) {
283             int tly = MIN(y0, y1);
284             int tlx = MIN(x0, x1);
285             pan = mkpanel(pair, ABS(y1 - y0) + 1, ABS(x1 - x0) + 1, tly, tlx);
286             /* finish */
287             myFill(pan);
288             pans[which] = pan;
289             pflush();
290             wmove(stdscr, y1, x1);
291         }
292     }
293 }
294
295 static void
296 my_move_panel(PANEL ** pans, int which, bool continuous)
297 {
298     if (pans[which] != 0) {
299         int code;
300         int y0, x0;
301         int y1, x1;
302         WINDOW *win = panel_window(pans[which]);
303         char also[80];
304
305         getbegyx(win, y0, x0);
306         sprintf(also, " (start %d,%d)", y0, x0);
307         wmove(stdscr, y0, x0);
308         while ((code = get_position("Move panel", also, which, &x1, &y1)) == 0) {
309             if (continuous) {
310                 move_panel(pans[which], y1, x1);
311                 pflush();
312             }
313         }
314         if (code > 0) {
315             move_panel(pans[which], y1, x1);
316         }
317     }
318 }
319
320 static void
321 my_resize_panel(PANEL ** pans, int which, FillPanel myFill)
322 {
323     if (pans[which] != 0) {
324         int code;
325         int y0, x0;
326         int y1, x1;
327         WINDOW *win = panel_window(pans[which]);
328         char also[80];
329
330         getbegyx(win, y0, x0);
331         sprintf(also, " (start %d,%d)", y0, x0);
332         wmove(stdscr, y0, x0);
333         while ((code = get_position("Resize panel",
334                                     also, which, &x1, &y1)) == 0) {
335             ;
336         }
337         if (code > 0) {
338             WINDOW *next = newwin(ABS(y1 - y0) + 1,
339                                   ABS(x1 - x0) + 1,
340                                   MIN(y0, y1),
341                                   MIN(x0, x1));
342             if (next != 0) {
343                 keypad(next, TRUE);
344                 if (use_colors) {
345                     wbkgdset(next, (chtype) (COLOR_PAIR(which) | ' '));
346                 } else if (!unboxed) {
347                     wbkgdset(next, A_BOLD | ' ');
348                 }
349                 replace_panel(pans[which], next);
350                 myFill(pans[which]);
351                 delwin(win);
352             }
353         }
354     }
355 }
356
357 static void
358 init_panel(void)
359 {
360     register int y, x;
361
362     for (y = 0; y < LINES - 1; y++) {
363         for (x = 0; x < COLS; x++)
364             wprintw(stdscr, "%d", (y + x) % 10);
365     }
366 }
367
368 static void
369 fill_panel(PANEL * pan)
370 {
371     WINDOW *win = panel_window(pan);
372     int num = ((const char *) panel_userptr(pan))[1];
373     int y, x;
374
375     wmove(win, 1, 1);
376     wprintw(win, "-pan%c-", num);
377     wclrtoeol(win);
378     box(win, 0, 0);
379     for (y = 2; y < getmaxy(win) - 1; y++) {
380         for (x = 1; x < getmaxx(win) - 1; x++) {
381             wmove(win, y, x);
382             waddch(win, UChar(num));
383         }
384     }
385 }
386
387 static void
388 fill_unboxed(PANEL * pan)
389 {
390     WINDOW *win = panel_window(pan);
391     int num = ((const char *) panel_userptr(pan))[1];
392     int y, x;
393
394     for (y = 0; y < getmaxy(win); y++) {
395         for (x = 0; x < getmaxx(win); x++) {
396             wmove(win, y, x);
397             waddch(win, UChar(num));
398         }
399     }
400 }
401
402 #if USE_WIDEC_SUPPORT
403 static void
404 make_fullwidth_digit(cchar_t *target, int digit)
405 {
406     wchar_t source[2];
407
408     source[0] = digit + 0xff10;
409     source[1] = 0;
410     setcchar(target, source, A_NORMAL, 0, 0);
411 }
412
413 static void
414 init_wide_panel(void)
415 {
416     int digit;
417     cchar_t temp[10];
418
419     for (digit = 0; digit < 10; ++digit)
420         make_fullwidth_digit(&temp[digit], digit);
421
422     do {
423         int y, x;
424         getyx(stdscr, y, x);
425         digit = (y + x / 2) % 10;
426     } while (add_wch(&temp[digit]) != ERR);
427 }
428
429 static void
430 fill_wide_panel(PANEL * pan)
431 {
432     WINDOW *win = panel_window(pan);
433     int num = ((const char *) panel_userptr(pan))[1];
434     int y, x;
435
436     wmove(win, 1, 1);
437     wprintw(win, "-pan%c-", num);
438     wclrtoeol(win);
439     box(win, 0, 0);
440     for (y = 2; y < getmaxy(win) - 1; y++) {
441         for (x = 1; x < getmaxx(win) - 1; x++) {
442             wmove(win, y, x);
443             waddch(win, UChar(num));
444         }
445     }
446 }
447 #endif
448
449 #define MAX_PANELS 5
450
451 static int
452 which_panel(PANEL * px[MAX_PANELS + 1], PANEL * pan)
453 {
454     int result = 0;
455     int j;
456
457     for (j = 1; j <= MAX_PANELS; ++j) {
458         if (px[j] == pan) {
459             result = j;
460             break;
461         }
462     }
463     return result;
464 }
465
466 static void
467 show_panels(PANEL * px[MAX_PANELS + 1])
468 {
469     static const char *help[] =
470     {
471         "",
472         "Commands are letter/digit pairs.  Digits are the panel number.",
473         "",
474         "  b - put the panel on the bottom of the stack",
475         "  c - create the panel",
476         "  d - delete the panel",
477         "  h - hide the panel",
478         "  m - move the panel (M for continuous move)",
479         "  r - resize the panel",
480         "  s - show the panel",
481         "  b - put the panel on the top of the stack"
482     };
483
484     struct {
485         bool valid;
486         bool hidden;
487         PANEL *above;
488         PANEL *below;
489     } table[MAX_PANELS + 1];
490
491     WINDOW *win;
492     PANEL *pan;
493     int j;
494
495     for (j = 1; j <= MAX_PANELS; ++j) {
496         table[j].valid = (px[j] != 0);
497         if (table[j].valid) {
498             table[j].hidden = panel_hidden(px[j]);
499             table[j].above = panel_above(px[j]);
500             table[j].below = panel_below(px[j]);
501         }
502     }
503
504     if ((win = newwin(LINES - 1, COLS, 0, 0)) != 0) {
505         keypad(win, TRUE);
506         if ((pan = new_panel(win)) != 0) {
507             werase(win);
508             mvwprintw(win, 0, 0, "Panels:\n");
509             for (j = 1; j <= MAX_PANELS; ++j) {
510                 if (table[j].valid) {
511                     wprintw(win, " %d:", j);
512                     if (table[j].hidden) {
513                         waddstr(win, " hidden");
514                     } else {
515                         if (table[j].above) {
516                             wprintw(win, " above %d",
517                                     which_panel(px, table[j].above));
518                         }
519                         if (table[j].below) {
520                             wprintw(win, "%s below %d",
521                                     table[j].above ? "," : "",
522                                     which_panel(px, table[j].below));
523                         }
524                     }
525                     waddch(win, '\n');
526                 }
527             }
528             for (j = 0; j < (int) SIZEOF(help); ++j) {
529                 if (wprintw(win, "%s\n", help[j]) == ERR)
530                     break;
531             }
532             wgetch(win);
533             del_panel(pan);
534             pflush();
535         }
536         delwin(win);
537     }
538 }
539
540 #define wrapper(func) \
541 static int my_##func(PANEL *pan) \
542 { \
543     int code = ERR; \
544     if (pan != 0) { \
545         code = func(pan); \
546     } \
547     return code; \
548 }
549 /* *INDENT-OFF* */
550 wrapper(bottom_panel)
551 wrapper(hide_panel)
552 wrapper(show_panel)
553 wrapper(top_panel)
554 /* *INDENT-ON* */
555
556 static void
557 do_panel(PANEL * px[MAX_PANELS + 1],
558          NCURSES_CONST char *cmd,
559          FillPanel myFill)
560 {
561     int which = cmd[1] - '0';
562
563     if (which < 1 || which > MAX_PANELS) {
564         beep();
565         return;
566     }
567
568     if (log_in != 0) {
569         pflush();
570     }
571
572     saywhat(cmd);
573     switch (*cmd) {
574     case 'b':
575         my_bottom_panel(px[which]);
576         break;
577     case 'c':
578         my_create_panel(px, which, myFill);
579         break;
580     case 'd':
581         my_remove_panel(px, which);
582         break;
583     case 'h':
584         my_hide_panel(px[which]);
585         break;
586     case 'm':
587         my_move_panel(px, which, FALSE);
588         break;
589     case 'M':
590         my_move_panel(px, which, TRUE);
591         break;
592     case 'r':
593         my_resize_panel(px, which, myFill);
594         break;
595     case 's':
596         my_show_panel(px[which]);
597         break;
598     case 't':
599         my_top_panel(px[which]);
600         break;
601     }
602 }
603
604 static bool
605 ok_letter(int ch)
606 {
607     return isalpha(UChar(ch)) && strchr("bcdhmMrst", ch) != 0;
608 }
609
610 static bool
611 ok_digit(int ch)
612 {
613     return isdigit(UChar(ch)) && (ch >= '1') && (ch - '0' <= MAX_PANELS);
614 }
615
616 /*
617  * A command consists of one or more letter/digit pairs separated by a space.
618  * Digits are limited to 1..MAX_PANELS.
619  *
620  * End the command with a newline.  Reject other characters.
621  */
622 static bool
623 get_command(PANEL * px[MAX_PANELS + 1], char *buffer, int limit)
624 {
625     int length = 0;
626     int y0, x0;
627     int c0, ch;
628     WINDOW *win;
629
630     getyx(stdscr, y0, x0);
631     win = statusline();
632     waddstr(win, "Command:");
633     buffer[length = 0] = '\0';
634
635     if (log_in != 0) {
636         if (fgets(buffer, limit - 3, log_in) != 0) {
637             length = strlen(buffer);
638             while (length > 0 && isspace(UChar(buffer[length - 1])))
639                 buffer[--length] = '\0';
640             waddstr(win, buffer);
641         } else {
642             close_input();
643         }
644         (void) wgetch(win);
645     } else {
646         c0 = 0;
647         for (;;) {
648             ch = wgetch(win);
649             if (ch == ERR || ch == QUIT || ch == ESCAPE) {
650                 buffer[0] = '\0';
651                 break;
652             } else if (ch == CTRL('L')) {
653                 wrefresh(curscr);
654             } else if (ch == '\n' || ch == KEY_ENTER) {
655                 break;
656             } else if (ch == '?') {
657                 show_panels(px);
658             } else if (length + 3 < limit) {
659                 if (ch >= KEY_MIN) {
660                     beep();
661                 } else if (ok_letter(UChar(ch))) {
662                     if (isalpha(UChar(c0))) {
663                         beep();
664                     } else if (isdigit(UChar(c0))) {
665                         wprintw(win, " %c", ch);
666                         buffer[length++] = ' ';
667                         buffer[length++] = c0 = ch;
668                     } else {
669                         wprintw(win, "%c", ch);
670                         buffer[length++] = c0 = ch;
671                     }
672                 } else if (ok_digit(ch)) {
673                     if (isalpha(UChar(c0))) {
674                         wprintw(win, "%c", ch);
675                         buffer[length++] = c0 = ch;
676                     } else {
677                         beep();
678                     }
679                 } else if (ch == ' ') {
680                     if (isdigit(UChar(c0))) {
681                         wprintw(win, "%c", ch);
682                         buffer[length++] = c0 = ch;
683                     } else {
684                         beep();
685                     }
686                 } else {
687                     beep();
688                 }
689             } else {
690                 beep();
691             }
692         }
693     }
694
695     wmove(stdscr, y0, x0);
696
697     buffer[length] = '\0';
698     if (log_out && length) {
699         fprintf(log_out, "%s\n", buffer);
700     }
701     return (length != 0);
702 }
703
704 static void
705 demo_panels(InitPanel myInit, FillPanel myFill)
706 {
707     int itmp;
708     PANEL *px[MAX_PANELS + 1];
709     char buffer[BUFSIZ];
710
711     scrollok(stdscr, FALSE);    /* we don't want stdscr to scroll! */
712     refresh();
713
714     myInit();
715     memset(px, 0, sizeof(px));
716
717     while (get_command(px, buffer, sizeof(buffer))) {
718         int limit = strlen(buffer);
719         for (itmp = 0; itmp < limit; itmp += 3) {
720             do_panel(px, buffer + itmp, myFill);
721         }
722         pflush();
723     }
724 #if NO_LEAKS
725     for (itmp = 1; itmp <= MAX_PANELS; ++itmp) {
726         my_remove_panel(px, itmp);
727     }
728 #endif
729 }
730
731 static void
732 usage(void)
733 {
734     static const char *const tbl[] =
735     {
736         "Usage: demo_panels [options]"
737         ,""
738         ,"Options:"
739         ,"  -i file  read commands from file"
740         ,"  -o file  record commands in file"
741         ,"  -m       do not use colors"
742 #if USE_WIDEC_SUPPORT
743         ,"  -w       use wide-characters in panels and background"
744 #endif
745         ,"  -x       do not enclose panels in boxes"
746     };
747     size_t n;
748     for (n = 0; n < SIZEOF(tbl); n++)
749         fprintf(stderr, "%s\n", tbl[n]);
750     ExitProgram(EXIT_FAILURE);
751 }
752
753 int
754 main(int argc, char *argv[])
755 {
756     int c;
757     bool monochrome = FALSE;
758     InitPanel myInit = init_panel;
759     FillPanel myFill = fill_panel;
760
761     setlocale(LC_ALL, "");
762
763     while ((c = getopt(argc, argv, "i:o:mwx")) != -1) {
764         switch (c) {
765         case 'i':
766             log_in = fopen(optarg, "r");
767             break;
768         case 'o':
769             log_out = fopen(optarg, "w");
770             break;
771         case 'm':
772             monochrome = TRUE;
773             break;
774 #if USE_WIDEC_SUPPORT
775         case 'w':
776             myInit = init_wide_panel;
777             myFill = fill_wide_panel;
778             break;
779 #endif
780         case 'x':
781             unboxed = TRUE;
782             break;
783         default:
784             usage();
785         }
786     }
787     if (unboxed)
788         myFill = fill_unboxed;
789
790     initscr();
791     cbreak();
792     noecho();
793     keypad(stdscr, TRUE);
794
795     use_colors = monochrome ? FALSE : has_colors();
796     if (use_colors)
797         start_color();
798
799     demo_panels(myInit, myFill);
800     endwin();
801
802     close_input();
803     close_output();
804
805     ExitProgram(EXIT_SUCCESS);
806 }
807 #else
808 int
809 main(void)
810 {
811     printf("This program requires the curses panel library\n");
812     ExitProgram(EXIT_FAILURE);
813 }
814 #endif