/***************************************************************************

  M.A.M.E.32  -  Multiple Arcade Machine Emulator for Win32
  Win32 Portions Copyright (C) 1997-2001 Michael Soderstrom and Chris Kirmse

  This file is part of MAME32, and may only be used, modified and
  distributed under the terms of the MAME license, in "readme.txt".
  By continuing to use, modify or distribute this file you indicate
  that you have read the license and understand and accept it fully.

***************************************************************************/
 
/***************************************************************************

  PaletteEdit.c

***************************************************************************/

#define WIN32_LEAN_AND_MEAN
#define NONAMELESSUNION 1
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#undef NONAMELESSUNION

#include <driver.h>
#include "options.h"
#ifdef BUILD_ENGLISH
#include "resrce.h"
#else
#include "resrcj.h"
#endif

#include "win32text.h"
#include "video.h"

/***************************************************************
 * Imported function prototypes
 ***************************************************************/

/**************************************************************
 * Local function prototypes
 **************************************************************/

static void InitializePaletteUI(HWND hwnd);
static void OptOnHScroll(HWND hwnd, HWND hwndCtl, UINT code, int pos);
static void PaletteSave(void);
static void PaletteView(HWND hwnd);
static void PaletteChange(HWND hwnd);
static void PaletteSet(HWND hwnd);

/**************************************************************
 * Local private variables
 **************************************************************/

static int palette_tmp[64][3];
static int palette_rgb[3];
static int palette_num = 0;

/***************************************************************
 * Public functions
 ***************************************************************/

INT_PTR CALLBACK PaletteDialogProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
	case WM_INITDIALOG :
		InitializePaletteUI(hDlg);
		return TRUE;

	case WM_HSCROLL :
		/* slider changed */
		HANDLE_WM_HSCROLL(hDlg, wParam, lParam, OptOnHScroll);
		break;

	case WM_COMMAND :
		switch (GET_WM_COMMAND_ID(wParam, lParam))
		{
		case IDC_PALETTE_COMBO :
			if (GET_WM_COMMAND_CMD(wParam, lParam) == CBN_SELCHANGE)
				PaletteChange(hDlg);
			break;
		case IDOK :
			PaletteSave();
		case IDCANCEL :
			EndDialog(hDlg, 0);
			return TRUE;
		}
		break;
	}

	PaletteView(hDlg);

	return 0;
}

/*********************************************************************
 * Local Functions
 *********************************************************************/

/* Initialize the palette options */
static void InitializePaletteUI(HWND hwnd)
{
	int i, j;
	HWND hCtrl = GetDlgItem(hwnd, IDC_PALETTE_COMBO);

	if (hCtrl)
	{
		for (i = 0; i < MAX_COLORTABLE; i++)
			ComboBox_AddString(hCtrl, win32_getstring(WIN32_palette_font_blank + i));
	}

	ComboBox_SetCurSel(GetDlgItem(hwnd, IDC_PALETTE_COMBO), 0);

	for (i = 0; i < MAX_COLORTABLE; i++)
		for (j = 0; j < 3; j++)
			palette_tmp[i][j] = GetPalette(i, j);

	SendMessage(GetDlgItem(hwnd, IDC_PALETTE_R), TBM_SETRANGE,
				(WPARAM)FALSE,
				(LPARAM)MAKELONG(0, 255));

	SendMessage(GetDlgItem(hwnd, IDC_PALETTE_G), TBM_SETRANGE,
				(WPARAM)FALSE,
				(LPARAM)MAKELONG(0, 255));

	SendMessage(GetDlgItem(hwnd, IDC_PALETTE_B), TBM_SETRANGE,
				(WPARAM)FALSE,
				(LPARAM)MAKELONG(0, 255));

	PaletteSet(hwnd);
}

