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