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

  1. #include    <stdio.h>
  2. #include    <mem.h>
  3. #include    <stdlib.h>
  4. #include    <graphics.h>
  5. #include    <ctype.h>
  6.  
  7. extern    struct    menu    *m;
  8. extern    int        m_total;
  9. extern    FILE    *menu_amp;
  10.  
  11. struct    menu
  12. {
  13.     char    level;    /* 0 (top) through 255 */
  14.     long    help;    /* location in menu.hlp where help starts */
  15.     int        action;    /* action to take place */
  16. };
  17.  
  18. #define    NOT_FOUND    -1
  19. #define    LEFT        0
  20. #define    TOP            1
  21. #define    RIGHT        2
  22. #define    BOTTOM        3
  23. #define    KEY            4
  24. #define    ESC            27
  25.  
  26. /*
  27. ************************************************************************
  28. *    init_menu - initialize menu array
  29. *
  30. *    Global:
  31. *        struct    menu *m[] - menu array being initialized
  32. *        m_total - initialized to total number of menu choices
  33. *
  34. *    Files:
  35. *        menu.def - stores menu hierarchy
  36. *        menu.amp - menu element names
  37. *
  38. *    Copyright:
  39. *        Original code by William H. Roetzheim
  40. *        Copyright 1989 by William H. Roetzheim
  41. *        All rights reserved.
  42. **********************************************************************
  43. */
  44.  
  45. void    init_menu()
  46. {
  47.     int        i;
  48.     FILE    *fp;
  49.  
  50.     m_total = 0;
  51.     menu_amp = fopen ("menu.amp","rb");
  52.     fp = fopen ("menu.def","rb");
  53.     if ((fp == NULL) || (menu_amp == NULL)) error(-10,"init_menu");
  54.     fread(&m_total, sizeof(int),1,fp);
  55.     m = malloc(m_total * sizeof(struct menu));
  56.     for (i = 0; i < m_total; i++)
  57.     {
  58.         if (fread(&m[i], sizeof(struct menu),1,fp) != 1) error(-30,"init_menu");
  59.     }
  60.     fclose(fp);
  61. }
  62.  
  63.  
  64. /*
  65. ************************************************************************
  66. *    find - find x/y coordinate in array of rectangles
  67. *
  68. *    Parameters:
  69. *        x (in) - x position in absolute coordinates
  70. *        y (in) - y position in absolute coordinates
  71. *        edges (in) - array of left, top, right, bottom, key, and user for
  72. *            rectangles.
  73. *
  74. *    Returns:
  75. *        If x,y is within a rectangle defined by edges, or key matches,
  76. *        returns number
  77. *        of first match.  If not found, returns NOT_FOUND.
  78. *
  79. *    Notes:
  80. *        Converts x and y to relative position (relative to viewport)
  81. *        for comparison.
  82. *
  83. *    Copyright:
  84. *        Original code by William H. Roetzheim
  85. *        Copyright 1989 by William H. Roetzheim
  86. *        All rights reserved.
  87. **********************************************************************
  88. */
  89.  
  90. int    find(int x, int y, int key, int edges[][6])
  91. {
  92.     int        i = 0;
  93.     struct    viewporttype v;
  94.  
  95.     getviewsettings(&v);
  96.     x -= v.left;
  97.     y -= v.top;
  98.  
  99.     while (edges[i][5] != -1)
  100.     {
  101.         if ((x >= edges[i][LEFT]) && (x <= edges[i][RIGHT]) &&
  102.             (y >= edges[i][TOP]) && (y <= edges[i][BOTTOM])) break;
  103.         if (toupper(key) == edges[i][KEY]) break;
  104.         /* ESC key acts like quit */
  105.         /* edges[i+1][LEFT] == -1 implies this is last entry */
  106.         if ((key == ESC) && (edges[i+1][LEFT] == -1)) break;
  107.         i++;
  108.     }
  109.     if (edges[i][LEFT] == -1) return NOT_FOUND;
  110.     else return i;
  111. }
  112.