// choosecolor.cpp : implementation file
//

#include "stdafx.h"

#include "Acp_Base.h"
#define HieFriend

#include "matres.h"

#include "inctex.h"
#include "choosecolor.hpp"
#include "_interf.hpp"
#include "x:\cpa\main\inc\_editid.h"
#include "TUT.h"

/////////////////////////////////////////////////////////////////////////////
// CChooseColor dialog

extern Material_Interface *gs_p_oMaterialInterface;

//==================================================================================================
//==================================================================================================
CChooseColor::CChooseColor(CWnd* pParent /*=NULL*/)
	: CDialog(CChooseColor::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChooseColor)
	m_uiRed = 0;
	m_uiGreen = 0;
	m_uiBlue = 0;
	m_uiAlpha = 0;
	
	m_uiCapturedRed = 0;
	m_uiCapturedGreen = 0;
	m_uiCapturedBlue = 0;
	m_uiCapturedAlpha = 0;
	//}}AFX_DATA_INIT
	m_bAutoFill = FALSE;
}


//==================================================================================================
//==================================================================================================
void CChooseColor::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChooseColor)
	DDX_Control(pDX, IDC_SLIDER_RED, m_oSliderRed);
	DDX_Control(pDX, IDC_SLIDER_GREEN, m_oSliderGreen);
	DDX_Control(pDX, IDC_SLIDER_BLUE, m_oSliderBlue);
	DDX_Control(pDX, IDC_SLIDER_ALPHA, m_oSliderAlpha);
	DDX_Text(pDX, IDC_EDIT_RED, m_uiRed);
	DDV_MinMaxUInt(pDX, m_uiRed, 0, 255);
	DDX_Text(pDX, IDC_EDIT_GREEN, m_uiGreen);
	DDV_MinMaxUInt(pDX, m_uiGreen, 0, 255);
	DDX_Text(pDX, IDC_EDIT_BLUE, m_uiBlue);
	DDV_MinMaxUInt(pDX, m_uiBlue, 0, 255);
	DDX_Text(pDX, IDC_EDIT_ALPHA, m_uiAlpha);
	DDV_MinMaxUInt(pDX, m_uiAlpha, 0, 255);
	//}}AFX_DATA_MAP

	if ( !pDX->m_bSaveAndValidate ) //move data from internal values to the slider controls
	{
		//and update the color display
		GetDlgItem(IDC_BUTTON_COLOR)->Invalidate();
	}
}


//==================================================================================================
//==================================================================================================
BEGIN_MESSAGE_MAP(CChooseColor, CDialog)
	//{{AFX_MSG_MAP(CChooseColor)
	ON_WM_DRAWITEM()
	ON_WM_HSCROLL()
	ON_EN_CHANGE(IDC_EDIT_RED, OnChangeEditRed)
	ON_EN_CHANGE(IDC_EDIT_GREEN, OnChangeEditGreen)
	ON_EN_CHANGE(IDC_EDIT_BLUE, OnChangeEditBlue)
	ON_EN_CHANGE(IDC_EDIT_ALPHA, OnChangeEditAlpha)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_TEXTURE_COLOR, OnTextureColor)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChooseColor message handlers

//==================================================================================================
//==================================================================================================
void CChooseColor::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	CDC      *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	RECT					*rect = &lpDrawItemStruct->rcItem;
	COLORREF			xColor;

	switch ( nIDCtl )
	{
	default: 
		CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
		break;

	case IDC_BUTTON_COLOR:
		// lefthand 2/3 with the color
		xColor = RGB(m_uiRed, m_uiGreen, m_uiBlue);
		pDC->FillSolidRect( rect, xColor);
		// righthand 1/3 with the alpha coeff
		rect->left += (rect->right * 2) / 3;
		xColor = RGB(m_uiAlpha, m_uiAlpha, m_uiAlpha);
		pDC->FillSolidRect(rect, xColor);
		break;
	}
}

