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