/*
	$Id: configure.cpp,v 1.92 2004/02/20 13:23:46 sphair Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/
// Configure.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Configure.h"
#include "configure_wizard.h"
#include "workspace_generator_msvc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CConfigureApp

BEGIN_MESSAGE_MAP(CConfigureApp, CWinApp)
	//{{AFX_MSG_MAP(CConfigureApp)
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConfigureApp construction

CConfigureApp::CConfigureApp()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CConfigureApp object

CConfigureApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CConfigureApp initialization

BOOL CConfigureApp::InitInstance()
{
	// Standard initialization
/*
#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif
*/
	SetRegistryKey("ClanSoft");

	CConfigureWizard wizard;
	m_pMainWnd = &wizard;

	int nResponse = wizard.DoModal();
	if (nResponse == ID_WIZFINISH)
	{
		Workspace workspace = create_workspace(wizard);

//		if(wizard.m_Page2.m_compilertarget == 0)
		{
			WorkspaceGenerator_MSVC6 generator6;
			generator6.write(workspace);
		}
//		else if(wizard.m_Page2.m_compilertarget == 1)
		{
			WorkspaceGenerator_MSVC7 generator7;
			generator7.write(workspace);
		}

		/* TODO: Update the following registry keys:

		\\LOCAL MACHINE\Software\Microsoft\DevStudio\6.0\Build System\Components\Platforms\Win32 (x86)\Directories\Include Dirs
		\\LOCAL MACHINE\Software\Microsoft\DevStudio\6.0\Build System\Components\Platforms\Win32 (x86)\Directories\Library Dirs
		\\LOCAL MACHINE\Software\Microsoft\DevStudio\6.0\Build System\Components\Platforms\Win32 (x86)\Directories\Path Dirs
		\\LOCAL MACHINE\Software\Microsoft\DevStudio\6.0\Build System\Components\Platforms\Win32 (x86)\Directories\Source Dirs
		*/
	}
	else if (nResponse == IDCANCEL)
	{
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// Workspace creation:

Workspace CConfigureApp::create_workspace(CConfigureWizard &wizard)
{
	Workspace workspace;
	workspace.input_lib_dir = std::string(wizard.m_Page4.m_input_lib);
	workspace.input_include_dir = std::string(wizard.m_Page4.m_input_include);
	workspace.output_include_dir = std::string(wizard.m_Page4.m_output_include);
	workspace.output_lib_dir = std::string(wizard.m_Page4.m_output_lib);

	// Write all library project files:
	std::list<std::string> libs_list_shared;
	std::list<std::string> libs_list_release;
	std::list<std::string> libs_list_debug;
	std::list<std::string> defines_list;

	if (wizard.m_Page3.m_use_opengl) defines_list.push_back("USE_OPENGL");
	if (wizard.m_Page3.m_use_network) defines_list.push_back("USE_NETWORK");
	if (wizard.m_Page3.m_use_sound) defines_list.push_back("USE_CLANSOUND");
	
	defines_list.push_back("DIRECTINPUT_VERSION=0x0800");
	
	Project clanCore(
		"Core",
		"clanCore",
		"core.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanSignals(
		"Signals",
		"clanSignals",
		"signals.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanApp(
		"Application",
		"clanApp",
		"application.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanNetwork(
		"Network",
		"clanNetwork",
		"network.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanDisplay(
		"Display",
		"clanDisplay",
		"display.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanSound(
		"Sound",
		"clanSound",
		"sound.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanGL(
		"GL",
		"clanGL",
		"gl.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanSDL(
		"SDL",
		"clanSDL",
		"sdl.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanGUI(
		"GUI",
		"clanGUI",
		"gui.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanGUIStyleSilver(
		"GUIStyleSilver",
		"clanGUIStyleSilver",
		"guistylesilver.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanVorbis(
		"Vorbis",
		"clanVorbis",
		"vorbis.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	Project clanMikMod(
		"MikMod",
		"clanMikMod",
		"mikmod.h",
		libs_list_shared,
		libs_list_release,
		libs_list_debug,
		defines_list);

	// Add projects to workspace:
	workspace.projects.push_back(clanSignals);
	workspace.projects.push_back(clanCore);
	workspace.projects.push_back(clanApp);
	workspace.projects.push_back(clanNetwork);
	workspace.projects.push_back(clanDisplay);
	workspace.projects.push_back(clanSound);
	workspace.projects.push_back(clanGL);
	workspace.projects.push_back(clanSDL);
	workspace.projects.push_back(clanGUI);
	workspace.projects.push_back(clanGUIStyleSilver);
	workspace.projects.push_back(clanVorbis);
	workspace.projects.push_back(clanMikMod);

	return workspace;
}

/////////////////////////////////////////////////////////////////////////////
// Browse helper function:

BOOL BrowseForFolder(HWND hOwner, char* szTitle, char* szRetval)
{
	BROWSEINFO		info;
	LPITEMIDLIST	itemidlist;
	char			szDirectory[_MAX_PATH];
	LPMALLOC		pMalloc;

	memset(szDirectory, '\0', _MAX_PATH);

	if (::SHGetMalloc(&pMalloc) == NOERROR)
	{
		info.hwndOwner = hOwner;
		info.pidlRoot = NULL;
		info.pszDisplayName = szDirectory;
		info.lpszTitle = szTitle;
		info.ulFlags = 0;
		info.lpfn = NULL;

		itemidlist = SHBrowseForFolder(&info);
		if (itemidlist != NULL)
		{
			SHGetPathFromIDList(itemidlist, szRetval);
			pMalloc->Free(itemidlist);
			pMalloc->Release();
			return TRUE;
		}
		else // User clicked Cancel
		{
			pMalloc->Release();
			return FALSE;
		}
	}
	else
		return FALSE;
}

void CConfigureApp::WinHelp(DWORD dwData, UINT nCmd) 
{
	ShellExecute(
		NULL,
		NULL,
		"notepad.exe",
		"INSTALL.WIN32",
		NULL,
		SW_SHOWNORMAL);
}
