file_list.inl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <bx/bx.h>
  2. #include <dirent.h>
  3. #include <sys/stat.h>
  4. namespace ImGui
  5. {
  6. ImFileInfo::ImFileInfo(const char* name, int64_t size)
  7. : Name(name)
  8. , Size(size)
  9. {
  10. }
  11. ImFileInfo::~ImFileInfo()
  12. {
  13. }
  14. void ImFileList::ChDir(const char* path)
  15. {
  16. #if BX_PLATFORM_PS4
  17. BX_UNUSED(path);
  18. #else
  19. DIR* dir = opendir(path);
  20. if (NULL != dir)
  21. {
  22. FileList.clear();
  23. for (dirent* item = readdir(dir); NULL != item; item = readdir(dir) )
  24. {
  25. if (0 == ImStricmp(item->d_name, "..") )
  26. {
  27. FileList.push_back(ImFileInfo(item->d_name, -1) );
  28. }
  29. else if (0 != ImStricmp(item->d_name, ".") )
  30. {
  31. if (item->d_type & DT_DIR)
  32. {
  33. FileList.push_back(ImFileInfo(item->d_name, -1) );
  34. }
  35. else
  36. {
  37. struct stat statbuf;
  38. stat(item->d_name, &statbuf);
  39. FileList.push_back(ImFileInfo(item->d_name, statbuf.st_size) );
  40. }
  41. }
  42. }
  43. closedir(dir);
  44. }
  45. #endif // BX_PLATFORM_PS4
  46. }
  47. void ImFileList::Draw()
  48. {
  49. BeginChild("##file_list", ImVec2(0.0f, 0.0f) );
  50. PushFont(Font::Mono);
  51. PushItemWidth(-1);
  52. if (BeginListBox("##empty", ImVec2(0.0f, 0.0f) ) )
  53. {
  54. const float lineHeight = GetTextLineHeightWithSpacing();
  55. ImString chdir;
  56. int pos = 0;
  57. ImGuiListClipper clipper;
  58. clipper.Begin(FileList.size(), lineHeight);
  59. clipper.Step();
  60. for (FileInfoArray::const_iterator it = FileList.begin(), itEnd = FileList.end()
  61. ; it != itEnd
  62. ; ++it
  63. )
  64. {
  65. if (pos >= clipper.DisplayStart
  66. && pos < clipper.DisplayEnd)
  67. {
  68. PushID(pos);
  69. const bool isDir = -1 == it->Size;
  70. bool isSelected = Pos == pos;
  71. bool clicked = Selectable(it->Name.CStr(), &isSelected);
  72. SameLine(150);
  73. if (isDir)
  74. {
  75. Text("%10s", "<DIR>");
  76. }
  77. else
  78. {
  79. Text("%10" PRId64, it->Size);
  80. }
  81. if (clicked)
  82. {
  83. if (0 == strcmp(it->Name.CStr(), "..") )
  84. {
  85. chdir = it->Name;
  86. }
  87. Pos = pos;
  88. if (isDir)
  89. {
  90. chdir = it->Name;
  91. }
  92. }
  93. PopID();
  94. }
  95. ++pos;
  96. }
  97. clipper.End();
  98. EndListBox();
  99. if (!chdir.IsEmpty() )
  100. {
  101. ChDir(chdir.CStr() );
  102. }
  103. }
  104. PopFont();
  105. EndChild();
  106. }
  107. } // namespace ImGui