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