editor: explorador de arquivos (tinyblocks-editor-files) para Abrir/Salvar
Nova library privada do bundle do editor (dlopen + hot reload) com um diálogo modal no lugar da barra de caminho: locais, trilha clicável, lista filtrada por extensão, campo de nome e confirmação ao substituir. - start_path() abre o explorador em todos os modos (projeto, exportar, imagem, código, .so); sem o módulo volta para a barra de caminho. - O switch do Enter virou path_commit(), usado pelos dois caminhos. - Estado em ed->fdlg para sobreviver à recarga do módulo. - Empacotado no AppImage; 6 cenários novos no harness headless. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
adf5d2cd12
commit
f0abc7593a
13 files changed
+1286
-46
No files matched your search
@@ -0,0 +1,43 @@
|
||||
# Makefile da library privada tinyblocks-editor-files
|
||||
# Bundle "tinyblocks-editor". Constroi .a e .so.
|
||||
CC := gcc
|
||||
BUILD := build
|
||||
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic \
|
||||
-DTB_EDITOR_FILES_BUILD \
|
||||
-D_POSIX_C_SOURCE=200809L \
|
||||
-I../../../../include -Iinclude \
|
||||
-I../../internal \
|
||||
-I../tinyblocks-editor-widgets/include \
|
||||
-I../tinyblocks-editor-image/include \
|
||||
-I../../../tinyblocks-graphics/include \
|
||||
-I../../../tinyblocks-variables/include \
|
||||
-I../../../tinyblocks-blueprint/include \
|
||||
-I../../../tinyblocks-environ/include \
|
||||
-I../../../tinyblocks-api/include \
|
||||
-I../../../tinyblocks-libman/include \
|
||||
-I../../../tinyblocks-syntax/include
|
||||
|
||||
OBJS := $(BUILD)/tb_editor_files.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-editor-files.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-editor-files.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
all: static shared
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(BUILD)/tb_editor_files.o: src/tb_editor_files.c include/tb_editor_files.h \
|
||||
../../internal/tb_editor_internal.h \
|
||||
../tinyblocks-editor-widgets/include/tb_editor_widgets.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(LIB_A): $(OBJS)
|
||||
ar rcs $@ $^
|
||||
|
||||
$(LIB_SO): $(OBJS)
|
||||
$(CC) -shared -o $@ $^ -Wl,-z,lazy
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD)
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* TinyBlocks - tb_editor_files.h
|
||||
* Explorador de arquivos do editor (library privada do bundle
|
||||
* tinyblocks-editor): dialogo modal usado por Abrir/Salvar projeto,
|
||||
* Exportar, abrir/salvar imagem e codigo e carregar library (.so).
|
||||
*
|
||||
* O estado mora em Ed (ed->fdlg, ver tb_editor_internal.h), entao o .so
|
||||
* pode ser recarregado em execucao sem perder a pasta ou a selecao.
|
||||
* Ao confirmar, o dialogo chama path_commit(ed, mode, caminho) do editor.
|
||||
*/
|
||||
#ifndef TB_EDITOR_FILES_H
|
||||
#define TB_EDITOR_FILES_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <tinyblocks/tb_renderable_block.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Ed;
|
||||
typedef struct Ed Ed;
|
||||
|
||||
/* Abre o dialogo para o modo PM_* com o nome/caminho sugerido `def`
|
||||
(pode trazer pasta: "dir/arquivo.png"). */
|
||||
void tb_editor_files_open (Ed* ed, int mode, const char* def);
|
||||
/* Fecha sem confirmar (libera a listagem) */
|
||||
void tb_editor_files_close (Ed* ed);
|
||||
|
||||
/* Desenha o dialogo (nada se estiver fechado) */
|
||||
void tb_editor_files_view (Ed* ed, TinyBlocks_RenderableBlock* rb);
|
||||
|
||||
/* Entrada enquanto o dialogo esta aberto */
|
||||
bool tb_editor_files_click (Ed* ed, float mx, float my);
|
||||
void tb_editor_files_key (Ed* ed, int sc, bool ctrl);
|
||||
void tb_editor_files_text (Ed* ed, const char* typed);
|
||||
void tb_editor_files_scroll(Ed* ed, float dy);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* TB_EDITOR_FILES_H */
|
||||
@@ -0,0 +1,17 @@
|
||||
# Explorador de arquivos do editor (bundle "tinyblocks-editor").
|
||||
[library]
|
||||
name = "tinyblocks-editor-files"
|
||||
bundle = "tinyblocks-editor"
|
||||
private = true
|
||||
version = "1.0.0"
|
||||
category = "Editor"
|
||||
description = "Explorador de arquivos: dialogos de Abrir/Salvar/Exportar"
|
||||
color = [76, 151, 255, 255]
|
||||
dark = [61, 121, 204, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/tb_editor_files.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-editor-files.so"
|
||||
static = "build/libtinyblocks-editor-files.a"
|
||||
depends = ["tinyblocks-editor-widgets"]
|
||||
@@ -0,0 +1,962 @@
|
||||
/*
|
||||
* TinyBlocks - tb_editor_files.c
|
||||
* Explorador de arquivos do editor: dialogo modal estilo Scratch com
|
||||
* locais (pasta inicial, Documentos, ...), trilha da pasta atual clicavel,
|
||||
* lista de pastas/arquivos filtrada por extensao, campo de nome e
|
||||
* confirmacao de substituicao ao salvar.
|
||||
*
|
||||
* Library privada do bundle "tinyblocks-editor": o estado mora em
|
||||
* ed->fdlg, aqui so ha codigo (hot reload troca o .so sem perder nada).
|
||||
* Ao confirmar chama path_commit(ed, mode, caminho) do editor.
|
||||
*/
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <strings.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../../../internal/tb_editor_internal.h"
|
||||
#include "tb_editor_files.h"
|
||||
|
||||
/* -- Geometria ------------------------------------------------------------ */
|
||||
#define FD_HEAD_H 46
|
||||
#define FD_BAR_H 44
|
||||
#define FD_SIDE_W 196
|
||||
#define FD_COLH_H 26
|
||||
#define FD_ROW_H 26
|
||||
#define FD_FOOT_H 100
|
||||
#define FD_PAD 14
|
||||
#define FD_BTN_W 104
|
||||
#define FD_BTN_H 32
|
||||
|
||||
static const Col COL_FD_FOLDER = C(255,191, 38);
|
||||
static const Col COL_FD_FOLDER_DARK = C(222,150, 10);
|
||||
static const Col COL_FD_SIDE = C(242,246,252);
|
||||
static const Col COL_FD_JSON = C(255,140, 26);
|
||||
static const Col COL_FD_IMAGE = C( 15,189,140);
|
||||
static const Col COL_FD_LIB = C(153,102,255);
|
||||
static const Col COL_FD_CODE = C( 76,151,255);
|
||||
static const Col COL_FD_ERR = C(220, 60, 60);
|
||||
|
||||
typedef struct {
|
||||
float x, y, w, h;
|
||||
float up_x, bar_y, crumb_x, crumb_w;
|
||||
float side_x, side_y, side_h;
|
||||
float list_x, list_y, list_w, list_h, colh_y;
|
||||
int rows;
|
||||
float name_x, name_y, name_w;
|
||||
float row2_y;
|
||||
float chk_all_x, chk_hid_x;
|
||||
float ok_x, cancel_x;
|
||||
float close_cx, close_cy;
|
||||
} FdL;
|
||||
|
||||
static void fd_layout(Ed* ed, FdL* L) {
|
||||
float w = WIN_W - 48.f, h = WIN_H - 48.f;
|
||||
if (w > 900.f) w = 900.f;
|
||||
if (h > 580.f) h = 580.f;
|
||||
L->w = w; L->h = h;
|
||||
L->x = (WIN_W - w) * 0.5f;
|
||||
L->y = (WIN_H - h) * 0.5f;
|
||||
L->close_cx = L->x + w - 26.f;
|
||||
L->close_cy = L->y + FD_HEAD_H * 0.5f;
|
||||
L->bar_y = L->y + FD_HEAD_H + 7.f;
|
||||
L->up_x = L->x + FD_PAD;
|
||||
L->crumb_x = L->up_x + 40.f;
|
||||
L->crumb_w = L->x + w - FD_PAD - L->crumb_x;
|
||||
float top = L->y + FD_HEAD_H + FD_BAR_H;
|
||||
float bot = L->y + h - FD_FOOT_H;
|
||||
L->side_x = L->x;
|
||||
L->side_y = top;
|
||||
L->side_h = bot - top;
|
||||
L->list_x = L->x + FD_SIDE_W;
|
||||
L->list_w = w - FD_SIDE_W;
|
||||
L->colh_y = top;
|
||||
L->list_y = top + FD_COLH_H;
|
||||
L->list_h = bot - L->list_y;
|
||||
L->rows = (int)(L->list_h / FD_ROW_H);
|
||||
if (L->rows < 1) L->rows = 1;
|
||||
L->name_x = L->x + FD_PAD + 56.f;
|
||||
L->name_y = bot + 14.f;
|
||||
L->name_w = L->x + w - FD_PAD - L->name_x;
|
||||
L->row2_y = bot + 56.f;
|
||||
L->chk_all_x = L->x + FD_PAD;
|
||||
L->chk_hid_x = L->x + FD_PAD + 210.f;
|
||||
L->ok_x = L->x + w - FD_PAD - FD_BTN_W;
|
||||
L->cancel_x = L->ok_x - 10.f - FD_BTN_W;
|
||||
}
|
||||
|
||||
/* -- Utilidades ----------------------------------------------------------- */
|
||||
|
||||
static unsigned fd_now_ms(void) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (unsigned)(ts.tv_sec * 1000u + ts.tv_nsec / 1000000u);
|
||||
}
|
||||
|
||||
static bool fd_is_dir(const char* p) {
|
||||
struct stat st;
|
||||
return stat(p, &st) == 0 && S_ISDIR(st.st_mode);
|
||||
}
|
||||
|
||||
static bool fd_exists(const char* p) {
|
||||
struct stat st;
|
||||
return stat(p, &st) == 0;
|
||||
}
|
||||
|
||||
static void fd_join(char* out, size_t cap, const char* dir, const char* name) {
|
||||
size_t n = strlen(dir);
|
||||
if (n > 0 && dir[n - 1] == '/') snprintf(out, cap, "%s%s", dir, name);
|
||||
else snprintf(out, cap, "%s/%s", dir, name);
|
||||
}
|
||||
|
||||
/* Normaliza um caminho absoluto: tira "//", "." e resolve ".." */
|
||||
static void fd_normalize(char* p) {
|
||||
char out[1024];
|
||||
size_t o = 0;
|
||||
const char* s = p;
|
||||
out[o++] = '/';
|
||||
while (*s) {
|
||||
while (*s == '/') s++;
|
||||
if (!*s) break;
|
||||
const char* e = s;
|
||||
while (*e && *e != '/') e++;
|
||||
size_t len = (size_t)(e - s);
|
||||
if (len == 1 && s[0] == '.') {
|
||||
/* ignora */
|
||||
} else if (len == 2 && s[0] == '.' && s[1] == '.') {
|
||||
if (o > 1) {
|
||||
o--; /* tira a '/' final */
|
||||
while (o > 1 && out[o - 1] != '/') o--;
|
||||
}
|
||||
} else if (o + len + 1 < sizeof(out)) {
|
||||
if (o > 1) out[o++] = '/';
|
||||
memcpy(out + o, s, len);
|
||||
o += len;
|
||||
}
|
||||
s = e;
|
||||
}
|
||||
out[o] = 0;
|
||||
scopy(p, 1024, out);
|
||||
}
|
||||
|
||||
/* Resolve o texto do campo (relativo a pasta atual, "~" = HOME) */
|
||||
static void fd_resolve(Ed* ed, const char* in, char out[1024]) {
|
||||
const char* home = getenv("HOME");
|
||||
if (in[0] == '/') scopy(out, 1024, in);
|
||||
else if (in[0] == '~' && (in[1] == '/' || !in[1]) && home)
|
||||
snprintf(out, 1024, "%s%s", home, in + 1);
|
||||
else fd_join(out, 1024, ed->fdlg.dir, in);
|
||||
fd_normalize(out);
|
||||
}
|
||||
|
||||
static const char* fd_ext(const char* name) {
|
||||
const char* d = strrchr(name, '.');
|
||||
return (d && d != name) ? d : "";
|
||||
}
|
||||
|
||||
/* O arquivo passa no filtro (".png .jpg")? */
|
||||
static bool fd_match(const FileDlg* F, const char* name) {
|
||||
if (F->all_files || !F->filter[0]) return true;
|
||||
const char* e = fd_ext(name);
|
||||
if (!e[0]) return false;
|
||||
size_t el = strlen(e);
|
||||
const char* f = F->filter;
|
||||
while (*f) {
|
||||
while (*f == ' ') f++;
|
||||
const char* fe = f;
|
||||
while (*fe && *fe != ' ') fe++;
|
||||
if ((size_t)(fe - f) == el && strncasecmp(f, e, el) == 0) return true;
|
||||
f = fe;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Primeira extensao do filtro (para completar o nome ao salvar) */
|
||||
static void fd_first_ext(const FileDlg* F, char* out, size_t cap) {
|
||||
const char* f = F->filter;
|
||||
while (*f == ' ') f++;
|
||||
size_t n = 0;
|
||||
while (f[n] && f[n] != ' ') n++;
|
||||
if (n >= cap) n = cap - 1;
|
||||
memcpy(out, f, n);
|
||||
out[n] = 0;
|
||||
}
|
||||
|
||||
/* Corta o texto com "..." para caber em maxw */
|
||||
static void fd_fit(char* out, size_t cap, const char* s, float fs, float maxw) {
|
||||
if (tw(s, fs) <= maxw) { scopy(out, cap, s); return; }
|
||||
size_t n = strlen(s);
|
||||
char tmp[512];
|
||||
tmp[0] = 0;
|
||||
while (n > 0) {
|
||||
n--;
|
||||
while (n > 0 && ((unsigned char)s[n] & 0xC0) == 0x80) n--;
|
||||
snprintf(tmp, sizeof(tmp), "%.*s...", (int)n, s);
|
||||
if (tw(tmp, fs) <= maxw) break;
|
||||
}
|
||||
scopy(out, cap, tmp);
|
||||
}
|
||||
|
||||
static void fd_size_str(char* out, size_t cap, long long sz) {
|
||||
if (sz < 1024) snprintf(out, cap, "%lld B", sz);
|
||||
else if (sz < 1024LL * 1024) snprintf(out, cap, "%.1f KB", sz / 1024.0);
|
||||
else if (sz < 1024LL * 1024 * 1024) snprintf(out, cap, "%.1f MB", sz / (1024.0 * 1024.0));
|
||||
else snprintf(out, cap, "%.1f GB", sz / (1024.0 * 1024.0 * 1024.0));
|
||||
}
|
||||
|
||||
static void fd_time_str(char* out, size_t cap, long long t) {
|
||||
time_t tt = (time_t)t;
|
||||
struct tm tmv;
|
||||
if (!localtime_r(&tt, &tmv)) { scopy(out, cap, ""); return; }
|
||||
strftime(out, cap, "%d/%m/%Y %H:%M", &tmv);
|
||||
}
|
||||
|
||||
/* -- Listagem ------------------------------------------------------------- */
|
||||
|
||||
static int fd_cmp(const void* a, const void* b) {
|
||||
const FileEnt* x = a;
|
||||
const FileEnt* y = b;
|
||||
if (x->is_dir == 2) return -1;
|
||||
if (y->is_dir == 2) return 1;
|
||||
if (x->is_dir != y->is_dir) return y->is_dir - x->is_dir;
|
||||
return strcasecmp(x->name, y->name);
|
||||
}
|
||||
|
||||
static bool fd_push(FileDlg* F, const FileEnt* e) {
|
||||
if (F->n == F->cap) {
|
||||
int nc = F->cap ? F->cap * 2 : 64;
|
||||
FileEnt* ne = realloc(F->ents, (size_t)nc * sizeof(FileEnt));
|
||||
if (!ne) return false;
|
||||
F->ents = ne;
|
||||
F->cap = nc;
|
||||
}
|
||||
F->ents[F->n++] = *e;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void fd_scan(Ed* ed) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
F->n = 0;
|
||||
F->sel = -1;
|
||||
F->scroll = 0;
|
||||
F->last_click_i = -1;
|
||||
DIR* d = opendir(F->dir);
|
||||
if (!d) {
|
||||
snprintf(F->err, sizeof(F->err), "Nao foi possivel abrir a pasta: %.120s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
if (strcmp(F->dir, "/") != 0) {
|
||||
FileEnt up;
|
||||
memset(&up, 0, sizeof(up));
|
||||
scopy(up.name, sizeof(up.name), "..");
|
||||
up.is_dir = 2;
|
||||
fd_push(F, &up);
|
||||
}
|
||||
struct dirent* de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
const char* nm = de->d_name;
|
||||
if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0) continue;
|
||||
if (nm[0] == '.' && !F->show_hidden) continue;
|
||||
char full[1300];
|
||||
fd_join(full, sizeof(full), F->dir, nm);
|
||||
struct stat st;
|
||||
if (stat(full, &st) != 0) continue; /* link quebrado etc */
|
||||
FileEnt e;
|
||||
memset(&e, 0, sizeof(e));
|
||||
scopy(e.name, sizeof(e.name), nm);
|
||||
e.is_dir = S_ISDIR(st.st_mode) ? 1 : 0;
|
||||
e.size = (long long)st.st_size;
|
||||
e.mtime = (long long)st.st_mtime;
|
||||
if (!e.is_dir && !fd_match(F, nm)) continue;
|
||||
if (!fd_push(F, &e)) break;
|
||||
}
|
||||
closedir(d);
|
||||
qsort(F->ents, (size_t)F->n, sizeof(FileEnt), fd_cmp);
|
||||
|
||||
/* pre-seleciona o arquivo do campo de nome, se estiver na lista */
|
||||
for (int i = 0; i < F->n; i++)
|
||||
if (F->name[0] && strcmp(F->ents[i].name, F->name) == 0) { F->sel = i; break; }
|
||||
}
|
||||
|
||||
static void fd_chdir(Ed* ed, const char* path) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
char p[1024];
|
||||
scopy(p, sizeof(p), path);
|
||||
fd_normalize(p);
|
||||
if (!fd_is_dir(p)) {
|
||||
snprintf(F->err, sizeof(F->err), "Pasta nao encontrada: %.150s", p);
|
||||
return;
|
||||
}
|
||||
scopy(F->dir, sizeof(F->dir), p);
|
||||
F->err[0] = 0;
|
||||
F->confirm = false;
|
||||
fd_scan(ed);
|
||||
}
|
||||
|
||||
static void fd_parent(Ed* ed) {
|
||||
char p[1024];
|
||||
fd_join(p, sizeof(p), ed->fdlg.dir, "..");
|
||||
fd_chdir(ed, p);
|
||||
}
|
||||
|
||||
static void fd_enter(Ed* ed, int i) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
if (i < 0 || i >= F->n || !F->ents[i].is_dir) return;
|
||||
if (F->ents[i].is_dir == 2) { fd_parent(ed); return; }
|
||||
char p[1024];
|
||||
fd_join(p, sizeof(p), F->dir, F->ents[i].name);
|
||||
fd_chdir(ed, p);
|
||||
}
|
||||
|
||||
/* Locais da barra lateral (so os que existem) */
|
||||
static void fd_add_place(FileDlg* F, const char* label, const char* path) {
|
||||
if (F->place_n >= FD_PLACES || !path || !path[0] || !fd_is_dir(path)) return;
|
||||
for (int i = 0; i < F->place_n; i++)
|
||||
if (strcmp(F->place_path[i], path) == 0) return;
|
||||
scopy(F->place_label[F->place_n], sizeof(F->place_label[0]), label);
|
||||
scopy(F->place_path[F->place_n], sizeof(F->place_path[0]), path);
|
||||
F->place_n++;
|
||||
}
|
||||
|
||||
static void fd_add_home_place(FileDlg* F, const char* home, const char* label,
|
||||
const char* a, const char* b) {
|
||||
char p[512];
|
||||
snprintf(p, sizeof(p), "%s/%s", home, a);
|
||||
if (fd_is_dir(p)) { fd_add_place(F, label, p); return; }
|
||||
if (!b) return;
|
||||
snprintf(p, sizeof(p), "%s/%s", home, b);
|
||||
fd_add_place(F, label, p);
|
||||
}
|
||||
|
||||
static void fd_places(Ed* ed) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
F->place_n = 0;
|
||||
const char* home = getenv("HOME");
|
||||
char cwd[512];
|
||||
if (home && home[0]) {
|
||||
fd_add_place(F, "Pasta pessoal", home);
|
||||
fd_add_home_place(F, home, "Área de Trabalho", "Área de Trabalho", "Desktop");
|
||||
fd_add_home_place(F, home, "Documentos", "Documentos", "Documents");
|
||||
fd_add_home_place(F, home, "Downloads", "Downloads", NULL);
|
||||
fd_add_home_place(F, home, "Imagens", "Imagens", "Pictures");
|
||||
}
|
||||
if (getcwd(cwd, sizeof(cwd))) fd_add_place(F, "Pasta de trabalho", cwd);
|
||||
fd_add_place(F, "Raiz do sistema", "/");
|
||||
}
|
||||
|
||||
/* -- Trilha da pasta atual (segmentos clicaveis) ------------------------- */
|
||||
#define FD_MAX_CRUMBS 48
|
||||
typedef struct {
|
||||
int n, first; /* segmentos; primeiro visivel */
|
||||
int start[FD_MAX_CRUMBS], end[FD_MAX_CRUMBS];
|
||||
float x[FD_MAX_CRUMBS], w[FD_MAX_CRUMBS];
|
||||
bool ellipsis;
|
||||
} FdCrumbs;
|
||||
|
||||
static void fd_crumb_label(const FileDlg* F, const FdCrumbs* K, int i, char* out, size_t cap) {
|
||||
if (i == 0) { scopy(out, cap, "/"); return; }
|
||||
int len = K->end[i] - K->start[i];
|
||||
snprintf(out, cap, "%.*s", len, F->dir + K->start[i]);
|
||||
}
|
||||
|
||||
#define FD_CRUMB_SEP 14.f
|
||||
static void fd_crumbs(Ed* ed, const FdL* L, FdCrumbs* K) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
K->n = 0;
|
||||
K->start[0] = 0; K->end[0] = 1; /* raiz */
|
||||
K->n = 1;
|
||||
const char* d = F->dir;
|
||||
int i = 1;
|
||||
while (d[i] && K->n < FD_MAX_CRUMBS) {
|
||||
while (d[i] == '/') i++;
|
||||
if (!d[i]) break;
|
||||
int s = i;
|
||||
while (d[i] && d[i] != '/') i++;
|
||||
K->start[K->n] = s;
|
||||
K->end[K->n] = i;
|
||||
K->n++;
|
||||
}
|
||||
float avail = L->crumb_w - 12.f;
|
||||
float wsum = 0;
|
||||
K->first = K->n - 1;
|
||||
for (int k = K->n - 1; k >= 0; k--) {
|
||||
char lb[256];
|
||||
fd_crumb_label(F, K, k, lb, sizeof(lb));
|
||||
float w = tw(lb, FS_UI) + 16.f;
|
||||
if (w > avail - 40.f) w = avail - 40.f;
|
||||
K->w[k] = w;
|
||||
float need = wsum + w + (k < K->n - 1 ? FD_CRUMB_SEP : 0);
|
||||
if (k < K->n - 1 && need > avail - 40.f) break;
|
||||
wsum = need;
|
||||
K->first = k;
|
||||
}
|
||||
K->ellipsis = K->first > 0;
|
||||
float x = L->crumb_x + 6.f + (K->ellipsis ? 30.f : 0.f);
|
||||
for (int k = K->first; k < K->n; k++) {
|
||||
K->x[k] = x;
|
||||
x += K->w[k] + (k == 0 ? 2.f : FD_CRUMB_SEP);
|
||||
}
|
||||
}
|
||||
|
||||
/* -- Acoes ---------------------------------------------------------------- */
|
||||
|
||||
static void fd_ensure_visible(Ed* ed) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
FdL L;
|
||||
fd_layout(ed, &L);
|
||||
if (F->sel < 0) return;
|
||||
if (F->sel < F->scroll) F->scroll = F->sel;
|
||||
if (F->sel >= F->scroll + L.rows) F->scroll = F->sel - L.rows + 1;
|
||||
if (F->scroll < 0) F->scroll = 0;
|
||||
}
|
||||
|
||||
static void fd_free(FileDlg* F) {
|
||||
free(F->ents);
|
||||
F->ents = NULL;
|
||||
F->n = F->cap = 0;
|
||||
}
|
||||
|
||||
void tb_editor_files_close(Ed* ed) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
F->open = false;
|
||||
F->confirm = false;
|
||||
fd_free(F);
|
||||
}
|
||||
|
||||
static void fd_commit(Ed* ed, const char* full) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
int mode = F->mode;
|
||||
char path[1024];
|
||||
scopy(path, sizeof(path), full);
|
||||
/* lembra a pasta do arquivo escolhido para a proxima vez */
|
||||
char* sl = strrchr(path, '/');
|
||||
if (sl) {
|
||||
if (sl == path) scopy(F->dir, sizeof(F->dir), "/");
|
||||
else snprintf(F->dir, sizeof(F->dir), "%.*s", (int)(sl - path), path);
|
||||
}
|
||||
tb_editor_files_close(ed);
|
||||
path_commit(ed, mode, path);
|
||||
}
|
||||
|
||||
/* Confirma: entra em pasta, ou abre/salva o arquivo do campo de nome.
|
||||
force = ja confirmou "substituir". */
|
||||
static void fd_accept(Ed* ed, bool force) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
F->err[0] = 0;
|
||||
if (!F->name[0]) {
|
||||
if (F->sel >= 0 && F->sel < F->n && F->ents[F->sel].is_dir) fd_enter(ed, F->sel);
|
||||
return;
|
||||
}
|
||||
char full[1024];
|
||||
fd_resolve(ed, F->name, full);
|
||||
if (fd_is_dir(full)) { /* digitou uma pasta: entra nela */
|
||||
fd_chdir(ed, full);
|
||||
scopy(F->name, sizeof(F->name), F->save ? F->def_name : "");
|
||||
return;
|
||||
}
|
||||
if (F->save) {
|
||||
const char* base = strrchr(full, '/');
|
||||
base = base ? base + 1 : full;
|
||||
if (!fd_ext(base)[0] && F->filter[0] && !F->all_files) {
|
||||
char ext[24];
|
||||
fd_first_ext(F, ext, sizeof(ext));
|
||||
size_t n = strlen(full);
|
||||
if (n + strlen(ext) < sizeof(full)) memcpy(full + n, ext, strlen(ext) + 1);
|
||||
}
|
||||
char parent[1024];
|
||||
scopy(parent, sizeof(parent), full);
|
||||
char* sl = strrchr(parent, '/');
|
||||
if (sl) { if (sl == parent) sl[1] = 0; else *sl = 0; }
|
||||
if (!fd_is_dir(parent)) {
|
||||
snprintf(F->err, sizeof(F->err), "A pasta nao existe: %.150s", parent);
|
||||
return;
|
||||
}
|
||||
if (fd_exists(full) && !force) {
|
||||
const char* b = strrchr(full, '/');
|
||||
scopy(F->name, sizeof(F->name), F->name[0] == '/' ? full : (b ? b + 1 : full));
|
||||
F->confirm = true;
|
||||
return;
|
||||
}
|
||||
} else if (!fd_exists(full)) {
|
||||
snprintf(F->err, sizeof(F->err), "Arquivo nao encontrado: %.150s", F->name);
|
||||
return;
|
||||
}
|
||||
fd_commit(ed, full);
|
||||
}
|
||||
|
||||
static void fd_mode_info(FileDlg* F, int mode) {
|
||||
const char* title = "Abrir arquivo";
|
||||
const char* ok = "Abrir";
|
||||
const char* filter = "";
|
||||
bool save = false;
|
||||
switch (mode) {
|
||||
case PM_LIB: title = "Carregar library"; ok = "Carregar"; filter = ".so"; break;
|
||||
case PM_LOAD: title = "Abrir projeto"; filter = ".json"; break;
|
||||
case PM_SAVE: title = "Salvar projeto"; ok = "Salvar"; filter = ".json"; save = true; break;
|
||||
case PM_EXP_JSON: title = "Exportar como JSON"; ok = "Exportar"; filter = ".json"; save = true; break;
|
||||
case PM_EXP_XML: title = "Exportar como XML"; ok = "Exportar"; filter = ".xml"; save = true; break;
|
||||
case PM_EXP_CSV: title = "Exportar como CSV"; ok = "Exportar"; filter = ".csv"; save = true; break;
|
||||
case PM_IMGLOAD: title = "Abrir imagem"; filter = ".png .jpg .jpeg .bmp .ppm .svg"; break;
|
||||
case PM_IMSAVE: title = "Salvar imagem"; ok = "Salvar"; filter = ".png .jpg .jpeg .bmp .ppm"; save = true; break;
|
||||
case PM_IMADDR: title = "Adicionar camada raster"; ok = "Adicionar"; filter = ".png .jpg .jpeg .bmp .ppm .svg"; break;
|
||||
case PM_IMADDV: title = "Adicionar camada vetorial"; ok = "Adicionar"; filter = ".svg"; break;
|
||||
case PM_CODELOAD: title = "Abrir código"; break;
|
||||
case PM_CODESAVE: title = "Salvar código"; ok = "Salvar"; save = true; break;
|
||||
default: break;
|
||||
}
|
||||
scopy(F->title, sizeof(F->title), title);
|
||||
scopy(F->ok_label, sizeof(F->ok_label), ok);
|
||||
scopy(F->filter, sizeof(F->filter), filter);
|
||||
F->save = save;
|
||||
}
|
||||
|
||||
void tb_editor_files_open(Ed* ed, int mode, const char* def) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
fd_free(F);
|
||||
fd_mode_info(F, mode);
|
||||
F->open = true;
|
||||
F->mode = mode;
|
||||
F->confirm = false;
|
||||
F->all_files = false;
|
||||
F->err[0] = 0;
|
||||
|
||||
char cwd[1024];
|
||||
if (!getcwd(cwd, sizeof(cwd))) scopy(cwd, sizeof(cwd), "/");
|
||||
if (!F->dir[0] || !fd_is_dir(F->dir)) scopy(F->dir, sizeof(F->dir), cwd);
|
||||
|
||||
/* o nome sugerido pode trazer a pasta ("imgs/saida.png") */
|
||||
const char* base = def ? def : "";
|
||||
const char* sl = strrchr(base, '/');
|
||||
if (sl) {
|
||||
char d[2048];
|
||||
if (base[0] == '/') snprintf(d, sizeof(d), "%.*s", (int)(sl - base), base);
|
||||
else snprintf(d, sizeof(d), "%s/%.*s", cwd, (int)(sl - base), base);
|
||||
if (!d[0]) scopy(d, sizeof(d), "/");
|
||||
fd_normalize(d);
|
||||
if (fd_is_dir(d)) scopy(F->dir, sizeof(F->dir), d);
|
||||
base = sl + 1;
|
||||
}
|
||||
scopy(F->def_name, sizeof(F->def_name), base);
|
||||
scopy(F->name, sizeof(F->name), base);
|
||||
|
||||
fd_places(ed);
|
||||
fd_scan(ed);
|
||||
/* abrir: so sugere o nome se o arquivo estiver aqui */
|
||||
if (!F->save && F->sel < 0) F->name[0] = 0;
|
||||
fd_ensure_visible(ed);
|
||||
}
|
||||
|
||||
/* -- Desenho -------------------------------------------------------------- */
|
||||
|
||||
static void fd_icon_folder(TinyBlocks_RenderableBlock* rb, float x, float y, bool dim) {
|
||||
Col c = COL_FD_FOLDER, d = COL_FD_FOLDER_DARK;
|
||||
if (dim) { c.a = 0.55f; d.a = 0.55f; }
|
||||
rb_fill_rrect(rb, x, y + 1, 9, 6, 2, d);
|
||||
rb_fill_rrect(rb, x, y + 4, 19, 13, 2, c);
|
||||
}
|
||||
|
||||
static Col fd_type_col(const char* name) {
|
||||
const char* e = fd_ext(name);
|
||||
if (!strcasecmp(e, ".json") || !strcasecmp(e, ".xml") || !strcasecmp(e, ".csv")) return COL_FD_JSON;
|
||||
if (!strcasecmp(e, ".png") || !strcasecmp(e, ".jpg") || !strcasecmp(e, ".jpeg") ||
|
||||
!strcasecmp(e, ".bmp") || !strcasecmp(e, ".ppm") || !strcasecmp(e, ".svg")) return COL_FD_IMAGE;
|
||||
if (!strcasecmp(e, ".so")) return COL_FD_LIB;
|
||||
if (!strcasecmp(e, ".c") || !strcasecmp(e, ".h") || !strcasecmp(e, ".py") ||
|
||||
!strcasecmp(e, ".md") || !strcasecmp(e, ".toml")) return COL_FD_CODE;
|
||||
return COL_TEXT_DIM;
|
||||
}
|
||||
|
||||
static void fd_icon_file(TinyBlocks_RenderableBlock* rb, float x, float y, Col band) {
|
||||
rb_fill_rrect(rb, x + 2, y, 15, 18, 2, COL_BORDER);
|
||||
rb_fill_rrect(rb, x + 3, y + 1, 13, 16, 1.5f, COL_WHITE);
|
||||
rb_fill_rect(rb, x + 3, y + 11, 13, 5, band);
|
||||
rb_line(rb, x + 6, y + 5, x + 13, y + 5, 1, COL_BORDER);
|
||||
rb_line(rb, x + 6, y + 8, x + 13, y + 8, 1, COL_BORDER);
|
||||
}
|
||||
|
||||
static void fd_checkbox(TinyBlocks_RenderableBlock* rb, float x, float cy, bool on, const char* label) {
|
||||
float s = 16;
|
||||
if (on) {
|
||||
rb_fill_rrect(rb, x, cy - s * 0.5f, s, s, 3, COL_MENU);
|
||||
rb_line(rb, x + 4, cy, x + 7, cy + 4, 2, COL_WHITE);
|
||||
rb_line(rb, x + 7, cy + 4, x + 12, cy - 4, 2, COL_WHITE);
|
||||
} else {
|
||||
rb_fill_rrect(rb, x, cy - s * 0.5f, s, s, 3, COL_BORDER);
|
||||
rb_fill_rrect(rb, x + 1.5f, cy - s * 0.5f + 1.5f, s - 3, s - 3, 2, COL_WHITE);
|
||||
}
|
||||
rb_text_c(rb, x + s + 8, cy, label, COL_TEXT, FS_UI);
|
||||
}
|
||||
|
||||
static void fd_button(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h,
|
||||
const char* label, bool primary, bool hov, Col accent) {
|
||||
if (primary) {
|
||||
rb_fill_rrect(rb, x, y, w, h, h * 0.5f, hov ? col_mul(accent, 0.86f) : accent);
|
||||
rb_text_c(rb, x + (w - tw(label, FS_UI)) * 0.5f, y + h * 0.5f, label, COL_WHITE, FS_UI);
|
||||
} else {
|
||||
rb_fill_rrect(rb, x, y, w, h, h * 0.5f, COL_BORDER);
|
||||
rb_fill_rrect(rb, x + 1, y + 1, w - 2, h - 2, h * 0.5f - 1, hov ? COL_HOVER : COL_WHITE);
|
||||
rb_text_c(rb, x + (w - tw(label, FS_UI)) * 0.5f, y + h * 0.5f, label, COL_TEXT, FS_UI);
|
||||
}
|
||||
}
|
||||
|
||||
/* Caixa de confirmacao "substituir?" (geometria compartilhada com o clique) */
|
||||
static void fd_confirm_geom(const FdL* L, float* bx, float* by, float* bw, float* bh) {
|
||||
*bw = 420; *bh = 150;
|
||||
*bx = L->x + (L->w - *bw) * 0.5f;
|
||||
*by = L->y + (L->h - *bh) * 0.5f;
|
||||
}
|
||||
|
||||
void tb_editor_files_view(Ed* ed, TinyBlocks_RenderableBlock* rb) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
if (!F->open) return;
|
||||
FdL L;
|
||||
fd_layout(ed, &L);
|
||||
float mx = ed->mouse_x, my = ed->mouse_y;
|
||||
bool modal_free = !F->confirm;
|
||||
char buf[512];
|
||||
|
||||
rb_fill_rect(rb, 0, 0, WIN_W, WIN_H, COLA(0, 0, 0, 120));
|
||||
rb_fill_rrect(rb, L.x + 3, L.y + 4, L.w, L.h, 12, COLA(0, 0, 0, 70));
|
||||
rb_fill_rrect(rb, L.x, L.y, L.w, L.h, 12, COL_PANEL);
|
||||
|
||||
/* Cabecalho */
|
||||
rb_fill_rrect(rb, L.x, L.y, L.w, FD_HEAD_H, 12, COL_MENU);
|
||||
rb_fill_rect(rb, L.x, L.y + FD_HEAD_H - 12, L.w, 12, COL_MENU);
|
||||
rb_text_c(rb, L.x + (L.w - tw(F->title, 18)) * 0.5f, L.y + FD_HEAD_H * 0.5f, F->title, COL_WHITE, 18);
|
||||
{
|
||||
bool hov = modal_free && hypotf(mx - L.close_cx, my - L.close_cy) < 14.f;
|
||||
rb_fill_circle(rb, L.close_cx, L.close_cy, 13, COLA(255, 255, 255, hov ? 110 : 60));
|
||||
rb_line(rb, L.close_cx - 5, L.close_cy - 5, L.close_cx + 5, L.close_cy + 5, 2, COL_WHITE);
|
||||
rb_line(rb, L.close_cx + 5, L.close_cy - 5, L.close_cx - 5, L.close_cy + 5, 2, COL_WHITE);
|
||||
}
|
||||
|
||||
/* Barra: pasta acima + trilha */
|
||||
{
|
||||
float by = L.bar_y, bh = 30;
|
||||
bool hov = modal_free && in_rect(mx, my, L.up_x, by, 32, bh);
|
||||
bool can_up = strcmp(F->dir, "/") != 0;
|
||||
rb_fill_rrect(rb, L.up_x, by, 32, bh, 6, hov && can_up ? COL_HOVER : COL_UI_BG);
|
||||
Col ac = can_up ? COL_TEXT : COL_TEXT_DIM;
|
||||
float cx = L.up_x + 16, cy = by + bh * 0.5f;
|
||||
rb_line(rb, cx, cy + 7, cx, cy - 7, 2, ac);
|
||||
rb_line(rb, cx - 6, cy - 1, cx, cy - 7, 2, ac);
|
||||
rb_line(rb, cx + 6, cy - 1, cx, cy - 7, 2, ac);
|
||||
|
||||
rb_fill_rrect(rb, L.crumb_x, by, L.crumb_w, bh, 6, COL_UI_BG);
|
||||
FdCrumbs K;
|
||||
fd_crumbs(ed, &L, &K);
|
||||
if (K.ellipsis)
|
||||
rb_text_c(rb, L.crumb_x + 10, cy, "...", COL_TEXT_DIM, FS_UI);
|
||||
for (int k = K.first; k < K.n; k++) {
|
||||
char lb[256], fit[256];
|
||||
fd_crumb_label(F, &K, k, lb, sizeof(lb));
|
||||
fd_fit(fit, sizeof(fit), lb, FS_UI, K.w[k] - 16.f);
|
||||
bool last = k == K.n - 1;
|
||||
bool h = modal_free && !last && in_rect(mx, my, K.x[k], by + 3, K.w[k], bh - 6);
|
||||
if (last) rb_fill_rrect(rb, K.x[k], by + 3, K.w[k], bh - 6, 5, COL_WHITE);
|
||||
else if (h) rb_fill_rrect(rb, K.x[k], by + 3, K.w[k], bh - 6, 5, COL_HOVER);
|
||||
rb_text_c(rb, K.x[k] + 8, cy, fit, last ? COL_TEXT : COL_MENU_DARK, FS_UI);
|
||||
if (!last && k > 0) rb_text_c(rb, K.x[k] + K.w[k] + 3, cy, "/", COL_TEXT_DIM, FS_UI);
|
||||
}
|
||||
}
|
||||
|
||||
/* Barra lateral: locais */
|
||||
rb_fill_rect(rb, L.side_x, L.side_y, FD_SIDE_W, L.side_h, COL_FD_SIDE);
|
||||
rb_line(rb, L.side_x + FD_SIDE_W, L.side_y, L.side_x + FD_SIDE_W, L.side_y + L.side_h, 1, COL_BORDER);
|
||||
rb_line(rb, L.x, L.side_y, L.x + L.w, L.side_y, 1, COL_BORDER);
|
||||
rb_text_c(rb, L.side_x + FD_PAD, L.side_y + 16, "Locais", COL_TEXT_DIM, FS_SM);
|
||||
for (int i = 0; i < F->place_n; i++) {
|
||||
float iy = L.side_y + 30 + i * 32;
|
||||
bool cur = strcmp(F->dir, F->place_path[i]) == 0;
|
||||
bool h = modal_free && in_rect(mx, my, L.side_x + 6, iy, FD_SIDE_W - 12, 30);
|
||||
if (cur || h) rb_fill_rrect(rb, L.side_x + 6, iy, FD_SIDE_W - 12, 30, 6, cur ? COL_HOVER : COLA(214, 228, 248, 140));
|
||||
if (cur) rb_fill_rrect(rb, L.side_x + 6, iy + 6, 3, 18, 1.5f, COL_MENU);
|
||||
fd_icon_folder(rb, L.side_x + 16, iy + 6, false);
|
||||
fd_fit(buf, sizeof(buf), F->place_label[i], FS_UI, FD_SIDE_W - 60);
|
||||
rb_text_c(rb, L.side_x + 42, iy + 15, buf, cur ? COL_MENU_DARK : COL_TEXT, FS_UI);
|
||||
}
|
||||
|
||||
/* Cabecalho das colunas */
|
||||
float col_size = L.list_x + L.list_w - 250.f;
|
||||
float col_time = L.list_x + L.list_w - 140.f;
|
||||
rb_text_c(rb, L.list_x + 44, L.colh_y + FD_COLH_H * 0.5f, "Nome", COL_TEXT_DIM, FS_SM);
|
||||
rb_text_c(rb, col_size, L.colh_y + FD_COLH_H * 0.5f, "Tamanho", COL_TEXT_DIM, FS_SM);
|
||||
rb_text_c(rb, col_time, L.colh_y + FD_COLH_H * 0.5f, "Modificado", COL_TEXT_DIM, FS_SM);
|
||||
rb_line(rb, L.list_x, L.list_y, L.list_x + L.list_w, L.list_y, 1, COL_BORDER);
|
||||
|
||||
/* Linhas */
|
||||
int end = F->scroll + L.rows;
|
||||
if (end > F->n) end = F->n;
|
||||
for (int i = F->scroll; i < end; i++) {
|
||||
FileEnt* e = &F->ents[i];
|
||||
float ry = L.list_y + (float)(i - F->scroll) * FD_ROW_H;
|
||||
bool sel = i == F->sel;
|
||||
bool h = modal_free && in_rect(mx, my, L.list_x, ry, L.list_w - 12, FD_ROW_H);
|
||||
if (sel) rb_fill_rrect(rb, L.list_x + 4, ry + 1, L.list_w - 18, FD_ROW_H - 2, 5, COL_MENU);
|
||||
else if (h) rb_fill_rrect(rb, L.list_x + 4, ry + 1, L.list_w - 18, FD_ROW_H - 2, 5, COL_HOVER);
|
||||
Col tc = sel ? COL_WHITE : COL_TEXT;
|
||||
Col dc = sel ? COLA(255, 255, 255, 210) : COL_TEXT_DIM;
|
||||
if (e->is_dir) fd_icon_folder(rb, L.list_x + 14, ry + 4, e->is_dir == 2);
|
||||
else fd_icon_file(rb, L.list_x + 14, ry + 4, fd_type_col(e->name));
|
||||
fd_fit(buf, sizeof(buf), e->is_dir == 2 ? ".. (pasta acima)" : e->name, FS_UI,
|
||||
col_size - (L.list_x + 44) - 16);
|
||||
rb_text_c(rb, L.list_x + 44, ry + FD_ROW_H * 0.5f, buf, tc, FS_UI);
|
||||
if (e->is_dir != 2) {
|
||||
char s[48];
|
||||
if (e->is_dir) scopy(s, sizeof(s), "pasta");
|
||||
else fd_size_str(s, sizeof(s), e->size);
|
||||
rb_text_c(rb, col_size, ry + FD_ROW_H * 0.5f, s, dc, FS_SM);
|
||||
fd_time_str(s, sizeof(s), e->mtime);
|
||||
rb_text_c(rb, col_time, ry + FD_ROW_H * 0.5f, s, dc, FS_SM);
|
||||
}
|
||||
}
|
||||
bool only_parent = F->n == 0 || (F->n == 1 && F->ents[0].is_dir == 2);
|
||||
if (only_parent && !F->err[0]) {
|
||||
const char* msg = F->filter[0] && !F->all_files ? "Nenhum arquivo compatível nesta pasta"
|
||||
: "Pasta vazia";
|
||||
rb_text_c(rb, L.list_x + (L.list_w - tw(msg, FS_UI)) * 0.5f,
|
||||
L.list_y + L.list_h * 0.5f, msg, COL_TEXT_DIM, FS_UI);
|
||||
}
|
||||
/* Barra de rolagem */
|
||||
if (F->n > L.rows) {
|
||||
float tx = L.list_x + L.list_w - 10, th = L.list_h - 8;
|
||||
float kh = th * (float)L.rows / (float)F->n;
|
||||
if (kh < 24) kh = 24;
|
||||
float ky = L.list_y + 4 + (th - kh) * (float)F->scroll / (float)(F->n - L.rows);
|
||||
rb_fill_rrect(rb, tx, L.list_y + 4, 6, th, 3, COL_UI_BG);
|
||||
rb_fill_rrect(rb, tx, ky, 6, kh, 3, COL_TEXT_DIM);
|
||||
}
|
||||
if (F->err[0]) {
|
||||
float ey = L.list_y + L.list_h - 30;
|
||||
rb_fill_rrect(rb, L.list_x + 8, ey, L.list_w - 24, 24, 6, COLA(220, 60, 60, 30));
|
||||
fd_fit(buf, sizeof(buf), F->err, FS_UI, L.list_w - 48);
|
||||
rb_text_c(rb, L.list_x + 18, ey + 12, buf, COL_FD_ERR, FS_UI);
|
||||
}
|
||||
|
||||
/* Rodape: nome + opcoes + botoes */
|
||||
float foot = L.y + L.h - FD_FOOT_H;
|
||||
rb_line(rb, L.x, foot, L.x + L.w, foot, 1, COL_BORDER);
|
||||
rb_text_c(rb, L.x + FD_PAD, L.name_y + 15, "Nome:", COL_TEXT, FS_UI);
|
||||
rb_fill_rrect(rb, L.name_x, L.name_y, L.name_w, 30, 6, COL_MENU);
|
||||
rb_fill_rrect(rb, L.name_x + 1.5f, L.name_y + 1.5f, L.name_w - 3, 27, 5, COL_WHITE);
|
||||
{
|
||||
float maxw = L.name_w - 24;
|
||||
const char* s = F->name;
|
||||
while (*s && tw(s, FS_UI) > maxw) { /* mostra o fim do texto */
|
||||
s++;
|
||||
while ((*s & 0xC0) == 0x80) s++;
|
||||
}
|
||||
if (F->name[0]) rb_text_c(rb, L.name_x + 10, L.name_y + 15, s, COL_TEXT, FS_UI);
|
||||
else rb_text_c(rb, L.name_x + 10, L.name_y + 15,
|
||||
F->save ? "nome do arquivo" : "escolha um arquivo ou digite o caminho",
|
||||
COL_TEXT_DIM, FS_UI);
|
||||
float cw = F->name[0] ? tw(s, FS_UI) : 0;
|
||||
if ((fd_now_ms() / 530u) % 2u == 0u)
|
||||
rb_fill_rect(rb, L.name_x + 11 + cw, L.name_y + 7, 1.5f, 16, COL_TEXT);
|
||||
}
|
||||
float r2 = L.row2_y + FD_BTN_H * 0.5f;
|
||||
if (F->filter[0]) {
|
||||
snprintf(buf, sizeof(buf), "Todos os arquivos");
|
||||
fd_checkbox(rb, L.chk_all_x, r2, F->all_files, buf);
|
||||
if (!F->all_files) {
|
||||
char f2[120];
|
||||
snprintf(f2, sizeof(f2), "(%s)", F->filter);
|
||||
fd_fit(buf, sizeof(buf), f2, FS_SM, L.chk_hid_x - L.chk_all_x - 160);
|
||||
rb_text_c(rb, L.chk_all_x + 24 + tw("Todos os arquivos", FS_UI) + 8, r2, buf, COL_TEXT_DIM, FS_SM);
|
||||
}
|
||||
}
|
||||
fd_checkbox(rb, L.chk_hid_x, r2, F->show_hidden, "Mostrar ocultos");
|
||||
fd_button(rb, L.cancel_x, L.row2_y, FD_BTN_W, FD_BTN_H, "Cancelar", false,
|
||||
modal_free && in_rect(mx, my, L.cancel_x, L.row2_y, FD_BTN_W, FD_BTN_H), COL_MENU);
|
||||
fd_button(rb, L.ok_x, L.row2_y, FD_BTN_W, FD_BTN_H, F->ok_label, true,
|
||||
modal_free && in_rect(mx, my, L.ok_x, L.row2_y, FD_BTN_W, FD_BTN_H), COL_MENU);
|
||||
|
||||
/* Confirmacao de substituicao */
|
||||
if (F->confirm) {
|
||||
rb_fill_rrect(rb, L.x, L.y, L.w, L.h, 12, COLA(0, 0, 0, 60));
|
||||
float bx, by, bw, bh;
|
||||
fd_confirm_geom(&L, &bx, &by, &bw, &bh);
|
||||
rb_fill_rrect(rb, bx + 2, by + 3, bw, bh, 10, COLA(0, 0, 0, 60));
|
||||
rb_fill_rrect(rb, bx, by, bw, bh, 10, COL_PANEL);
|
||||
const char* b = strrchr(F->name, '/');
|
||||
b = b ? b + 1 : F->name;
|
||||
char q[400];
|
||||
snprintf(q, sizeof(q), "\"%s\" já existe.", b);
|
||||
fd_fit(buf, sizeof(buf), q, 15, bw - 40);
|
||||
rb_text_c(rb, bx + 20, by + 34, buf, COL_TEXT, 15);
|
||||
rb_text_c(rb, bx + 20, by + 60, "Deseja substituir o arquivo?", COL_TEXT_DIM, FS_UI);
|
||||
float yb = by + bh - FD_BTN_H - 16;
|
||||
float xo = bx + bw - 20 - FD_BTN_W, xc = xo - 10 - FD_BTN_W;
|
||||
fd_button(rb, xc, yb, FD_BTN_W, FD_BTN_H, "Cancelar", false,
|
||||
in_rect(mx, my, xc, yb, FD_BTN_W, FD_BTN_H), COL_MENU);
|
||||
fd_button(rb, xo, yb, FD_BTN_W, FD_BTN_H, "Substituir", true,
|
||||
in_rect(mx, my, xo, yb, FD_BTN_W, FD_BTN_H), COL_STOP);
|
||||
}
|
||||
}
|
||||
|
||||
/* -- Entrada -------------------------------------------------------------- */
|
||||
|
||||
static void fd_rescan_keep(Ed* ed) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
char nm[256];
|
||||
scopy(nm, sizeof(nm), F->name);
|
||||
fd_scan(ed);
|
||||
scopy(F->name, sizeof(F->name), nm);
|
||||
fd_ensure_visible(ed);
|
||||
}
|
||||
|
||||
bool tb_editor_files_click(Ed* ed, float mx, float my) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
if (!F->open) return false;
|
||||
FdL L;
|
||||
fd_layout(ed, &L);
|
||||
|
||||
if (F->confirm) {
|
||||
float bx, by, bw, bh;
|
||||
fd_confirm_geom(&L, &bx, &by, &bw, &bh);
|
||||
float yb = by + bh - FD_BTN_H - 16;
|
||||
float xo = bx + bw - 20 - FD_BTN_W;
|
||||
if (in_rect(mx, my, xo, yb, FD_BTN_W, FD_BTN_H)) fd_accept(ed, true);
|
||||
else if (!in_rect(mx, my, bx, by, bw, bh) || in_rect(mx, my, xo - 10 - FD_BTN_W, yb, FD_BTN_W, FD_BTN_H))
|
||||
F->confirm = false;
|
||||
return true;
|
||||
}
|
||||
if (!in_rect(mx, my, L.x, L.y, L.w, L.h)) return true; /* modal */
|
||||
F->err[0] = 0;
|
||||
|
||||
if (hypotf(mx - L.close_cx, my - L.close_cy) < 14.f) { tb_editor_files_close(ed); return true; }
|
||||
|
||||
/* barra: pasta acima / trilha */
|
||||
if (in_rect(mx, my, L.up_x, L.bar_y, 32, 30)) { fd_parent(ed); return true; }
|
||||
if (in_rect(mx, my, L.crumb_x, L.bar_y, L.crumb_w, 30)) {
|
||||
FdCrumbs K;
|
||||
fd_crumbs(ed, &L, &K);
|
||||
for (int k = K.first; k < K.n - 1; k++) {
|
||||
if (in_rect(mx, my, K.x[k], L.bar_y, K.w[k], 30)) {
|
||||
char p[1024];
|
||||
snprintf(p, sizeof(p), "%.*s", K.end[k], F->dir);
|
||||
if (!p[0]) scopy(p, sizeof(p), "/");
|
||||
fd_chdir(ed, p);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* locais */
|
||||
if (in_rect(mx, my, L.side_x, L.side_y, FD_SIDE_W, L.side_h)) {
|
||||
for (int i = 0; i < F->place_n; i++) {
|
||||
float iy = L.side_y + 30 + i * 32;
|
||||
if (in_rect(mx, my, L.side_x + 6, iy, FD_SIDE_W - 12, 30)) {
|
||||
fd_chdir(ed, F->place_path[i]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* lista */
|
||||
if (in_rect(mx, my, L.list_x, L.list_y, L.list_w - 12, L.list_h)) {
|
||||
int i = F->scroll + (int)((my - L.list_y) / FD_ROW_H);
|
||||
if (i < 0 || i >= F->n || i >= F->scroll + L.rows) return true;
|
||||
unsigned t = fd_now_ms();
|
||||
bool dbl = i == F->last_click_i && (t - F->last_click_ms) <= 400u;
|
||||
F->last_click_i = i;
|
||||
F->last_click_ms = t;
|
||||
F->sel = i;
|
||||
FileEnt* e = &F->ents[i];
|
||||
if (e->is_dir) {
|
||||
if (dbl) fd_enter(ed, i);
|
||||
else if (!F->save) F->name[0] = 0;
|
||||
return true;
|
||||
}
|
||||
scopy(F->name, sizeof(F->name), e->name);
|
||||
if (dbl) fd_accept(ed, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* rodape */
|
||||
float r2 = L.row2_y;
|
||||
if (F->filter[0] && in_rect(mx, my, L.chk_all_x, r2, 24 + tw("Todos os arquivos", FS_UI), FD_BTN_H)) {
|
||||
F->all_files = !F->all_files;
|
||||
fd_rescan_keep(ed);
|
||||
return true;
|
||||
}
|
||||
if (in_rect(mx, my, L.chk_hid_x, r2, 24 + tw("Mostrar ocultos", FS_UI), FD_BTN_H)) {
|
||||
F->show_hidden = !F->show_hidden;
|
||||
fd_rescan_keep(ed);
|
||||
return true;
|
||||
}
|
||||
if (in_rect(mx, my, L.cancel_x, r2, FD_BTN_W, FD_BTN_H)) { tb_editor_files_close(ed); return true; }
|
||||
if (in_rect(mx, my, L.ok_x, r2, FD_BTN_W, FD_BTN_H)) { fd_accept(ed, false); return true; }
|
||||
return true;
|
||||
}
|
||||
|
||||
static void fd_pop(char* s) {
|
||||
size_t n = strlen(s);
|
||||
if (!n) return;
|
||||
n--;
|
||||
while (n > 0 && ((unsigned char)s[n] & 0xC0) == 0x80) n--;
|
||||
s[n] = 0;
|
||||
}
|
||||
|
||||
void tb_editor_files_key(Ed* ed, int sc, bool ctrl) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
if (!F->open) return;
|
||||
if (F->confirm) {
|
||||
if (sc == TB_KEY_ESCAPE) F->confirm = false;
|
||||
else if (sc == TB_KEY_RETURN) fd_accept(ed, true);
|
||||
return;
|
||||
}
|
||||
if (ctrl && sc == TB_KEY_A + ('h' - 'a')) {
|
||||
F->show_hidden = !F->show_hidden;
|
||||
fd_rescan_keep(ed);
|
||||
return;
|
||||
}
|
||||
switch (sc) {
|
||||
case TB_KEY_ESCAPE: tb_editor_files_close(ed); return;
|
||||
case TB_KEY_RETURN: fd_accept(ed, false); return;
|
||||
case TB_KEY_BACKSPACE: fd_pop(F->name); F->err[0] = 0; return;
|
||||
case TB_KEY_UP:
|
||||
case TB_KEY_DOWN:
|
||||
case TB_KEY_HOME:
|
||||
case TB_KEY_END: {
|
||||
if (F->n == 0) return;
|
||||
int s = F->sel;
|
||||
if (sc == TB_KEY_UP) s = s < 0 ? F->n - 1 : s - 1;
|
||||
else if (sc == TB_KEY_DOWN) s = s < 0 ? 0 : s + 1;
|
||||
else if (sc == TB_KEY_HOME) s = 0;
|
||||
else s = F->n - 1;
|
||||
if (s < 0) s = 0;
|
||||
if (s >= F->n) s = F->n - 1;
|
||||
F->sel = s;
|
||||
F->last_click_i = -1;
|
||||
if (!F->ents[s].is_dir) scopy(F->name, sizeof(F->name), F->ents[s].name);
|
||||
else if (!F->save) F->name[0] = 0;
|
||||
fd_ensure_visible(ed);
|
||||
return;
|
||||
}
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
|
||||
void tb_editor_files_text(Ed* ed, const char* typed) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
if (!F->open || F->confirm) return;
|
||||
size_t n = strlen(F->name), a = strlen(typed);
|
||||
if (n + a < sizeof(F->name)) memcpy(F->name + n, typed, a + 1);
|
||||
F->err[0] = 0;
|
||||
}
|
||||
|
||||
void tb_editor_files_scroll(Ed* ed, float dy) {
|
||||
FileDlg* F = &ed->fdlg;
|
||||
if (!F->open) return;
|
||||
FdL L;
|
||||
fd_layout(ed, &L);
|
||||
F->scroll -= (int)(dy * 3.f);
|
||||
int mx = F->n - L.rows;
|
||||
if (F->scroll > mx) F->scroll = mx;
|
||||
if (F->scroll < 0) F->scroll = 0;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ CFLAGS := -std=c11 -Wall -Wextra -pedantic \
|
||||
-I../../../../include -Iinclude \
|
||||
-I../../internal \
|
||||
-I../tinyblocks-editor-widgets/include \
|
||||
-I../tinyblocks-editor-files/include \
|
||||
-I../../../tinyblocks-graphics/include \
|
||||
-I../../../tinyblocks-variables/include \
|
||||
-I../../../tinyblocks-blueprint/include \
|
||||
|
||||
Reference in new issue
Block a user