home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DP Tool Club 26
/
CD_ASCQ_26_1295.iso
/
vrac
/
user_int.zip
/
INT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-08-28
|
15KB
|
494 lines
#include <dir.h>
#include <ctype.h>
#include <stdlib.h>
#include <process.h>
// contains menu definition and SIMPLE instructions //
#include "int.h"
// do not touch! window class library and mouse event hanler //
// use functions from window class to display text on your screens //
#include "window.cpp"
#include "mhandler.cpp"
// do not touch! //
#define scroll_while_pulled -2
// do not touch these! add your own if needed //
struct text_info ti; // to save cursor position //
wintype w1 (0,0,79,24,1,""); // main screen must be full screen //
int xl, xr, i, key, mcnt, lastsel=0, xpos[21], pulled, scrolltype; // globals //
int fail; // global tracking int //
// do not touch! function also makes mouse repeat/delay rate like keyboard //
int getkey(void)
{
Mshow();
while(L_DOWN || R_DOWN)
{ if(mcnt == 400)
break;
delay(1);
mcnt++;
}
if(!L_DOWN && !R_DOWN)
{ mcnt=0;
}
while(!kbhit())
{ if(L_DOWN)
{ delay(20);
return 999; // I use 999 for left as no other key could be 999 //
}
if(R_DOWN)
{ delay(20);
return 1000; // I use 1000 for right //
} // you can write a mini key program using the following lines //
} // to determine what value any key will return to the caller() //
switch (key = (int)getch())
{ case 0: return 256 + (int)getch();
default: return key;
}
}
// draws main screen //
void draw_window(void)
{
if(vmode==3) { w1.setcolor(white); w1.setbkcolor(blue); }
else { w1.setcolor(white); w1.setbkcolor(black); }
w1.winput();
if(vmode!=3)
w1.line_border();
w1.wincls();
w1.setcolor(black); w1.setbkcolor(lightgray);
for(i=1; i<=80; i++)
{ w1.wcout(i,1," ");
}
}
// aborts program if your items do not fit on screen //
void size_error(int size)
{
w1.winremove();
clrscr();
printf("\nError...\nMenu items do not fit on the screen.\nMaximum size is 80 total characters.");
printf("\nCurrent total: %d characters.", size);
printf("\n\nTo calculate total characters, add 2 to the length of the"
"\nfirst item, then add 4 to the length of all other items."
"\n\nExample: Item 1 - \"File\" - 4 + 2 = 6,"
"\n Item 2 - \"Edit\" - 4 + 4 = 8,"
"\n Item 3 - \"Search\" - 6 + 4 = 10,"
"\n Item 4 - \"Help\" - 4 + 4 = 8,"
"\n\n Total characters: 6 + 8 + 10 + 8 = 32.\n"
"\n\nEdit the MENUS.H file to correct the problem.\n");
Mhide();
Mreset();
_setcursortype(_NORMALCURSOR);
exit(1);
}
// do not touch! //
void showmsg(char *message)
{
w1.setcolor(black); w1.setbkcolor(lightgray);
for(i=1; i<=80; i++)
{ w1.wcout(i,25," ");
}
w1.wcout(2,25,message);
}
// do not touch! //
void showitem(int x, int y, char *item, int hotkey, int hl)
{
int test=0;
char temp[80];
if(hl==1)
{ if(vmode==3) { w1.setcolor(black); w1.setbkcolor(cyan); }
else { w1.setcolor(lightgray); w1.setbkcolor(black); }
}
else
{ if(vmode==3) { w1.setcolor(black); w1.setbkcolor(lightgray); }
else { w1.setcolor(black); w1.setbkcolor(lightgray); }
}
sprintf(temp," %s ",item);
w1.wcout(x-1,y,temp);
for(i=0;mainmenu[i]!=NULL;i++)
{ if(strcmp(mainmenu[i],item)==0)
test=1;
}
if(test==0)
{ for(i=x+strlen(item);i<=xr;i++)
w1.wcout(i,y," ");
}
if(hl==1)
{ if(vmode==3) { w1.setcolor(red); w1.setbkcolor(cyan); }
else { w1.setcolor(white); w1.setbkcolor(black); }
}
else
{ if(vmode==3) { w1.setcolor(red); w1.setbkcolor(lightgray); }
else { w1.setcolor(white); w1.setbkcolor(lightgray); }
}
sprintf(temp,"%c", item[hotkey]);
w1.wcout(x+hotkey,y,temp);
w1.setcolor(black); w1.setbkcolor(lightgray);
}
// do not touch! //
void scroll_pulled(void)
{
int i, tot;
for(i=0;mainmenu[i]!=NULL;i++)
tot=i;
tot=i-1;
if(scrolltype==0)
{ if(lastsel==0)
key=lastsel=tot;
else key=lastsel=lastsel-1;
}
else
{ if(lastsel==tot)
key=lastsel=0;
else key=lastsel=lastsel+1;
}
}
// do not touch! fully automatic! lots of math! //
int pulldown_menu(char *items[], int hotkeys[], char *messages[])
{
int i, l, len, done=0, num, max, sel;
xr=0; xl=xpos[lastsel]-2;
for(i=0; items[i]!=NULL; i++)
{ len=strlen(items[i])+3;
if(len>xr)
xr=len;
}
xr+=xl; l=xl;
if(xr>79)
{ len=xr-xl;
xr=79;
xl=79-len;
}
if(xl<0) xl=0;
wintype pdown(xl,1,xr,i+2,1,"");
pdown.setcolor(black); pdown.setbkcolor(lightgray);
pdown.winput(); pdown.line_border(); pdown.wincls();
showitem(xl+3,3,items[0],hotkeys[0],1);
for(i=1; items[i]!=NULL; i++)
{ showitem(xl+3,i+3,items[i],hotkeys[i],0);
}
max=i-1; num=0; pulled=0;
showmsg(messages[num]);
while(done==0)
{ Mshow();
switch(sel=getkey())
{ case 27: sel= -1; done=1;
break;
case 8:
case 328: Mhide();
showitem(xl+3,num+3,items[num],hotkeys[num],0);
num--; if(num==-1) { num=-1; done=1; break; }
showitem(xl+3,num+3,items[num],hotkeys[num],1);
showmsg(messages[num]);
break;
case 9:
case 32:
case 336: Mhide();
showitem(xl+3,num+3,items[num],hotkeys[num],0);
num++; if(num==max+1) num=0;
showitem(xl+3,num+3,items[num],hotkeys[num],1);
showmsg(messages[num]);
break;
case 13: sel=num; done=1;
break;
case 331: sel=scroll_while_pulled; pulled=1; scrolltype=0; done=1;
break;
case 333: sel=scroll_while_pulled; pulled=1; scrolltype=1; done=1;
break;
case 999: while(L_DOWN)
{ Mshow();
if(M_ypos>=3 && M_ypos<=max+3 && M_xpos>=xl+2 && M_xpos<=xr)
{ sel=M_ypos-3;
if(sel<0) sel=0; if(sel>max) sel=max;
if(num!=sel)
{ showitem(xl+3,num+3,items[num],hotkeys[num],0);
Mhide();
showitem(xl+3,sel+3,items[sel],hotkeys[sel],1);
num=sel;
showmsg(messages[sel]);
}
}
if((M_ypos==1) && (M_xpos<l+1 || M_xpos>l+strlen(mainmenu[lastsel])+2))
{ sel= -1; done=1; break;
}
if(!L_DOWN) done=1;
}
if(M_xpos<=xl+1 || M_xpos>xr || M_ypos<3 || M_ypos>max+3)
{ sel= -1; done=1; break;
}
break;
default : for(i=0; items[i]!=NULL; i++)
{ if(sel>32 && sel<127 && toupper(items[i][hotkeys[i]]) == toupper(sel))
{ sel=i;
done=1;
break;
}
}
break;
}
}
Mhide();
pdown.winremove();
return sel;
}
// do not touch! fully automatic! lots of math! //
int main_menu(char *items[], int hotkeys[], char *messages[])
{
int i, pos=3, done=0, num, max, sel;
w1.setcolor(black); w1.setbkcolor(lightgray);
for(i=0; items[i]!=NULL; i++)
{ if(pos+strlen(items[i])-1>80)
{ size_error(pos+strlen(items[i])-1);
}
xpos[i]=pos;
showitem(pos,1,items[i],hotkeys[i],0);
pos+=strlen(items[i])+4;
}
showitem(xpos[lastsel],1,items[lastsel],hotkeys[lastsel],1);
max=i-1; num=lastsel;
showmsg(messages[lastsel]);
if(pulled==1) return key;
while(done==0)
{ Mshow();
switch(sel=getkey())
{ case 27: sel= -1; done=1;
break;
case 8:
case 331: Mhide();
showitem(xpos[num],1,items[num],hotkeys[num],0);
num--; if(num==-1) num=max;
sel=num;
showitem(xpos[num],1,items[num],hotkeys[num],1);
showmsg(messages[num]);
break;
case 9:
case 32:
case 333: Mhide();
showitem(xpos[num],1,items[num],hotkeys[num],0);
num++; if(num==max+1) num=0;
sel=num;
showitem(xpos[num],1,items[num],hotkeys[num],1);
showmsg(messages[num]);
break;
case 336:
case 13: sel=num; done=1;
break;
case 999: if(M_ypos==1)
{ for(i=0; i<=max; i++)
{ if(M_xpos>=xpos[i] && M_xpos<=xpos[i]+strlen(items[i])-1)
{ Mhide();
sel=i;
showitem(xpos[num],1,items[num],hotkeys[num],0);
num=sel;
showitem(xpos[num],1,items[num],hotkeys[num],1);
showmsg(messages[num]);
done=1;
break;
}
}
}
break;
default : for(i=0; items[i]!=NULL; i++)
{ if(sel>32 && sel<127 && toupper(items[i][hotkeys[i]]) == toupper(sel))
{ Mhide();
showitem(xpos[num],1,items[num],hotkeys[num],0);
num=sel=i;
showitem(xpos[num],1,items[num],hotkeys[num],1);
done=1;
break;
}
}
break;
}
}
if(sel!=-1)
lastsel=sel;
Mhide();
return sel;
}
void message(char *msg) // centers and displays a message //
{
int half;
half = strlen(msg) / 2;
xl = 40 - half - 3;
xr = 40 + half + 3;
wintype m(xl, 7, xr, 9, 1, "");
m.setcolor(white); if(vmode==3) m.setbkcolor(red);
else m.setbkcolor(black); m.winput(); m.wincls();
m.line_border();
m << " " << msg;
getkey();
key=0;
Mhide();
m.winremove();
}
void shell(char *prompt_string) // shell to dos with new prompt //
{
char *comspec, prompt[256], *oldprompt;
Mhide();
w1.winremove();
gotoxy(ti.curx, ti.cury);
_setcursortype(_NORMALCURSOR);
comspec = getenv("COMSPEC");
if(comspec == NULL)
comspec = "COMMAND.COM";
oldprompt = getenv("PROMPT");
sprintf(prompt, "PROMPT=%s$_$p$g", prompt_string);
putenv(prompt);
spawnlp(P_WAIT, comspec, comspec, NULL);
Minsthandler();
sprintf(prompt,"PROMPT=%s", oldprompt);
putenv(prompt);
_setcursortype(_NOCURSOR);
gettextinfo(&ti);
return;
}
// spawns my file viewer. included free with source code. //
int help(char *filename, char *viewer)
{
char *p;
char help_parameter[81];
p = searchpath(filename);
if(!p) return -1;
Mhide();
w1.winremove();
strcpy(help_parameter, p);
spawnlp(P_WAIT, viewer, viewer, help_parameter, NULL);
_setcursortype(_NOCURSOR);
set_v_ptr();
Minsthandler();
return 1;
}
void about(void) // add your own about info and size window accordingly //
{
wintype a(25,7,55,11,1,"");
a.setcolor(black); a.setbkcolor(lightgray);
a.winput(); a.wincls(); a.line_border();
a << " User Interface, Version 1.0";
a << "\n Jay Lewis, 1995";
a << "\n Free!";
getkey();
key=0;
Mhide();
a.winremove();
}
void stub(void) // dummy function //
{
wintype s(25,7,55,10,1,"");
s.setcolor(black); s.setbkcolor(lightgray);
s.winput(); s.wincls(); s.line_border();
s << " Stub function executed.";
s << "\n Press any key ...";
getkey();
key=0;
Mhide();
s.winremove();
}
int main(void)
{ // don't touch the following //
gettextinfo(&ti); // to save cursor position //
set_v_ptr(); // must set video pointer to use window class //
Mreset(); // resets mouse //
Minsthandler(); // installs mouse handler //
mcnt=0; // initializes mouse repeat/delay rate tracker //
_setcursortype(_NOCURSOR); // no cursor //
draw_window();
// Just modify the case statements to execute your functions. //
// Selecting the first main menu choice returns a zero, second 1, etc. //
// Add or remove menu items and cases as desired. Match to INT.H file. //
// Don't forget to hide mouse when changing screen in your functions. //
// Check my example functions above to see how. Use the WINDOW.CPP //
// functions as shown in the above functions. Very simple class. //
// Fully mouse driven if a mouse is present. Otherwise kb driven. //
while(key!=27)
{ switch(key=main_menu(mainmenu,mainkeys,mainmsgs))
{ case 0: /*number 1*/
switch(key=pulldown_menu(pull0items,pull0keys,pull0msgs))
{ // first pulldown item returns a zero, next a 1, etc. //
case 0: /*first*/ stub(); /* or your function here */ break;
case 1: /*second*/ stub(); /* or your function here */ break;
case 2: /*third*/ stub(); /* or your function here */ break;
case 3: /*fourth*/ stub(); /* or your function here */ break;
case 4: /*fifth*/ stub(); /* or your function here */ break;
case 5: /*sixth*/ stub(); /* or your function here */ break;
case 6: /*seventh*/ stub(); /* or your function here */ break;
default: break;
}
break;
case 1: /*number 2*/
switch(key=pulldown_menu(pull1items,pull1keys,pull1msgs))
{ case 0: /*only*/ stub(); /* or your function here */ break;
default: break;
}
break;
case 2: /*number 3*/
switch(key=pulldown_menu(pull2items,pull2keys,pull2msgs))
{ case 0: /*first*/ stub(); /* or your function here */ break;
case 1: /*second*/ stub(); /* or your function here */ break;
default: break;
}
break;
case 3: /*shell*/
switch(key=pulldown_menu(pull3items,pull3keys,pull3msgs))
{ case 0: /*shell*/ shell("Type EXIT to return to User Interface"); draw_window(); break;
default: break;
}
break;
case 4: /*quit*/
switch(key=pulldown_menu(pull4items,pull4keys,pull4msgs))
{ case 0: /*quit*/ key=27; break;
default: break;
}
break;
case 5: /*read,about*/
switch(key=pulldown_menu(pull5items,pull5keys,pull5msgs))
{ case 0: /*read*/ fail=help("int.h","read.exe");
if(fail != -1) draw_window();
else message("File not found."); break;
case 1: /*about*/ about(); break;
default: break;
}
break;
// leave this alone! to scroll left/right while pulldowns extended //
case scroll_while_pulled: scroll_pulled(); break;
default : break;
}
}
// leave this alone //
w1.winremove(); // removes main screen //
_setcursortype(_NORMALCURSOR); // turns on cursor //
gotoxy(ti.curx, ti.cury); // goes to saved cursor position //
Mhide(); // hide mouse //
Mreset(); // reset mouse //
return 0; // exit //
}
// Note: you can also download my directory scanner function with source //
// called READDIR.ZIP (free). Or you may extract it from the READ.CPP //
// viewer code included. It builds a list of drives, directories, and //
// files for further display to the user. Uses dynamically allocated //
// memory for storage and files are sortable by name or extension. //
// Another super easy one to use! //
// All by Jay Lewis, dedicated to helping the novice programmer with //
// the GOOD STUFF! Please write, 10365,3264 at Compuserve. //