From 32b8f82aa85a9f616eef7b6442670d8558a0b4d8 Mon Sep 17 00:00:00 2001 From: Krow Savcik Date: Thu, 11 Jan 2024 16:00:40 +0200 Subject: improved: new file picking function A new static function choose_file_path in user.c Now import/export/open/save functions all call this function. --- src/user.c | 130 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 65 insertions(+), 65 deletions(-) (limited to 'src') diff --git a/src/user.c b/src/user.c index d5a545e..ab86010 100644 --- a/src/user.c +++ b/src/user.c @@ -14,6 +14,8 @@ #include "canvas.h" #include "palette.h" +static void choose_file_path(const char *, char *); + void user_canvas_zoom_change(const Arg *x) { @@ -48,13 +50,10 @@ user_canvas_save(const Arg *x) if (cur_canvas == NULL) return; if (cur_canvas->proj_path != NULL) goto SAVENONSP; - printf("Project File Name (save): "); - fflush(stdout); - do { - fgets(line, 1024, stdin); - } while (strlen(line) < 4); - line[strcspn(line, "\n")] = '\0'; + choose_file_path("Project File Name (save): ", line); + if (line[0] == '\0') return; + printf("Saved project to file %s\n", line); canvas_save(cur_canvas, line, 1); return; @@ -67,44 +66,36 @@ void user_canvas_open(const Arg *x) { char line[1024]; - canvas_destroy(cur_canvas); + char *z = x->s; - if (x->s == NULL) { - printf("File Name (open): "); - fflush(stdout); - - do { - fgets(line, 1024, stdin); - } while (strlen(line) < 4); - line[strcspn(line, "\n")] = '\0'; - cur_canvas = canvas_open(line, ren); - } else { - cur_canvas = canvas_open(x->s, ren); + if (z == NULL) { + choose_file_path("Project File Name (open): ", line); + z = line; } + if (z[0] == '\0') return; + canvas_destroy(cur_canvas); + cur_canvas = canvas_open(z, ren); + /* TODO: show error window */ - if (cur_canvas == NULL) - puts("Error opening file"); - ui_redraw_panel(UI_PANELTYPE_CANVAS); + if (cur_canvas == NULL) + puts("Error opening file"); + ui_redraw_panel(UI_PANELTYPE_CANVAS); } void user_canvas_export_png(const Arg *x) { + puts("called"); char line[1024]; if (cur_canvas == NULL) return; - printf("File Name (export): "); - fflush(stdout); + choose_file_path("Image File Name (export): ", line); - do { - fgets(line, 1024, stdin); - } while (strlen(line) < 4); - line[strcspn(line, "\n")] = '\0'; /* TODO: show error window */ - if (canvas_export_png(cur_canvas, line, ren)) - puts("Error while saving file"); - else - puts("File saved"); + if (canvas_export_png(cur_canvas, line, ren)) + puts("Error while saving file"); + else + puts("File saved"); } void @@ -119,52 +110,48 @@ user_canvas_refresh(const Arg *x) void user_canvas_create_new(const Arg *x) { - int w, h; - printf("Width: "); - fflush(stdout); - scanf("%d", &w); - printf("Height: "); - fflush(stdout); - scanf("%d", &h); - if (cur_canvas != NULL) - canvas_destroy(cur_canvas); - - cur_canvas = canvas_init(w, h, ren); - ui_redraw_panel(UI_PANELTYPE_CANVAS); + int w, h; + printf("Width: "); + fflush(stdout); + scanf("%d", &w); + printf("Height: "); + fflush(stdout); + scanf("%d", &h); + if (cur_canvas != NULL) + canvas_destroy(cur_canvas); + + cur_canvas = canvas_init(w, h, ren); + ui_redraw_panel(UI_PANELTYPE_CANVAS); } void user_canvas_import_png(const Arg *x) { - char line[1024]; - canvas_destroy(cur_canvas); - - if (x->s == NULL) { - printf("File Name (i): "); - fflush(stdout); + char line[1024]; + char *z = x->s; - do { - fgets(line, 1024, stdin); - } while (strlen(line) < 4); - line[strcspn(line, "\n")] = '\0'; - cur_canvas = canvas_import_png(line, ren); - } else { - cur_canvas = canvas_import_png(x->s, ren); - } + if (z == NULL) { + choose_file_path("Image File Name (import): ", line); + z = line; + } + + if (z[0] == '\0') return; + canvas_destroy(cur_canvas); + cur_canvas = canvas_import_png(z, ren); -/* TODO: show error window */ - if (cur_canvas == NULL) - puts("Error opening file"); - ui_redraw_panel(UI_PANELTYPE_CANVAS); + /* TODO: show error window */ + if (cur_canvas == NULL) + puts("Error opening file"); + ui_redraw_panel(UI_PANELTYPE_CANVAS); } void user_canvas_pal_col_chng(const Arg *x) { - cur_canvas->cur_col += ((Palette*)def_palette)->num; - cur_canvas->cur_col += x->i; - cur_canvas->cur_col %= ((Palette*)def_palette)->num; - ui_redraw_panel(UI_PANELTYPE_CANVAS); + cur_canvas->cur_col += ((Palette*)def_palette)->num; + cur_canvas->cur_col += x->i; + cur_canvas->cur_col %= ((Palette*)def_palette)->num; + ui_redraw_panel(UI_PANELTYPE_CANVAS); } void @@ -236,3 +223,16 @@ user_testing_reload_tex(const Arg *_x) ui_redraw(); } +static void +choose_file_path(const char *prompt, char *input) +{ + printf(prompt); + fflush(stdout); + + do { + fgets(input, 1024, stdin); + } while (strlen(input) < 4); + input[strcspn(input, "\n")] = '\0'; + + printf("%s\n", input); +} -- cgit v1.2.3