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

//ANNECY Shaitan Nettoyage (12/05/98) {
/*
#include "stdafx.h"
#include "acp_base.h"
#include "resource.h"
#include "incitf.h"
#include "incgam.h"
#include "incai.h"
#include "WpObj.hpp"
#include "WayObj.hpp"
#include "LinkObj.hpp"
#include "Inter.hpp"
#include "WayDia.hpp"
#include "WpDia.hpp"
#include "LinkDia.hpp"
#include "RotDia.hpp"
#include "oac.h"
#include "x:\cpa\main\inc\_editid.h"
#include "tut.h"


/////////////////////////////////////////////////////////////////////////////
// DiaWay dialog

IMPLEMENT_DYNCREATE ( DiaWay, CFormView);


DiaWay::DiaWay(CWnd* pParent)
	: CFormView(DiaWay::IDD)
{
	//{{AFX_DATA_INIT(DiaWay)
	m_EditWay = _T("");
	m_csWayName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	
	m_hIcon = AfxGetApp()->LoadIcon(IDI_NETWAYPOINT);
	m_bInsert=FALSE;
	m_bDisableDelete=TRUE;
	m_pWayObj=NULL;
}

void DiaWay::fn_vInitDialog (void)
{
	GetDlgItem (IDC_WAY_COMBO)->EnableWindow (FALSE);
	GetDlgItem (IDC_WAY_DELETE_BUTTON)->EnableWindow (FALSE);
	GetDlgItem (IDC_WAY_ADD_BUTTON)->EnableWindow (FALSE);
	GetDlgItem (IDC_WAY_NAME_EDIT)->EnableWindow (FALSE);
}


void DiaWay::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DiaWay)
	DDX_Control(pDX, IDC_WAY_COMBO, m_ListNameWP);
	DDX_CBString(pDX, IDC_WAY_COMBO, m_EditWay);
	DDV_MaxChars(pDX, m_EditWay, 30);
	DDX_Text(pDX, IDC_WAY_NAME_EDIT, m_csWayName);
	DDV_MaxChars(pDX, m_csWayName, 30);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(DiaWay, CFormView)
	//{{AFX_MSG_MAP(DiaWay)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_WAY_DELETE_BUTTON, OnDeleteWayButton)
	ON_BN_CLICKED(IDC_WAY_ADD_BUTTON, OnAddWayButton)
	ON_CBN_SELCHANGE(IDC_WAY_COMBO, OnSelchangeWayCombo)
	ON_CBN_EDITUPDATE(IDC_WAY_COMBO, OnEditupdateWayCombo)
	ON_WM_CLOSE()
	ON_WM_DESTROY()
	ON_CBN_DBLCLK(IDC_WAY_COMBO, OnDblclkWayCombo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// to edit a new way
void DiaWay::fn_vEdit (Way* poWay, int iSelect)
{
	// Aspect of the dialog
	if (!m_pWayObj && poWay) 
	{
		GetDlgItem (IDC_WAY_COMBO)->EnableWindow (TRUE);
		GetDlgItem (IDC_WAY_DELETE_BUTTON)->EnableWindow (TRUE);
		GetDlgItem (IDC_WAY_ADD_BUTTON)->EnableWindow (TRUE);
		GetDlgItem (IDC_WAY_NAME_EDIT)->EnableWindow (TRUE);
	}
	else if (m_pWayObj && !poWay) 
	{
		GetDlgItem (IDC_WAY_COMBO)->EnableWindow (FALSE);
		GetDlgItem (IDC_WAY_DELETE_BUTTON)->EnableWindow (FALSE);
		GetDlgItem (IDC_WAY_ADD_BUTTON)->EnableWindow (FALSE);
		GetDlgItem (IDC_WAY_NAME_EDIT)->EnableWindow (FALSE);
		m_csWayName="";
		UpdateData (FALSE);	
	}
	m_pWayObj=poWay;
	
	if (m_pWayObj)
	{
		CString csWindowTitle;
		csWindowTitle = m_pWayObj->fn_csGetNameInNameList ();
		GetParent()->SetWindowText ( csWindowTitle );

		
		fn_vRefreshDialog (iSelect);
		fn_vEditName ();
	}
	else
	{
		GetParent()->SetWindowText ( "no way to edit" );
		m_ListNameWP.ResetContent();
		m_bInsert=FALSE;
		m_bDisableDelete=TRUE;
		m_csWayName="";	
	}
	GetParent()->GetParent()->RedrawWindow (NULL, NULL, RDW_INVALIDATE|RDW_FRAME);
}


BOOL DiaWay::fn_bIsEdit (Way* poWay)
{
	return m_pWayObj==poWay;
}


void DiaWay::fn_vRefreshDialog (int iSelect)
{
	long lCount;
	char (*dpszNameWay) [30];
	long lNbWayPointInNet = m_pWayObj->fn_lGetWPName (&dpszNameWay);
	
	UpdateData (FALSE); // just to call DoDataExange the first time 

	m_ListNameWP.ResetContent();
	for (lCount=0; lCount<lNbWayPointInNet; lCount++) m_ListNameWP.AddString (dpszNameWay[lCount]);
	m_ListNameWP.AddString ("");
	m_iSelect=iSelect;
	delete[] dpszNameWay;

	m_bDisableDelete=m_ListNameWP.GetCount()<=1;
	GetDlgItem (IDC_WAY_DELETE_BUTTON)->EnableWindow (!m_bDisableDelete);

	fn_vRefreshLinkBox ();
	m_csWayName = m_pWayObj->GetName ();

	UpdateData (FALSE);
	m_ListNameWP.SetCurSel (m_iSelect);

}

void DiaWay::fn_vRefreshNameList (void)
{
	if (!m_pWayObj) return;
	long lCount;
	char (*dpszNameWay) [30];
	long lNbWayPointInNet = m_pWayObj->fn_lGetWPName (&dpszNameWay);
	m_ListNameWP.ResetContent();
	for (lCount=0; lCount<lNbWayPointInNet; lCount++) m_ListNameWP.AddString (dpszNameWay[lCount]);
	m_ListNameWP.AddString ("");
	UpdateData (FALSE);
	m_ListNameWP.SetCurSel (m_iSelect);
}

void DiaWay::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 DiaWay::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 DiaWay::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void DiaWay::OnDeleteWayButton() 
{
	char szBuffer [30];

	m_ListNameWP.GetLBText (m_iSelect, szBuffer);

	// delete in the editor object
	if (m_iSelect<m_pWayObj->GetListWaypoint()->GetCount()) m_pWayObj->fn_vSupWP (m_iSelect);
	m_bInsert=FALSE;
	m_pWayObj->fn_vDialogDeleteWp ();

	// disable the delete button because there is nothing to delete
	if (!m_bDisableDelete && m_ListNameWP.GetCount()<=1) 
	{
		GetDlgItem (IDC_WAY_DELETE_BUTTON)->EnableWindow (FALSE);
		m_bDisableDelete=TRUE;
	}
}

void DiaWay::OnAddWayButton() 
{
	if (m_bInsert || m_iSelect==m_ListNameWP.GetCount()-1) 
	{
		m_ListNameWP.SetFocus();
		return;
	}
	m_bInsert=TRUE;
	m_ListNameWP.InsertString (m_iSelect,"");
	m_ListNameWP.SetEditSel (0,-1);
	m_ListNameWP.SetCurSel (m_iSelect);
	m_ListNameWP.SetFocus();
}

void DiaWay::OnSelchangeWayCombo() 
{
	if (m_bInsert)
	{
		if (!fn_bValidEntry()) 
		{
			m_ListNameWP.SetCurSel(m_iSelect);
			return;
		}
		m_bInsert=FALSE;
	}
	m_iSelect = m_ListNameWP.GetCurSel();
	
	if (m_iSelect==m_pWayObj->GetListWaypoint()->GetCount()) 		
	{
		GetDlgItem (IDC_WAY_DELETE_BUTTON)->EnableWindow (FALSE);
		m_bDisableDelete=TRUE;
	}
	else
	{
		GetDlgItem (IDC_WAY_DELETE_BUTTON)->EnableWindow (TRUE);
		m_bDisableDelete=FALSE;
	}
	
	
	WayPoint::fn_pGetDialog()->fn_vEdit ( m_pWayObj->fn_pGetWayPoint (m_iSelect) );
	m_pWayObj->GetInterface()->GetInterface()->fn_vCancelCurrentSelection (FALSE);
	if (m_pWayObj->fn_pGetWayPoint (m_iSelect)) m_pWayObj->GetInterface()->GetInterface()->fn_vAddSelectedObject (m_pWayObj->fn_pGetWayPoint (m_iSelect)->GetSuperObject() );
	else m_pWayObj->GetInterface()->GetInterface()->fn_vCancelCurrentSelection ();
	Link::fn_pGetDialog()->fn_vEdit (m_pWayObj->fn_pGetLink (m_iSelect));
}

void DiaWay::OnEditupdateWayCombo() 
{
	m_bInsert=TRUE;
	UpdateData (TRUE);
	m_ListNameWP.DeleteString (m_iSelect);
	m_ListNameWP.InsertString (m_iSelect,m_EditWay);
	m_ListNameWP.SetCurSel (m_iSelect);
	m_ListNameWP.SetEditSel(-1,0);
}

BOOL DiaWay::fn_bValidEntry (void)
{
	WayPoint* pInsertWP=NULL;
	UpdateData (TRUE);
	if (m_iSelect==m_ListNameWP.GetCount()-1) m_ListNameWP.AddString (""); 	

	if (*m_EditWay)
	{
		if (!fn_bSameNameSuit())
		{
			// this WP exist ?
			pInsertWP = (WayPoint*)(m_pWayObj->GetInterface()->GetMainWorld()->fn_p_oFindObject (m_EditWay.GetBuffer(30), "Waypoint" ));
			
			if (pInsertWP)
			{
				// this WP is static ?
				if (pInsertWP->fn_bGetCoordinate())
				{
					m_pWayObj->fn_vInsBeforeWP (pInsertWP, m_iSelect );
				
					if (m_bDisableDelete && m_ListNameWP.GetCount()>1)
					{
						GetDlgItem (IDC_WAY_DELETE_BUTTON)->EnableWindow (TRUE);
						m_bDisableDelete=FALSE;
					}
					return TRUE;
				}
				else MessageBox ( "The waypoint "+m_EditWay+ " is not static.", "Error", MB_ICONEXCLAMATION);
			}
			else MessageBox ( "The waypoint "+m_EditWay+ " is not defined.", "Error", MB_ICONEXCLAMATION);
		}
		else MessageBox ("The waypoint "+m_EditWay+ " is doubled in the net.", "Error", MB_ICONEXCLAMATION);
		m_ListNameWP.SetCurSel (m_iSelect); 
		m_ListNameWP.SetEditSel(-1,0);
		return FALSE;
	}
	if (m_iSelect!=m_ListNameWP.GetCount()-1) {
		MessageBox ("You must give a waypoint name", "Error", MB_ICONEXCLAMATION);
		return FALSE;
	}
	return TRUE;
}

void DiaWay::fn_vMessageBoxDynamicWP ( WayPoint* poWP )
{
	MessageBox ( "The waypoint "+CString(poWP->GetSuperObject()->GetName())+ " is not static.", "Error", MB_ICONEXCLAMATION);
}


BOOL DiaWay::fn_bSameNameSuit (void) 
{
	char szCurrentText [30];
	char szPreviousText [30];
	long lCounter;
	sprintf (szPreviousText,"Impossible_name_WGDTSY");
	for (lCounter=0; lCounter<m_ListNameWP.GetCount(); lCounter++)
	{
		m_ListNameWP.GetLBText( lCounter, szCurrentText );
		if (!strcmp(szCurrentText,szPreviousText)) return TRUE;
	    strcpy (szPreviousText,szCurrentText);
	}
	return FALSE;
}


void DiaWay::OnClose() 
{
	if (m_bInsert) 
		if (!fn_bValidEntry()) 
		{
			m_ListNameWP.SetFocus();
			return;
		}
	CFormView::OnClose();
}

  
BOOL DiaWay::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (wParam==394228) 
	{
		if ( GetAsyncKeyState(VK_DELETE) )
		{
			OnDeleteWayButton();	
			return TRUE;
		}
	}

	if (wParam==1)
	{
		OnKillfocusWayNameEdit ();
		if (m_bInsert)
		{

			if (fn_bValidEntry())
			{
				if (m_iSelect==m_ListNameWP.GetCount()-2) m_iSelect++;
				m_ListNameWP.SetCurSel (m_iSelect); 
				m_ListNameWP.SetFocus();
				m_bInsert=FALSE;
				m_pWayObj->fn_vDialogAddWp ();
			}
			else 
			{
				m_ListNameWP.SetCurSel (m_iSelect);
				m_ListNameWP.SetFocus();
			}
			return TRUE;
		}
	}
	
	return CFormView::OnCommand(wParam, lParam);
}


BOOL DiaWay::fn_vSelectWPInList (WayPoint* poWP)
{
	m_iSelect = m_ListNameWP.FindStringExact( 0, poWP->GetSuperObject()->GetName() );
	if (m_iSelect==LB_ERR) return FALSE;
	m_ListNameWP.SetCurSel (m_iSelect);
	fn_vRefreshLinkBox ();
	return TRUE;
}


BOOL DiaWay::fn_vSelectWPInList (int iIndiceWP)
{
	m_iSelect = iIndiceWP;
	m_ListNameWP.SetCurSel (m_iSelect);
	fn_vRefreshLinkBox ();
	return TRUE;
}


void DiaWay::fn_vRefreshLinkBox (void) 
{
	Link* poLink = m_pWayObj->fn_pGetLink(m_iSelect);

	if (poLink && poLink->fn_bIsRotationMode()) Link::fn_pGetRotationDialog()->fn_vEdit (poLink);
	else Link::fn_pGetDialog()->fn_vEdit (poLink);
}


void DiaWay::fn_vEditName (void)
{
	m_csWayName = m_pWayObj->GetName ();
}


void DiaWay::OnKillfocusWayNameEdit() 
{
	CString pOldName = m_csWayName;
	UpdateData (TRUE);
	if (pOldName == m_csWayName) return;
	
	if (m_pWayObj->fn_eRename(m_csWayName)!=E_mc_None)
	{
		CString csMessage = m_csWayName+CString( " is already used.");
		M_GetMainWnd()->UpdateStatus ( csMessage.GetBuffer(80), C_STATUSPANE_INFOS, C_STATUS_ERROR );
		m_csWayName=pOldName;
		UpdateData (FALSE);
	}
	else
	{
		m_pWayObj->GetInterface()->fn_vRefreshHierarchyList ();
		m_pWayObj->fn_vNotifyAllEdtList ();
	}
}

void DiaWay::OnDblclkWayCombo() 
{
	WayPoint* poWp = m_pWayObj->fn_pGetWayPoint (m_iSelect);
	m_pWayObj->GetInterface()->GetInterface()->fn_bSelectObject (poWp->GetSuperObject(), TRUE );
}

BOOL DiaWay::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_WayDialog" , TUT_e_Window);
	TUT_M_vRegisterControlID (IDC_WAY_COMBO,"OWP_WayCombo",TUT_e_ComboBox);
	TUT_M_vRegisterControlID (IDC_WAY_DELETE_BUTTON,"OWP_WayDelete",TUT_e_Button);
	TUT_M_vRegisterControlID (IDC_WAY_ADD_BUTTON,"OWP_WayAdd",TUT_e_Button);
	TUT_M_vRegisterControlID (IDC_WAY_NAME_EDIT,"OWP_WayNameEdit",TUT_e_TextEdit);
	return bCreate;
}

void DiaWay::OnDestroy() 
{
	TUT_M_vGetTutDll ();
	TUT_M_vUnregisterControl (m_hWnd);
	TUT_M_vUnregisterControlID (IDC_WAY_COMBO);
	TUT_M_vUnregisterControlID (IDC_WAY_DELETE_BUTTON);
	TUT_M_vUnregisterControlID (IDC_WAY_ADD_BUTTON);
	TUT_M_vUnregisterControlID (IDC_WAY_NAME_EDIT);
}

*/
//ENDANNECY Shaitan Nettoyage }
