home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 8090 / ModelEdit.7z / DirDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-03-08  |  2.9 KB  |  115 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. // DirDialog.cpp: implementation of the CDirDialog class.
  3. //
  4. //////////////////////////////////////////////////////////////////////
  5.  
  6. #include "stdafx.h"
  7. #include "DirDialog.h"
  8.  
  9.  
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char THIS_FILE[]=__FILE__;
  13. #define new DEBUG_NEW
  14. #endif
  15.  
  16. // Callback function called by SHBrowseForFolder's browse control
  17. // after initialization and when selection changes
  18. static int __stdcall BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
  19. {
  20.   CDirDialog* pDirDialogObj = (CDirDialog*)lpData;
  21.  
  22.   if (uMsg == BFFM_INITIALIZED && !pDirDialogObj->m_strSelDir.IsEmpty())
  23.   {
  24.     ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPCTSTR)(pDirDialogObj->m_strSelDir));
  25.   }
  26.   else // uMsg == BFFM_SELCHANGED
  27.   {
  28.   }
  29.  
  30.   return 0;
  31. }
  32.  
  33. CDirDialog::CDirDialog()
  34. {
  35.     m_strTitle = "Select a folder";
  36. }
  37.  
  38. CDirDialog::~CDirDialog()
  39. {
  40. }
  41.  
  42. bool CDirDialog::DoBrowse()
  43. {
  44.   LPMALLOC pMalloc;
  45.   if( SHGetMalloc( &pMalloc ) != NOERROR )
  46.     return false;
  47.  
  48.   BROWSEINFO bInfo;
  49.   LPITEMIDLIST pidl;
  50.   ZeroMemory( (PVOID)&bInfo, sizeof( BROWSEINFO ) );
  51.  
  52.   if( !m_strInitDir.IsEmpty() )
  53.   {
  54.     OLECHAR       olePath[MAX_PATH];
  55.     ULONG         chEaten;
  56.     ULONG         dwAttributes;
  57.     HRESULT       hr;
  58.     LPSHELLFOLDER pDesktopFolder;
  59.     // 
  60.     // Get a pointer to the Desktop's IShellFolder interface. 
  61.     //
  62.     if( SUCCEEDED( SHGetDesktopFolder( &pDesktopFolder ) ) )
  63.     {
  64.       //
  65.       // IShellFolder::ParseDisplayName requires the file name be in Unicode.
  66.       //
  67.       MultiByteToWideChar( 
  68.                 CP_ACP, MB_PRECOMPOSED, 
  69.                 m_strInitDir.GetBuffer(MAX_PATH), -1,
  70.         olePath, MAX_PATH );
  71.       m_strInitDir.ReleaseBuffer( -1 );
  72.       //
  73.       // Convert the path to an ITEMIDLIST.
  74.       //
  75.       hr = pDesktopFolder->ParseDisplayName(
  76.                 NULL, NULL, olePath, &chEaten, &pidl, &dwAttributes );
  77.       if( FAILED( hr ) )
  78.       {
  79.         pMalloc->Free( pidl );
  80.         pMalloc->Release();
  81.             return false;
  82.       }
  83.       bInfo.pidlRoot = pidl;
  84.     }
  85.   }
  86.   bInfo.hwndOwner = NULL;
  87.   bInfo.pszDisplayName = m_strPath.GetBuffer( MAX_PATH );
  88.   bInfo.lpszTitle = m_strTitle;
  89.   bInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS | BIF_EDITBOX ;
  90.   
  91.   bInfo.lpfn = BrowseCtrlCallback;  // address of callback function
  92.   bInfo.lParam = (LPARAM)this;      // pass address of object to callback function
  93.  
  94.   
  95.   if( ( pidl = ::SHBrowseForFolder( &bInfo ) ) == NULL )
  96.     return false;
  97.  
  98.   m_strPath.ReleaseBuffer();
  99.   m_iImageIndex = bInfo.iImage;
  100.  
  101.   if( ::SHGetPathFromIDList( pidl, m_strPath.GetBuffer( MAX_PATH ) ) == FALSE )
  102.   {
  103.     pMalloc->Free( pidl );
  104.     pMalloc->Release();
  105.     return false;
  106.   }
  107.  
  108.   m_strPath.ReleaseBuffer();
  109.  
  110.   pMalloc->Free( pidl );
  111.   pMalloc->Release();
  112.  
  113.   return true;
  114. }
  115.