root/trunk/GUIComponents/DirDialog.cpp

Revision 5, 3.2 kB (checked in by qbert, 3 years ago)

Initial ( and last :( ) commit

Line 
1 ///////////////////////////////////////////////////////////////////////////
2 // DirDialog.cpp: implementation of the CDirDialog class.
3 //
4 //////////////////////////////////////////////////////////////////////
5
6 #include "stdafx.h"
7 #include "DirDialog.h"
8 #include "shlobj.h"
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 //////////////////////////////////////////////////////////////////////
34 // Construction/Destruction
35 //////////////////////////////////////////////////////////////////////
36
37 CDirDialog::CDirDialog()
38 {
39 }
40
41 CDirDialog::~CDirDialog()
42 {
43 }
44
45 int CDirDialog::DoBrowse()
46 {
47   LPMALLOC pMalloc;
48   if (SHGetMalloc (&pMalloc)!= NOERROR)
49   {
50       return 0;
51   }
52
53   BROWSEINFO bInfo;
54   LPITEMIDLIST pidl;
55   ZeroMemory ( (PVOID) &bInfo,sizeof (BROWSEINFO));
56
57   if (!m_strInitDir.IsEmpty ())
58   {
59     OLECHAR       olePath[MAX_PATH];
60     ULONG         chEaten;
61     ULONG         dwAttributes;
62     HRESULT       hr;
63     LPSHELLFOLDER pDesktopFolder;
64     //
65     // Get a pointer to the Desktop's IShellFolder interface.
66     //
67     if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
68     {
69       //
70       // IShellFolder::ParseDisplayName requires the file name be in Unicode.
71       //
72       MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strInitDir.GetBuffer(MAX_PATH), -1,
73                           olePath, MAX_PATH);
74
75       m_strInitDir.ReleaseBuffer (-1);
76       //
77       // Convert the path to an ITEMIDLIST.
78       //
79       hr = pDesktopFolder->ParseDisplayName(NULL,
80                                             NULL,
81                                             olePath,
82                                             &chEaten,
83                                             &pidl,
84                                             &dwAttributes);
85       if (FAILED(hr))
86       {
87         pMalloc ->Free (pidl);
88         pMalloc ->Release ();
89         return 0;
90       }
91       bInfo.pidlRoot = pidl;
92
93     }
94   }
95   bInfo.hwndOwner = NULL;
96   bInfo.pszDisplayName = m_strPath.GetBuffer (MAX_PATH);
97   bInfo.lpszTitle = "Browse";
98   bInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
99   bInfo.lpfn = BrowseCtrlCallback;  // address of callback function
100   bInfo.lParam = (LPARAM)this;      // pass address of object to callback function
101
102   if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL)
103   {
104     return 0;
105   }
106   m_strPath.ReleaseBuffer();
107   m_iImageIndex = bInfo.iImage;
108
109   if (::SHGetPathFromIDList(pidl,m_strPath.GetBuffer(MAX_PATH)) == FALSE)
110   {
111     pMalloc ->Free(pidl);
112     pMalloc ->Release();
113     return 0;
114   }
115
116   m_strPath.ReleaseBuffer();
117
118   pMalloc ->Free(pidl);
119   pMalloc ->Release();
120
121   return 1;
122 }
Note: See TracBrowser for help on using the browser.