]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/inchs.c
ncurses 5.6 - patch 20070428
[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.4 2007/02/11 00:27:12 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 MAX_COLS 1024
49
50 static bool
51 Quit(int ch)
52 {
53     return (ch == ERR || ch == QUIT || ch == ESCAPE);
54 }
55
56 int
57 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
58 {
59     WINDOW *txtbox;
60     WINDOW *txtwin;
61     WINDOW *chrbox;
62     WINDOW *chrwin;
63     WINDOW *strwin;
64     FILE *fp;
65     int ch, j;
66     int txt_x = 0, txt_y = 0;
67     int limit;
68     chtype text[MAX_COLS];
69
70     setlocale(LC_ALL, "");
71
72     if (argc != 2) {
73         fprintf(stderr, "usage: %s file\n", argv[0]);
74         return EXIT_FAILURE;
75     }
76
77     initscr();
78
79     chrbox = newwin(7, COLS, 0, 0);
80     box(chrbox, 0, 0);
81     wnoutrefresh(chrbox);
82
83     chrwin = derwin(chrbox, 1, COLS - 2, 1, 1);
84     strwin = derwin(chrbox, 4, COLS - 2, 2, 1);
85
86     txtbox = newwin(LINES - 7, COLS, 7, 0);
87     box(txtbox, 0, 0);
88     wnoutrefresh(txtbox);
89
90     txtwin = derwin(txtbox,
91                     getmaxy(txtbox) - 2,
92                     getmaxx(txtbox) - 2,
93                     1, 1);
94
95     keypad(txtwin, TRUE);       /* enable keyboard mapping */
96     (void) cbreak();            /* take input chars one at a time, no wait for \n */
97     (void) noecho();            /* don't echo input */
98
99     if ((fp = fopen(argv[1], "r")) != 0) {
100         while ((ch = fgetc(fp)) != EOF) {
101             if (waddch(txtwin, ch) != OK) {
102                 break;
103             }
104         }
105     } else {
106         wprintw(txtwin, "Cannot open:\n%s", argv[1]);
107     }
108     fclose(fp);
109     while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) {
110         switch (ch) {
111         case KEY_DOWN:
112         case 'j':
113             if (txt_y < getmaxy(txtwin) - 1)
114                 txt_y++;
115             else
116                 beep();
117             break;
118         case KEY_UP:
119         case 'k':
120             if (txt_y > 0)
121                 txt_y--;
122             else
123                 beep();
124             break;
125         case KEY_LEFT:
126         case 'h':
127             if (txt_x > 0)
128                 txt_x--;
129             else
130                 beep();
131             break;
132         case KEY_RIGHT:
133         case 'l':
134             if (txt_x < getmaxx(txtwin) - 1)
135                 txt_x++;
136             else
137                 beep();
138             break;
139         }
140
141         wmove(txtwin, txt_y, txt_x);
142
143         mvwprintw(chrwin, 0, 0, "char:");
144         wclrtoeol(chrwin);
145         if ((ch = winch(txtwin)) != ERR) {
146             if (waddch(chrwin, (chtype) ch) != ERR) {
147                 for (j = txt_x + 1; j < getmaxx(txtwin); ++j) {
148                     if ((ch = mvwinch(txtwin, txt_y, j)) != ERR) {
149                         if (waddch(chrwin, (chtype) ch) == ERR) {
150                             break;
151                         }
152                     } else {
153                         break;
154                     }
155                 }
156             }
157         }
158         wnoutrefresh(chrwin);
159
160         mvwprintw(strwin, 0, 0, "text:");
161         wclrtobot(strwin);
162
163         limit = getmaxx(strwin) - 5;
164
165         wmove(txtwin, txt_y, txt_x);
166         if (winchstr(txtwin, text) != ERR) {
167             mvwaddchstr(strwin, 0, 5, text);
168         }
169
170         wmove(txtwin, txt_y, txt_x);
171         if (winchnstr(txtwin, text, limit) != ERR) {
172             mvwaddchstr(strwin, 1, 5, text);
173         }
174
175         if (mvwinchstr(txtwin, txt_y, txt_x, text) != ERR) {
176             mvwaddchstr(strwin, 2, 5, text);
177         }
178
179         if (mvwinchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) {
180             mvwaddchstr(strwin, 3, 5, text);
181         }
182
183         wnoutrefresh(strwin);
184
185         /* FIXME: want stdscr tests also, but must be separate program */
186     }
187     endwin();
188     return EXIT_SUCCESS;
189 }