home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / tc-book.zip / TMENU.C < prev    next >
Text File  |  1987-08-20  |  4KB  |  162 lines

  1. /* ------------ tmenu.c ------------ */
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <stdlib.h>
  6. #include "keys.h"
  7. #include "twindow.h"
  8.  
  9. extern int VSG;
  10.  
  11. WINDOW *open_menu(char *mnm, MENU *mn, int hsel);
  12. int gethmenu(MENU *mn, WINDOW *hmenu, int hsel);
  13. int getvmn(MENU *mn, WINDOW *hmenu, int *hsel, int vsel);
  14. int haccent(MENU *mn, WINDOW *hmenu, int hsel, int vsel);
  15. void dimension(char *sl[], int *ht, int *wd);
  16. void light(MENU *mn, WINDOW *hmenu, int hsel, int d);
  17.  
  18. /* ------------- display & process a menu ----------- */
  19. void menu_select(char *name, MENU *mn)
  20. {
  21.     WINDOW *open_menu();
  22.     WINDOW *hmenu;
  23.     int sx, sy;
  24.     int hsel = 1, vsel;
  25.  
  26.     curr_cursor(&sx, &sy);
  27.     cursor(0, 26);
  28.     hmenu = open_menu(name, mn, hsel);
  29.     while (hsel = gethmenu(mn, hmenu, hsel))    {
  30.         vsel = 1;
  31.         while (vsel = getvmn(mn, hmenu, &hsel, vsel))    {
  32.             delete_window(hmenu);
  33.             set_help("", 0, 0);
  34.             (*(mn+hsel-1)->func [vsel-1])(hsel, vsel);
  35.             hmenu = open_menu(name, mn, hsel);
  36.         }
  37.     }
  38.     delete_window(hmenu);
  39.     cursor(sx, sy);
  40. }
  41.  
  42. /* ------- open a horizontal menu ------- */
  43. static WINDOW *open_menu(char *mnm, MENU *mn, int hsel)
  44. {
  45.     int i = 0;
  46.     WINDOW *hmenu;
  47.  
  48.     set_help("menu    ", 30, 10);
  49.     hmenu = establish_window(0, 0, 3, 80);
  50.     set_title(hmenu, mnm);
  51.     set_colors(hmenu, ALL, BLUE, AQUA, BRIGHT);
  52.     set_colors(hmenu, ACCENT, WHITE, BLACK, DIM);
  53.     display_window(hmenu);
  54.     while ((mn+i)->mname)
  55.         wprintf(hmenu, " %-10.10s ", (mn+i++)->mname);
  56.     light(mn, hmenu, hsel, 1);
  57.     cursor(0, 26);
  58.     return hmenu;
  59. }
  60.  
  61. /* ------- get a horizontal selection ---------- */
  62. static int gethmenu(MENU *mn, WINDOW *hmenu, int hsel)
  63. {
  64.     int sel;
  65.  
  66.     light(mn, hmenu, hsel, 1);
  67.     while (TRUE)    {
  68.         switch (sel = get_char())    {
  69.             case FWD:
  70.             case BS:    hsel = haccent(mn, hmenu, hsel, sel);
  71.                         break;
  72.             case ESC:    return 0;
  73.             case '\r':    return hsel;
  74.             default:    putch(BELL);
  75.                         break;
  76.         }
  77.     }
  78. }
  79. /*page*/
  80. /* ---------pop down a vertical menu --------- */
  81. static int getvmn(MENU *mn,WINDOW *hmenu,int *hsel,int vsel)
  82. {
  83.     WINDOW *vmenu;
  84.     int ht = 10, wd = 20;
  85.     char **mp;
  86.  
  87.     while (1)    {
  88.         dimension((mn+*hsel-1)->mselcs, &ht, &wd);
  89.         vmenu = establish_window(2+(*hsel-1)*12, 2, ht, wd);
  90.         set_colors(vmenu, ALL, BLUE, AQUA, BRIGHT);
  91.         set_colors(vmenu, ACCENT, WHITE, BLACK, DIM);
  92.         set_border(vmenu, 4);
  93.         display_window(vmenu);
  94.         mp = (mn+*hsel-1)->mselcs;
  95.         while(*mp)
  96.             wprintf(vmenu, "\n%s", *mp++);
  97.         vsel = get_selection(vmenu, vsel, "");
  98.         delete_window(vmenu);
  99.         if (vsel == FWD || vsel == BS)    {
  100.             *hsel = haccent(mn, hmenu, *hsel, vsel);
  101.             vsel = 1;
  102.         }
  103.         else
  104.             return vsel;
  105.     }
  106. }
  107.  
  108. /* ----- manage the horizontal menu selection accent ----- */
  109. static int haccent(MENU *mn,WINDOW *hmenu,int hsel,int sel)
  110. {
  111.     switch (sel)    {
  112.         case FWD:
  113.             light(mn, hmenu, hsel, 0);
  114.             if ((mn+hsel)->mname)
  115.                 hsel++;
  116.             else
  117.                 hsel = 1;
  118.             light(mn, hmenu, hsel, 1);
  119.             break;
  120.         case BS:
  121.             light(mn, hmenu, hsel, 0);
  122.             if (hsel == 1)
  123.                 while ((mn+hsel)->mname)
  124.                     hsel++;
  125.             else
  126.                 --hsel;
  127.             light(mn, hmenu, hsel, 1);
  128.             break;
  129.         default:
  130.             break;
  131.     }
  132.     return hsel;
  133. }
  134.  
  135. /* ---------- compute a menu's height & width --------- */
  136. static void dimension(char *sl[], int *ht, int *wd)
  137. {
  138.     unsigned strlen(char *);
  139.  
  140.     *ht = *wd = 0;
  141.     while (sl [*ht])    {
  142.         *wd = max(*wd, strlen(sl [*ht]));
  143.         (*ht)++;
  144.     }
  145.     *ht += 2;
  146.     *wd += 2;
  147. }
  148.  
  149. /* --------- accent a horizontal menu selection ---------- */
  150. static void light(MENU *mn, WINDOW *hmenu, int hsel, int d)
  151. {
  152.     if (d)
  153.         reverse_video(hmenu);
  154.     wcursor(hmenu, (hsel-1)*12+2, 0);
  155.     wprintf(hmenu, (mn+hsel-1)->mname);
  156.     normal_video(hmenu);
  157.     cursor(0, 26);
  158. }
  159.  
  160.  
  161.                                
  162.