/*********************************************************/
//	NkILSampleSet1Dlg.cpp
//		Source file of ImageInfo dialog(Settings1 dialog) class.
//
//	Copyright(c) 2007, Nikon Corporation - All rights reserved.
/*********************************************************/

#include "stdafx.h"
#include "NkILSample.h"
#include "NkILSampleSet1Dlg.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(CNkILSampleSet1Dlg, CDialog)

CNkILSampleSet1Dlg::CNkILSampleSet1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNkILSampleSet1Dlg::IDD, pParent)
{
	// initialize parameter
	m_pParent		= pParent;		// set pointer of owner window	
	m_lRotate		= 0;	
	m_ulQuality		= 0;	
	m_bFlip			= true;

	// initialize structure
	memset( &m_StrInfo, 0x00, sizeof( m_StrInfo ));	
}

CNkILSampleSet1Dlg::~CNkILSampleSet1Dlg()
{	
}

void CNkILSampleSet1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text( pDX, IDC_EDIT_IMGINFO_WIDTH, m_StrInfo.ulWidth );
	DDX_Text( pDX, IDC_EDIT_IMGINFO_HEIGHT, m_StrInfo.ulHeight );
	DDX_Text( pDX, IDC_EDIT_PROFILE_NAME, m_StrProfile.strProfilePath );
}

