// AnimGenerate.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "AnimGenerate.h"
#include "AnimGenerateDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAnimGenerateApp

BEGIN_MESSAGE_MAP(CAnimGenerateApp, CWinApp)
	//{{AFX_MSG_MAP(CAnimGenerateApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAnimGenerateApp construction

CAnimGenerateApp::CAnimGenerateApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CAnimGenerateApp object

CAnimGenerateApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CAnimGenerateApp initialization

BOOL CAnimGenerateApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CAnimGenerateDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

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

int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	RECT rect;
	//
	//Center the browse dialog on the screen...
	//
	switch (uMsg)
	{
	case BFFM_INITIALIZED:
		{
			char szPath[MAX_PATH];
			GetCurrentDirectory(MAX_PATH,szPath);
			SendMessage(hWnd,BFFM_SETSELECTION,TRUE,(LPARAM)szPath);
	
			if (GetWindowRect(hWnd, &rect))
			{
				SetWindowPos(hWnd, 
							 HWND_TOP,
							 (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) >> 1,
							 (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) >> 1,
							 0,
							 0,
							 SWP_NOSIZE);
			}
		}
	}
	return 0;
}

CString CAnimGenerateApp::BrowseDirectory(char *,CWnd *pWnd)
{
	CString str;
	TCHAR        szBuffer[MAX_PATH];
	LPITEMIDLIST pidlBrowse,pidlRoot;
	BROWSEINFO   bi;

	LPMALLOC lpMalloc;
	SHGetMalloc(&lpMalloc);
	ASSERT(lpMalloc);
	bi.hwndOwner = pWnd->GetSafeHwnd();
	// Get the ID for the "My Computer" root folder
	SHGetSpecialFolderLocation(pWnd->GetSafeHwnd(),CSIDL_DRIVES,&pidlRoot);

	bi.pidlRoot  = pidlRoot;
	bi.pszDisplayName = szBuffer;

	// Localize the descriptive text
	bi.lpszTitle = "Select Destination Folder";

	// BIF_EDITBOX and BIF_VALIDATE are for versions 4.71 and later, they
	// might be ignored by older versions
	bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT | BIF_EDITBOX;// | BIF_USENEWUI;// | BIF_VALIDATE;
	bi.lParam = 0;
	bi.iImage = NULL;
	bi.lpfn = BrowseCallbackProc;
	//
	//Call the Windows shell folder browsing routine...
	//
	pidlBrowse = SHBrowseForFolder(&bi);
	if (NULL != pidlBrowse)
	{
		if (SHGetPathFromIDList(pidlBrowse, szBuffer))
		{
			if ('\\' == szBuffer[lstrlen(szBuffer)-1])
				szBuffer[lstrlen(szBuffer)-1] = '\0';
			str = CString(szBuffer);
		}
		else
			str = "";
		//
		//Free up shell memory used...
		//
		lpMalloc->Free(pidlBrowse);
	}
	else
		str = "";
	lpMalloc->Free(pidlRoot);
	lpMalloc->Release();
	return str;
}
