| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685 |
- /* textedit: a Notepad-like plain text editor for a canvas window.
- *
- * The text area is ngui's multi-line editor widget (soft wrap, caret,
- * mouse/double-click/drag selection, Ctrl+A, word jumps, page keys and a
- * vertical scrollbar). The toolbar offers New, Open, Save and Save As; Open
- * and Save As use ngui's modal file dialog (directory list, mounts root,
- * Up/Home/Refresh and a file-name field). The status bar shows the caret
- * line/column and the file name with a modified marker.
- *
- * Keys: the printable set, Enter, Tab, Backspace/Delete and the arrows edit;
- * Ctrl+Left/Right and Ctrl+Backspace/Delete work by word, Ctrl+A selects all,
- * Ctrl+N new, Ctrl+O open, Ctrl+S save, Ctrl+Q (or Esc) quit. A modified
- * buffer gets a discard confirmation before new/open/quit. The window close
- * button still ends the process without a prompt (the canvas API has no
- * close-request event).
- *
- * The first argument is a path to open (relative paths resolve against the
- * inherited cwd); a missing file becomes the document's save path. */
- #include "ucmd_lib.h"
- #include "ngui.h"
- #define TE_MAX (256u * 1024u)
- #define TE_BUF_SIZE (TE_MAX + 512u) /* the FAT read may touch the last sector */
- #define TE_PATH_MAX 64
- #define TE_TOOLBAR_H 38
- #define TE_STATUS_H 24
- #define TE_MARGIN 6
- #define TE_STATUS_MS 4000
- enum {
- ID_AREA = 1,
- ID_NEW,
- ID_OPEN,
- ID_SAVE,
- ID_SAVEAS,
- ID_CONFIRM_DISCARD = 0x20,
- ID_CONFIRM_CANCEL
- };
- enum {
- MODAL_NONE,
- MODAL_CONFIRM
- };
- enum {
- ACT_NONE,
- ACT_NEW,
- ACT_OPEN,
- ACT_QUIT
- };
- static struct ngui ui;
- static struct ngui_text_area area;
- static struct ngui_file_dialog file_dlg;
- static char text[TE_BUF_SIZE];
- static char doc_path[TE_PATH_MAX];
- static int dirty;
- static int quit;
- static int modal;
- static int pending_action;
- static char status_text[128];
- static unsigned int status_until;
- static int status_error;
- /* ---- small freestanding string helpers ---- */
- static unsigned int s_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static void s_copy(char *d, const char *s, unsigned int cap)
- {
- unsigned int i = 0;
- if (!cap)
- return;
- while (s && s[i] && i + 1 < cap) {
- d[i] = s[i];
- i++;
- }
- d[i] = '\0';
- }
- static void s_append(char *d, unsigned int *n, const char *s,
- unsigned int cap)
- {
- while (s && *s && *n + 1 < cap)
- d[(*n)++] = *s++;
- d[*n] = '\0';
- }
- static void s_append_uint(char *d, unsigned int *n, unsigned int v,
- unsigned int cap)
- {
- char tmp[12];
- unsigned int i = 0, j;
- do {
- tmp[i++] = (char)('0' + v % 10);
- v /= 10;
- } while (v && i < sizeof(tmp));
- for (j = 0; j < i && *n + 1 < cap; j++)
- d[(*n)++] = tmp[i - 1 - j];
- d[*n] = '\0';
- }
- static const char *base_name(const char *path)
- {
- const char *b = path;
- unsigned int i;
- for (i = 0; path && path[i]; i++)
- if (path[i] == '/')
- b = path + i + 1;
- return b;
- }
- static void set_status(const char *msg, int error)
- {
- s_copy(status_text, msg, sizeof(status_text));
- status_until = u_ticks_ms() + TE_STATUS_MS;
- status_error = error;
- ngui_invalidate(&ui);
- }
- static const char *read_error(int r)
- {
- switch (r) {
- case SYS_ENODEV:
- return "No such device";
- case SYS_ENOFS:
- return "No filesystem on the device";
- case SYS_ENOENT:
- return "File not found";
- case SYS_EIO:
- return "Disk read error";
- case SYS_EINVAL:
- return "Invalid path";
- default:
- return "Cannot read the file";
- }
- }
- static const char *write_error(int r)
- {
- switch (r) {
- case SYS_ENODEV:
- return "No such device";
- case SYS_ENOFS:
- return "No filesystem on the device";
- case SYS_ENOENT:
- return "Path not found";
- case SYS_EROFS:
- return "Read-only filesystem";
- case SYS_EWRITE:
- return "Disk full or write refused";
- case SYS_ENOSPC:
- return "No space left on the filesystem";
- case SYS_EINVAL:
- return "Invalid path";
- case SYS_EIO:
- return "Disk write error";
- default:
- return "Cannot save the file";
- }
- }
- static void reset_view(void)
- {
- area.len = 0;
- area.cursor = 0;
- area.anchor = 0;
- area.dragging = 0;
- area.scroll.offset = 0;
- area.scroll.bar_vis = 0;
- area.scroll.bar_target = 0;
- area.line = 1;
- area.column = 1;
- text[0] = '\0';
- }
- static void new_document(void)
- {
- reset_view();
- doc_path[0] = '\0';
- dirty = 0;
- set_status("New document", 0);
- ngui_set_focus(&ui, ID_AREA);
- ngui_invalidate(&ui);
- }
- /* Load a file into the editor. With allow_new, a missing path becomes the
- * document's save path instead of an error (a launch argument such as
- * `textedit notes.txt`). Returns NULL on success or an error string. */
- static const char *load_file(const char *path, int allow_new)
- {
- struct sys_stat st;
- unsigned int i, j;
- int n, r;
- if (!path || !path[0])
- return "No path given";
- if (s_len(path) >= TE_PATH_MAX)
- return "Path is too long";
- r = u_stat(path, &st);
- if (r == SYS_ENOENT) {
- if (!allow_new)
- return "File not found";
- reset_view();
- s_copy(doc_path, path, sizeof(doc_path));
- dirty = 0;
- set_status("New file", 0);
- return 0;
- }
- if (r != 0)
- return read_error(r);
- if (st.is_dir)
- return "That is a directory";
- if (st.size > TE_MAX)
- return "File is larger than 256 KB";
- n = u_read_file(path, text, TE_MAX);
- if (n < 0)
- return read_error(n);
- /* Normalize CRLF and lone CR to LF; the editor and save path use LF. */
- j = 0;
- for (i = 0; i < (unsigned int)n; i++) {
- if (text[i] == '\r') {
- if (i + 1 < (unsigned int)n && text[i + 1] == '\n')
- continue;
- text[j++] = '\n';
- } else {
- text[j++] = text[i];
- }
- }
- text[j] = '\0';
- area.len = j;
- area.cursor = 0;
- area.anchor = 0;
- area.dragging = 0;
- area.scroll.offset = 0;
- area.scroll.bar_vis = 0;
- area.scroll.bar_target = 0;
- area.line = 1;
- area.column = 1;
- s_copy(doc_path, path, sizeof(doc_path));
- dirty = 0;
- set_status("Opened file", 0);
- return 0;
- }
- static const char *save_file(const char *path)
- {
- int r;
- if (!path || !path[0])
- return "No path given";
- if (s_len(path) >= TE_PATH_MAX)
- return "Path is too long";
- r = u_write_file(path, text, area.len);
- if (r != 0)
- return write_error(r);
- s_copy(doc_path, path, sizeof(doc_path));
- dirty = 0;
- set_status("Saved", 0);
- return 0;
- }
- static void close_modal(void)
- {
- modal = MODAL_NONE;
- ngui_set_focus(&ui, ID_AREA);
- ngui_invalidate(&ui);
- }
- /* Open the ngui file dialog. With a current document, start in its folder
- * with its name selected; a brand-new Save As starts in /share. */
- static void open_file_dialog(int mode)
- {
- const char *start = "";
- const char *suggested = "";
- char base[TE_PATH_MAX];
- if (doc_path[0]) {
- start = doc_path;
- s_copy(base, base_name(doc_path), sizeof(base));
- suggested = base;
- } else if (mode == NGUI_FILE_SAVE) {
- start = "/share";
- suggested = "UNTITLED.TXT";
- }
- ngui_file_dialog_open(&file_dlg, mode, start, suggested, 0x4000);
- ngui_invalidate(&ui);
- }
- /* The dialog stays active until the app closes it, so a failed load/save can
- * put its error back into the dialog and let the user adjust the name. */
- static void handle_file_dialog_result(int result)
- {
- const char *path, *err;
- if (result == NGUI_FILE_CANCELLED) {
- ngui_file_dialog_close(&file_dlg);
- ngui_set_focus(&ui, ID_AREA);
- ngui_invalidate(&ui);
- return;
- }
- if (result != NGUI_FILE_ACCEPTED)
- return;
- path = ngui_file_dialog_result(&file_dlg);
- if (file_dlg.mode == NGUI_FILE_OPEN)
- err = load_file(path, 0);
- else
- err = save_file(path);
- if (err) {
- ngui_file_dialog_error(&file_dlg, err);
- ngui_invalidate(&ui);
- return;
- }
- ngui_file_dialog_close(&file_dlg);
- ngui_set_focus(&ui, ID_AREA);
- ngui_invalidate(&ui);
- }
- static void do_save(void)
- {
- if (doc_path[0]) {
- const char *err = save_file(doc_path);
- if (err)
- set_status(err, 1);
- ngui_set_focus(&ui, ID_AREA);
- ngui_invalidate(&ui);
- } else {
- open_file_dialog(NGUI_FILE_SAVE);
- }
- }
- static void run_action(int action)
- {
- switch (action) {
- case ACT_NEW:
- new_document();
- break;
- case ACT_OPEN:
- open_file_dialog(NGUI_FILE_OPEN);
- break;
- case ACT_QUIT:
- quit = 1;
- break;
- default:
- break;
- }
- }
- /* New/Open/Quit lose the buffer; ask before discarding a modified one. */
- static void request_action(int action)
- {
- if (action == ACT_NONE)
- return;
- if (dirty) {
- modal = MODAL_CONFIRM;
- pending_action = action;
- ngui_set_focus(&ui, 0);
- ngui_invalidate(&ui);
- return;
- }
- run_action(action);
- }
- static void discard_and_run(void)
- {
- int action = pending_action;
- pending_action = ACT_NONE;
- modal = MODAL_NONE;
- /* Do not clear `dirty` here: an Open dialog can still be cancelled,
- * and the existing buffer must keep its modified state until a file
- * is actually loaded. New/load/quit update it themselves. */
- run_action(action);
- if (!quit)
- ngui_set_focus(&ui, ID_AREA);
- ngui_invalidate(&ui);
- }
- /* ---- drawing ---- */
- static void draw_toolbar(struct ngui_rect r)
- {
- static const char *const labels[4] = { "New", "Open", "Save", "Save As" };
- static const unsigned int ids[4] = {
- ID_NEW, ID_OPEN, ID_SAVE, ID_SAVEAS
- };
- int x = r.x + 8;
- int i;
- ngui_fill_rect(&ui, r, ui.theme.panel);
- ngui_fill_rect(&ui, (struct ngui_rect){ r.x, r.y + r.h - 1, r.w, 1 },
- ui.theme.border);
- for (i = 0; i < 4; i++) {
- int w = (int)ngui_text_width_ui(&ui, labels[i]) + 26;
- if (x + w > r.x + r.w - 8)
- break;
- if (ngui_button(&ui, ids[i],
- (struct ngui_rect){ x, r.y + 6, w, 26 },
- labels[i])) {
- switch (i) {
- case 0:
- request_action(ACT_NEW);
- break;
- case 1:
- open_file_dialog(NGUI_FILE_OPEN);
- break;
- case 2:
- do_save();
- break;
- case 3:
- open_file_dialog(NGUI_FILE_SAVE);
- break;
- default:
- break;
- }
- }
- x += w + 6;
- }
- {
- const char *name = doc_path[0] ? base_name(doc_path) :
- "Untitled";
- char label[96];
- unsigned int n = 0;
- int tw;
- s_append(label, &n, name, sizeof(label));
- if (dirty)
- s_append(label, &n, " *", sizeof(label));
- tw = (int)ngui_text_width_ui(&ui, label);
- if (i > 0 && x + 12 < r.x + r.w - tw) {
- ngui_text_ellipsis(&ui,
- (struct ngui_rect){ x + 8, r.y,
- r.x + r.w - x - 16, r.h },
- x + 8, r.y + (r.h - 16) / 2, label,
- ui.theme.text_dim);
- }
- }
- }
- static void draw_status(struct ngui_rect r)
- {
- char left[128];
- char right[96];
- unsigned int n = 0, m = 0;
- int rw, rx;
- ngui_fill_rect(&ui, r, ui.theme.panel);
- ngui_fill_rect(&ui, (struct ngui_rect){ r.x, r.y, r.w, 1 },
- ui.theme.border);
- if (status_text[0] && status_until &&
- (int)(u_ticks_ms() - status_until) < 0) {
- s_append(left, &n, status_text, sizeof(left));
- } else {
- const char *name = doc_path[0] ? base_name(doc_path) :
- "Untitled";
- s_append(left, &n, "Ln ", sizeof(left));
- s_append_uint(left, &n, area.line, sizeof(left));
- s_append(left, &n, ", Col ", sizeof(left));
- s_append_uint(left, &n, area.column, sizeof(left));
- s_append(left, &n, " ", sizeof(left));
- s_append_uint(left, &n, area.len, sizeof(left));
- s_append(left, &n, " bytes ", sizeof(left));
- s_append(left, &n, name, sizeof(left));
- if (dirty)
- s_append(left, &n, " (modified)", sizeof(left));
- }
- /* Right side: the save/open shortcuts. */
- s_append(right, &m, "Ctrl+S save Ctrl+O open", sizeof(right));
- rw = (int)ngui_text_width_ui(&ui, right);
- rx = r.x + r.w - rw - 10;
- if (rx > r.x + 20)
- ngui_text_ellipsis(&ui,
- (struct ngui_rect){ r.x + 10, r.y, rx - r.x - 16, r.h },
- r.x + 10, r.y + (r.h - 16) / 2, left,
- status_error ? ui.theme.error : ui.theme.text);
- if (rx > r.x + 20)
- ngui_text(&ui, rx, r.y + (r.h - 16) / 2, right,
- ui.theme.text_dim);
- }
- static void draw_confirm_modal(void)
- {
- int mw, mh, mx, my;
- struct ngui_rect panel;
- ngui_set_cursor(&ui, CURSOR_DEFAULT);
- mw = ui.w - 40;
- if (mw > 440)
- mw = 440;
- if (mw < 240)
- mw = 240;
- mh = 132;
- mx = (ui.w - mw) / 2;
- my = (ui.h - mh) / 2;
- panel = (struct ngui_rect){ mx, my, mw, mh };
- ngui_fill_rect(&ui, (struct ngui_rect){ 0, 0, ui.w, ui.h },
- NGUI_RGB(8, 10, 16));
- ngui_fill_round_rect(&ui, panel, NGUI_RADIUS, NGUI_RADIUS,
- NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
- ngui_frame_round(&ui, panel, NGUI_RADIUS, NGUI_RADIUS,
- NGUI_RADIUS, NGUI_RADIUS, 1, ui.theme.border);
- ngui_text(&ui, mx + 18, my + 18, "Discard unsaved changes?",
- ui.theme.text);
- ngui_text_ellipsis(&ui,
- (struct ngui_rect){ mx + 18, my + 42, mw - 36, 18 },
- mx + 18, my + 42,
- doc_path[0] ? base_name(doc_path) : "Untitled",
- ui.theme.text_dim);
- if (ngui_button(&ui, ID_CONFIRM_CANCEL,
- (struct ngui_rect){ mx + mw - 188, my + mh - 44,
- 84, 28 }, "Cancel"))
- close_modal();
- if (ngui_button(&ui, ID_CONFIRM_DISCARD,
- (struct ngui_rect){ mx + mw - 96, my + mh - 44,
- 78, 28 }, "Discard"))
- discard_and_run();
- }
- static void draw(void)
- {
- struct ngui_rect all = { 0, 0, ui.w, ui.h };
- struct ngui_rect toolbar, status, edit;
- int res;
- if (file_dlg.active) {
- handle_file_dialog_result(ngui_file_dialog(&ui, &file_dlg));
- return;
- }
- ngui_fill_rect(&ui, all, ui.theme.bg);
- toolbar = ngui_cut_top(&all, TE_TOOLBAR_H);
- status = ngui_cut_bottom(&all, TE_STATUS_H);
- ngui_inset(&all, TE_MARGIN);
- edit = all;
- draw_toolbar(toolbar);
- /* A toolbar action may have opened the file dialog. */
- if (file_dlg.active) {
- handle_file_dialog_result(ngui_file_dialog(&ui, &file_dlg));
- return;
- }
- if (modal == MODAL_NONE) {
- if (edit.w < 1)
- edit.w = 1;
- if (edit.h < 1)
- edit.h = 1;
- area.scroll.hide_bar = 0;
- res = ngui_text_area(&ui, ID_AREA, edit, &area);
- if (res & NGUI_TEXTAREA_CHANGED)
- dirty = 1;
- } else {
- res = 0;
- }
- draw_status(status);
- if (res & NGUI_TEXTAREA_CANCEL)
- request_action(ACT_QUIT);
- if (modal != MODAL_NONE)
- draw_confirm_modal();
- }
- static void handle_keys(void)
- {
- int k;
- if (file_dlg.active) {
- /* The dialog has already handled its keys; discard any app
- * shortcut it deliberately left alone. */
- while (ngui_key(&ui) != 0)
- ;
- return;
- }
- while ((k = ngui_key(&ui)) != 0) {
- if (modal == MODAL_CONFIRM) {
- if (k == NGUI_KEY_ESC)
- close_modal();
- else if (k == '\r' || k == '\n')
- discard_and_run();
- continue;
- }
- switch (k) {
- case NGUI_KEY_ESC:
- request_action(ACT_QUIT);
- break;
- case 0x0E: /* Ctrl+N */
- request_action(ACT_NEW);
- break;
- case 0x0F: /* Ctrl+O */
- request_action(ACT_OPEN);
- break;
- case 0x13: /* Ctrl+S */
- do_save();
- break;
- case 0x11: /* Ctrl+Q */
- case 0x17: /* Ctrl+W */
- request_action(ACT_QUIT);
- break;
- default:
- break;
- }
- }
- }
- void _start(const char *arg)
- {
- struct win_info wi;
- if (u_win_create(820, 620, "Textedit", &wi) != 0) {
- u_puts("textedit: no graphics window\n");
- u_exit(1);
- }
- ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
- ngui_font_attach(&ui);
- area.text = text;
- area.cap = TE_MAX + 1;
- area.len = 0;
- area.cursor = 0;
- area.anchor = 0;
- area.dragging = 0;
- area.read_only = 0;
- area.line = 1;
- area.column = 1;
- text[0] = '\0';
- doc_path[0] = '\0';
- dirty = 0;
- modal = MODAL_NONE;
- pending_action = ACT_NONE;
- file_dlg.active = 0;
- if (arg && arg[0]) {
- const char *err = load_file(arg, 1);
- if (err)
- set_status(err, 1);
- } else {
- set_status("New document", 0);
- }
- ngui_set_focus(&ui, ID_AREA);
- ngui_invalidate(&ui);
- for (;;) {
- int want;
- if (status_until &&
- (int)(u_ticks_ms() - status_until) >= 0) {
- status_text[0] = '\0';
- status_until = 0;
- status_error = 0;
- ngui_invalidate(&ui);
- }
- want = ngui_begin(&ui);
- if (quit)
- break;
- if (!want) {
- ngui_end_ex(&ui, 0);
- u_sleep_ms(10);
- continue;
- }
- draw();
- handle_keys();
- if (quit)
- break;
- ngui_end(&ui);
- u_sleep_ms(10);
- }
- u_win_close();
- u_exit(0);
- }
|