BEGIN_MESSAGE_MAP(CNkILSampleSet1Dlg, CDialog)
	ON_COMMAND( IDC_BUTTON_IMGINFO_APPLY, OnApplyImageInfo )
	ON_COMMAND_RANGE( IDC_BUTTON_IMGINFO_CW, IDC_BUTTON_IMGINFO_FLIP, OnOrientation )	
	ON_CONTROL_RANGE( EN_KILLFOCUS, IDC_EDIT_IMGINFO_WIDTH, IDC_EDIT_IMGINFO_HEIGHT, OnKillFocusEdit )
	ON_CONTROL_RANGE( EN_UPDATE, IDC_EDIT_IMGINFO_WIDTH, IDC_EDIT_IMGINFO_HEIGHT, OnUpdateEdit )
	ON_COMMAND( IDC_BUTTON_QUALITY_APPLY, OnApplyRawQuality )
	ON_COMMAND( IDC_BUTTON_PROFILE_APPLY, OnApplyOutputProfile )
	ON_COMMAND( IDC_BUTTON_PROFILE_SELECT, OnSelectProfile )	
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////
// OnInitDialog
BOOL CNkILSampleSet1Dlg::OnInitDialog()
{	
	CDialog::OnInitDialog();
	InitComboString(IDC_COMBO_PROFILE_REND, IDS_PROFILERENDER_PERCEPT, 4);	// output profile rendering
	InitComboString(IDC_COMBO_QUALITY, IDS_RAWQUALITY_HIGH, 3);				// raw quality
	// Initialize each control
	InitSet1DlgStatus();

	// set dialog title
	CString strTitle;
	strTitle.LoadString( IDS_TITLE_SET1DLG );
	SetWindowText( strTitle );

	return TRUE;
}
// OnInitDialog
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// InitSet1DlgStatus
void CNkILSampleSet1Dlg::InitSet1DlgStatus()
{
	// initialize ComboBox control
	// rendering for output profile	
	
	switch( m_StrProfile.ulRenderingIntent )
	{		
	case kNkfl_RenderingIntent_Relative:
		((CComboBox*)GetDlgItem( IDC_COMBO_PROFILE_REND ))->SetCurSel(1);
		break;
	case kNkfl_RenderingIntent_Absolute:
		((CComboBox*)GetDlgItem( IDC_COMBO_PROFILE_REND ))->SetCurSel(2);
		break;
	case kNkfl_RenderingIntent_Saturation:
		((CComboBox*)GetDlgItem( IDC_COMBO_PROFILE_REND ))->SetCurSel(3);
		break;
	case kNkfl_RenderingIntent_Perceptual:
	default:
		((CComboBox*)GetDlgItem( IDC_COMBO_PROFILE_REND ))->SetCurSel(0);
		break;	
	}
	// quality			
	switch( m_ulQuality )
	{		
	case kNkfl_RawQuality_Low:
		((CComboBox*)GetDlgItem( IDC_COMBO_QUALITY ))->SetCurSel(1);
		break;
	case kNkfl_RawQuality_LowResolution:
		((CComboBox*)GetDlgItem( IDC_COMBO_QUALITY ))->SetCurSel(2);
		break;
	case kNkfl_RawQuality_High:
	default:
		((CComboBox*)GetDlgItem( IDC_COMBO_QUALITY ))->SetCurSel(0);
		break;
	}

	if( kNkfl_FileFormat_NEF_RAW != ((CMainFrame*)m_pParent)->GetFormat() && kNkfl_FileFormat_NEF_COMP != ((CMainFrame*)m_pParent)->GetFormat() )
	{
		// excluding raw, disable raw quality control
		GetDlgItem( IDC_COMBO_QUALITY )->EnableWindow( false );
		GetDlgItem( IDC_BUTTON_QUALITY_APPLY )->EnableWindow( false );
	}		
	// updata parameters of control
	UpdateData( false );
}
// InitSet1DlgStatus
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// OnCancel
void CNkILSampleSet1Dlg::OnCancel()
{
	if( m_pParent != NULL )
	{
		m_pParent->PostMessage( MSG_WM_DELETE_SET1DLG );	// send message for delete dialog	
	}
	else
	{
		CDialog::OnCancel();
	}
}
// OnCancel
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// PreTranslateMessage
BOOL CNkILSampleSet1Dlg::PreTranslateMessage(MSG *pMsg)
{
	if( WM_KEYDOWN == pMsg->message )
	{
		if( VK_RETURN == pMsg->wParam )
		{
			return TRUE;	// prevent calling OnOK() by click Enter
		}
		if( VK_ESCAPE == pMsg->wParam )
		{
			return TRUE;	// prevent calling OnCancel() by click Escape
		}
	}

	return CDialog::PreTranslateMessage(pMsg);
}
// PreTranslateMessage
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// OnApplyImageInfo
void CNkILSampleSet1Dlg::OnApplyImageInfo()
{
	// get ImageInfo status at dialog and set struct
	UpdateData();		// reflect edit value

	// send message and enum value for check kind of struct
	m_pParent->SendMessage( MSG_WM_APPLY_SET1DLG, kILSample_Group_ImageInfo, (LPARAM)&m_StrInfo );
}
// OnApplyImageInfo
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
// OnApplyRawQuality
void CNkILSampleSet1Dlg::OnApplyRawQuality()
{
	// get RawQuality combo sel
	int nSel = ((CComboBox*)GetDlgItem(IDC_COMBO_QUALITY))->GetCurSel();
	switch( nSel )
	{
	case 0:
		m_ulQuality = kNkfl_RawQuality_High;
		break;
	case 1:
		m_ulQuality = kNkfl_RawQuality_Low;
		break;
	case 2:
		m_ulQuality = kNkfl_RawQuality_LowResolution;
		break;
	}

	// send message and enum value for check kind of struct
	m_pParent->SendMessage( MSG_WM_APPLY_SET1DLG, kILSample_Group_RawQuality, (LPARAM)m_ulQuality );
}
// OnApplyRawQuality
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
//OnApplyOutputProfile
void CNkILSampleSet1Dlg::OnApplyOutputProfile()
{
	// get OutputProfile status at dialog and set sturcture
	UpdateData(false);
	// get rendering intent from combobox
	int nSel = ((CComboBox*)GetDlgItem(IDC_COMBO_PROFILE_REND))->GetCurSel();
	switch( nSel )
	{
	case 0:
		m_StrProfile.ulRenderingIntent = kNkfl_RenderingIntent_Perceptual;
		break;
	case 1:
		m_StrProfile.ulRenderingIntent = kNkfl_RenderingIntent_Relative;
		break;
	case 2:
		m_StrProfile.ulRenderingIntent = kNkfl_RenderingIntent_Absolute;
		break;
	case 3:
		m_StrProfile.ulRenderingIntent = kNkfl_RenderingIntent_Saturation;
		break;
	}
	
	// send message and enum value for check kind of struct
	m_pParent->SendMessage( MSG_WM_APPLY_SET1DLG, kILSample_Group_OutputProfile, (LPARAM)&m_StrProfile );
}
//OnApplyOutputProfile
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
//OnOrientation
void CNkILSampleSet1Dlg::OnOrientation( UINT nID )	
{
	// check ID	
	switch( nID )
	{
	case IDC_BUTTON_IMGINFO_CW:	
		m_lRotate += 90;		
		if( m_lRotate >= 360 )	m_lRotate -= 360;	// adjust Image angle 					
		break;
	case IDC_BUTTON_IMGINFO_CCW:
		m_lRotate -= 90;		
		if( m_lRotate < 0 ) m_lRotate += 360;	// adjust Image angle 						
		break;
	case IDC_BUTTON_IMGINFO_FLIP:
		m_bFlip = ( m_bFlip ) ? false : true;						// adjust Image flip        
		break;
	}

	// orientation
	switch( m_lRotate )
	{
	case 90:
		m_StrInfo.ulOrientation = ( m_bFlip ) ? kNkfl_Orientation_FlippedCW90 : kNkfl_Orientation_CW90;
		break;
	case 180:
		m_StrInfo.ulOrientation = ( m_bFlip ) ? kNkfl_Orientation_FlippedCW180 : kNkfl_Orientation_CW180;
		break;
	case 270:
		m_StrInfo.ulOrientation = ( m_bFlip ) ? kNkfl_Orientation_FlippedCW270 : kNkfl_Orientation_CW270;
		break;
	default:
		m_StrInfo.ulOrientation = ( m_bFlip ) ? kNkfl_Orientation_FlippedCW0 : kNkfl_Orientation_CW0;		
	}	

	// send message and enum value for check kind of struct
	m_pParent->SendMessage( MSG_WM_APPLY_SET1DLG, kILSample_Group_ImageInfo, (LPARAM)&m_StrInfo );
}
//OnOrientation
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
//OnSelectProfile
void CNkILSampleSet1Dlg::OnSelectProfile()
{		
	// create filedialog for Profile
	char szFilter[] = "Output Profiles (*.icm)|*.icm||";
	CFileDialog	clFileDlg(TRUE, ".icm", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
	// show filedialog and select OutputProfile
	if(clFileDlg.DoModal()==IDOK) 
	{
		m_StrProfile.strProfilePath = clFileDlg.GetPathName();
	}
	// reflect filepath to dialog control
	UpdateData(false);
}
//OnSelectProfile
/////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
// OnKillFocusEdit
void CNkILSampleSet1Dlg::OnKillFocusEdit( UINT nID )
{
	CString strEdit;
	((CEdit*)GetDlgItem( nID ))->GetWindowText(strEdit);
	strEdit.TrimLeft();														// delete space
	if( 0 == strEdit.GetLength() )
	{		
		strEdit.Format( "%d", 0 );
		((CEdit*)GetDlgItem( nID ))->SetWindowText( strEdit );	// set minimum value
	}
}
// OnKillFocusEdit
//////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
// OnUpdateEdit
void CNkILSampleSet1Dlg::OnUpdateEdit( UINT nID )
{
	// get value of editbox
	CEdit* pEdit = (CEdit*)GetDlgItem( nID );
	CString strEdit;
	pEdit->GetWindowText( strEdit );

	strEdit.TrimLeft();			// trim space of the first part
	int nEdit = atoi( strEdit );// convert to numeric by strtod()

	if( nEdit <= 0 )
	{
		// if under limit size or except numerical value is set, set ""
		if( '0' != strEdit && 0 != strcmp( strEdit, "" ))		
			pEdit->SetWindowText( "" );		
	}
	else
	{
		CString strTmp;
		strTmp.Format( "%d", nEdit );
		if( strTmp != strEdit )		
			pEdit->SetWindowText( strTmp );		
	}
} // OnUpdateEdit
//====================================================================
// function name		：InitComboString
// function abstract	：set string of all parameter to ComboBox control
// parameter			：nComboID		ID of ComboBox control
//						  nFirstParamID	ID of first parameter's string
//						  nParamNum		total parameter number of this ComboBox 
// return value			：none
//====================================================================
void CNkILSampleSet1Dlg::InitComboString( UINT nComboID, UINT nFirstParamID, int nParamNum )
{
	CComboBox* pCombo = (CComboBox*)GetDlgItem( nComboID );						
	for( int i = 0; i < nParamNum; i++ )
	{
		CString strComboParam;
		strComboParam.LoadString( nFirstParamID++ );// get string of param
		pCombo->AddString( strComboParam );			// add param to ComboBox
	}
}
//InitComboString
/////////////////////////////////////////////////////////////////

//====================================================================
// function name		：InitParameter
// function abstract	：initialize parameter 
// parameter			：StrInfo	 sturcture of iamgeinfo
//						  ulQuality  raw qulaity
//						  StrProfile sturcture of outputprofile
// return value			：true/false
//====================================================================
bool CNkILSampleSet1Dlg::InitParameter(NkIL_ImageInfo StrInfo, unsigned long ulQuality, ILSample_OutputProfile StrProfile)
{
	// copy structure data
	m_StrInfo		= StrInfo;
	m_StrProfile	= StrProfile;

	m_ulQuality = ulQuality;
	// get rotate and flip from orientation
	// convert 
	switch( StrInfo.ulOrientation )
	{	
	case kNkfl_Orientation_FlippedCW90:
	case kNkfl_Orientation_CW90:
		m_lRotate = 90;
		break;
	case kNkfl_Orientation_FlippedCW180:
	case kNkfl_Orientation_CW180:
		m_lRotate = 180;
		break;
	case kNkfl_Orientation_FlippedCW270:
	case kNkfl_Orientation_CW270:
		m_lRotate = 270;
		break;
	case kNkfl_Orientation_FlippedCW0:
	case kNkfl_Orientation_CW0: 
	default:
		m_lRotate = 0;
		break;
	}
	switch( StrInfo.ulOrientation ) 
	{
	case kNkfl_Orientation_FlippedCW90:
	case kNkfl_Orientation_FlippedCW180:
	case kNkfl_Orientation_FlippedCW270:
	case kNkfl_Orientation_FlippedCW0:
		m_bFlip	= true;
		break;
	default:
		m_bFlip	= false;
		break;
	}		

	return true;
}
//InitParameter
/////////////////////////////////////////////////////////////////

