#include "cp.h"

LONG listcode = -1;

BOOL sel = FALSE;

int addClicked( void )
{
     /* routine when gadget "_Add" is clicked. */
     if (AddNewSet())
       {
          DrawView ( FALSE );
       }
     return ( 1 );
}

int newfile( void )
{
     /* routine when "Open File" is selected from menu. */
     FreeAllSets();
     AddNewSet();
     DrawView ( TRUE );
     return ( 1 );
}

int PlotDump( void )
{
     /* routine when "Print" is selected from menu. */
     Dump ();
     return ( 1 );
}

int deleteClicked( void )
{
     /* routine when gadget "_Kill" is clicked. */

     if ( OpenGTXWindow( OpendelwinWindow ))
       {
          Death(20);
       }

     SetPointer( delwinWnd, ROBOTPointer, ROBOTHEIGHT, ROBOTWIDTH, ROBOTXOFF , ROBOTYOFF);    /* Set pointer to Robot */


     sel = FALSE;
     GT_SetGadgetAttrs(delwinGadgets[0],delwinWnd,NULL,GTLV_Labels,SetList,TAG_DONE);

        do {
               Wait ( 1L << delwinWnd-> UserPort-> mp_SigBit );
             }
          while ( HandledelwinIDCMP() );

     ClosedelwinWindow();
     return (1);
}


int fullClicked( void )
{
     /* routine when gadget "_Full" is clicked. */
     DrawView( TRUE );
     return(1);
}

int zoomClicked( void )
{
     /* routine when gadget "_Zoom" is clicked. */

     SetPointer( PlotWindowWnd, XPPointer, XPHEIGHT, XPWIDTH, XPXOFF , XPYOFF);    /* Set pointer cross for zoom */

     Zoom();

     return (1);
}

int gridClicked( void )
{
     /* routine when gadget "_Grid" is clicked. */
     Grid();
     if ( grid == TRUE ) grid = FALSE;
     else grid = TRUE;
     return (1);
}

int quitClicked( void )
{
     /* routine when gadget "_Quit" is clicked. */
     return (0);
}

int PreOpenRequest( void )
{
/* menu selection to set precision */

     if ( OpenGTXWindow( OpenPreSlideWindow ))
       {
          Death(20);
       }

        do {
               Wait ( 1L << PreSlideWnd-> UserPort-> mp_SigBit );
             }
          while ( HandlePreSlideIDCMP() );

	ClosePreSlideWindow();
	return (1);
}

int UnZoom( void )
{
double txmax;
double txmin;
double tymax;
double tymin;

	txmax = cv.xmax;
	txmin = cv.xmin;
	tymax = cv.ymax;
	tymin = cv.ymin;

	/* restore unzoom settings */
     cv.xmin   = ov.xmin;
     cv.xmax   = ov.xmax;
	cv.ymin   = ov.ymin;
     cv.ymax   = ov.ymax;
    	cv.xdelta = ov.xdelta;
  	cv.ydelta = ov.ydelta;

	ov.xmax = txmax;
	ov.xmin = txmin;
	ov.ymax = tymax;
	ov.ymin = tymin;

	DrawView( FALSE );
     return (1);
}


int PlotWindowItem0( void )
{

struct EasyStruct ES =
   {
     sizeof(struct EasyStruct),
     0,
     "SAIC",
     "%s",
     "It's Freeware!"
    };
     EasyRequest (PlotWindowWnd,&ES,NULL,"cP\nby\nChris Conger\nand\nSAIC\nan employee owned company");
     return (1);
}

int PlotWindowItem1( void )
{
     return (0);
}

int PlotWindowItem2( void )
{
     LOGX = FALSE;
     DrawView( TRUE );
     return (1);
}

int PlotWindowItem3( void )
{
     LOGX = TRUE;
     DrawView( TRUE );
     return (1);
}

int PlotWindowItem4( void )
{
     LOGY = FALSE;
     DrawView( TRUE );
     return (1);
}

int PlotWindowItem5( void )
{
     LOGY = TRUE;
     DrawView( TRUE );
     return (1);
}

int PlotWindowItem6( void )
{
     if( sym == TRUE ) 
       {
		sym = FALSE;
     	DrawView ( FALSE );
       }
     else
       {
          sym = TRUE;
		SymAllSets();
	  }
     return ( 1 );
}

int PlotWindowItem7( void )
{

     if( CPANEL == TRUE )
       {
          CPANEL = FALSE;
          RemoveGList( PlotWindowWnd, PlotWindowGList, PlotWindow_CNT+1 );
       }
     else
       {
          AddGList( PlotWindowWnd,PlotWindowGList, NULL, PlotWindow_CNT+1, NULL );
          CPANEL = TRUE;
       }
     DrawView ( FALSE );
     return ( 1 );
}

int NewDepth(WORD d)
{
    if ( delwinWnd )     ClosedelwinWindow();
    if ( PlotWindowWnd ) ClosePlotWindowWindow();
    if ( Scr )           CloseDownScreen();

     depth = d;

     if (d==1) { MONO = TRUE; lastcolor=1;}
     if (d==2) { MONO = FALSE; lastcolor=3;}
     if (d==3) { MONO = FALSE; lastcolor=7;}
     if (d==4) { MONO = FALSE; lastcolor=15;}
     
     if ( OpenGTXScreen()) Death(10);

     if ( OpenGTXWindow( OpenPlotWindowWindow )) Death(10);

	DrawView (FALSE);
     
  return ( 1 );    
}

int NewDepth1()
{
    return (10);
}
int NewDepth2()
{
    return (20);
}
int NewDepth3()
{
    return (30);
}
int NewDepth4()
{
    return (40);
}


