home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 26 / CD_ASCQ_26_1295.iso / vrac / user_int.zip / INT.CPP < prev    next >
C/C++ Source or Header  |  1995-08-28  |  15KB  |  494 lines

  1. #include <dir.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <process.h>
  5.  
  6. // contains menu definition and SIMPLE instructions //
  7. #include "int.h"
  8.  
  9. // do not touch!  window class library and mouse event hanler //
  10. // use functions from window class to display text on your screens //
  11. #include "window.cpp"
  12. #include "mhandler.cpp"
  13.  
  14. // do not touch! //
  15. #define scroll_while_pulled -2
  16.  
  17. // do not touch these!  add your own if needed //
  18.    struct text_info ti; // to save cursor position //
  19.    wintype w1 (0,0,79,24,1,""); // main screen must be full screen //
  20.    int xl, xr, i, key, mcnt, lastsel=0, xpos[21], pulled, scrolltype; // globals //
  21.    int fail; // global tracking int //
  22.  
  23. // do not touch! function also makes mouse repeat/delay rate like keyboard //
  24. int getkey(void)
  25. {
  26.    Mshow();
  27.    while(L_DOWN || R_DOWN)
  28.    { if(mcnt == 400)
  29.      break;
  30.      delay(1);
  31.      mcnt++;
  32.     }
  33.    if(!L_DOWN && !R_DOWN)
  34.    { mcnt=0;
  35.     }
  36.    while(!kbhit())
  37.    { if(L_DOWN)
  38.      { delay(20);
  39.        return 999; // I use 999 for left as no other key could be 999 //
  40.       }
  41.      if(R_DOWN)
  42.      { delay(20);
  43.        return 1000; // I use 1000 for right //
  44.       }      // you can write a mini key program using the following lines //
  45.     }        // to determine what value any key will return to the caller() //
  46.    switch (key = (int)getch())
  47.    { case 0: return 256 + (int)getch();
  48.      default: return key;
  49.     }
  50.  }
  51. // draws main screen //
  52. void draw_window(void)
  53. {
  54.    if(vmode==3) { w1.setcolor(white); w1.setbkcolor(blue); }
  55.    else { w1.setcolor(white); w1.setbkcolor(black); }
  56.    w1.winput();
  57.    if(vmode!=3)
  58.    w1.line_border();
  59.    w1.wincls();
  60.    w1.setcolor(black); w1.setbkcolor(lightgray);
  61.    for(i=1; i<=80; i++)
  62.    { w1.wcout(i,1," ");
  63.     }
  64.  }
  65. // aborts program if your items do not fit on screen //
  66. void size_error(int size)
  67. {
  68.    w1.winremove();
  69.    clrscr();
  70.    printf("\nError...\nMenu items do not fit on the screen.\nMaximum size is 80 total characters.");
  71.    printf("\nCurrent total:  %d characters.", size);
  72.    printf("\n\nTo calculate total characters, add 2 to the length of the"
  73.         "\nfirst item, then add 4 to the length of all other items."
  74.       "\n\nExample:  Item 1 - \"File\"   -  4 + 2 = 6,"
  75.         "\n          Item 2 - \"Edit\"   -  4 + 4 = 8,"
  76.         "\n          Item 3 - \"Search\" -  6 + 4 = 10,"
  77.         "\n          Item 4 - \"Help\"   -  4 + 4 = 8,"
  78.       "\n\n          Total characters:  6 + 8 + 10 + 8 = 32.\n"
  79.       "\n\nEdit the MENUS.H file to correct the problem.\n");
  80.    Mhide();
  81.    Mreset();
  82.    _setcursortype(_NORMALCURSOR);
  83.    exit(1);
  84.  }
  85. // do not touch! //
  86. void showmsg(char *message)
  87. {
  88.    w1.setcolor(black); w1.setbkcolor(lightgray);
  89.    for(i=1; i<=80; i++)
  90.    { w1.wcout(i,25," ");
  91.     }
  92.    w1.wcout(2,25,message);
  93.  }
  94. // do not touch! //
  95. void showitem(int x, int y, char *item, int hotkey, int hl)
  96. {
  97.    int test=0;
  98.    char temp[80];
  99.  
  100.    if(hl==1)
  101.    { if(vmode==3) { w1.setcolor(black); w1.setbkcolor(cyan); }
  102.      else { w1.setcolor(lightgray); w1.setbkcolor(black); }
  103.     }
  104.    else
  105.    { if(vmode==3) { w1.setcolor(black); w1.setbkcolor(lightgray); }
  106.      else { w1.setcolor(black); w1.setbkcolor(lightgray); }
  107.     }
  108.    sprintf(temp," %s ",item);
  109.    w1.wcout(x-1,y,temp);
  110.    for(i=0;mainmenu[i]!=NULL;i++)
  111.    { if(strcmp(mainmenu[i],item)==0)
  112.      test=1;
  113.     }
  114.    if(test==0)
  115.    { for(i=x+strlen(item);i<=xr;i++)
  116.      w1.wcout(i,y," ");
  117.     }
  118.    if(hl==1)
  119.    { if(vmode==3) { w1.setcolor(red); w1.setbkcolor(cyan); }
  120.      else { w1.setcolor(white); w1.setbkcolor(black); }
  121.     }
  122.    else
  123.    { if(vmode==3) { w1.setcolor(red); w1.setbkcolor(lightgray); }
  124.      else { w1.setcolor(white); w1.setbkcolor(lightgray); }
  125.     }
  126.    sprintf(temp,"%c", item[hotkey]);
  127.    w1.wcout(x+hotkey,y,temp);
  128.    w1.setcolor(black); w1.setbkcolor(lightgray);
  129.  }
  130. // do not touch! //
  131. void scroll_pulled(void)
  132. {
  133.    int i, tot;
  134.  
  135.    for(i=0;mainmenu[i]!=NULL;i++)
  136.    tot=i;
  137.    tot=i-1;
  138.    if(scrolltype==0)
  139.    { if(lastsel==0)
  140.      key=lastsel=tot;
  141.      else key=lastsel=lastsel-1;
  142.     }
  143.    else
  144.    { if(lastsel==tot)
  145.      key=lastsel=0;
  146.      else key=lastsel=lastsel+1;
  147.     }
  148.  }
  149. // do not touch!  fully automatic!  lots of math! //
  150. int pulldown_menu(char *items[], int hotkeys[], char *messages[])
  151. {
  152.    int i, l, len, done=0, num, max, sel;
  153.  
  154.    xr=0; xl=xpos[lastsel]-2;
  155.    for(i=0; items[i]!=NULL; i++)
  156.    { len=strlen(items[i])+3;
  157.      if(len>xr)
  158.      xr=len;
  159.     }
  160.    xr+=xl; l=xl;
  161.    if(xr>79)
  162.    { len=xr-xl;
  163.      xr=79;
  164.      xl=79-len;
  165.     }
  166.    if(xl<0) xl=0;
  167.    wintype pdown(xl,1,xr,i+2,1,"");
  168.    pdown.setcolor(black); pdown.setbkcolor(lightgray);
  169.    pdown.winput(); pdown.line_border(); pdown.wincls();
  170.  
  171.    showitem(xl+3,3,items[0],hotkeys[0],1);
  172.    for(i=1; items[i]!=NULL; i++)
  173.    { showitem(xl+3,i+3,items[i],hotkeys[i],0);
  174.     }
  175.  
  176.    max=i-1; num=0; pulled=0;
  177.    showmsg(messages[num]);
  178.    while(done==0)
  179.    { Mshow();
  180.      switch(sel=getkey())
  181.      { case  27: sel= -1; done=1;
  182.          break;
  183.        case   8:
  184.        case 328: Mhide();
  185.          showitem(xl+3,num+3,items[num],hotkeys[num],0);
  186.          num--; if(num==-1) { num=-1; done=1; break; }
  187.          showitem(xl+3,num+3,items[num],hotkeys[num],1);
  188.          showmsg(messages[num]);
  189.          break;
  190.        case   9:
  191.        case  32:
  192.        case 336: Mhide();
  193.          showitem(xl+3,num+3,items[num],hotkeys[num],0);
  194.          num++; if(num==max+1) num=0;
  195.          showitem(xl+3,num+3,items[num],hotkeys[num],1);
  196.          showmsg(messages[num]);
  197.          break;
  198.        case  13: sel=num; done=1;
  199.          break;
  200.        case 331: sel=scroll_while_pulled; pulled=1; scrolltype=0; done=1;
  201.          break;
  202.        case 333: sel=scroll_while_pulled; pulled=1; scrolltype=1; done=1;
  203.          break;
  204.        case 999: while(L_DOWN)
  205.          { Mshow();
  206.            if(M_ypos>=3 && M_ypos<=max+3 && M_xpos>=xl+2 && M_xpos<=xr)
  207.            { sel=M_ypos-3;
  208.              if(sel<0) sel=0; if(sel>max) sel=max;
  209.              if(num!=sel)
  210.              { showitem(xl+3,num+3,items[num],hotkeys[num],0);
  211.                Mhide();
  212.                showitem(xl+3,sel+3,items[sel],hotkeys[sel],1);
  213.                num=sel;
  214.                showmsg(messages[sel]);
  215.               }
  216.             }
  217.            if((M_ypos==1) && (M_xpos<l+1 || M_xpos>l+strlen(mainmenu[lastsel])+2))
  218.            { sel= -1; done=1; break;
  219.             }
  220.            if(!L_DOWN) done=1;
  221.           }
  222.          if(M_xpos<=xl+1 || M_xpos>xr || M_ypos<3 || M_ypos>max+3)
  223.          { sel= -1; done=1; break;
  224.           }
  225.          break;
  226.        default : for(i=0; items[i]!=NULL; i++)
  227.          { if(sel>32 && sel<127 && toupper(items[i][hotkeys[i]]) == toupper(sel))
  228.            { sel=i;
  229.              done=1;
  230.              break;
  231.             }
  232.           }
  233.          break;
  234.       }
  235.     }
  236.    Mhide();
  237.    pdown.winremove();
  238.    return sel;
  239.  }
  240. // do not touch!  fully automatic!  lots of math! //
  241. int main_menu(char *items[], int hotkeys[], char *messages[])
  242. {
  243.    int i, pos=3, done=0, num, max, sel;
  244.  
  245.    w1.setcolor(black); w1.setbkcolor(lightgray);
  246.    for(i=0; items[i]!=NULL; i++)
  247.    { if(pos+strlen(items[i])-1>80)
  248.      { size_error(pos+strlen(items[i])-1);
  249.       }
  250.      xpos[i]=pos;
  251.      showitem(pos,1,items[i],hotkeys[i],0);
  252.      pos+=strlen(items[i])+4;
  253.     }
  254.    showitem(xpos[lastsel],1,items[lastsel],hotkeys[lastsel],1);
  255.  
  256.    max=i-1; num=lastsel;
  257.    showmsg(messages[lastsel]);
  258.    if(pulled==1) return key;
  259.    while(done==0)
  260.    { Mshow();
  261.      switch(sel=getkey())
  262.      { case  27: sel= -1; done=1;
  263.          break;
  264.        case   8:
  265.        case 331: Mhide();
  266.          showitem(xpos[num],1,items[num],hotkeys[num],0);
  267.          num--; if(num==-1) num=max;
  268.          sel=num;
  269.          showitem(xpos[num],1,items[num],hotkeys[num],1);
  270.          showmsg(messages[num]);
  271.          break;
  272.        case   9:
  273.        case  32:
  274.        case 333: Mhide();
  275.          showitem(xpos[num],1,items[num],hotkeys[num],0);
  276.          num++; if(num==max+1) num=0;
  277.          sel=num;
  278.          showitem(xpos[num],1,items[num],hotkeys[num],1);
  279.          showmsg(messages[num]);
  280.          break;
  281.        case 336:
  282.        case  13: sel=num; done=1;
  283.          break;
  284.        case 999: if(M_ypos==1)
  285.          { for(i=0; i<=max; i++)
  286.            { if(M_xpos>=xpos[i] && M_xpos<=xpos[i]+strlen(items[i])-1)
  287.              { Mhide();
  288.                sel=i;
  289.                showitem(xpos[num],1,items[num],hotkeys[num],0);
  290.                num=sel;
  291.                showitem(xpos[num],1,items[num],hotkeys[num],1);
  292.                showmsg(messages[num]);
  293.                done=1;
  294.                break;
  295.               }
  296.             }
  297.           }
  298.          break;
  299.        default : for(i=0; items[i]!=NULL; i++)
  300.         { if(sel>32 && sel<127 && toupper(items[i][hotkeys[i]]) == toupper(sel))
  301.           { Mhide();
  302.             showitem(xpos[num],1,items[num],hotkeys[num],0);
  303.             num=sel=i;
  304.             showitem(xpos[num],1,items[num],hotkeys[num],1);
  305.             done=1;
  306.             break;
  307.            }
  308.          }
  309.         break;
  310.       }
  311.     }
  312.    if(sel!=-1)
  313.    lastsel=sel;
  314.    Mhide();
  315.    return sel;
  316.  }
  317. void message(char *msg) // centers and displays a message //
  318. {
  319.    int half;
  320.  
  321.    half = strlen(msg) / 2;
  322.    xl = 40 - half - 3;
  323.    xr = 40 + half + 3;
  324.    wintype m(xl, 7, xr, 9, 1, "");
  325.    m.setcolor(white); if(vmode==3) m.setbkcolor(red);
  326.    else m.setbkcolor(black); m.winput(); m.wincls();
  327.    m.line_border();
  328.    m << "  " << msg;
  329.    getkey();
  330.    key=0;
  331.    Mhide();
  332.    m.winremove();
  333.  }
  334. void shell(char *prompt_string) // shell to dos with new prompt //
  335. {
  336.    char *comspec, prompt[256], *oldprompt;
  337.  
  338.    Mhide();
  339.    w1.winremove();
  340.    gotoxy(ti.curx, ti.cury);
  341.    _setcursortype(_NORMALCURSOR);
  342.    comspec = getenv("COMSPEC");
  343.    if(comspec == NULL)
  344.    comspec = "COMMAND.COM";
  345.    oldprompt = getenv("PROMPT");
  346.    sprintf(prompt, "PROMPT=%s$_$p$g", prompt_string);
  347.    putenv(prompt);
  348.    spawnlp(P_WAIT, comspec, comspec, NULL);
  349.    Minsthandler();
  350.    sprintf(prompt,"PROMPT=%s", oldprompt);
  351.    putenv(prompt);
  352.    _setcursortype(_NOCURSOR);
  353.    gettextinfo(&ti);
  354.    return;
  355.  }
  356. // spawns my file viewer.  included free with source code. //
  357. int help(char *filename, char *viewer)
  358. {
  359.    char *p;
  360.    char help_parameter[81];
  361.  
  362.    p = searchpath(filename);
  363.    if(!p) return -1;
  364.    Mhide();
  365.    w1.winremove();
  366.    strcpy(help_parameter, p);
  367.    spawnlp(P_WAIT, viewer, viewer, help_parameter, NULL);
  368.    _setcursortype(_NOCURSOR);
  369.    set_v_ptr();
  370.    Minsthandler();
  371.    return 1;
  372.  }
  373. void about(void) // add your own about info and size window accordingly //
  374. {
  375.    wintype a(25,7,55,11,1,"");
  376.    a.setcolor(black); a.setbkcolor(lightgray);
  377.    a.winput(); a.wincls(); a.line_border();
  378.    a <<   " User Interface, Version 1.0";
  379.    a <<    "\n       Jay Lewis, 1995";
  380.    a << "\n            Free!";
  381.    getkey();
  382.    key=0;
  383.    Mhide();
  384.    a.winremove();
  385.  }
  386. void stub(void) // dummy function //
  387. {
  388.    wintype s(25,7,55,10,1,"");
  389.    s.setcolor(black); s.setbkcolor(lightgray);
  390.    s.winput(); s.wincls(); s.line_border();
  391.    s <<   "   Stub function executed.";
  392.    s <<    "\n      Press any key ...";
  393.    getkey();
  394.    key=0;
  395.    Mhide();
  396.    s.winremove();
  397.  }
  398. int main(void)
  399. {  // don't touch the following //
  400.    gettextinfo(&ti); // to save cursor position //
  401.    set_v_ptr(); // must set video pointer to use window class //
  402.    Mreset(); // resets mouse //
  403.    Minsthandler(); // installs mouse handler //
  404.    mcnt=0; // initializes mouse repeat/delay rate tracker //
  405.    _setcursortype(_NOCURSOR); // no cursor //
  406.    draw_window();
  407.  
  408.    // Just modify the case statements to execute your functions. //
  409.    // Selecting the first main menu choice returns a zero, second 1, etc. //
  410.    // Add or remove menu items and cases as desired. Match to INT.H file. //
  411.    // Don't forget to hide mouse when changing screen in your functions. //
  412.    // Check my example functions above to see how. Use the WINDOW.CPP //
  413.    // functions as shown in the above functions.  Very simple class. //
  414.    // Fully mouse driven if a mouse is present.  Otherwise kb driven. //
  415.  
  416.    while(key!=27)
  417.    { switch(key=main_menu(mainmenu,mainkeys,mainmsgs))
  418.      { case   0: /*number 1*/
  419.          switch(key=pulldown_menu(pull0items,pull0keys,pull0msgs))
  420.          { // first pulldown item returns a zero, next a 1, etc. //
  421.            case  0: /*first*/   stub(); /* or your function here */ break;
  422.            case  1: /*second*/  stub(); /* or your function here */ break;
  423.            case  2: /*third*/   stub(); /* or your function here */ break;
  424.            case  3: /*fourth*/  stub(); /* or your function here */ break;
  425.            case  4: /*fifth*/   stub(); /* or your function here */ break;
  426.            case  5: /*sixth*/   stub(); /* or your function here */ break;
  427.            case  6: /*seventh*/ stub(); /* or your function here */ break;
  428.            default: break;
  429.           }
  430.          break;
  431.  
  432.        case   1: /*number 2*/
  433.          switch(key=pulldown_menu(pull1items,pull1keys,pull1msgs))
  434.          { case  0: /*only*/ stub(); /* or your function here */ break;
  435.            default: break;
  436.           }
  437.          break;
  438.  
  439.        case   2: /*number 3*/
  440.          switch(key=pulldown_menu(pull2items,pull2keys,pull2msgs))
  441.          { case  0: /*first*/  stub(); /* or your function here */ break;
  442.            case  1: /*second*/ stub(); /* or your function here */ break;
  443.            default: break;
  444.           }
  445.          break;
  446.  
  447.        case   3: /*shell*/
  448.          switch(key=pulldown_menu(pull3items,pull3keys,pull3msgs))
  449.          { case  0: /*shell*/ shell("Type EXIT to return to User Interface"); draw_window(); break;
  450.            default: break;
  451.           }
  452.          break;
  453.  
  454.        case   4: /*quit*/
  455.          switch(key=pulldown_menu(pull4items,pull4keys,pull4msgs))
  456.          { case  0: /*quit*/ key=27; break;
  457.            default: break;
  458.           }
  459.          break;
  460.  
  461.        case   5: /*read,about*/
  462.          switch(key=pulldown_menu(pull5items,pull5keys,pull5msgs))
  463.          { case  0: /*read*/ fail=help("int.h","read.exe");
  464.                      if(fail != -1) draw_window();
  465.                      else message("File not found."); break;
  466.            case  1: /*about*/ about(); break;
  467.            default: break;
  468.           }
  469.          break;
  470.  
  471.        // leave this alone! to scroll left/right while pulldowns extended //
  472.        case scroll_while_pulled: scroll_pulled(); break;
  473.        default : break;
  474.       }
  475.     }
  476.  
  477.    // leave this alone //
  478.    w1.winremove(); // removes main screen //
  479.    _setcursortype(_NORMALCURSOR); // turns on cursor //
  480.    gotoxy(ti.curx, ti.cury); // goes to saved cursor position //
  481.    Mhide(); // hide mouse //
  482.    Mreset(); // reset mouse //
  483.    return 0; // exit //
  484.  }
  485.   // Note:  you can also download my directory scanner function with source //
  486.   // called READDIR.ZIP (free).  Or you may extract it from the READ.CPP //
  487.   // viewer code included.  It builds a list of drives, directories, and //
  488.   // files for further display to the user.  Uses dynamically allocated //
  489.   // memory for storage and files are sortable by name or extension. //
  490.   // Another super easy one to use! //
  491.  
  492.   // All by Jay Lewis, dedicated to helping the novice programmer with //
  493.   // the GOOD STUFF!  Please write, 10365,3264 at Compuserve. //
  494.