#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <bios.h>
#include <string.h>
#include "mouse.h"
#include <errno.h>

#define XPAND 3
#define IX 240

#define NOT_MOVED 0
#define LEFT  1
#define RIGHT  2
#define UP  3
#define DOWN  4

#define TRUE 1
#define FALSE 0

char getpoint(int x, int y), getkey(void);
void mode(int mode_code), gotoxy (int x, int y);
void palette(int pnum), clr_eol(int x, int y);
void display_grid(void), edit_icon(void);
void display_icon(int x, int y);
void init_icon(void);
int save_icon(void), load_icon(char *fname);
void putpoint(int x, int y, int color, int how);
int change_colors(void);
void opening_screen(int argc);
void status_line(void);
void box(int startx, int endx, int starty, int endy, int how, int color);
int rightb_pressed(void);
int leftb_pressed(void);
void mouse_motion(int *deltax, int *deltay);

struct icon_type {
  unsigned char image[60][52];
  void (*func)();

  int XDim, YDim;
  int x, y;
  } icon;

char far *egabase;
int FORE_GND = 15;
int BCK_GND ;
int XDIM, YDIM;
int IY, IIY;


void main(int argc, char *argv[])
{
  mouse_init ();
  hide_cursor();

  mode (16);

  egabase = (char far *) MK_FP(0xA000, 0000);

  opening_screen (argc);
  status_line ();

  init_icon();

  if (argc==2) load_icon(argv[1]);

  edit_icon();
  mode (3);
}

void display_grid(void)
{
  register int x, y;

  for (y=IY; y<(IY+YDIM); y++)
    for (x=IX; x<(IX+XDIM); x++)
      putpoint(x+((x-IX)*XPAND), y+((y-IY)*XPAND),
            icon.image[x-IX][y-IY], 0);
}

void edit_icon()
{
  register int x, y;
  char ch, pen, temp;
  int button, xpos, ypos;
  int draw = FALSE;
  int HIGHLIGHT ;
  /* int x1, y1;  alternate mouse update varibles */

  if (BCK_GND != 15 && BCK_GND != 14 && BCK_GND != 7)
     HIGHLIGHT = 15;
  else
     HIGHLIGHT = 4;
  x = IX+1; y = IY+1;
  pen = BCK_GND;

  hide_cursor ();
  display_icon(0, IY);
  display_grid();

  do {


      putpoint(x+((x-IX)*XPAND), y+((y-IY)*XPAND), pen, 0);

      icon.image[x-IX][y-IY] = pen;

      putpoint(x-IX, y, icon.image[x-IX][y-IY], 0);

      temp = getpoint(x+((x-IX)*XPAND), y+((y-IY)*XPAND));
      putpoint(x+((x-IX)*XPAND), y+((y-IY)*XPAND), HIGHLIGHT, 0);

      show_cursor ();

     ch = 0;
     while(1) {
	 get_status(&button, &xpos, &ypos);
	 gotoxy(70,5);
	 printf ("%3d:%3d", xpos, ypos);

	if (button==1) {

	  if (xpos > IX && xpos < ((IX + XDIM) + (XDIM*XPAND)) &&
	  ypos > IY && ypos < ((IY + YDIM) + (YDIM*XPAND))){

	     hide_cursor ();
	     putpoint (x+((x-IX)*XPAND), y+((y-IY)*XPAND), temp, 0);
	     x = ((xpos / 4)- 60)+ IX;
	     y = ((ypos / 4)-IIY)+IY;
	     break;
	       }

       if (ypos < 12 && ypos >= 0){
         if (xpos > 0 && xpos < 70){
	    ch = 59;
	    hide_cursor();
	    break; }
	 if (xpos > 100 && xpos < 220){
	    ch = 60;
	    hide_cursor();
	    break;  }
	  if (xpos > 250 && xpos < 360){
	    ch = 61;
	    hide_cursor();
	    break;  }
	  if (xpos > 361 && xpos < 539){
	    ch = 62;
	    hide_cursor();
	    break; }
	  if (xpos > 540 && xpos < 639){
	    ch = 63;
	    hide_cursor();
	    break; } }
       if (ypos >= 321 && ypos <= 335){
	   if (xpos > 550 && xpos < 639){
	     mode (3);
	     exit (0);  }
	   if (xpos >= 0 && xpos < 80){
	     ch = 64;
	     hide_cursor();
	     break;  }
	    }

	 }
       if (kbhit()) {
	 ch = getkey ();
	 hide_cursor ();
	 putpoint(x+((x-IX)*XPAND), y+((y-IY)*XPAND), temp, 0);
	 break;  }
  }



  switch(ch) {
     case 75: /* left */
       x--;
       break;
     case 77: /* right */
       x++;
       break;
     case 72: /* up */
       y--;
       break;
     case 80: /* down */
       y++;
       break;
     case 71: /* up left */
       x--; y--;
       break;
     case 73: /* up right */
       x++; y--;
       break;
     case 79: /* down left */
       x--; y++;
       break;
     case 81: /* down right */
       x++; y++;
       break;
     case 59: pen = FORE_GND; /* F1 - pen down */
       draw = TRUE;
       gotoxy (39, 3);
       printf ("draw       ");
       break;
     case 60: pen = BCK_GND; /* F2 - pen up */
       draw = FALSE;
       gotoxy (39, 3);
       printf ("erase/move");
       break;
     case 61: save_icon();
       break;
     case 62: load_icon("");
       display_icon(0, IY);
       display_grid ();
       break;
     case 63:
       gotoxy (60, 20);
       printf ("New Canvas Color ?");
       BCK_GND = change_colors();
       if (!draw)
	 pen = BCK_GND;
       if (BCK_GND != 15 && BCK_GND != 14 && BCK_GND != 7)
	 HIGHLIGHT = 15;
       else
	 HIGHLIGHT = 4;
       init_icon();
       display_icon(0, IY);
       display_grid ();
       break;
     case 64: FORE_GND =  change_colors();
       if (draw)
         pen = FORE_GND;
       break;

      }  /* end switch */

      if (BCK_GND != 0) {
	if (x < IX) x++;  if (x > IX+XDIM-1) x--;
	if (y < IY) y++;  if (y > IY+YDIM-1) y--;
	}
      else {
	if (x < IX+1) x++;  if (x > IX+XDIM-2) x--;
	if (y < IY+1) y++;  if (y > IY+YDIM-2) y--;
	}

  } while (ch != 68);
}

