Refactor disc browser a bit (file browsing is really general, so add a base

class for it)
This commit is contained in:
simon.kagstrom 2009-12-15 18:37:34 +00:00
parent b52c401665
commit a2d9a82c9d
3 changed files with 60 additions and 41 deletions

View File

@ -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

View File

@ -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;
};

51
file_browser.hh Normal file
View File

@ -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;
};