Skip to content

Commit

Permalink
Internals: Fixed ImFileOpen not working before context is created.
Browse files Browse the repository at this point in the history
Fixes #7314
  • Loading branch information
PathogenDavid committed Feb 13, 2024
1 parent f966da1 commit 46c4eda
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Other changes:
a wrong viewport if none was initially set.
- Backends: DirectX9: Using RGBA format when allowed by the driver to avoid CPU side
conversion. (#6575) [@Demonese]
- Internals: Fixed ImFileOpen not working before context is created. (#7314) [@PathogenDavid]


-----------------------------------------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2063,9 +2063,12 @@ ImFileHandle ImFileOpen(const char* filename, const char* mode)
// Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32!
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
ImVector<char> local_temp;
// Fall back to own temp buffer if no context exists yet (EG: when creating shared font atlas before initializing.)
ImGuiContext& g = *GImGui;
g.TempBuffer.reserve((filename_wsize + mode_wsize) * sizeof(wchar_t));
wchar_t* buf = (wchar_t*)(void*)g.TempBuffer.Data;
ImVector<char>& temp = &g != NULL ? g.TempBuffer : local_temp;
temp.reserve((filename_wsize + mode_wsize) * sizeof(wchar_t));
wchar_t* buf = (wchar_t*)(void*)temp.Data;
::MultiByteToWideChar(CP_UTF8, 0, filename, -1, (wchar_t*)&buf[0], filename_wsize);
::MultiByteToWideChar(CP_UTF8, 0, mode, -1, (wchar_t*)&buf[filename_wsize], mode_wsize);
return ::_wfopen((const wchar_t*)&buf[0], (const wchar_t*)&buf[filename_wsize]);
Expand Down

0 comments on commit 46c4eda

Please sign in to comment.