void display_icon(int startx, int starty)
{
  register int x, y;

  for (y = starty; y<starty+YDIM; y++)
    for(x = startx; x<startx+XDIM; x++)
      putpoint (x, y, icon.image[x-startx][y-starty], 0);
}

void init_icon()
{
  register int x, y;

  for (x = 0; x<XDIM; x++){
    for (y=0; y<YDIM; y++)
      icon.image[x][y] = BCK_GND;
  }

  if (BCK_GND == 0) {

    for (x=0; x < XDIM; x++)
      icon.image[x][YDIM-1] = 15;
    for (y=0; y < YDIM; y++)
      icon.image[XDIM-1][y] = 15;
    for (x=0; x < XDIM; x++)
      icon.image[x][0] = 15;
    for (y=0; y < YDIM; y++)
      icon.image[0][y] = 15;
  }
}

save_icon(void)
{
  FILE *fp;
  char fname[80];
  int result;
  gotoxy(0, 20);
  printf("save to: ");
  gets(fname);

  if((fp = fopen(fname, "wb"))==NULL) {
     clr_eol (9, 20);
     gotoxy (9, 20);
     printf(strerror(errno));
     sleep (2);
     clr_eol (0, 20);
     return 0;  }

  fwrite(&icon , sizeof icon, 1, fp);

  if(ferror(fp)) result = 0;
  else result = 1;
  fclose(fp);
  clr_eol (0, 20);
  return result;
}

load_icon(char *name)
{
  FILE *fp;
  char fname[80];
  int result;

  if (!*name) {
    gotoxy(0, 20);
    printf("load from: ");
    gets(fname); }
  else strcpy(fname, name);

  if ((fp = fopen(fname, "rb"))==NULL) {
    clr_eol (11, 20);
    gotoxy (11, 20);
    printf (strerror(errno));
    sleep (2);
    clr_eol (0, 20);
    return 0;  }

  fread(&icon, sizeof icon, 1, fp);

  if (ferror(fp)) result = 0;
  else result = 1;
  fclose(fp);
  clr_eol (0, 20);
  return result;
}

