#include "stdafx.h"    
#include "DirectoryDialog.h"    
   
CDirectoryDialog::CDirectoryDialog()   
{   
    init();    
}   
   
CDirectoryDialog::CDirectoryDialog(CString strIniKeyName)   
{   
    init();   
    m_strIniKeyName = strIniKeyName;   
}   
   
CDirectoryDialog::~CDirectoryDialog()   
{   
   
}   
   
int CALLBACK CDirectoryDialog::BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)   
{   
    switch(uMsg)   
    {          
    case BFFM_INITIALIZED:       
        ::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,lpData);   
        return 1 ;   
    case BFFM_SELCHANGED:   
        {   
            char szDir[1000];   
            if (SHGetPathFromIDList((LPITEMIDLIST) lParam ,szDir))   
            {    
                SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)(LPCTSTR)GetShortDir(szDir));   
            }              
        }   
        return 1 ;   
    }   
    return 1 ;   
}   
    
CString CDirectoryDialog::ShowDirectoryTree(CString strIniDir)   
{   
    CString strPath;   
    char pszTemp[1000] = {""};   
    char pszTitle[1000] = {""};   
       
    if(NULL == m_hwndOwner)     m_hwndOwner = AfxGetMainWnd()->GetSafeHwnd();   
    if(m_strTitle.IsEmpty())    m_strTitle = GetDefaultTitle();
    BROWSEINFOA bi ={0};       
    char pszDisplayName[MAX_PATH] = "\0";
    bi.hwndOwner=m_hwndOwner;      
    bi.pszDisplayName = pszDisplayName;    
    bi.ulFlags   = GetStyle(m_ulFlags);    
    bi.lpszTitle = m_strTitle ;   
    bi.lpfn      = BrowseCallbackProc ;   
   
    if(strIniDir.IsEmpty())   
        strcpy(pszTemp,(LPCTSTR)(AfxGetApp()->GetProfileString(m_strIniAppName,m_strIniKeyName,"")));   
    else
        strcpy(pszTemp,strIniDir);   
    
    bi.lParam   = (int)pszTemp ;       
    LPITEMIDLIST lpIL = SHBrowseForFolder(&bi);    
    if(lpIL)
        SHGetPathFromIDList(lpIL,bi.pszDisplayName);   
    else
        return "";     
    
    strPath = bi.pszDisplayName;
    if(strPath.Right(1)!='\\')      
        strPath += "\\";
    SetDefaultDirectory(strPath);
    return strPath ;   
}   
   
CString CDirectoryDialog::GetDefaultDirectory()   
{   
    if(m_strIniAppName.IsEmpty() || m_strIniKeyName.IsEmpty())   
        return "";   
       
    return AfxGetApp()->GetProfileString(m_strIniAppName,m_strIniKeyName,"");   
}   
   
void CDirectoryDialog::SetDefaultDirectory(CString strDefaultDirectory)   
{    
    if (m_strIniAppName.IsEmpty() || 
        m_strIniKeyName.IsEmpty())   
    {   
        return ;   
    }   
       
    AfxGetApp()->WriteProfileString(m_strIniAppName,m_strIniKeyName,strDefaultDirectory);   
}   
   
void CDirectoryDialog::init()   
{          
    m_hwndOwner = NULL;       
    m_strTitle.Empty();    
    m_ulFlags  = SD_DIR;        
           
    m_strIniAppName = "文件夹初始路径";   
    m_strIniKeyName.Empty();   
}   
      
CString CDirectoryDialog::GetShortDir(CString strDir,long nMaxLength/*=45*/)   
{   
    ASSERT(nMaxLength > 5);   
    if(strDir.GetLength() < nMaxLength)  
        return strDir ;
    CString strShortDir;
    if(':'==strDir[1] && '\\'==strDir[2])   
        strShortDir = strDir.Left(3);   
    CString strLastDir;
    int nPos = strDir.ReverseFind('\\');   
    if(nPos != -1)
        strLastDir = strDir.Mid(nPos);   
    if(strLastDir > nMaxLength - 5 )   
    {            
        strLastDir = strLastDir.Mid(strLastDir.GetLength() - (nMaxLength - 5));   
        if('\\' != strLastDir[0])   strLastDir = '\\' + strLastDir ;   
    }
    int nSpace = nMaxLength - strShortDir.GetLength() - strLastDir.GetLength() ;   
    if(nSpace > 6)       nSpace = 6;   
    for(int i = 0 ; i< nSpace ; i++)     strShortDir +=".";   
    strShortDir += strLastDir ;   
    strLastDir.Empty();   
    return strShortDir ;       
}   
    
CString CDirectoryDialog::ShowDirectoryTree(CWnd *pBuddyWnd)   
{   
    if(NULL == pBuddyWnd)   return "" ;   
    CString strDir ;   
       
    if(NULL == m_hwndOwner)
    {   
        if(NULL != pBuddyWnd->GetParent())   
            m_hwndOwner = pBuddyWnd->GetParent()->GetSafeHwnd();   
    }          
    if(m_strIniKeyName.IsEmpty())   pBuddyWnd->GetWindowText(strDir);
    strDir = ShowDirectoryTree(strDir);        
    if(!strDir.IsEmpty())   
    {
        pBuddyWnd->SetWindowText(strDir);   
        if(NULL != pBuddyWnd->GetParent())   
            pBuddyWnd->GetParent()->UpdateData(true);   
    }          
    return strDir ;   
}   
   
CString CDirectoryDialog::GetDefaultTitle()   
{   
    CString strTitle ;   
    if(m_strIniKeyName.IsEmpty())       strTitle = "选择目录:";     
    else   
    {   
        char pszTitle[255];   
        sprintf(pszTitle,"请为%s选择合适的目录:",m_strIniKeyName);   
        strTitle =  pszTitle ;   
    }   
    return strTitle ;   
}   
   
UINT CDirectoryDialog::GetStyle(enum Flag flag)   
{   
    switch(flag)   
    {   
    case SD_DIR:      
        return BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT ;
    case SD_DIR_FILE :     
        return BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT|BIF_BROWSEINCLUDEFILES;   
    }   
    return BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;
}  