Skip to content

Commit

Permalink
Folder browser (like when clicking Browse...): Preinitialize with the…
Browse files Browse the repository at this point in the history
… current folder.
  • Loading branch information
hrydgard committed Jan 25, 2024
1 parent 9218efd commit bc92226
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 27 deletions.
4 changes: 4 additions & 0 deletions Common/System/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,7 @@ void System_CreateGameShortcut(const Path &path, const std::string &title) {
void System_ShowFileInFolder(const Path &path) {
g_requestManager.MakeSystemRequest(SystemRequestType::SHOW_FILE_IN_FOLDER, NO_REQUESTER_TOKEN, nullptr, nullptr, path.ToString(), "", 0);
}

void System_BrowseForFolder(RequesterToken token, const std::string &title, const Path &initialPath, RequestCallback callback, RequestFailedCallback failedCallback) {
g_requestManager.MakeSystemRequest(SystemRequestType::BROWSE_FOR_FOLDER, token, callback, failedCallback, title, initialPath.ToCString(), 0);
}
4 changes: 1 addition & 3 deletions Common/System/Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ inline void System_BrowseForFile(RequesterToken token, const std::string &title,
g_requestManager.MakeSystemRequest(SystemRequestType::BROWSE_FOR_FILE, token, callback, failedCallback, title, "", (int)type);
}

inline void System_BrowseForFolder(RequesterToken token, const std::string &title, RequestCallback callback, RequestFailedCallback failedCallback = nullptr) {
g_requestManager.MakeSystemRequest(SystemRequestType::BROWSE_FOR_FOLDER, token, callback, failedCallback, title, "", 0);
}
void System_BrowseForFolder(RequesterToken token, const std::string &title, const Path &initialPath, RequestCallback callback, RequestFailedCallback failedCallback = nullptr);

// The returned string is username + '\n' + password.
inline void System_AskUsernamePassword(RequesterToken token, const std::string &title, RequestCallback callback, RequestFailedCallback failedCallback = nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion Common/UI/PopupScreens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ std::string FileChooserChoice::ValueText() const {
FolderChooserChoice::FolderChooserChoice(RequesterToken token, std::string *value, const std::string &text, LayoutParams *layoutParams)
: AbstractChoiceWithValueDisplay(text, layoutParams), value_(value), token_(token) {
OnClick.Add([=](UI::EventParams &) {
System_BrowseForFolder(token_, text_, [=](const std::string &returnValue, int) {
System_BrowseForFolder(token_, text_, Path(*value), [=](const std::string &returnValue, int) {
if (*value_ != returnValue) {
*value = returnValue;
UI::EventParams e{};
Expand Down
3 changes: 2 additions & 1 deletion UI/GameSettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,8 @@ UI::EventReturn GameSettingsScreen::OnSavePathOther(UI::EventParams &e) {
const Path &PPSSPPpath = File::GetExeDirectory();
if (otherinstalled_) {
auto di = GetI18NCategory(I18NCat::DIALOG);
std::string folder = W32Util::BrowseForFolder(MainWindow::GetHWND(), di->T("Choose PPSSPP save folder"));
std::string initialPath = g_Config.memStickDirectory.ToCString();
std::string folder = W32Util::BrowseForFolder(MainWindow::GetHWND(), di->T("Choose PPSSPP save folder"), initialPath);
if (folder.size()) {
g_Config.memStickDirectory = Path(folder);
FILE *f = File::OpenCFile(PPSSPPpath / "installed.txt", "wb");
Expand Down
2 changes: 1 addition & 1 deletion UI/MainScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ UI::EventReturn GameBrowser::LastClick(UI::EventParams &e) {

UI::EventReturn GameBrowser::BrowseClick(UI::EventParams &e) {
auto mm = GetI18NCategory(I18NCat::MAINMENU);
System_BrowseForFolder(token_, mm->T("Choose folder"), [this](const std::string &filename, int) {
System_BrowseForFolder(token_, mm->T("Choose folder"), path_.GetPath(), [this](const std::string &filename, int) {
this->SetPath(Path(filename));
});
return UI::EVENT_DONE;
Expand Down
2 changes: 1 addition & 1 deletion UI/MemStickScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ UI::EventReturn MemStickScreen::UseStorageRoot(UI::EventParams &params) {

UI::EventReturn MemStickScreen::Browse(UI::EventParams &params) {
auto mm = GetI18NCategory(I18NCat::MAINMENU);
System_BrowseForFolder(GetRequesterToken(), mm->T("Choose folder"), [=](const std::string &value, int) {
System_BrowseForFolder(GetRequesterToken(), mm->T("Choose folder"), g_Config.memStickDirectory, [=](const std::string &value, int) {
Path pendingMemStickFolder = Path(value);
INFO_LOG(SYSTEM, "Got folder: '%s'", pendingMemStickFolder.c_str());
// Browse finished. Let's pop up the confirmation dialog.
Expand Down
2 changes: 1 addition & 1 deletion Windows/MainWindowMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ namespace MainWindow {
W32Util::MakeTopMost(GetHWND(), false);

if (browseDirectory) {
System_BrowseForFolder(token, mm->T("Load"), [](const std::string &value, int) {
System_BrowseForFolder(token, mm->T("Load"), Path(), [](const std::string &value, int) {
BrowseAndBootDone(value);
});
} else {
Expand Down
26 changes: 20 additions & 6 deletions Windows/W32Util/ShellUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@

namespace W32Util
{
std::string BrowseForFolder(HWND parent, const char *title)
{
std::string BrowseForFolder(HWND parent, std::string_view title, std::string_view initialPath) {
std::wstring titleString = ConvertUTF8ToWString(title);
return BrowseForFolder(parent, titleString.c_str());
return BrowseForFolder(parent, titleString.c_str(), initialPath);
}

std::string BrowseForFolder(HWND parent, const wchar_t *title)
{
static int CALLBACK BrowseFolderCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData) {
if (uMsg == BFFM_INITIALIZED) {
LPCTSTR path = reinterpret_cast<LPCTSTR>(lpData);
::SendMessage(hwnd, BFFM_SETSELECTION, true, (LPARAM)path);
}
return 0;
}

std::string BrowseForFolder(HWND parent, const wchar_t *title, std::string_view initialPath) {
BROWSEINFO info{};
info.hwndOwner = parent;
info.lpszTitle = title;
info.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
info.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS | BIF_USENEWUI | BIF_NEWDIALOGSTYLE;

std::wstring initialPathW;

if (!initialPath.empty()) {
initialPathW = ConvertUTF8ToWString(initialPath);
info.lParam = reinterpret_cast<LPARAM>(initialPathW.c_str());
info.lpfn = BrowseFolderCallback;
}

//info.pszDisplayName
auto idList = SHBrowseForFolder(&info);
Expand Down
29 changes: 17 additions & 12 deletions Windows/W32Util/ShellUtil.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
#pragma once

#include <string>
#include <string_view>
#include <vector>
#include <thread>

namespace W32Util
{
std::string BrowseForFolder(HWND parent, const char *title);
std::string BrowseForFolder(HWND parent, const wchar_t *title);
bool BrowseForFileName (bool _bLoad, HWND _hParent, const wchar_t*_pTitle,
const wchar_t *_pInitialFolder,const wchar_t *_pFilter,const wchar_t*_pExtension,
std::string& _strFileName);
std::vector<std::string> BrowseForFileNameMultiSelect(bool _bLoad, HWND _hParent, const wchar_t*_pTitle,
const wchar_t*_pInitialFolder,const wchar_t*_pFilter,const wchar_t*_pExtension);
namespace W32Util {

std::string UserDocumentsPath();
// Can't make initialPath a string_view, need the null so might as well require it.
std::string BrowseForFolder(HWND parent, std::string_view title, std::string_view initialPath);
std::string BrowseForFolder(HWND parent, const wchar_t *title, std::string_view initialPath);

bool CreateDesktopShortcut(const std::string &argumentPath, std::string gameTitle);
}
bool BrowseForFileName(bool _bLoad, HWND _hParent,
const wchar_t *_pTitle, const wchar_t *_pInitialFolder, const wchar_t *_pFilter, const wchar_t *_pExtension,
std::string& _strFileName);

std::vector<std::string> BrowseForFileNameMultiSelect(bool _bLoad, HWND _hParent, const wchar_t *_pTitle,
const wchar_t *_pInitialFolder, const wchar_t *_pFilter, const wchar_t *_pExtension);

std::string UserDocumentsPath();

bool CreateDesktopShortcut(const std::string &argumentPath, std::string gameTitle);

} // namespace
2 changes: 1 addition & 1 deletion Windows/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
case SystemRequestType::BROWSE_FOR_FOLDER:
{
std::thread([=] {
std::string folder = W32Util::BrowseForFolder(MainWindow::GetHWND(), param1.c_str());
std::string folder = W32Util::BrowseForFolder(MainWindow::GetHWND(), param1, param2);
if (folder.size()) {
g_requestManager.PostSystemSuccess(requestId, folder.c_str());
} else {
Expand Down

0 comments on commit bc92226

Please sign in to comment.