int PlotWindowVanillaKey( void )
{
     switch (PlotWindowMsg.Code)
       {
          case 'a':
          case 'A':
               return( addClicked() );
          case 'k':
          case 'K':
               return( deleteClicked() );
          case 'z':
          case 'Z':
               return( zoomClicked() );
          case 'f':
          case 'F':
               return( fullClicked() );
          case 'g':
          case 'G':
               return( gridClicked() );
          case 'h':
          case 'H':
               return( CrossHair() );
          case 'r':
          case 'R':
		     DrawView( FALSE );
               return( 1 );
          case 'q':
          case 'Q':
               return ( quitClicked() );
          case 's':
          case 'S':
               return ( PlotWindowItem6() );
          case 'u':
          case 'U':
			return (UnZoom());
          case 'd':
          case 'D':
               return (PreOpenRequest());
          case 'x':
          case 'X':
		     if(LOGX) LOGX = FALSE;
		     else LOGX = TRUE;
     		DrawView( TRUE );
		     return (1);
          case 'y':
          case 'Y':
		     if(LOGY) LOGY = FALSE;
		     else LOGY = TRUE;
     		DrawView( TRUE );
		     return (1);
          default:
               return (1);
       }
}

int PlotWindowRawKey( void )
{
struct Rectangle rect;

	if (QueryOverscan(Disp, &rect, OSCAN_TEXT))
	  {

	     switch (PlotWindowMsg.Code)
     	  {
          	case 76:
				MoveScreen(Scr,0,min((rect.MaxY-rect.MinY), -(Scr->ViewPort.DyOffset)));
				return(1);
          	case 77:
				MoveScreen(Scr,0,rect.MinY-rect.MaxY);
				return(1);
     	     case 78:
				MoveScreen(Scr,(rect.MinX-rect.MaxX), 0);
				return(1);
	          case 79:
				MoveScreen(Scr,(rect.MaxX-rect.MinX), 0);
				return(1);
	          default:
     	          return (1);
       	  }
       }
     return(1);
}


int delwinVanillaKey(void)
{
     switch (delwinMsg.Code)
       {
          case 'r':
          case 'R':
               return( canlistClicked() );

          case 'k':
          case 'K':
               return ( delsetClicked());

		case 13 : /* hit enter */
			delsetClicked();
			return ( 0 );

		case 27 : /* hit escape */
			return ( 0 );

          default:
    	          return (1);
       }
}

int delwinRawKey(void)
{
struct Set *node;
WORD k=0;

	node = (struct Set *)SetList-> lh_Head;

     while ( node-> snode.ln_Succ ) { node = (struct Set*)node-> snode.ln_Succ; k++; }

     switch (delwinMsg.Code)
       {

          case 76:     /* up arrow key */
          	
          	listcode = max(listcode-1,0);
			sel = TRUE;
			GT_SetGadgetAttrs(delwinGadgets[0],delwinWnd,NULL,GTLV_Selected,(UWORD)listcode,TAG_DONE);
			return (1);

          case 77:    /* down arrow key */
          	
	          listcode = min(listcode+1,k-1);
    	     	sel = TRUE;
		     GT_SetGadgetAttrs(delwinGadgets[0],delwinWnd,NULL,GTLV_Selected,(UWORD)listcode,TAG_DONE);
			return(1);
       }
     return (1);
}

int dellistClicked( void )
{
static ULONG osec=0, omsec=0;
ULONG csec, cmsec;
static LONG olcode=100;

	CurrentTime( &csec, &cmsec );

     sel = TRUE;
     listcode = delwinMsg.Code;

	if ( DoubleClick ( osec, omsec, csec, cmsec ))
	  {
		if ( olcode == listcode )
		  {
		  	delsetClicked ();
		  	return( 1 );
		  }
	  }

	else
	  {
	  	osec   = csec;
	  	omsec  = cmsec;
	  	olcode = listcode;
	  }

     return ( 1 );
}

int delsetClicked( void )
{
struct Set *node;
WORD k;
UBYTE str[132];
	
     if ( sel )
       {
          node = (struct Set *)SetList-> lh_Head;
          k = 0;

          while ( node && k < listcode )
            {
               node = (struct Set *)node-> snode.ln_Succ;
               k++;
            }

          GT_SetGadgetAttrs(delwinGadgets[0],delwinWnd,NULL,GTLV_Labels,~0,TAG_DONE);


          if ( ! IsListEmpty(SetList))
            {
			strcpy(str,"Freeing -> ");
			strcat(str,node-> snode.ln_Name);
	          SetWindowTitles( delwinWnd, str, str );
			strcpy(str,"I'm Gone -> ");
			strcat(str,node-> snode.ln_Name);
          	FreePoints( node -> FirstPoint);
	          Remove( (struct Node *)node );
     	     FreeMem( node, sizeof(struct Set));
     	  }

          SetWindowTitles( delwinWnd, str , MyName );

          GT_SetGadgetAttrs(delwinGadgets[0],delwinWnd,NULL,GTLV_Labels,SetList,TAG_DONE);
		--listcode;
          GT_SetGadgetAttrs(delwinGadgets[0],delwinWnd,NULL,GTLV_Selected,(UWORD)max(listcode,0),TAG_DONE);
       }
     return ( 1 );
}

int canlistClicked( void )
{
     DrawView( FALSE );
     return ( 0 );
}

int delwinCloseWindow( void )
{
     return ( 0 );
}

int preClicked( void )
{
     precision = PreSlideMsg.Code - 1;
	return (1);
}

int PreSlideCloseWindow( void )
{
	return ( 0 );
}
