]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/combine.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / combine.c
1 /****************************************************************************
2  * Copyright 2021,2022 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.19 2022/05/21 23:19:31 tom Exp $
30  */
31
32 #include <test.priv.h>
33
34 #if USE_WIDEC_SUPPORT
35
36 #include <wctype.h>
37 #include <dump_window.h>
38 #include <popup_msg.h>
39
40 static int c_opt;
41 static int r_opt;
42
43 static int
44 next_char(int value)
45 {
46     do {
47         ++value;
48     } while (!iswprint((wint_t) value));
49     return value;
50 }
51
52 static int
53 prev_char(int value)
54 {
55     do {
56         --value;
57     } while (!iswprint((wint_t) value));
58     return value;
59 }
60
61 static void
62 do_row(int row, int base_ch, int over_ch)
63 {
64     int col = 0;
65     bool done = FALSE;
66     bool reverse = (r_opt && !(row % 2));
67
68     move(row, col);
69     printw("[U+%04X]", over_ch);
70     do {
71         if (c_opt) {
72             wchar_t source[2];
73             cchar_t target;
74             attr_t attr = reverse ? A_REVERSE : A_NORMAL;
75
76             source[1] = 0;
77
78             source[0] = (wchar_t) base_ch;
79             setcchar(&target, source, attr, 0, NULL);
80             add_wch(&target);
81
82             source[0] = (wchar_t) over_ch;
83             setcchar(&target, source, attr, 0, NULL);
84             add_wch(&target);
85         } else {
86             wchar_t data[3];
87
88             data[0] = (wchar_t) base_ch;
89             data[1] = (wchar_t) over_ch;
90             data[2] = 0;
91             if (reverse)
92                 attr_on(A_REVERSE, NULL);
93             addwstr(data);
94             if (reverse)
95                 attr_off(A_REVERSE, NULL);
96         }
97         col = getcurx(stdscr);
98         base_ch = next_char(base_ch);
99         done = (col + 1 >= COLS);
100     } while (!done);
101 }
102
103 #define LAST_OVER 0x6f
104
105 static int
106 next_over(int value)
107 {
108     if (++value > LAST_OVER)
109         value = 0;
110     return value;
111 }
112
113 static int
114 prev_over(int value)
115 {
116     if (--value < 0)
117         value = LAST_OVER;
118     return value;
119 }
120
121 static void
122 do_all(int left_at, int over_it)
123 {
124     int row;
125
126     for (row = 0; row < LINES; ++row) {
127         do_row(row, left_at, 0x300 + over_it);
128         over_it = next_over(over_it);
129     }
130 }
131
132 static void
133 show_help(WINDOW *current)
134 {
135     /* *INDENT-OFF* */
136     static struct {
137         int     key;
138         CONST_FMT char * msg;
139     } help[] = {
140         { HELP_KEY_1,   "Show this screen" },
141         { CTRL('L'),    "Repaint screen" },
142         { '$',          "Scroll to end of combining-character range" },
143         { '+',          "Scroll to next combining-character in range" },
144         { KEY_DOWN,     "(same as \"+\")" },
145         { '-',          "Scroll to previous combining-character in range" },
146         { KEY_UP,       "(same as \"-\")" },
147         { '0',          "Scroll to beginning of combining-character range" },
148         { 'c',          "Toggle command-line option \"-c\"" },
149         { 'd',          "Dump screen using scr_dump unless \"-l\" option used" },
150         { 'h',          "Scroll test-data left one column" },
151         { 'j',          "Scroll test-data down one row" },
152         { 'k',          "Scroll test-data up one row" },
153         { 'l',          "Scroll test-data right one column" },
154         { 'q',          "Quit" },
155         { ESCAPE,       "(same as \"q\")" },
156         { QUIT,         "(same as \"q\")" },
157         { 'r',          "Toggle command-line option \"-r\"" },
158     };
159     /* *INDENT-ON* */
160
161     char **msgs = typeCalloc(char *, SIZEOF(help) + 3);
162     size_t s;
163     int d = 0;
164
165     msgs[d++] = strdup("Test diacritic combining-characters range "
166                        "U+0300..U+036F");
167     msgs[d++] = strdup("");
168     for (s = 0; s < SIZEOF(help); ++s) {
169         char *name = strdup(keyname(help[s].key));
170         size_t need = (11 + strlen(name) + strlen(help[s].msg));
171         msgs[d] = typeMalloc(char, need);
172         _nc_SPRINTF(msgs[d], _nc_SLIMIT(need) "%-10s%s", name, help[s].msg);
173         free(name);
174         ++d;
175     }
176     popup_msg2(current, msgs);
177     for (s = 0; msgs[s] != 0; ++s) {
178         free(msgs[s]);
179     }
180     free(msgs);
181 }
182
183 static void
184 usage(void)
185 {
186     static const char *msg[] =
187     {
188         "Usage: combine [options]",
189         "",
190         "Demonstrate combining-characters.",
191         "",
192         "Options:",
193         " -c       use cchar_t data rather than wchar_t string",
194         " -l FILE  log window-dumps to this file",
195         " -r       draw even-numbered rows in reverse-video",
196     };
197     unsigned n;
198     for (n = 0; n < SIZEOF(msg); ++n) {
199         fprintf(stderr, "%s\n", msg[n]);
200     }
201     ExitProgram(EXIT_FAILURE);
202 }
203
204 int
205 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
206 {
207     int n;
208     int left_at = ' ';
209     int over_it = 0;
210     bool done = FALSE;
211     bool log_option = FALSE;
212     const char *dump_log = "combine.log";
213
214     while ((n = getopt(argc, argv, "cl:r")) != -1) {
215         switch (n) {
216         case 'c':
217             c_opt = TRUE;
218             break;
219         case 'l':
220             log_option = TRUE;
221             if (!open_dump(optarg))
222                 usage();
223             break;
224         case 'r':
225             r_opt = TRUE;
226             break;
227         default:
228             usage();
229             break;
230         }
231     }
232
233     setlocale(LC_ALL, "");
234     initscr();
235     cbreak();
236     noecho();
237     keypad(stdscr, TRUE);
238
239     do {
240         do_all(left_at, over_it);
241         switch (getch()) {
242         case HELP_KEY_1:
243             show_help(stdscr);
244             break;
245         case 'q':
246         case QUIT:
247         case ESCAPE:
248             done = TRUE;
249             break;
250         case CTRL('L'):
251             redrawwin(stdscr);
252             break;
253         case 'd':
254             if (log_option)
255                 dump_window(stdscr);
256 #if HAVE_SCR_DUMP
257             else
258                 scr_dump(dump_log);
259 #endif
260             break;
261         case 'h':
262             if (left_at > ' ')
263                 left_at = prev_char(left_at);
264             break;
265         case 'l':
266             left_at = next_char(left_at);
267             break;
268         case 'c':
269             c_opt = !c_opt;
270             break;
271         case 'r':
272             r_opt = !r_opt;
273             break;
274         case KEY_HOME:
275         case '0':
276             over_it = 0;
277             break;
278         case KEY_END:
279         case '$':
280             over_it = LAST_OVER;
281             break;
282         case KEY_UP:
283         case 'k':
284         case '-':
285             over_it = prev_over(over_it);
286             break;
287         case KEY_DOWN:
288         case 'j':
289         case '+':
290             over_it = next_over(over_it);
291             break;
292         }
293     } while (!done);
294
295     endwin();
296
297     ExitProgram(EXIT_SUCCESS);
298 }
299 #else
300 int
301 main(void)
302 {
303     printf("This program requires wide-curses functions\n");
304     ExitProgram(EXIT_FAILURE);
305 }
306 #endif