home *** CD-ROM | disk | FTP | other *** search
- #include "stdafx.h"
- #include "FileDropListCtrl.h"
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <afxdisp.h>
- #include <shlwapi.h>
- #include <afxpriv.h>
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- CFileDropListCtrl::CFileDropListCtrl()
- {
- m_dropMode = DL_ACCEPT_FILES | DL_ACCEPT_FOLDERS;
- m_bMustUninitOLE = FALSE;
- _AFX_THREAD_STATE* pState = AfxGetThreadState();
- if (!pState->m_bNeedTerm)
- {
- HRESULT hr = ::OleInitialize(NULL);
- if (FAILED(hr))
- {
- AfxMessageBox(_T("OLE initialization failed.\n\nMake sure that the OLE libraries are the correct version."));
- }
- else
- {
- m_bMustUninitOLE = TRUE;
- }
- }
- }
-
- CFileDropListCtrl::~CFileDropListCtrl()
- {
- if(m_bMustUninitOLE)
- {
- ::OleUninitialize();
- }
- }
-
- BEGIN_MESSAGE_MAP(CFileDropListCtrl, CListCtrl)
- //{{AFX_MSG_MAP(CFileDropListCtrl)
- ON_WM_DROPFILES()
- ON_WM_CREATE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
-
- int CFileDropListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListCtrl::OnCreate(lpCreateStruct) == -1)
- {
- return -1;
- }
- DragAcceptFiles(TRUE);
- return 0;
- }
-
- BOOL CFileDropListCtrl::SetDropMode(const UINT& dropMode)
- {
- m_dropMode = dropMode;
- return TRUE;
- }
-
-
- void CFileDropListCtrl::OnDropFiles(HDROP dropInfo)
- {
- UINT nNumFilesDropped = DragQueryFile(dropInfo, 0xFFFFFFFF, NULL, 0);
-
- TCHAR szFilename[MAX_PATH + 1];
- CString csPathname;
- CString csExpandedFilename;
-
- for (UINT nFile = 0 ; nFile < nNumFilesDropped; nFile++)
- {
- DragQueryFile(dropInfo, nFile, szFilename, MAX_PATH + 1);
- csPathname = szFilename;
- csExpandedFilename = ExpandShortcut(csPathname);
- if(!csExpandedFilename.IsEmpty())
- {
- csPathname = csExpandedFilename;
- }
- UINT iPathType = 0;
- if(ValidatePathname(csPathname, iPathType))
- {
- if (iPathType == DL_FOLDER_TYPE)
- {
- //////////////////////
- //csPathname += "\\";
- //////////////////////
- }
- InsertPathname(csPathname);
- }
- }
- DragFinish(dropInfo);
- }
-
- CString CFileDropListCtrl::ExpandShortcut(CString& csFilename) const
- {
- USES_CONVERSION;
- CString csExpandedFile;
-
- if(csFilename.IsEmpty())
- {
- ASSERT(FALSE);
- return csExpandedFile;
- }
-
- HRESULT hr;
- IShellLink* pIShellLink;
- hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
- IID_IShellLink, (LPVOID*) &pIShellLink);
- if (SUCCEEDED(hr))
- {
-
- IPersistFile* pIPersistFile;
- hr = pIShellLink->QueryInterface(IID_IPersistFile, (LPVOID*) &pIPersistFile);
- if (SUCCEEDED(hr))
- {
- hr = pIPersistFile->Load(T2COLE(csFilename), STGM_READ);
-
- if (SUCCEEDED(hr))
- {
- WIN32_FIND_DATA wfd;
- hr = pIShellLink->GetPath(csExpandedFile.GetBuffer(MAX_PATH),
- MAX_PATH,
- &wfd,
- SLGP_UNCPRIORITY);
- csExpandedFile.ReleaseBuffer(-1);
- }
- pIPersistFile->Release();
- }
- pIShellLink->Release();
- }
-
- return csExpandedFile;
- }
-
- BOOL CFileDropListCtrl::ValidatePathname(const CString& csPathname, UINT& iPathType) const
- {
- BOOL bValid = FALSE;
- struct _stat buf;
- int result = _tstat( csPathname, &buf );
-
- if( result == 0 )
- {
- if ((m_dropMode & DL_ACCEPT_FOLDERS) &&
- ((buf.st_mode & _S_IFDIR) == _S_IFDIR))
- {
- bValid = TRUE;
- iPathType = DL_FOLDER_TYPE;
- }
- else if ((m_dropMode & DL_ACCEPT_FILES) &&
- ((buf.st_mode & _S_IFREG) == _S_IFREG))
- {
- iPathType = DL_FILE_TYPE;
- bValid = TRUE;
- }
- }
-
- return bValid;
- }
-
- int CFileDropListCtrl::InsertPathname(const CString& csFilename)
- {
- if(!(m_dropMode & DL_ALLOW_DUPLICATES))
- {
- LVFINDINFO lvInfo;
- lvInfo.flags = LVFI_STRING;
- lvInfo.psz = csFilename;
-
- if(FindItem(&lvInfo, -1) != -1)
- {
- return -1;
- }
- }
-
- int result = InsertItem(0, _T(""));
- SetItemText(0, 1, csFilename);
- SetCheck(0);
-
- return result;
- }