/*
  MENU menu-name r1, c1, r2, c2 [COLOR color]
  {
    text     [->function]
    ......
  }
*/

#define HORIZ 1
#define VERT  2
#define TOP_LEFT  3
#define TOP_RIGHT 4
#define BOT_LEFT  5
#define BOT_RIGHT 6

#define DOWN    208
#define UP      200

string BoxChars[10];

int MenuRow1, MenuCol1, MenuRow2, MenuCol2;
int MenuColor;
string MenuName;
int debug;


do_menu(menuname)
  string menuname;
{
  debug = 0;

  old_buf = currbuf();

  /*
     The format of a menu is :
       MENU menu-name row1, col1, row2, col2 [COLOR colornum]
       {
         text1          [->function]
         .....          ............
       }
  */

  if (!index(menuname, ".mnu"))
    menuname = strcat(menuname, ".mnu");
  new_buf = setcurrbuf(create_buffer(menuname));
  setcol(1);

  if (MenuParse() < 0)
  {
    delete_buffer(new_buf);
    show_buffer(old_buf);
    return;
  }
  
  win_id = create_window(MenuRow1+1, MenuCol1+1, MenuRow2-1, MenuCol2-1, MenuColor);
  attach_window(win_id, new_buf);
  gobof();
  DrawBox(MenuRow1-1, MenuCol1, MenuRow2-1, MenuCol2, MenuColor);
  
  MenuProcess();

  delete_buffer(new_buf);
  show_buffer(old_buf);
  refresh();
}


MenuParse()
{
  if (get_token() != "MENU")
  {
    message("The file is not a menu deinition file");
    get_tty_char();
    return -1;
  }
  MenuName = get_token();
  MenuRow1 = get_int_token();
  MenuCol1 = get_int_token();
  MenuRow2 = get_int_token();
  MenuCol2 = get_int_token();
  
  if ((s = get_token()) == "COLOR")
  {
    MenuColor = get_int_token();
    s = get_token();
  }
  else
    MenuColor = 07;

  if (s != "{")
  {
    message("Missing '{' in menu definition");
/*
    get_tty_char();
    return -1;
*/
  }

  start_text_line = currlinenum();

  /* Get rid of the Menu Defn line and the opening brace */
  gobof();
  setcount(start_text_line-1);
  delline();
  
  /* Get rid of the ending brace */
  if (fsearch("}") > 0)
    delline();
  /* Get rid of any trailing blank lines */
  while (strlen(currline()) == 0)
    delline();
    
  return 1;
}

get_token()
{
  string s;

  if (is_eol() || currchar() == " ")
    nextword();

  if (is_eof())
    return "";

  start = currcol();
  while (!is_eol() && currchar() != ' ')
    right();
  s = substr(currline(), start, currcol() - start);
  nextword();
  if (debug)
    get_tty_str(sprintf("GET_TOKEN - s is [%s]", s));
  return s;
}

get_int_token()
{
  n = atoi(get_token());
  if (debug)
    get_tty_str(sprintf("GET_INT_TOKEN - n is %d", n));
  return n;
}

/* ------------------------- MENU PROCESSING ------------------------- */

#define ESC   27
#define UP    200
#define DOWN  208

MenuProcess()
{
  markline();

do_it:
  while ((c = get_tty_char()) != '\n' && c != ESC)
  {
    switch (c)
    {
      case UP   :
        MenuUp();
        break;
      case DOWN :
        MenuDown();
        break;
      default   :
        bell();
        break;
    }
  }
  
  if (c == '\n')
  {
    cln = currlinenum();

    if ((i = index(currline(), "->")) > 0)
    {
      macname = substr(currline(), i+2, 100);
      domacro(macname);
    }

    DrawBox(MenuRow1-1, MenuCol1, MenuRow2-1, MenuCol2, MenuColor);
    goline(cln);
    goto do_it;
  }
}


MenuUp()
{
  if (currlinenum() > 1)
  {
    clear_mark();
    up();
    markline();
  }
}

MenuDown()
{
  if (currlinenum() < lastlinenum())
  {
    clear_mark();
    down();
    markline();
  }
}

DrawBox(row1, col1, row2, col2, color)
{
  BoxChars[HORIZ]     = "Í";
  BoxChars[VERT]      = "º";
  BoxChars[TOP_LEFT]  = "É";
  BoxChars[TOP_RIGHT] = "»";
  BoxChars[BOT_LEFT]  = "È";
  BoxChars[BOT_RIGHT] = "¼";
  
  start_row = row1;
  start_col = col1;
  end_row   = row2;
  end_col   = col2;

  width = col2 - col1 + 1;
  horiz_line = repstr(BoxChars[HORIZ], width - 2);
  blank_line = repstr(" ", width - 2);

  display(row1, col1 + 1, col1 + width - 1,  color, horiz_line);
  display(row2, col1 + 1, col1 + width - 1,  color, horiz_line);
  for (r = row1+1;  r < row2;  r++)
  {
    display(r, col1, col1, color, BoxChars[VERT]);
/*  display(r, col1+1, col2-1, color, blank_line); */
    display(r, col2, col2, color, BoxChars[VERT]);
  }
  display(row1, col1, col1,  color, BoxChars[TOP_LEFT]);
  display(row1, col2, col2,  color, BoxChars[TOP_RIGHT]);
  display(row2, col1, col1,  color, BoxChars[BOT_LEFT]);
  display(row2, col2, col2,  color, BoxChars[BOT_RIGHT]);
}


