Further improve file_browser.c
-Adds visible boundary to file icon box; -Adds a 'cancel' button that closes the file dialog; -Adds a 'save' button that should be used for the save() routine defined by the user;
This commit is contained in:
parent
ceee8839ab
commit
a1df0c4f57
@ -1,3 +1,5 @@
|
|||||||
|
nk_bool file_browser_is_open;
|
||||||
|
|
||||||
struct icons {
|
struct icons {
|
||||||
struct nk_image desktop;
|
struct nk_image desktop;
|
||||||
struct nk_image home;
|
struct nk_image home;
|
||||||
@ -325,6 +327,7 @@ file_browser_reload_directory_content(struct file_browser *browser, const char *
|
|||||||
static void
|
static void
|
||||||
file_browser_init(struct file_browser *browser, struct media *media)
|
file_browser_init(struct file_browser *browser, struct media *media)
|
||||||
{
|
{
|
||||||
|
file_browser_is_open = nk_true;
|
||||||
memset(browser, 0, sizeof(*browser));
|
memset(browser, 0, sizeof(*browser));
|
||||||
browser->media = media;
|
browser->media = media;
|
||||||
{
|
{
|
||||||
@ -373,113 +376,127 @@ file_browser_run(struct file_browser *browser, struct nk_context *ctx)
|
|||||||
struct media *media = browser->media;
|
struct media *media = browser->media;
|
||||||
struct nk_rect total_space;
|
struct nk_rect total_space;
|
||||||
|
|
||||||
if (nk_begin(ctx, "File Browser", nk_rect(50, 50, 600, 400),
|
if (file_browser_is_open)
|
||||||
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
|
|
||||||
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
|
|
||||||
{
|
{
|
||||||
static float ratio[] = {0.25f, NK_UNDEFINED};
|
if (nk_begin(ctx, "File Browser", nk_rect(50, 50, 600, 400),
|
||||||
float spacing_x = ctx->style.window.spacing.x;
|
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|NK_WINDOW_NO_SCROLLBAR|
|
||||||
|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
|
||||||
/* output path directory selector in the menubar */
|
|
||||||
ctx->style.window.spacing.x = 0;
|
|
||||||
nk_menubar_begin(ctx);
|
|
||||||
{
|
{
|
||||||
char *d = browser->directory;
|
static float ratio[] = {0.25f, NK_UNDEFINED};
|
||||||
char *begin = d + 1;
|
float spacing_x = ctx->style.window.spacing.x;
|
||||||
nk_layout_row_dynamic(ctx, 25, 6);
|
|
||||||
while (*d++) {
|
/* output path directory selector in the menubar */
|
||||||
if (*d == '/') {
|
ctx->style.window.spacing.x = 0;
|
||||||
*d = '\0';
|
nk_menubar_begin(ctx);
|
||||||
if (nk_button_label(ctx, begin)) {
|
{
|
||||||
*d++ = '/'; *d = '\0';
|
char *d = browser->directory;
|
||||||
file_browser_reload_directory_content(browser, browser->directory);
|
char *begin = d + 1;
|
||||||
break;
|
nk_layout_row_dynamic(ctx, 25, 6);
|
||||||
|
while (*d++) {
|
||||||
|
if (*d == '/') {
|
||||||
|
*d = '\0';
|
||||||
|
if (nk_button_label(ctx, begin)) {
|
||||||
|
*d++ = '/'; *d = '\0';
|
||||||
|
file_browser_reload_directory_content(browser, browser->directory);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
*d = '/';
|
||||||
|
begin = d + 1;
|
||||||
}
|
}
|
||||||
*d = '/';
|
|
||||||
begin = d + 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
nk_menubar_end(ctx);
|
||||||
nk_menubar_end(ctx);
|
ctx->style.window.spacing.x = spacing_x;
|
||||||
ctx->style.window.spacing.x = spacing_x;
|
|
||||||
|
|
||||||
/* window layout */
|
/* window layout */
|
||||||
total_space = nk_window_get_content_region(ctx);
|
total_space = nk_window_get_content_region(ctx);
|
||||||
nk_layout_row(ctx, NK_DYNAMIC, total_space.h, 2, ratio);
|
nk_layout_row(ctx, NK_DYNAMIC, total_space.h - 40, 2, ratio);
|
||||||
|
|
||||||
nk_group_begin(ctx, "Special", NK_WINDOW_NO_SCROLLBAR);
|
nk_group_begin(ctx, "Special", NK_WINDOW_NO_SCROLLBAR);
|
||||||
{
|
{
|
||||||
struct nk_image home = media->icons.home;
|
struct nk_image home = media->icons.home;
|
||||||
struct nk_image desktop = media->icons.desktop;
|
struct nk_image desktop = media->icons.desktop;
|
||||||
struct nk_image computer = media->icons.computer;
|
struct nk_image computer = media->icons.computer;
|
||||||
|
|
||||||
nk_layout_row_dynamic(ctx, 40, 1);
|
nk_layout_row_dynamic(ctx, 40, 1);
|
||||||
if (nk_button_image_label(ctx, home, "home", NK_TEXT_CENTERED))
|
if (nk_button_image_label(ctx, home, "home", NK_TEXT_CENTERED))
|
||||||
file_browser_reload_directory_content(browser, browser->home);
|
file_browser_reload_directory_content(browser, browser->home);
|
||||||
if (nk_button_image_label(ctx,desktop,"desktop",NK_TEXT_CENTERED))
|
if (nk_button_image_label(ctx,desktop,"desktop",NK_TEXT_CENTERED))
|
||||||
file_browser_reload_directory_content(browser, browser->desktop);
|
file_browser_reload_directory_content(browser, browser->desktop);
|
||||||
if (nk_button_image_label(ctx,computer,"computer",NK_TEXT_CENTERED))
|
if (nk_button_image_label(ctx,computer,"computer",NK_TEXT_CENTERED))
|
||||||
file_browser_reload_directory_content(browser, "/");
|
file_browser_reload_directory_content(browser, "/");
|
||||||
nk_group_end(ctx);
|
nk_group_end(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* output directory content window */
|
/* output directory content window */
|
||||||
nk_group_begin(ctx, "Content", 0);
|
nk_group_begin(ctx, "Content", NK_WINDOW_BORDER);
|
||||||
{
|
{
|
||||||
int index = -1;
|
int index = -1;
|
||||||
size_t i = 0, j = 0;
|
size_t i = 0, j = 0;
|
||||||
size_t rows = 0, cols = 0;
|
size_t rows = 0, cols = 0;
|
||||||
size_t count = browser->dir_count + browser->file_count;
|
size_t count = browser->dir_count + browser->file_count;
|
||||||
|
|
||||||
/* File icons layout */
|
/* File icons layout */
|
||||||
cols = 2;
|
cols = 2;
|
||||||
rows = count / cols;
|
rows = count / cols;
|
||||||
static float ratio2[] = {0.08f, NK_UNDEFINED};
|
static float ratio2[] = {0.08f, NK_UNDEFINED};
|
||||||
nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio2);
|
nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio2);
|
||||||
for (i = 0; i <= rows; i += 1) {
|
for (i = 0; i <= rows; i += 1) {
|
||||||
size_t n = j + cols;
|
size_t n = j + cols;
|
||||||
for (; j < count && j < n; ++j) {
|
for (; j < count && j < n; ++j) {
|
||||||
/* draw one column of icons */
|
/* draw one column of icons */
|
||||||
if (j < browser->dir_count) {
|
if (j < browser->dir_count) {
|
||||||
/* draw and execute directory buttons */
|
/* draw and execute directory buttons */
|
||||||
if (nk_button_image(ctx,media->icons.directory))
|
if (nk_button_image(ctx,media->icons.directory))
|
||||||
index = (int)j;
|
index = (int)j;
|
||||||
|
|
||||||
nk_label(ctx, browser->directories[j], NK_TEXT_LEFT);
|
nk_label(ctx, browser->directories[j], NK_TEXT_LEFT);
|
||||||
} else {
|
} else {
|
||||||
/* draw and execute files buttons */
|
/* draw and execute files buttons */
|
||||||
struct nk_image *icon;
|
struct nk_image *icon;
|
||||||
size_t fileIndex = ((size_t)j - browser->dir_count);
|
size_t fileIndex = ((size_t)j - browser->dir_count);
|
||||||
icon = media_icon_for_file(media,browser->files[fileIndex]);
|
icon = media_icon_for_file(media,browser->files[fileIndex]);
|
||||||
if (nk_button_image(ctx, *icon)) {
|
if (nk_button_image(ctx, *icon)) {
|
||||||
strncpy(browser->file, browser->directory, MAX_PATH_LEN);
|
strncpy(browser->file, browser->directory, MAX_PATH_LEN);
|
||||||
n = strlen(browser->file);
|
n = strlen(browser->file);
|
||||||
strncpy(browser->file + n, browser->files[fileIndex], MAX_PATH_LEN - n);
|
strncpy(browser->file + n, browser->files[fileIndex], MAX_PATH_LEN - n);
|
||||||
ret = 1;
|
ret = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* draw one column of labels */
|
||||||
|
if (j >= browser->dir_count) {
|
||||||
|
size_t t = j - browser->dir_count;
|
||||||
|
nk_label(ctx,browser->files[t],NK_TEXT_LEFT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* draw one column of labels */
|
|
||||||
if (j >= browser->dir_count) {
|
|
||||||
size_t t = j - browser->dir_count;
|
|
||||||
nk_label(ctx,browser->files[t],NK_TEXT_LEFT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (index != -1) {
|
||||||
|
size_t n = strlen(browser->directory);
|
||||||
|
strncpy(browser->directory + n, browser->directories[index], MAX_PATH_LEN - n);
|
||||||
|
n = strlen(browser->directory);
|
||||||
|
if (n < MAX_PATH_LEN - 1) {
|
||||||
|
browser->directory[n] = '/';
|
||||||
|
browser->directory[n+1] = '\0';
|
||||||
|
}
|
||||||
|
file_browser_reload_directory_content(browser, browser->directory);
|
||||||
|
}
|
||||||
|
nk_group_end(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index != -1) {
|
nk_layout_row_dynamic(ctx, 30, 5);
|
||||||
size_t n = strlen(browser->directory);
|
nk_label(ctx,"",NK_TEXT_LEFT);
|
||||||
strncpy(browser->directory + n, browser->directories[index], MAX_PATH_LEN - n);
|
nk_label(ctx,"",NK_TEXT_LEFT);
|
||||||
n = strlen(browser->directory);
|
nk_label(ctx,"",NK_TEXT_LEFT);
|
||||||
if (n < MAX_PATH_LEN - 1) {
|
if(nk_button_label(ctx, "Cancel"))
|
||||||
browser->directory[n] = '/';
|
{
|
||||||
browser->directory[n+1] = '\0';
|
fprintf(stdout, "File dialog has been closed!\n");
|
||||||
}
|
file_browser_is_open = nk_false;
|
||||||
file_browser_reload_directory_content(browser, browser->directory);
|
|
||||||
}
|
}
|
||||||
nk_group_end(ctx);
|
if(nk_button_label(ctx, "Save"))
|
||||||
|
fprintf(stdout, "Insert routine to open/save the file!\n");
|
||||||
}
|
}
|
||||||
|
nk_end(ctx);
|
||||||
}
|
}
|
||||||
nk_end(ctx);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user