bgfx/3rdparty/dear-imgui/widgets/file_list.inl

128 lines
2.2 KiB
Plaintext
Raw Normal View History

2016-09-03 07:25:01 +03:00
#include <bx/bx.h>
2016-06-07 04:17:40 +03:00
#include <dirent.h>
#include <sys/stat.h>
namespace ImGui
{
ImFileInfo::ImFileInfo(const char* name, int64_t size)
2016-06-07 06:13:03 +03:00
: Name(name)
, Size(size)
2016-06-07 04:17:40 +03:00
{
}
ImFileInfo::~ImFileInfo()
{
}
void ImFileList::ChDir(const char* path)
{
#if BX_PLATFORM_PS4
2016-09-03 07:25:01 +03:00
BX_UNUSED(path);
#else
2016-06-07 04:17:40 +03:00
DIR* dir = opendir(path);
if (NULL != dir)
{
FileList.clear();
for (dirent* item = readdir(dir); NULL != item; item = readdir(dir) )
{
if (0 == ImStricmp(item->d_name, "..") )
{
FileList.push_back(ImFileInfo(item->d_name, -1) );
}
else if (0 != ImStricmp(item->d_name, ".") )
{
if (item->d_type & DT_DIR)
{
FileList.push_back(ImFileInfo(item->d_name, -1) );
}
else
{
struct stat statbuf;
stat(item->d_name, &statbuf);
FileList.push_back(ImFileInfo(item->d_name, statbuf.st_size) );
}
}
}
closedir(dir);
}
#endif // BX_PLATFORM_PS4
2016-06-07 04:17:40 +03:00
}
void ImFileList::Draw()
{
BeginChild("##file_list", ImVec2(0.0f, 0.0f) );
PushFont(Font::Mono);
PushItemWidth(-1);
2021-02-11 09:28:11 +03:00
if (BeginListBox("##empty", ImVec2(0.0f, 0.0f) ) )
2016-06-07 04:17:40 +03:00
{
const float lineHeight = GetTextLineHeightWithSpacing();
2016-06-07 06:13:03 +03:00
ImString chdir;
2016-06-07 04:17:40 +03:00
int pos = 0;
2020-10-09 18:50:40 +03:00
ImGuiListClipper clipper;
clipper.Begin(FileList.size(), lineHeight);
clipper.Step();
2016-06-07 06:13:03 +03:00
for (FileInfoArray::const_iterator it = FileList.begin(), itEnd = FileList.end()
2016-06-07 04:17:40 +03:00
; it != itEnd
; ++it
)
{
if (pos >= clipper.DisplayStart
&& pos < clipper.DisplayEnd)
{
PushID(pos);
const bool isDir = -1 == it->Size;
bool isSelected = Pos == pos;
2016-06-07 19:20:43 +03:00
bool clicked = Selectable(it->Name.CStr(), &isSelected);
SameLine(150);
2016-06-07 04:17:40 +03:00
if (isDir)
{
Text("%10s", "<DIR>");
}
else
{
2016-06-07 21:28:00 +03:00
Text("%10" PRId64, it->Size);
2016-06-07 04:17:40 +03:00
}
if (clicked)
{
2016-06-07 06:13:03 +03:00
if (0 == strcmp(it->Name.CStr(), "..") )
2016-06-07 04:17:40 +03:00
{
2016-06-07 06:13:03 +03:00
chdir = it->Name;
2016-06-07 04:17:40 +03:00
}
Pos = pos;
if (isDir)
{
2016-06-07 06:13:03 +03:00
chdir = it->Name;
2016-06-07 04:17:40 +03:00
}
}
PopID();
}
++pos;
}
clipper.End();
2021-02-11 09:28:11 +03:00
EndListBox();
2016-06-07 04:17:40 +03:00
2016-06-07 06:13:03 +03:00
if (!chdir.IsEmpty() )
2016-06-07 04:17:40 +03:00
{
2016-06-07 06:13:03 +03:00
ChDir(chdir.CStr() );
2016-06-07 04:17:40 +03:00
}
}
PopFont();
EndChild();
}
} // namespace ImGui