/****************************************************
 * vt100 emulator - window/keyboard support
 * This has been rationalized and speeded up quite a bit. (but wait for
 * AmigaDOS 1.2!) The Tek support is put in in the appropriate place
 * The function emit() is now never used to handle remote characters.
 *    T.Whelan Oct 1986
 *
 *           860823 DBW - Integrated and rewrote lots of code
 *      v2.0 860809 DBW - Major rewrite
 *      v1.1 860720 DBW - Switches, 80 cols, colors, bug fixes
 *      v1.0 860712 DBW - First version released
 *
 ****************************************************/

#include "vt100.h"

/* forward declarations for LATTICE */
void filename();
void emits();
void emit();
void emitbatch();
void cursoroff();
void cursoron();

/************************************************
*  function to print a string
*************************************************/
void emits(string)
char string[];
    {
    int i;
    char c;

    i=0;
    while (string[i] != 0)
        {
        c=string[i];
        if (c == 10) emit(13);
        emit(c);
        i += 1;
        }
    }

/*************************************************
*  function to output ascii chars to window
*************************************************/
void emit(c)
char c;
    {
    switch( c )
        {
        case '\t':
        x += 64 - ((x-MINX) % 64);
        break;

        case 10:  /* lf */
        y += 8;
        break;

        case 13:  /* cr */
        x = MINX;
        break;

        case 8:   /* backspace */
        x -= 8;
        if (x < MINX) x = MINX;
        break;

        case 12:     /* page */
        x = MINX;
        y = MINY;
        SetRast(mywindow->RPort, 0);
        break;

        case 7:     /* bell */
        DisplayBeep(NULL);
        break;

        default:
        if (c < ' ' || c > '~') break;
	   emitbatch(1, &c);
        } /* end of switch */

    while (x > MAXX) x -= 8;
    while (y > MAXY) {
        y -= 8;
        x  = MINX;
        ScrollRaster(mywindow->RPort, 0, 8, MINX, MINY-6, MAXX+7, MAXY+1);
        }
    }

/*************************************************
*  function to output ascii chars to window.
* this function will only ever get printable characters.
*************************************************/
void emitbatch(la,lookahead)
int la;
char *lookahead;
    {
    int i;

    if (TekMode) {
      for (i = 0; i < la; i++)
         Tek(lookahead[i]);
      return;
      }

    Move(mywindow->RPort,x, y);
    i = x / 8;
    if (i+la > maxcol) la = maxcol-i;

/* the logic here is built for speed, but I think that all the time is
 * spent in Text(), so there is not much gain */
    if (curmode&BOLD)
             SetAPen(mywindow->RPort, 3);

    if (curmode&REVERSE)
        SetDrMd(mywindow->RPort, JAM2+INVERSVID);

    Text(mywindow->RPort,lookahead, la);

    if (curmode&UNDERLINE) {
         Move(mywindow->RPort, x-1 ,y+1);
         Draw(mywindow->RPort, x+8*la+8, y+1);
         }

     if (curmode&BOLD)
          SetAPen(mywindow->RPort, 1);

    if (curmode&REVERSE) 
        SetDrMd(mywindow->RPort, JAM2);

    x += (8 * la);
    
    }

/******************************
* Manipulate cursor
******************************/
void cursoroff()
    {
    SetDrMd(mywindow->RPort, COMPLEMENT);
    SetAPen(mywindow->RPort, 3);
    RectFill(mywindow->RPort, x, y-6, x+8, y);
    SetAPen(mywindow->RPort,1);
    SetDrMd(mywindow->RPort, JAM2);
    }

void cursoron()
    {
    SetDrMd(mywindow->RPort, COMPLEMENT);
    SetAPen(mywindow->RPort,3);
    RectFill(mywindow->RPort, x, y-6, x+8, y);
    SetAPen(mywindow->RPort,1);
    SetDrMd(mywindow->RPort, JAM2);
    }
