Listing 3 uilibs.c — library code

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#ifdef VMS
#include <smgdef.h>
#endif
#ifdef BCC
#include <dos.h>
#include <conio.h>
#include <ctype.h>
#endif

#include "curses.h"
#include "menu.h"

extern WINDOW *dialogue;
extern WINDOW *tbar;

static bar_size;   /* size of menubar */
static int.menu_pos;  /* position in menubar */

/* display menubar */
WINDOW *topbar(WINDOW *win)
{

  WINDOW *swin;

  int string_count, string_size;
  if((swin = subwin(win,3,(win->MAXX)-4,(win->BEGY)+1,
       (win->BEGX)+2)) == NULL)
    clean_up();

#ifdef BCC
  wattrset(swin, F_BLUE | B_GRAY);
#endif

  box (swin, SINGLE_SIDE,SINGLE_ACROSS);

  bar_size = (swin->MAXX)-2;
  menu_pos=0;

  return (swin);
}

/* print string to menubar */
char do_menubar(WINDOW *swin, MENUBAR *menubar)
{

  char * menu;

  char buffer[80];

  int status;

#ifdef VMS
  int keyboard;
  short term_code;
#else
  char term_code;

#endif

#ifdef VMS
  if ( (( status =
      SMG$CREATE_VIRTUAL_KEYBOARD(&keyboard))&1)!=1)
    clean_up();
#endif

  term_code = 0;

  while (term_code != RETURN) {

    /* get the new menubar string */
    menu = strmenu(bar_size, menubar, menu_pos);

    mvwaddstr(swin, 1, 1, menu);
    wrefresh(swin);

    /* get a single keystroke */

#ifdef VMS
    if ( (( status = SMG$READ_KEYSTROKE
        (&keyboard,&term_code))&l)!=l)
      clean_up();
#endif
#ifdef BCC
    term_code = wgetch(swin);
#endif
#ifdef HPUX
    term_code = getch();
#endif
#ifdef SUN
    term_code = getch();
    if (term_code == ESCAPE) {
      getch();
      term_code = getch();
    }
#endif

    /* process keystroke */
    switch (term_code) {

      /* arrows check for wrap-around */
      case LEFT_ARROW:
        if (menu_pos == 0)
          menu_pos = TCHOICES-1;
        else
          menu_pos--;
      break;

      case RIGHT_ARROW:
        if (menu_pos == TCHOICES-1)
          menu_pos = 0;
        else
          menu_pos++;
      break;

      /* do nothing */
      case RETURN:
      break;

      /* exit program */
      case ESCAPE:
        clean_up();
      break;

      /* return keyboard input */
      default :
        return (term_code);
      break;

    }

  }

  /* return highlighted option */
  return (menubar[menu_pos].letter);

}

WINDOW *popup(int rows,int columns,int sy,int sx)
{
  WINDOW *win;

  win = newwin(rows, columns, sy, sx);

  if(win == NULL) {
    endwin();
    clean_up();
  }

#ifdef BCC
  wattrset(win, F_BLACK | B_GRAY);
#endif
  box(win, SINGLE_SIDE, SINGLE_ACROSS);
  wrefresh(win);

  return (win);

}

/* erase windows and surrounding box */
void erase_window(WINDOW *win)
{

  werase(win);
  box(win, ' ', ' ');
  wrefresh(win);

}

void delete_window(WINDOW *win)
{

  delwin(win);

}

void refresh_window(WINDOW *win)
{

  wrefresh(win);

}

void touch_window(WINDOW *win)
{

  touchwin(win);
  wrefresh(win);

}

/* process pulldown menu options */
char do_pulldown
     (int i, PULLDOWN *pullmenu, MENUBAR *menubar)

