/* -*-mode: C; fill-column: 77; c-basic-offset: 4; -*- */
/* 
 * Copyright 2001 by Eric House.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */

#include <PalmTypes.h>
#include <Event.h>
#include <Form.h>
#include <UIControls.h>

#include "../callback.h"
/* #include "palmmain.h" */
/* #include "palmutil.h" */
/* #include "xwords4defines.h" */

#include "xwconfig.h"
#include "xwconfigdefines.h"
#include "palmutil.h"


extern ColorSelState gColorSelState;
extern Boolean gColorsDirty;
extern unsigned char* gBoardResPtr;

static void
fillGadgetWithColor( DrawingPrefs* prefs, UInt16 index )
{
    XP_U16 colorIndex = COLOR_PLAYER1 + index - PLAYER_1_GADGET;
    IndexedColorType ict = prefs->drawColors[colorIndex];
    RectangleType divRect;

    getObjectBounds( index, &divRect );
    WinDrawRectangleFrame( rectangleFrame, &divRect );
    WinSetBackColor( ict );
    WinEraseRectangle( &divRect, 0 );
} /* fillGadgetWithColor */

static void
drawColors( DrawingPrefs* prefs, UInt16 which )
{
    UInt16 index;

    WinPushDrawState();

    if ( which == 0 ) {
	for ( index = PLAYER_1_GADGET; index <= TILE_BACK_GADGET;
	      ++index ) {
	    fillGadgetWithColor( prefs, index );
	}
    } else {
	fillGadgetWithColor( prefs, which );
    }

    WinPopDrawState();
} /* drawColors */

static Boolean
handleColorPick( DrawingPrefs* prefs, UInt16 selGadget )
{
    Boolean result;
    XP_U16 colorIndex = COLOR_PLAYER1 + selGadget - PLAYER_1_GADGET;
    IndexedColorType ict = prefs->drawColors[colorIndex];
    
    result = UIPickColor( &ict, NULL, UIPickColorStartPalette, NULL, NULL );

    if ( result ) {
	prefs->drawColors[colorIndex] = ict;	
    }

    return result;
} /* handleColorPick */

Boolean
colorPrefHandleEvent( EventPtr event )
{
    Boolean result = true;
    FormPtr form;
    UInt16 selGadget;

    CALLBACK_PROLOGUE();

    switch (event->eType) {
    
    case frmOpenEvent:
/* 	ASSERT( !colorSelState ); */
/* 	colorSelState = XP_MALLOC( globals->mpool,  */
/* 				   sizeof(*colorSelState) ); */
/* 	XP_MEMSET( colorSelState, 0, sizeof(*colorSelState) ); */
/* 	XP_MEMCPY( &colorSelState->drawingPrefs,  */
/* 		   &globals->gState.drawingPrefs, */
/* 		   sizeof(colorSelState->drawingPrefs) ); */

    case frmUpdateEvent:
	form = FrmGetActiveForm();
	FrmDrawForm( form );
	drawColors( &gColorSelState.drawingPrefs, 0 );
	break;

    case penDownEvent:
	result = penInGadget( event, &selGadget );
	if ( result ) {
	    gColorSelState.selGadget = selGadget;
	}
	break;

    case penUpEvent:
	result = penInGadget( event, &selGadget );
	if ( result && selGadget == gColorSelState.selGadget ) {
	    if ( handleColorPick( &gColorSelState.drawingPrefs, 
				  selGadget ) ) {
		gColorsDirty = true;
		drawColors( &gColorSelState.drawingPrefs, selGadget );
	    }
	}
	gColorSelState.selGadget = 0;
	break;

/*     case ctlSelectEvent: */
/* 	result = true; */
/* 	switch ( event->data.ctlEnter.controlID ) { */

/* 	case XWC_COLORS_OK_BUTTON_ID: */

/* 	    MemCopy( &globals->gState.drawingPrefs,  */
/* 		     &colorSelState->drawingPrefs,  */
/* 		     sizeof(colorSelState->drawingPrefs) ); */
/* 	    if ( !!globals->board ) { */
/* 		board_invalAll( globals->board ); */

/* 		eventToPost.eType = boardRedrawEvt; */
/* 		EvtAddEventToQueue( &eventToPost ); */
/* 	    } */
/* 	case XWC_COLORS_CANCEL_BUTTON_ID: */
/* 	    FREE( globals->mpool, colorSelState ); */
/* 	    FrmReturnToForm( 0 ); */
/* 	    break; */
/* 	} */
/* 	break; */
    case menuEvent:
	result = handleMenu( event->data.menu.itemID );
	break;

    default:
	result = false;
    }

    CALLBACK_EPILOGUE();
    return result;
} /* colorPrefHandleEvent */
