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_popup.c < prev    next >
C/C++ Source or Header  |  1991-01-14  |  5KB  |  154 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   L_POPUP.C   ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <stdio.h>
  7.  
  8.  
  9. /*****************************************************************
  10.  
  11.  Usage: int pop_up(struct pop_struc pop_menu[],int x,int y,
  12.                    char normal, char inverse)
  13.  
  14.  
  15.   struct pop_struc pop_menu=  data structure containing menu
  16.                               options.
  17.  
  18.   int x,y= upper left corner of pop_up window.
  19.  
  20.   char normal,inverse= text attributes used for regular and
  21.                        high-lighted options.
  22.  
  23.   Creates and manages a pop-up menu at the location specified.
  24.  
  25. *****************************************************************/
  26.  
  27. int pop_up(struct pop_struc pop_menu[],int x,int y, char normal,
  28.            char inverse)
  29. {
  30. extern struct  screen_structure scr;
  31. extern struct window_structure w[];
  32.  
  33. int col;              /* screen column */
  34. int i=0,j;            /* general purpose index variables */
  35. int width;            /* width of window */
  36. int nu_opt;           /* number of options */
  37. int cur_opt;          /* current option (highlighted */
  38. char ch,ext;          /* character and extension */
  39. int found = FALSE;    /* flag to indicate option found(selected) */
  40. int return_code;      /* return code */
  41. int pop_window;
  42. int old_caps=scr.bold_caps;  /* the original value of bold caps */
  43. /* set on bold caps to highlight menu quick keys */
  44. scr.bold_caps=ON;  
  45.  
  46. /* figure how many options there are */
  47.  
  48. nu_opt=0;
  49.  
  50. /* loop until empty string found */
  51. while (pop_menu[i++].name[0]!='\0'); 
  52.  
  53. nu_opt=i-1;   /* set nu_opt to the number of options found */
  54.  
  55. /* figure size of box */
  56.  
  57.   width=0;      /* figure max length of window */
  58.  
  59.   for (i=0;i< nu_opt;i++){                   /* for each option */
  60.    /* find largest option length */
  61.    if (strlen(pop_menu[i].name) > width){
  62.     width= strlen(pop_menu[i].name);   
  63.    }
  64.   }
  65.  
  66. /* make a window based on x,y parameters
  67.    and calculated width and height */
  68.  
  69. pop_window= win_make(x,y,width,nu_opt,STD_FRAME,"",normal,normal);
  70.  
  71. cursor(NO_CURSOR);    /* hide cursor */
  72.  
  73. scr.current = scr.normal;
  74.  
  75. cur_opt = 0;   /* first option */
  76.  
  77.   /* infinite loop */
  78.    for(;;){
  79.  
  80.    scr.bold_caps=!found; /* turn off scr.bold_caps if true */
  81.  
  82.        /* print menu options, highlight current option */
  83.          col=1;                     /* start at first column */
  84.          for(i=0;i< nu_opt;i++){    /* print each option */
  85.            /* highlight current option */
  86.            if(i == cur_opt) scr.current= inverse;  
  87.             else scr.current=normal;          /* else normal */
  88.             print(1,col++,pop_menu[i].name);
  89.          };
  90.  
  91.           if(found ) {          /* selection found */
  92.  
  93.             /* return specified code if NULL */
  94.             if(*pop_menu[cur_opt].fun==NULL){   
  95.              return_code=pop_menu[cur_opt].select_id;
  96.              break;
  97.             }
  98.             else
  99.               /* a function was specified by pointer,
  100.                  call the function and get code */
  101.  
  102.              return_code=(*pop_menu[cur_opt].fun)() ;
  103.  
  104.              win_pop_top(pop_window);
  105.              /* a non-zero value is signal to exit */
  106.              if(return_code!=0) break;  
  107.              found = FALSE;
  108.              /* make sure keyboard buffer is clear */
  109.              if (kbhit()) getch();  
  110.  
  111.           } /* end if(found) */
  112.  
  113.           else{  /* not found */
  114.            /* read keys until selection is made */
  115.            get_key(&ch,&ext); /* get a character */
  116.            ch=toupper(ch);    /* make it upper case */
  117.           }
  118.            if (ext == DOWN)  cur_opt++;  /* move down */
  119.            if (ext == UP)    cur_opt--;  /* move up */
  120.            /* wrap if boundaries exceeded */
  121.            if (cur_opt >= nu_opt) cur_opt =0;
  122.            if (cur_opt < 0) cur_opt = nu_opt-1;
  123.  
  124.           if (ch== 13) found = TRUE;
  125.  
  126.           if(ch!='\0'){  /* do we have a letter? */
  127.             for(i=0;i<nu_opt;i++){   /* scan each option? */
  128.              j=0;
  129.  
  130.              /* check each letter within option */
  131.              while(pop_menu[i].name[j]!=0){  
  132.                /* ignore spaces in string */
  133.                if (pop_menu[i].name[j++]==ch && ch != ' '){
  134.                 cur_opt = i;
  135.                 found = TRUE;
  136.                }
  137.              } /* end while */
  138.             }
  139.            } /* end ch!='\0' */
  140.  
  141.  
  142.            if (ch==ESCAPE){  /* EXIT IF ESCAPE KEY */
  143.             return_code=0;
  144.             break;
  145.            }
  146.            ext=' ';ch=' ';
  147.    } /* end for(;;)*/
  148.  
  149.    scr.bold_caps=old_caps;   /* restore bold caps */
  150.    win_delete_top();         /* remove top window */
  151.  
  152.    return (return_code);
  153. }
  154.