/*
DRAW
This macro provides the user with a simple line-drawing facility using the
ASCII line drawing characters. When you press <ALT D>, you are prompted for 
a choice of selecting the type of line drawing chars or going into drawing
mode. You can select from three different styles of drawing characters -
ASCII, single line or double line.

Once you are in drawing mode, you can use the cursor keypad to draw your
box characters (keys 1,2,3,4,6,7,8,9). Pressing <ESC> will exit drawing
mode.
*/

#define  UP       200
#define  DOWN     208
#define  LEFT     203
#define  RIGHT    205
#define  PGDN     209
#define  PGUP     201
#define  HOME     199
#define  END      207
#define  RETURN   10
#define  ESC      27
#define  ALT_D    160

int reached_eof;

string tl;
string t;
string tr;
string r;
string br;
string b;
string bl;
string l;

init()
{
  tl = chr(218);
  t  = chr(196);
  tr = chr(191);
  r  = chr(179);
  br = chr(217);
  b  = chr(196);
  bl = chr(192);
  l  = chr(179);
  assign_key("draw_cmd", ALT_D);
}


draw_cmd()
{
  message("Draw cmd:  D)raw, T)ype_box");
  key = get_tty_char();
  if (key == 'd' || key == 'D')
    draw();
  else if (key == 't' || key == 'T')
    type_box();
}


draw()
{
   int   key;
                   
   setprompt(2);
   key = 0;
   while (key != RETURN && key != ESC)
   {
      message(sprintf(" Draw Mode: Use numeric keypad to draw...ESC to quit      col: %d", currcol()));
      key = get_tty_char();
      if (key >= HOME && key <= PGDN)
      {  
         if (key == UP)
         {
            insert(t);
            if (! is_eol()) 
               delchar();
         }
         else if (key == DOWN)
         {
            insert(b);
            if (! is_eol()) 
               delchar();
            left();
            left();
         }
         else if (key == LEFT)
         {
            insert(l);
            if (! is_eol()) 
               delchar();
            left();
            up();
         }
         else if (key == RIGHT)
         {
            insert(r);
            if (! is_eol()) 
               delchar();
            left();
            if (!down())  appnd_line();
         }
         else if (key == PGDN)
         {
            insert(br);
            if (! is_eol()) 
               delchar();
            left();
            left();
         }
         else if (key == PGUP)
         {
            insert(tr);
            if (! is_eol()) 
               delchar();
            left();
            if (!down())  appnd_line();
         }
         else if (key == HOME)
         {
            insert(tl);
            if (! is_eol()) 
               delchar();
         }
         else if (key == END)
         {
            insert(bl);
            if (! is_eol()) 
               delchar();
            left();
            up();
         }
      }
   }

   setprompt(0);
   return(key);
}


/* posn_cursor() - this internal macro lets the user position cursor */
posn_cursor(msg)
string msg;
{
   int   key,
         col;
                   
   key = 0;
   while (key != RETURN && key != ESC)
   {
      message(sprintf(strcat(msg,"  col:  %d  line:  %d"),
                                                 currcol(), currlinenum()));
      key = get_tty_char();
      if (key >= HOME && key <= PGDN)
      {  
         if (key == UP)
            up();
         else if (key == DOWN)
         {
            if (!down())
              append_line();
         }
         else if (key == LEFT)
            left();
         else if (key == RIGHT)
            right();
         else if (key == PGDN)
            nextscreen();
         else if (key == PGUP)
            prevscreen();
         else if (key == HOME)
            gobol();
         else if (key == END)
            goeol();
      }
   }
   return(key);
}


type_box()
{
   message("Choose box type:  A)scii, D)ouble_line, S)ingle_line");
   key = get_tty_char();

   if (key == 'a' || key == 'A')
   {
      tl = chr(45);
      t  = chr(45);
      tr = chr(45);
      r  = chr(124);
      br = chr(45);
      b  = chr(45);
      bl = chr(45);
      l  = chr(124);
   }
   else if (key == 'd' || key == 'D')
   {
      tl = chr(201);
      t  = chr(205);
      tr = chr(187);
      r  = chr(186);
      br = chr(188);
      b  = chr(205);
      bl = chr(200);
      l  = chr(186);
   }
   else if (key == 's' || key == 'S')
   {
      tl = chr(218);
      t  = chr(196);
      tr = chr(191);
      r  = chr(179);
      br = chr(217);
      b  = chr(196);
      bl = chr(192);
      l  = chr(179);
   }
}


appnd_line()
{
  col = currcol();
  goeol();
  insert("\n");
  setcol(col);
}