//==================================================================================================
//==================================================================================================
BOOL CChooseColor::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_oSliderRed.SetRange(0, 255);
	m_oSliderGreen.SetRange(0, 255);
	m_oSliderBlue.SetRange(0, 255);
	m_oSliderAlpha.SetRange(0, 255);

	//move data in the controls to update the sliders, because their range just changed
	UpdateData(FALSE);
	//set the sliders properly
	m_oSliderRed.SetPos(m_uiRed);
	m_oSliderGreen.SetPos(m_uiGreen);
	m_oSliderBlue.SetPos(m_uiBlue);
	m_oSliderAlpha.SetPos(m_uiAlpha);
	// update button
	GetDlgItem(IDC_TEXTURE_COLOR)->EnableWindow(m_bCapturedColor);

	/*m_oColorBitmap.LoadBitmap(IDB_BITMAP_CHOOSECOLOR);
	((CButton *) GetDlgItem(IDC_BUTTON_COLOR))->SetBitmap(HBITMAP(m_oColorBitmap));*/
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//==================================================================================================
//==================================================================================================
void CChooseColor::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if ( !m_bAutoFill )
	{
		m_bAutoFill = TRUE;

		long lPos = ((CSliderCtrl *) pScrollBar)->GetPos();

		//set the internal value as needed
		if ( pScrollBar == (CScrollBar *) &m_oSliderRed )
			m_uiRed = lPos;
		else if ( pScrollBar == (CScrollBar *) &m_oSliderGreen )
			m_uiGreen = lPos;
		else if ( pScrollBar == (CScrollBar *) &m_oSliderBlue )
			m_uiBlue = lPos;
		else if ( pScrollBar == (CScrollBar *) &m_oSliderAlpha )
			m_uiAlpha = lPos;

		//update the controls with the new value
		UpdateData(FALSE);		
		m_bAutoFill = FALSE;
	}

	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

//==================================================================================================
//==================================================================================================
void CChooseColor::OnChangeEditRed() 
{
	if ( !m_bAutoFill )
	{
		m_bAutoFill = TRUE;

		//retrieve data in internal values
		UpdateData(TRUE);
		//store them back in the slider controls
		m_oSliderRed.SetPos(m_uiRed);

		m_bAutoFill = FALSE;
	}
}

//==================================================================================================
//==================================================================================================
void CChooseColor::OnChangeEditGreen() 
{
	if ( !m_bAutoFill )
	{
		m_bAutoFill = TRUE;

		//retrieve data in internal values
		UpdateData(TRUE);
		//store them back in the slider controls
		m_oSliderGreen.SetPos(m_uiGreen);

		m_bAutoFill = FALSE;
	}
}

//==================================================================================================
//==================================================================================================
void CChooseColor::OnChangeEditBlue() 
{
	if ( !m_bAutoFill )
	{
		m_bAutoFill = TRUE;

		//retrieve data in internal values
		UpdateData(TRUE);
		//store them back in the slider controls
		m_oSliderBlue.SetPos(m_uiBlue);

		m_bAutoFill = FALSE;
	}
}

//==================================================================================================
//==================================================================================================
void CChooseColor::OnChangeEditAlpha() 
{
	if ( !m_bAutoFill )
	{
		m_bAutoFill = TRUE;

		//retrieve data in internal values
		UpdateData(TRUE);
		//store them back in the slider controls
		m_oSliderAlpha.SetPos(m_uiAlpha);

		m_bAutoFill = FALSE;
	}
}

//==================================================================================================
//==================================================================================================
void CChooseColor::m_vSetColor(GLI_tdstColor *_p_stColor)
{
	m_uiRed = (UINT) (_p_stColor->xR * 255);
	m_uiGreen = (UINT) (_p_stColor->xG * 255);
	m_uiBlue = (UINT) (_p_stColor->xB * 255);
	m_uiAlpha = (UINT) (_p_stColor->xA * 255);
}
//Mircea Dunka 12.11.98
//==================================================================================================
//==================================================================================================
void CChooseColor::m_vSetCapturedColor(GLI_tdstColor *_p_stCapturedColor)
{
	if (_p_stCapturedColor)
	{
		m_uiCapturedRed = (UINT) (_p_stCapturedColor->xR * 255);
		m_uiCapturedGreen = (UINT) (_p_stCapturedColor->xG * 255);
		m_uiCapturedBlue = (UINT) (_p_stCapturedColor->xB * 255);
		m_uiCapturedAlpha = (UINT) (_p_stCapturedColor->xA * 255);
		m_bCapturedColor = TRUE;
	}
	else
		m_bCapturedColor = FALSE;
}


//==================================================================================================
//==================================================================================================
void CChooseColor::m_vGetColor(GLI_tdstColor *_p_stColor)
{
	_p_stColor->xR = ((float) m_uiRed) / 255;
	_p_stColor->xG = ((float) m_uiGreen) / 255;
	_p_stColor->xB = ((float) m_uiBlue) / 255;
	_p_stColor->xA = ((float) m_uiAlpha) / 255;
}

