From a664d7c884764a967c51e273c4cfc2a25a066a95 Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Sat, 30 Jan 2010 08:06:32 +0000 Subject: [PATCH] Refactor the filebrowser to allow nested directories --- Makefile.host | 3 +- Src/gui/disc_menu.cpp | 9 ++++- Src/gui/file_browser.cpp | 79 ++++++++++++++++++++++++++++++++++++++++ Src/gui/file_browser.hh | 42 ++++----------------- 4 files changed, 96 insertions(+), 37 deletions(-) create mode 100644 Src/gui/file_browser.cpp diff --git a/Makefile.host b/Makefile.host index 41d1c40..5e889d0 100644 --- a/Makefile.host +++ b/Makefile.host @@ -36,7 +36,8 @@ CPP_SRCS=Src/C64_SC.cpp Src/main.cpp Src/Display.cpp Src/Prefs.cpp Src/SID.cpp \ Src/CIA_SC.cpp Src/CPU1541_SC.cpp Src/CPU_common.cpp Src/Network.cpp \ Src/gui/menu_messages.cpp Src/gui/dialogue_box.cpp Src/gui/widget.cpp \ Src/gui/game_info.cpp Src/gui/status_bar.cpp Src/gui/gui.cpp Src/gui/listener.cpp \ - Src/timer.cpp Src/utils.cpp Src/gui/virtual_keyboard.cpp Src/gui/menu.cpp + Src/timer.cpp Src/utils.cpp Src/gui/virtual_keyboard.cpp Src/gui/menu.cpp \ + Src/gui/file_browser.cpp C_SRCS=Src/d64-read.c diff --git a/Src/gui/disc_menu.cpp b/Src/gui/disc_menu.cpp index 701bb8a..7075d36 100644 --- a/Src/gui/disc_menu.cpp +++ b/Src/gui/disc_menu.cpp @@ -58,8 +58,15 @@ public: { const char *fileName = this->pp_msgs[this->cur_sel]; + /* If we selected a directory, just take the next one */ + if (fileName[0] == '[') + { + this->pushDirectory(fileName); + return; + } + snprintf(Gui::gui->np->DrivePath[0], sizeof(Gui::gui->np->DrivePath[0]), - "%s/%s", Gui::gui->game_base_path, fileName); + "%s/%s", this->cur_path_prefix, fileName); if (ext_matches_list(fileName, prg_exts)) { char tmp_filename[255]; diff --git a/Src/gui/file_browser.cpp b/Src/gui/file_browser.cpp new file mode 100644 index 0000000..538a705 --- /dev/null +++ b/Src/gui/file_browser.cpp @@ -0,0 +1,79 @@ +#include + +#include +#include "file_browser.hh" + +FileBrowser::FileBrowser(const char **exts, Font *font) : Menu(font) +{ + this->path = NULL; + this->exts = exts; + this->cur_path_prefix = NULL; + + if (!this->exts) + this->exts = (const char **){ NULL }; + + /* If nothing else: Set the default list */ + this->setDefaultFileList(); +} + +FileBrowser::~FileBrowser() +{ + this->freeFileList(); +} + +void FileBrowser::pushDirectory(const char *in_path) +{ + const char *cur = this->cur_path_prefix ? xstrdup(this->cur_path_prefix) : NULL; + size_t cur_len = this->cur_path_prefix ? strlen(this->cur_path_prefix) : NULL; + char *path_cpy = xstrdup(in_path); + char *path = path_cpy; + + /* Weed out [ and ] from paths */ + if (*path_cpy == '[') + path = path_cpy + 1; + for (size_t i = 0; i < strlen(path); i++) + { + if (path[i] == ']') + path[i] = '\0'; + } + + this->cur_path_prefix = (char *)xrealloc(this->cur_path_prefix, + cur_len + 2 + strlen(path)); + + if (cur != NULL) + sprintf(this->cur_path_prefix, "%s/%s", cur, path); + else + strcpy(this->cur_path_prefix, path); + + free((void *)cur); + free(path_cpy); + + this->freeFileList(); + this->file_list = get_file_list(this->cur_path_prefix, this->exts); + if (!this->file_list) + this->setDefaultFileList(); + this->setText(this->file_list); +} + +void FileBrowser::setDirectory(const char *path) +{ + free(this->cur_path_prefix); + this->cur_path_prefix = NULL; + + this->pushDirectory(path); +} + +void FileBrowser::setDefaultFileList() +{ + this->file_list = (const char **)xmalloc(2 * sizeof(char*)); + this->file_list[0] = xstrdup("None"); +} + +void FileBrowser::freeFileList() +{ + if (!this->file_list) + return; + for (int i = 0; this->file_list[i]; i++) + free((void*)this->file_list[i]); + free(this->file_list); +} diff --git a/Src/gui/file_browser.hh b/Src/gui/file_browser.hh index f7cae80..cc6bb7d 100644 --- a/Src/gui/file_browser.hh +++ b/Src/gui/file_browser.hh @@ -7,48 +7,20 @@ class FileBrowser : public Menu { public: - FileBrowser(const char **exts, Font *font) : Menu(font) - { - this->path = NULL; - this->exts = exts; + FileBrowser(const char **exts, Font *font); - if (!this->exts) - this->exts = (const char **){ NULL }; + ~FileBrowser(); - /* If nothing else: Set the default list */ - this->setDefaultFileList(); - } + void pushDirectory(const char *path); - ~FileBrowser() - { - this->freeFileList(); - } - - void setDirectory(const char *path) - { - this->freeFileList(); - this->file_list = get_file_list(path, this->exts); - if (!this->file_list) - this->setDefaultFileList(); - this->setText(this->file_list); - } + void setDirectory(const char *path); protected: - void setDefaultFileList() - { - this->file_list = (const char **)xmalloc(2 * sizeof(char*)); - this->file_list[0] = xstrdup("None"); - } + void setDefaultFileList(); - void freeFileList() - { - if (!this->file_list) - return; - for (int i = 0; this->file_list[i]; i++) - free((void*)this->file_list[i]); - free(this->file_list); - } + void freeFileList(); + char *cur_path_prefix; const char *path; const char **file_list; const char **exts;