// ObjWindow.cpp : implementation file
//

#include "stdafx.h"
#include "MW4GameEd.h"
#include "ObjWindow.h"
#include "InstanceWindow.h"
#include "EditEntityDlg.h"

/////////////////////////////////////////////////////////////////////////////
// CObjWindow dialog


CObjWindow::CObjWindow(CWnd* pParent /*=NULL*/)
	: CDialog(CObjWindow::IDD, pParent)
{
	//{{AFX_DATA_INIT(CObjWindow)
	//}}AFX_DATA_INIT
	curSort	= SORT_TYPE;
	mechTab = NULL;
	buildingTab = NULL;
	vehicleTab = NULL;
	miscTab = NULL;
}

CObjWindow::~CObjWindow()
{
	if (mechTab)
	{
		if (mechTab->pszText)
			delete [] mechTab->pszText;
		Unregister_Pointer(mechTab);
		delete mechTab;
	}
	if (buildingTab)
	{
		if (buildingTab->pszText)
			delete [] buildingTab->pszText;
		Unregister_Pointer(buildingTab);
		delete mechTab;
	}
	if (vehicleTab)
	{
		if (vehicleTab->pszText)
			delete [] vehicleTab->pszText;
		Unregister_Pointer(vehicleTab);
		delete mechTab;
	}
	if (miscTab)
	{
		if (miscTab->pszText)
			delete [] miscTab->pszText;
		Unregister_Pointer(miscTab);
		delete mechTab;
	}
}

void CObjWindow::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CObjWindow)
	DDX_Control(pDX, IDC_OBJECTTAB, m_TabCtrl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CObjWindow, CDialog)
	//{{AFX_MSG_MAP(CObjWindow)
	ON_BN_CLICKED(IDC_INSTANCEALINGMENTBTN, OnInstancealingmentbtn)
	ON_BN_CLICKED(IDC_INSTANCETYPEBTN, OnInstancetypebtn)
	ON_NOTIFY(TCN_SELCHANGE, IDC_OBJECTTAB, OnSelchangeObjecttab)
	ON_WM_CREATE()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CObjWindow message handlers

void CObjWindow::OnInstancealingmentbtn() 
{
	curSort	= SORT_ALIGNMENT;
	((CInstanceWindow*)GetParent())->FillTabBox();
}

void CObjWindow::OnInstancetypebtn() 
{
	curSort	= SORT_TYPE;
	((CInstanceWindow*)GetParent())->FillTabBox();
}

void CObjWindow::OnSelchangeObjecttab(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NMHDR  lParam;

    lParam.hwndFrom = m_hWnd;
    lParam.idFrom = IDC_OBJECTTAB; 
    lParam.code = m_TabCtrl.GetCurSel();

	GetParent()->SendMessage(WM_NOTIFY,WPARAM(IDC_OBJECTTAB),LPARAM(&lParam));
	
	*pResult = 0;
}

int CObjWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;

	mechTab = new TCITEM;
	Register_Pointer(mechTab);

	mechTab->mask = TCIF_TEXT;
	mechTab->cchTextMax = strlen("Mechs");		// Localize
	mechTab->pszText = new char[mechTab->cchTextMax + 1];
	strcpy(mechTab->pszText,"Mechs");			// Localize

	buildingTab = new TCITEM;
	Register_Pointer(buildingTab);

	buildingTab->mask = TCIF_TEXT;
	buildingTab->cchTextMax = strlen("Buildings");		// Localize
	buildingTab->pszText = new char[buildingTab->cchTextMax + 1];
	strcpy(buildingTab->pszText,"Buildings");			// Localize

	vehicleTab = new TCITEM;
	Register_Pointer(vehicleTab);

	vehicleTab->mask = TCIF_TEXT;
	vehicleTab->cchTextMax = strlen("Vehicles");		// Localize
	vehicleTab->pszText = new char[vehicleTab->cchTextMax + 1];
	strcpy(vehicleTab->pszText,"Vehicles");			// Localize

	miscTab = new TCITEM;
	Register_Pointer(miscTab);

	miscTab->mask = TCIF_TEXT;
	miscTab->cchTextMax = strlen("Misc");		// Localize
	miscTab->pszText = new char[miscTab->cchTextMax + 1];
	strcpy(miscTab->pszText,"Misc");			// Localize

	return 0;
}

void CObjWindow::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	GetParent()->SendMessage(WM_LBUTTONDBLCLK,WPARAM(nFlags),MAKELPARAM(point.x,point.y));	
	CDialog::OnLButtonDblClk(nFlags, point);
}

void CObjWindow::OnRButtonDown(UINT nFlags, CPoint point) 
{
	GetParent()->SendMessage(WM_RBUTTONDOWN,WPARAM(nFlags),MAKELPARAM(point.x,point.y));
	CDialog::OnRButtonDown(nFlags, point);
}

void CObjWindow::OnLButtonUp(UINT nFlags, CPoint point) 
{
	GetParent()->SendMessage(WM_LBUTTONUP,WPARAM(nFlags),MAKELPARAM(point.x,point.y));		
	CDialog::OnLButtonUp(nFlags, point);
}

void CObjWindow::OnOK() 
{
	// someone in MFC thought it would be a good idea to have this function get called everytime
	// the ENTER key is pressed.

	return;
}

void CObjWindow::OnCancel() 
{
	// someone in MFC thought it would be a good idea to have this function get called everytime
	// the ESCAPE key is pressed.

	return;
}

void CObjWindow::OnRButtonDblClk(UINT nFlags, CPoint point) 
{
	GetParent()->SendMessage(WM_RBUTTONDBLCLK,WPARAM(nFlags),MAKELPARAM(point.x,point.y));
	CDialog::OnRButtonDblClk(nFlags, point);
}
