/* ngui_file.c: modal file Open/Save dialog for ngui canvas windows. * * The dialog owns a directory listing (the VFS root shows the mount table), * Up/Home/Refresh controls, a scrollable row list, a file-name field and * Cancel/Open|Save. It is not a separate window: the app draws it on top of * its own canvas and calls ngui_file_dialog() until it returns ACCEPTED or * CANCELLED. See ngui.h for the caller contract. */ #include "ngui.h" #include "ucmd_lib.h" #define FD_ROW_H 22 #define FD_PAD 16 #define FD_ID_NAME 1 #define FD_ID_UP 2 #define FD_ID_HOME 3 #define FD_ID_REFRESH 4 #define FD_ID_CANCEL 5 #define FD_ID_ACCEPT 6 #define FD_ID_ROW 0x100 static unsigned int fl_len(const char *s) { unsigned int n = 0; while (s && s[n]) n++; return n; } /* Copy s into d (always NUL terminated). Returns 0 when it had to truncate. */ static int fl_copy(char *d, const char *s, unsigned int cap) { unsigned int i = 0; if (!cap) return 0; while (s && s[i]) { if (i + 1 >= cap) { d[i] = '\0'; return 0; } d[i] = s[i]; i++; } d[i] = '\0'; return 1; } /* Join dir and name into an absolute path. Returns 0 if it does not fit. */ static int fl_join(const char *dir, const char *name, char *out, unsigned int cap) { unsigned int i = 0, j; if (!dir || !dir[0]) dir = "/"; if (name && name[0] == '/') return fl_copy(out, name, cap); while (dir[i]) { if (i + 1 >= cap) { out[0] = '\0'; return 0; } out[i] = dir[i]; i++; } if (i && out[i - 1] != '/') { if (i + 1 >= cap) { out[0] = '\0'; return 0; } out[i++] = '/'; } for (j = 0; name && name[j]; j++) { if (i + 1 >= cap) { out[0] = '\0'; return 0; } out[i++] = name[j]; } out[i] = '\0'; return 1; } static void fl_parent(const char *in, char *out, unsigned int cap) { unsigned int n, i, last = 0; fl_copy(out, in && in[0] ? in : "/", cap); n = fl_len(out); if (n > 1 && out[n - 1] == '/') out[--n] = '\0'; for (i = 0; i < n; i++) if (out[i] == '/') last = i; if (last == 0) { out[0] = '/'; out[1] = '\0'; } else { out[last] = '\0'; } if (!out[0]) fl_copy(out, "/", cap); } static const char *fl_base(const char *path) { const char *b = path; while (path && *path) { if (*path == '/') b = path + 1; path++; } return b; } static void fl_abs(const char *p, char *out, unsigned int cap) { char cwd[NGUI_FILE_PATH_MAX]; if (!p || !p[0]) { fl_copy(out, "/", cap); return; } if (p[0] == '/') { fl_copy(out, p, cap); return; } if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0]) fl_copy(cwd, "/", sizeof(cwd)); if (!fl_join(cwd, p, out, cap)) fl_copy(out, "/", cap); } static int fl_ci_cmp(const char *a, const char *b) { for (;;) { unsigned char ca = (unsigned char)*a++; unsigned char cb = (unsigned char)*b++; if (ca >= 'a' && ca <= 'z') ca = (unsigned char)(ca - 32); if (cb >= 'a' && cb <= 'z') cb = (unsigned char)(cb - 32); if (ca != cb) return (int)ca - (int)cb; if (!ca) return 0; } } static void fl_size_text(unsigned int bytes, char *out, unsigned int cap) { char tmp[12]; unsigned int n = 0, i = 0, j; if (bytes < 1024) { do { tmp[i++] = (char)('0' + bytes % 10); bytes /= 10; } while (bytes && i < sizeof(tmp)); for (j = 0; j < i && n + 1 < cap; j++) out[n++] = tmp[i - 1 - j]; if (n + 1 < cap) out[n++] = ' '; if (n + 1 < cap) out[n++] = 'B'; out[n] = '\0'; return; } if (bytes < 1024u * 1024u) { unsigned int kb = bytes / 1024; i = 0; do { tmp[i++] = (char)('0' + kb % 10); kb /= 10; } while (kb && i < sizeof(tmp)); for (j = 0; j < i && n + 1 < cap; j++) out[n++] = tmp[i - 1 - j]; if (n + 1 < cap) out[n++] = ' '; if (n + 1 < cap) out[n++] = 'K'; if (n + 1 < cap) out[n++] = 'B'; } else { unsigned int mb = bytes / (1024u * 1024u); i = 0; do { tmp[i++] = (char)('0' + mb % 10); mb /= 10; } while (mb && i < sizeof(tmp)); for (j = 0; j < i && n + 1 < cap; j++) out[n++] = tmp[i - 1 - j]; if (n + 1 < cap) out[n++] = ' '; if (n + 1 < cap) out[n++] = 'M'; if (n + 1 < cap) out[n++] = 'B'; } out[n] = '\0'; } static const char *fd_error_text(int r) { switch (r) { case SYS_ENODEV: return "No such device"; case SYS_ENOFS: return "No filesystem on the device"; case SYS_ENOENT: return "Folder not found"; case SYS_EINVAL: return "Not a folder"; case SYS_EIO: return "Disk read error"; default: return "Cannot open the folder"; } } static int fd_entry_before(const struct ngui_file_entry *a, const struct ngui_file_entry *b) { if (a->is_dir != b->is_dir) return a->is_dir > b->is_dir; return fl_ci_cmp(a->name, b->name) < 0; } static void fd_sort_entries(struct ngui_file_entry *list, unsigned int n) { unsigned int i; for (i = 1; i < n; i++) { struct ngui_file_entry key = list[i]; unsigned int j = i; while (j > 0 && fd_entry_before(&key, &list[j - 1])) { list[j] = list[j - 1]; j--; } list[j] = key; } } /* Load a directory into the dialog. The previous path and listing are kept * when listing fails, so the error can be shown without leaving the user in * a dead folder. */ static int fd_load_dir(struct ngui_file_dialog *fd, const char *path) { struct ngui_file_entry tmp[NGUI_FILE_MAX_ENTRIES]; char dir[NGUI_FILE_PATH_MAX]; unsigned int n = 0, i, dl; int truncated = 0; if (!fd) return -1; if (!path || !path[0]) path = "/"; if (!fl_copy(dir, path, sizeof(dir))) { fl_copy(fd->error, "Path is too long", sizeof(fd->error)); return -1; } dl = fl_len(dir); if (dl > 1 && dir[dl - 1] == '/') dir[--dl] = '\0'; if (dir[0] == '/' && dir[1] == '\0') { struct sys_mount m; for (i = 0; ; i++) { if (u_syscall2(SYS_LIST_MOUNTS, (int)i, (int)&m) != 1) break; if (!m.name[0]) continue; if (n >= NGUI_FILE_MAX_ENTRIES) { truncated = 1; break; } fl_copy(tmp[n].name, m.name, sizeof(tmp[n].name)); tmp[n].size = 0; tmp[n].is_dir = 1; tmp[n].is_mount = 1; n++; } } else { struct sys_dirent d; for (i = 0; ; i++) { int r = u_syscall3(SYS_LIST_DIR, (int)dir, (int)i, (int)&d); if (r != 1) { if (r < 0) { fl_copy(fd->error, fd_error_text(r), sizeof(fd->error)); return -1; } break; } if (n >= NGUI_FILE_MAX_ENTRIES) { truncated = 1; break; } { char disp[NGUI_FILE_NAME_MAX]; if (d.raw_83) u_fat_name(d.name, disp, sizeof(disp)); else fl_copy(disp, d.name, sizeof(disp)); if (!disp[0] || disp[0] == '.') continue; fl_copy(tmp[n].name, disp, sizeof(tmp[n].name)); tmp[n].size = d.size; tmp[n].is_dir = d.is_dir ? 1 : 0; tmp[n].is_mount = 0; n++; } } } fd_sort_entries(tmp, n); for (i = 0; i < n; i++) fd->entries[i] = tmp[i]; fd->count = n; fd->truncated = truncated; fd->selected = -1; fd->last_click = -1; fd->last_click_ms = 0; fl_copy(fd->path, dir, sizeof(fd->path)); fd->scroll.offset = 0; fd->scroll.dragging = 0; fd->scroll.bar_vis = 0; fd->scroll.bar_target = 0; fd->scroll.bar_hot = 0; fd->error[0] = '\0'; return 0; } static void fd_set_name(struct ngui_file_dialog *fd, const char *name) { ngui_input_set(&fd->name, name ? name : ""); if (fd->name.len) { fd->name.anchor = 0; fd->name.cursor = fd->name.len; } } static void fd_default_dir(int mode, char *out, unsigned int cap) { struct sys_stat st; if (mode == NGUI_FILE_SAVE && u_stat("/share", &st) == 0 && st.is_dir) { fl_copy(out, "/share", cap); return; } if (mode == NGUI_FILE_OPEN && u_getcwd(out, cap) == 0 && out[0] && u_stat(out, &st) == 0 && st.is_dir) return; fl_copy(out, "/", cap); } static void fd_focus_name(struct ngui *ui, struct ngui_file_dialog *fd) { unsigned int id = fd->id_base + FD_ID_NAME; ngui_set_focus(ui, id); ui->focus_claimed = 1; ui->last_blink = u_ticks_ms(); ui->caret_on = 1; } /* Resolve the name field (or the highlighted entry) to an absolute path and * either navigate to a directory or accept it. Returns 1 on accept. */ static int fd_accept(struct ngui_file_dialog *fd) { char path[NGUI_FILE_PATH_MAX]; const char *name = fd->name.text; struct sys_stat st; int r; if (!name[0]) { const struct ngui_file_entry *e; if (fd->selected < 0 || fd->selected >= (int)fd->count) { fl_copy(fd->error, "Enter a file name", sizeof(fd->error)); return 0; } e = &fd->entries[fd->selected]; if (!fl_join(fd->path, e->name, path, sizeof(path))) { fl_copy(fd->error, "Path is too long", sizeof(fd->error)); return 0; } if (e->is_dir) { if (fd_load_dir(fd, path) == 0) fd_set_name(fd, ""); return 0; } name = e->name; } if (!fl_join(fd->path, name, path, sizeof(path))) { fl_copy(fd->error, "Path is too long", sizeof(fd->error)); return 0; } r = u_stat(path, &st); if (r == 0 && st.is_dir) { if (fd_load_dir(fd, path) == 0) fd_set_name(fd, ""); return 0; } if (fd->mode == NGUI_FILE_OPEN) { if (r != 0) { fl_copy(fd->error, "File not found", sizeof(fd->error)); return 0; } } else if (r != 0 && r != SYS_ENOENT) { fl_copy(fd->error, fd_error_text(r), sizeof(fd->error)); return 0; } else if (r != 0) { /* Saving a new file: make sure the parent exists. */ char parent[NGUI_FILE_PATH_MAX]; fl_parent(path, parent, sizeof(parent)); if (u_stat(parent, &st) != 0 || !st.is_dir) { fl_copy(fd->error, "Folder not found", sizeof(fd->error)); return 0; } } if (fl_len(path) >= sizeof(fd->result)) { fl_copy(fd->error, "Path is too long", sizeof(fd->error)); return 0; } fl_copy(fd->result, path, sizeof(fd->result)); return 1; } static void fd_move_selection(struct ngui *ui, struct ngui_file_dialog *fd, int delta) { int i; if (!fd->count) return; if (fd->selected < 0) fd->selected = delta > 0 ? 0 : (int)fd->count - 1; else { fd->selected += delta; if (fd->selected < 0) fd->selected = 0; if (fd->selected >= (int)fd->count) fd->selected = (int)fd->count - 1; } i = fd->selected; if (fd->entries[i].is_dir) fd_set_name(fd, ""); else fd_set_name(fd, fd->entries[i].name); fd->error[0] = '\0'; fd_focus_name(ui, fd); ngui_scroll_ensure_visible(&fd->scroll, 2 + i * FD_ROW_H, FD_ROW_H); ngui_invalidate(ui); } void ngui_file_dialog_open(struct ngui_file_dialog *fd, int mode, const char *start_path, const char *suggested_name, unsigned int id_base) { char abs[NGUI_FILE_PATH_MAX], dir[NGUI_FILE_PATH_MAX]; char nm[NGUI_FILE_NAME_MAX]; struct sys_stat st; unsigned int i; if (!fd) return; for (i = 0; i < sizeof(*fd); i++) ((unsigned char *)fd)[i] = 0; fd->active = 1; fd->mode = mode == NGUI_FILE_SAVE ? NGUI_FILE_SAVE : NGUI_FILE_OPEN; fd->id_base = id_base ? id_base : 0x1000u; fd->last_click = -1; fd->need_focus = 1; fd->name.placeholder = mode == NGUI_FILE_OPEN ? "File name or path" : "File name"; if (start_path && start_path[0]) fl_abs(start_path, abs, sizeof(abs)); else abs[0] = '\0'; if (!abs[0]) { fd_default_dir(fd->mode, dir, sizeof(dir)); fl_copy(nm, suggested_name ? suggested_name : "", sizeof(nm)); } else if (u_stat(abs, &st) == 0 && st.is_dir) { fl_copy(dir, abs, sizeof(dir)); fl_copy(nm, suggested_name ? suggested_name : "", sizeof(nm)); } else { fl_parent(abs, dir, sizeof(dir)); fl_copy(nm, suggested_name && suggested_name[0] ? suggested_name : fl_base(abs), sizeof(nm)); } if (!dir[0] || fd_load_dir(fd, dir) != 0) { if (!dir[0] || fl_ci_cmp(dir, "/") != 0) fd_load_dir(fd, "/"); } fd_set_name(fd, nm); for (i = 0; i < fd->count; i++) { if (fl_ci_cmp(fd->entries[i].name, fd->name.text) == 0) { fd->selected = (int)i; break; } } } int ngui_file_dialog(struct ngui *ui, struct ngui_file_dialog *fd) { struct ngui_rect panel, list, r, name_r; char size_text[24]; int pw, ph, px, py, x, y, w, list_y, list_h, name_y, err_y, btn_y; int event = NGUI_FILE_NONE; int clicked_dir = -1, accept_after = 0; unsigned int id_name, i; int k; if (!ui || !fd || !fd->active) return NGUI_FILE_NONE; if (ui->focus == 0) fd_focus_name(ui, fd); id_name = fd->id_base + FD_ID_NAME; /* The name field must keep the focus for typing, but ngui_text_input * consumes every key it sees. Take the list-navigation keys first so * they still move the highlight. */ for (;;) { int page = fd->scroll.view_h / FD_ROW_H - 1; if (page < 1) page = 1; k = ngui_key_peek(ui); if (k == NGUI_KEY_UP) { ngui_key(ui); fd_move_selection(ui, fd, -1); } else if (k == NGUI_KEY_DOWN) { ngui_key(ui); fd_move_selection(ui, fd, 1); } else if (k == NGUI_KEY_PAGE_UP) { ngui_key(ui); fd_move_selection(ui, fd, -page); } else if (k == NGUI_KEY_PAGE_DOWN) { ngui_key(ui); fd_move_selection(ui, fd, page); } else { break; } } pw = ui->w - 40; if (pw > 720) pw = 720; if (pw < 320) pw = ui->w - 20; if (pw < 160) pw = 160; ph = ui->h - 40; if (ph > 480) ph = 480; if (ph < 260) ph = ui->h - 20; if (ph < 160) ph = 160; px = (ui->w - pw) / 2; py = (ui->h - ph) / 2; if (px < 0) px = 0; if (py < 0) py = 0; panel = (struct ngui_rect){ px, py, pw, ph }; ngui_set_cursor(ui, CURSOR_DEFAULT); ngui_fill_rect(ui, (struct ngui_rect){ 0, 0, ui->w, ui->h }, NGUI_RGB(8, 10, 16)); ngui_fill_round_rect(ui, panel, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, ui->theme.panel); ngui_frame_round(ui, panel, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, NGUI_RADIUS, 1, ui->theme.border); ngui_text(ui, px + FD_PAD, py + 14, fd->mode == NGUI_FILE_OPEN ? "Open file" : "Save as", ui->theme.text); x = px + FD_PAD; y = py + 40; r = (struct ngui_rect){ x, y, 28, 26 }; if (ngui_icon_button(ui, fd->id_base + FD_ID_UP, r, NGUI_ICON_UP)) { char parent[NGUI_FILE_PATH_MAX]; fl_parent(fd->path, parent, sizeof(parent)); if (fd_load_dir(fd, parent) == 0) fd_set_name(fd, ""); fd_focus_name(ui, fd); } x += 32; r.x = x; if (ngui_icon_button(ui, fd->id_base + FD_ID_HOME, r, NGUI_ICON_HOME)) { if (fd_load_dir(fd, "/") == 0) fd_set_name(fd, ""); fd_focus_name(ui, fd); } x += 32; r.x = x; if (ngui_icon_button(ui, fd->id_base + FD_ID_REFRESH, r, NGUI_ICON_REFRESH)) { fd_load_dir(fd, fd->path); fd_focus_name(ui, fd); } x += 38; w = px + pw - FD_PAD - x; if (w < 10) w = 10; ngui_text_ellipsis(ui, (struct ngui_rect){ x, y, w, 26 }, x, y + 5, fd->path, ui->theme.text_dim); list_y = py + 96; btn_y = py + ph - 44; err_y = btn_y - 22; name_y = err_y - 36; list_h = name_y - 10 - list_y; if (list_h < 40) list_h = 40; list = (struct ngui_rect){ px + FD_PAD, list_y, pw - 2 * FD_PAD, list_h }; ngui_text(ui, list.x + 6, list_y - 18, "Name", ui->theme.text_dim); { int sw = (int)ngui_text_width_ui(ui, "Size"); ngui_text(ui, list.x + list.w - sw - 10, list_y - 18, "Size", ui->theme.text_dim); } ngui_fill_rect(ui, list, ui->theme.bg); ngui_frame(ui, list, ui->theme.border); fd->scroll.view_h = list.h; fd->scroll.content_h = (int)fd->count * FD_ROW_H + 4; ngui_scroll_begin(ui, list, &fd->scroll); if (ngui_scroll_wheel(ui, list, &fd->scroll)) ui->dirty = 1; if (!fd->count) { ngui_text(ui, list.x + 10, list.y + 8, fd->truncated ? "No entries" : "This folder is empty", ui->theme.text_dim); } for (i = 0; i < fd->count; i++) { struct ngui_rect rr = { list.x + 2, list.y + 2 + (int)i * FD_ROW_H - fd->scroll.offset, list.w - 4, FD_ROW_H }; const struct ngui_file_entry *e = &fd->entries[i]; int hot, selected; int name_w; if (rr.y + rr.h <= list.y || rr.y >= list.y + list.h) continue; hot = ui->inside && ngui_rect_contains(&rr, ui->mouse_x, ui->mouse_y); selected = (int)i == fd->selected; if (selected) ngui_fill_rect(ui, rr, ui->theme.selection); else if (hot) ngui_fill_rect(ui, rr, ui->theme.panel_alt); ngui_icon(ui, (struct ngui_rect){ rr.x + 5, rr.y + 3, 16, 16 }, e->is_dir ? NGUI_ICON_FOLDER : NGUI_ICON_FILE, e->is_dir ? ui->theme.icon_folder : ui->theme.icon_file); if (e->is_dir) fl_copy(size_text, e->is_mount ? "Mount" : "Folder", sizeof(size_text)); else fl_size_text(e->size, size_text, sizeof(size_text)); { int sw = (int)ngui_text_width_ui(ui, size_text); int tx = rr.x + rr.w - sw - 10; if (tx < rr.x + 28) tx = rr.x + 28; ngui_text(ui, tx, rr.y + 3, size_text, selected ? ui->theme.text : ui->theme.text_dim); name_w = tx - (rr.x + 28) - 6; } if (name_w < 10) name_w = 10; ngui_text_ellipsis(ui, (struct ngui_rect){ rr.x + 28, rr.y, name_w, rr.h }, rr.x + 28, rr.y + 3, e->name, ui->theme.text); if (hot) ui->hot = fd->id_base + FD_ID_ROW + i; if (ui->pressed && ngui_rect_contains(&rr, ui->press_x, ui->press_y)) { unsigned int now = u_ticks_ms(); int dbl = fd->last_click == (int)i && fd->last_click_ms && (unsigned int)(now - fd->last_click_ms) <= 500; fd->selected = (int)i; fd->last_click = (int)i; fd->last_click_ms = now; fd->error[0] = '\0'; fd_focus_name(ui, fd); if (e->is_dir) { clicked_dir = (int)i; } else { fd_set_name(fd, e->name); if (dbl) accept_after = 1; } ngui_invalidate(ui); } } ngui_scroll_end(ui, list, &fd->scroll); name_r = (struct ngui_rect){ px + FD_PAD + 86, name_y, pw - 2 * FD_PAD - 86, 28 }; if (name_r.w < 80) name_r.w = 80; ngui_text(ui, px + FD_PAD, name_y + 6, "File name", ui->theme.text_dim); { int input_result = ngui_text_input(ui, id_name, name_r, &fd->name); if (input_result & NGUI_INPUT_CHANGED) fd->error[0] = '\0'; if (input_result & NGUI_INPUT_SUBMIT) { if (fd_accept(fd)) event = NGUI_FILE_ACCEPTED; else ngui_invalidate(ui); } if ((input_result & NGUI_INPUT_CANCEL) && !(input_result & NGUI_INPUT_NEXT)) { event = NGUI_FILE_CANCELLED; fd->active = 0; } if (input_result & NGUI_INPUT_NEXT) fd_focus_name(ui, fd); } if (clicked_dir >= 0) { char path[NGUI_FILE_PATH_MAX]; if (fl_join(fd->path, fd->entries[clicked_dir].name, path, sizeof(path)) && fd_load_dir(fd, path) == 0) fd_set_name(fd, ""); clicked_dir = -1; } if (accept_after && event == NGUI_FILE_NONE) { if (fd_accept(fd)) event = NGUI_FILE_ACCEPTED; else ngui_invalidate(ui); } if (event == NGUI_FILE_NONE && ui->focus != id_name) { k = ngui_key_peek(ui); if (k == '\r' || k == '\n') { ngui_key(ui); if (fd_accept(fd)) event = NGUI_FILE_ACCEPTED; else ngui_invalidate(ui); } else if (k == NGUI_KEY_ESC) { ngui_key(ui); event = NGUI_FILE_CANCELLED; fd->active = 0; } } if (fd->error[0]) ngui_text_ellipsis(ui, (struct ngui_rect){ px + FD_PAD, err_y, pw - 2 * FD_PAD, 18 }, px + FD_PAD, err_y, fd->error, ui->theme.error); else if (fd->truncated) ngui_text(ui, px + FD_PAD, err_y, "Showing the first 128 entries", ui->theme.text_dim); if (ngui_button(ui, fd->id_base + FD_ID_CANCEL, (struct ngui_rect){ px + pw - 16 - 84 - 8 - 84, btn_y, 84, 28 }, "Cancel")) { event = NGUI_FILE_CANCELLED; fd->active = 0; } if (ngui_button(ui, fd->id_base + FD_ID_ACCEPT, (struct ngui_rect){ px + pw - 16 - 84, btn_y, 84, 28 }, fd->mode == NGUI_FILE_OPEN ? "Open" : "Save")) { if (fd_accept(fd)) event = NGUI_FILE_ACCEPTED; else ngui_invalidate(ui); fd_focus_name(ui, fd); } if (fd->need_focus) { fd->need_focus = 0; fd_focus_name(ui, fd); if (fd->selected >= 0) ngui_scroll_ensure_visible(&fd->scroll, 2 + fd->selected * FD_ROW_H, FD_ROW_H); } return event; } void ngui_file_dialog_close(struct ngui_file_dialog *fd) { if (!fd) return; fd->active = 0; fd->error[0] = '\0'; } void ngui_file_dialog_error(struct ngui_file_dialog *fd, const char *msg) { if (!fd) return; fl_copy(fd->error, msg ? msg : "Cannot use that path", sizeof(fd->error)); fd->active = 1; fd->need_focus = 1; } const char *ngui_file_dialog_result(const struct ngui_file_dialog *fd) { return fd ? fd->result : ""; }