//ROMTEAM WorldEditor
////////////////////////////////////////////////////////////////////////////////////////
// File        : EToolBar.cpp
// Author      : Cristian S
// Date        : 97.11
// Description :
////////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//ROMTEAM WorldEditor (Cristian Stegaru 11/97)
#include "3DGe_res.h"       // main symbols
//ENDROMTEAM WorldEditor (Cristian Stegaru)
#include "EToolBar.h"
#include "ITView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static UINT BASED_CODE ToolBarButtons[] =
{
	// same order as in the bitmap 'IDR_EDIT_TOOLBAR'
	ID_EDIT_SLIDER,
		ID_SEPARATOR,
	ID_STATIC_NAME,
	ID_EDIT_NAME,
	ID_STATIC_XNB,
	ID_EDIT_XNB,
	ID_STATIC_YNB,
	ID_EDIT_YNB,
		ID_SEPARATOR,
	ID_BT_NEW,
	ID_BT_SAVE,
	ID_BT_HIDE,
	ID_BT_UNDO,
	ID_BT_REDO,
	ID_BT_ONTOP,
	ID_BT_EXIT
};

#define SLIDER_INDEX		0
#define SLIDER_BITMAP		0
#define SLIDER_WIDTH		100
#define SLIDER_HEIGHT		150

#define STATIC_NAME_INDEX	2
#define STATIC_NAME_BITMAP	1
#define STATIC_NAME_WIDTH	50
#define STATIC_NAME_HEIGHT	20

#define EDIT_NAME_INDEX		3
#define EDIT_NAME_BITMAP	2
#define EDIT_NAME_WIDTH		80
#define EDIT_NAME_HEIGHT	20

#define STATIC_XNB_INDEX	4
#define	STATIC_XNB_BITMAP	3
#define	STATIC_XNB_WIDTH	40
#define	STATIC_XNB_HEIGHT	20

#define EDIT_XNB_INDEX		5
#define	EDIT_XNB_BITMAP		4
#define	EDIT_XNB_WIDTH		40
#define	EDIT_XNB_HEIGHT		20

#define STATIC_YNB_INDEX	6
#define	STATIC_YNB_BITMAP	5
#define	STATIC_YNB_WIDTH	40
#define	STATIC_YNB_HEIGHT	20

#define EDIT_YNB_INDEX		7
#define	EDIT_YNB_BITMAP		6
#define	EDIT_YNB_WIDTH		40
#define	EDIT_YNB_HEIGHT		20

#define EDIT_ONTOP_INDEX	14
#define EDIT_HIDE_INDEX		11


BEGIN_MESSAGE_MAP(CEditorToolBar, CToolBar)
	//{{AFX_MSG_MAP(CEditorToolBar)
	ON_WM_HSCROLL()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

#define DEFAULT_X	15
#define DEFAULT_Y	15

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : CEditorToolBar constructor
// Date   : November 1997
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
CEditorToolBar::CEditorToolBar ()
{
	m_iXNb = DEFAULT_X;
	m_iYNb = DEFAULT_Y;
	m_csName.LoadString (IDS_DEFAULT_ITNAME);
}

