ngui_file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /* ngui_file.c: modal file Open/Save dialog for ngui canvas windows.
  2. *
  3. * The dialog owns a directory listing (the VFS root shows the mount table),
  4. * Up/Home/Refresh controls, a scrollable row list, a file-name field and
  5. * Cancel/Open|Save. It is not a separate window: the app draws it on top of
  6. * its own canvas and calls ngui_file_dialog() until it returns ACCEPTED or
  7. * CANCELLED. See ngui.h for the caller contract. */
  8. #include "ngui.h"
  9. #include "ucmd_lib.h"
  10. #define FD_ROW_H 22
  11. #define FD_PAD 16
  12. #define FD_ID_NAME 1
  13. #define FD_ID_UP 2
  14. #define FD_ID_HOME 3
  15. #define FD_ID_REFRESH 4
  16. #define FD_ID_CANCEL 5
  17. #define FD_ID_ACCEPT 6
  18. #define FD_ID_ROW 0x100
  19. static unsigned int fl_len(const char *s)
  20. {
  21. unsigned int n = 0;
  22. while (s && s[n])
  23. n++;
  24. return n;
  25. }
  26. /* Copy s into d (always NUL terminated). Returns 0 when it had to truncate. */
  27. static int fl_copy(char *d, const char *s, unsigned int cap)
  28. {
  29. unsigned int i = 0;
  30. if (!cap)
  31. return 0;
  32. while (s && s[i]) {
  33. if (i + 1 >= cap) {
  34. d[i] = '\0';
  35. return 0;
  36. }
  37. d[i] = s[i];
  38. i++;
  39. }
  40. d[i] = '\0';
  41. return 1;
  42. }
  43. /* Join dir and name into an absolute path. Returns 0 if it does not fit. */
  44. static int fl_join(const char *dir, const char *name, char *out,
  45. unsigned int cap)
  46. {
  47. unsigned int i = 0, j;
  48. if (!dir || !dir[0])
  49. dir = "/";
  50. if (name && name[0] == '/')
  51. return fl_copy(out, name, cap);
  52. while (dir[i]) {
  53. if (i + 1 >= cap) {
  54. out[0] = '\0';
  55. return 0;
  56. }
  57. out[i] = dir[i];
  58. i++;
  59. }
  60. if (i && out[i - 1] != '/') {
  61. if (i + 1 >= cap) {
  62. out[0] = '\0';
  63. return 0;
  64. }
  65. out[i++] = '/';
  66. }
  67. for (j = 0; name && name[j]; j++) {
  68. if (i + 1 >= cap) {
  69. out[0] = '\0';
  70. return 0;
  71. }
  72. out[i++] = name[j];
  73. }
  74. out[i] = '\0';
  75. return 1;
  76. }
  77. static void fl_parent(const char *in, char *out, unsigned int cap)
  78. {
  79. unsigned int n, i, last = 0;
  80. fl_copy(out, in && in[0] ? in : "/", cap);
  81. n = fl_len(out);
  82. if (n > 1 && out[n - 1] == '/')
  83. out[--n] = '\0';
  84. for (i = 0; i < n; i++)
  85. if (out[i] == '/')
  86. last = i;
  87. if (last == 0) {
  88. out[0] = '/';
  89. out[1] = '\0';
  90. } else {
  91. out[last] = '\0';
  92. }
  93. if (!out[0])
  94. fl_copy(out, "/", cap);
  95. }
  96. static const char *fl_base(const char *path)
  97. {
  98. const char *b = path;
  99. while (path && *path) {
  100. if (*path == '/')
  101. b = path + 1;
  102. path++;
  103. }
  104. return b;
  105. }
  106. static void fl_abs(const char *p, char *out, unsigned int cap)
  107. {
  108. char cwd[NGUI_FILE_PATH_MAX];
  109. if (!p || !p[0]) {
  110. fl_copy(out, "/", cap);
  111. return;
  112. }
  113. if (p[0] == '/') {
  114. fl_copy(out, p, cap);
  115. return;
  116. }
  117. if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0])
  118. fl_copy(cwd, "/", sizeof(cwd));
  119. if (!fl_join(cwd, p, out, cap))
  120. fl_copy(out, "/", cap);
  121. }
  122. static int fl_ci_cmp(const char *a, const char *b)
  123. {
  124. for (;;) {
  125. unsigned char ca = (unsigned char)*a++;
  126. unsigned char cb = (unsigned char)*b++;
  127. if (ca >= 'a' && ca <= 'z')
  128. ca = (unsigned char)(ca - 32);
  129. if (cb >= 'a' && cb <= 'z')
  130. cb = (unsigned char)(cb - 32);
  131. if (ca != cb)
  132. return (int)ca - (int)cb;
  133. if (!ca)
  134. return 0;
  135. }
  136. }
  137. static void fl_size_text(unsigned int bytes, char *out, unsigned int cap)
  138. {
  139. char tmp[12];
  140. unsigned int n = 0, i = 0, j;
  141. if (bytes < 1024) {
  142. do {
  143. tmp[i++] = (char)('0' + bytes % 10);
  144. bytes /= 10;
  145. } while (bytes && i < sizeof(tmp));
  146. for (j = 0; j < i && n + 1 < cap; j++)
  147. out[n++] = tmp[i - 1 - j];
  148. if (n + 1 < cap)
  149. out[n++] = ' ';
  150. if (n + 1 < cap)
  151. out[n++] = 'B';
  152. out[n] = '\0';
  153. return;
  154. }
  155. if (bytes < 1024u * 1024u) {
  156. unsigned int kb = bytes / 1024;
  157. i = 0;
  158. do {
  159. tmp[i++] = (char)('0' + kb % 10);
  160. kb /= 10;
  161. } while (kb && i < sizeof(tmp));
  162. for (j = 0; j < i && n + 1 < cap; j++)
  163. out[n++] = tmp[i - 1 - j];
  164. if (n + 1 < cap)
  165. out[n++] = ' ';
  166. if (n + 1 < cap)
  167. out[n++] = 'K';
  168. if (n + 1 < cap)
  169. out[n++] = 'B';
  170. } else {
  171. unsigned int mb = bytes / (1024u * 1024u);
  172. i = 0;
  173. do {
  174. tmp[i++] = (char)('0' + mb % 10);
  175. mb /= 10;
  176. } while (mb && i < sizeof(tmp));
  177. for (j = 0; j < i && n + 1 < cap; j++)
  178. out[n++] = tmp[i - 1 - j];
  179. if (n + 1 < cap)
  180. out[n++] = ' ';
  181. if (n + 1 < cap)
  182. out[n++] = 'M';
  183. if (n + 1 < cap)
  184. out[n++] = 'B';
  185. }
  186. out[n] = '\0';
  187. }
  188. static const char *fd_error_text(int r)
  189. {
  190. switch (r) {
  191. case SYS_ENODEV:
  192. return "No such device";
  193. case SYS_ENOFS:
  194. return "No filesystem on the device";
  195. case SYS_ENOENT:
  196. return "Folder not found";
  197. case SYS_EINVAL:
  198. return "Not a folder";
  199. case SYS_EIO:
  200. return "Disk read error";
  201. default:
  202. return "Cannot open the folder";
  203. }
  204. }
  205. static int fd_entry_before(const struct ngui_file_entry *a,
  206. const struct ngui_file_entry *b)
  207. {
  208. if (a->is_dir != b->is_dir)
  209. return a->is_dir > b->is_dir;
  210. return fl_ci_cmp(a->name, b->name) < 0;
  211. }
  212. static void fd_sort_entries(struct ngui_file_entry *list, unsigned int n)
  213. {
  214. unsigned int i;
  215. for (i = 1; i < n; i++) {
  216. struct ngui_file_entry key = list[i];
  217. unsigned int j = i;
  218. while (j > 0 && fd_entry_before(&key, &list[j - 1])) {
  219. list[j] = list[j - 1];
  220. j--;
  221. }
  222. list[j] = key;
  223. }
  224. }
  225. /* Load a directory into the dialog. The previous path and listing are kept
  226. * when listing fails, so the error can be shown without leaving the user in
  227. * a dead folder. */
  228. static int fd_load_dir(struct ngui_file_dialog *fd, const char *path)
  229. {
  230. struct ngui_file_entry tmp[NGUI_FILE_MAX_ENTRIES];
  231. char dir[NGUI_FILE_PATH_MAX];
  232. unsigned int n = 0, i, dl;
  233. int truncated = 0;
  234. if (!fd)
  235. return -1;
  236. if (!path || !path[0])
  237. path = "/";
  238. if (!fl_copy(dir, path, sizeof(dir))) {
  239. fl_copy(fd->error, "Path is too long", sizeof(fd->error));
  240. return -1;
  241. }
  242. dl = fl_len(dir);
  243. if (dl > 1 && dir[dl - 1] == '/')
  244. dir[--dl] = '\0';
  245. if (dir[0] == '/' && dir[1] == '\0') {
  246. struct sys_mount m;
  247. for (i = 0; ; i++) {
  248. if (u_syscall2(SYS_LIST_MOUNTS, (int)i,
  249. (int)&m) != 1)
  250. break;
  251. if (!m.name[0])
  252. continue;
  253. if (n >= NGUI_FILE_MAX_ENTRIES) {
  254. truncated = 1;
  255. break;
  256. }
  257. fl_copy(tmp[n].name, m.name, sizeof(tmp[n].name));
  258. tmp[n].size = 0;
  259. tmp[n].is_dir = 1;
  260. tmp[n].is_mount = 1;
  261. n++;
  262. }
  263. } else {
  264. struct sys_dirent d;
  265. for (i = 0; ; i++) {
  266. int r = u_syscall3(SYS_LIST_DIR, (int)dir, (int)i,
  267. (int)&d);
  268. if (r != 1) {
  269. if (r < 0) {
  270. fl_copy(fd->error, fd_error_text(r),
  271. sizeof(fd->error));
  272. return -1;
  273. }
  274. break;
  275. }
  276. if (n >= NGUI_FILE_MAX_ENTRIES) {
  277. truncated = 1;
  278. break;
  279. }
  280. {
  281. char disp[NGUI_FILE_NAME_MAX];
  282. if (d.raw_83)
  283. u_fat_name(d.name, disp, sizeof(disp));
  284. else
  285. fl_copy(disp, d.name, sizeof(disp));
  286. if (!disp[0] || disp[0] == '.')
  287. continue;
  288. fl_copy(tmp[n].name, disp, sizeof(tmp[n].name));
  289. tmp[n].size = d.size;
  290. tmp[n].is_dir = d.is_dir ? 1 : 0;
  291. tmp[n].is_mount = 0;
  292. n++;
  293. }
  294. }
  295. }
  296. fd_sort_entries(tmp, n);
  297. for (i = 0; i < n; i++)
  298. fd->entries[i] = tmp[i];
  299. fd->count = n;
  300. fd->truncated = truncated;
  301. fd->selected = -1;
  302. fd->last_click = -1;
  303. fd->last_click_ms = 0;
  304. fl_copy(fd->path, dir, sizeof(fd->path));
  305. fd->scroll.offset = 0;
  306. fd->scroll.dragging = 0;
  307. fd->scroll.bar_vis = 0;
  308. fd->scroll.bar_target = 0;
  309. fd->scroll.bar_hot = 0;
  310. fd->error[0] = '\0';
  311. return 0;
  312. }
  313. static void fd_set_name(struct ngui_file_dialog *fd, const char *name)
  314. {
  315. ngui_input_set(&fd->name, name ? name : "");
  316. if (fd->name.len) {
  317. fd->name.anchor = 0;
  318. fd->name.cursor = fd->name.len;
  319. }
  320. }
  321. static void fd_default_dir(int mode, char *out, unsigned int cap)
  322. {
  323. struct sys_stat st;
  324. if (mode == NGUI_FILE_SAVE &&
  325. u_stat("/share", &st) == 0 && st.is_dir) {
  326. fl_copy(out, "/share", cap);
  327. return;
  328. }
  329. if (mode == NGUI_FILE_OPEN &&
  330. u_getcwd(out, cap) == 0 && out[0] &&
  331. u_stat(out, &st) == 0 && st.is_dir)
  332. return;
  333. fl_copy(out, "/", cap);
  334. }
  335. static void fd_focus_name(struct ngui *ui, struct ngui_file_dialog *fd)
  336. {
  337. unsigned int id = fd->id_base + FD_ID_NAME;
  338. ngui_set_focus(ui, id);
  339. ui->focus_claimed = 1;
  340. ui->last_blink = u_ticks_ms();
  341. ui->caret_on = 1;
  342. }
  343. /* Resolve the name field (or the highlighted entry) to an absolute path and
  344. * either navigate to a directory or accept it. Returns 1 on accept. */
  345. static int fd_accept(struct ngui_file_dialog *fd)
  346. {
  347. char path[NGUI_FILE_PATH_MAX];
  348. const char *name = fd->name.text;
  349. struct sys_stat st;
  350. int r;
  351. if (!name[0]) {
  352. const struct ngui_file_entry *e;
  353. if (fd->selected < 0 ||
  354. fd->selected >= (int)fd->count) {
  355. fl_copy(fd->error, "Enter a file name",
  356. sizeof(fd->error));
  357. return 0;
  358. }
  359. e = &fd->entries[fd->selected];
  360. if (!fl_join(fd->path, e->name, path, sizeof(path))) {
  361. fl_copy(fd->error, "Path is too long",
  362. sizeof(fd->error));
  363. return 0;
  364. }
  365. if (e->is_dir) {
  366. if (fd_load_dir(fd, path) == 0)
  367. fd_set_name(fd, "");
  368. return 0;
  369. }
  370. name = e->name;
  371. }
  372. if (!fl_join(fd->path, name, path, sizeof(path))) {
  373. fl_copy(fd->error, "Path is too long", sizeof(fd->error));
  374. return 0;
  375. }
  376. r = u_stat(path, &st);
  377. if (r == 0 && st.is_dir) {
  378. if (fd_load_dir(fd, path) == 0)
  379. fd_set_name(fd, "");
  380. return 0;
  381. }
  382. if (fd->mode == NGUI_FILE_OPEN) {
  383. if (r != 0) {
  384. fl_copy(fd->error, "File not found",
  385. sizeof(fd->error));
  386. return 0;
  387. }
  388. } else if (r != 0 && r != SYS_ENOENT) {
  389. fl_copy(fd->error, fd_error_text(r), sizeof(fd->error));
  390. return 0;
  391. } else if (r != 0) {
  392. /* Saving a new file: make sure the parent exists. */
  393. char parent[NGUI_FILE_PATH_MAX];
  394. fl_parent(path, parent, sizeof(parent));
  395. if (u_stat(parent, &st) != 0 || !st.is_dir) {
  396. fl_copy(fd->error, "Folder not found",
  397. sizeof(fd->error));
  398. return 0;
  399. }
  400. }
  401. if (fl_len(path) >= sizeof(fd->result)) {
  402. fl_copy(fd->error, "Path is too long", sizeof(fd->error));
  403. return 0;
  404. }
  405. fl_copy(fd->result, path, sizeof(fd->result));
  406. return 1;
  407. }
  408. static void fd_move_selection(struct ngui *ui, struct ngui_file_dialog *fd,
  409. int delta)
  410. {
  411. int i;
  412. if (!fd->count)
  413. return;
  414. if (fd->selected < 0)
  415. fd->selected = delta > 0 ? 0 : (int)fd->count - 1;
  416. else {
  417. fd->selected += delta;
  418. if (fd->selected < 0)
  419. fd->selected = 0;
  420. if (fd->selected >= (int)fd->count)
  421. fd->selected = (int)fd->count - 1;
  422. }
  423. i = fd->selected;
  424. if (fd->entries[i].is_dir)
  425. fd_set_name(fd, "");
  426. else
  427. fd_set_name(fd, fd->entries[i].name);
  428. fd->error[0] = '\0';
  429. fd_focus_name(ui, fd);
  430. ngui_scroll_ensure_visible(&fd->scroll,
  431. 2 + i * FD_ROW_H, FD_ROW_H);
  432. ngui_invalidate(ui);
  433. }
  434. void ngui_file_dialog_open(struct ngui_file_dialog *fd, int mode,
  435. const char *start_path, const char *suggested_name,
  436. unsigned int id_base)
  437. {
  438. char abs[NGUI_FILE_PATH_MAX], dir[NGUI_FILE_PATH_MAX];
  439. char nm[NGUI_FILE_NAME_MAX];
  440. struct sys_stat st;
  441. unsigned int i;
  442. if (!fd)
  443. return;
  444. for (i = 0; i < sizeof(*fd); i++)
  445. ((unsigned char *)fd)[i] = 0;
  446. fd->active = 1;
  447. fd->mode = mode == NGUI_FILE_SAVE ? NGUI_FILE_SAVE : NGUI_FILE_OPEN;
  448. fd->id_base = id_base ? id_base : 0x1000u;
  449. fd->last_click = -1;
  450. fd->need_focus = 1;
  451. fd->name.placeholder = mode == NGUI_FILE_OPEN ?
  452. "File name or path" : "File name";
  453. if (start_path && start_path[0])
  454. fl_abs(start_path, abs, sizeof(abs));
  455. else
  456. abs[0] = '\0';
  457. if (!abs[0]) {
  458. fd_default_dir(fd->mode, dir, sizeof(dir));
  459. fl_copy(nm, suggested_name ? suggested_name : "", sizeof(nm));
  460. } else if (u_stat(abs, &st) == 0 && st.is_dir) {
  461. fl_copy(dir, abs, sizeof(dir));
  462. fl_copy(nm, suggested_name ? suggested_name : "", sizeof(nm));
  463. } else {
  464. fl_parent(abs, dir, sizeof(dir));
  465. fl_copy(nm, suggested_name && suggested_name[0] ?
  466. suggested_name : fl_base(abs), sizeof(nm));
  467. }
  468. if (!dir[0] || fd_load_dir(fd, dir) != 0) {
  469. if (!dir[0] || fl_ci_cmp(dir, "/") != 0)
  470. fd_load_dir(fd, "/");
  471. }
  472. fd_set_name(fd, nm);
  473. for (i = 0; i < fd->count; i++) {
  474. if (fl_ci_cmp(fd->entries[i].name, fd->name.text) == 0) {
  475. fd->selected = (int)i;
  476. break;
  477. }
  478. }
  479. }
  480. int ngui_file_dialog(struct ngui *ui, struct ngui_file_dialog *fd)
  481. {
  482. struct ngui_rect panel, list, r, name_r;
  483. char size_text[24];
  484. int pw, ph, px, py, x, y, w, list_y, list_h, name_y, err_y, btn_y;
  485. int event = NGUI_FILE_NONE;
  486. int clicked_dir = -1, accept_after = 0;
  487. unsigned int id_name, i;
  488. int k;
  489. if (!ui || !fd || !fd->active)
  490. return NGUI_FILE_NONE;
  491. if (ui->focus == 0)
  492. fd_focus_name(ui, fd);
  493. id_name = fd->id_base + FD_ID_NAME;
  494. /* The name field must keep the focus for typing, but ngui_text_input
  495. * consumes every key it sees. Take the list-navigation keys first so
  496. * they still move the highlight. */
  497. for (;;) {
  498. int page = fd->scroll.view_h / FD_ROW_H - 1;
  499. if (page < 1)
  500. page = 1;
  501. k = ngui_key_peek(ui);
  502. if (k == NGUI_KEY_UP) {
  503. ngui_key(ui);
  504. fd_move_selection(ui, fd, -1);
  505. } else if (k == NGUI_KEY_DOWN) {
  506. ngui_key(ui);
  507. fd_move_selection(ui, fd, 1);
  508. } else if (k == NGUI_KEY_PAGE_UP) {
  509. ngui_key(ui);
  510. fd_move_selection(ui, fd, -page);
  511. } else if (k == NGUI_KEY_PAGE_DOWN) {
  512. ngui_key(ui);
  513. fd_move_selection(ui, fd, page);
  514. } else {
  515. break;
  516. }
  517. }
  518. pw = ui->w - 40;
  519. if (pw > 720)
  520. pw = 720;
  521. if (pw < 320)
  522. pw = ui->w - 20;
  523. if (pw < 160)
  524. pw = 160;
  525. ph = ui->h - 40;
  526. if (ph > 480)
  527. ph = 480;
  528. if (ph < 260)
  529. ph = ui->h - 20;
  530. if (ph < 160)
  531. ph = 160;
  532. px = (ui->w - pw) / 2;
  533. py = (ui->h - ph) / 2;
  534. if (px < 0)
  535. px = 0;
  536. if (py < 0)
  537. py = 0;
  538. panel = (struct ngui_rect){ px, py, pw, ph };
  539. ngui_set_cursor(ui, CURSOR_DEFAULT);
  540. ngui_fill_rect(ui, (struct ngui_rect){ 0, 0, ui->w, ui->h },
  541. NGUI_RGB(8, 10, 16));
  542. ngui_fill_round_rect(ui, panel, NGUI_RADIUS, NGUI_RADIUS,
  543. NGUI_RADIUS, NGUI_RADIUS, ui->theme.panel);
  544. ngui_frame_round(ui, panel, NGUI_RADIUS, NGUI_RADIUS,
  545. NGUI_RADIUS, NGUI_RADIUS, 1, ui->theme.border);
  546. ngui_text(ui, px + FD_PAD, py + 14,
  547. fd->mode == NGUI_FILE_OPEN ? "Open file" : "Save as",
  548. ui->theme.text);
  549. x = px + FD_PAD;
  550. y = py + 40;
  551. r = (struct ngui_rect){ x, y, 28, 26 };
  552. if (ngui_icon_button(ui, fd->id_base + FD_ID_UP, r, NGUI_ICON_UP)) {
  553. char parent[NGUI_FILE_PATH_MAX];
  554. fl_parent(fd->path, parent, sizeof(parent));
  555. if (fd_load_dir(fd, parent) == 0)
  556. fd_set_name(fd, "");
  557. fd_focus_name(ui, fd);
  558. }
  559. x += 32;
  560. r.x = x;
  561. if (ngui_icon_button(ui, fd->id_base + FD_ID_HOME, r,
  562. NGUI_ICON_HOME)) {
  563. if (fd_load_dir(fd, "/") == 0)
  564. fd_set_name(fd, "");
  565. fd_focus_name(ui, fd);
  566. }
  567. x += 32;
  568. r.x = x;
  569. if (ngui_icon_button(ui, fd->id_base + FD_ID_REFRESH, r,
  570. NGUI_ICON_REFRESH)) {
  571. fd_load_dir(fd, fd->path);
  572. fd_focus_name(ui, fd);
  573. }
  574. x += 38;
  575. w = px + pw - FD_PAD - x;
  576. if (w < 10)
  577. w = 10;
  578. ngui_text_ellipsis(ui, (struct ngui_rect){ x, y, w, 26 },
  579. x, y + 5, fd->path, ui->theme.text_dim);
  580. list_y = py + 96;
  581. btn_y = py + ph - 44;
  582. err_y = btn_y - 22;
  583. name_y = err_y - 36;
  584. list_h = name_y - 10 - list_y;
  585. if (list_h < 40)
  586. list_h = 40;
  587. list = (struct ngui_rect){ px + FD_PAD, list_y, pw - 2 * FD_PAD,
  588. list_h };
  589. ngui_text(ui, list.x + 6, list_y - 18, "Name", ui->theme.text_dim);
  590. {
  591. int sw = (int)ngui_text_width_ui(ui, "Size");
  592. ngui_text(ui, list.x + list.w - sw - 10, list_y - 18,
  593. "Size", ui->theme.text_dim);
  594. }
  595. ngui_fill_rect(ui, list, ui->theme.bg);
  596. ngui_frame(ui, list, ui->theme.border);
  597. fd->scroll.view_h = list.h;
  598. fd->scroll.content_h = (int)fd->count * FD_ROW_H + 4;
  599. ngui_scroll_begin(ui, list, &fd->scroll);
  600. if (ngui_scroll_wheel(ui, list, &fd->scroll))
  601. ui->dirty = 1;
  602. if (!fd->count) {
  603. ngui_text(ui, list.x + 10, list.y + 8,
  604. fd->truncated ? "No entries" :
  605. "This folder is empty", ui->theme.text_dim);
  606. }
  607. for (i = 0; i < fd->count; i++) {
  608. struct ngui_rect rr = { list.x + 2,
  609. list.y + 2 + (int)i * FD_ROW_H - fd->scroll.offset,
  610. list.w - 4, FD_ROW_H };
  611. const struct ngui_file_entry *e = &fd->entries[i];
  612. int hot, selected;
  613. int name_w;
  614. if (rr.y + rr.h <= list.y || rr.y >= list.y + list.h)
  615. continue;
  616. hot = ui->inside &&
  617. ngui_rect_contains(&rr, ui->mouse_x, ui->mouse_y);
  618. selected = (int)i == fd->selected;
  619. if (selected)
  620. ngui_fill_rect(ui, rr, ui->theme.selection);
  621. else if (hot)
  622. ngui_fill_rect(ui, rr, ui->theme.panel_alt);
  623. ngui_icon(ui, (struct ngui_rect){ rr.x + 5, rr.y + 3, 16, 16 },
  624. e->is_dir ? NGUI_ICON_FOLDER : NGUI_ICON_FILE,
  625. e->is_dir ? ui->theme.icon_folder :
  626. ui->theme.icon_file);
  627. if (e->is_dir)
  628. fl_copy(size_text, e->is_mount ? "Mount" : "Folder",
  629. sizeof(size_text));
  630. else
  631. fl_size_text(e->size, size_text, sizeof(size_text));
  632. {
  633. int sw = (int)ngui_text_width_ui(ui, size_text);
  634. int tx = rr.x + rr.w - sw - 10;
  635. if (tx < rr.x + 28)
  636. tx = rr.x + 28;
  637. ngui_text(ui, tx, rr.y + 3, size_text,
  638. selected ? ui->theme.text :
  639. ui->theme.text_dim);
  640. name_w = tx - (rr.x + 28) - 6;
  641. }
  642. if (name_w < 10)
  643. name_w = 10;
  644. ngui_text_ellipsis(ui,
  645. (struct ngui_rect){ rr.x + 28, rr.y, name_w, rr.h },
  646. rr.x + 28, rr.y + 3, e->name, ui->theme.text);
  647. if (hot)
  648. ui->hot = fd->id_base + FD_ID_ROW + i;
  649. if (ui->pressed && ngui_rect_contains(&rr, ui->press_x,
  650. ui->press_y)) {
  651. unsigned int now = u_ticks_ms();
  652. int dbl = fd->last_click == (int)i &&
  653. fd->last_click_ms &&
  654. (unsigned int)(now - fd->last_click_ms) <= 500;
  655. fd->selected = (int)i;
  656. fd->last_click = (int)i;
  657. fd->last_click_ms = now;
  658. fd->error[0] = '\0';
  659. fd_focus_name(ui, fd);
  660. if (e->is_dir) {
  661. clicked_dir = (int)i;
  662. } else {
  663. fd_set_name(fd, e->name);
  664. if (dbl)
  665. accept_after = 1;
  666. }
  667. ngui_invalidate(ui);
  668. }
  669. }
  670. ngui_scroll_end(ui, list, &fd->scroll);
  671. name_r = (struct ngui_rect){ px + FD_PAD + 86, name_y,
  672. pw - 2 * FD_PAD - 86, 28 };
  673. if (name_r.w < 80)
  674. name_r.w = 80;
  675. ngui_text(ui, px + FD_PAD, name_y + 6, "File name",
  676. ui->theme.text_dim);
  677. {
  678. int input_result = ngui_text_input(ui, id_name, name_r,
  679. &fd->name);
  680. if (input_result & NGUI_INPUT_CHANGED)
  681. fd->error[0] = '\0';
  682. if (input_result & NGUI_INPUT_SUBMIT) {
  683. if (fd_accept(fd))
  684. event = NGUI_FILE_ACCEPTED;
  685. else
  686. ngui_invalidate(ui);
  687. }
  688. if ((input_result & NGUI_INPUT_CANCEL) &&
  689. !(input_result & NGUI_INPUT_NEXT)) {
  690. event = NGUI_FILE_CANCELLED;
  691. fd->active = 0;
  692. }
  693. if (input_result & NGUI_INPUT_NEXT)
  694. fd_focus_name(ui, fd);
  695. }
  696. if (clicked_dir >= 0) {
  697. char path[NGUI_FILE_PATH_MAX];
  698. if (fl_join(fd->path, fd->entries[clicked_dir].name,
  699. path, sizeof(path)) &&
  700. fd_load_dir(fd, path) == 0)
  701. fd_set_name(fd, "");
  702. clicked_dir = -1;
  703. }
  704. if (accept_after && event == NGUI_FILE_NONE) {
  705. if (fd_accept(fd))
  706. event = NGUI_FILE_ACCEPTED;
  707. else
  708. ngui_invalidate(ui);
  709. }
  710. if (event == NGUI_FILE_NONE && ui->focus != id_name) {
  711. k = ngui_key_peek(ui);
  712. if (k == '\r' || k == '\n') {
  713. ngui_key(ui);
  714. if (fd_accept(fd))
  715. event = NGUI_FILE_ACCEPTED;
  716. else
  717. ngui_invalidate(ui);
  718. } else if (k == NGUI_KEY_ESC) {
  719. ngui_key(ui);
  720. event = NGUI_FILE_CANCELLED;
  721. fd->active = 0;
  722. }
  723. }
  724. if (fd->error[0])
  725. ngui_text_ellipsis(ui,
  726. (struct ngui_rect){ px + FD_PAD, err_y,
  727. pw - 2 * FD_PAD, 18 },
  728. px + FD_PAD, err_y, fd->error, ui->theme.error);
  729. else if (fd->truncated)
  730. ngui_text(ui, px + FD_PAD, err_y,
  731. "Showing the first 128 entries", ui->theme.text_dim);
  732. if (ngui_button(ui, fd->id_base + FD_ID_CANCEL,
  733. (struct ngui_rect){ px + pw - 16 - 84 - 8 - 84,
  734. btn_y, 84, 28 }, "Cancel")) {
  735. event = NGUI_FILE_CANCELLED;
  736. fd->active = 0;
  737. }
  738. if (ngui_button(ui, fd->id_base + FD_ID_ACCEPT,
  739. (struct ngui_rect){ px + pw - 16 - 84, btn_y,
  740. 84, 28 },
  741. fd->mode == NGUI_FILE_OPEN ? "Open" : "Save")) {
  742. if (fd_accept(fd))
  743. event = NGUI_FILE_ACCEPTED;
  744. else
  745. ngui_invalidate(ui);
  746. fd_focus_name(ui, fd);
  747. }
  748. if (fd->need_focus) {
  749. fd->need_focus = 0;
  750. fd_focus_name(ui, fd);
  751. if (fd->selected >= 0)
  752. ngui_scroll_ensure_visible(&fd->scroll,
  753. 2 + fd->selected * FD_ROW_H, FD_ROW_H);
  754. }
  755. return event;
  756. }
  757. void ngui_file_dialog_close(struct ngui_file_dialog *fd)
  758. {
  759. if (!fd)
  760. return;
  761. fd->active = 0;
  762. fd->error[0] = '\0';
  763. }
  764. void ngui_file_dialog_error(struct ngui_file_dialog *fd, const char *msg)
  765. {
  766. if (!fd)
  767. return;
  768. fl_copy(fd->error, msg ? msg : "Cannot use that path",
  769. sizeof(fd->error));
  770. fd->active = 1;
  771. fd->need_focus = 1;
  772. }
  773. const char *ngui_file_dialog_result(const struct ngui_file_dialog *fd)
  774. {
  775. return fd ? fd->result : "";
  776. }