char getkey(void)
{
  union key {
    int i;
    char ch[2];
  } k;

  k.i = bioskey(0);
  return k.ch[1];
}

void mode(int mode_code)
{
  union REGS r;
  r.h.al = mode_code;
  r.h.ah = 0;
  int86(0x10, &r, &r);
}

char getpoint(int x, int y)
{
  union REGS r;

  r.h.ah = 13;
  r.x.dx = y;
  r.x.cx = x;
  r.h.bh = 0;
  int86(16, &r, &r);
  return r.h.al;
}

#define ENABLE 0x0F
#define INDEXREG 0x3CE
#define VALREG 0x3CF

#define OUTINDEX(index, val) outp((INDEXREG), (index));\
                    outp((VALREG), (val));
#define WIDTH 80L

/* these range values assume EGA mode 16 */
#define XMAX 639
#define YMAX 349
#define XMIN 0
#define YMIN 0

void putpoint(int x, int y, int color, int how)
{
  register unsigned char mask = 0x80;
  register char far *base;
  unsigned dummy;

  /* range checking   Slows program  */
  /* if (x<XMIN || x>XMAX || y<YMIN || Y>YMAX) return; */

  base = (char far *) (egabase + ((long) y * WIDTH + (long) x/8L ));

  mask >>= x % 8;

  /* this causes the memory READ necessary to load the EGA/VGA's internal
  registers */

  dummy = *base;

  OUTINDEX(0, color);
  OUTINDEX(1, ENABLE);
  OUTINDEX(3, how);
  OUTINDEX(8, mask);

  *base = 1;

  OUTINDEX(0, 0);
  OUTINDEX(1, 0);
  OUTINDEX(3, 0);
  OUTINDEX(8, 0xff);
}

void gotoxy(int x, int y)
{
  union REGS r;

  r.h.ah = 2;
  r.h.dl = x;
  r.h.dh = y;
  r.h.bh = 0;
  int86(0x10, &r, &r);
}

void clr_eol(int x, int y)
{
  for (; x<80; x++) {
    gotoxy(x, y);
    printf(" "); }
}