CEditorToolBar::~CEditorToolBar ()
{
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : Init 
// Date   : November 1997
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CEditorToolBar::Init (CWnd *pParent)
{
	DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_SIZE_DYNAMIC | CBRS_TOOLTIPS | CBRS_FLYBY;
	if (!Create (pParent, dwStyle, IDW_TOOLBAR) || !LoadBitmap(IDR_EDIT_TOOLBAR))
		return FALSE;

	if(!SetButtons(ToolBarButtons, sizeof(ToolBarButtons)/sizeof(UINT)))
		return FALSE;

	CRect sliderRect (-SLIDER_WIDTH, -SLIDER_HEIGHT, 0, 0);
	CRect staticNameRect (-STATIC_NAME_WIDTH, -STATIC_NAME_HEIGHT, 0, 0);
	CRect editNameRect (-EDIT_NAME_WIDTH, -EDIT_NAME_HEIGHT, 0, 0);
	CRect xnbRect (-STATIC_XNB_WIDTH, -STATIC_XNB_HEIGHT, 0, 0);
	CRect ynbRect (-STATIC_YNB_WIDTH, -STATIC_YNB_HEIGHT, 0, 0);
	
	DWORD dwStaticStyle = WS_CHILD | SS_CENTER;
	DWORD dwEditStyle = WS_CHILD | ES_LEFT | ES_AUTOHSCROLL | WS_TABSTOP | WS_BORDER;
	DWORD dwSliderStyle = WS_CHILD | TBS_HORZ | TBS_NOTICKS;

	if (!m_stName.Create ("Name:", dwStaticStyle, staticNameRect, this, IDC_STATIC_NAME) ||
		!m_stXNb.Create ("XNb:", dwStaticStyle, xnbRect, this, IDC_STATIC_XNB) ||
		!m_stYNb.Create ("YNb:", dwStaticStyle, ynbRect, this, IDC_STATIC_YNB) ||
		!m_edName.Create (dwEditStyle, editNameRect, this, IDC_EDIT_NAME) ||
		!m_edXNb.Create (dwEditStyle, xnbRect, this, IDC_EDIT_XNB) ||
		!m_edYNb.Create (dwEditStyle, ynbRect, this, IDC_EDIT_YNB) ||
		!m_Slider.Create (dwSliderStyle, sliderRect, this, IDC_SLIDER))
		return FALSE;

	SetButtonInfo(STATIC_NAME_INDEX, IDC_STATIC_NAME, TBBS_SEPARATOR, STATIC_NAME_WIDTH);
	SetButtonInfo(STATIC_XNB_INDEX, IDC_STATIC_XNB, TBBS_SEPARATOR, STATIC_XNB_WIDTH);
	SetButtonInfo(STATIC_YNB_INDEX, IDC_STATIC_YNB, TBBS_SEPARATOR, STATIC_YNB_WIDTH);
	SetButtonInfo(EDIT_NAME_INDEX, IDC_EDIT_NAME, TBBS_SEPARATOR, EDIT_NAME_WIDTH);
	SetButtonInfo(EDIT_XNB_INDEX, IDC_EDIT_XNB, TBBS_SEPARATOR, EDIT_XNB_WIDTH);
	SetButtonInfo(EDIT_YNB_INDEX, IDC_EDIT_YNB, TBBS_SEPARATOR, EDIT_YNB_WIDTH);
	SetButtonInfo(SLIDER_INDEX, IDC_SLIDER, TBBS_SEPARATOR, SLIDER_WIDTH);

	UINT uID = 0, uStyle = 0;
	int  iImage = 0;
	GetButtonInfo (EDIT_ONTOP_INDEX, uID, uStyle, iImage);
	SetButtonInfo (EDIT_ONTOP_INDEX, uID, uStyle | TBBS_CHECKBOX, iImage);

	GetButtonInfo (EDIT_HIDE_INDEX, uID, uStyle, iImage);
	SetButtonInfo (EDIT_HIDE_INDEX, uID, uStyle | TBBS_CHECKBOX, iImage);

	if (NULL != m_stName.m_hWnd)
	{
		CRect rect;
		GetItemRect(STATIC_NAME_INDEX, rect);
		
		m_stName.SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS);
		m_stName.ShowWindow(SW_SHOW);
	}

	if (NULL != m_stXNb.m_hWnd)
	{
		CRect rect;
		GetItemRect(STATIC_XNB_INDEX, rect);
		
		m_stXNb.SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS);
		m_stXNb.ShowWindow(SW_SHOW);
	}

	if (NULL != m_stYNb.m_hWnd)
	{
		CRect rect;
		GetItemRect(STATIC_YNB_INDEX, rect);
		
		m_stYNb.SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS);
		m_stYNb.ShowWindow(SW_SHOW);
	}

	if (NULL != m_edName.m_hWnd)
	{
		CRect rect;
		GetItemRect(EDIT_NAME_INDEX, rect);
		
		m_edName.SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS);
		m_edName.ShowWindow(SW_SHOW);
	}

	if (NULL != m_edXNb.m_hWnd)
	{
		CRect rect;
		GetItemRect(EDIT_XNB_INDEX, rect);
		
		m_edXNb.SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS);
		m_edXNb.ShowWindow(SW_SHOW);
	}

	if (NULL != m_edYNb.m_hWnd)
	{
		CRect rect;
		GetItemRect(EDIT_YNB_INDEX, rect);
		
		m_edYNb.SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS);
		m_edYNb.ShowWindow(SW_SHOW);
	}

	if (NULL != m_Slider.m_hWnd)
	{
		CRect rect;
		GetItemRect(SLIDER_INDEX, rect);
		
		m_Slider.SetWindowPos(NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS);
		m_Slider.ShowWindow(SW_SHOW);
	}

	UpdateData (FALSE);
	return TRUE;
}
	

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : OnHScroll
// Date   : November 1997
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
void CEditorToolBar::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	int min, max;
	CSliderCtrl *sl = (CSliderCtrl*)pScrollBar;
	sl->GetRange(min, max);
	GetView ()->SetLinearity((float)sl->GetPos()/(max-min));
	CToolBar::OnHScroll(nSBCode, nPos, pScrollBar);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : GetView 
