/*
///////////////////////////////////////////////////////////////////////////////////////////////////
// Description : WPDia.cpp
//   
// Dialog edition the waypoints
//
///////////////////////////////////////////////////////////////////////////////////////////////////
// 
//					
///////////////////////////////////////////////////////////////////////////////////////////////////
// Creation date: 21 jan 1997                        Author: J Thénoz
///////////////////////////////////////////////////////////////////////////////////////////////////
// Modification date:			                      Author: 
// 
//
// 
//
///////////////////////////////////////////////////////////////////////////////////////////////////
*/

#include "stdafx.h"
#include "acp_base.h"
#include "resource.h"
#include "incitf.h"
#include "incgam.h"
#include "incai.h"
#include "WPObj.hpp"
//ANNECY Shaitan Nettoyage (12/05/98) {
/*
#include "WayObj.hpp"
#include "LinkObj.hpp"
#include "WayDia.hpp"
*/
//ENDANNECY Shaitan Nettoyage }
#include "WPDia.hpp"
#include "Inter.hpp"
#include "ogd.h"
#include "x:/cpa/main/inc/_EditId.h"
#include "tut.h"



IMPLEMENT_DYNCREATE ( DiaWP, CFormView );

/////////////////////////////////////////////////////////////////////////////
// DiaWP dialog


DiaWP::DiaWP(CWnd* pParent /*=NULL*/)
	: CFormView(DiaWP::IDD)

{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);  //IDI_SYMBOLICWP
	m_bEditRadius=FALSE;
	m_poWP = NULL;
}

void DiaWP::fn_vInitDialog ( void )
{
	// init the radio buttons icons
	((CButton*)GetDlgItem (IDC_SYMBOLICALWP_RADIO))->SetIcon( AfxGetApp()->LoadIcon(IDI_SYMBOLICWP) );
	((CButton*)GetDlgItem (IDC_REALWP_RADIO))->SetIcon( AfxGetApp()->LoadIcon(IDI_REALWP) );

	// desactivate all the controls
	GetDlgItem (IDC_GLOBALDRAW_CHECK)->EnableWindow (FALSE);
	GetDlgItem (IDC_SYMBOLICALWP_RADIO)->EnableWindow (FALSE);
	GetDlgItem (IDC_REALWP_RADIO)->EnableWindow (FALSE);
	GetDlgItem (IDC_WAYPOINT_COORD_CHECK)->EnableWindow (FALSE);
	GetDlgItem (IDC_WAYPOINT_EDIT)->EnableWindow (FALSE);
	GetDlgItem (IDC_WAYPOINT_NAME_EDIT)->EnableWindow (FALSE);
}


// to edit a new waypoint
void DiaWP::fn_vEdit (WayPoint* poWP)
{
	// Aspect of the dialog
	if (!m_poWP && poWP)
	{
		// wake up
		GetDlgItem (IDC_GLOBALDRAW_CHECK)->EnableWindow (TRUE);
		GetDlgItem (IDC_SYMBOLICALWP_RADIO)->EnableWindow (TRUE);
		GetDlgItem (IDC_REALWP_RADIO)->EnableWindow (TRUE);
		GetDlgItem (IDC_WAYPOINT_COORD_CHECK)->EnableWindow (TRUE);
		GetDlgItem (IDC_WAYPOINT_EDIT)->EnableWindow (TRUE);
		GetDlgItem (IDC_WAYPOINT_TEXT)->EnableWindow (TRUE);
		GetDlgItem (IDC_WAYPOINT_NAME_EDIT)->EnableWindow (TRUE);
	}
	else if (m_poWP && !poWP)
	{
		GetDlgItem (IDC_GLOBALDRAW_CHECK)->EnableWindow (FALSE);
		GetDlgItem (IDC_SYMBOLICALWP_RADIO)->EnableWindow (FALSE);
		GetDlgItem (IDC_REALWP_RADIO)->EnableWindow (FALSE);
		GetDlgItem (IDC_WAYPOINT_COORD_CHECK)->EnableWindow (FALSE);
		GetDlgItem (IDC_WAYPOINT_EDIT)->EnableWindow (FALSE);
		GetDlgItem (IDC_WAYPOINT_TEXT)->EnableWindow (FALSE);
		GetDlgItem (IDC_WAYPOINT_NAME_EDIT)->EnableWindow (FALSE);
	}

	m_poWP = poWP;
	fn_vRefreshCoordinate();
	if (poWP) 
	{
		GetParent()->SetWindowText ( poWP->GetName() );
		// set the style buttons
		CheckRadioButton( IDC_SYMBOLICALWP_RADIO, IDC_REALWP_RADIO, poWP->fn_bIsRealDraw() ? IDC_REALWP_RADIO : IDC_SYMBOLICALWP_RADIO  );
		
		if (m_poWP->fn_bIsAlone())
		{
			GetDlgItem (IDC_WAYPOINT_ALONE_PICTURE)->ShowWindow( SW_SHOW );
			GetDlgItem (IDC_WAYPOINT_WAY_PICTURE)->ShowWindow( SW_HIDE );
		}
		else 
		{	
			GetDlgItem (IDC_WAYPOINT_WAY_PICTURE)->ShowWindow( SW_SHOW ); 
			GetDlgItem (IDC_WAYPOINT_ALONE_PICTURE)->ShowWindow( SW_HIDE );
		}
		GetDlgItem (IDC_WAYPOINT_COORD_CHECK)->EnableWindow (m_poWP->fn_pGetDynamicFather() ? TRUE : FALSE);

		fn_vEditName ();
		fn_vRefreshDialog ();
	}	
	else 
	{
		m_fRadius = 0.; 
		m_bStaticCoord = FALSE;
		GetParent()->SetWindowText ( "no waypoint to edit" );
		UpdateData (FALSE);
		m_csWaypointName="";
		UpdateData (FALSE);
	}
	GetParent()->GetParent()->RedrawWindow (NULL, NULL, RDW_INVALIDATE|RDW_FRAME);
}

