Refactor the filebrowser to allow nested directories

This commit is contained in:
simon.kagstrom 2010-01-30 08:06:32 +00:00
parent 9d1d6190c8
commit a664d7c884
4 changed files with 96 additions and 37 deletions

View File

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

View File

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

79
Src/gui/file_browser.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <string.h>
#include <utils.hh>
#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);
}

View File

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