home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_04 / 1104023a < prev    next >
Text File  |  1992-12-15  |  8KB  |  496 lines

  1. /*
  2.   listing3 - uililbs.c
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <signal.h>
  8. #include <time.h>
  9. #include <string.h>
  10. #ifdef VMS
  11. #include <smgdef.h>
  12. #endif
  13. #ifdef BCC
  14. #include <dos.h>
  15. #include <conio.h>
  16. #include <ctype.h>
  17. #endif
  18.  
  19. #include "curses.h"
  20. #include "menu.h"
  21.  
  22. extern WINDOW *dialogue;
  23. extern WINDOW *tbar;
  24.  
  25. static bar_size;   /* size of menubar */
  26. static int menu_pos;  /* position in menubar */
  27.  
  28. /* display menubar */
  29. WINDOW *topbar(WINDOW *win)
  30. {
  31.  
  32.   WINDOW  *swin;
  33.  
  34.   int string_count, string_size;
  35.  
  36.   if((swin = subwin(win,3,(win->MAXX)-4,(win->BEGY)+1,
  37.         (win->BEGX)+2)) == NULL)
  38.     clean_up();
  39.  
  40. #ifdef BCC
  41.   wattrset(swin, F_BLUE | B_GRAY);
  42. #endif
  43.  
  44.   box (swin, SINGLE_SIDE,SINGLE_ACROSS);
  45.  
  46.   bar_size = (swin->MAXX)-2;
  47.   menu_pos=0;
  48.  
  49.   return  (swin);
  50. }
  51.  
  52. /* print string to menubar */
  53. char do_menubar(WINDOW *swin, MENUBAR *menubar)
  54. {
  55.  
  56.   char * menu;
  57.  
  58.   char buffer[80];
  59.  
  60.   int status; 
  61.  
  62. #ifdef VMS
  63.   int keyboard;
  64.   short term_code;
  65. #else
  66.   char term_code;
  67. #endif
  68.  
  69. #ifdef VMS
  70.   if ( (( status =
  71.       SMG$CREATE_VIRTUAL_KEYBOARD(&keyboard))&1)!=1)
  72.     clean_up();
  73. #endif
  74.  
  75.   term_code = 0;
  76.  
  77.   while (term_code != RETURN) {
  78.  
  79.     /* get the new menubar string */
  80.     menu = strmenu(bar_size, menubar, menu_pos);
  81.  
  82.     mvwaddstr(swin, 1, 1, menu);
  83.     wrefresh(swin);
  84.  
  85.     /* get a single keystroke */
  86.  
  87. #ifdef VMS
  88.     if ( (( status = SMG$READ_KEYSTROKE
  89.          (&keyboard,&term_code))&1)!=1)
  90.       clean_up();
  91. #endif
  92. #ifdef BCC
  93.     term_code = wgetch(swin);
  94. #endif
  95. #ifdef HPUX
  96.     term_code = getch();
  97. #endif
  98. #ifdef SUN
  99.     term_code = getch();
  100.     if (term_code == ESCAPE) {
  101.       getch();
  102.       term_code = getch();
  103.     }
  104. #endif
  105.  
  106.     /* process keystroke */ 
  107.     switch (term_code) {
  108.  
  109.       /* arrows check for wrap-around */
  110.       case LEFT_ARROW:
  111.         if (menu_pos == 0)
  112.           menu_pos = TCHOICES-1;
  113.         else
  114.           menu_pos--;
  115.       break;
  116.  
  117.       case RIGHT_ARROW:
  118.         if (menu_pos == TCHOICES-1)
  119.           menu_pos = 0;
  120.         else
  121.           menu_pos++;
  122.       break;
  123.  
  124.       /* do nothing */
  125.       case RETURN:
  126.       break;
  127.  
  128.       /* exit program */
  129.       case ESCAPE:
  130.         clean_up();
  131.       break;
  132.  
  133.       /* return keyboard input */
  134.       default :
  135.         return (term_code);
  136.       break;
  137.  
  138.     }
  139.  
  140.   }
  141.  
  142.   /* return highlighted option */
  143.   return (menubar[menu_pos].letter);
  144.  
  145. }
  146.  
  147. WINDOW *popup(int rows,int columns,int sy,int sx)
  148. {
  149.   WINDOW *win;
  150.  
  151.   win = newwin(rows, columns, sy, sx);
  152.  
  153.   if(win == NULL) {
  154.     endwin();
  155.     clean_up();
  156.   }
  157.  
  158. #ifdef BCC
  159.   wattrset(win, F_BLACK | B_GRAY);
  160. #endif
  161.   box(win, SINGLE_SIDE, SINGLE_ACROSS);
  162.   wrefresh(win);
  163.  
  164.   return (win);
  165.  
  166. }
  167.  
  168.  
  169. /* erase windows and surrounding box */
  170. void erase_window(WINDOW *win)
  171. {
  172.  
  173.  
  174.   werase(win);
  175.   box(win, ' ', ' ');
  176.   wrefresh(win);
  177.  
  178.  
  179. }
  180.  
  181. void delete_window(WINDOW *win)
  182. {
  183.  
  184.   delwin(win);
  185.  
  186.  
  187. }
  188.  
  189. void refresh_window(WINDOW *win)
  190. {
  191.  
  192.   wrefresh(win);
  193.  
  194.  
  195. }
  196.  
  197. void touch_window(WINDOW *win)
  198. {
  199.  
  200.   touchwin(win);
  201.   wrefresh(win);
  202.  
  203.  
  204. }
  205.  
  206. /* process pulldown menu options */
  207. char do_pulldown
  208.       (int i, PULLDOWN *pullmenu, MENUBAR *menubar)
  209. {
  210.  
  211.   WINDOW *subwin1;
  212.  
  213.   int j;
  214.   int position, oldpos;
  215.  
  216.   char *ptr;
  217.  
  218.   int status;
  219.  
  220. #ifdef VMS
  221.   int keyboard;
  222.   short term_code;
  223. #else
  224.   char term_code;
  225. #endif
  226.  
  227. #ifdef VMS
  228.   if ( (( status =
  229.       SMG$CREATE_VIRTUAL_KEYBOARD(&keyboard))&1)!=1)
  230.     clean_up();
  231. #endif
  232.  
  233.   subwin1 = popup( (pullmenu[i].num)+2,
  234.     (pullmenu[i].maxlength)+2,stdscr->BEGY+3,
  235.       (menubar[i].pos)+2 );
  236.   
  237.   /* print pulldown options */
  238.  
  239.   for (j=0;j<pullmenu[i].num;j++) {
  240.  
  241.     ptr = pullmenu[i].ptr[j].string;
  242.  
  243.     mvwaddstr(subwin1, j+1, 1, ptr );
  244.  
  245.   }
  246.  
  247.   term_code = 0;
  248.  
  249.   position=0;
  250.   oldpos = 0;
  251.  
  252.   while (term_code != RETURN) {
  253.  
  254.     /* highlight selected option */
  255.  
  256.     ptr = pullmenu[i].ptr[position].string;
  257.  
  258.     strtoupper(ptr);
  259.  
  260.     mvwaddstr(subwin1, position+1, 1, ptr );
  261.  
  262.     wrefresh(subwin1);
  263.  
  264.     /* get keystroke */
  265.  
  266. #ifdef VMS
  267.     if ( (( status =SMG$READ_KEYSTROKE
  268.         (&keyboard,&term_code)) & 1)!=1)
  269.       clean_up();
  270. #endif
  271. #ifdef BCC
  272.     term_code = wgetch(subwin1);
  273. #endif
  274. #ifdef HPUX
  275.     term_code = getch();
  276. #endif
  277. #ifdef SUN
  278.     term_code = getch();
  279.     if (term_code == ESCAPE) {
  280.       getch();
  281.       term_code = getch();
  282.     }
  283. #endif
  284.  
  285.  
  286.     oldpos = position;
  287.   
  288.     /* process keystroke */
  289.  
  290.     switch (term_code) {
  291.  
  292.       case UP_ARROW:
  293.  
  294.         if (position == 0)
  295.           position = pullmenu[i].num-1;
  296.         else
  297.           position--;
  298.  
  299.       break;
  300.  
  301.       case DOWN_ARROW:
  302.  
  303.         if (position == pullmenu[i].num-1)
  304.           position = 0;
  305.         else
  306.           position++;
  307.  
  308.       break;
  309.  
  310.       /* do nothing */
  311.       case RETURN:
  312.       break;
  313.  
  314.       /* get keyboard input and
  315.           erase menu */
  316.       default :
  317.         erase_window(subwin1);
  318.         delwin(subwin1);
  319.  
  320.         touchwin(stdscr);
  321.         wrefresh(stdscr);
  322.         touchwin(dialogue);
  323.         wrefresh(dialogue);
  324.  
  325.         return (term_code);
  326.       break;
  327.  
  328.     }
  329.  
  330.     /* restore to lowercase */
  331.  
  332.     ptr = pullmenu[i].ptr[oldpos].string;
  333.  
  334.     strtolower(ptr);
  335.     mvwaddstr(subwin1, oldpos+1, 1, ptr );
  336.  
  337.     wrefresh(subwin1);
  338.  
  339.   }
  340.  
  341.   /* return highlighted optoin 
  342.      and erase menu */
  343.   delwin(subwin1);
  344.   erase_window(subwin1);
  345.   touchwin(stdscr);
  346.   wrefresh(stdscr);
  347.   touchwin(dialogue);
  348.   wrefresh(dialogue);
  349.   return (pullmenu[i].ptr[position].letter);
  350.  
  351.  
  352. }
  353.  
  354. /* calculate and produce menubar string */
  355. char *strmenu(int length, MENUBAR *menubar, int pos)
  356. {
  357.  
  358.   int i,j,k;
  359.   int count;
  360.   int string_length;
  361.  
  362.   static char buffer[100];
  363.  
  364.   /* determine max length for string */
  365.   string_length = length/TCHOICES;
  366.  
  367.   k = 0;
  368.   j = 0;
  369.  
  370.   /* add proper number of options */
  371.   for (i=0;i<TCHOICES;i++) {
  372.  
  373.     menubar[i].pos = k;
  374.  
  375.     /* add each option, highlight as necessary */
  376.     for (j=0;menubar[i].string[j]!='\0';j++,k++) {
  377.       if (pos == i)
  378.         buffer[k] = toupper(menubar[i].string[j]);  
  379.       else
  380.         buffer[k] = tolower(menubar[i].string[j]);
  381.     }
  382.  
  383.     /* pad with spaces to proper length */
  384.     while (k<(string_length*(i+1)+2)) {
  385.       buffer[k] = ' ';
  386.       k++;
  387.     }
  388.  
  389.   }
  390.  
  391.  
  392.   return(buffer);
  393.  
  394. }
  395.  
  396. /* initialize the screen at start */
  397. void set_stdscr(void)
  398. {
  399.  
  400.   int i;
  401.  
  402.   wclear (stdscr);
  403.  
  404. #ifdef BCC
  405.   wattrset(stdscr, F_RED | B_BLUE);
  406.   /* fill in screen with color */
  407.   for (i=0;i<MAX_ROWS;i++)
  408.     mvinsertln(i,0);
  409. #endif
  410.   box(stdscr, DOUBLE_SIDE, DOUBLE_ACROSS);
  411.  
  412.   refresh();
  413.  
  414.   return;
  415.  
  416. }
  417.  
  418. /* print string to dialogue box */
  419. int to_dialogue(char *string)
  420. {
  421.  
  422.   clear_dialogue();
  423.   mvwaddstr(dialogue, 1, 1, string);
  424.   box(dialogue, SINGLE_SIDE, SINGLE_ACROSS);
  425.   wrefresh(dialogue);
  426.  
  427.   return;
  428.  
  429. }
  430.  
  431. void clear_dialogue(void)
  432. {
  433.  
  434.   werase(dialogue);
  435.   box(dialogue, SINGLE_SIDE, SINGLE_ACROSS);
  436.   wrefresh(dialogue);
  437.  
  438.   return;
  439.  
  440. }
  441.  
  442. void execute_command
  443.         (int i, int choice, PULLDOWN *pullmenu)
  444. {
  445.  
  446.   int j;
  447.  
  448.   touch_window(tbar);
  449.  
  450.   for (j=0;j<pullmenu[i].num;j++) {
  451.  
  452.     /* use function pointer to execute command */
  453.     if ( choice == pullmenu[i].ptr[j].letter) {
  454.       (*(pullmenu[i].ptr[j].funcptr))();
  455.       break;
  456.     };
  457.  
  458.   }
  459.  
  460.   clear_dialogue();
  461.  
  462. }
  463.  
  464. /* convert a string to all uppercase */
  465. void strtoupper(char *string)
  466. {
  467.  
  468.   int i;
  469.  
  470.   for (i=0;string[i]!='\0';i++) {
  471.  
  472.     string[i] = toupper(string[i]);
  473.  
  474.   }
  475.  
  476.   return;
  477.  
  478. }
  479.  
  480. /* convert a string to all lowercase */
  481. void strtolower(char *string)
  482. {
  483.  
  484.   int i;
  485.  
  486.   for (i=0;string[i]!='\0';i++) {
  487.  
  488.     string[i] = tolower(string[i]);
  489.  
  490.   }
  491.  
  492.   return;
  493.  
  494. }
  495.  
  496.