]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/inch_wide.c
ncurses 5.6 - patch 20070407
[ncurses.git] / test / inch_wide.c
1 /****************************************************************************
2  * Copyright (c) 2007 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: inch_wide.c,v 1.3 2007/02/11 00:32:58 tom Exp $
30  */
31 /*
32        int in_wch(cchar_t *wcval);
33        int mvin_wch(int y, int x, cchar_t *wcval);
34        int mvwin_wch(WINDOW *win, int y, int x, cchar_t *wcval);
35        int win_wch(WINDOW *win, cchar_t *wcval);
36        int in_wchstr(cchar_t *wchstr);
37        int in_wchnstr(cchar_t *wchstr, int n);
38        int win_wchstr(WINDOW *win, cchar_t *wchstr);
39        int win_wchnstr(WINDOW *win, cchar_t *wchstr, int n);
40        int mvin_wchstr(int y, int x, cchar_t *wchstr);
41        int mvin_wchnstr(int y, int x, cchar_t *wchstr, int n);
42        int mvwin_wchstr(WINDOW *win, int y, int x, cchar_t *wchstr);
43        int mvwin_wchnstr(WINDOW *win, int y, int x, cchar_t *wchstr, int n);
44 */
45
46 #include <test.priv.h>
47
48 #if USE_WIDEC_SUPPORT
49
50 #define MAX_COLS 1024
51
52 static bool
53 Quit(int ch)
54 {
55     return (ch == ERR || ch == QUIT || ch == ESCAPE);
56 }
57
58 int
59 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
60 {
61     WINDOW *txtbox;
62     WINDOW *txtwin;
63     WINDOW *chrbox;
64     WINDOW *chrwin;
65     WINDOW *strwin;
66     FILE *fp;
67     int j;
68     int txt_x = 0, txt_y = 0;
69     int limit;
70     cchar_t ch;
71     cchar_t text[MAX_COLS];
72
73     setlocale(LC_ALL, "");
74
75     if (argc != 2) {
76         fprintf(stderr, "usage: %s file\n", argv[0]);
77         return EXIT_FAILURE;
78     }
79
80     initscr();
81
82     chrbox = newwin(7, COLS, 0, 0);
83     box(chrbox, 0, 0);
84     wnoutrefresh(chrbox);
85
86     chrwin = derwin(chrbox, 1, COLS - 2, 1, 1);
87     strwin = derwin(chrbox, 4, COLS - 2, 2, 1);
88
89     txtbox = newwin(LINES - 7, COLS, 7, 0);
90     box(txtbox, 0, 0);
91     wnoutrefresh(txtbox);
92
93     txtwin = derwin(txtbox,
94                     getmaxy(txtbox) - 2,
95                     getmaxx(txtbox) - 2,
96                     1, 1);
97
98     keypad(txtwin, TRUE);       /* enable keyboard mapping */
99     (void) cbreak();            /* take input chars one at a time, no wait for \n */
100     (void) noecho();            /* don't echo input */
101
102     if ((fp = fopen(argv[1], "r")) != 0) {
103         while ((j = fgetc(fp)) != EOF) {
104             if (waddch(txtwin, j) != OK) {
105                 break;
106             }
107         }
108     } else {
109         wprintw(txtwin, "Cannot open:\n%s", argv[1]);
110     }
111     fclose(fp);
112     while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) {
113         switch (j) {
114         case KEY_DOWN:
115         case 'j':
116             if (txt_y < getmaxy(txtwin) - 1)
117                 txt_y++;
118             else
119                 beep();
120             break;
121         case KEY_UP:
122         case 'k':
123             if (txt_y > 0)
124                 txt_y--;
125             else
126                 beep();
127             break;
128         case KEY_LEFT:
129         case 'h':
130             if (txt_x > 0)
131                 txt_x--;
132             else
133                 beep();
134             break;
135         case KEY_RIGHT:
136         case 'l':
137             if (txt_x < getmaxx(txtwin) - 1)
138                 txt_x++;
139             else
140                 beep();
141             break;
142         }
143
144         wmove(txtwin, txt_y, txt_x);
145
146         mvwprintw(chrwin, 0, 0, "char:");
147         wclrtoeol(chrwin);
148         if (win_wch(txtwin, &ch) != ERR) {
149             if (wadd_wch(chrwin, &ch) != ERR) {
150                 for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
151                     if (mvwin_wch(txtwin, txt_y, j, &ch) != ERR) {
152                         if (wadd_wch(chrwin, &ch) == ERR) {
153                             break;
154                         }
155                     } else {
156                         break;
157                     }
158                 }
159             }
160         }
161         wnoutrefresh(chrwin);
162
163         mvwprintw(strwin, 0, 0, "text:");
164         wclrtobot(strwin);
165
166         limit = getmaxx(strwin) - 5;
167
168         wmove(txtwin, txt_y, txt_x);
169         if (win_wchstr(txtwin, text) != ERR) {
170             mvwadd_wchstr(strwin, 0, 5, text);
171         }
172
173         wmove(txtwin, txt_y, txt_x);
174         if (win_wchnstr(txtwin, text, limit) != ERR) {
175             mvwadd_wchstr(strwin, 1, 5, text);
176         }
177
178         if (mvwin_wchstr(txtwin, txt_y, txt_x, text) != ERR) {
179             mvwadd_wchstr(strwin, 2, 5, text);
180         }
181
182         if (mvwin_wchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) {
183             mvwadd_wchstr(strwin, 3, 5, text);
184         }
185
186         wnoutrefresh(strwin);
187
188         /* FIXME: want stdscr tests also, but must be separate program */
189     }
190     endwin();
191     return EXIT_SUCCESS;
192 }
193 #else
194 int
195 main(void)
196 {
197     printf("This program requires the wide-ncurses library\n");
198     ExitProgram(EXIT_FAILURE);
199 }
200 #endif