diff --git a/include/gui.h b/include/gui.h index e1b97af..21268d6 100644 --- a/include/gui.h +++ b/include/gui.h @@ -15,7 +15,8 @@ enum MENU_STATES { MENU_STATE_PROPERTIES, MENU_STATE_SETTINGS, MENU_STATE_IMAGEVIEWER, - MENU_STATE_ARCHIVEEXTRACT + MENU_STATE_ARCHIVEEXTRACT, + MENU_STATE_TEXTREADER }; typedef struct { @@ -35,8 +36,14 @@ typedef struct { Tex texture; } MenuItem; +typedef struct { + char *buf; + size_t buf_size; +} TextReader; + extern SDL_Window *window; extern MenuItem item; +extern TextReader text_reader; namespace GUI { inline void ResetCheckbox(void) { diff --git a/include/windows.h b/include/windows.h index a1461a8..86baf6a 100644 --- a/include/windows.h +++ b/include/windows.h @@ -19,6 +19,7 @@ namespace Windows { void FileBrowserWindow(bool *focus, bool *first_item); void ImageWindow(Tex *texture); void SettingsWindow(void); + void TextReaderWindow(void); } #endif diff --git a/source/config.cpp b/source/config.cpp index cad0c40..e0d41e1 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -96,7 +96,7 @@ namespace Config { std::strcpy(config.cwd, "/"); // Delete config file if config file is updated. This will rarely happen. - if (config_version_holder < CONFIG_VERSION) { + if (config_version_holder < CONFIG_VERSION) { fsFsDeleteFile(fs, "/switch/NX-Shell/config.json"); Config::SetDefault(&config); return Config::Save(config); diff --git a/source/gui.cpp b/source/gui.cpp index 77c49c2..e2b2173 100644 --- a/source/gui.cpp +++ b/source/gui.cpp @@ -87,6 +87,11 @@ namespace GUI { item.state = MENU_STATE_HOME; } } + else if (item.state == MENU_STATE_TEXTREADER) { + text_reader.buf_size = 0; + delete[] text_reader.buf; + item.state = MENU_STATE_HOME; + } else item.state = MENU_STATE_HOME; } @@ -149,6 +154,10 @@ namespace GUI { case MENU_STATE_SETTINGS: Windows::SettingsWindow(); break; + + case MENU_STATE_TEXTREADER: + Windows::TextReaderWindow(); + break; default: break; diff --git a/source/windows/filebrowser.cpp b/source/windows/filebrowser.cpp index c93b36a..1e56715 100644 --- a/source/windows/filebrowser.cpp +++ b/source/windows/filebrowser.cpp @@ -3,6 +3,7 @@ #include "gui.h" #include "imgui.h" #include "imgui_internal.h" +#include "log.h" #include "windows.h" namespace Windows { @@ -44,18 +45,49 @@ namespace Windows { ImGui::SameLine(); if (ImGui::Selectable(filename.c_str())) { + char path[FS_MAX_PATH + 1]; + switch(file_type) { case FileTypeArchive: item.state = MENU_STATE_ARCHIVEEXTRACT; break; case FileTypeImage: - char path[FS_MAX_PATH + 1]; if ((std::snprintf(path, FS_MAX_PATH, "%s/%s", config.cwd, item.selected_filename.c_str())) > 0) { Textures::LoadImageFile(path, &item.texture); item.state = MENU_STATE_IMAGEVIEWER; } break; + + case FileTypeText: + if ((std::snprintf(path, FS_MAX_PATH, "%s/%s", config.cwd, item.selected_filename.c_str())) > 0) { + Log::Exit(); + + FsFile file; + Result ret = 0; + if (R_FAILED(ret = fsFsOpenFile(fs, path, FsOpenMode_Read, &file))) + Log::Error("fsFsOpenFile(%s) failed: 0x%x\n", path, ret); + + s64 size = 0; + if (R_FAILED(ret = fsFileGetSize(&file, &size))) { + Log::Error("fsFileGetSize(%s) failed: 0x%x\n", path, ret); + fsFileClose(&file); + } + + text_reader.buf = new char[size]; + + u64 bytes_read = 0; + if (R_FAILED(ret = fsFileRead(&file, 0, text_reader.buf, static_cast(size), FsReadOption_None, &bytes_read))) { + Log::Error("fsFileRead(%s) failed: 0x%x\n", path, ret); + fsFileClose(&file); + } + + fsFileClose(&file); + Log::Init(); + text_reader.buf_size = bytes_read; + item.state = MENU_STATE_TEXTREADER; + } + break; default: break; diff --git a/source/windows/textreader.cpp b/source/windows/textreader.cpp new file mode 100644 index 0000000..2e26c1f --- /dev/null +++ b/source/windows/textreader.cpp @@ -0,0 +1,19 @@ +#include "config.h" +#include "gui.h" +#include "imgui.h" +#include "windows.h" + +TextReader text_reader; + +namespace Windows { + void TextReaderWindow(void) { + Windows::SetupWindow(); + + if (ImGui::Begin(item.selected_filename.c_str(), nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse)) { + ImGui::SetNextWindowFocus(); + ImGui::InputTextMultiline(item.selected_filename.c_str(), text_reader.buf, text_reader.buf_size, ImVec2(1270.0f, 660.0f)); + } + + Windows::ExitWindow(); + } +}