home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Shareware / Programare / magiccd / mcdbc111t.exe / %MAINDIR% / MFC / FileDropListCtrl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-03-08  |  3.9 KB  |  184 lines

  1. #include "stdafx.h"
  2. #include "FileDropListCtrl.h"
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <afxdisp.h>
  6. #include <shlwapi.h>
  7. #include <afxpriv.h>
  8.  
  9. #ifdef _DEBUG 
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. CFileDropListCtrl::CFileDropListCtrl()
  16. {
  17.     m_dropMode = DL_ACCEPT_FILES | DL_ACCEPT_FOLDERS;
  18.     m_bMustUninitOLE = FALSE;
  19.     _AFX_THREAD_STATE* pState = AfxGetThreadState();
  20.     if (!pState->m_bNeedTerm)
  21.     {
  22.         HRESULT hr = ::OleInitialize(NULL);
  23.         if (FAILED(hr))
  24.         {
  25.             AfxMessageBox(_T("OLE initialization failed.\n\nMake sure that the OLE libraries are the correct version."));
  26.         }
  27.         else
  28.         {
  29.             m_bMustUninitOLE = TRUE;
  30.         }
  31.     }
  32. }
  33.  
  34. CFileDropListCtrl::~CFileDropListCtrl()
  35. {
  36.     if(m_bMustUninitOLE)
  37.     {
  38.         ::OleUninitialize();
  39.     }
  40. }
  41.  
  42. BEGIN_MESSAGE_MAP(CFileDropListCtrl, CListCtrl)
  43.     //{{AFX_MSG_MAP(CFileDropListCtrl)
  44.     ON_WM_DROPFILES()
  45.     ON_WM_CREATE()
  46.     //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48.  
  49.  
  50. int CFileDropListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  51. {
  52.     if (CListCtrl::OnCreate(lpCreateStruct) == -1)
  53.     {
  54.         return -1;
  55.     }
  56.     DragAcceptFiles(TRUE);
  57.     return 0;
  58. }
  59.  
  60. BOOL CFileDropListCtrl::SetDropMode(const UINT& dropMode)
  61. {
  62.     m_dropMode = dropMode;
  63.     return TRUE;
  64. }
  65.  
  66.  
  67. void CFileDropListCtrl::OnDropFiles(HDROP dropInfo)
  68. {
  69.     UINT nNumFilesDropped = DragQueryFile(dropInfo, 0xFFFFFFFF, NULL, 0);
  70.  
  71.     TCHAR szFilename[MAX_PATH + 1];
  72.     CString csPathname;
  73.     CString csExpandedFilename;
  74.  
  75.     for (UINT nFile = 0 ; nFile < nNumFilesDropped; nFile++)
  76.     {
  77.         DragQueryFile(dropInfo, nFile, szFilename, MAX_PATH + 1);
  78.         csPathname = szFilename;
  79.         csExpandedFilename = ExpandShortcut(csPathname);
  80.         if(!csExpandedFilename.IsEmpty())
  81.         {
  82.             csPathname = csExpandedFilename;
  83.         }
  84.         UINT iPathType = 0;
  85.         if(ValidatePathname(csPathname, iPathType))
  86.         {
  87.             if (iPathType == DL_FOLDER_TYPE)
  88.             {
  89.                 //////////////////////
  90.                 //csPathname += "\\";
  91.                 //////////////////////
  92.             }
  93.             InsertPathname(csPathname);
  94.         }
  95.     }
  96.     DragFinish(dropInfo);
  97. }
  98.  
  99. CString CFileDropListCtrl::ExpandShortcut(CString& csFilename) const
  100. {
  101.     USES_CONVERSION;
  102.     CString csExpandedFile;
  103.     
  104.     if(csFilename.IsEmpty())
  105.     {
  106.         ASSERT(FALSE);
  107.         return csExpandedFile;
  108.     }
  109.  
  110.     HRESULT hr;
  111.     IShellLink* pIShellLink;
  112.     hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
  113.                             IID_IShellLink, (LPVOID*) &pIShellLink);
  114.     if (SUCCEEDED(hr))
  115.     {
  116.  
  117.         IPersistFile* pIPersistFile;
  118.         hr = pIShellLink->QueryInterface(IID_IPersistFile, (LPVOID*) &pIPersistFile);
  119.         if (SUCCEEDED(hr))
  120.         {
  121.             hr = pIPersistFile->Load(T2COLE(csFilename), STGM_READ);
  122.  
  123.             if (SUCCEEDED(hr))
  124.             {
  125.                 WIN32_FIND_DATA wfd;
  126.                 hr = pIShellLink->GetPath(csExpandedFile.GetBuffer(MAX_PATH),
  127.                                           MAX_PATH,
  128.                                           &wfd,
  129.                                           SLGP_UNCPRIORITY);
  130.                 csExpandedFile.ReleaseBuffer(-1);
  131.             }
  132.             pIPersistFile->Release();
  133.         }
  134.         pIShellLink->Release();
  135.     }
  136.  
  137.     return csExpandedFile;
  138. }
  139.  
  140. BOOL CFileDropListCtrl::ValidatePathname(const CString& csPathname, UINT& iPathType) const
  141. {
  142.     BOOL bValid = FALSE;
  143.     struct _stat buf;
  144.     int result = _tstat( csPathname, &buf );
  145.  
  146.     if( result == 0 )
  147.     {
  148.         if ((m_dropMode & DL_ACCEPT_FOLDERS) &&
  149.             ((buf.st_mode & _S_IFDIR) == _S_IFDIR)) 
  150.         {
  151.             bValid = TRUE;
  152.             iPathType = DL_FOLDER_TYPE;
  153.         } 
  154.         else if ((m_dropMode & DL_ACCEPT_FILES) &&
  155.                 ((buf.st_mode & _S_IFREG) == _S_IFREG)) 
  156.         {
  157.             iPathType = DL_FILE_TYPE;
  158.             bValid = TRUE;
  159.         } 
  160.     }
  161.  
  162.     return bValid;
  163. }
  164.  
  165. int CFileDropListCtrl::InsertPathname(const CString& csFilename)
  166. {
  167.     if(!(m_dropMode & DL_ALLOW_DUPLICATES))
  168.     {
  169.         LVFINDINFO lvInfo;
  170.         lvInfo.flags = LVFI_STRING;
  171.         lvInfo.psz = csFilename;
  172.  
  173.         if(FindItem(&lvInfo, -1) != -1)
  174.         {
  175.             return -1;
  176.         }
  177.     }
  178.  
  179.     int result = InsertItem(0, _T(""));
  180.     SetItemText(0, 1, csFilename);
  181.     SetCheck(0);
  182.  
  183.     return result;
  184. }