static void OptOnHScroll(HWND hwnd, HWND hwndCtl, UINT code, int pos)
{
	char   buf[100];
	UINT   nValue;

	if (hwndCtl == GetDlgItem(hwnd, IDC_PALETTE_R))
	{
		/* Get the current value of the control */
		nValue = SendMessage(GetDlgItem(hwnd, IDC_PALETTE_R), TBM_GETPOS, 0, 0);
		palette_rgb[0] = nValue;

		/* Set the static display to the new value */
		sprintf(buf, "%d", nValue);
		Static_SetText(GetDlgItem(hwnd, IDC_PALETTE_TEXTR), buf);
	}
	else if (hwndCtl == GetDlgItem(hwnd, IDC_PALETTE_G))
	{
		/* Get the current value of the control */
		nValue = SendMessage(GetDlgItem(hwnd, IDC_PALETTE_G), TBM_GETPOS, 0, 0);
		palette_rgb[1] = nValue;

		/* Set the static display to the new value */
		sprintf(buf, "%d", nValue);
		Static_SetText(GetDlgItem(hwnd, IDC_PALETTE_TEXTG), buf);
	}
	else	// (IDC_PALETTE_B)
	{
		/* Get the current value of the control */
		nValue = SendMessage(GetDlgItem(hwnd, IDC_PALETTE_B), TBM_GETPOS, 0, 0);
		palette_rgb[2] = nValue;

		/* Set the static display to the new value */
		sprintf(buf, "%d", nValue);
		Static_SetText(GetDlgItem(hwnd, IDC_PALETTE_TEXTB), buf);
	}
}

static void PaletteSave(void)
{
	int i, j;

	for (i = 0; i < 3; i++)
		palette_tmp[palette_num][i] = palette_rgb[i];

	for (i = 0; i < MAX_COLORTABLE; i++)
		for (j = 0; j < 3; j++)
			SetPalette(i, j, palette_tmp[i][j]);
}

static void PaletteView(HWND hwnd)
{
	HWND hWnd = GetDlgItem(hwnd, IDC_PALETTE_VIEW);
	HDC  hDC  = GetDC(hWnd);
	HPEN hPen, hOldPen;
	RECT rt;

	if (hWnd == NULL)
		return;

	GetClientRect(hWnd, &rt);
	hPen = CreatePen(PS_INSIDEFRAME, 100, RGB(palette_rgb[0], palette_rgb[1], palette_rgb[2]));
	hOldPen = SelectObject(hDC, hPen);
	SelectObject(hDC, hPen);
	Rectangle(hDC, rt.left, rt.top, rt.right, rt.bottom);
	SelectObject(hDC, hOldPen);
	DeleteObject(hPen);
	ReleaseDC(hWnd, hDC);
}

static void PaletteChange(HWND hwnd)
{
	int i;

	for (i = 0; i < 3; i++)
		palette_tmp[palette_num][i] = palette_rgb[i];

	PaletteSet(hwnd);
}

static void PaletteSet(HWND hwnd)
{
	char buf[100];
	int i;

	palette_num = ComboBox_GetCurSel(GetDlgItem(hwnd, IDC_PALETTE_COMBO));

	for (i = 0; i < 3; i++)
		palette_rgb[i] = palette_tmp[palette_num][i];

	SendMessage(GetDlgItem(hwnd, IDC_PALETTE_R), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)palette_rgb[0]);
	SendMessage(GetDlgItem(hwnd, IDC_PALETTE_G), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)palette_rgb[1]);
	SendMessage(GetDlgItem(hwnd, IDC_PALETTE_B), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)palette_rgb[2]);

	sprintf(buf, "%d", palette_rgb[0]);
	Static_SetText(GetDlgItem(hwnd, IDC_PALETTE_TEXTR), buf);

	sprintf(buf, "%d", palette_rgb[1]);
	Static_SetText(GetDlgItem(hwnd, IDC_PALETTE_TEXTG), buf);

	sprintf(buf, "%d", palette_rgb[2]);
	Static_SetText(GetDlgItem(hwnd, IDC_PALETTE_TEXTB), buf);
}

/* End of source file */
