From e60c12736184ed6b4801254caa0621356101edd3 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Fri, 17 Oct 2025 10:37:34 +0000 Subject: [PATCH] add files to list git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@395 b9cfdab3-6d41-4d17-bbe4-086880011989 --- src/filechooser.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/filechooser.c b/src/filechooser.c index 6527055..0dc5489 100644 --- a/src/filechooser.c +++ b/src/filechooser.c @@ -1,6 +1,8 @@ /* $Id$ */ #include +#include "../external/stb_ds.h" + typedef struct filechooser { MwWidget nav; MwWidget files; @@ -234,8 +236,27 @@ static void resize(MwWidget handle, void* user, void* call) { layout(handle); } +static int qsort_files(const void* a, const void* b){ + MwDirectoryEntry* aent = *(MwDirectoryEntry**)a; + MwDirectoryEntry* bent = *(MwDirectoryEntry**)b; + + return strcmp(aent->name, bent->name); +} + static void scan(MwWidget handle, const char* path) { filechooser_t* fc = handle->opaque; + void* dir = MwDirectoryOpen(path); + MwDirectoryEntry** entries = NULL; + int i; + char** names = NULL; + MwLLPixmap* icons = NULL; + if(dir != NULL){ + MwDirectoryEntry* entry; + while((entry = MwDirectoryRead(dir)) != NULL) arrput(entries, entry); + MwDirectoryClose(dir); + + qsort(entries, arrlen(entries), sizeof(MwDirectoryEntry*), qsort_files); + } MwVaApply(fc->addr, MwNtext, path, @@ -246,6 +267,28 @@ static void scan(MwWidget handle, const char* path) { MwListBoxSetWidth(fc->files, 0, -128 - 64); MwListBoxSetWidth(fc->files, 1, 128); MwListBoxSetWidth(fc->files, 2, 0); + + icons = NULL; + names = NULL; + for(i = 0; i < arrlen(entries); i++){ + if(strcmp(entries[i]->name, ".") == 0 || strcmp(entries[i]->name, "..") == 0) continue; + if(entries[i]->type == MwDIRECTORY_DIRECTORY){ + arrput(names, entries[i]->name); + arrput(icons, fc->dir); + } + } + for(i = 0; i < arrlen(entries); i++){ + if(entries[i]->type == MwDIRECTORY_FILE){ + arrput(names, entries[i]->name); + arrput(icons, fc->file); + } + } + MwListBoxInsertMultiple(fc->files, -1, arrlen(names), icons, names, NULL); + arrfree(names); + arrfree(icons); + + for(i = 0; i < arrlen(entries); i++) MwDirectoryFreeEntry(entries[i]); + arrfree(entries); } MwWidget MwFileChooser(MwWidget handle, const char* title) {