13 # error "include imgui.h before this header" 61 bool SetPwd(
const std::filesystem::path &pwd = std::filesystem::current_path());
87 static std::uint32_t GetDrivesBitMask();
136 newDirNameBuf_ = std::make_unique<std::array<char, INPUT_NAME_BUF_SIZE>>();
140 SetPwd(std::filesystem::current_path());
146 drives_ = GetDrivesBitMask();
176 newDirNameBuf_ = std::make_unique<std::array<char, INPUT_NAME_BUF_SIZE>>();
185 title_ = std::move(title);
186 openLabel_ =
title_ +
"##filebrowser_" + std::to_string(reinterpret_cast<size_t>(
this));
187 openNewDirLabel_ =
"new dir##new_dir_" + std::to_string(reinterpret_cast<size_t>(
this));
226 SetNextWindowSize(ImVec2(700, 450));
228 SetNextWindowSize(ImVec2(700, 450), ImGuiCond_FirstUseEver);
229 if(
flags_ & ImGuiFileBrowserFlags_NoModal)
234 else if(!BeginPopupModal(
openLabel_.c_str(),
nullptr,
245 char currentDrive =
static_cast<char>(
pwd_.c_str()[0]);
246 char driveStr[] = { currentDrive,
':',
'\0' };
248 PushItemWidth(4 * GetFontSize());
249 if(BeginCombo(
"##SelectDrive", driveStr))
252 for(
int i = 0; i < 26; ++i)
254 if(!(drives_ & (1 << i)))
256 char driveCh =
static_cast<char>(
'A' + i);
257 char selectableStr[] = { driveCh,
':',
'\0' };
258 bool selected = currentDrive == driveCh;
259 if(Selectable(selectableStr, selected) && !selected)
261 char newPwd[] = { driveCh,
':',
'\\',
'\0' };
271 int secIdx = 0, newPwdLastSecIdx = -1;
272 for(
auto &sec :
pwd_)
284 if(SmallButton(sec.u8string().c_str()))
285 newPwdLastSecIdx = secIdx;
290 if(newPwdLastSecIdx >= 0)
293 std::filesystem::path newPwd;
294 for(
auto &sec : pwd_)
296 if(i++ > newPwdLastSecIdx)
301 if(newPwdLastSecIdx == 0)
318 (*newDirNameBuf_)[0] =
'\0';
323 ScopeGuard endNewDirPopup([] { EndPopup(); });
328 ScopeGuard closeNewDirPopup([] { CloseCurrentPopup(); });
339 float reserveHeight = GetItemsLineHeightWithSpacing();
340 std::filesystem::path newPwd;
bool setNewPwd =
false;
342 reserveHeight += GetItemsLineHeightWithSpacing();
344 BeginChild(
"ch", ImVec2(0, -reserveHeight),
true,
345 (
flags_ & ImGuiFileBrowserFlags_NoModal) ? ImGuiWindowFlags_AlwaysHorizontalScrollbar : 0);
355 if(!rsc.name.empty() && rsc.name[0] ==
'$')
359 if(Selectable(rsc.showName.c_str(), selected, ImGuiSelectableFlags_DontClosePopups))
364 (*inputNameBuf_)[0] =
'\0';
366 else if(rsc.name !=
"..")
368 if((rsc.isDir && (
flags_ & ImGuiFileBrowserFlags_SelectDirectory)) ||
369 (!rsc.isDir && !(
flags_ & ImGuiFileBrowserFlags_SelectDirectory)))
372 if(!(
flags_ & ImGuiFileBrowserFlags_SelectDirectory))
378 if(IsItemClicked(0) && IsMouseDoubleClicked(0) && rsc.isDir)
381 newPwd = (rsc.name !=
"..") ? (pwd_ / rsc.name) : pwd_.parent_path();
389 if(!(
flags_ & ImGuiFileBrowserFlags_SelectDirectory) && (
flags_ & ImGuiFileBrowserFlags_EnterNewFilename))
400 if(!(
flags_ & ImGuiFileBrowserFlags_SelectDirectory))
418 else if(Button(
"open"))
424 int escIdx = GetIO().KeyMap[ImGuiKey_Escape];
438 PushItemWidth(8 * GetFontSize());
456 catch(
const std::exception &err)
458 statusStr_ = std::string(
"last error: ") + err.what();
477 (*inputNameBuf_)[0] =
'\0';
491 for(
auto &p : std::filesystem::directory_iterator(pwd))
495 if(p.is_regular_file())
497 else if(p.is_directory())
502 rcd.
name = p.path().filename().string();
506 rcd.
extension = p.path().filename().extension().string();
508 rcd.
showName = (rcd.
isDir ?
"[D] " :
"[F] ") + p.path().filename().u8string();
518 pwd_ = absolute(pwd);
520 (*inputNameBuf_)[0] =
'\0';
525 inline std::uint32_t ImGui::FileBrowser::GetDrivesBitMask()
528 DWORD mask = GetLogicalDrives();
530 for(
int i = 0; i < 26; ++i)
532 if(!(mask & (1 << i)))
534 sprintf(rootName,
"%c:\\",
'A' + i);
535 UINT type = GetDriveTypeA(rootName);
536 if(type == DRIVE_REMOVABLE || type == DRIVE_FIXED)
Definition: ImFileBrowser.hpp:24
Definition: ImFileBrowser.hpp:108
std::vector< const char * > typeFilters_
Definition: ImFileBrowser.hpp:102
Definition: ImFileBrowser.hpp:26
std::string name
Definition: ImFileBrowser.hpp:111
void SetTitle(std::string title)
Definition: ImFileBrowser.hpp:183
std::string openLabel_
Definition: ImFileBrowser.hpp:93
std::string statusStr_
Definition: ImFileBrowser.hpp:100
Definition: ImFileBrowser.hpp:25
int typeFilterIndex_
Definition: ImFileBrowser.hpp:103
bool SetPwd(const std::filesystem::path &pwd=std::filesystem::current_path())
Definition: ImFileBrowser.hpp:449
ImGuiFileBrowserFlags_
Definition: ImFileBrowser.hpp:18
std::string extension
Definition: ImFileBrowser.hpp:113
Definition: ImFileBrowser.hpp:20
Definition: ImFileBrowser.hpp:31
~ScopeGuard()
Definition: ImFileBrowser.hpp:81
FileBrowser & operator=(const FileBrowser ©From)
Definition: ImFileBrowser.hpp:156
std::string title_
Definition: ImFileBrowser.hpp:92
bool isDir
Definition: ImFileBrowser.hpp:110
ScopeGuard(T func)
Definition: ImFileBrowser.hpp:80
bool closeFlag_
Definition: ImFileBrowser.hpp:96
Definition: ImFileBrowser.hpp:23
std::unique_ptr< std::array< char, INPUT_NAME_BUF_SIZE > > inputNameBuf_
Definition: ImFileBrowser.hpp:119
Definition: ImFileBrowser.hpp:22
void SetTypeFilters(const std::vector< const char *> &typeFilters)
Definition: ImFileBrowser.hpp:481
void Display()
Definition: ImFileBrowser.hpp:214
static constexpr size_t INPUT_NAME_BUF_SIZE
Definition: ImFileBrowser.hpp:118
void Close()
Definition: ImFileBrowser.hpp:201
std::filesystem::path pwd_
Definition: ImFileBrowser.hpp:105
std::function< void()> func_
Definition: ImFileBrowser.hpp:75
std::string openNewDirLabel_
Definition: ImFileBrowser.hpp:121
void SetPwdUncatched(const std::filesystem::path &pwd)
Definition: ImFileBrowser.hpp:487
std::string selectedFilename_
Definition: ImFileBrowser.hpp:106
std::filesystem::path GetSelected() const
Definition: ImFileBrowser.hpp:469
Definition: ImFileBrowser.hpp:29
ImGuiFileBrowserFlags flags_
Definition: ImFileBrowser.hpp:90
Definition: ImFileBrowser.hpp:73
int ImGuiFileBrowserFlags
Definition: ImFileBrowser.hpp:16
std::vector< FileRecord > fileRecords_
Definition: ImFileBrowser.hpp:115
bool HasSelected() const noexcept
Definition: ImFileBrowser.hpp:444
void Open()
Definition: ImFileBrowser.hpp:190
Definition: ImFileBrowser.hpp:21
bool IsOpened() const noexcept
Definition: ImFileBrowser.hpp:209
std::string showName
Definition: ImFileBrowser.hpp:112
bool openFlag_
Definition: ImFileBrowser.hpp:95
bool isOpened_
Definition: ImFileBrowser.hpp:97
bool ok_
Definition: ImFileBrowser.hpp:98
FileBrowser(ImGuiFileBrowserFlags flags=0)
Definition: ImFileBrowser.hpp:130
std::unique_ptr< std::array< char, INPUT_NAME_BUF_SIZE > > newDirNameBuf_
Definition: ImFileBrowser.hpp:122
void ClearSelected()
Definition: ImFileBrowser.hpp:474