int change_colors()
{
  register int x = 0;
  register int y;
  int color = 0;
  int i = 0;
  char choice[80];
  int xpos, ypos, button;


  hide_cursor ();
  while (x < 639 && color < 16){
   for (y=321; y<= 335; y++){
    for (x = i; x < (i + 40); x++) {
      putpoint (x, y, color, 0); }}
    i += 40;
    color ++; }
   box(0, 38, 322, 335, 0x18, 15);
   gotoxy (0,22);
   printf ("  1    2    3    4    5    6    7    ");
   printf ("8    9    10   11   12   13   14   15   16 ");
   do{
      gotoxy (0, 20);
      printf ("enter # of choice: ");

      while (!kbhit ()) {
	 show_cursor ();
	 get_status(&button, &xpos, &ypos);
	 gotoxy(70,5);
	 printf("%3d:%3d", xpos, ypos);

	 if (button == 1){
	    if (ypos <= 335 && ypos >= 321) {
	      if (xpos >= 0 && xpos <= 38) {
		 choice[0] = '1';
		 goto mouse_end; }
	      if (xpos >= 39 && xpos <= 78) {
		 choice[0] = '2';
		 goto mouse_end; }
	      if (xpos >= 79 && xpos <= 118) {
		 choice[0] = '3';
		 goto mouse_end; }
	      if (xpos >= 119 && xpos <= 158) {
		 choice[0] = '4';
		 goto mouse_end; }
	      if (xpos >= 159 && xpos <= 198) {
		 choice[0] = '5';
		 goto mouse_end; }
	      if (xpos >= 199 && xpos <= 238) {
		 choice[0] = '6';
		 goto mouse_end; }
	      if (xpos >= 239 && xpos <= 278) {
		 choice[0] = '7';
		 goto mouse_end; }
	      if (xpos >= 279 && xpos <= 318) {
		 choice[0] = '8';
		 goto mouse_end; }
	      if (xpos >= 319 && xpos <= 358) {
		 choice[0] = '9';
		 goto mouse_end; }
	      if (xpos >= 359 && xpos <= 398) {
		 choice[0] = '1';
		 choice[1] = '0';
		 goto mouse_end; }
	      if (xpos >= 399 && xpos <= 438) {
		 choice[0] = '1';
		 choice[1] = '1';
		 goto mouse_end; }
	      if (xpos >= 439 && xpos <= 478) {
		 choice[0] = '1';
		 choice[1] = '2';
		 goto mouse_end; }
	      if (xpos >= 479 && xpos <= 518) {
		 choice[0] = '1';
		 choice[1] = '3';
		 goto mouse_end; }
	      if (xpos >= 519 && xpos <= 558) {
		 choice[0] = '1';
		 choice[1] = '4';
		 goto mouse_end; }
	      if (xpos >= 559 && xpos <= 598) {
		 choice[0] = '1';
		 choice[1] = '5';
		 goto mouse_end; }
	      if (xpos >= 599) {
		 choice[0] = '1';
		 choice[1] = '6';
		 goto mouse_end; }

	     }
	  }
      }

      gotoxy (19, 20);
      gets (choice);
   } while (atoi(choice) < 1 || atoi(choice) > 16);

mouse_end:
   hide_cursor ();
   clr_eol (0, 20);
   clr_eol (0, 22);
   clr_eol (0, 23);
   status_line ();


   return (atoi(choice) - 1);
}
void opening_screen (int argc)
{
  char str[80];
  char choice;

  printf ("F1: draw    F2: erase/move    F3: save icon    ");
  printf ("F4: load icon    F5: erase icon");
  if (argc != 2){
      gotoxy (31, 12);
      printf ("Canvas Color ?");
      BCK_GND = change_colors ();
      clr_eol (30, 12);

      do {
	gotoxy (30, 12);
	printf ("Size of Icon ?");
	gotoxy (24, 14);
	printf ("[S]mall, [M]edium, or [L]arge: ");
	gets (str);
	choice = toupper (str[0]);
	clr_eol (30, 12);
	clr_eol (24, 14);
      } while (choice != 'S' && choice != 'M' && choice != 'L');

      if (choice == 'S') {
	 icon.XDim = XDIM = 40;
	 icon.YDim = YDIM = 32;
	 IY = 144;
	 IIY = 36; }
      if (choice == 'M') {
	 icon.XDim = XDIM = 50;
	 icon.YDim = YDIM = 42;
	 IY = 100;
	 IIY = 25; }
      if (choice == 'L') {
	 icon.XDim = XDIM = 60;
	 icon.YDim = YDIM = 52;
	 IY = 72;
	 IIY = 18; }
   }

  if (argc == 2)
  status_line ();
  gotoxy (30, 3);
  printf ("status : erase/move");
  show_cursor ();

}

void status_line(void)
{
  gotoxy (0,23);
  printf ("F6: colors      F7: (reserved)   F8: (reserved)     F9: (reser");
  printf ("ved)   F10: quit");
}

void box(int startx, int endx, int starty, int endy, int how, int color)
{
  register int x, y;


  for (x=startx; x<endx; x++)
    putpoint(x, endy, color, how);
  for (y=starty; y<endy; y++)
    putpoint(endx, y, color, how);
  for (x=startx; x<endx; x++)
    putpoint(x, starty, color, how);
  for (y=starty; y<endy; y++)
    putpoint(startx, y, color, how);
}

int rightb_pressed(void)
{
  int button, xpos, ypos;

  get_status(&button, &xpos, &ypos);

  if (button == 2)
    return TRUE;
  else
    return FALSE;
}

int leftb_pressed(void)
{
  int button, xpos, ypos;

  get_status(&button, &xpos, &ypos);

  if (button == 1)
    return TRUE;
  else
    return FALSE;
}

void mouse_motion(int *deltax, int *deltay)
{
  int  xmove, ymove;

  motion(&xmove, &ymove);

  if(xmove > 0) *deltax = RIGHT;
  else if (xmove < 0) *deltay = LEFT;
  else *deltax = NOT_MOVED;

  if(ymove > 0) *deltay = DOWN;
  else if(ymove < 0) *deltay = UP;
  else *deltay = NOT_MOVED;
}















