emote2ss

Animated webp to spritesheets converting tool
git clone git://bsandro.tech/emote2ss
Log | Files | Refs | README | LICENSE

commit bd128ed3a63cfc84aa38a42b26768c994d02fe44
parent ca53d7d32806bb43437c9c401bb1ae32215ab960
Author: bsandro <[email protected]>
Date:   Mon,  2 Sep 2024 00:57:32 +0300

Dirty implementation of file browser (incomplete)

Diffstat:
Mgui/main.c | 57+++++++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 47 insertions(+), 10 deletions(-)

diff --git a/gui/main.c b/gui/main.c @@ -3,6 +3,7 @@ */ // #define _GNU_SOURCE +#define _DEFAULT_SOURCE // moved into the compiler command line arguments // #define UI_LINUX @@ -18,6 +19,7 @@ #include <string.h> #include <strings.h> #include <assert.h> +#include <dirent.h> #include "luigi.h" #include "animation.h" @@ -208,19 +210,53 @@ int ButtonDialogOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) { return 0; } +/* +@todo +- display list of files and directories in open directory +- click on directory 'opens' it +- OR first click on directory highlights it, second - opens it +- open directory full path is being copied into the path field above +- list has .. element to move up in the hierarchy +- click on filename highlights it and copies the name into file name field above +- unify path and filename fields (more canonical way) +- use system open/save dialog on Windows + */ int TableEvent(UIElement *element, UIMessage msg, int di, void *dp) { static int selected = -1; + static struct dirent **filelist = NULL; + char *dir_name = element->cp; + UITable *table = (UITable *)element; + if (filelist==NULL) { + //@todo sort directories before files in list + table->itemCount = scandir(dir_name, &filelist, NULL, alphasort); + printf("populate dir %s with %d items\n", dir_name, table->itemCount); + } if (msg == UI_MSG_TABLE_GET_ITEM) { - //printf("table get item\n"); UITableGetItem *m = (UITableGetItem *)dp; m->isSelected = selected==m->index; - //printf("m->index = %d\n", m->index); - return snprintf(m->buffer, m->bufferBytes, "dir %d", m->index); - } else if (msg == UI_MSG_LEFT_DOWN) { - int hit = UITableHitTest((UITable *)element, element->window->cursorX, element->window->cursorY); - if (selected!=hit) { + bool is_dir = filelist[m->index]->d_type==DT_DIR; + return snprintf(m->buffer, m->bufferBytes, is_dir?"[%s]":"%s", filelist[m->index]->d_name); + } else if (msg == UI_MSG_CLICKED) { + int hit = UITableHitTest(table, element->window->cursorX, element->window->cursorY); + if (selected==hit) { + if (hit!=-1) { + printf("second click on selected item %d (%s)\n", hit, filelist[hit]->d_name); + if (filelist[hit]->d_type==DT_DIR) { + char newpath[PATH_MAX]; + bzero(newpath, PATH_MAX); + strcpy(newpath, element->cp); + strcat(newpath, "/"); + strcat(newpath, filelist[hit]->d_name); + //element->cp = realpath(filelist[hit]->d_name, NULL); + element->cp = realpath(newpath, NULL); + printf("new dir: %s\n", element->cp); + //@todo cleanup filelist! + filelist = NULL; + } + } + } else { selected = hit; - if (!UITableEnsureVisible((UITable *)element, selected)) { + if (!UITableEnsureVisible(table, selected)) { UIElementRepaint(element, NULL); } } @@ -266,10 +302,11 @@ void ShowModalWindow(UIWindow *parent, const char *def_dir, const char *def_file UIButton *btn_cancel = UIButtonCreate(&panel_top->e, UI_ELEMENT_TAB_STOP, "Cancel", -1); btn_cancel->e.messageUser = ButtonCloseEvent; - /*UITable *table = UITableCreate(&panel_out->e, UI_ELEMENT_V_FILL, "Directory"); - table->itemCount = 100; + UITable *table = UITableCreate(&panel_out->e, UI_ELEMENT_V_FILL, "Directory"); + table->itemCount = 1; // at least '..' element + table->e.cp = "."; table->e.messageUser = TableEvent; - UITableResizeColumns(table);*/ + UITableResizeColumns(table); } else { UIElementFocus(modal_win); }