Category : C Source Code
Archive   : POWER-C.ZIP
Filename : TEST.C

 
Output of file : TEST.C contained in archive : POWER-C.ZIP

#include
#include
#include

#define BORDER 1
#define ESC 27
#define REV_VID 0x70
#define NORM_VID 7

void save_video(), restore_video();
void goto_xy(), cls(), write_video();
void display_menu(), draw_border();

char *fruit[]= {
"Apple",
"Orange",
"Pear",
"Grape",
"Raspberry",
"Strawberry"
};

char *color[]= {
"Red",
"Yellow",
"Orange",
"Green",
};

char *apple_type[]= {
"Red delicious",
"Jonathan",
"Winesap",
"Rome"
};

main()
{
int i;

cls();
goto_xy(0,0);
for(i=0; i<25; i++)
printf("This is a test of the pop-up window routines.\n");
popup(fruit, "aopgrs", 6, 1, 3, BORDER);
popup(color, "ryog", 4, 5, 10, BORDER);
popup(apple_type, "rjwr", 4, 10, 18, BORDER);
}

/* display a pop-up menu and return selection
returns -2 if menu cannot be constructed
returns -1 if user hits escape key
otherwise the item number is returned starting
with 0 as the first (top most) entry
*/
int popup(menu, keys, count, x, y, border)
char *menu[]; /* menu text */
char *keys; /* hot keys */
int count; /* number of menu items */
int x, y; /* X,Y coordinates of left hand corner */
int border; /* no border if 0 */
{
register int i, len;
int endx, endy, choice;
unsigned int *p;

if((x>24) || (x<0) || (y>79) || (y<0)) {
printf("range error");
return -2;
}

/* compute the dimensions */
len = 0;
for(i=0; i if(strlen(menu[i]) > len) len = strlen(menu[i]);
endy = len + 2 + y;
endx = count + 1 + x;
if((endx+1>24) || (endy+1>79)) {
printf("menu won't fit");
return -2;
}

/* allocate enough memory for it */
p = (unsigned int *) malloc((endx-x+1) * (endy-y+1));
if(!p) exit(1); /* install your own error handler here */

/* save the current screen data */
save_video(x, endx+1, y, endy+1, p);

if(border) draw_border(x, y, endx, endy);

/* display the menu */
display_menu(menu, x+1, y+1, count);

/* get the user's response */
choice = get_resp(x+1, y, count, menu, keys);

/* restore the original screen */
restore_video(x, endx+1, y, endy+1, (char *) p);
free(p);
return choice;
}

/* display the menu in its proper location */
void display_menu(menu, x, y, count)
char *menu[];
int x, y, count;
{
register int i;

for(i=0; i goto_xy(x, y);
printf(menu[i]);
}
}

void draw_border(startx, starty, endx, endy)
int startx, starty, endx, endy;
{
register int i;

for(i=startx+1; i goto_xy(i, starty);
putchar(179);
goto_xy(i, endy);
putchar(179);
}

for(i=starty+1; i goto_xy(startx, i);
putchar(196);
goto_xy(endx, i);
putchar(196);
}
goto_xy(startx, starty); putchar(218);
goto_xy(startx, endy); putchar(191);
goto_xy(endx, starty); putchar(192);
goto_xy(endx, endy); putchar(217);

}

/* input user's selection */
get_resp(x, y, count, menu, keys)
int x, y, count;
char *menu[];
char *keys;
{
union inkey {
char ch[2];
int i;
} c;
int arrow_choice=0, key_choice;

y++;

/* highlight the first selection */
goto_xy(x, y);
write_video(x, y, menu[0], REV_VID); /* reverse video */

for(;;) {
while(!bioskey(1)) ; /* wait for key stroke */
c.i = bioskey(0); /* read the key */

/* reset the selection to normal video */
goto_xy(x+arrow_choice, y);
write_video(x+arrow_choice, y,
menu[arrow_choice], NORM_VID); /* redisplay */

if(c.ch[0]) { /* is normal key */
/* see if it is a hot key */
key_choice = is_in(keys, tolower(c.ch[0]));
if(key_choice) return key_choice-1;
/* check for ENTER or space bar */
switch(c.ch[0]) {
case '\r': return arrow_choice;
case ' ' : arrow_choice++;
break;
case ESC : return -1; /* cancel */
}
}
else { /* is special key */
switch(c.ch[1]) {
case 72: arrow_choice--; /* up arrow */
break;
case 80: arrow_choice++; /* down arrow */
break;
}
}
if(arrow_choice==count) arrow_choice=0;
if(arrow_choice<0) arrow_choice=count-1;

/* highlight the next selection */
goto_xy(x+arrow_choice, y);
write_video(x+arrow_choice, y, menu[arrow_choice], REV_VID);
}
}

/* display a string with specified attribute */
void write_video(x, y, p, attrib)
int x, y;
char *p;
int attrib;
{
union REGS r;
register int i,j;

for(i=y; *p; i++) {
goto_xy(x, i);
r.h.ah = 9; /* write character function */
r.h.bh = 0; /* assume active display page is 0 */
r.x.cx = 1; /* number of times to write the character */
r.h.al = *p++; /* character */
r.h.bl = attrib; /* attribute */
int86(0x10, &r, &r);
}
}


/* save a portion of the screen */
void save_video(startx, endx, starty, endy, buf_ptr)
int startx, endx, starty, endy;
unsigned int *buf_ptr;
{
union REGS r;
register int i,j;
for(i=starty; i for(j=startx; j goto_xy(j, i);
r.h.ah = 8; /* read character function */
r.h.bh = 0; /* assume active display page is 0 */
*buf_ptr++ = int86(0x10, &r, &r);
putchar(' '); /* clear the screen */
}
}

/* restore a portion of the screen */
void restore_video(startx, endx, starty, endy, buf_ptr)
int startx, endx, starty, endy;
unsigned char *buf_ptr;
{
union REGS r;
register int i,j;

for(i=starty; i for(j=startx; j goto_xy(j, i);
r.h.ah = 9; /* write character function */
r.h.bh = 0; /* assume active display page is 0 */
r.x.cx = 1; /* number of times to write the character */
r.h.al = *buf_ptr++; /* character */
r.h.bl = *buf_ptr++; /* attribute */
int86(0x10, &r, &r);
}
}


/* clear the screen */
void cls()
{
union REGS r;

r.h.ah=6; /* screen scroll code */
r.h.al=0; /* clear screen code */
r.h.ch=0; /* start row */
r.h.cl=0; /* start column */
r.h.dh=24; /* end row */
r.h.dl=79; /* end column */
r.h.bh=7; /* blank line is blank */
int86(0x10, &r, &r);
}

/* send the cursor to x,y */
void goto_xy(x,y)
int x,y;
{
union REGS r;

r.h.ah=2; /* cursor addressing function */
r.h.dl=y; /* column coordinate */
r.h.dh=x; /* row coordinate */
r.h.bh=0; /* video page */
int86(0x10, &r, &r);
}

is_in(s, c)
char *s, c;
{
register int i;

for(i=0; *s; i++) if(*s++==c) return i+1;
return 0;
}

/* emulate part of the Turbo C bioskey() function */
bioskey(c)
int c;
{
switch(c) {
case 0: return get_key();
case 1: return kbhit();
}
}

/* read the 16-bit scan code of a key */
get_key()
{
union REGS r;

r.h.ah = 0;
return int86(0x16, &r, &r);
}



  3 Responses to “Category : C Source Code
Archive   : POWER-C.ZIP
Filename : TEST.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/