home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_04 / 1n04053a < prev    next >
Text File  |  1990-06-25  |  3KB  |  134 lines

  1. #include    <stdio.h>
  2. #include    <graphics.h>
  3. #include    <stdlib.h>
  4.  
  5. extern    struct    menu    *m;
  6. extern    int        current;    /* index into menu array */
  7.  
  8. #define    MAX_MENU_CHOICES    400
  9. #define    INVALID                -1
  10. #define    MOUSE_L                -2
  11. #define    MOUSE_R                -3
  12. #define    MOUSE_OFF            -4
  13. #define    MOUSE_UP            -5
  14. #define    MOUSE_N                -6
  15. #define    NOT_FOUND            -1
  16.  
  17. /*
  18. ************************************************************************
  19. *    disp_menu - display menu/return item selected/display menu help
  20. *
  21. *    Parameters:
  22. *        button (in) - mouse button pressed
  23. *            MOUSE_N: none
  24. *            MOUSE_L, MOUSE_R: mouse left or right button
  25. *            Other key: other key pressed
  26. *        x (in) - mouse cursor x position when button pressed
  27. *        y (in) - mouse cursor y position when button pressed
  28. *
  29. *    Global:
  30. *        m[]->action - number representing action to take on selection
  31. *                      (defined by calling program)
  32. *        m[]->help - pointer into menu.hlp file for help string
  33. *        m[]->level - menu level in hierarchy
  34. *        current - current menu choice
  35. *
  36. *    Files:
  37. *        menu.hlp - File containing help for each m[] choice.
  38. *
  39. *    Returns:
  40. *        Returns m[]->action or INVALID
  41. *
  42. *    Notes:
  43. *        x and y are not used if button == MOUSE_N.
  44. *        x and y are in absolute screen coordinates.
  45. *
  46. *    Copyright:
  47. *        Original code by William H. Roetzheim
  48. *        Copyright 1989 by William H. Roetzheim
  49. *        All rights reserved.
  50. **********************************************************************
  51. */
  52.  
  53. int    disp_menu(int button, int x, int y)
  54. {
  55.             FILE    *fp;
  56.     static    int        menu_edges [MAX_MENU_CHOICES] [6]; /* l, t, r, b, key, choice */
  57.             int        choice;        /* index into menu_edges array */
  58.             int        level;
  59.             int        retval = INVALID;
  60.             char    string[81];
  61.     struct    environment *e;
  62.  
  63.     e = save_environment();
  64.     setviewport(0,0,getmaxx(), getmaxy(),NO_CLIP);
  65.  
  66.     if (button == MOUSE_UP) button = MOUSE_L;
  67.  
  68.     if (button == MOUSE_N)         /* redisplay current menu */
  69.     {
  70.         out_menu(menu_edges);
  71.     }
  72.     else
  73.     {
  74.         choice = find(x, y, button, menu_edges);
  75.  
  76.         if (choice == NOT_FOUND)
  77.         {
  78.             disp_help("Click on a menu choice.");
  79.         }
  80.         else
  81.         {
  82.             if (button != MOUSE_R)
  83.             {
  84.                 current = menu_edges[choice][5];
  85.                 retval = m[current].action;
  86.                 if (menu_edges[choice+1][0] == INVALID)    /* last value is quit */
  87.                 {
  88.                     level = m[current].level;
  89.                     if (level != 0)        /* zero means quit program was selected */
  90.                     {
  91.                         /* find first element higher in hierarchy */
  92.                         while (m[--current].level >= level);
  93.  
  94.                         /* display resultant menu */
  95.                         out_menu(menu_edges);
  96.                     }
  97.                 }
  98.                 /* see if lower level exists for selected menu choice */
  99.                 else if (m[current +1].level > m[current].level)
  100.                 {
  101.                     /* descend to next menu hierarchy level */
  102.                     current++;
  103.                     out_menu(menu_edges);
  104.                 }
  105.             }
  106.             if (button == MOUSE_R)
  107.             {
  108.                 current = menu_edges[choice][5];
  109.                 fp = fopen ("menu.hlp","rb");
  110.                 if (fp == NULL)
  111.                 {
  112.                     error(12,"disp_menu");
  113.                     return INVALID;
  114.                 }
  115.                 if (fseek(fp,m[current].help,SEEK_SET) != NULL)
  116.                 {
  117.                     error(13,"disp_menu");
  118.                     fclose(fp);
  119.                     return INVALID;
  120.                 }
  121.                 if (reads(string, 81, fp) == EOF)
  122.                 {
  123.                     error(13,"disp_menu");
  124.                     fclose(fp);
  125.                     return INVALID;
  126.                 }
  127.                 fclose(fp);
  128.                 disp_help(string);
  129.             }
  130.         }
  131.     }
  132.     restore_environment(e);
  133.     return retval;
  134. }