1 .\" Copyright (c) 1992, 1993 2 .\" The Regents of the University of California. All rights reserved. 3 .\" 4 .\" Redistribution and use in source and binary forms, with or without 5 .\" modification, are permitted provided that the following conditions 6 .\" are met: 7 .\" 1. Redistributions of source code must retain the above copyright 8 .\" notice, this list of conditions and the following disclaimer. 9 .\" 2. Redistributions in binary form must reproduce the above copyright 10 .\" notice, this list of conditions and the following disclaimer in the 11 .\" documentation and/or other materials provided with the distribution. 12 .\" 3. Neither the name of the University nor the names of its contributors 13 .\" may be used to endorse or promote products derived from this software 14 .\" without specific prior written permission. 15 .\" 16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 .\" SUCH DAMAGE. 27 .\" 28 .\" @(#)ex2.c 8.1 (Berkeley) 6/8/93 29 .\" 30 #include <curses.h> 31 #include <stdio.h> 32 #include <signal.h> 33 34 #define YSIZE LINES 35 #define XSIZE COLS 36 37 static int quit(); 38 39 /* 40 * This program fills the screen up with characters and the allows the user to 41 * manipulate the text on the screen using some basic commands. 42 * Nothing fancy, just a demonstration of the elementary features of the 43 * curses(3) package. 44 */ 45 main() 46 { 47 int i, j, c, n, d = 0; 48 char id[100]; 49 int hh = 0; 50 int curx, cury, base, arg; 51 52 initscr(); 53 signal(SIGINT, quit); 54 crmode(); 55 noecho(); 56 nonl(); 57 delwin(stdscr); 58 stdscr = newwin(YSIZE, XSIZE, 0, 0); 59 flushok(stdscr, TRUE); 60 scrollok(stdscr, TRUE); 61 erase(); 62 refresh(); 63 64 move(0,0); 65 refresh(); 66 for (i = 0; i < YSIZE + 2; i++) { 67 sprintf(id, "%d: ", i); 68 addstr(id); 69 for (j = 0; j < XSIZE - strlen(id); j++) 70 addch('0' + (i % 10)); 71 } 72 c = getchar(); 73 base = 2; 74 curx = cury = 0; 75 move(0, 0); 76 refresh(); 77 78 /* 79 * The screen manipulator has the following commands: 80 * 'D' - clear to the end of the current line. 81 * 'B' - clear to the bottom of the screen. 82 * 'E' - erase the screen. 83 * 's' - enter standout mode. 84 * 'e' - exit standout mode. 85 * 'd' n - delete n lines below cursor line. 86 * 'i' n - insert n lines below cursor line. 87 * 'q' - quit. 88 * 'f' - move cursor one position to the right. 89 * 'b' - move cursor one position to the left. 90 * 'n' - move cursor one line down. 91 * 'p' - move cursor one line up. 92 * 'h' - home cusor. 93 * 'l' - force refresh. 94 * 'r' - simulate a carriage return. 95 * 96 * All other characters are ignored. 97 */ 98 for(;;) { 99 switch(c = getchar()) { 100 case 'D': 101 clrtoeol(); 102 refresh(); 103 continue; 104 case 'B': 105 clrtobot(); 106 refresh(); 107 continue; 108 case 'E': 109 erase(); 110 refresh(); 111 continue; 112 case 's': 113 standout(); 114 continue; 115 case 'e': 116 standend(); 117 continue; 118 case 'd': 119 arg = getchar() - '0'; 120 for (i = 0; i < arg; i++) 121 deleteln(); 122 refresh(); 123 continue; 124 case 'i': 125 arg = getchar() - '0'; 126 for (i = 0; i < arg; i++) 127 insertln(); 128 refresh(); 129 continue; 130 case 'q': 131 quit(); 132 case 'f': 133 if (curx < XSIZE - 1) 134 curx++; 135 else { 136 cury++; 137 curx = 0; 138 } 139 break; 140 case 'b': 141 if (curx == 0) { 142 cury--; 143 curx = XSIZE - 1; 144 } else 145 curx--; 146 break; 147 case 'n': 148 cury++; 149 break; 150 case 'p': 151 cury--; 152 break; 153 case 'h': 154 curx = cury = 0; 155 break; 156 case 'l': 157 wrefresh(curscr); 158 continue; 159 case 'r': /* return */ 160 { 161 int x, y; 162 getyx(stdscr, y, x); 163 move(y+1, 0); 164 insertln(); 165 move(y, x); 166 clrtoeol(); 167 refresh(); 168 continue; 169 } 170 default: 171 continue; 172 } 173 174 if (cury < 0) { 175 base--; 176 move(0, 0); 177 insertln(); 178 sprintf(id, "%d: ", base); 179 addstr(id); 180 for (j = 0; j < XSIZE - strlen(id) - 2; j++) 181 addch('0' + (base % 10)); 182 cury++; 183 } else if (cury >= YSIZE) { 184 move(0, 0); 185 deleteln(); 186 move(YSIZE - 1, 0); 187 sprintf(id, "%d: ", base + YSIZE); 188 addstr(id); 189 for (j = 0; j < XSIZE - strlen(id) - 2; j++) 190 addch('0' + ((base + YSIZE) % 10)); 191 cury--; 192 base++; 193 } 194 move(cury, curx); 195 refresh(); 196 } 197 } 198 199 int 200 quit() 201 { 202 erase(); 203 refresh(); 204 endwin(); 205 exit(0); 206 } 207