// Date   : November 1997
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
CIndexedTriangleView* CEditorToolBar::GetView ()
{
	CWnd *pDockBar = GetParent ();
	ASSERT (pDockBar);
	CFrameWnd *pFrameWnd = (CFrameWnd*)pDockBar->GetParent ();
	ASSERT (pFrameWnd);
	return (CIndexedTriangleView*)pFrameWnd->GetActiveView();
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : OnLButtonDblClk
// Date   : November 1997
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
void CEditorToolBar::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	//don't allow floating toolbar
	CWnd::OnLButtonDblClk(nFlags, point);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : OnLButtonDown
// Date   : November 1997
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
void CEditorToolBar::OnLButtonDown(UINT nFlags, CPoint point) 
{
	//don't allow floating toolbar
	CWnd::OnLButtonDown(nFlags, point);
}


#define MAX_CHARS			30
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : UpdateData 
// Date   : November 1997
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description :
// Author      : Stegaru Cristian
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
void CEditorToolBar::UpdateData (BOOL bGet/* = TRUE*/)
{
	CString csXNb, csYNb;
	if (bGet)
	{
		GetDlgItem (IDC_EDIT_XNB)->GetWindowText (csXNb);
		m_iXNb = atoi (csXNb);
		GetDlgItem (IDC_EDIT_YNB)->GetWindowText (csYNb);
		m_iYNb = atoi (csYNb);
		GetDlgItem (IDC_EDIT_NAME)->GetWindowText (m_csName);
//ANNECY Shaitan Correction 03/03/98 {
		while (((m_iXNb + 1)*(m_iYNb +1)) >= 2500)
		{
			if (m_iXNb > m_iYNb)
				m_iXNb--;
			else
				m_iYNb--;
		}
		UpdateData(FALSE);
//ENDANNECY Shaitan Correction }
	}
	else
	{
		itoa (m_iXNb, csXNb.GetBuffer (MAX_CHARS), 10);
		GetDlgItem (IDC_EDIT_XNB)->SetWindowText (csXNb);
		itoa (m_iYNb, csYNb.GetBuffer (MAX_CHARS), 10);
		GetDlgItem (IDC_EDIT_YNB)->SetWindowText (csYNb);
		GetDlgItem (IDC_EDIT_NAME)->SetWindowText (m_csName);
	}
}

