Skip to content

Commit

Permalink
windows: ensure dialogs are modal
Browse files Browse the repository at this point in the history
`GetActiveWindow()` will return NULL if called asynchronously, as it's
calling thread will not have a message loop. To work around this, use
`GetForegroundWindow()` if `GetActiveWindow()` is null.

This will cause any dialogs to be modal.

Fixes samhocevar#82 and
samhocevar#93
  • Loading branch information
samangh committed Mar 11, 2024
1 parent de7217a commit 05921d3
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions portable-file-dialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,15 @@ namespace internal
{

#if _WIN32
static inline HWND get_window_handle(){
// Get current Window
//
// GetActiveWindow() sometimes returns null, in that case use the
// foreground window
HWND hwndOwner = GetActiveWindow();
return hwndOwner ? hwndOwner :GetForegroundWindow();
}

static inline std::wstring str2wstr(std::string const &str)
{
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), nullptr, 0);
Expand Down Expand Up @@ -1132,7 +1141,7 @@ inline internal::file_dialog::file_dialog(type in_type,
OPENFILENAMEW ofn;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAMEW);
ofn.hwndOwner = GetActiveWindow();
ofn.hwndOwner = internal::get_window_handle();

ofn.lpstrFilter = wfilter_list.c_str();

Expand Down Expand Up @@ -1459,7 +1468,7 @@ inline std::string internal::file_dialog::select_folder_vista(IFileDialog *ifd,
ifd->SetOptions(FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM);
ifd->SetTitle(m_wtitle.c_str());

hr = ifd->Show(GetActiveWindow());
hr = ifd->Show(internal::get_window_handle());
if (SUCCEEDED(hr))
{
IShellItem* item;
Expand Down Expand Up @@ -1649,7 +1658,7 @@ inline message::message(std::string const &title,
auto wtitle = internal::str2wstr(title);
// Apply new visual style (required for all Windows versions)
new_style_context ctx;
*exit_code = MessageBoxW(GetActiveWindow(), wtext.c_str(), wtitle.c_str(), style);
*exit_code = MessageBoxW(internal::get_window_handle(), wtext.c_str(), wtitle.c_str(), style);
return "";
});

Expand Down

0 comments on commit 05921d3

Please sign in to comment.