Use IFileOpenDialog instead of SHBrowseForFolder

pull/367/head
Wouter 4 years ago committed by Selva Nair
parent af7fc7d223
commit bf47b62c36

@ -27,6 +27,7 @@
#endif #endif
#include <windows.h> #include <windows.h>
#include <initguid.h>
#include <windowsx.h> #include <windowsx.h>
#include <prsht.h> #include <prsht.h>
#include <stdlib.h> #include <stdlib.h>
@ -34,6 +35,7 @@
#include <memory.h> #include <memory.h>
#include <shlobj.h> #include <shlobj.h>
#include <shlwapi.h> #include <shlwapi.h>
#include <combaseapi.h>
#include "options.h" #include "options.h"
#include "main.h" #include "main.h"
@ -441,37 +443,77 @@ GetConnByName(const WCHAR *name)
return NULL; return NULL;
} }
/* callback to set the initial value of folder browse selection */ static BOOL
static int CALLBACK BrowseFolder (const WCHAR* initial_path, WCHAR* selected_path, size_t selected_path_size)
BrowseCallback (HWND h, UINT msg, UNUSED LPARAM l, LPARAM data)
{ {
if (msg == BFFM_INITIALIZED) IFileOpenDialog* pfd;
SendMessage (h, BFFM_SETSELECTION, TRUE, data); HRESULT initResult, result, dialogResult;
return 0; // Create dialog
initResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(initResult))
{
return false;
} }
static BOOL result = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_ALL, &IID_IFileOpenDialog, (void**)&pfd);
BrowseFolder (const WCHAR *initial_path, WCHAR *selected_path) if (SUCCEEDED(result))
{
// Select folders, not files
DWORD dwOptions;
result = pfd->lpVtbl->GetOptions(pfd, &dwOptions);
if (SUCCEEDED(result))
{
dwOptions |= FOS_PICKFOLDERS;
result = pfd->lpVtbl->SetOptions(pfd, dwOptions);
}
// Set initial path
IShellItem* psi;
result = SHCreateItemFromParsingName(initial_path, NULL, &IID_IShellItem, (void**)&psi);
if (SUCCEEDED(result))
{ {
BOOL ret = false; pfd->lpVtbl->SetFolder(pfd, psi);
BROWSEINFO bi; psi->lpVtbl->Release(psi);
}
CLEAR(bi); // Show dialog and copy the selected file path if the user didn't cancel
bi.lpszTitle = L"Select folder..."; dialogResult = pfd->lpVtbl->Show(pfd, NULL);
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE; if (SUCCEEDED(dialogResult))
bi.lpfn = BrowseCallback; {
bi.lParam = (LPARAM) initial_path; IShellItem* psi;
LPOLESTR path = NULL;
PIDLIST_ABSOLUTE idlist = SHBrowseForFolder (&bi); result = pfd->lpVtbl->GetResult(pfd, &psi);
if(SUCCEEDED(result))
{
result = psi->lpVtbl->GetDisplayName(psi, SIGDN_FILESYSPATH, &path);
}
if (SUCCEEDED(result))
{
wcsncpy_s(selected_path, selected_path_size, path, wcslen(path));
CoTaskMemFree(path);
psi->lpVtbl->Release(psi);
}
else
{
dialogResult = E_FAIL;
}
}
// Cleanup
pfd->lpVtbl->Release(pfd);
}
if (idlist) if (initResult != RPC_E_CHANGED_MODE && SUCCEEDED(initResult))
{ {
ret = SHGetPathFromIDList (idlist, selected_path); CoUninitialize(); //All successful CoInitializeEx calls must be balanced by a corresponding CoUninitialize
CoTaskMemFree (idlist);
} }
return ret; return SUCCEEDED(dialogResult);
} }
static BOOL static BOOL
@ -577,13 +619,13 @@ AdvancedSettingsDlgProc (HWND hwndDlg, UINT msg, UNUSED WPARAM wParam, LPARAM lP
case ID_BTN_CONFIG_DIR: case ID_BTN_CONFIG_DIR:
GetDlgItemText (hwndDlg, ID_EDT_CONFIG_DIR, path, _countof(path)); GetDlgItemText (hwndDlg, ID_EDT_CONFIG_DIR, path, _countof(path));
if (BrowseFolder (path, path)) if (BrowseFolder (path, path, _countof(path)))
SetDlgItemText (hwndDlg, ID_EDT_CONFIG_DIR, path); SetDlgItemText (hwndDlg, ID_EDT_CONFIG_DIR, path);
break; break;
case ID_BTN_LOG_DIR: case ID_BTN_LOG_DIR:
GetDlgItemText (hwndDlg, ID_EDT_LOG_DIR, path, _countof(path)); GetDlgItemText (hwndDlg, ID_EDT_LOG_DIR, path, _countof(path));
if (BrowseFolder (path, path)) if (BrowseFolder (path, path, _countof(path)))
SetDlgItemText (hwndDlg, ID_EDT_LOG_DIR, path); SetDlgItemText (hwndDlg, ID_EDT_LOG_DIR, path);
break; break;
} }

Loading…
Cancel
Save