/*
 *  Source generated with GadToolsBox V1.3
 *  which is (c) Copyright 1991,92 Jaba Development
 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/graphics.h>
#include <string.h>

#include "display.h"

#define SCR_WIDTH	640
#define SCR_HEIGHT	200

struct Screen        *Scr = NULL;
APTR                  VisualInfo = NULL;
struct Window        *ChessWnd = NULL;
struct Menu          *ChessMenus = NULL;
UWORD                 ChessLeft = 0;
UWORD                 ChessTop = 0;
UWORD                 ChessWidth = SCR_WIDTH;
UWORD                 ChessHeight = SCR_HEIGHT;
UBYTE                *ChessWdt = NULL;

struct TextAttr topaz8 = {
    ( STRPTR )"topaz.font", 8, 0x00, 0x00 };


struct NewMenu ChessNewMenu[] = {
    NM_TITLE, (STRPTR)"Project", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"New game", (STRPTR)"N", 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Load game...", (STRPTR)"L", 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Save game...", (STRPTR)"S", 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Create listing", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Edit board", (STRPTR)"E", 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Help...", "?", 0, 0L, NULL,
    NM_ITEM, (STRPTR)"About...", (STRPTR)"A", 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Quit", (STRPTR)"Q", 0, 0L, NULL,
    NM_TITLE, (STRPTR)"Settings", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Select level", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Change colors", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Save colors", NULL, 0, 0L, NULL,
    NM_TITLE,(STRPTR)"Toggles", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Reverse board", NULL, CHECKIT|MENUTOGGLE, 0L, NULL,
    NM_ITEM, (STRPTR)"Coordinates", NULL, CHECKIT|MENUTOGGLE, 0L, NULL,
    NM_ITEM, (STRPTR)"Show thinking", NULL, CHECKIT|MENUTOGGLE, 0L, NULL,
    NM_ITEM, (STRPTR)"Random play", NULL, CHECKIT|MENUTOGGLE, 0L, NULL,
    NM_ITEM, (STRPTR)"Beep after move", NULL, CHECKIT|MENUTOGGLE, 0L, NULL,
/*    NM_ITEM, (STRPTR)"Use opening book", NULL, CHECKIT|MENUTOGGLE, 0L, NULL,*/
    NM_TITLE,(STRPTR)"Move", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Go!", (STRPTR)"G", 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Undo half-move", (STRPTR)"U", 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Undo whole-move", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Switch sides", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Input both sides", NULL, 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Redraw board", (STRPTR)"R", 0, 0L, NULL,
    NM_ITEM, (STRPTR)"Hint", (STRPTR)"H", 0, 0L, NULL,
    NM_END, NULL, NULL, 0, 0L, NULL };

struct ColorSpec  ScreenColors[] = {
     0, 0x0B, 0x0A, 0x09,
     1, 0x09, 0x07, 0x03,
     2, 0x0E, 0x0C, 0x09,
     3, 0x00, 0x00, 0x00,
     4, 0x08, 0x0B, 0x05,
     5, 0x0E, 0x0E, 0x0C,
     6, 0x00, 0x06, 0x07,
     7, 0x0B, 0x05, 0x07,
    ~0, 0x00, 0x00, 0x00 };

UWORD             DriPens[] = {
    0,3,1,5,3,7,3,0,7,~0 };

void LoadColors(char *name)
{
	FILE *f;
	UWORD i;
	int r, g, b;

	if (f = fopen(name, "r"))
	{
		for (i = 0; i < 8; i++)
			if (fscanf(f, "%x %x %x", &r, &g, &b) == 3)
			{
				ScreenColors[i].Red = r;
				ScreenColors[i].Green = g;
				ScreenColors[i].Blue = b;
			}
			else break;

		fclose(f);
	}
}

BOOL SaveColors(char *name)
{
	FILE *fp;
	struct ColorMap *cm;
	LONG colorval;
	UWORD i, r,g,b;

	if (fp = fopen(name, "w"))
	{
		cm = Scr->ViewPort.ColorMap;
		for (i = 0; i < 8; i++)
		{
			colorval = GetRGB4(cm, i);
			r = colorval >> 8;
			g = colorval >> 4 & 0xF;
			b = colorval & 0xF;
			fprintf(fp, "0x%x 0x%x 0x%x\n", r, g, b);
		}
		fclose(fp);
		return TRUE;
	}
	return FALSE;
}

int SetupScreen( void )
{
    if ( ! ( Scr = OpenScreenTags( NULL,  SA_Left,          0,
                                          SA_Top,           0,
                                          SA_Width,         SCR_WIDTH,
                                          SA_Height,        SCR_HEIGHT,
                                          SA_Depth,         3,
                                          SA_Colors,        &ScreenColors[0],
                                          SA_Font,          &topaz8,
                                          SA_Type,          CUSTOMSCREEN,
                                          SA_DisplayID,     HIRES_KEY,
                                          SA_Pens,          &DriPens[0],
                                          SA_Title,	    
"Amiga GnuChess v1.0  (c) 1986-92 F.S.F, 1992 M.W.Scott",
					  SA_ShowTitle,	    TRUE,
                                          TAG_DONE )))
        return( 1L );

    if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
        return( 2L );

    return( 0L );
}

void CloseDownScreen( void )
{
    if ( VisualInfo ) {
        FreeVisualInfo( VisualInfo );
        VisualInfo = NULL;
    }

    if ( Scr        ) {
        CloseScreen( Scr );
        Scr = NULL;
    }
}

int OpenChessWindow( void )
{
    struct NewGadget     ng;
    struct Gadget       *g;
    UWORD               offx, offy;

    offx = Scr->WBorLeft;
    offy = Scr->WBorTop + Scr->RastPort.TxHeight + 1;

    if ( ! ( ChessMenus = CreateMenus( ChessNewMenu, GTMN_FrontPen, 0L, TAG_DONE )))
        return( 3L );

    LayoutMenus( ChessMenus, VisualInfo, GTMN_TextAttr, &topaz8, TAG_DONE );

    if ( ! ( ChessWnd = OpenWindowTags( NULL,
                    WA_Left,          ChessLeft,
                    WA_Top,           ChessTop = offy,
                    WA_Width,         ChessWidth,
                    WA_Height,        ChessHeight - offy,
                    WA_Flags,         WFLG_ACTIVATE|WFLG_SMART_REFRESH|WFLG_BORDERLESS,
                    WA_Title,         ChessWdt,
		    WA_IDCMP,	      IDCMP_VANILLAKEY,
                    WA_CustomScreen,  Scr,
                    TAG_DONE )))
        return( 4L );

/*    SetMenuStrip( ChessWnd, ChessMenus );*/
    GT_RefreshWindow( ChessWnd, NULL );

    return( 0L );
}

void CloseChessWindow( void )
{
    if ( ChessMenus      ) {
        ClearMenuStrip( ChessWnd );
        FreeMenus( ChessMenus );
        ChessMenus = NULL;    }

    if ( ChessWnd        ) {
        CloseWindow( ChessWnd );
        ChessWnd = NULL;
    }
}