void DiaWP::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DiaWP)
	DDX_Control(pDX, IDC_WAYPOINT_EDIT, m_EditWP);
	DDX_Text(pDX, IDC_WAYPOINT_EDIT, m_fRadius);
	DDV_MinMaxDouble(pDX, m_fRadius, 0., 99.99);
	DDX_Check(pDX, IDC_WAYPOINT_COORD_CHECK, m_bStaticCoord);
	DDX_Check(pDX, IDC_GLOBALDRAW_CHECK, m_bGlobalCheck);
	DDX_Text(pDX, IDC_WAYPOINT_NAME_EDIT, m_csWaypointName);
	DDV_MaxChars(pDX, m_csWaypointName, 50);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(DiaWP, CFormView)
	//{{AFX_MSG_MAP(DiaWP)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_KILLFOCUS(IDC_WAYPOINT_EDIT, OnKillfocusWaypointEdit)
	ON_BN_CLICKED(IDC_WAYPOINT_COORD_CHECK, OnWaypointCoordCheck)
	ON_EN_CHANGE(IDC_WAYPOINT_EDIT, OnChangeWaypointEdit)
	ON_BN_CLICKED(IDC_REALWP_RADIO, OnRealwpRadio)
	ON_BN_CLICKED(IDC_SYMBOLICALWP_RADIO, OnSymbolicalwpRadio)
	ON_BN_CLICKED(IDC_GLOBALDRAW_CHECK, OnGlobaldrawCheck)
	ON_WM_DESTROY()
	ON_EN_KILLFOCUS(IDC_WAYPOINT_NAME_EDIT, OnKillfocusWaypointNameEdit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DiaWP message handlers

/////////////////////////////////////////////////////////////////////////////
// DiaNetWP message handlers

  
void DiaWP::fn_vRefreshDialog (void)
{
	m_fRadius = (float)(m_poWP->fn_xGetRadius());
	m_bStaticCoord = m_poWP->fn_bGetCoordinate();
	// change the radio buttons push
	CheckRadioButton( IDC_SYMBOLICALWP_RADIO, IDC_REALWP_RADIO, m_poWP->fn_bIsGeneralRealDraw() ? IDC_REALWP_RADIO : IDC_SYMBOLICALWP_RADIO  );
	UpdateData (FALSE);
}

void DiaWP::fn_vRefreshCoordinate(void)
{
	if (!m_poWP) return; // there's no waypoint to edit
//ANNECY Shaitan Nettoyage (12/05/98) {
//	if (m_poWP->GetInterface()->fn_bWaypointInAWay(m_poWP)) GetDlgItem (IDC_WAYPOINT_COORD_CHECK)->EnableWindow (FALSE);
//ENDANNECY Shaitan Nettoyage }
	else GetDlgItem (IDC_WAYPOINT_COORD_CHECK)->EnableWindow (TRUE);
}

void DiaWP::OnSysCommand(UINT nID, LPARAM lParam)
{
		CFormView::OnSysCommand(nID, lParam);
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void DiaWP::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CFormView::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR DiaWP::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void DiaWP::OnKillfocusWaypointEdit() 
{
	if (!m_bEditRadius) return;
	UpdateData (TRUE);
	GLI_tdxValue xRadius = (GLI_tdxValue)m_fRadius;
	if (xRadius == 0.0f)
		xRadius = 0.001f;
	m_poWP->fn_vSetRadius (&xRadius);
	m_poWP->fn_vDrawNewRadius ();
	m_bEditRadius=FALSE;
}


BOOL DiaWP::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (wParam==1)
	{
		OnKillfocusWaypointEdit();
		OnKillfocusWaypointNameEdit();
	}
	return CFormView::OnCommand(wParam, lParam);
}


void DiaWP::OnWaypointCoordCheck() 
{
	m_poWP->fn_vChangeSystemCoordinate ();
}

void DiaWP::OnChangeWaypointEdit() 
{
	m_bEditRadius=TRUE;
}


// we want a real draw
void DiaWP::OnRealwpRadio() 
{
	m_poWP->fn_vSetGeneralRealDraw (TRUE);
	fn_vDrawWP ();

}

void DiaWP::OnSymbolicalwpRadio() 
{
	m_poWP->fn_vSetGeneralRealDraw (FALSE);
	fn_vDrawWP ();
}

void DiaWP::OnGlobaldrawCheck() 
{
	// set the memeber varaible
	UpdateData (TRUE);
	
	// change the status of the WP object
	m_poWP->fn_vSetGlobalDraw (m_bGlobalCheck);

	// change the radio buttons push
	CheckRadioButton( IDC_SYMBOLICALWP_RADIO, IDC_REALWP_RADIO, m_poWP->fn_bIsGeneralRealDraw() ? IDC_REALWP_RADIO : IDC_SYMBOLICALWP_RADIO  );
	
	((Waypoint_Interface*)(m_poWP->GetInterface()))->fn_vDrawAllWP();
}

// draw all the WP that must be redraw
void DiaWP::fn_vDrawWP (void)
{
	if (m_bGlobalCheck) ((Waypoint_Interface*)(m_poWP->GetInterface()))->fn_vDrawAllWP();
	else  m_poWP->fn_vDraw ();	// draw the WP in his new mode
}

// Neme edition
void DiaWP::fn_vEditName (void)
{
	m_csWaypointName = m_poWP->GetName();
}

void DiaWP::OnKillfocusWaypointNameEdit() 
{
	CString pOldName = m_csWaypointName;
	UpdateData (TRUE);
	if (pOldName == m_csWaypointName) return;
	
	if (m_poWP->fn_eRename (m_csWaypointName)!=E_mc_None)
	{
		MessageBox ( m_csWaypointName+CString( " is already used."),"Attention",MB_ICONEXCLAMATION );
		m_csWaypointName=pOldName;
		UpdateData (FALSE);
	}
	else 
	{
		m_poWP->GetInterface()->fn_vRefreshHierarchyList ();
//ANNECY Shaitan Nettoyage (12/05/98) {
/*
		Way::fn_pGetDialog()->fn_vRefreshNameList ();
	
		// notify all the ways using this waypoint
		POSITION xPos;
		Way* poWay;
		for ( poWay=Way::fn_poGetListOfWay()->GetHeadElement(xPos); poWay;  poWay=Way::fn_poGetListOfWay()->GetNextElement(xPos) )
			if ( poWay->fn_iIsWayPointInNet ((WayPoint*)m_poWP)>=0 ) poWay->fn_vNotifySave ();

*/
//ENDANNECY Shaitan Nettoyage }

	}
}

BOOL DiaWP::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	BOOL bCreate = CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
	TUT_M_vGetTutDll ();
	TUT_M_vRegisterControl (m_hWnd , "OWP_WaypointDialog" , TUT_e_Window);
	TUT_M_vRegisterControlID (IDC_GLOBALDRAW_CHECK,"OWP_GlobalDraw",TUT_e_Button);
	TUT_M_vRegisterControlID (IDC_SYMBOLICALWP_RADIO,"OWP_SymbolicalDraw",TUT_e_Button);
	TUT_M_vRegisterControlID (IDC_REALWP_RADIO,"OWP_RealDraw",TUT_e_Button);
	TUT_M_vRegisterControlID (IDC_WAYPOINT_COORD_CHECK,"OWP_Absolute",TUT_e_Button);
	TUT_M_vRegisterControlID (IDC_WAYPOINT_EDIT,"OWP_WaypointRadiusEdit",TUT_e_TextEdit);
	TUT_M_vRegisterControlID (IDC_WAYPOINT_NAME_EDIT,"OWP_WaypointNameEdit",TUT_e_TextEdit);
	return bCreate;
}


void DiaWP::OnDestroy() 
{
	TUT_M_vGetTutDll ();
	TUT_M_vUnregisterControl (m_hWnd);
	TUT_M_vUnregisterControlID (IDC_GLOBALDRAW_CHECK);
	TUT_M_vUnregisterControlID (IDC_SYMBOLICALWP_RADIO);
	TUT_M_vUnregisterControlID (IDC_REALWP_RADIO);
	TUT_M_vUnregisterControlID (IDC_WAYPOINT_COORD_CHECK);
	TUT_M_vUnregisterControlID (IDC_WAYPOINT_EDIT);
	TUT_M_vUnregisterControlID (IDC_WAYPOINT_NAME_EDIT);
}