{

  WINDOW *subwin1;

  int j;
  int position, oldpos;

  char *ptr;

  int status;

#ifdef VMS
  int keyboard;
  short term_code;
#else
  char term_code;
#endif

#ifdef VMS
  if ( (( status =
      SMG$CREATE_VIRTUAL_KEYBOARD(&keyboard))&1)!=1)
    clean_up();
#endif

  subwin1 = popup( (pullmenu[i].num)+2,
    (pullmenu[i].maxlength)+2,stdscr->BEGY+3,
      (menubar[i].pos)+2);

  /* print pulldown options */

  for (j=0;j<pullmenu[i].num;j++) {

    ptr = pullmenu[i].ptr[j].string;

    mvwaddstr(subwin1, j+1, 1, ptr );

  }

  term_code = 0;

  position=0;
  oldpos = 0;

  while (term_code != RETURN) {

    /* highlight selected option */

    ptr = pullmenu[i].ptr[position].string;

    strtoupper(ptr);

    mvwaddstr(subwin1, position+1, 1, ptr );

    wrefresh(subwin1);

    /* get keystroke */

#ifdef VMS
    if ( (( status =SMG$READ_KEYSTROKE
        (&keyboard,&term_code)) & 1)!=1)
      clean_up();
#endif
#ifdef BCC
    term_code = wgetch(subwin1);
#endif
#ifdef HPUX
    term_code = getch();
#endif
#ifdef SUN
    term_code = getch();
    if (term_code == ESCAPE) {
      getch();
      term_code = getch();
    }
#endif

    oldpos = position;

    /* process keystroke */

    switch (term_code) {

      case UP_ARROW:

        if (position == 0)
          position = pullmenu[i].num-1;
        else
          position--;

      break;

      case DOWN_ARROW:

        if (position == pullmenu[i].num-1)
          position = 0;
        else
          position++;

      break;

      /* do nothing */
      case RETURN:
      break;

      /* get keyboard input and
          erase menu */
      default :
        erase_window(subwin1);
        delwin(subwin1);

        touchwin(stdscr);
        wrefresh(stdscr);
        touchwin(dialogue);
        wrefresh(dialogue);

        return (term_code);
      break;

    }

    /* restore to lowercase */

    ptr = pullmenu[i].ptr[oldpos].string;

    strtolower(ptr);
    mvwaddstr(subwin1, oldpos+1, 1, ptr );

    wrefresh(subwin1);

  }

  /* return highlighted optoin
     and erase menu */
  delwin(subwin1);
  erase_window(subwin1);
  touchwin(stdscr);
  wrefresh(stdscr);
  touchwin(dialogue);

  wrefresh(dialogue);
  return (pullmenu[i].ptr[position].letter);

}

/* calculate and produce menubar string */
char *strmenu(int length, MENUBAR *menubar, int pos)
{

  int i,j,k;
  int count;
  int string_length;

  static char buffer[100];

  /* determine max length for string */
  string_length = length/TCHOICES;

  k = 0;
  j = 0;

  /* add proper number of options */
  for (i=0;i<TCHOICES;i++) {

    menubar[i].pos = k;

    /* add each option, highlight as necessary */
    for (j=0;menubar[i].string[j]!='\0';j++,k++) {
      if (pos == i)
        buffer[k] = toupper(menubar[i].string[j]);
      else
        buffer[k] = tolower(menubar[i].string[j]);
    }

    /* pad with spaces to proper length */
    while (k<(string_length*(i+1)+2)) {
      buffer[k] = ' ';
      k++;
    }

  }

  return(buffer);

}

/* initialize the screen at start */
void set_stdscr(void)
{

  int i;

  wclear (stdscr);

#ifdef BCC
  wattrset(stdscr, F_RED | B_BLUE);
  /* fill in screen with color */
  for (i=0;i<MAX_ROWS;i++)
    mvinsertln(i,0);
#endif
  box(stdscr, DOUBLE_SIDE, DOUBLE_ACROSS);

  refresh();

  return;

}

/* print string to dialogue box */
int to_dialogue(char *string)
{

  clear_dialogue();
  mvwaddstr(dialogue, 1, 1, string);

  box(dialogue, SINGLE_SIDE, SINGLE_ACROSS);
  wrefresh(dialogue);

  return;

}

void clear_dialogue(void)
{

  werase(dialogue);
  box(dialogue, SINGLE_SIDE, SINGLE_ACROSS);
  wrefresh(dialogue);

  return;

}

void execute_command
       (int i, int choice, PULLDOWN *pullmenu)
{

  int j;

  touch_window(tbar);

  for (j=0;j<pullmenu[i].num;j++) {

    /* use function pointer to execute command */
    if ( choice == pullmenu[i].ptr[j].letter) {
      (*(pullmenu[i].ptr[j].funcptr))();
      break;
    };

  }

  clear_dialogue();

}

/* convert a string to all uppercase */
void strtoupper(char *string)
{

  int i;

  for (i=0;string[i]!='\0';i++) {

    string[i] = toupper(string[i]);

  }

  return;

}

/* convert a string to all lowercase */
void strtolower(char *string)
{

  int i;

  for (i=0;string[i]!='\0';i++) {

    string[i] = tolower(string[i]);

  }

  return;

}

/* End of File */