//Mircea Dunka 12.11.98 - captured color added
//==================================================================================================
//  Description : Edit a color, return true if color has changed
//==================================================================================================
BOOL fn_bEditColor(CWnd *_pParentWnd, GLI_tdstColor *_p_stColor, GLI_tdstColor * _p_stCapturedColor)
{
	CChooseColor oCColor(_pParentWnd);
	oCColor.m_vSetColor(_p_stColor);

	oCColor.m_vSetCapturedColor(_p_stCapturedColor);

	if( oCColor.DoModal() == IDOK )
	{
		oCColor.m_vGetColor(_p_stColor);
		return TRUE;
	}
	return FALSE;

	/*
	static COLORREF s_a16CustColors[16] = 
	{ 
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0,
	};

	COLORREF				cr = RGB(p_stColor->xR * 255, p_stColor->xG * 255, p_stColor->xB * 255);
	CColorDialog		CColor( cr, CC_RGBINIT | CC_FULLOPEN | CC_ENABLEHOOK, _pParentWnd);
	CColor.m_cc.lpCustColors = (COLORREF *) &s_a16CustColors;
	CColor.m_cc.lpfnHook = CCHookProc;
	if ( _wAlpha >= 0 )
		s_a16CustColors[15] = RGB(_wAlpha, 0, 0);
	if (CColor.DoModal() == IDOK)
	{
		cr = CColor.GetColor();
		p_stColor->xR = (GEO_tdxColorValue) (GetRValue(cr) / 255.0);
		p_stColor->xG = (GEO_tdxColorValue) (GetGValue(cr) / 255.0);
		p_stColor->xB = (GEO_tdxColorValue) (GetBValue(cr) / 255.0);
		if ( _wAlpha >= 0 )
			p_stColor->xA = (GEO_tdxColorValue) (GetRValue(s_a16CustColors[15]) / 255.0);
		return TRUE;
	}
	return FALSE;
	*/
}

BOOL CChooseColor::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	if ( CDialog::Create(IDD, pParentWnd) )
	{
		//control registration for tutorial and assistants, should be unregistered when view is destroyed
		TUT_M_vGetTutDll(); 
		TUT_M_vRegisterControlID(IDC_SLIDER_RED,"TGM_ChooseColorSliderRed",TUT_e_Slider);
		TUT_M_vRegisterControlID(IDC_SLIDER_GREEN,"TGM_ChooseColorSliderGreen",TUT_e_Slider);
		TUT_M_vRegisterControlID(IDC_SLIDER_BLUE,"TGM_ChooseColorSliderBlue",TUT_e_Slider);
		TUT_M_vRegisterControlID(IDC_SLIDER_ALPHA,"TGM_ChooseColorSliderAlpha",TUT_e_Slider);
		TUT_M_vRegisterControlID(IDC_EDIT_RED,"TGM_ChooseColorEditRed",TUT_e_TextEdit);
		TUT_M_vRegisterControlID(IDC_EDIT_GREEN,"TGM_ChooseColorEditGreen",TUT_e_TextEdit);
		TUT_M_vRegisterControlID(IDC_EDIT_BLUE,"TGM_ChooseColorEditBlue",TUT_e_TextEdit);
		TUT_M_vRegisterControlID(IDC_EDIT_ALPHA,"TGM_ChooseColorEditAlpha",TUT_e_TextEdit);
		TUT_M_vRegisterControlID(IDOK,"TGM_ChooseColorOK",TUT_e_Button);
		TUT_M_vRegisterControlID(IDCANCEL,"TGM_ChooseColorCancel",TUT_e_Button);

		return TRUE;
	}
	else
		return FALSE;
}

void CChooseColor::OnDestroy() 
{
	//control registration for tutorial and assistants, should be unregistered when view is destroyed
	TUT_M_vGetTutDll(); 
	TUT_M_vUnregisterControlID(IDC_SLIDER_RED);
	TUT_M_vUnregisterControlID(IDC_SLIDER_GREEN);
	TUT_M_vUnregisterControlID(IDC_SLIDER_BLUE);
	TUT_M_vUnregisterControlID(IDC_SLIDER_ALPHA);
	TUT_M_vUnregisterControlID(IDC_EDIT_RED);
	TUT_M_vUnregisterControlID(IDC_EDIT_GREEN);
	TUT_M_vUnregisterControlID(IDC_EDIT_BLUE);
	TUT_M_vUnregisterControlID(IDC_EDIT_ALPHA);
	TUT_M_vUnregisterControlID(IDOK);
	TUT_M_vUnregisterControlID(IDCANCEL);

	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

//Mircea Dunka 11.11.98
void CChooseColor::OnTextureColor() 
{
	m_uiRed = m_uiCapturedRed;
	m_uiGreen = m_uiCapturedGreen;
	m_uiBlue = m_uiCapturedBlue;
	m_uiAlpha = m_uiCapturedAlpha;
	//set the sliders properly
	m_oSliderRed.SetPos(m_uiRed);
	m_oSliderGreen.SetPos(m_uiGreen);
	m_oSliderBlue.SetPos(m_uiBlue);
	m_oSliderAlpha.SetPos(m_uiAlpha);
	UpdateData(FALSE);

	GetDlgItem(IDC_BUTTON_COLOR)->InvalidateRect(NULL);
}
// end Mircea Dunka
