xref: /csrg-svn/lib/libcurses/PSD.doc/ex2.c (revision 62780)
162779Sbostic .\" Copyright (c) 1992, 1993
262779Sbostic .\"	 The Regents of the University of California.  All rights reserved.
357374Selan .\"
457374Selan .\" %sccs.include.redist.roff%
557374Selan .\"
6*62780Sbostic .\"     @(#)ex2.c	8.1 (Berkeley) 06/08/93
757374Selan .\"
857374Selan #include <curses.h>
957374Selan #include <stdio.h>
1057374Selan #include <signal.h>
1157374Selan 
1257374Selan #define YSIZE LINES
1357374Selan #define XSIZE COLS
1457374Selan 
1557374Selan static int quit();
1657374Selan 
1757374Selan /*
1857374Selan  * This program fills the screen up with characters and the allows the user to
1957374Selan  * manipulate the text on the screen using some basic commands.
2057374Selan  * Nothing fancy, just a demonstration of the elementary features of the
2157374Selan  * curses(3) package.
2257374Selan  */
2357374Selan main()
2457374Selan {
2557374Selan 	int i, j, c, n, d = 0;
2657374Selan 	char id[100];
2757374Selan 	int hh = 0;
2857374Selan 	int curx, cury, base, arg;
2957374Selan 
3057374Selan 	initscr();
3157374Selan 	signal(SIGINT, quit);
3257374Selan 	crmode();
3357374Selan 	noecho();
3457374Selan 	nonl();
3557374Selan 	delwin(stdscr);
3657374Selan 	stdscr = newwin(YSIZE, XSIZE, 0, 0);
3757374Selan 	flushok(stdscr, TRUE);
3857374Selan 	scrollok(stdscr, TRUE);
3957374Selan 	erase();
4057374Selan 	refresh();
4157374Selan 
4257374Selan 	move(0,0);
4357374Selan 	refresh();
4457374Selan 	for (i = 0; i < YSIZE + 2; i++) {
4557374Selan 		sprintf(id, "%d: ", i);
4657374Selan 		addstr(id);
4757374Selan 		for (j = 0; j < XSIZE - strlen(id); j++)
4857374Selan 			addch('0' + (i % 10));
4957374Selan 	}
5057374Selan 	c = getchar();
5157374Selan 	base = 2;
5257374Selan 	curx = cury = 0;
5357374Selan 	move(0, 0);
5457374Selan 	refresh();
5557374Selan 
5657374Selan 	/*
5757374Selan 	 * The screen manipulator has the following commands:
5857374Selan 	 * 'D' - clear to the end of the current line.
5957374Selan 	 * 'B' - clear to the bottom of the screen.
6057374Selan 	 * 'E' - erase the screen.
6157374Selan 	 * 's' - enter standout mode.
6257374Selan 	 * 'e' - exit standout mode.
6357374Selan 	 * 'd' n - delete n lines below cursor line.
6457374Selan 	 * 'i' n - insert n lines below cursor line.
6557374Selan 	 * 'q' - quit.
6657374Selan 	 * 'f' - move cursor one position to the right.
6757374Selan 	 * 'b' - move cursor one position to the left.
6857374Selan 	 * 'n' - move cursor one line down.
6957374Selan 	 * 'p' - move cursor one line up.
7057374Selan 	 * 'h' - home cusor.
7157374Selan 	 * 'l' - force refresh.
7257374Selan 	 * 'r' - simulate a carriage return.
7357374Selan 	 *
7457374Selan 	 * All other characters are ignored.
7557374Selan 	 */
7657374Selan 	for(;;) {
7757374Selan 		switch(c = getchar()) {
7857374Selan 		case 'D':
7957374Selan 			clrtoeol();
8057374Selan 			refresh();
8157374Selan 			continue;
8257374Selan 		case 'B':
8357374Selan 			clrtobot();
8457374Selan 			refresh();
8557374Selan 			continue;
8657374Selan 		case 'E':
8757374Selan 			erase();
8857374Selan 			refresh();
8957374Selan 			continue;
9057374Selan 		case 's':
9157374Selan 			standout();
9257374Selan 			continue;
9357374Selan 		case 'e':
9457374Selan 			standend();
9557374Selan 			continue;
9657374Selan 		case 'd':
9757374Selan 			arg = getchar() - '0';
9857374Selan 			for (i = 0; i < arg; i++)
9957374Selan 				deleteln();
10057374Selan 			refresh();
10157374Selan 			continue;
10257374Selan 		case 'i':
10357374Selan 			arg = getchar() - '0';
10457374Selan 			for (i = 0; i < arg; i++)
10557374Selan 				insertln();
10657374Selan 			refresh();
10757374Selan 			continue;
10857374Selan 		case 'q':
10957374Selan 			quit();
11057374Selan 		case 'f':
11157374Selan 			if (curx < XSIZE - 1)
11257374Selan 				curx++;
11357374Selan 			else {
11457374Selan 				cury++;
11557374Selan 				curx = 0;
11657374Selan 			}
11757374Selan 			break;
11857374Selan 		case 'b':
11957374Selan 			if (curx == 0) {
12057374Selan 				cury--;
12157374Selan 				curx = XSIZE - 1;
12257374Selan 			} else
12357374Selan 				curx--;
12457374Selan 			break;
12557374Selan 		case 'n':
12657374Selan 			cury++;
12757374Selan 			break;
12857374Selan 		case 'p':
12957374Selan 			cury--;
13057374Selan 			break;
13157374Selan 		case 'h':
13257374Selan 			curx = cury = 0;
13357374Selan 			break;
13457374Selan 		case 'l':
13557374Selan 			wrefresh(curscr);
13657374Selan 			continue;
13757374Selan 		case 'r':   /* return */
13857374Selan 		{
13957374Selan 			int x, y;
14057374Selan 			getyx(stdscr, y, x);
14157374Selan 			move(y+1, 0);
14257374Selan 			insertln();
14357374Selan 			move(y, x);
14457374Selan 			clrtoeol();
14557374Selan 			refresh();
14657374Selan 			continue;
14757374Selan 		}
14857374Selan 		default:
14957374Selan 			continue;
15057374Selan 		}
15157374Selan 
15257374Selan 		if (cury < 0) {
15357374Selan 			base--;
15457374Selan 			move(0, 0);
15557374Selan 			insertln();
15657374Selan 			sprintf(id, "%d: ", base);
15757374Selan 			addstr(id);
15857374Selan 			for (j = 0; j < XSIZE - strlen(id) - 2; j++)
15957374Selan 				addch('0' + (base % 10));
16057374Selan 			cury++;
16157374Selan 		} else if (cury >= YSIZE) {
16257374Selan 			move(0, 0);
16357374Selan 			deleteln();
16457374Selan 			move(YSIZE - 1, 0);
16557374Selan 			sprintf(id, "%d: ", base + YSIZE);
16657374Selan 			addstr(id);
16757374Selan 			for (j = 0; j < XSIZE - strlen(id) - 2; j++)
16857374Selan 				addch('0' + ((base + YSIZE) % 10));
16957374Selan 			cury--;
17057374Selan 			base++;
17157374Selan 		}
17257374Selan 		move(cury, curx);
17357374Selan 		refresh();
17457374Selan 	}
17557374Selan }
17657374Selan 
17757374Selan int
17857374Selan quit()
17957374Selan {
18057374Selan 	erase();
18157374Selan 	refresh();
18257374Selan 	endwin();
18357374Selan 	exit(0);
18457374Selan }
185