//***************************************************************************************
static CBCGPFileDialog* GetBCGFileDlg (HWND hwdParent)
{
	CFileDialog* pDlg = (CFileDialog*)CWnd::FromHandle (hwdParent);
	ASSERT (pDlg != NULL);

	CBCGPFileDialog* pFD = (CBCGPFileDialog*) pDlg->GetDlgItem(0);
	ASSERT (pFD != NULL);

	return pFD;
}
/* WindowProcDiag
 * ----------------------------------------------------------------------------
 */
LRESULT CALLBACK WindowProcDiag(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == WM_COMMAND)
    {
        if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == IDOK)
        {
            CFileDialog *pDlg = (CFileDialog *)CWnd::FromHandle(hwnd);
            if (pDlg == NULL)
            {
                return CallWindowProc(CFolderDialog::m_fpWndProcDiag, hwnd, message, wParam, lParam);
            }

            CString sPath;
            pDlg->GetDlgItem(edt1)->GetWindowText(sPath);

            // If the user has entered something like "c:\\windows\\\\system"
            // this is invalid but _access returns valid if this string is passed to
            // it. So before giving it to _access validate it.
            for (int i = 1; i < sPath.GetLength() - 1; i++)
            {
                if (sPath[i] == '\\' && sPath[i+1] == '\\')
                {
                    CString sMsg;
                    sMsg.Format(_T("The selected folder '%s' is invalid. Please select another one"), sPath);

                    MessageBox(hwnd, sMsg, ((CFolderDialog*)pDlg->GetDlgItem(0))->m_sMsgTitle, MB_OK | MB_ICONHAND);
                    return NULL;
                }
            }

            // If user has entered a trailing backslash remove it
            // e.g. "c:\\windows\\system\\" --> "c:\\windows\\system"
            if (sPath[sPath.GetLength() - 1] == '\\')
                sPath = sPath.Left (sPath.GetLength() - 1);

            //If path does not exist
            if(_access(sPath, 00) != 0)
            {
                CString sTempPath(sPath);
                sTempPath += _T('\\');

                CString sMsg;
                sMsg.Format(_T("The selected path '%s' does not exist.\nDo you want to create it?"), sTempPath);

                if(MessageBox(hwnd, sMsg, ((CFolderDialog*)pDlg->GetDlgItem(0))->m_sMsgTitle, MB_YESNO | MB_ICONQUESTION) == IDYES)
                {
                    UINT uPos = 0;

                    if (! ((CFolderDialog*)pDlg->GetDlgItem(0))->CreateDirTree(sTempPath, uPos, TRUE))
                    {
                        sMsg.Format(_T("Could not create folder: %s"), sTempPath);

                        MessageBox(hwnd, sMsg, ((CFolderDialog*)pDlg->GetDlgItem(0))->m_sMsgTitle, MB_OK | MB_ICONHAND);
                        return NULL;
                    }
                }
                else
                {
                    return NULL;
                }
            }
            else
            {
                TCHAR szCurrent[MAX_PATH];
                GetCurrentDirectory (MAX_PATH, szCurrent);

                HWND hwndTest = GetFocus() ;
                if (hwndTest == pDlg->GetDlgItem(edt1)->m_hWnd && stricmp(sPath, szCurrent) != 0)
                {
                    // OK was caused by edit control, don't close the dialog.
                    return CallWindowProc(CFolderDialog::m_fpWndProcDiag, hwnd, message, wParam, lParam);
                }
            }

            ((CFolderDialog*)pDlg->GetDlgItem(0))->m_sSelPath =  sPath;
            ((CFolderDialog*)pDlg->GetDlgItem(0))->m_sSelPath += _T('\\');

            pDlg->EndDialog(IDOK);
            return NULL;
        }
    }

    return CallWindowProc(CFolderDialog::m_fpWndProcDiag, hwnd, message, wParam, lParam);
}