home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
CURSES.LZH
/
DEMO.C
< prev
next >
Wrap
Text File
|
1990-02-02
|
16KB
|
599 lines
/*
* demo.c demonstrate the method of pop-up windows without
* a hardware clear screen.
*
* a hardware clear screen could be done as follows:
* *** place just before endwin() ***
* wclear(s_image);
* wnoutrefresh(s_image);
* touchwin(pop);
* wrefresh(pop);
* endwin();
* exit(0);
*
* the wclear() not only sets the window to spaces with
* A_NORMAL attribute, but also sets the clear screen
* flag. this leaves no reason to move the cursor back
* to its origin.
*/
#include <stdio.h>
#define _NO_STDIO 1
#include <stdlib.h>
#include "curses.h"
#define CTRL(c) (c & 037)
#define MAX_WNDW 8
#define BEG_WNDW 0
#define START_ROW 5
#define START_COL 5
char *intro[] = {"\tWelcome to a demonstration of using curses on the "
,"\tIBM PC. This curses package is a public domain "
,"\tsoftware package, which includes source code and "
,"\ttwo libraries, a medimum memory model library and "
,"\ta large memory model library. "
,""
,"\tThis demonstration program will show only some of "
,"\tthe abilities of curses to use and manipulate "
,"\twindows on the display screen. A maximum of 8 "
,"\tdisplay windows will be used by this program. "
,""
,"\tPress any key to continue from this message to the"
,"\trest of the demonstration or press ESC to stop "
,"\tnow. When other windows appear you may press the "
,"\tPgUp key to move to the previous window display; "
,"\tuse the PgDn key to move to the next window "
,"\tdisplay. Also, CTRL+P instead of PgUp and CTRL+N "
,"\tcan be used instead of PgDn. ESC ends the "
,"\tdemonstration at any time. "
,""
,"\tPress any key to continue or ESC to stop. "
,(char *)NULL
};
main()
{
WINDOW
*s_image /* base window, stdscr, where everything else is laid */
,*pop[8] /* an array of pop up windows, just for demonstration */
;
int
row /* starting row of window */
,col /* starting column of window */
,height /* height in rows of a window */
,width /* width in columns of a window */
,wndx /* index into array of pop up windows */
,direction /* 0=current window; 1=next window; -1=previous window */
;
/*
* function declarations
*/
int
show_intro(WINDOW *, char *[])
;
void
fill_pop(WINDOW *, int)
,close_wndws(WINDOW *[])
,werrwarn(char *, char *, WINDOW *)
;
/*
* make s_image point to the stdscr
*/
if(initscr() == ERR)
{
fprintf(stderr,"\nCould not start curses environment.");
exit(1);
}
/*
* set certain terminal input attributes
*/
raw(); /* get all eight bits of each character from keyboard */
nonl(); /* turn off acceptance of carriage-return as newline */
noecho(); /* do not echo to the screen */
cbreak(); /* send all keystrokes directly to this application */
keypad(stdscr,TRUE); /* enable use of keys on numeric pad */
/*
* clear the window
*/
wclear(stdscr);
/*
* box in the window
*/
box(stdscr,'=','=');
/*
* display introduction message
*/
if( !show_intro(stdscr,intro) )
{
/*
* close out curses environment gracefully
*/
wmove(stdscr,LINES,0);
endwin();
exit(0);
}
/*
* loop on pop up of a max of eight windows
*/
for( wndx = 0, direction = 0, height = 8,
width = 50, row = 5, col = 5;
wndx < MAX_WNDW && direction != KEY_ESC;)
{
/*
* pop up a window only if not coming back from other window
*/
if(direction != KEY_PPAGE && direction != CTRL('P'))
{
pop[wndx] = newwin(height, width, row, col);
keypad(pop[wndx],TRUE);
fill_pop(pop[wndx],wndx);
}
/*
* read associated keyboard with window
*/
direction = wgetch(pop[wndx]);
switch (direction)
{
case KEY_ESC:
close_wndws(pop);
break;
/*
* going forward in window list
*/
/* case KEY_NPAGE: break;*/
case CTRL('N'):
case CTRL('n'):
if((row + 5) > (LINES - height))
row = START_ROW;
else
row += 5;
if( (col + 5) > (COLS - width) )
col = START_COL;
else
col += 5;
/*
* increment index into window array and retain largest
* index
*/
++wndx;
break;
/*case KEY_PPAGE: break;*/
case CTRL('P'):
case CTRL('p'):
/*
* moving to previous, requires current
* window on top be discarded
*/
wclear(pop[wndx]);
wrefresh(pop[wndx]);
delwin(pop[wndx]);
pop[wndx] = (WINDOW *)NULL;
if( --wndx < BEG_WNDW)
direction = KEY_ESC;
/*
* previous window must be redisplayed completely
*/
if( wndx >= BEG_WNDW)
{
touchwin(pop[wndx]);
wrefresh(pop[wndx]);
}
break;
}/* end of switch */
}/* end of for */
/*
* restore complete image of standard screen
*/
touchwin(stdscr);
/*
* find cursor location within window
*/
getyx(stdscr,row,col);
wattrset(stdscr,A_REVERSE);
mvwaddstr(stdscr,++row,1,"\tI hope you enjoyed the demonstration.");
mvwaddstr(stdscr,++row,1,"\tPress any key to terminate demonstration.");
wattrset(stdscr,A_DIM);
/*
* bring window image to screen
*/
wrefresh(stdscr);
wgetch(stdscr);
/*
* clear the standard screen window
*/
wclear(stdscr);
wrefresh(stdscr);
/*
* close out the curses environment
*/
endwin();
/*
* endwin() moved the cursor to LINES-1,0
* just move it back to where it started.
* not dependant on internals which endwin
* shut down.
*/
mvcur((LINES-1), 0, 0, 0);
exit(0);
}/* end of main() */
int
show_intro(basewndw,msg)
WINDOW *basewndw;
char *msg[];
{
int i
,chr
;
/*
* write the message to basewndw
* message starts on line 1 of window, because
* border is in line 0
*/
for(i = 0; msg[i]; ++i)
mvwaddstr(basewndw,i+1,1,msg[i]);
/*
* display contents of window on screen
*/
wrefresh(basewndw);
/*
* read the keyboard associated with the window for input
*/
if( (chr = wgetch(basewndw)) == KEY_ESC)
return ( FALSE); /* ESC pressed, signal calling function */
return( TRUE ); /* any key was pressed but ESC continue */
} /* end of show_intro() */
char *note1[] = { "\tHi, i'm \"pop\" window number 1."
, " "
, "\tI just pop up to show you how "
, "\tto emulate a pop-up style system"
, "\tCTRL+P=PREV CTRL+N=NEXT or "
, "\tPgUp=PREV PgDn=NEXT ESC=EXIT "
, (char *)NULL
};
char *note2[] = { "\tHi, i'm \"pop\" window number 2."
, " "
, "\tI just pop up to show you how "
, "\tto emulate a pop-up style system"
, "\tCTRL+P=PREV CTRL+N=NEXT or "
, "\tPgUp=PREV PgDn=NEXT ESC=EXIT "
, (char *)NULL
};
char *note3[] = { "\tHi, i'm \"pop\" window number 3."
, " "
, "\tI just pop up to show you how "
, "\tto emulate a pop-up style system"
, "\tCTRL+P=PREV CTRL+N=NEXT or "
, "\tPgUp=PREV PgDn=NEXT ESC=EXIT "
, (char *)NULL
};
char *note4[] = { "\tHi, i'm \"pop\" window number 4."
, " "
, "\tI just pop up to show you how "
, "\tto emulate a pop-up style system"
, "\tCTRL+P=PREV CTRL+N=NEXT or "
, "\tPgUp=PREV PgDn=NEXT ESC=EXIT "
, (char *)NULL
};
char *note5[] = { "\tHi, i'm \"pop\" window number 5."
, " "
, "\tI just pop up to show you how "
, "\tto emulate a pop-up style system"
, "\tCTRL+P=PREV CTRL+N=NEXT or "
, "\tPgUp=PREV PgDn=NEXT ESC=EXIT "
, (char *)NULL
};
char *note6[] = { "\tHi, i'm \"pop\" window number 6."
, " "
, "\tI just pop up to show you how "
, "\tto emulate a pop-up style system"
, "\tCTRL+P=PREV CTRL+N=NEXT or "
, "\tPgUp=PREV PgDn=NEXT ESC=EXIT "
, (char *)NULL
};
char *note7[] = { "\tHi, i'm \"pop\" window number 7."
, " "
, "\tI just pop up to show you how "
, "\tto emulate a pop-up style system"
, "\tCTRL+P=PREV CTRL+N=NEXT or "
, "\tPgUp=PREV PgDn=NEXT ESC=EXIT "
, (char *)NULL
};
char *note8[] = { "\tHi, i'm \"pop\" window number 8."
, " "
, "\tI just pop up to show you how "
, "\tto emulate a pop-up style system"
, "\tCTRL+P=PREV CTRL+N=NEXT or "
, "\tPgUp=PREV PgDn=NEXT ESC=EXIT "
, (char *)NULL
};
void
fill_pop(WINDOW *wndw, int indx)
{
int i;
/*
* which set of notes are to be used for this display
*/
switch (indx)
{
case 0:
box(wndw, '*', '-');
for(i=0; note1[i]; i++)
mvwaddstr(wndw, i+1, 1, note1[i]);
break;
case 1:
box(wndw, '|', '=');
for(i=0; note2[i]; ++i)
mvwaddstr(wndw, i+1, 1, note2[i]);
break;
case 2:
box(wndw,'|','=');
for(i=0; note3[i]; ++i)
mvwaddstr(wndw, i+1, 1, note3[i]);
break;
case 3:
box(wndw,'|','-');
for(i=0; note4[i]; ++i)
mvwaddstr(wndw, i+1, 1, note4[i]);
break;
case 4:
box(wndw,'*','-');
for(i=0; note5[i]; ++i)
mvwaddstr(wndw, i+1, 1, note5[i]);
break;
case 5:
box(wndw,'|','-');
for(i=0; note6[i]; ++i)
mvwaddstr(wndw, i+1, 1, note6[i]);
break;
case 6:
box(wndw,'*','=');
for(i=0; note7[i]; ++i)
mvwaddstr(wndw, i+1, 1, note7[i]);
break;
case 7:
box(wndw,'|','=');
for(i=0; note8[i]; ++i)
mvwaddstr(wndw, i+1, 1, note8[i]);
break;
}/* end of switch */
/*
* make text window visible
*/
wrefresh(wndw);
}/* end of fill_pop() */
void
close_wndws(WINDOW *wns[])
{
register int i;
/*
* loop thru array of WINDOWs and remove them
*/
for(i = 0; i < MAX_WNDW; ++i)
{
if( wns[i] != (WINDOW *)NULL)
{
wclear(wns[i]);
delwin(wns[i]);
}
}
}/* end of close_wndws() */
/*********************************************************************
* Function Name: void werrwarn(first,second,wndw)
* Source Name : \source\werrwarn.c
* Author :
* Installation :
* Date Written :
* Description : Displays a two string diagnostic message on
* line 24, waits for the user to press ESC,
* and then erases the diagnostic message.
*********************************************************************/
#include <string.h>
void
werrwarn(first,second,wn)
char
*first /* first message string to be displayed */
,*second /* second message string to be displayed */
;
WINDOW
*wn /* window that this function opens a window over */
;
{
WINDOW
*msg /* message window */
;
int
ch /* character returned from keyboard */
;
short
first_len = strlen(first); /* length of first string */
short
second_len = strlen(second); /* length of second string */
/*
* open message window
*/
if((msg = newwin(4,80,20,0)) == (WINDOW *)NULL)
{
fprintf(stderr,"ALLOCATE ERROR: Could not open window!");
exit(1);
}
/*
* draw box around the window
*/
box(msg,'*','-');
/*
* write text into window
*/
mvwaddstr(msg,1,1,first);
mvwaddstr(msg,1,2 + first_len,second);
mvwaddstr(msg,2,1,"Strike any key to continue...");
/*
* display the window
*/
wrefresh(msg);
/*
* key is pressed
*/
ch = wgetch(msg);
/*
* remove the message window
*/
wclear(msg);
delwin(msg);
touchwin(wn);
}/* end of err_warn() **************************************************/
/************************************************************************
* Function Name : void werrexit(loc,first,second,wn);
* Source Name : \source\werrwarn.c
* Author :
* Company :
* Date Written :
* Description : Displays a diagnostic message through err_warn()
* : and calls exit() to terminate execution. The
* : two part message is displayed on line 24. When
* : the user presses ESC, the message is erased.
**********************************************************************/
void
werrexit(loc,first,second,wn)
int
loc /* program location indicator */
;
char
*first /* first dipslay message */
,*second /* second display message */
;
WINDOW
*wn /* window being overlaid */
;
{
char
log_buf[512] /* buffer to be passed to logentry() */
,locnum[6] /* buffer to hold location number */
;
void logentry(), werrwarn();
strcpy(log_buf," LOCATION: ");
itoa(loc,locnum,10);
strcat(log_buf,locnum);
strcat(log_buf," ");
strcat(log_buf, first);
strcat(log_buf, " ");
strcat(log_buf, second);
logentry(log_buf,wn); /* record the message in the log */
werrwarn(first,second,wn);
endwin();
exit(loc); /* terminate program: FAIL status */
}/*end of err_exit() ***************************************************/
/**********************************************************************
* Function Name : void logentry(buf,wn)
* Source Name : \source\logentry.c
* Author :
* Company :
* Date Written :
* Date Updated :
* Description : Record error messages in a transaction log file.
*********************************************************************/
#include <time.h>
void
logentry(buf,wn)
char
*buf
;
WINDOW
*wn
;
{
int
buf_len=strlen(buf) /* length of buffer being logged */
;
static
FILE *fp = NULL; /* file pointer to logging file */
char
*datetime /* time and date */
,*gettime() /* function returning date and time */
;
/*
* Open logfile, if it isn't already open.
*/
if(!fp && !(fp = fopen("logfile.dat","a")))
{
werrwarn("No File:","logfile.dat",wn);
abort();
}
/*
* time and date stamp the log entry
*/
datetime = gettime();
fprintf(fp,"%s%s\n",datetime,buf);
}/* end of logentry() ***************************************************/
/********************************************************************
* Function Name : char *gettime();
* Source Name : \source\gettime.c
* Author : Jim Ragsdale
* Company : Copyright (C) 1989 Ragsdale Software Engineering Co.
* Date Written :
* Date Updated :
* Description : This function will retrieve the date and time
* : and return a pointer to the string.
********************************************************************/
#include <time.h>
char *gettime()
{
long timeptr; /* long integer to hold date/time value from system */
char *datetime; /* pointer to beginning of date-time string */
/*
* call the time() function to get long integer value
*/
time(&timeptr);
/*
* format date and time and return pointer
*/
datetime = ctime(&timeptr);
/*
* get rid of newline character
*/
*(datetime + 24) = '\0';
return(datetime);
}/* end of gettime() *************************************************/