home *** CD-ROM | disk | FTP | other *** search
/ Boldly Go Collection / version40.iso / TS / 17A / DRWIN101.ZIP / MENUTIL.C < prev    next >
C/C++ Source or Header  |  1991-07-23  |  11KB  |  204 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4.  
  5. #include "strutil.h"
  6. #include "scrutil.h"
  7. #include "wutil.h"
  8. #include "menutil.h"
  9.  
  10.  
  11.  
  12. /****************************************************************************/
  13. /*  menshow                                                                 */
  14. /****************************************************************************/
  15. /*-----------------------------Description----------------------------------*/
  16. /*  This function redisplays the described window.                          */
  17. /*-----------------------------Arguments------------------------------------*/
  18. /*  MENU_DESC* md      pointer to menu descriptor for menu.                 */
  19. /*-----------------------------Return value---------------------------------*/
  20. /*  Returns the current selection or one of the return codes described in   */
  21. /*  menutil.h.                                                              */
  22. /*-----------------------------Global constants-----------------------------*/
  23. /*-------------------Mod-------Global variables-----------------------------*/
  24. /*-----------------------------Functions called-----------------------------*/
  25. /*-----------------------------Examples-------------------------------------*/
  26. /*-----------------------------Constraints/Gotchas--------------------------*/
  27. /*--Date--------Programmer----------Comments--------------------------------*/
  28. /*  1990.01.01  A. Turing           initial code                            */
  29. /****************************************************************************/
  30. int  menshow(MENU_DESC* md)
  31. {
  32.   char twupd;                          /*temporary windowing update flag*/
  33.   int i;                               /*index*/
  34.   MENU_LIST* ml;                       /*local copy of menu list pointer*/
  35.   WIND* w;                             /*pointer to window*/
  36.  
  37.   if (!md) return MEN_ERROR;           /*make sure legal descriptor*/
  38.   if (md->typ>=MEN_LAST) return MEN_ERROR;  /*ditto*/
  39.   w=md->w;                             /*pointer to window*/
  40.   if (!w) return MEN_ERROR;            /*bad window, oops!*/
  41.   ml=md->menu_list;                    /*pointer to menu list*/
  42.   twupd=wupd;                          /*copy window updating flag*/
  43.   wupd=0;                              /*disable updating*/
  44.  
  45.   switch (md->typ) {                   /*differently for each type*/
  46.   case MEN_VERTICAL:                   /*vertical selection list*/
  47.     wclr(w);                           /*clear window*/
  48.     if (md->cur>=md->siz) md->cur=0;   /*check current*/
  49.     wsetattr(w,md->wnd_attr);          /*set window attributes*/
  50.     for (i=0;i<md->siz;i++) {          /*go through each to write out*/
  51.       wsetcpos(w,i,0);                 /*go to start of row in window*/
  52.       wputshot(w,ml[i].str,md->hot_attr,HOTKEY);  /*write it out*/
  53.     }   /*to write out whole list*/
  54.     wsetcpos(w,md->cur,0);             /*current line*/
  55.     wsetattr(w,md->sel_attr);          /*invert selection colors*/
  56.     wclreor(w);                        /*clear line*/
  57.     wputshot(w,ml[md->cur].str,md->sel_attr,HOTKEY);  /*write string*/
  58.     wsetattr(w,md->wnd_attr);          /*restore attributes*/
  59.     break;
  60.   case MEN_HORIZONTAL:                 /*horizontal selection list*/
  61.   default:                             /*unknown type*/
  62.     wupd=twupd;                        /*restore window updating flag*/
  63.     return MEN_ERROR;                  /*bad descriptor*/
  64.   }   /*switch*/
  65.   wupd=twupd;                          /*restore window updating flag*/
  66.   if (wupd) wupdate(w);                /*update window*/
  67.   return md->cur;                      /*all ok*/
  68. }   /*menshow*/
  69.  
  70.  
  71. /****************************************************************************/
  72. /*  menvert                                                                 */
  73. /****************************************************************************/
  74. /*-----------------------------Description----------------------------------*/
  75. /*  This function displays a vertical menu using the information in the     */
  76. /*  menu descriptor argument.  The menu descriptor tells menvert() where    */
  77. /*  to place the window, the attributes, the selection strings and the      */
  78. /*  function to call when a selection is made.                              */
  79. /*-----------------------------Arguments------------------------------------*/
  80. /*  MENU_DESC* md      see menutil.h for field definitions of MENU_DESC     */
  81. /*-----------------------------Return value---------------------------------*/
  82. /*  Returns the last selection made or one of the return codes described    */
  83. /*  in menutil.h.                                                           */
  84. /*-----------------------------Global constants-----------------------------*/
  85. /*-------------------Mod-------Global variables-----------------------------*/
  86. /*-----------------------------Functions called-----------------------------*/
  87. /*-----------------------------Examples-------------------------------------*/
  88. /*-----------------------------Constraints/Gotchas--------------------------*/
  89. /*--Date--------Programmer----------Comments--------------------------------*/
  90. /*  1990.01.01  A. Turing           initial code                            */
  91. /****************************************************************************/
  92. int  menvert(MENU_DESC* md)
  93. {
  94.   int i;                               /*scrap*/
  95.   int c;                               /*input char*/
  96.   char* s;                             /*scrap*/
  97.   char done=0;                         /*finished with menu*/
  98.   char exec=0;                         /*execute selection flag*/
  99.   MENU_LIST* ml;                       /*menu list struct pointer*/
  100.   WIND* w;                             /*pointer to window*/
  101.  
  102.   ml=md->menu_list;                    /*point to menu list*/
  103.   for (i=0;ml[i].str;i++) {            /*get number of menu elements*/
  104.     md->wid=MAX(md->wid,hotlen(ml[i].str,HOTKEY));    /*check width*/
  105.     ml[i].hot=0;                       /*..reset hotkey*/
  106.     s=strchr(ml[i].str,HOTKEY);        /*..find hotkey flag*/
  107.     while (s) {                        /*..check 'til no more &&'s*/
  108.       s++;                             /*....point to next char*/
  109.       if (*s!=HOTKEY) {                /*....found a non-doubled hotkey*/
  110.         ml[i].hot=toupper(*s);         /*......set it*/
  111.         break;                         /*......cut out on loop*/
  112.       }   /*if*/
  113.       s++;                              /*....pass second &*/
  114.       s=strchr(s,HOTKEY);               /*....look for next &X*/
  115.     }   /*while seeking hotkey*/
  116.   }   /*for each menu item*/
  117.   if (!i) return MEN_ERROR;            /*no menu selections*/
  118.   md->siz=i;                           /*set up size*/
  119.   c=0;
  120.   if (md->row==0xFFFF) md->row=getcrow();
  121.   if (md->col==0xFFFF) md->col=getccol();
  122.   if (!md->wnd_attr) md->wnd_attr=WND_ATTR;
  123.   if (!md->brd_attr) md->brd_attr=BRD_ATTR;
  124.   if (!md->sel_attr) md->sel_attr=SEL_ATTR;
  125.   if (!md->hot_attr) md->hot_attr=HOT_ATTR;
  126.   md->typ=MEN_VERTICAL;                /*vertical selection list menu*/
  127.   wupd=0;                              /*disallow window updating*/
  128.   md->w=wopen(                         /*open the menu's window..*/
  129.     md->border,md->title,              /*..border type, title string*/
  130.     md->row,md->col,                   /*..screen row, column*/
  131.     md->siz,md->wid,                   /*..height in rows, width in columns*/
  132.     md->wnd_attr                       /*..normal attribute*/
  133.   );   /*wopen*/
  134.   w=md->w;                             /*readability*/
  135.   wupd=1;                              /*allow window updating*/
  136.   if (!w) return MEN_ALLOC;            /*oops! no window*/
  137.   w->fl.wrap=0;                        /*no wrap*/
  138.   w->fl.scroll=0;                      /*no scroll*/
  139.   w->battr=md->brd_attr;               /*border attribute*/
  140.   w->csiz=NO_CURSOR;                   /*no cursor in the window*/
  141.   wupd=0;                              /*reset updating*/
  142.   for (i=0;i<md->siz;i++) {            /*go through each to write out*/
  143.     wsetcpos(w,i,0);                   /*go to start of row in window*/
  144.     wputshot(w,ml[i].str,md->hot_attr,HOTKEY);  /*write it out*/
  145.   }   /*to write out whole list*/
  146.   wupdate(w);                          /*update screen*/
  147.  
  148.   while (!done) {                      /*go 'til done*/
  149.     if (md->cur>=md->siz) md->cur=0;   /*start at top if bad*/
  150.     menshow(md);                       /*redraw window*/
  151.     exec=0;                            /*reset execute selection flag*/
  152.     do {                               /*only redo when need to*/
  153.       c=wgetc(w);                      /*get input character*/
  154.       if ((c>='a')&&(c<='z')) c-=0x20; /*upperize it*/
  155.       switch (c) {                     /*look at value*/
  156.       case _UPAR:
  157.         if (md->cur==0)                /*if at top*/
  158.           md->cur=md->siz-1;           /*wrap*/
  159.         else
  160.           md->cur--;
  161.         break;
  162.       case _DNAR:
  163.         md->cur++;                     /*go down*/
  164.         if (md->cur>=md->siz) md->cur=0;  /*wrap*/
  165.         break;
  166.       case _PGUP:
  167.       case _HOME:
  168.         md->cur=0;                     /*top of list*/
  169.         break;
  170.       case _PGDN:
  171.       case _END:
  172.         md->cur=md->siz-1;             /*end of list*/
  173.         break;
  174.       case CR:
  175.         exec=1;                        /*execute selection*/
  176.         break;
  177.       case ESC:
  178.         done=1;                        /*all done*/
  179.         break;
  180.       default:
  181.         for (i=0;i<md->siz;i++) if (c==ml[i].hot) break;  /*look for hotkey*/
  182.         if (i>=md->siz) { c=0; break; }        /*not found*/
  183.         md->cur=i;                     /*current window*/
  184.         menshow(md);                   /*redraw menu window*/
  185.         exec=1;                        /*execute selection*/
  186.       }   /*switch*/
  187.     } while (c==0);                    /*if nothing pressed, try again*/
  188.  
  189.     if (exec) {                        /*if we need to execute*/
  190.       if (ml[md->cur].fun) ml[md->cur].fun(md);   /*call selected function*/
  191.       if (ml[md->cur].flags&MEN_CLOSE) done=1;    /*if autoclose*/
  192.       if (ml[md->cur].flags&MEN_NEXT) md->cur++;  /*if autonext*/
  193.     }   /*if executing*/
  194.   }   /*while not done*/
  195.   wclose(w);                           /*shut down window*/
  196.   if (c==ESC)                          /*if ESC pressed to exit..*/
  197.     return MEN_ESC;                    /*..return code*/
  198.   else                                 /*otherwise..*/
  199.     return md->cur;                    /*..return selection*/
  200. }   /*menvert*/
  201.  
  202.  
  203.  
  204.