From aadf05dcb8ee392f3afeb7aee46772e4ac0516f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Mon, 6 Jun 2016 18:17:40 -0700 Subject: [PATCH] Added basic file list ImGui widget. --- 3rdparty/ocornut-imgui/imgui_user.h | 1 + 3rdparty/ocornut-imgui/imgui_user.inl | 1 + 3rdparty/ocornut-imgui/widgets/file_list.h | 30 +++++ 3rdparty/ocornut-imgui/widgets/file_list.inl | 135 +++++++++++++++++++ 4 files changed, 167 insertions(+) create mode 100644 3rdparty/ocornut-imgui/widgets/file_list.h create mode 100644 3rdparty/ocornut-imgui/widgets/file_list.inl diff --git a/3rdparty/ocornut-imgui/imgui_user.h b/3rdparty/ocornut-imgui/imgui_user.h index bf68388cd..69f26228b 100644 --- a/3rdparty/ocornut-imgui/imgui_user.h +++ b/3rdparty/ocornut-imgui/imgui_user.h @@ -15,4 +15,5 @@ namespace ImGui } // namespace ImGui +#include "widgets/file_list.h" #include "widgets/memory_editor.h" diff --git a/3rdparty/ocornut-imgui/imgui_user.inl b/3rdparty/ocornut-imgui/imgui_user.inl index abdb141f2..81a949ac5 100644 --- a/3rdparty/ocornut-imgui/imgui_user.inl +++ b/3rdparty/ocornut-imgui/imgui_user.inl @@ -1 +1,2 @@ +#include "widgets/file_list.inl" #include "widgets/memory_editor.inl" diff --git a/3rdparty/ocornut-imgui/widgets/file_list.h b/3rdparty/ocornut-imgui/widgets/file_list.h new file mode 100644 index 000000000..c82155989 --- /dev/null +++ b/3rdparty/ocornut-imgui/widgets/file_list.h @@ -0,0 +1,30 @@ +#include + +namespace ImGui +{ + struct ImFileInfo + { + ImFileInfo(const char* name, int64_t size); + ~ImFileInfo(); + ImFileInfo& operator=(const ImFileInfo& rhs); + + char* Name; + int64_t Size; + }; + + struct ImFileList + { + ImVector FileList; + int Pos; + + ImFileList(const char* path = ".") + : Pos(0) + { + ChDir(path); + } + + void ChDir(const char* path); + void Draw(); + }; + +} // namespace ImGui diff --git a/3rdparty/ocornut-imgui/widgets/file_list.inl b/3rdparty/ocornut-imgui/widgets/file_list.inl new file mode 100644 index 000000000..7da4cea22 --- /dev/null +++ b/3rdparty/ocornut-imgui/widgets/file_list.inl @@ -0,0 +1,135 @@ +#include +#include + +namespace ImGui +{ + ImFileInfo::ImFileInfo(const char* name, int64_t size) + { + Name = ImStrdup(name); + Size = size; + } + + ImFileInfo::~ImFileInfo() + { + MemFree(Name); + Name = NULL; + } + + ImFileInfo& ImFileInfo::operator=(const ImFileInfo& rhs) + { + if (this != &rhs) + { + Name = ImStrdup(rhs.Name); + Size = rhs.Size; + } + + return *this; + } + + void ImFileList::ChDir(const char* path) + { + 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); + } + } + + void ImFileList::Draw() + { + BeginChild("##file_list", ImVec2(0.0f, 0.0f) ); + PushFont(Font::Mono); + + PushItemWidth(-1); + if (ListBoxHeader("##empty", ImVec2(0.0f, 0.0f) ) ) + { + const float lineHeight = GetTextLineHeightWithSpacing(); + + char* chdir = NULL; + + int pos = 0; + ImGuiListClipper clipper(FileList.size(), lineHeight); + for (ImVector::const_iterator it = FileList.begin(), itEnd = FileList.end() + ; it != itEnd + ; ++it + ) + { + if (pos >= clipper.DisplayStart + && pos < clipper.DisplayEnd) + { + PushID(pos); + + const bool isDir = -1 == it->Size; + bool isSelected = Pos == pos; + + bool clicked = Selectable(it->Name, &isSelected, 0, ImVec2(150.0f, 0.0f) ); + SameLine(); + if (isDir) + { + Text("%10s", ""); + } + else + { + Text("%10d", it->Size); + } + + if (clicked) + { + if (0 == ImStricmp(it->Name, "..") ) + { + MemFree(chdir); + chdir = ImStrdup(it->Name); + } + + Pos = pos; + + if (isDir) + { + MemFree(chdir); + chdir = ImStrdup(it->Name); + } + } + + PopID(); + } + ++pos; + } + clipper.End(); + + ListBoxFooter(); + + if (NULL != chdir) + { + ChDir(chdir); + MemFree(chdir); + } + } + + PopFont(); + EndChild(); + } + +} // namespace ImGui