Added basic file list ImGui widget.

This commit is contained in:
Branimir Karadžić
2016-06-06 18:17:40 -07:00
parent 680cb30b4a
commit aadf05dcb8
4 changed files with 167 additions and 0 deletions

View File

@@ -15,4 +15,5 @@ namespace ImGui
} // namespace ImGui
#include "widgets/file_list.h"
#include "widgets/memory_editor.h"

View File

@@ -1 +1,2 @@
#include "widgets/file_list.inl"
#include "widgets/memory_editor.inl"

View File

@@ -0,0 +1,30 @@
#include <stdint.h>
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<ImFileInfo> FileList;
int Pos;
ImFileList(const char* path = ".")
: Pos(0)
{
ChDir(path);
}
void ChDir(const char* path);
void Draw();
};
} // namespace ImGui

View File

@@ -0,0 +1,135 @@
#include <dirent.h>
#include <sys/stat.h>
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<ImFileInfo>::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", "<DIR>");
}
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