]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/inchs.c
38747a2f39bd2c70162b0b16831a420b99374f3a
[ncurses.git] / test / inchs.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: inchs.c,v 1.6 2007/06/09 21:25:06 tom Exp $
30  */
31 /*
32        chtype inch(void);
33        chtype winch(WINDOW *win);
34        chtype mvinch(int y, int x);
35        chtype mvwinch(WINDOW *win, int y, int x);
36        int inchstr(chtype *chstr);
37        int inchnstr(chtype *chstr, int n);
38        int winchstr(WINDOW *win, chtype *chstr);
39        int winchnstr(WINDOW *win, chtype *chstr, int n);
40        int mvinchstr(int y, int x, chtype *chstr);
41        int mvinchnstr(int y, int x, chtype *chstr, int n);
42        int mvwinchstr(WINDOW *win, int y, int x, chtype *chstr);
43        int mvwinchnstr(WINDOW *win, int y, int x, chtype *chstr, int n);
44 */
45
46 #include <test.priv.h>
47
48 #define BASE_Y 7
49 #define MAX_COLS 1024
50
51 static bool
52 Quit(int ch)
53 {
54     return (ch == ERR || ch == 'q' || ch == QUIT || ch == ESCAPE);
55 }
56
57 static int
58 test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin)
59 {
60     WINDOW *txtbox = 0;
61     WINDOW *txtwin = 0;
62     FILE *fp;
63     int ch, j;
64     int txt_x = 0, txt_y = 0;
65     int base_y;
66     int limit;
67     chtype text[MAX_COLS];
68
69     if (argv[level] == 0) {
70         beep();
71         return FALSE;
72     }
73
74     if (level > 1) {
75         txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level);
76         box(txtbox, 0, 0);
77         wnoutrefresh(txtbox);
78
79         txtwin = derwin(txtbox,
80                         getmaxy(txtbox) - 2,
81                         getmaxx(txtbox) - 2,
82                         1, 1);
83         base_y = 0;
84     } else {
85         txtwin = stdscr;
86         base_y = BASE_Y;
87     }
88
89     keypad(txtwin, TRUE);       /* enable keyboard mapping */
90     (void) cbreak();            /* take input chars one at a time, no wait for \n */
91     (void) noecho();            /* don't echo input */
92
93     txt_y = base_y;
94     txt_x = 0;
95     wmove(txtwin, txt_y, txt_x);
96
97     if ((fp = fopen(argv[level], "r")) != 0) {
98         while ((j = fgetc(fp)) != EOF) {
99             if (waddch(txtwin, j) != OK) {
100                 break;
101             }
102         }
103     } else {
104         wprintw(txtwin, "Cannot open:\n%s", argv[1]);
105     }
106     fclose(fp);
107
108     while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) {
109         switch (j) {
110         case KEY_DOWN:
111         case 'j':
112             if (txt_y < getmaxy(txtwin) - 1)
113                 txt_y++;
114             else
115                 beep();
116             break;
117         case KEY_UP:
118         case 'k':
119             if (txt_y > base_y)
120                 txt_y--;
121             else
122                 beep();
123             break;
124         case KEY_LEFT:
125         case 'h':
126             if (txt_x > 0)
127                 txt_x--;
128             else
129                 beep();
130             break;
131         case KEY_RIGHT:
132         case 'l':
133             if (txt_x < getmaxx(txtwin) - 1)
134                 txt_x++;
135             else
136                 beep();
137             break;
138         case 'w':
139             test_inchs(level + 1, argv, chrwin, strwin);
140             if (txtbox != 0) {
141                 touchwin(txtbox);
142                 wnoutrefresh(txtbox);
143             } else {
144                 touchwin(txtwin);
145                 wnoutrefresh(txtwin);
146             }
147             break;
148         default:
149             beep();
150             break;
151         }
152
153         mvwprintw(chrwin, 0, 0, "char:");
154         wclrtoeol(chrwin);
155
156         if (txtwin != stdscr) {
157             wmove(txtwin, txt_y, txt_x);
158
159             if ((ch = winch(txtwin)) != ERR) {
160                 if (waddch(chrwin, (chtype) ch) != ERR) {
161                     for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
162                         if ((ch = mvwinch(txtwin, txt_y, j)) != ERR) {
163                             if (waddch(chrwin, (chtype) ch) == ERR) {
164                                 break;
165                             }
166                         } else {
167                             break;
168                         }
169                     }
170                 }
171             }
172         } else {
173             move(txt_y, txt_x);
174
175             if ((ch = inch()) != ERR) {
176                 if (waddch(chrwin, (chtype) ch) != ERR) {
177                     for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
178                         if ((ch = mvinch(txt_y, j)) != ERR) {
179                             if (waddch(chrwin, (chtype) ch) == ERR) {
180                                 break;
181                             }
182                         } else {
183                             break;
184                         }
185                     }
186                 }
187             }
188         }
189         wnoutrefresh(chrwin);
190
191         mvwprintw(strwin, 0, 0, "text:");
192         wclrtobot(strwin);
193
194         limit = getmaxx(strwin) - 5;
195
196         if (txtwin != stdscr) {
197             wmove(txtwin, txt_y, txt_x);
198             if (winchstr(txtwin, text) != ERR) {
199                 mvwaddchstr(strwin, 0, 5, text);
200             }
201
202             wmove(txtwin, txt_y, txt_x);
203             if (winchnstr(txtwin, text, limit) != ERR) {
204                 mvwaddchstr(strwin, 1, 5, text);
205             }
206
207             if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) {
208                 mvwaddchstr(strwin, 2, 5, text);
209             }
210
211             if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) {
212                 mvwaddchstr(strwin, 3, 5, text);
213             }
214         } else {
215             move(txt_y, txt_x);
216             if (inchstr(text) != ERR) {
217                 mvwaddchstr(strwin, 0, 5, text);
218             }
219
220             move(txt_y, txt_x);
221             if (inchnstr(text, limit) != ERR) {
222                 mvwaddchstr(strwin, 1, 5, text);
223             }
224
225             if (mvinchstr(txt_y, txt_x, text) != ERR) {
226                 mvwaddchstr(strwin, 2, 5, text);
227             }
228
229             if (mvinchnstr(txt_y, txt_x, text, limit) != ERR) {
230                 mvwaddchstr(strwin, 3, 5, text);
231             }
232         }
233
234         wnoutrefresh(strwin);
235     }
236     if (level > 1) {
237         delwin(txtwin);
238         delwin(txtbox);
239     }
240     return TRUE;
241 }
242
243 int
244 main(int argc, char *argv[])
245 {
246     WINDOW *chrbox;
247     WINDOW *chrwin;
248     WINDOW *strwin;
249
250     setlocale(LC_ALL, "");
251
252     if (argc < 2) {
253         fprintf(stderr, "usage: %s file\n", argv[0]);
254         return EXIT_FAILURE;
255     }
256
257     initscr();
258
259     chrbox = derwin(stdscr, BASE_Y, COLS, 0, 0);
260     box(chrbox, 0, 0);
261     wnoutrefresh(chrbox);
262
263     chrwin = derwin(chrbox, 1, COLS - 2, 1, 1);
264     strwin = derwin(chrbox, 4, COLS - 2, 2, 1);
265
266     test_inchs(1, argv, chrwin, strwin);
267
268     endwin();
269     return EXIT_SUCCESS;
270 }