ucmd_textedit.c 16 KB

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