From a2d9a82c9d624b8aba8c57fd46cd22959165265b Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Tue, 15 Dec 2009 18:37:34 +0000 Subject: [PATCH] Refactor disc browser a bit (file browsing is really general, so add a base class for it) --- Makefile | 3 ++- disc_menu.cpp | 47 +++++++-------------------------------------- file_browser.hh | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 file_browser.hh diff --git a/Makefile b/Makefile index a087721..760d700 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,8 @@ all: menu menu.oo: menu.cpp menu.hh utils.hh font.hh Makefile gui.oo: Makefile gui.cpp gui.hh font.hh menu.hh sdl_ttf_font.hh \ - dialogue_box.hh help_box.hh main_menu.cpp disc_menu.cpp + dialogue_box.hh help_box.hh main_menu.cpp disc_menu.cpp \ + file_browser.hh utils.oo: utils.cpp utils.hh Makefile diff --git a/disc_menu.cpp b/disc_menu.cpp index 5e5b0e9..327d4ed 100644 --- a/disc_menu.cpp +++ b/disc_menu.cpp @@ -1,23 +1,22 @@ #include "menu.hh" +#include "file_browser.hh" + +static const char *game_exts[] = {".d64", ".D64", ".t64", ".T64", + ".prg",".PRG", ".p00", ".P00", NULL}; class DiscView; -class DiscMenu : public Menu +class DiscMenu : public FileBrowser { friend class DiscView; public: - DiscMenu(Font *font, GuiView *parent) : Menu(font) + DiscMenu(Font *font, GuiView *parent) : + FileBrowser(game_exts, font, parent) { - this->parent = parent; - this->path = NULL; - - /* If nothing else: Set the default list */ - this->setDefaultFileList(); } ~DiscMenu() { - this->freeFileList(); } virtual void selectCallback(int which) @@ -25,18 +24,6 @@ public: printf("entry %d selected: %s\n", which, this->pp_msgs[which]); } - void setDirectory(const char *path) - { - const char *exts[] = {".d64", ".D64", ".t64", ".T64", ".prg", - ".PRG", ".p00", ".P00"}; - - this->freeFileList(); - this->file_list = get_file_list(path, exts); - if (!this->file_list) - this->setDefaultFileList(); - this->setText(this->file_list); - } - virtual void hoverCallback(int which) { } @@ -45,26 +32,6 @@ public: { Gui::gui->exitMenu(); } - -private: - void setDefaultFileList() - { - this->file_list = (const char **)xmalloc(2 * sizeof(char*)); - this->file_list[0] = xstrdup("None"); - } - - 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); - } - - const char *path; - const char **file_list; - GuiView *parent; }; diff --git a/file_browser.hh b/file_browser.hh new file mode 100644 index 0000000..fd7fd88 --- /dev/null +++ b/file_browser.hh @@ -0,0 +1,51 @@ +#include "menu.hh" +#include "gui.hh" + +class FileBrowser : public Menu +{ +public: + FileBrowser(const char **exts, Font *font, GuiView *parent) : Menu(font) + { + this->parent = parent; + this->path = NULL; + this->exts = exts; + + /* If nothing else: Set the default list */ + this->setDefaultFileList(); + } + + ~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); + } + +protected: + void setDefaultFileList() + { + this->file_list = (const char **)xmalloc(2 * sizeof(char*)); + this->file_list[0] = xstrdup("None"); + } + + 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); + } + + const char *path; + const char **file_list; + const char **exts; + GuiView *parent; +};