/* Part of DoorLib v1.12

   Name   : DrawBox
   Version: 1.12
   Date   : Saturday, 23rd November 1996

   Purpose: DoorLib's box drawing function.

   Creator: Richard Murray
*/

#include <stdio.h>
#include <stdlib.h>

#include "ctype.h"
#include "kernel.h"
#include "stdarg.h"
#include "strings.h"
#include "time.h"

#include "doorlib.h"


int doors_drawbox(int bs_port, int bs_startX, int bs_startY, int bs_width, int bs_height,int bs_type)
/* Draws a box on the screen... 'bs_startX' and 'bs_startY' are the cursor beginning points.
   The cursor is *returned* to there after the box has been drawn. The 'bs_width' is how
   many characters across the box is, and the 'bs_height' is how many rows (down) it goes.
   The type is TRUE for double-outline and FALSE for single-outline. Other values fall back
   to a simple ASCII outline style. */
{
   int  bs_upperleft  =  46;  /* period                                      */
   int  bs_lowerleft  =  39;  /* apostrophe   .---------------------------.  */
   int  bs_upperright =  46;  /* period       | Will provide fallback to  |  */
   int  bs_lowerright =  39;  /* apostrophe   | a simple box like this... |  */
   int  bs_vertical   = 124;  /* bar (|)      '---------------------------'  */
   int  bs_horizontal =  45;  /* minus                                       */
   int  bs_voffset;           /* Vertical drawing counter                    */
   int  bs_hoffset;	      /* Horizontal (string creation) counter 	     */
   int  bs_X;	      	      /* X variable 	    	      		     */
   int  bs_Y;		      /* Y variable				     */
   char bs_buffer[80];        /* Room for 80col. 160col not supported...     */

   /* A few simple validity checks */
   if (bs_startX<0 || bs_startX>80) return 20;
   if (bs_startY<0 || bs_startY>21) return 21;   /* 21+3 for the box = 24 rows */
   if (bs_width<3 || bs_width>80) return 22;     /* Minimum width is three */
   if (bs_height<3 || bs_height>24) return 23;
   if ((bs_height+bs_startY)>24) return 25;      /* Overrun the screen? (vertical) */
   if ((bs_width+bs_startX)>80) return 26;       /* Overrun the screen? (horizontal) */

   /* Amend border characters as required */
   if (bs_type==TRUE)
   {
      bs_upperleft  = ''; bs_lowerleft  = '';  /* IBM-ANSI double-outline box */
      bs_upperright = ''; bs_lowerright = '';  /* characters. */
      bs_vertical   = ''; bs_horizontal = '';
   }

   if (bs_type==FALSE)
   {
      bs_upperleft  = ''; bs_lowerleft  = '';  /* IBM-ANSI single-outline box */
      bs_upperright = ''; bs_lowerright = '';  /* characters */
      bs_vertical   = ''; bs_horizontal = '';
   }

   /* Move to starting position */
   doors_txf(bs_port,"%c[%d;%dH",27,bs_startY,bs_startX);

   /* Draw top line of our box */
   bs_buffer[0]=bs_upperleft;
   bs_hoffset=0;
   while (bs_hoffset<(bs_width-2))
   {
      bs_buffer[bs_hoffset+1]=bs_horizontal;
      bs_hoffset++;
   }
   bs_buffer[bs_hoffset+1]=bs_upperright;
   bs_buffer[bs_hoffset+2]='\0';
   doors_tx(bs_port,bs_buffer);

   /* Fill in the middle bits */
   bs_voffset=0;
   while (bs_voffset<(bs_height-2))
   {
      bs_X=bs_startX;
      bs_Y=bs_startY + bs_voffset + 1;
      doors_txf(bs_port,"%c[%d;%dH%c",27,bs_Y,bs_X,bs_vertical);
      bs_X=bs_startX + bs_width - 1;
      bs_Y=bs_startY + bs_voffset + 1;
      doors_txf(bs_port,"%c[%d;%dH%c",27,bs_Y,bs_X,bs_vertical);
      bs_voffset++;
   }

   /* Fill in the bottom line now */
   bs_buffer[0]=bs_lowerleft;
   bs_hoffset=0;
   while (bs_hoffset<(bs_width-2))
   {
      bs_buffer[bs_hoffset+1]=bs_horizontal;
      bs_hoffset++;
   }
   bs_buffer[bs_hoffset+1]=bs_lowerright;
   bs_buffer[bs_hoffset+2]='\0';
   bs_Y=bs_startY + bs_height - 1;
   doors_txf(bs_port,"%c[%d;%dH",27,bs_Y,bs_startX);
   doors_tx(bs_port,bs_buffer);

   /* Return cursor to start */
   doors_txf(bs_port,"%c[%d;%dH",27,bs_startY,bs_startX);

   /* Done! */
   return 0;
}


void doors_includeX1(void)
{
   printf("BUDGIESOFT DOORLIB :: DRAWBOX");
}
