/* 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 a small in-window path dialog. 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_DLG_PATH = 0x20, ID_DLG_OK, ID_DLG_CANCEL, ID_CONFIRM_DISCARD, ID_CONFIRM_CANCEL }; enum { MODAL_NONE, MODAL_OPEN, MODAL_SAVEAS, MODAL_CONFIRM }; enum { ACT_NONE, ACT_NEW, ACT_OPEN, ACT_QUIT }; static struct ngui ui; static struct ngui_text_area area; static struct ngui_input path_input; 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 modal_error[96]; 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; modal_error[0] = '\0'; ngui_set_focus(&ui, ID_AREA); ngui_invalidate(&ui); } static void open_dialog(int which) { unsigned int n, a; modal = which; modal_error[0] = '\0'; if (doc_path[0]) ngui_input_set(&path_input, doc_path); else ngui_input_set(&path_input, "/share/UNTITLED.TXT"); /* Preselect the file name after the last slash, so typing a name * replaces it while the directory stays. */ n = s_len(path_input.text); a = n; while (a > 0 && path_input.text[a - 1] != '/') a--; path_input.anchor = a; path_input.cursor = n; ngui_set_focus(&ui, ID_DLG_PATH); 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_dialog(MODAL_SAVEAS); } } static void run_action(int action) { switch (action) { case ACT_NEW: new_document(); break; case ACT_OPEN: open_dialog(MODAL_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; modal_error[0] = '\0'; ngui_set_focus(&ui, 0); ngui_invalidate(&ui); return; } run_action(action); } static void modal_ok(void) { const char *err; if (modal == MODAL_OPEN) { err = load_file(path_input.text, 0); if (err) { s_copy(modal_error, err, sizeof(modal_error)); ngui_set_focus(&ui, ID_DLG_PATH); ngui_invalidate(&ui); return; } close_modal(); } else if (modal == MODAL_SAVEAS) { err = save_file(path_input.text); if (err) { s_copy(modal_error, err, sizeof(modal_error)); ngui_set_focus(&ui, ID_DLG_PATH); ngui_invalidate(&ui); return; } close_modal(); } } static void discard_and_run(void) { int action = pending_action; pending_action = ACT_NONE; modal = MODAL_NONE; modal_error[0] = '\0'; dirty = 0; run_action(action); if (modal == MODAL_NONE && !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_dialog(MODAL_OPEN); break; case 2: do_save(); break; case 3: open_dialog(MODAL_SAVEAS); 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_modal(void) { int mw, mh, mx, my; struct ngui_rect panel, input_r; int ok; ngui_set_cursor(&ui, CURSOR_DEFAULT); if (modal == MODAL_CONFIRM) { 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(); return; } mw = ui.w - 60; if (mw > 560) mw = 560; if (mw < 280) mw = 280; mh = 178; mx = (ui.w - mw) / 2; my = (ui.h - mh) / 2; if (mx < 0) mx = 0; if (my < 0) my = 0; 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 + 16, modal == MODAL_OPEN ? "Open file" : "Save as", ui.theme.text); ngui_text(&ui, mx + 18, my + 44, "Path", ui.theme.text_dim); input_r = (struct ngui_rect){ mx + 18, my + 62, mw - 36, 28 }; ok = ngui_text_input(&ui, ID_DLG_PATH, input_r, &path_input); if (ok & NGUI_INPUT_SUBMIT) modal_ok(); else if ((ok & NGUI_INPUT_CANCEL) && !(ok & NGUI_INPUT_NEXT)) close_modal(); if (modal_error[0]) ngui_text_ellipsis(&ui, (struct ngui_rect){ mx + 18, my + 96, mw - 36, 18 }, mx + 18, my + 96, modal_error, ui.theme.error); if (ngui_button(&ui, ID_DLG_CANCEL, (struct ngui_rect){ mx + mw - 188, my + mh - 44, 84, 28 }, "Cancel")) close_modal(); if (ngui_button(&ui, ID_DLG_OK, (struct ngui_rect){ mx + mw - 96, my + mh - 44, 78, 28 }, modal == MODAL_OPEN ? "Open" : "Save")) modal_ok(); } static void draw(void) { struct ngui_rect all = { 0, 0, ui.w, ui.h }; struct ngui_rect toolbar, status, edit; int res; 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); 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_modal(); } static void handle_keys(void) { int k; 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; } if (modal == MODAL_OPEN || modal == MODAL_SAVEAS) { if (k == NGUI_KEY_ESC && ngui_focus(&ui) != ID_DLG_PATH) close_modal(); else if ((k == '\r' || k == '\n') && ngui_focus(&ui) != ID_DLG_PATH) modal_ok(); 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; path_input.placeholder = "/share/FILE.TXT"; 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); }