ucmd_textedit.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /* textedit: a Notepad-like plain text editor for a canvas window.
  2. *
  3. * The text area is ngui's multi-line editor widget (soft wrap, caret,
  4. * mouse/double-click/drag selection, Ctrl+A, word jumps, page keys and a
  5. * vertical scrollbar). The toolbar offers New, Open, Save and Save As; Open
  6. * and Save As use ngui's modal file dialog (directory list, mounts root,
  7. * Up/Home/Refresh and a file-name field). The status bar shows the caret
  8. * line/column and the file name with a modified marker.
  9. *
  10. * Keys: the printable set, Enter, Tab, Backspace/Delete and the arrows edit;
  11. * Ctrl+Left/Right and Ctrl+Backspace/Delete work by word, Ctrl+A selects all,
  12. * Ctrl+N new, Ctrl+O open, Ctrl+S save, Ctrl+Q (or Esc) quit. A modified
  13. * buffer gets a discard confirmation before new/open/quit. The window close
  14. * button still ends the process without a prompt (the canvas API has no
  15. * close-request event).
  16. *
  17. * The first argument is a path to open (relative paths resolve against the
  18. * inherited cwd); a missing file becomes the document's save path. */
  19. #include "ucmd_lib.h"
  20. #include "ngui.h"
  21. #define TE_MAX (256u * 1024u)
  22. #define TE_BUF_SIZE (TE_MAX + 512u) /* the FAT read may touch the last sector */
  23. #define TE_PATH_MAX 64
  24. #define TE_TOOLBAR_H 38
  25. #define TE_STATUS_H 24
  26. #define TE_MARGIN 6
  27. #define TE_STATUS_MS 4000
  28. enum {
  29. ID_AREA = 1,
  30. ID_NEW,
  31. ID_OPEN,
  32. ID_SAVE,
  33. ID_SAVEAS,
  34. ID_CONFIRM_DISCARD = 0x20,
  35. ID_CONFIRM_CANCEL
  36. };
  37. enum {
  38. MODAL_NONE,
  39. MODAL_CONFIRM
  40. };
  41. enum {
  42. ACT_NONE,
  43. ACT_NEW,
  44. ACT_OPEN,
  45. ACT_QUIT
  46. };
  47. static struct ngui ui;
  48. static struct ngui_text_area area;
  49. static struct ngui_file_dialog file_dlg;
  50. static char text[TE_BUF_SIZE];
  51. static char doc_path[TE_PATH_MAX];
  52. static int dirty;
  53. static int quit;
  54. static int modal;
  55. static int pending_action;
  56. static char status_text[128];
  57. static unsigned int status_until;
  58. static int status_error;
  59. /* ---- small freestanding string helpers ---- */
  60. static unsigned int s_len(const char *s)
  61. {
  62. unsigned int n = 0;
  63. while (s && s[n])
  64. n++;
  65. return n;
  66. }
  67. static void s_copy(char *d, const char *s, unsigned int cap)
  68. {
  69. unsigned int i = 0;
  70. if (!cap)
  71. return;
  72. while (s && s[i] && i + 1 < cap) {
  73. d[i] = s[i];
  74. i++;
  75. }
  76. d[i] = '\0';
  77. }
  78. static void s_append(char *d, unsigned int *n, const char *s,
  79. unsigned int cap)
  80. {
  81. while (s && *s && *n + 1 < cap)
  82. d[(*n)++] = *s++;
  83. d[*n] = '\0';
  84. }
  85. static void s_append_uint(char *d, unsigned int *n, unsigned int v,
  86. unsigned int cap)
  87. {
  88. char tmp[12];
  89. unsigned int i = 0, j;
  90. do {
  91. tmp[i++] = (char)('0' + v % 10);
  92. v /= 10;
  93. } while (v && i < sizeof(tmp));
  94. for (j = 0; j < i && *n + 1 < cap; j++)
  95. d[(*n)++] = tmp[i - 1 - j];
  96. d[*n] = '\0';
  97. }
  98. static const char *base_name(const char *path)
  99. {
  100. const char *b = path;
  101. unsigned int i;
  102. for (i = 0; path && path[i]; i++)
  103. if (path[i] == '/')
  104. b = path + i + 1;
  105. return b;
  106. }
  107. static void set_status(const char *msg, int error)
  108. {
  109. s_copy(status_text, msg, sizeof(status_text));
  110. status_until = u_ticks_ms() + TE_STATUS_MS;
  111. status_error = error;
  112. ngui_invalidate(&ui);
  113. }
  114. static const char *read_error(int r)
  115. {
  116. switch (r) {
  117. case SYS_ENODEV:
  118. return "No such device";
  119. case SYS_ENOFS:
  120. return "No filesystem on the device";
  121. case SYS_ENOENT:
  122. return "File not found";
  123. case SYS_EIO:
  124. return "Disk read error";
  125. case SYS_EINVAL:
  126. return "Invalid path";
  127. default:
  128. return "Cannot read the file";
  129. }
  130. }
  131. static const char *write_error(int r)
  132. {
  133. switch (r) {
  134. case SYS_ENODEV:
  135. return "No such device";
  136. case SYS_ENOFS:
  137. return "No filesystem on the device";
  138. case SYS_ENOENT:
  139. return "Path not found";
  140. case SYS_EROFS:
  141. return "Read-only filesystem";
  142. case SYS_EWRITE:
  143. return "Disk full or write refused";
  144. case SYS_ENOSPC:
  145. return "No space left on the filesystem";
  146. case SYS_EINVAL:
  147. return "Invalid path";
  148. case SYS_EIO:
  149. return "Disk write error";
  150. default:
  151. return "Cannot save the file";
  152. }
  153. }
  154. static void reset_view(void)
  155. {
  156. area.len = 0;
  157. area.cursor = 0;
  158. area.anchor = 0;
  159. area.dragging = 0;
  160. area.scroll.offset = 0;
  161. area.scroll.bar_vis = 0;
  162. area.scroll.bar_target = 0;
  163. area.line = 1;
  164. area.column = 1;
  165. text[0] = '\0';
  166. }
  167. static void new_document(void)
  168. {
  169. reset_view();
  170. doc_path[0] = '\0';
  171. dirty = 0;
  172. set_status("New document", 0);
  173. ngui_set_focus(&ui, ID_AREA);
  174. ngui_invalidate(&ui);
  175. }
  176. /* Load a file into the editor. With allow_new, a missing path becomes the
  177. * document's save path instead of an error (a launch argument such as
  178. * `textedit notes.txt`). Returns NULL on success or an error string. */
  179. static const char *load_file(const char *path, int allow_new)
  180. {
  181. struct sys_stat st;
  182. unsigned int i, j;
  183. int n, r;
  184. if (!path || !path[0])
  185. return "No path given";
  186. if (s_len(path) >= TE_PATH_MAX)
  187. return "Path is too long";
  188. r = u_stat(path, &st);
  189. if (r == SYS_ENOENT) {
  190. if (!allow_new)
  191. return "File not found";
  192. reset_view();
  193. s_copy(doc_path, path, sizeof(doc_path));
  194. dirty = 0;
  195. set_status("New file", 0);
  196. return 0;
  197. }
  198. if (r != 0)
  199. return read_error(r);
  200. if (st.is_dir)
  201. return "That is a directory";
  202. if (st.size > TE_MAX)
  203. return "File is larger than 256 KB";
  204. n = u_read_file(path, text, TE_MAX);
  205. if (n < 0)
  206. return read_error(n);
  207. /* Normalize CRLF and lone CR to LF; the editor and save path use LF. */
  208. j = 0;
  209. for (i = 0; i < (unsigned int)n; i++) {
  210. if (text[i] == '\r') {
  211. if (i + 1 < (unsigned int)n && text[i + 1] == '\n')
  212. continue;
  213. text[j++] = '\n';
  214. } else {
  215. text[j++] = text[i];
  216. }
  217. }
  218. text[j] = '\0';
  219. area.len = j;
  220. area.cursor = 0;
  221. area.anchor = 0;
  222. area.dragging = 0;
  223. area.scroll.offset = 0;
  224. area.scroll.bar_vis = 0;
  225. area.scroll.bar_target = 0;
  226. area.line = 1;
  227. area.column = 1;
  228. s_copy(doc_path, path, sizeof(doc_path));
  229. dirty = 0;
  230. set_status("Opened file", 0);
  231. return 0;
  232. }
  233. static const char *save_file(const char *path)
  234. {
  235. int r;
  236. if (!path || !path[0])
  237. return "No path given";
  238. if (s_len(path) >= TE_PATH_MAX)
  239. return "Path is too long";
  240. r = u_write_file(path, text, area.len);
  241. if (r != 0)
  242. return write_error(r);
  243. s_copy(doc_path, path, sizeof(doc_path));
  244. dirty = 0;
  245. set_status("Saved", 0);
  246. return 0;
  247. }
  248. static void close_modal(void)
  249. {
  250. modal = MODAL_NONE;
  251. ngui_set_focus(&ui, ID_AREA);
  252. ngui_invalidate(&ui);
  253. }
  254. /* Open the ngui file dialog. With a current document, start in its folder
  255. * with its name selected; a brand-new Save As starts in /share. */
  256. static void open_file_dialog(int mode)
  257. {
  258. const char *start = "";
  259. const char *suggested = "";
  260. char base[TE_PATH_MAX];
  261. if (doc_path[0]) {
  262. start = doc_path;
  263. s_copy(base, base_name(doc_path), sizeof(base));
  264. suggested = base;
  265. } else if (mode == NGUI_FILE_SAVE) {
  266. start = "/share";
  267. suggested = "UNTITLED.TXT";
  268. }
  269. ngui_file_dialog_open(&file_dlg, mode, start, suggested, 0x4000);
  270. ngui_invalidate(&ui);
  271. }
  272. /* The dialog stays active until the app closes it, so a failed load/save can
  273. * put its error back into the dialog and let the user adjust the name. */
  274. static void handle_file_dialog_result(int result)
  275. {
  276. const char *path, *err;
  277. if (result == NGUI_FILE_CANCELLED) {
  278. ngui_file_dialog_close(&file_dlg);
  279. ngui_set_focus(&ui, ID_AREA);
  280. ngui_invalidate(&ui);
  281. return;
  282. }
  283. if (result != NGUI_FILE_ACCEPTED)
  284. return;
  285. path = ngui_file_dialog_result(&file_dlg);
  286. if (file_dlg.mode == NGUI_FILE_OPEN)
  287. err = load_file(path, 0);
  288. else
  289. err = save_file(path);
  290. if (err) {
  291. ngui_file_dialog_error(&file_dlg, err);
  292. ngui_invalidate(&ui);
  293. return;
  294. }
  295. ngui_file_dialog_close(&file_dlg);
  296. ngui_set_focus(&ui, ID_AREA);
  297. ngui_invalidate(&ui);
  298. }
  299. static void do_save(void)
  300. {
  301. if (doc_path[0]) {
  302. const char *err = save_file(doc_path);
  303. if (err)
  304. set_status(err, 1);
  305. ngui_set_focus(&ui, ID_AREA);
  306. ngui_invalidate(&ui);
  307. } else {
  308. open_file_dialog(NGUI_FILE_SAVE);
  309. }
  310. }
  311. static void run_action(int action)
  312. {
  313. switch (action) {
  314. case ACT_NEW:
  315. new_document();
  316. break;
  317. case ACT_OPEN:
  318. open_file_dialog(NGUI_FILE_OPEN);
  319. break;
  320. case ACT_QUIT:
  321. quit = 1;
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. /* New/Open/Quit lose the buffer; ask before discarding a modified one. */
  328. static void request_action(int action)
  329. {
  330. if (action == ACT_NONE)
  331. return;
  332. if (dirty) {
  333. modal = MODAL_CONFIRM;
  334. pending_action = action;
  335. ngui_set_focus(&ui, 0);
  336. ngui_invalidate(&ui);
  337. return;
  338. }
  339. run_action(action);
  340. }
  341. static void discard_and_run(void)
  342. {
  343. int action = pending_action;
  344. pending_action = ACT_NONE;
  345. modal = MODAL_NONE;
  346. /* Do not clear `dirty` here: an Open dialog can still be cancelled,
  347. * and the existing buffer must keep its modified state until a file
  348. * is actually loaded. New/load/quit update it themselves. */
  349. run_action(action);
  350. if (!quit)
  351. ngui_set_focus(&ui, ID_AREA);
  352. ngui_invalidate(&ui);
  353. }
  354. /* ---- drawing ---- */
  355. static void draw_toolbar(struct ngui_rect r)
  356. {
  357. static const char *const labels[4] = { "New", "Open", "Save", "Save As" };
  358. static const unsigned int ids[4] = {
  359. ID_NEW, ID_OPEN, ID_SAVE, ID_SAVEAS
  360. };
  361. int x = r.x + 8;
  362. int i;
  363. ngui_fill_rect(&ui, r, ui.theme.panel);
  364. ngui_fill_rect(&ui, (struct ngui_rect){ r.x, r.y + r.h - 1, r.w, 1 },
  365. ui.theme.border);
  366. for (i = 0; i < 4; i++) {
  367. int w = (int)ngui_text_width_ui(&ui, labels[i]) + 26;
  368. if (x + w > r.x + r.w - 8)
  369. break;
  370. if (ngui_button(&ui, ids[i],
  371. (struct ngui_rect){ x, r.y + 6, w, 26 },
  372. labels[i])) {
  373. switch (i) {
  374. case 0:
  375. request_action(ACT_NEW);
  376. break;
  377. case 1:
  378. open_file_dialog(NGUI_FILE_OPEN);
  379. break;
  380. case 2:
  381. do_save();
  382. break;
  383. case 3:
  384. open_file_dialog(NGUI_FILE_SAVE);
  385. break;
  386. default:
  387. break;
  388. }
  389. }
  390. x += w + 6;
  391. }
  392. {
  393. const char *name = doc_path[0] ? base_name(doc_path) :
  394. "Untitled";
  395. char label[96];
  396. unsigned int n = 0;
  397. int tw;
  398. s_append(label, &n, name, sizeof(label));
  399. if (dirty)
  400. s_append(label, &n, " *", sizeof(label));
  401. tw = (int)ngui_text_width_ui(&ui, label);
  402. if (i > 0 && x + 12 < r.x + r.w - tw) {
  403. ngui_text_ellipsis(&ui,
  404. (struct ngui_rect){ x + 8, r.y,
  405. r.x + r.w - x - 16, r.h },
  406. x + 8, r.y + (r.h - 16) / 2, label,
  407. ui.theme.text_dim);
  408. }
  409. }
  410. }
  411. static void draw_status(struct ngui_rect r)
  412. {
  413. char left[128];
  414. char right[96];
  415. unsigned int n = 0, m = 0;
  416. int rw, rx;
  417. ngui_fill_rect(&ui, r, ui.theme.panel);
  418. ngui_fill_rect(&ui, (struct ngui_rect){ r.x, r.y, r.w, 1 },
  419. ui.theme.border);
  420. if (status_text[0] && status_until &&
  421. (int)(u_ticks_ms() - status_until) < 0) {
  422. s_append(left, &n, status_text, sizeof(left));
  423. } else {
  424. const char *name = doc_path[0] ? base_name(doc_path) :
  425. "Untitled";
  426. s_append(left, &n, "Ln ", sizeof(left));
  427. s_append_uint(left, &n, area.line, sizeof(left));
  428. s_append(left, &n, ", Col ", sizeof(left));
  429. s_append_uint(left, &n, area.column, sizeof(left));
  430. s_append(left, &n, " ", sizeof(left));
  431. s_append_uint(left, &n, area.len, sizeof(left));
  432. s_append(left, &n, " bytes ", sizeof(left));
  433. s_append(left, &n, name, sizeof(left));
  434. if (dirty)
  435. s_append(left, &n, " (modified)", sizeof(left));
  436. }
  437. /* Right side: the save/open shortcuts. */
  438. s_append(right, &m, "Ctrl+S save Ctrl+O open", sizeof(right));
  439. rw = (int)ngui_text_width_ui(&ui, right);
  440. rx = r.x + r.w - rw - 10;
  441. if (rx > r.x + 20)
  442. ngui_text_ellipsis(&ui,
  443. (struct ngui_rect){ r.x + 10, r.y, rx - r.x - 16, r.h },
  444. r.x + 10, r.y + (r.h - 16) / 2, left,
  445. status_error ? ui.theme.error : ui.theme.text);
  446. if (rx > r.x + 20)
  447. ngui_text(&ui, rx, r.y + (r.h - 16) / 2, right,
  448. ui.theme.text_dim);
  449. }
  450. static void draw_confirm_modal(void)
  451. {
  452. int mw, mh, mx, my;
  453. struct ngui_rect panel;
  454. ngui_set_cursor(&ui, CURSOR_DEFAULT);
  455. mw = ui.w - 40;
  456. if (mw > 440)
  457. mw = 440;
  458. if (mw < 240)
  459. mw = 240;
  460. mh = 132;
  461. mx = (ui.w - mw) / 2;
  462. my = (ui.h - mh) / 2;
  463. panel = (struct ngui_rect){ mx, my, mw, mh };
  464. ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
  465. NGUI_RGB(8, 10, 16));
  466. ngui_fill_round_rect(&ui, panel, NGUI_RADIUS, NGUI_RADIUS,
  467. NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
  468. ngui_frame_round(&ui, panel, NGUI_RADIUS, NGUI_RADIUS,
  469. NGUI_RADIUS, NGUI_RADIUS, 1, ui.theme.border);
  470. ngui_text(&ui, mx + 18, my + 18, "Discard unsaved changes?",
  471. ui.theme.text);
  472. ngui_text_ellipsis(&ui,
  473. (struct ngui_rect){ mx + 18, my + 42, mw - 36, 18 },
  474. mx + 18, my + 42,
  475. doc_path[0] ? base_name(doc_path) : "Untitled",
  476. ui.theme.text_dim);
  477. if (ngui_button(&ui, ID_CONFIRM_CANCEL,
  478. (struct ngui_rect){ mx + mw - 188, my + mh - 44,
  479. 84, 28 }, "Cancel"))
  480. close_modal();
  481. if (ngui_button(&ui, ID_CONFIRM_DISCARD,
  482. (struct ngui_rect){ mx + mw - 96, my + mh - 44,
  483. 78, 28 }, "Discard"))
  484. discard_and_run();
  485. }
  486. static void draw(void)
  487. {
  488. struct ngui_rect all = { 0, 0, ui.w, ui.h };
  489. struct ngui_rect toolbar, status, edit;
  490. int res;
  491. if (file_dlg.active) {
  492. handle_file_dialog_result(ngui_file_dialog(&ui, &file_dlg));
  493. return;
  494. }
  495. ngui_fill_rect(&ui, all, ui.theme.bg);
  496. toolbar = ngui_cut_top(&all, TE_TOOLBAR_H);
  497. status = ngui_cut_bottom(&all, TE_STATUS_H);
  498. ngui_inset(&all, TE_MARGIN);
  499. edit = all;
  500. draw_toolbar(toolbar);
  501. /* A toolbar action may have opened the file dialog. */
  502. if (file_dlg.active) {
  503. handle_file_dialog_result(ngui_file_dialog(&ui, &file_dlg));
  504. return;
  505. }
  506. if (modal == MODAL_NONE) {
  507. if (edit.w < 1)
  508. edit.w = 1;
  509. if (edit.h < 1)
  510. edit.h = 1;
  511. area.scroll.hide_bar = 0;
  512. res = ngui_text_area(&ui, ID_AREA, edit, &area);
  513. if (res & NGUI_TEXTAREA_CHANGED)
  514. dirty = 1;
  515. } else {
  516. res = 0;
  517. }
  518. draw_status(status);
  519. if (res & NGUI_TEXTAREA_CANCEL)
  520. request_action(ACT_QUIT);
  521. if (modal != MODAL_NONE)
  522. draw_confirm_modal();
  523. }
  524. static void handle_keys(void)
  525. {
  526. int k;
  527. if (file_dlg.active) {
  528. /* The dialog has already handled its keys; discard any app
  529. * shortcut it deliberately left alone. */
  530. while (ngui_key(&ui) != 0)
  531. ;
  532. return;
  533. }
  534. while ((k = ngui_key(&ui)) != 0) {
  535. if (modal == MODAL_CONFIRM) {
  536. if (k == NGUI_KEY_ESC)
  537. close_modal();
  538. else if (k == '\r' || k == '\n')
  539. discard_and_run();
  540. continue;
  541. }
  542. switch (k) {
  543. case NGUI_KEY_ESC:
  544. request_action(ACT_QUIT);
  545. break;
  546. case 0x0E: /* Ctrl+N */
  547. request_action(ACT_NEW);
  548. break;
  549. case 0x0F: /* Ctrl+O */
  550. request_action(ACT_OPEN);
  551. break;
  552. case 0x13: /* Ctrl+S */
  553. do_save();
  554. break;
  555. case 0x11: /* Ctrl+Q */
  556. case 0x17: /* Ctrl+W */
  557. request_action(ACT_QUIT);
  558. break;
  559. default:
  560. break;
  561. }
  562. }
  563. }
  564. void _start(const char *arg)
  565. {
  566. struct win_info wi;
  567. if (u_win_create(820, 620, "Textedit", &wi) != 0) {
  568. u_puts("textedit: no graphics window\n");
  569. u_exit(1);
  570. }
  571. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  572. ngui_font_attach(&ui);
  573. area.text = text;
  574. area.cap = TE_MAX + 1;
  575. area.len = 0;
  576. area.cursor = 0;
  577. area.anchor = 0;
  578. area.dragging = 0;
  579. area.read_only = 0;
  580. area.line = 1;
  581. area.column = 1;
  582. text[0] = '\0';
  583. doc_path[0] = '\0';
  584. dirty = 0;
  585. modal = MODAL_NONE;
  586. pending_action = ACT_NONE;
  587. file_dlg.active = 0;
  588. if (arg && arg[0]) {
  589. const char *err = load_file(arg, 1);
  590. if (err)
  591. set_status(err, 1);
  592. } else {
  593. set_status("New document", 0);
  594. }
  595. ngui_set_focus(&ui, ID_AREA);
  596. ngui_invalidate(&ui);
  597. for (;;) {
  598. int want;
  599. if (status_until &&
  600. (int)(u_ticks_ms() - status_until) >= 0) {
  601. status_text[0] = '\0';
  602. status_until = 0;
  603. status_error = 0;
  604. ngui_invalidate(&ui);
  605. }
  606. want = ngui_begin(&ui);
  607. if (quit)
  608. break;
  609. if (!want) {
  610. ngui_end_ex(&ui, 0);
  611. u_sleep_ms(10);
  612. continue;
  613. }
  614. draw();
  615. handle_keys();
  616. if (quit)
  617. break;
  618. ngui_end(&ui);
  619. u_sleep_ms(10);
  620. }
  621. u_win_close();
  622. u_exit(0);
  623. }