]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/combine.c
ncurses 6.3 - patch 20211211
[ncurses.git] / test / combine.c
1 /****************************************************************************
2  * Copyright 2021 Thomas E. Dickey                                          *
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: combine.c,v 1.7 2021/12/12 01:10:28 tom Exp $
30  */
31
32 #include <test.priv.h>
33
34 #if USE_WIDEC_SUPPORT
35
36 static int c_opt;
37 static int r_opt;
38
39 static int
40 next_char(int value)
41 {
42     do {
43         ++value;
44     } while (!iswprint((wint_t) value));
45     return value;
46 }
47
48 static void
49 do_row(int row, int base_ch, int over_ch)
50 {
51     int col = 0;
52     bool done = FALSE;
53     bool reverse = (r_opt && !(row % 2));
54
55     move(row, col);
56     printw("[U+%04X]", over_ch);
57     do {
58         if (c_opt) {
59             wchar_t source[2];
60             cchar_t target;
61             attr_t attr = reverse ? A_REVERSE : A_NORMAL;
62
63             source[1] = 0;
64
65             source[0] = base_ch;
66             setcchar(&target, source, attr, 0, NULL);
67             add_wch(&target);
68
69             source[0] = over_ch;
70             setcchar(&target, source, attr, 0, NULL);
71             add_wch(&target);
72         } else {
73             wchar_t data[3];
74
75             data[0] = base_ch;
76             data[1] = over_ch;
77             data[2] = 0;
78             if (reverse)
79                 attr_on(A_REVERSE, NULL);
80             addwstr(data);
81             if (reverse)
82                 attr_off(A_REVERSE, NULL);
83         }
84         col = getcurx(stdscr);
85         base_ch = next_char(base_ch);
86         done = (col + 1 >= COLS);
87     } while (!done);
88 }
89
90 #define LAST_OVER 0x6f
91
92 static int
93 next_over(int value)
94 {
95     if (++value > LAST_OVER)
96         value = 0;
97     return value;
98 }
99
100 static int
101 prev_over(int value)
102 {
103     if (--value < 0)
104         value = LAST_OVER;
105     return value;
106 }
107
108 static void
109 do_all(int over_it)
110 {
111     int row;
112
113     for (row = 0; row < LINES; ++row) {
114         do_row(row, ' ', 0x300 + over_it);
115         over_it = next_over(over_it);
116     }
117 }
118
119 static void
120 usage(void)
121 {
122     static const char *msg[] =
123     {
124         "Usage: combine [options]",
125         "",
126         "Demonstrate combining-characters.",
127         "",
128         "Options:",
129         " -c       use cchar_t data rather than wchar_t string",
130         " -r       draw even-numbered rows in reverse-video",
131     };
132     unsigned n;
133     for (n = 0; n < SIZEOF(msg); ++n) {
134         fprintf(stderr, "%s\n", msg[n]);
135     }
136     ExitProgram(EXIT_FAILURE);
137 }
138
139 int
140 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
141 {
142     int n;
143     int over_it = 0;
144     bool done = FALSE;
145
146     while ((n = getopt(argc, argv, "cr")) != -1) {
147         switch (n) {
148         case 'c':
149             c_opt = TRUE;
150             break;
151         case 'r':
152             r_opt = TRUE;
153             break;
154         default:
155             usage();
156             break;
157         }
158     }
159
160     setlocale(LC_ALL, "");
161     initscr();
162     cbreak();
163     noecho();
164     keypad(stdscr, TRUE);
165
166     do {
167         do_all(over_it);
168         switch (getch()) {
169         case 'q':
170         case QUIT:
171         case ESCAPE:
172             done = TRUE;
173             break;
174         case KEY_HOME:
175         case '0':
176             over_it = 0;
177             break;
178         case KEY_END:
179         case '$':
180             over_it = LAST_OVER;
181             break;
182         case KEY_UP:
183         case '-':
184             over_it = prev_over(over_it);
185             break;
186         case KEY_DOWN:
187         case '+':
188             over_it = next_over(over_it);
189             break;
190         }
191     } while (!done);
192
193     endwin();
194
195     ExitProgram(EXIT_SUCCESS);
196 }
197 #else
198 int
199 main(void)
200 {
201     printf("This program requires wide-curses functions\n");
202     ExitProgram(EXIT_FAILURE);
203 }
204 #endif