commit 7c608da9bfe2af6e0501294c79ffa759dd499f4a
parent 13cd14167c23038863e6c83a8621c5dac43df869
Author: bsandro <email@bsandro.tech>
Date: Sat, 30 Aug 2025 00:33:44 +0300
remember last open dir
Diffstat:
3 files changed, 12 insertions(+), 20 deletions(-)
diff --git a/TODO b/TODO
@@ -15,5 +15,6 @@
- `-> copy the full path+name into the unified field
- M$ Windows build
- Use system open/save dialog on Windows?
-- Remember last open directory
++ Remember last open directory
- Bug: closing the modal window closes the application
+- Bug: large amount of frames doesn't let to choose 1frame height properly.
diff --git a/gui/ui.c b/gui/ui.c
@@ -11,6 +11,7 @@ static UIWindow *modal_win = NULL;
static UIImageDisplay *img_disp = NULL;
static UILabel *lbl_header = NULL;
static Animation *img = NULL;
+static char *current_path = NULL;
static int FilelistFilter(const struct dirent *f) {
if (strncmp(f->d_name, ".", 255)==0) return 0;
@@ -125,10 +126,9 @@ int ButtonDialogOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) {
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) {
- table->itemCount = scandir(dir_name, &filelist, FilelistFilter, FilelistCompare);
+ table->itemCount = scandir(element->cp, &filelist, FilelistFilter, FilelistCompare);
// printf("populated dir %s with %d items\n", dir_name, table->itemCount);
}
if (msg==UI_MSG_TABLE_GET_ITEM) {
@@ -150,6 +150,7 @@ int TableEvent(UIElement *element, UIMessage msg, int di, void *dp) {
strcat(newpath, filelist[hit]->d_name);
free(element->cp);
element->cp = realpath(newpath, NULL);
+ current_path = element->cp;
//free(filelist);
filelist = NULL;
selected = -1;
@@ -205,7 +206,8 @@ int ButtonCloseEvent(UIElement *element, UIMessage msg, int di, void *dp) {
return 0;
}
-void ShowModalWindow(UIWindow *parent, const char *def_dir, const char *def_file, CallbackFn cb) {
+void ShowModalWindow(UIWindow *parent, char *def_dir, const char *def_file, CallbackFn cb) {
+ assert(def_dir!=NULL);
if (modal_win==NULL) {
// printf("create modal window\n");
int w = UIElementMessage(&parent->e, UI_MSG_GET_WIDTH, 0, 0);
@@ -216,10 +218,7 @@ void ShowModalWindow(UIWindow *parent, const char *def_dir, const char *def_file
UILabel *lbl_title = UILabelCreate(&panel_out->e, 0, def_file?"Save File":"Open File", -1);
UIPanel *panel_top = UIPanelCreate(&panel_out->e, UI_PANEL_GRAY|UI_PANEL_MEDIUM_SPACING|UI_PANEL_HORIZONTAL);
UITextbox *path_input = UITextboxCreate(&panel_top->e, UI_ELEMENT_DISABLED|UI_ELEMENT_H_FILL);
- if (def_dir!=NULL) {
- UITextboxReplace(path_input, def_dir, -1, false);
- }
-
+ UITextboxReplace(path_input, def_dir, -1, false);
UITextbox *filename_input = UITextboxCreate(&panel_top->e, UI_ELEMENT_TAB_STOP|UI_ELEMENT_H_FILL);
printf("path_input: %p (%s)\nfilename_input: %p (%s)\n", path_input, def_dir, filename_input, def_file);
@@ -235,16 +234,7 @@ void ShowModalWindow(UIWindow *parent, const char *def_dir, const char *def_file
UITable *table = UITableCreate(&panel_out->e, UI_ELEMENT_V_FILL, "Directory");
table->itemCount = 1; // at least '..' element
- // @todo setting the initial directory this way is 100% trashcan
- char *init_path = NULL;
- if (def_dir==NULL) {
- init_path = malloc(sizeof(char)*2);
- init_path[0] = '.';
- init_path[1] = '\0';
- } else {
- init_path = strndup(def_dir, PATH_MAX-1);
- }
- table->e.cp = (void *)init_path;
+ table->e.cp = def_dir;
table->e.messageUser = TableEvent;
UITableResizeColumns(table);
} else {
@@ -265,8 +255,9 @@ int ButtonSaveEvent(UIElement *element, UIMessage msg, int di, void *dp) {
int ButtonOpenEvent(UIElement *element, UIMessage msg, int di, void *dp) {
if (msg==UI_MSG_CLICKED) {
+ char *dir = current_path==NULL ? strndup(getenv("HOME"), strlen(getenv("HOME"))) : current_path;
// printf("open button clicked\n");
- ShowModalWindow(element->window, getenv("HOME"), NULL, ButtonDialogOpenEvent);
+ ShowModalWindow(element->window, dir, NULL, ButtonDialogOpenEvent);
}
return 0;
}
diff --git a/gui/ui.h b/gui/ui.h
@@ -16,7 +16,7 @@ typedef int (*CallbackFn)(struct UIElement *element, UIMessage message, int di,
UIWindow * MainWindowCreate(const char *wname, int w, int h);
void PreviewUpdate(Animation *, UIImageDisplay *);
-void ShowModalWindow(UIWindow *parent, const char *def_dir, const char *def_file, CallbackFn cb);
+void ShowModalWindow(UIWindow *parent, char *def_dir, const char *def_file, CallbackFn cb);
int WinMainEvent(UIElement *element, UIMessage msg, int di, void *dp);
int WinModalEvent(UIElement *element, UIMessage msg, int di, void *dp);
int SliderEvent(UIElement *element, UIMessage msg, int di, void *dp);