home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 337_01 / l_bar.c < prev    next >
C/C++ Source or Header  |  1991-01-14  |  4KB  |  141 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************    L_BAR.C    ***************************/
  4.  
  5. #include "mydef.h"
  6. #include "stdio.h"
  7.  
  8.  
  9. /*****************************************************************
  10.  
  11.  Usage: int bar_menu(struct bar_struc bar_menu[], char normal,
  12.                      char inverse)
  13.  
  14.   struct bar_struc bar_menu=  data structure containing menu options.
  15.  
  16.   char normal,inverse= text attributes used for regular and
  17.                        highlighted options.
  18.  
  19.   Creates and manages a moving light bar menu at the first line of
  20.   the topmost (active) window.
  21.  
  22. *****************************************************************/
  23.  
  24. int bar_menu(struct bar_struc menu[], char normal, char inverse)
  25. {
  26.  
  27. extern struct  screen_structure scr;
  28. extern struct window_structure w[];
  29.  
  30. int x=1;       /* x screen location */
  31. int y=1;       /* y screen location */
  32. int i,j;       /* index variables */
  33. int nu_opt;    /* number of options in menu */
  34. int old_caps=scr.bold_caps;  /* original value of bold_caps */
  35.  
  36. int cur_opt;   /* current menu (highlighted option) */
  37.  
  38. char ch;       /* char and extension variables */
  39. char ext;
  40.  
  41. int found = FALSE;    /* selection made (found) flag */
  42. int return_code=0;
  43. int my_win=scr.active;
  44.  
  45. cls();
  46. cursor(NO_CURSOR); /* turn off cursor */
  47.  /* set number of options to max */
  48.  
  49. /* figure how many options there are */
  50.  
  51. for(i=0;i<MAX_BAR;i++){
  52.   if (menu[i].name[0] == '\0'){
  53.    nu_opt = i;
  54.    break;
  55.   }
  56. }
  57.  
  58. cur_opt = 0;
  59.  
  60.    for(;;){       /* loop infinite */
  61.          x=1;
  62.  
  63.           scr.bold_caps=!found;  /* turn off caps when found */
  64.  
  65.          for(i=0;i< nu_opt;i++){    /* print all options */
  66.            if(i == cur_opt) scr.current= inverse;
  67.             else scr.current= normal;
  68.             print(x,y,menu[i].name);
  69.             x=x+strlen(menu[i].name)+3; /* move option location */
  70.  
  71.          };  /* end of menu printing loop */
  72.  
  73.            if(menu[cur_opt].info[0]!='\0'){ /* is there info? */
  74.             scr.current=normal;    /* then print it on next line */
  75.             scr.bold_caps=FALSE; /* don't highlight bold caps */
  76.  
  77.             ceol(1,y+1);    /* clear to end of line
  78.                                to clear old info */
  79.             /* print new information */
  80.             print(1,y+1,menu[cur_opt].info);    
  81.            }
  82.  
  83.   if(found ){        /* selection made.
  84.                         return correct return code if function
  85.                         pointer NULL */
  86.    if(menu[cur_opt].fun==NULL) return(menu[cur_opt].select_id);
  87.       else{  /* else run option to get code */
  88.         return_code= (*menu[cur_opt].fun)() ;  
  89.         win_pop_top(my_win);
  90.         ceol(1,1);
  91.       }
  92.         found = FALSE;   /* reset flag, ready for new selection */
  93.  
  94.       if (kbhit()) getch(); /* make sure keyboard buffer is clear */
  95.  
  96.   }
  97.    else{       /* selection not made
  98.                   read keys until selection is made */
  99.  
  100.         get_key(&ch,&ext); ch=toupper(ch);  /* get a character */
  101.         if (ext == RIGHT)  cur_opt = cur_opt +1;  /* move right */
  102.         if (ext == LEFT)  cur_opt = cur_opt -1;   /* move left */
  103.         if (cur_opt >= nu_opt) cur_opt =0;        /* wrap if out of
  104.                                                      bounds */
  105.         if (cur_opt < 0) cur_opt = nu_opt-1;
  106.  
  107.         if (ch== RETURN) found = TRUE;
  108.  
  109.      /* if we have a valid character then it may be a quick key */
  110.      if(ch!='\0'){
  111.         for(i=0;i<nu_opt;i++){    /* does it match an option? */
  112.          j=0;                     /* scan each menu option name */
  113.  
  114.       /* check each letter within option */
  115.       while(menu[i].name[j]!= '\0'){
  116.           /* if match and not space */
  117.           if ( ch==menu[i].name[j++] && ch != ' '){
  118.            cur_opt = i;            /* mark found flag and break */
  119.            found = TRUE;
  120.            break;
  121.           }
  122.        }
  123.        if(found==TRUE) break;   /* break if found */
  124.      }
  125.    }
  126.            if (ch==ESCAPE){          /* EXIT IF ESCAPE KEY */
  127.             return_code = 0;         /* exit but don't close down */
  128.             break;                   /* parent menu */
  129.            }
  130.            ext=ch=' ';
  131.  
  132.            }  /* end else */
  133.           /* a non-zero return code means exit menu */
  134.           if (return_code!=0) break;
  135.  
  136.    } /* end for(;;)*/
  137.  
  138.  scr.bold_caps=old_caps; /* restore old value */
  139.  return (return_code);
  140. }
  141.