// FoldersDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dslavsplit.h"
#include "FoldersDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFoldersDlg dialog


CFoldersDlg::CFoldersDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFoldersDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFoldersDlg)
	m_pathContents = _T("");
	m_pathVideos = _T("");
	//}}AFX_DATA_INIT
}


void CFoldersDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFoldersDlg)
	DDX_Control(pDX, IDC_BUTTON_VIDEO_BROWSE, m_ctrlBrowseVideos);
	DDX_Control(pDX, IDC_BUTTON_CONTENTS_BROWSE, m_ctrlBrowseContents);
	DDX_Text(pDX, IDC_PATH_CONTENTS_FILES, m_pathContents);
	DDX_Text(pDX, IDC_PATH_VIDEO_FILES, m_pathVideos);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFoldersDlg, CDialog)
	//{{AFX_MSG_MAP(CFoldersDlg)
	ON_BN_CLICKED(IDC_BUTTON_CONTENTS_BROWSE, OnButtonContentsBrowse)
	ON_BN_CLICKED(IDC_BUTTON_VIDEO_BROWSE, OnButtonVideoBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFoldersDlg message handlers

// Browse for Contents files folder - user can create new folder if desired
void CFoldersDlg::OnButtonContentsBrowse() 
{
	BROWSEINFO bi;
	memset((LPVOID)&bi, 0, sizeof(bi));
	TCHAR szDisplayName[_MAX_PATH];
	szDisplayName[0] = '\0';
	bi.hwndOwner = GetSafeHwnd();
	bi.pidlRoot = NULL;
	bi.pszDisplayName = szDisplayName;
	bi.lpszTitle = _T("Pick a folder used for Contents file storage:");
	bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI; // BIF_USENEWUI for new folder

	LPITEMIDLIST pIIL = ::SHBrowseForFolder(&bi);

	TCHAR szInitialDir[_MAX_PATH];

	BOOL bRet = ::SHGetPathFromIDList(pIIL, (LPWSTR)&szInitialDir);
	
	if(bRet)
	{
		CString str = szInitialDir;
		if(!str.IsEmpty())
		{
			// Set member.
			m_pathContents = str;
			UpdateData(FALSE);
		}

		LPMALLOC pMalloc;
		HRESULT hr = SHGetMalloc(&pMalloc);
		pMalloc->Free(pIIL);
		pMalloc->Release();
	}	
}

// Browse for Video files folder - user can create new folder if desired.
void CFoldersDlg::OnButtonVideoBrowse() 
{
	BROWSEINFO bi;
	memset((LPVOID)&bi, 0, sizeof(bi));
	TCHAR szDisplayName[_MAX_PATH];
	szDisplayName[0] = '\0';
	bi.hwndOwner = GetSafeHwnd();
	bi.pidlRoot = NULL;
	bi.pszDisplayName = szDisplayName;
	bi.lpszTitle = _T("Pick a folder used for Video file storage:");
	bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI; // BIF_USENEWUI for new folder

	LPITEMIDLIST pIIL = ::SHBrowseForFolder(&bi);

	TCHAR szInitialDir[_MAX_PATH];

	BOOL bRet = ::SHGetPathFromIDList(pIIL, (LPWSTR)&szInitialDir);
	
	if(bRet)
	{
		CString str = szInitialDir;
		if(!str.IsEmpty())
		{
			// Set member.
			m_pathVideos = str;
			UpdateData(FALSE);
		}

		LPMALLOC pMalloc;
		HRESULT hr = SHGetMalloc(&pMalloc);
		pMalloc->Free(pIIL);
		pMalloc->Release();
	}	
}

BOOL CFoldersDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// Set initial folder paths with caller supplied data.
	//UpdateData(FALSE); framework does	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


