fix: fix Word Processor issues

This commit is contained in:
daniel committed 2026-11-11 07:04:09 +01:00
1 parent 51a2b3a932
commit 352c1307fe
7 files changed
+672 -460

No files matched your search

+1 -1
View File
@@ -12,4 +12,4 @@
#pragma once #pragma once
#define MONTAUK_BUILD_NUMBER 123 #define MONTAUK_BUILD_NUMBER 125
+395 -137
View File
@@ -44,6 +44,10 @@ static bool wp_same_style(const StyledRun* a, uint8_t font_id, uint8_t size, uin
return a->font_id == font_id && a->size == size && a->flags == flags; return a->font_id == font_id && a->size == size && a->flags == flags;
} }
void wp_set_status(WordProcessorState* wp, const char* msg) {
montauk::strncpy(wp->status_msg, msg ? msg : "", (int)sizeof(wp->status_msg));
}
void wp_init_paragraph_style(ParagraphStyle* para) { void wp_init_paragraph_style(ParagraphStyle* para) {
para->align = PARA_ALIGN_LEFT; para->align = PARA_ALIGN_LEFT;
para->list_type = PARA_LIST_NONE; para->list_type = PARA_LIST_NONE;
@@ -102,6 +106,23 @@ static void wp_paragraph_bounds(WordProcessorState* wp, int para_idx,
if (out_has_newline) *out_has_newline = has_newline; if (out_has_newline) *out_has_newline = has_newline;
} }
// Run and offset of the character at abs_pos. wp_pos_to_run answers "where is
// the cursor" and gives the end of the earlier run at a boundary; the
// character there belongs to the run that follows.
static void wp_char_to_run(WordProcessorState* wp, int abs_pos, int* out_run, int* out_offset) {
int pos = 0;
for (int i = 0; i < wp->run_count; i++) {
if (abs_pos < pos + wp->runs[i].len) {
*out_run = i;
*out_offset = abs_pos < pos ? 0 : abs_pos - pos;
return;
}
pos += wp->runs[i].len;
}
*out_run = wp->run_count > 0 ? wp->run_count - 1 : 0;
*out_offset = wp->run_count > 0 ? wp->runs[*out_run].len : 0;
}
static void wp_font_at(WordProcessorState* wp, int abs_pos, int layout_dpi, static void wp_font_at(WordProcessorState* wp, int abs_pos, int layout_dpi,
TrueTypeFont** out_font, GlyphCache** out_gc) { TrueTypeFont** out_font, GlyphCache** out_gc) {
if (wp->run_count <= 0) { if (wp->run_count <= 0) {
@@ -113,9 +134,7 @@ static void wp_font_at(WordProcessorState* wp, int abs_pos, int layout_dpi,
} }
int r, o; int r, o;
wp_pos_to_run(wp, abs_pos, &r, &o); wp_char_to_run(wp, abs_pos, &r, &o);
if (r >= wp->run_count) r = wp->run_count - 1;
if (r < 0) r = 0;
StyledRun* run = &wp->runs[r]; StyledRun* run = &wp->runs[r];
*out_font = wp_get_font(run->font_id, run->flags); *out_font = wp_get_font(run->font_id, run->flags);
*out_gc = (*out_font && (*out_font)->valid) *out_gc = (*out_font && (*out_font)->valid)
@@ -225,12 +244,6 @@ static void wp_on_insert_newline(WordProcessorState* wp, int abs_pos) {
wp_insert_paragraph_style(wp, para + 1, &style); wp_insert_paragraph_style(wp, para + 1, &style);
} }
static void wp_on_delete_newline(WordProcessorState* wp, int abs_pos) {
int para = wp_find_paragraph_at(wp, abs_pos);
if (para + 1 < wp->paragraph_count)
wp_remove_paragraph_style(wp, para + 1);
}
static void wp_default_line_metrics(WordProcessorState* wp, int abs_pos, int layout_dpi, static void wp_default_line_metrics(WordProcessorState* wp, int abs_pos, int layout_dpi,
int* out_height, int* out_ascent) { int* out_height, int* out_ascent) {
TrueTypeFont* font = nullptr; TrueTypeFont* font = nullptr;
@@ -757,6 +770,9 @@ void wp_init_empty_document(WordProcessorState* wp) {
wp->wrap_dirty = true; wp->wrap_dirty = true;
wp->last_wrap_width = 0; wp->last_wrap_width = 0;
wp->last_wrap_dpi = 0; wp->last_wrap_dpi = 0;
wp->last_wrap_paged = false;
wp->page_count = 0;
wp->scroll_to_cursor = true;
wp->paragraph_count = 1; wp->paragraph_count = 1;
wp_init_paragraph_style(&wp->paragraphs[0]); wp_init_paragraph_style(&wp->paragraphs[0]);
@@ -786,6 +802,8 @@ void wp_init_empty_document(WordProcessorState* wp) {
wp->undo_count = 0; wp->undo_count = 0;
wp->undo_pos = 0; wp->undo_pos = 0;
wp->undo_typing = false;
wp->undo_typing_word = false;
montauk::memset(wp->undo, 0, sizeof(wp->undo)); montauk::memset(wp->undo, 0, sizeof(wp->undo));
wp_history_reset(wp); wp_history_reset(wp);
@@ -834,7 +852,7 @@ void wp_pos_to_run(WordProcessorState* wp, int abs_pos, int* out_run, int* out_o
char wp_char_at(WordProcessorState* wp, int abs_pos) { char wp_char_at(WordProcessorState* wp, int abs_pos) {
int r, o; int r, o;
wp_pos_to_run(wp, abs_pos, &r, &o); wp_char_to_run(wp, abs_pos, &r, &o);
if (r < wp->run_count && o < wp->runs[r].len) if (r < wp->run_count && o < wp->runs[r].len)
return wp->runs[r].text[o]; return wp->runs[r].text[o];
return '\0'; return '\0';
@@ -958,61 +976,50 @@ void wp_insert_char(WordProcessorState* wp, char c) {
if (c == '\n') wp_on_insert_newline(wp, insert_abs); if (c == '\n') wp_on_insert_newline(wp, insert_abs);
} }
void wp_backspace(WordProcessorState* wp) { void wp_delete_range(WordProcessorState* wp, int start, int end) {
if (wp->cursor_run == 0 && wp->cursor_offset == 0) return; if (start < 0) start = 0;
if (end > wp->total_text_len) end = wp->total_text_len;
if (start >= end) return;
int removed_abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset) - 1; int newlines = wp_count_newlines_in_range(wp, start, end);
char removed = '\0'; if (newlines > 0) {
if (removed_abs >= 0) // The paragraph at `start` absorbs the ones joined onto it and keeps
removed = wp_char_at(wp, removed_abs); // its style, unless it is a divider: text never inherits a rule.
int para = wp_find_paragraph_at(wp, start);
if (wp->cursor_offset > 0) { int first = wp->paragraphs[para].divider_type != PARA_DIVIDER_NONE ? para : para + 1;
StyledRun* r = &wp->runs[wp->cursor_run]; for (int i = 0; i < newlines && first < wp->paragraph_count; i++)
for (int i = wp->cursor_offset - 1; i < r->len - 1; i++) wp_remove_paragraph_style(wp, first);
r->text[i] = r->text[i + 1];
r->len--;
wp->cursor_offset--;
} else {
wp->cursor_run--;
StyledRun* r = &wp->runs[wp->cursor_run];
if (r->len > 0) r->len--;
wp->cursor_offset = r->len;
} }
wp->total_text_len--; int pos = 0;
for (int i = 0; i < wp->run_count && pos < end; i++) {
StyledRun* r = &wp->runs[i];
int run_start = pos;
int run_end = pos + r->len;
pos = run_end;
int s = start > run_start ? start : run_start;
int e = end < run_end ? end : run_end;
if (s >= e) continue;
montauk::memmove(r->text + (s - run_start), r->text + (e - run_start), run_end - e);
r->len -= e - s;
}
wp->total_text_len -= end - start;
wp->modified = true; wp->modified = true;
wp->wrap_dirty = true; wp->wrap_dirty = true;
if (removed == '\n') wp_on_delete_newline(wp, removed_abs); wp_pos_to_run(wp, start, &wp->cursor_run, &wp->cursor_offset);
wp_merge_adjacent(wp); wp_merge_adjacent(wp);
wp_sync_paragraph_count(wp); wp_sync_paragraph_count(wp);
} }
void wp_backspace(WordProcessorState* wp) {
int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset);
wp_delete_range(wp, abs - 1, abs);
}
void wp_delete_char(WordProcessorState* wp) { void wp_delete_char(WordProcessorState* wp) {
int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset); int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset);
if (abs >= wp->total_text_len) return; wp_delete_range(wp, abs, abs + 1);
char removed = wp_char_at(wp, abs);
StyledRun* r = &wp->runs[wp->cursor_run];
if (wp->cursor_offset < r->len) {
for (int i = wp->cursor_offset; i < r->len - 1; i++)
r->text[i] = r->text[i + 1];
r->len--;
} else if (wp->cursor_run + 1 < wp->run_count) {
StyledRun* next = &wp->runs[wp->cursor_run + 1];
if (next->len > 0) {
for (int i = 0; i < next->len - 1; i++)
next->text[i] = next->text[i + 1];
next->len--;
}
}
wp->total_text_len--;
wp->modified = true;
wp->wrap_dirty = true;
if (removed == '\n') wp_on_delete_newline(wp, abs);
wp_merge_adjacent(wp);
wp_sync_paragraph_count(wp);
} }
void wp_cursor_left(WordProcessorState* wp) { void wp_cursor_left(WordProcessorState* wp) {
@@ -1069,6 +1076,14 @@ void wp_apply_style_to_selection(WordProcessorState* wp, int mode, int value) {
wp_sel_range(wp, &sel_s, &sel_e); wp_sel_range(wp, &sel_s, &sel_e);
if (sel_s >= sel_e) return; if (sel_s >= sel_e) return;
// Splitting both ends takes up to two more runs; without them the style
// would spill onto text outside the selection.
wp_merge_adjacent(wp);
if (wp->run_count + 2 > WP_MAX_RUNS) {
wp_set_status(wp, "Document has too many style changes");
return;
}
int end_ri = wp_split_at(wp, sel_e); int end_ri = wp_split_at(wp, sel_e);
int start_ri = wp_split_at(wp, sel_s); int start_ri = wp_split_at(wp, sel_s);
@@ -1084,16 +1099,35 @@ void wp_apply_style_to_selection(WordProcessorState* wp, int mode, int value) {
} }
} }
// Bold/italic clear only when the whole selection already has them.
uint8_t flag = mode == 2 ? STYLE_BOLD : mode == 3 ? STYLE_ITALIC : 0;
bool set_flag = false;
for (int i = start_ri; flag && i < end_ri && i < wp->run_count; i++) {
if (wp->runs[i].len > 0 && !(wp->runs[i].flags & flag)) {
set_flag = true;
break;
}
}
for (int i = start_ri; i < end_ri && i < wp->run_count; i++) { for (int i = start_ri; i < end_ri && i < wp->run_count; i++) {
StyledRun* r = &wp->runs[i]; StyledRun* r = &wp->runs[i];
switch (mode) { switch (mode) {
case 0: r->font_id = (uint8_t)value; break; case 0: r->font_id = (uint8_t)value; break;
case 1: r->size = (uint8_t)value; break; case 1: r->size = (uint8_t)value; break;
case 2: r->flags ^= STYLE_BOLD; break; default:
case 3: r->flags ^= STYLE_ITALIC; break; r->flags = set_flag ? (r->flags | flag) : (r->flags & ~flag);
break;
} }
} }
switch (mode) {
case 0: wp->cur_font_id = (uint8_t)value; break;
case 1: wp->cur_size = (uint8_t)value; break;
default:
wp->cur_flags = set_flag ? (wp->cur_flags | flag) : (wp->cur_flags & ~flag);
break;
}
wp->wrap_dirty = true; wp->wrap_dirty = true;
wp->modified = true; wp->modified = true;
wp_merge_adjacent(wp); wp_merge_adjacent(wp);
@@ -1371,10 +1405,7 @@ void wp_delete_selection(WordProcessorState* wp) {
return; return;
} }
wp_pos_to_run(wp, sel_s, &wp->cursor_run, &wp->cursor_offset); wp_delete_range(wp, sel_s, sel_e);
for (int i = 0; i < count; i++)
wp_delete_char(wp);
wp_clear_selection(wp); wp_clear_selection(wp);
} }
@@ -1413,19 +1444,110 @@ bool wp_insert_text(WordProcessorState* wp, const char* text, int len) {
} }
if (wp->run_count + extra_runs > WP_MAX_RUNS) return false; if (wp->run_count + extra_runs > WP_MAX_RUNS) return false;
for (int i = 0; i < len; i++) // The first character goes through wp_insert_char, which sets up a run in
wp_insert_char(wp, text[i]); // the current style; the rest is spliced into that run in one move.
wp_insert_char(wp, text[0]);
const char* rest = text + 1;
int rest_len = len - 1;
if (rest_len == 0) return true;
StyledRun* r = &wp->runs[wp->cursor_run];
if (!wp_same_style(r, wp->cur_font_id, wp->cur_size, wp->cur_flags)) {
for (int i = 0; i < rest_len; i++)
wp_insert_char(wp, rest[i]);
return true;
}
int para = wp_find_paragraph_at(wp, wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset));
int off = wp->cursor_offset;
wp_ensure_run_cap(r, rest_len);
montauk::memmove(r->text + off + rest_len, r->text + off, r->len - off);
montauk::memcpy(r->text + off, rest, rest_len);
r->len += rest_len;
wp->cursor_offset += rest_len;
wp->total_text_len += rest_len;
ParagraphStyle style = wp->paragraphs[para];
style.divider_type = PARA_DIVIDER_NONE;
for (int i = wp_count_newline_chars(rest, rest_len); i > 0; i--)
wp_insert_paragraph_style(wp, para + 1, &style);
wp->modified = true;
wp->wrap_dirty = true;
return true; return true;
} }
void wp_recompute_wrap(WordProcessorState* wp, int content_w, int layout_dpi) { // Assigns each wrap line a page and a y on that page, keeping the gaps of the
// continuous layout. Returns the page count.
int wp_paginate(WordProcessorState* wp, int page_h, int margin_y, int* line_pages, int* line_y) {
if (wp->wrap_line_count <= 0) return 0;
int base_y = wp->wrap_lines[0].y;
int printable_bottom = page_h - margin_y;
int current_page = 0;
int current_y = margin_y;
int prev_rel_bottom = 0;
for (int i = 0; i < wp->wrap_line_count; i++) {
WrapLine* wl = &wp->wrap_lines[i];
int rel_y = wl->y - base_y;
if (rel_y < 0) rel_y = 0;
int gap_before = (i == 0) ? 0 : (rel_y - prev_rel_bottom);
if (gap_before < 0) gap_before = 0;
int line_top = current_y + gap_before;
if (line_top + wl->height > printable_bottom && current_y > margin_y) {
current_page++;
current_y = margin_y;
line_top = current_y;
}
line_pages[i] = current_page;
line_y[i] = line_top;
current_y = line_top + wl->height;
prev_rel_bottom = rel_y + wl->height;
}
return current_page + 1;
}
// Moves the continuous layout onto a column of pages. Line coordinates stay
// relative to the top-left of the edit area's content, as in the plain view.
static void wp_stack_lines_on_pages(WordProcessorState* wp) {
int n = wp->wrap_line_count;
int* pages = (int*)montauk::malloc((uint64_t)n * sizeof(int));
int* ys = (int*)montauk::malloc((uint64_t)n * sizeof(int));
if (pages && ys) {
wp->page_count = wp_paginate(wp, WP_PAGE_H, WP_PAGE_MARGIN_Y, pages, ys);
for (int i = 0; i < n; i++) {
WrapLine* wl = &wp->wrap_lines[i];
wl->y = WP_PAGE_TOP_MARGIN + pages[i] * (WP_PAGE_H + WP_PAGE_GAP) + ys[i];
wl->x += WP_PAGE_MARGIN_X - WP_MARGIN;
}
wp->content_height = WP_PAGE_TOP_MARGIN + wp->page_count * (WP_PAGE_H + WP_PAGE_GAP);
}
if (pages) montauk::mfree(pages);
if (ys) montauk::mfree(ys);
}
void wp_layout_screen(WordProcessorState* wp) {
if (wp->page_view_mode)
wp_recompute_wrap(wp, WP_PAGE_CONTENT_W + WP_MARGIN * 2 + WP_SCROLLBAR_W, WP_SCREEN_DPI, true);
else
wp_recompute_wrap(wp, g_win_w, WP_SCREEN_DPI, false);
}
void wp_recompute_wrap(WordProcessorState* wp, int content_w, int layout_dpi, bool paged) {
int wrap_width = content_w - WP_MARGIN * 2 - WP_SCROLLBAR_W; int wrap_width = content_w - WP_MARGIN * 2 - WP_SCROLLBAR_W;
if (wrap_width < 50) wrap_width = 50; if (wrap_width < 50) wrap_width = 50;
if (wrap_width != wp->last_wrap_width || layout_dpi != wp->last_wrap_dpi) { if (wrap_width != wp->last_wrap_width || layout_dpi != wp->last_wrap_dpi ||
paged != wp->last_wrap_paged) {
wp->wrap_dirty = true; wp->wrap_dirty = true;
wp->last_wrap_width = wrap_width; wp->last_wrap_width = wrap_width;
wp->last_wrap_dpi = layout_dpi; wp->last_wrap_dpi = layout_dpi;
wp->last_wrap_paged = paged;
} }
if (!wp->wrap_dirty && wp->wrap_lines) return; if (!wp->wrap_dirty && wp->wrap_lines) return;
@@ -1511,7 +1633,7 @@ void wp_recompute_wrap(WordProcessorState* wp, int content_w, int layout_dpi) {
&line_width, &line_height, &line_ascent); &line_width, &line_height, &line_ascent);
WrapLine* line = &wp->wrap_lines[wp->wrap_line_count]; WrapLine* line = &wp->wrap_lines[wp->wrap_line_count];
wp_pos_to_run(wp, line_start, &line->run_idx, &line->run_offset); wp_char_to_run(wp, line_start, &line->run_idx, &line->run_offset);
line->char_count = char_count; line->char_count = char_count;
line->y = y; line->y = y;
line->height = line_height; line->height = line_height;
@@ -1542,7 +1664,7 @@ void wp_recompute_wrap(WordProcessorState* wp, int content_w, int layout_dpi) {
if (para_start == para_content_end && wp->wrap_line_count < WP_MAX_WRAP_LINES) { if (para_start == para_content_end && wp->wrap_line_count < WP_MAX_WRAP_LINES) {
if (!wp_ensure_wrap_capacity(wp, wp->wrap_line_count + 1)) break; if (!wp_ensure_wrap_capacity(wp, wp->wrap_line_count + 1)) break;
WrapLine* line = &wp->wrap_lines[wp->wrap_line_count]; WrapLine* line = &wp->wrap_lines[wp->wrap_line_count];
wp_pos_to_run(wp, para_start, &line->run_idx, &line->run_offset); wp_char_to_run(wp, para_start, &line->run_idx, &line->run_offset);
line->char_count = has_newline ? 1 : 0; line->char_count = has_newline ? 1 : 0;
line->y = y; line->y = y;
line->paragraph_idx = para_idx; line->paragraph_idx = para_idx;
@@ -1605,6 +1727,8 @@ void wp_recompute_wrap(WordProcessorState* wp, int content_w, int layout_dpi) {
} }
wp->content_height = y + WP_MARGIN; wp->content_height = y + WP_MARGIN;
wp->page_count = 0;
if (paged) wp_stack_lines_on_pages(wp);
wp->wrap_dirty = false; wp->wrap_dirty = false;
} }
@@ -1626,36 +1750,63 @@ int wp_wrap_line_start(WordProcessorState* wp, int line_idx) {
return pos; return pos;
} }
static int wp_line_x_of(WordProcessorState* wp, int line, int abs_pos) {
int x = wp->wrap_lines[line].x;
for (int p = wp_wrap_line_start(wp, line); p < abs_pos; p++)
x += wp_char_advance_at(wp, p, wp->last_wrap_dpi, wp_char_at(wp, p));
return x;
}
// Puts the cursor on `line` at the character boundary nearest target_x.
static void wp_cursor_to_line_x(WordProcessorState* wp, int line, int target_x) {
WrapLine* wl = &wp->wrap_lines[line];
int start = wp_wrap_line_start(wp, line);
int count = wl->char_count;
if (count > 0 && wp_char_at(wp, start + count - 1) == '\n')
count--;
else if (count > 0 && line < wp->wrap_line_count - 1)
count--; // past the last character of a soft-wrapped line is the next line
int pos = start;
int x = wl->x;
for (int i = 0; i < count; i++) {
int adv = wp_char_advance_at(wp, pos, wp->last_wrap_dpi, wp_char_at(wp, pos));
if (x + adv / 2 > target_x) break;
x += adv;
pos++;
}
wp_pos_to_run(wp, pos, &wp->cursor_run, &wp->cursor_offset);
}
void wp_cursor_up(WordProcessorState* wp) { void wp_cursor_up(WordProcessorState* wp) {
int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset); int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset);
int line = wp_find_wrap_line(wp, abs); int line = wp_find_wrap_line(wp, abs);
if (line <= 0) return; if (line <= 0) return;
wp_cursor_to_line_x(wp, line - 1, wp_line_x_of(wp, line, abs));
int col = abs - wp_wrap_line_start(wp, line);
int prev_start = wp_wrap_line_start(wp, line - 1);
int prev_len = wp->wrap_lines[line - 1].char_count;
int new_col = col < prev_len ? col : prev_len;
if (new_col > 0 && new_col == prev_len) {
char ch = wp_char_at(wp, prev_start + prev_len - 1);
if (ch == '\n') new_col = prev_len - 1;
}
wp_pos_to_run(wp, prev_start + new_col, &wp->cursor_run, &wp->cursor_offset);
} }
void wp_cursor_down(WordProcessorState* wp) { void wp_cursor_down(WordProcessorState* wp) {
int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset); int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset);
int line = wp_find_wrap_line(wp, abs); int line = wp_find_wrap_line(wp, abs);
if (line >= wp->wrap_line_count - 1) return; if (line >= wp->wrap_line_count - 1) return;
wp_cursor_to_line_x(wp, line + 1, wp_line_x_of(wp, line, abs));
}
int col = abs - wp_wrap_line_start(wp, line); // Moves the cursor dy pixels up or down the layout (Page Up / Page Down).
int next_start = wp_wrap_line_start(wp, line + 1); void wp_cursor_page(WordProcessorState* wp, int dy) {
int next_len = wp->wrap_lines[line + 1].char_count; if (wp->wrap_line_count <= 0) return;
int new_col = col < next_len ? col : next_len; int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset);
if (new_col > 0 && new_col == next_len && line + 1 < wp->wrap_line_count - 1) { int line = wp_find_wrap_line(wp, abs);
char ch = wp_char_at(wp, next_start + next_len - 1); int target_y = wp->wrap_lines[line].y + dy;
if (ch == '\n') new_col = next_len - 1;
int target = line;
if (dy < 0) {
while (target > 0 && wp->wrap_lines[target].y > target_y) target--;
} else {
while (target < wp->wrap_line_count - 1 && wp->wrap_lines[target].y < target_y) target++;
} }
wp_pos_to_run(wp, next_start + new_col, &wp->cursor_run, &wp->cursor_offset); if (target != line)
wp_cursor_to_line_x(wp, target, wp_line_x_of(wp, line, abs));
} }
void wp_ensure_cursor_visible(WordProcessorState* wp, int view_h) { void wp_ensure_cursor_visible(WordProcessorState* wp, int view_h) {
@@ -1700,19 +1851,50 @@ void wp_start_pathbar(WordProcessorState* wp, bool save_mode, const char* initia
wp->pathbar_cursor = wp->pathbar_len; wp->pathbar_cursor = wp->pathbar_len;
} }
// Saves under `path`, adding the .mwp extension Files associates with this app
// when the name has none.
void wp_save_file_as(WordProcessorState* wp, const char* path) {
char full[256];
montauk::strncpy(full, path, (int)sizeof(full) - 4);
int len = montauk::slen(full);
bool has_ext = false;
for (int i = len - 1; i >= 0 && full[i] != '/'; i--) {
if (full[i] == '.') {
has_ext = true;
break;
}
}
if (!has_ext && len > 0 && full[len - 1] != '/')
montauk::strncpy(full + len, ".mwp", 5);
char old_path[256];
montauk::strncpy(old_path, wp->filepath, (int)sizeof(old_path));
wp_set_filepath(wp, full);
if (!wp_save_file(wp))
wp_set_filepath(wp, old_path); // still the document it was
}
void wp_open_save_pathbar(WordProcessorState* wp) { void wp_open_save_pathbar(WordProcessorState* wp) {
char path[256] = {}; char path[256] = {};
const char* initial_path = wp->filepath[0] ? wp->filepath : ""; const char* initial_path = wp->filepath[0] ? wp->filepath : "";
const char* suggested_name = wp->filename[0] ? wp->filename : "document.mwp"; const char* suggested_name = wp->filename[0] ? wp->filename : "document.mwp";
char msg[160] = {}; char msg[160] = {};
if (dialogs::save_file("Save Document", initial_path, suggested_name, path, sizeof(path), msg, sizeof(msg))) { if (dialogs::save_file("Save Document", initial_path, suggested_name, path, sizeof(path), msg, sizeof(msg))) {
wp_set_filepath(wp, path); wp_save_file_as(wp, path);
wp_save_file(wp);
} else if (msg[0]) { } else if (msg[0]) {
wp_start_pathbar(wp, true, initial_path[0] ? initial_path : suggested_name); wp_start_pathbar(wp, true, initial_path[0] ? initial_path : suggested_name);
} }
} }
// A run record stores its length as a u16, so longer runs are written as
// several records of the same style; loading joins them again.
static constexpr int WP_RUN_RECORD_MAX = 0xFFFF;
static int wp_run_record_count(const StyledRun* r) {
return r->len <= WP_RUN_RECORD_MAX ? 1 : (r->len + WP_RUN_RECORD_MAX - 1) / WP_RUN_RECORD_MAX;
}
static int wp_serialized_size(WordProcessorState* wp) { static int wp_serialized_size(WordProcessorState* wp) {
int size = 4 + 2 + 1 + 1 + 1 + 2 + 2; int size = 4 + 2 + 1 + 1 + 1 + 2 + 2;
// Embedded font-name table: u8 count, then per family u8 len + name bytes. // Embedded font-name table: u8 count, then per family u8 len + name bytes.
@@ -1720,7 +1902,7 @@ static int wp_serialized_size(WordProcessorState* wp) {
for (int i = 0; i < g_wp_fonts.count; i++) for (int i = 0; i < g_wp_fonts.count; i++)
size += 1 + montauk::slen(g_wp_fonts.families[i].name); size += 1 + montauk::slen(g_wp_fonts.families[i].name);
for (int i = 0; i < wp->run_count; i++) for (int i = 0; i < wp->run_count; i++)
size += 2 + 1 + 1 + 1 + 1 + wp->runs[i].len; size += wp_run_record_count(&wp->runs[i]) * 6 + wp->runs[i].len;
size += wp->paragraph_count * 12; size += wp->paragraph_count * 12;
return size; return size;
} }
@@ -1751,7 +1933,10 @@ static bool wp_serialize_document(WordProcessorState* wp, uint8_t** out_buf, int
buf[off++] = wp->cur_font_id; buf[off++] = wp->cur_font_id;
buf[off++] = wp->cur_size; buf[off++] = wp->cur_size;
buf[off++] = wp->cur_flags; buf[off++] = wp->cur_flags;
wp_write_u16(buf, &off, wp->run_count); int record_count = 0;
for (int i = 0; i < wp->run_count; i++)
record_count += wp_run_record_count(&wp->runs[i]);
wp_write_u16(buf, &off, record_count);
wp_write_u16(buf, &off, wp->paragraph_count); wp_write_u16(buf, &off, wp->paragraph_count);
// Font-name table: maps each runtime font id (used by cur_font_id and run // Font-name table: maps each runtime font id (used by cur_font_id and run
@@ -1769,13 +1954,18 @@ static bool wp_serialize_document(WordProcessorState* wp, uint8_t** out_buf, int
for (int i = 0; i < wp->run_count; i++) { for (int i = 0; i < wp->run_count; i++) {
StyledRun* r = &wp->runs[i]; StyledRun* r = &wp->runs[i];
wp_write_u16(buf, &off, r->len); int written = 0;
buf[off++] = r->font_id; do {
buf[off++] = r->size; int chunk = wp_min_int(r->len - written, WP_RUN_RECORD_MAX);
buf[off++] = r->flags; wp_write_u16(buf, &off, chunk);
buf[off++] = 0; buf[off++] = r->font_id;
montauk::memcpy(buf + off, r->text, r->len); buf[off++] = r->size;
off += r->len; buf[off++] = r->flags;
buf[off++] = 0;
montauk::memcpy(buf + off, r->text + written, chunk);
off += chunk;
written += chunk;
} while (written < r->len);
} }
for (int i = 0; i < wp->paragraph_count; i++) { for (int i = 0; i < wp->paragraph_count; i++) {
@@ -1836,7 +2026,8 @@ static bool wp_deserialize_document(WordProcessorState* wp, const uint8_t* buf,
header_off += 2; header_off += 2;
} }
if (run_count < 0 || run_count > WP_MAX_RUNS) return false; // Split records of long runs can push the record count past WP_MAX_RUNS.
if (run_count < 0 || run_count > WP_MAX_RUNS + WP_MAX_TEXT / WP_RUN_RECORD_MAX + 1) return false;
// Resolve stored font ids to current runtime ids via the embedded font-name // Resolve stored font ids to current runtime ids via the embedded font-name
// table (v3); legacy documents used fixed ids 0=Roboto, 1=NotoSerif, 2=C059. // table (v3); legacy documents used fixed ids 0=Roboto, 1=NotoSerif, 2=C059.
@@ -1873,11 +2064,19 @@ static bool wp_deserialize_document(WordProcessorState* wp, const uint8_t* buf,
int off = header_off; int off = header_off;
int actual_para_count = 1; int actual_para_count = 1;
int total_text = 0;
int merged_runs = 0;
int prev_style = -1;
for (int i = 0; i < run_count; i++) { for (int i = 0; i < run_count; i++) {
if (off + 6 > size) return false; if (off + 6 > size) return false;
int text_len = wp_read_u16(buf, &off); int text_len = wp_read_u16(buf, &off);
int style = (map_font(buf[off]) << 16) | (buf[off + 1] << 8) | buf[off + 2];
off += 4; off += 4;
if (text_len < 0 || off + text_len > size) return false; if (text_len < 0 || off + text_len > size) return false;
if (style != prev_style) merged_runs++;
prev_style = style;
total_text += text_len;
if (merged_runs > WP_MAX_RUNS || total_text > WP_MAX_TEXT - 1) return false;
int para_count = wp_count_newlines_in_buf(buf + off, text_len); int para_count = wp_count_newlines_in_buf(buf + off, text_len);
if (para_count > WP_MAX_PARAGRAPHS) return false; if (para_count > WP_MAX_PARAGRAPHS) return false;
actual_para_count += para_count - 1; actual_para_count += para_count - 1;
@@ -1904,12 +2103,14 @@ static bool wp_deserialize_document(WordProcessorState* wp, const uint8_t* buf,
uint8_t flags = buf[off++]; uint8_t flags = buf[off++];
off++; off++;
StyledRun* r = &wp->runs[wp->run_count]; StyledRun* r = wp->run_count > 0 ? &wp->runs[wp->run_count - 1] : nullptr;
wp_init_run(r, font_id, run_size, flags); if (!r || !wp_same_style(r, font_id, run_size, flags)) {
r = &wp->runs[wp->run_count++];
wp_init_run(r, font_id, run_size, flags);
}
wp_ensure_run_cap(r, text_len); wp_ensure_run_cap(r, text_len);
montauk::memcpy(r->text, buf + off, text_len); montauk::memcpy(r->text + r->len, buf + off, text_len);
r->len = text_len; r->len += text_len;
wp->run_count++;
wp->total_text_len += text_len; wp->total_text_len += text_len;
off += text_len; off += text_len;
} }
@@ -1965,10 +2166,9 @@ static bool wp_deserialize_document(WordProcessorState* wp, const uint8_t* buf,
wp->cursor_run = 0; wp->cursor_run = 0;
wp->cursor_offset = 0; wp->cursor_offset = 0;
wp->scrollbar.scroll_offset = 0; wp->scrollbar.scroll_offset = 0;
wp->scroll_to_cursor = true;
wp->modified = false; wp->modified = false;
wp->wrap_dirty = true; wp->wrap_dirty = true;
wp->last_wrap_width = 0;
wp->last_wrap_dpi = 0;
wp->sel_anchor = 0; wp->sel_anchor = 0;
wp->sel_end = 0; wp->sel_end = 0;
wp->has_selection = false; wp->has_selection = false;
@@ -1979,8 +2179,6 @@ static bool wp_deserialize_document(WordProcessorState* wp, const uint8_t* buf,
wp->divider_flyout_open = false; wp->divider_flyout_open = false;
wp->special_char_flyout_open = false; wp->special_char_flyout_open = false;
wp->show_pathbar = false; wp->show_pathbar = false;
wp->page_view_mode = false;
wp->pending_page_view_toggle = false;
return true; return true;
} }
@@ -2008,8 +2206,10 @@ static bool wp_snapshots_match(const UndoSnapshot* a, const UndoSnapshot* b) {
} }
static bool wp_restore_snapshot(WordProcessorState* wp, const UndoSnapshot* snap) { static bool wp_restore_snapshot(WordProcessorState* wp, const UndoSnapshot* snap) {
int scroll = wp->scrollbar.scroll_offset;
if (!wp_deserialize_document(wp, snap->data, snap->size)) if (!wp_deserialize_document(wp, snap->data, snap->size))
return false; return false;
wp->scrollbar.scroll_offset = scroll;
int cursor_abs = gui_clamp(snap->cursor_abs, 0, wp->total_text_len); int cursor_abs = gui_clamp(snap->cursor_abs, 0, wp->total_text_len);
wp_pos_to_run(wp, cursor_abs, &wp->cursor_run, &wp->cursor_offset); wp_pos_to_run(wp, cursor_abs, &wp->cursor_run, &wp->cursor_offset);
@@ -2017,7 +2217,6 @@ static bool wp_restore_snapshot(WordProcessorState* wp, const UndoSnapshot* snap
wp->sel_end = gui_clamp(snap->sel_end, 0, wp->total_text_len); wp->sel_end = gui_clamp(snap->sel_end, 0, wp->total_text_len);
wp->has_selection = snap->has_selection && (wp->sel_anchor != wp->sel_end); wp->has_selection = snap->has_selection && (wp->sel_anchor != wp->sel_end);
wp->modified = snap->modified; wp->modified = snap->modified;
wp->scrollbar.scroll_offset = 0;
return true; return true;
} }
@@ -2053,56 +2252,77 @@ bool wp_replace_selection_with_text(WordProcessorState* wp, const char* text, in
return ok; return ok;
} }
void wp_save_file(WordProcessorState* wp) { bool wp_save_file(WordProcessorState* wp) {
if (wp->filepath[0] == '\0') { if (wp->filepath[0] == '\0') {
wp_open_save_pathbar(wp); wp_open_save_pathbar(wp);
return; return !wp->modified;
} }
char msg[160];
uint8_t* buf = nullptr; uint8_t* buf = nullptr;
int off = 0; int size = 0;
if (!wp_serialize_document(wp, &buf, &off)) if (!wp_serialize_document(wp, &buf, &size)) {
return; wp_set_status(wp, "Out of memory while saving");
return false;
}
int fd = montauk::fcreate(wp->filepath); int fd = montauk::fcreate(wp->filepath);
if (fd >= 0) { if (fd < 0) {
montauk::fwrite(fd, buf, 0, off); montauk::mfree(buf);
montauk::close(fd); snprintf(msg, sizeof(msg), "Cannot create %s", wp->filepath);
wp_history_mark_saved(wp); wp_set_status(wp, msg);
return false;
} }
int wrote = montauk::fwrite(fd, buf, 0, size);
montauk::close(fd);
montauk::mfree(buf); montauk::mfree(buf);
if (wrote != size) {
snprintf(msg, sizeof(msg), "Failed to write %s", wp->filepath);
wp_set_status(wp, msg);
return false;
}
wp_history_mark_saved(wp);
snprintf(msg, sizeof(msg), "Saved %s", wp->filename);
wp_set_status(wp, msg);
return true;
} }
void wp_load_file(WordProcessorState* wp, const char* path) { void wp_load_file(WordProcessorState* wp, const char* path) {
char msg[160];
int fd = montauk::open(path); int fd = montauk::open(path);
if (fd < 0) return; if (fd < 0) {
snprintf(msg, sizeof(msg), "Cannot open %s", path);
wp_set_status(wp, msg);
return;
}
uint64_t fsize = montauk::getsize(fd); uint64_t fsize = montauk::getsize(fd);
if (fsize < 10 || fsize > WP_MAX_TEXT * 2) { uint8_t* buf = nullptr;
montauk::close(fd); bool ok = fsize >= 10 && fsize <= WP_MAX_TEXT * 2;
return; if (ok) {
buf = (uint8_t*)montauk::malloc(fsize);
ok = buf && montauk::read(fd, buf, 0, fsize) == (int)fsize;
} }
uint8_t* buf = (uint8_t*)montauk::malloc((int)fsize);
montauk::read(fd, buf, 0, fsize);
montauk::close(fd); montauk::close(fd);
if (buf[0] != 'M' || buf[1] != 'W' || buf[2] != 'P' || buf[3] != '1') { ok = ok && wp_deserialize_document(wp, buf, (int)fsize);
montauk::mfree(buf); if (buf) montauk::mfree(buf);
if (!ok) {
snprintf(msg, sizeof(msg), "%s is not a Word Processor document", path);
wp_set_status(wp, msg);
return; return;
} }
bool ok = wp_deserialize_document(wp, buf, (int)fsize);
montauk::mfree(buf);
if (!ok) return;
wp_set_filepath(wp, path); wp_set_filepath(wp, path);
wp_history_reset(wp); wp_history_reset(wp);
wp_set_status(wp, "");
} }
void wp_history_reset(WordProcessorState* wp) { void wp_history_reset(WordProcessorState* wp) {
wp_clear_history(wp); wp_clear_history(wp);
wp->undo_typing = false;
UndoSnapshot snap = {}; UndoSnapshot snap = {};
if (!wp_capture_snapshot(wp, &snap)) if (!wp_capture_snapshot(wp, &snap))
@@ -2113,7 +2333,18 @@ void wp_history_reset(WordProcessorState* wp) {
wp->undo_pos = 0; wp->undo_pos = 0;
} }
static void wp_history_drop_oldest(WordProcessorState* wp) {
wp_free_snapshot(&wp->undo[0]);
for (int i = 1; i < wp->undo_count; i++)
wp->undo[i - 1] = wp->undo[i];
montauk::memset(&wp->undo[wp->undo_count - 1], 0, sizeof(UndoSnapshot));
wp->undo_count--;
if (wp->undo_pos > 0) wp->undo_pos--;
}
void wp_history_checkpoint(WordProcessorState* wp) { void wp_history_checkpoint(WordProcessorState* wp) {
wp->undo_typing = false;
UndoSnapshot snap = {}; UndoSnapshot snap = {};
if (!wp_capture_snapshot(wp, &snap)) if (!wp_capture_snapshot(wp, &snap))
return; return;
@@ -2127,26 +2358,51 @@ void wp_history_checkpoint(WordProcessorState* wp) {
wp_free_snapshot(&wp->undo[i]); wp_free_snapshot(&wp->undo[i]);
wp->undo_count = wp->undo_pos + 1; wp->undo_count = wp->undo_pos + 1;
if (wp->undo_count >= WP_UNDO_MAX) { if (wp->undo_count >= WP_UNDO_MAX)
wp_free_snapshot(&wp->undo[0]); wp_history_drop_oldest(wp);
for (int i = 1; i < wp->undo_count; i++)
wp->undo[i - 1] = wp->undo[i];
montauk::memset(&wp->undo[wp->undo_count - 1], 0, sizeof(UndoSnapshot));
wp->undo_count--;
if (wp->undo_pos > 0) wp->undo_pos--;
}
wp->undo[wp->undo_count++] = snap; wp->undo[wp->undo_count++] = snap;
wp->undo_pos = wp->undo_count - 1; wp->undo_pos = wp->undo_count - 1;
// Snapshots are whole documents, so large ones get a shorter history.
int64_t bytes = 0;
for (int i = 0; i < wp->undo_count; i++)
bytes += wp->undo[i].size;
while (bytes > WP_UNDO_MAX_BYTES && wp->undo_count > 2) {
bytes -= wp->undo[0].size;
wp_history_drop_oldest(wp);
}
}
// Checkpoint after typing `c`. Consecutive characters share one undo step,
// which ends with the spaces after a word, so undo removes a word at a time.
void wp_history_checkpoint_typing(WordProcessorState* wp, char c) {
bool word_char = c != ' ';
int top = wp->undo_count - 1;
bool extend = wp->undo_typing && top > 0 && wp->undo_pos == top &&
wp->undo[top].cursor_abs + 1 == wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset) &&
(wp->undo_typing_word || !word_char);
if (extend) {
wp_free_snapshot(&wp->undo[top]);
wp->undo_count--;
wp->undo_pos--;
}
wp_history_checkpoint(wp);
wp->undo_typing = true;
wp->undo_typing_word = word_char;
} }
void wp_history_mark_saved(WordProcessorState* wp) { void wp_history_mark_saved(WordProcessorState* wp) {
wp->modified = false; wp->modified = false;
wp->undo_typing = false;
if (wp->undo_count <= 0) { if (wp->undo_count <= 0) {
wp_history_reset(wp); wp_history_reset(wp);
return; return;
} }
wp->undo[wp->undo_pos].modified = false; // Only the current state matches the file now.
for (int i = 0; i < wp->undo_count; i++)
wp->undo[i].modified = (i != wp->undo_pos);
} }
bool wp_undo(WordProcessorState* wp) { bool wp_undo(WordProcessorState* wp) {
@@ -2154,6 +2410,7 @@ bool wp_undo(WordProcessorState* wp) {
return false; return false;
wp->undo_pos--; wp->undo_pos--;
wp->undo_typing = false;
return wp_restore_snapshot(wp, &wp->undo[wp->undo_pos]); return wp_restore_snapshot(wp, &wp->undo[wp->undo_pos]);
} }
@@ -2162,5 +2419,6 @@ bool wp_redo(WordProcessorState* wp) {
return false; return false;
wp->undo_pos++; wp->undo_pos++;
wp->undo_typing = false;
return wp_restore_snapshot(wp, &wp->undo[wp->undo_pos]); return wp_restore_snapshot(wp, &wp->undo[wp->undo_pos]);
} }
+130 -118
View File
@@ -8,12 +8,6 @@
#include <gui/dialogs.hpp> #include <gui/dialogs.hpp>
#include <gui/clipboard.hpp> #include <gui/clipboard.hpp>
static void wp_set_status(WordProcessorState* wp, const char* msg) {
if (!msg) msg = "";
montauk::strncpy(wp->status_msg, msg, (int)sizeof(wp->status_msg) - 1);
wp->status_msg[sizeof(wp->status_msg) - 1] = '\0';
}
// Documents store text as Windows-1252 (one byte per character, matching the // Documents store text as Windows-1252 (one byte per character, matching the
// WinAnsi fonts used for PDF export); the clipboard is UTF-8. Convert both ways. // WinAnsi fonts used for PDF export); the clipboard is UTF-8. Convert both ways.
static char* wp_cp1252_to_utf8_alloc(const char* text, int len, int* out_len) { static char* wp_cp1252_to_utf8_alloc(const char* text, int len, int* out_len) {
@@ -27,18 +21,28 @@ static char* wp_cp1252_to_utf8_alloc(const char* text, int len, int* out_len) {
return out; return out;
} }
// Characters with no Windows-1252 form become '?'. // Characters with no Windows-1252 form become '?'. Tabs expand to spaces as
static int wp_utf8_to_cp1252_inplace(char* text, int len) { // when typed; other control characters (the CR of CRLF text) are dropped.
static char* wp_utf8_to_cp1252_alloc(const char* text, int len, int* out_len) {
char* out = (char*)montauk::malloc(len * 4 + 1);
if (!out) return nullptr;
int o = 0; int o = 0;
for (int pos = 0; pos < len;) { for (int pos = 0; pos < len;) {
int n; int n;
uint32_t cp = montauk::utf8::decode(text + pos, len - pos, &n); uint32_t cp = montauk::utf8::decode(text + pos, len - pos, &n);
pos += n; pos += n;
uint8_t b = cp == 0 ? 0 : montauk::utf8::to_cp1252(cp); if (cp == '\t') {
text[o++] = b ? (char)b : '?'; for (int i = 0; i < 4; i++) out[o++] = ' ';
} else if (cp == '\n') {
out[o++] = '\n';
} else if (cp >= 0x20 && cp != 0x7F) {
uint8_t b = montauk::utf8::to_cp1252(cp);
out[o++] = b ? (char)b : '?';
}
} }
text[o] = '\0'; out[o] = '\0';
return o; *out_len = o;
return out;
} }
static bool wp_copy_selection_to_clipboard(WordProcessorState* wp) { static bool wp_copy_selection_to_clipboard(WordProcessorState* wp) {
@@ -75,13 +79,14 @@ static bool wp_cut_selection_to_clipboard(WordProcessorState* wp) {
static bool wp_paste_from_clipboard(WordProcessorState* wp) { static bool wp_paste_from_clipboard(WordProcessorState* wp) {
int len = 0; int len = 0;
char* text = gui::clipboard_get_text_alloc(&len); char* utf8 = gui::clipboard_get_text_alloc(&len);
char* text = (utf8 && len > 0) ? wp_utf8_to_cp1252_alloc(utf8, len, &len) : nullptr;
if (utf8) montauk::mfree(utf8);
if (text == nullptr || len <= 0) { if (text == nullptr || len <= 0) {
if (text) montauk::mfree(text); if (text) montauk::mfree(text);
wp_set_status(wp, "Clipboard is empty"); wp_set_status(wp, "Clipboard is empty");
return false; return false;
} }
len = wp_utf8_to_cp1252_inplace(text, len);
bool ok = wp_replace_selection_with_text(wp, text, len); bool ok = wp_replace_selection_with_text(wp, text, len);
montauk::mfree(text); montauk::mfree(text);
@@ -203,8 +208,7 @@ static void wp_open_export_dialog(WordProcessorState* wp) {
static void wp_commit_pathbar(WordProcessorState* wp) { static void wp_commit_pathbar(WordProcessorState* wp) {
if (!wp->pathbar_text[0]) return; if (!wp->pathbar_text[0]) return;
if (wp->pathbar_save_mode) { if (wp->pathbar_save_mode) {
wp_set_filepath(wp, wp->pathbar_text); wp_save_file_as(wp, wp->pathbar_text);
wp_save_file(wp);
} else { } else {
wp_load_file(wp, wp->pathbar_text); wp_load_file(wp, wp->pathbar_text);
} }
@@ -231,6 +235,33 @@ static void wp_close_dropdowns(WordProcessorState* wp) {
wp->special_char_flyout_open = false; wp->special_char_flyout_open = false;
} }
// Typing continues in the style of the text the cursor was moved into: the
// character before it, or the one after it at the start of a paragraph.
static void wp_adopt_style_at_cursor(WordProcessorState* wp) {
if (wp->total_text_len <= 0) return;
int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset);
int ch = abs > 0 ? abs - 1 : 0;
if (abs > 0 && abs < wp->total_text_len &&
wp_char_at(wp, abs - 1) == '\n' && wp_char_at(wp, abs) != '\n')
ch = abs;
int r, o;
wp_pos_to_run(wp, ch + 1, &r, &o);
wp->cur_font_id = wp->runs[r].font_id;
wp->cur_size = wp->runs[r].size;
wp->cur_flags = wp->runs[r].flags;
}
static void wp_toggle_style(WordProcessorState* wp, int mode, uint8_t flag) {
if (wp->has_selection) {
wp_apply_style_to_selection(wp, mode, 0);
wp_history_checkpoint(wp);
} else {
wp->cur_flags ^= flag;
}
}
static void wp_insert_special_char(WordProcessorState* wp, uint8_t ch) { static void wp_insert_special_char(WordProcessorState* wp, uint8_t ch) {
if (wp->has_selection) wp_delete_selection(wp); if (wp->has_selection) wp_delete_selection(wp);
wp_insert_char(wp, (char)ch); wp_insert_char(wp, (char)ch);
@@ -243,32 +274,22 @@ static void wp_insert_divider_choice(WordProcessorState* wp, uint8_t divider_typ
} }
static int wp_hit_test_text(WordProcessorState* wp, int local_x, int local_y, int edit_y, static int wp_hit_test_text(WordProcessorState* wp, int local_x, int local_y, int edit_y,
int page_off_x, int page_off_y) { int page_off_x) {
int click_y = local_y - edit_y + wp->scrollbar.scroll_offset; int click_y = local_y - edit_y + wp->scrollbar.scroll_offset;
// In page view mode, subtract the page y offset
if (wp->page_view_mode) {
click_y = local_y - page_off_y + wp->scrollbar.scroll_offset;
}
int target_line = wp->wrap_line_count - 1; // Nearest line at or above the click, so gaps (paragraph spacing, page
// breaks) resolve to the line before them.
int target_line = 0;
for (int i = 0; i < wp->wrap_line_count; i++) { for (int i = 0; i < wp->wrap_line_count; i++) {
if (click_y >= wp->wrap_lines[i].y && if (wp->wrap_lines[i].y > click_y) break;
click_y < wp->wrap_lines[i].y + wp->wrap_lines[i].height) { target_line = i;
target_line = i;
break;
}
} }
WrapLine* wl = &wp->wrap_lines[target_line]; WrapLine* wl = &wp->wrap_lines[target_line];
if (wl->divider_type != PARA_DIVIDER_NONE) if (wl->divider_type != PARA_DIVIDER_NONE)
return wp_wrap_line_start(wp, target_line); return wp_wrap_line_start(wp, target_line);
// In page view mode, subtract the page x offset from click x int click_x = local_x - page_off_x - wl->x;
int click_x = local_x;
if (wp->page_view_mode) {
click_x = local_x - page_off_x;
}
click_x -= wl->x;
int ri = wl->run_idx; int ri = wl->run_idx;
int ro = wl->run_offset; int ro = wl->run_offset;
int chars_left = wl->char_count; int chars_left = wl->char_count;
@@ -326,16 +347,11 @@ void wp_handle_mouse(const montauk::abi::WinEvent& ev) {
wp->wrap_dirty = true; wp->wrap_dirty = true;
} }
// Compute page offsets for hit testing (same logic as wp_render) // Page x offset for hit testing (same logic as wp_render)
int page_off_x = 0; int page_off_x = 0;
int page_off_y = edit_y;
int recompute_w = g_win_w;
if (wp->page_view_mode) { if (wp->page_view_mode) {
page_off_x = (g_win_w - WP_A4_PAGE_W) / 2; page_off_x = (g_win_w - WP_SCROLLBAR_W - WP_PAGE_W) / 2;
if (page_off_x < 0) page_off_x = 0; if (page_off_x < 0) page_off_x = 0;
page_off_y = edit_y + WP_PAGE_TOP_MARGIN;
// Use A4 page width for wrap computation (to get proper margins)
recompute_w = WP_A4_PAGE_W;
} }
if (wp->context_menu.open) { if (wp->context_menu.open) {
@@ -358,7 +374,7 @@ void wp_handle_mouse(const montauk::abi::WinEvent& ev) {
return; return;
} }
wp->scrollbar.handle_mouse(local_x, local_y, ev.mouse.buttons, ev.mouse.prev_buttons, ev.mouse.scroll); wp->scrollbar.handle_mouse(local_x, local_y, ev.mouse.buttons, ev.mouse.prev_buttons);
if (wp->font_dropdown_open && wp_left_pressed(ev.mouse.buttons, ev.mouse.prev_buttons)) { if (wp->font_dropdown_open && wp_left_pressed(ev.mouse.buttons, ev.mouse.prev_buttons)) {
int dx = WP_FONT_DD_X; int dx = WP_FONT_DD_X;
@@ -462,15 +478,11 @@ void wp_handle_mouse(const montauk::abi::WinEvent& ev) {
return; return;
} }
if (local_x >= WP_BTN_BOLD_X && local_x < WP_BTN_BOLD_X + 24 && local_y >= 6 && local_y < 30) { if (local_x >= WP_BTN_BOLD_X && local_x < WP_BTN_BOLD_X + 24 && local_y >= 6 && local_y < 30) {
if (wp->has_selection) wp_apply_style_to_selection(wp, 2, 0); wp_toggle_style(wp, 2, STYLE_BOLD);
wp->cur_flags ^= STYLE_BOLD;
if (wp->has_selection) wp_history_checkpoint(wp);
return; return;
} }
if (local_x >= WP_BTN_ITALIC_X && local_x < WP_BTN_ITALIC_X + 24 && local_y >= 6 && local_y < 30) { if (local_x >= WP_BTN_ITALIC_X && local_x < WP_BTN_ITALIC_X + 24 && local_y >= 6 && local_y < 30) {
if (wp->has_selection) wp_apply_style_to_selection(wp, 3, 0); wp_toggle_style(wp, 3, STYLE_ITALIC);
wp->cur_flags ^= STYLE_ITALIC;
if (wp->has_selection) wp_history_checkpoint(wp);
return; return;
} }
if (local_x >= WP_FONT_DD_X && local_x < WP_FONT_DD_X + WP_FONT_DD_W && local_y >= 6 && local_y < 30) { if (local_x >= WP_FONT_DD_X && local_x < WP_FONT_DD_X + WP_FONT_DD_W && local_y >= 6 && local_y < 30) {
@@ -565,44 +577,49 @@ void wp_handle_mouse(const montauk::abi::WinEvent& ev) {
return; return;
} }
wp_recompute_wrap(wp, recompute_w, WP_SCREEN_DPI); // A scrollbar drag must not also place the cursor or extend a selection.
if (wp->scrollbar.dragging) {
wp_close_dropdowns(wp);
return;
}
wp_layout_screen(wp);
bool right_pressed = wp_right_pressed(ev.mouse.buttons, ev.mouse.prev_buttons); bool right_pressed = wp_right_pressed(ev.mouse.buttons, ev.mouse.prev_buttons);
bool in_text_area = local_y >= edit_y && local_y < edit_y + text_area_h && bool in_text_area = local_y >= edit_y && local_y < edit_y + text_area_h &&
local_x >= 0 && local_x < g_win_w - WP_SCROLLBAR_W; local_x >= 0 && local_x < g_win_w - WP_SCROLLBAR_W;
if (in_text_area && wp->page_view_mode) {
in_text_area = local_x >= page_off_x && local_x < page_off_x + WP_A4_PAGE_W &&
local_y >= page_off_y && local_y < page_off_y + WP_A4_PAGE_H;
}
if (right_pressed && in_text_area) { if (right_pressed && in_text_area) {
wp_close_dropdowns(wp); wp_close_dropdowns(wp);
int abs = wp_hit_test_text(wp, local_x, local_y, edit_y, page_off_x, page_off_y); int abs = wp_hit_test_text(wp, local_x, local_y, edit_y, page_off_x);
if (!wp_selection_contains_abs(wp, abs)) { if (!wp_selection_contains_abs(wp, abs)) {
wp_pos_to_run(wp, abs, &wp->cursor_run, &wp->cursor_offset); wp_pos_to_run(wp, abs, &wp->cursor_run, &wp->cursor_offset);
wp_clear_selection(wp); wp_clear_selection(wp);
wp_adopt_style_at_cursor(wp);
} }
wp->mouse_selecting = false; wp->mouse_selecting = false;
mtk::context_menu_open(wp->context_menu, local_x, local_y); mtk::context_menu_open(wp->context_menu, local_x, local_y);
return; return;
} }
if (wp_left_pressed(ev.mouse.buttons, ev.mouse.prev_buttons) && if (wp_left_pressed(ev.mouse.buttons, ev.mouse.prev_buttons) && in_text_area) {
local_y >= edit_y && local_y < edit_y + text_area_h) {
wp_close_dropdowns(wp); wp_close_dropdowns(wp);
int abs = wp_hit_test_text(wp, local_x, local_y, edit_y, page_off_x, page_off_y); int abs = wp_hit_test_text(wp, local_x, local_y, edit_y, page_off_x);
wp_pos_to_run(wp, abs, &wp->cursor_run, &wp->cursor_offset); wp_pos_to_run(wp, abs, &wp->cursor_run, &wp->cursor_offset);
wp->sel_anchor = abs; wp->sel_anchor = abs;
wp->sel_end = abs; wp->sel_end = abs;
wp->has_selection = false; wp->has_selection = false;
wp->mouse_selecting = true; wp->mouse_selecting = true;
wp->scroll_to_cursor = true;
wp_adopt_style_at_cursor(wp);
return; return;
} }
if (wp->mouse_selecting && wp_left_held(ev.mouse.buttons) && local_y >= edit_y - 20) { if (wp->mouse_selecting && wp_left_held(ev.mouse.buttons) && local_y >= edit_y - 20) {
int abs = wp_hit_test_text(wp, local_x, local_y, edit_y, page_off_x, page_off_y); int abs = wp_hit_test_text(wp, local_x, local_y, edit_y, page_off_x);
if (abs != wp->sel_end) wp->scroll_to_cursor = true;
wp->sel_end = abs; wp->sel_end = abs;
wp_pos_to_run(wp, abs, &wp->cursor_run, &wp->cursor_offset); wp_pos_to_run(wp, abs, &wp->cursor_run, &wp->cursor_offset);
wp->has_selection = (wp->sel_anchor != wp->sel_end); wp->has_selection = (wp->sel_anchor != wp->sel_end);
@@ -657,7 +674,8 @@ void wp_handle_key(const montauk::abi::KeyEvent& key) {
} }
wp_close_dropdowns(wp); wp_close_dropdowns(wp);
wp_recompute_wrap(wp, g_win_w, WP_SCREEN_DPI); wp_layout_screen(wp);
wp->scroll_to_cursor = true;
if (key.ctrl && (key.ascii == 's' || key.ascii == 'S')) { if (key.ctrl && (key.ascii == 's' || key.ascii == 'S')) {
wp_save_file(wp); wp_save_file(wp);
@@ -774,15 +792,11 @@ void wp_handle_key(const montauk::abi::KeyEvent& key) {
} }
if (key.ctrl && !key.alt && (key.ascii == 'b' || key.ascii == 'B')) { if (key.ctrl && !key.alt && (key.ascii == 'b' || key.ascii == 'B')) {
if (wp->has_selection) wp_apply_style_to_selection(wp, 2, 0); wp_toggle_style(wp, 2, STYLE_BOLD);
wp->cur_flags ^= STYLE_BOLD;
if (wp->has_selection) wp_history_checkpoint(wp);
return; return;
} }
if (key.ctrl && !key.alt && (key.ascii == 'i' || key.ascii == 'I')) { if (key.ctrl && !key.alt && (key.ascii == 'i' || key.ascii == 'I')) {
if (wp->has_selection) wp_apply_style_to_selection(wp, 3, 0); wp_toggle_style(wp, 3, STYLE_ITALIC);
wp->cur_flags ^= STYLE_ITALIC;
if (wp->has_selection) wp_history_checkpoint(wp);
return; return;
} }
@@ -799,70 +813,63 @@ void wp_handle_key(const montauk::abi::KeyEvent& key) {
return; return;
} }
if (montauk::nav_scancode(key) == 0x48) { uint8_t nav = montauk::nav_scancode(key);
if (key.shift) wp_start_selection(wp); bool collapse = !key.shift && wp->has_selection;
else wp_clear_selection(wp); if (nav == 0x4B && collapse) {
wp_cursor_up(wp); int s, e;
if (key.shift) wp_update_selection_to_cursor(wp); wp_sel_range(wp, &s, &e);
wp_pos_to_run(wp, s, &wp->cursor_run, &wp->cursor_offset);
wp_clear_selection(wp);
wp_adopt_style_at_cursor(wp);
return; return;
} }
if (montauk::nav_scancode(key) == 0x50) { if (nav == 0x4D && collapse) {
if (key.shift) wp_start_selection(wp); int s, e;
else wp_clear_selection(wp); wp_sel_range(wp, &s, &e);
wp_cursor_down(wp); wp_pos_to_run(wp, e, &wp->cursor_run, &wp->cursor_offset);
if (key.shift) wp_update_selection_to_cursor(wp); wp_clear_selection(wp);
return; wp_adopt_style_at_cursor(wp);
}
if (montauk::nav_scancode(key) == 0x4B) {
if (key.shift) wp_start_selection(wp);
else if (wp->has_selection) {
int s, e;
wp_sel_range(wp, &s, &e);
wp_pos_to_run(wp, s, &wp->cursor_run, &wp->cursor_offset);
wp_clear_selection(wp);
return;
}
wp_cursor_left(wp);
if (key.shift) wp_update_selection_to_cursor(wp);
return;
}
if (montauk::nav_scancode(key) == 0x4D) {
if (key.shift) wp_start_selection(wp);
else if (wp->has_selection) {
int s, e;
wp_sel_range(wp, &s, &e);
wp_pos_to_run(wp, e, &wp->cursor_run, &wp->cursor_offset);
wp_clear_selection(wp);
return;
}
wp_cursor_right(wp);
if (key.shift) wp_update_selection_to_cursor(wp);
return; return;
} }
if (montauk::nav_scancode(key) == 0x47) { if (nav == 0x48 || nav == 0x50 || nav == 0x4B || nav == 0x4D ||
nav == 0x47 || nav == 0x4F || nav == 0x49 || nav == 0x51) {
if (key.shift) wp_start_selection(wp); if (key.shift) wp_start_selection(wp);
else wp_clear_selection(wp); else wp_clear_selection(wp);
int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset); int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset);
int line = wp_find_wrap_line(wp, abs); int line = wp_find_wrap_line(wp, abs);
int start = wp_wrap_line_start(wp, line); int start = wp_wrap_line_start(wp, line);
wp_pos_to_run(wp, start, &wp->cursor_run, &wp->cursor_offset); int page_h = g_win_h - WP_TOOLBAR_H - WP_STATUS_H - WP_MARGIN * 2;
if (key.shift) wp_update_selection_to_cursor(wp); if (page_h < 40) page_h = 40;
return;
} switch (nav) {
if (montauk::nav_scancode(key) == 0x4F) { case 0x48: wp_cursor_up(wp); break;
if (key.shift) wp_start_selection(wp); case 0x50: wp_cursor_down(wp); break;
else wp_clear_selection(wp); case 0x4B: wp_cursor_left(wp); break;
int abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset); case 0x4D: wp_cursor_right(wp); break;
int line = wp_find_wrap_line(wp, abs); case 0x49: wp_cursor_page(wp, -page_h); break;
int start = wp_wrap_line_start(wp, line); case 0x51: wp_cursor_page(wp, page_h); break;
int end = start + wp->wrap_lines[line].char_count; case 0x47:
if (end > start) { wp_pos_to_run(wp, key.ctrl ? 0 : start, &wp->cursor_run, &wp->cursor_offset);
char ch = wp_char_at(wp, end - 1); break;
if (ch == '\n') end--; case 0x4F: {
int end = start + wp->wrap_lines[line].char_count;
if (key.ctrl) {
end = wp->total_text_len;
} else if (end > start) {
// Stay on this line: before its newline, or before the space a
// soft wrap broke at (past it is the start of the next line).
char ch = wp_char_at(wp, end - 1);
if (ch == '\n' || (ch == ' ' && line < wp->wrap_line_count - 1)) end--;
}
wp_pos_to_run(wp, end, &wp->cursor_run, &wp->cursor_offset);
break;
} }
wp_pos_to_run(wp, end, &wp->cursor_run, &wp->cursor_offset); }
if (key.shift) wp_update_selection_to_cursor(wp); if (key.shift) wp_update_selection_to_cursor(wp);
wp_adopt_style_at_cursor(wp);
return; return;
} }
@@ -902,9 +909,14 @@ void wp_handle_key(const montauk::abi::KeyEvent& key) {
return; return;
} }
// Unbound Ctrl/Alt chords are not text. AltGr is reported on its own.
if ((key.ctrl || key.alt) && !key.altgr) return;
if ((unsigned char)key.ascii >= 32 && (unsigned char)key.ascii != 127) { if ((unsigned char)key.ascii >= 32 && (unsigned char)key.ascii != 127) {
if (wp->has_selection) wp_delete_selection(wp); bool replaced = wp->has_selection;
if (replaced) wp_delete_selection(wp);
wp_insert_char(wp, key.ascii); wp_insert_char(wp, key.ascii);
wp_history_checkpoint(wp); if (replaced) wp_history_checkpoint(wp);
else wp_history_checkpoint_typing(wp, key.ascii);
} }
} }
+41 -29
View File
@@ -5,6 +5,7 @@
*/ */
#include "wordprocessor.hpp" #include "wordprocessor.hpp"
#include <gui/transfer.hpp>
int g_win_w = INIT_W; int g_win_w = INIT_W;
int g_win_h = INIT_H; int g_win_h = INIT_H;
@@ -93,6 +94,10 @@ extern "C" void _start() {
int arglen = montauk::getargs(args, sizeof(args)); int arglen = montauk::getargs(args, sizeof(args));
if (arglen > 0 && args[0]) { if (arglen > 0 && args[0]) {
wp_load_file(&g_wp, args); wp_load_file(&g_wp, args);
// The launcher starts us with the home directory, not a document.
int n = montauk::slen(args);
if (!g_wp.filename[0] && (n < 4 || !montauk::streq(args + n - 4, ".mwp")))
wp_set_status(&g_wp, "");
} }
char title[64] = "Word Processor"; char title[64] = "Word Processor";
@@ -109,38 +114,45 @@ extern "C" void _start() {
wp_render(); wp_render();
g_win.present(); g_win.present();
while (true) { bool running = true;
montauk::abi::WinEvent ev; while (running) {
int r = g_win.poll(&ev); // Handle everything that is queued, then draw once.
bool redraw = false;
for (;;) {
montauk::abi::WinEvent ev;
int r = g_win.poll(&ev);
if (r < 0 || (r > 0 && ev.type == 3)) {
running = false;
break;
}
if (r == 0) break;
if (r < 0) break; redraw = true;
if (r == 0) { // A file dropped from Files opens here, unless it would discard
// unsaved changes (then the drop is refused).
char dropped[512];
if (gui::handle_file_drop(g_win.id, ev, dropped, sizeof(dropped), !g_wp.modified)) {
wp_load_file(&g_wp, dropped);
continue;
}
if (ev.type == 2 || ev.type == 4) {
g_win_w = g_win.width;
g_win_h = g_win.height;
} else if (ev.type == 0 && ev.key.pressed) {
wp_handle_key(ev.key);
} else if (ev.type == 1) {
wp_handle_mouse(ev);
}
}
if (!running) break;
if (redraw) {
wp_render();
g_win.present();
} else {
montauk::sleep_ms(16); montauk::sleep_ms(16);
continue;
}
if (ev.type == 3) break;
if (ev.type == 2 || ev.type == 4) {
g_win_w = g_win.width;
g_win_h = g_win.height;
wp_render();
g_win.present();
continue;
}
if (ev.type == 0 && ev.key.pressed) {
wp_handle_key(ev.key);
wp_render();
g_win.present();
continue;
}
if (ev.type == 1) {
wp_handle_mouse(ev);
wp_render();
g_win.present();
} }
} }
+2 -31
View File
@@ -19,10 +19,6 @@ extern "C" {
namespace dialogs = gui::dialogs; namespace dialogs = gui::dialogs;
static constexpr int WP_PRINT_PAGE_W = 1275; // 8.5in at 150 DPI
static constexpr int WP_PRINT_PAGE_H = 1650; // 11in at 150 DPI
static constexpr int WP_PRINT_MARGIN_X = 96;
static constexpr int WP_PRINT_MARGIN_Y = 96;
static constexpr int WP_PRINT_JPEG_QUALITY = 92; static constexpr int WP_PRINT_JPEG_QUALITY = 92;
struct WpJpegBuffer { struct WpJpegBuffer {
@@ -162,38 +158,13 @@ static bool wp_build_print_page_map(WordProcessorState* wp,
return false; return false;
} }
int base_y = wp->wrap_lines[0].y; int page_count = wp_paginate(wp, WP_PRINT_PAGE_H, WP_PRINT_MARGIN_Y, line_pages, line_y);
int printable_bottom = WP_PRINT_PAGE_H - WP_PRINT_MARGIN_Y;
int current_page = 0;
int current_y = WP_PRINT_MARGIN_Y;
int prev_rel_bottom = 0;
for (int i = 0; i < wp->wrap_line_count; i++) {
WrapLine* wl = &wp->wrap_lines[i];
int rel_y = wl->y - base_y;
if (rel_y < 0) rel_y = 0;
int gap_before = (i == 0) ? 0 : (rel_y - prev_rel_bottom);
if (gap_before < 0) gap_before = 0;
int line_top = current_y + gap_before;
if (line_top + wl->height > printable_bottom && current_y > WP_PRINT_MARGIN_Y) {
current_page++;
current_y = WP_PRINT_MARGIN_Y;
line_top = current_y;
}
line_pages[i] = current_page;
line_y[i] = line_top;
current_y = line_top + wl->height;
prev_rel_bottom = rel_y + wl->height;
}
if (out_line_pages) *out_line_pages = line_pages; if (out_line_pages) *out_line_pages = line_pages;
else montauk::mfree(line_pages); else montauk::mfree(line_pages);
if (out_line_y) *out_line_y = line_y; if (out_line_y) *out_line_y = line_y;
else montauk::mfree(line_y); else montauk::mfree(line_y);
if (out_page_count) *out_page_count = current_page + 1; if (out_page_count) *out_page_count = page_count;
return true; return true;
} }
+68 -121
View File
@@ -208,15 +208,15 @@ static const char* wp_divider_label(uint8_t divider_type) {
} }
} }
static void wp_draw_list_marker(Canvas& c, WordProcessorState* wp, WrapLine* wl, int py, int page_off_x) { static void wp_draw_list_marker(Canvas& c, WordProcessorState* wp, WrapLine* wl, int py, int origin_x) {
if (!wl->first_in_paragraph || wl->paragraph_idx < 0 || wl->paragraph_idx >= wp->paragraph_count) if (!wl->first_in_paragraph || wl->paragraph_idx < 0 || wl->paragraph_idx >= wp->paragraph_count)
return; return;
ParagraphStyle* para = &wp->paragraphs[wl->paragraph_idx]; ParagraphStyle* para = &wp->paragraphs[wl->paragraph_idx];
if (para->divider_type != PARA_DIVIDER_NONE || para->list_type == PARA_LIST_NONE) return; if (para->divider_type != PARA_DIVIDER_NONE || para->list_type == PARA_LIST_NONE) return;
int marker_x = page_off_x + WP_MARGIN + wp_scale_layout_units(para->left_indent + para->first_line_indent, WP_SCREEN_DPI); int marker_x = origin_x + wp_scale_layout_units(para->left_indent + para->first_line_indent, WP_SCREEN_DPI);
if (marker_x < page_off_x + 4) marker_x = page_off_x + 4; if (marker_x < origin_x - WP_MARGIN + 4) marker_x = origin_x - WP_MARGIN + 4;
if (para->list_type == PARA_LIST_BULLET) { if (para->list_type == PARA_LIST_BULLET) {
int bullet_x = marker_x + wp_scale_layout_units(7, WP_SCREEN_DPI); int bullet_x = marker_x + wp_scale_layout_units(7, WP_SCREEN_DPI);
@@ -242,16 +242,9 @@ static void wp_draw_list_marker(Canvas& c, WordProcessorState* wp, WrapLine* wl,
draw_text(c, font ? font : g_ui_font, marker_x, top_y, label, colors::TEXT_COLOR, font_px); draw_text(c, font ? font : g_ui_font, marker_x, top_y, label, colors::TEXT_COLOR, font_px);
} }
void wp_render() { // Toolbar and path bar. Drawn after the document so a line scrolled partly
if (!g_win.pixels) return; // above the edit area cannot paint over them.
static void wp_draw_chrome(Canvas& c, WordProcessorState* wp, ParagraphStyle* cur_para, int sfh) {
WordProcessorState* wp = &g_wp;
Canvas c = g_win.canvas();
c.fill(colors::WINDOW_BG);
int sfh = wp_ui_line_height();
int cursor_abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset);
ParagraphStyle* cur_para = wp_current_paragraph_style(wp);
Color toolbar_bg = Color::from_rgb(0xF5, 0xF5, 0xF5); Color toolbar_bg = Color::from_rgb(0xF5, 0xF5, 0xF5);
Color btn_bg = Color::from_rgb(0xE8, 0xE8, 0xE8); Color btn_bg = Color::from_rgb(0xE8, 0xE8, 0xE8);
Color btn_active = Color::from_rgb(0xC0, 0xD0, 0xE8); Color btn_active = Color::from_rgb(0xC0, 0xD0, 0xE8);
@@ -362,7 +355,6 @@ void wp_render() {
c.hline(0, WP_TOOLBAR_H - 1, c.w, colors::BORDER); c.hline(0, WP_TOOLBAR_H - 1, c.w, colors::BORDER);
int edit_y = WP_TOOLBAR_H;
if (wp->show_pathbar) { if (wp->show_pathbar) {
int pb_y = WP_TOOLBAR_H; int pb_y = WP_TOOLBAR_H;
c.fill_rect(0, pb_y, c.w, WP_PATHBAR_H, Color::from_rgb(0xF0, 0xF0, 0xF0)); c.fill_rect(0, pb_y, c.w, WP_PATHBAR_H, Color::from_rgb(0xF0, 0xF0, 0xF0));
@@ -393,90 +385,86 @@ void wp_render() {
colors::ACCENT, colors::WHITE); colors::ACCENT, colors::WHITE);
c.hline(0, pb_y + WP_PATHBAR_H - 1, c.w, colors::BORDER); c.hline(0, pb_y + WP_PATHBAR_H - 1, c.w, colors::BORDER);
edit_y = WP_TOOLBAR_H + WP_PATHBAR_H;
} }
}
void wp_render() {
if (!g_win.pixels) return;
WordProcessorState* wp = &g_wp;
Canvas c = g_win.canvas();
c.fill(colors::WINDOW_BG);
int sfh = wp_ui_line_height();
int cursor_abs = wp_abs_pos(wp, wp->cursor_run, wp->cursor_offset);
ParagraphStyle* cur_para = wp_current_paragraph_style(wp);
int edit_y = WP_TOOLBAR_H + (wp->show_pathbar ? WP_PATHBAR_H : 0);
int text_area_h = c.h - edit_y - WP_STATUS_H; int text_area_h = c.h - edit_y - WP_STATUS_H;
// Handle pending page view toggle
if (wp->pending_page_view_toggle) { if (wp->pending_page_view_toggle) {
wp->page_view_mode = !wp->page_view_mode; wp->page_view_mode = !wp->page_view_mode;
wp->pending_page_view_toggle = false; wp->pending_page_view_toggle = false;
wp->wrap_dirty = true; wp->wrap_dirty = true;
wp->scroll_to_cursor = true;
} }
// Page view mode: compute A4 page position and use A4 content width
int page_x = 0;
int page_y = edit_y;
int page_content_w = c.w;
bool in_page_view = wp->page_view_mode; bool in_page_view = wp->page_view_mode;
int view_w = c.w - WP_SCROLLBAR_W;
int page_x = 0;
if (in_page_view) { if (in_page_view) {
// Center A4 page horizontally in the window page_x = (view_w - WP_PAGE_W) / 2;
page_x = (c.w - WP_A4_PAGE_W) / 2;
if (page_x < 0) page_x = 0; if (page_x < 0) page_x = 0;
// Page top with margin from edit area top
page_y = edit_y + WP_PAGE_TOP_MARGIN;
// Use full A4 page width for wrap computation to get proper A4 margins
// (WP_MARGIN is used for left margin, right margin comes from WP_A4_PAGE_W)
page_content_w = WP_A4_PAGE_W;
} }
wp_recompute_wrap(wp, page_content_w, WP_SCREEN_DPI); wp_layout_screen(wp);
wp->scrollbar.bounds = {c.w - WP_SCROLLBAR_W, edit_y, WP_SCROLLBAR_W, text_area_h}; wp->scrollbar.bounds = {c.w - WP_SCROLLBAR_W, edit_y, WP_SCROLLBAR_W, text_area_h};
wp->scrollbar.content_height = wp->content_height; wp->scrollbar.content_height = wp->content_height;
wp->scrollbar.view_height = text_area_h; wp->scrollbar.view_height = text_area_h;
// In page view mode, keep scrollbar at right edge but use text_area_h for proper thumb sizing if (wp->scroll_to_cursor) {
// The scrollbar position doesn't change - it stays at c.w - WP_SCROLLBAR_W wp_ensure_cursor_visible(wp, text_area_h);
// But the content scrolls within the A4 page view wp->scroll_to_cursor = false;
}
{ {
int ms = wp->scrollbar.max_scroll(); int ms = wp->scrollbar.max_scroll();
if (wp->scrollbar.scroll_offset > ms) wp->scrollbar.scroll_offset = ms; if (wp->scrollbar.scroll_offset > ms) wp->scrollbar.scroll_offset = ms;
if (wp->scrollbar.scroll_offset < 0) wp->scrollbar.scroll_offset = 0; if (wp->scrollbar.scroll_offset < 0) wp->scrollbar.scroll_offset = 0;
} }
wp_ensure_cursor_visible(wp, text_area_h);
int scroll_y = wp->scrollbar.scroll_offset; int scroll_y = wp->scrollbar.scroll_offset;
int sel_s = 0; int sel_s = 0;
int sel_e = 0; int sel_e = 0;
if (wp->has_selection) wp_sel_range(wp, &sel_s, &sel_e); if (wp->has_selection) wp_sel_range(wp, &sel_s, &sel_e);
Color sel_bg = Color::from_rgb(0xB0, 0xD0, 0xF0); Color sel_bg = Color::from_rgb(0xB0, 0xD0, 0xF0);
// In page view mode, draw the A4 page with shadow before text rendering // Text may not run under the scrollbar, nor off the page in page view.
int clip_right = in_page_view ? gui_min(page_x + WP_PAGE_W, view_w) : view_w - WP_MARGIN;
int cursor_right = in_page_view ? clip_right : view_w;
if (in_page_view) { if (in_page_view) {
// Gray background behind page (same style as PDF viewer) // Gray backdrop and offset shadow, same style as the PDF viewer
Color page_bg = Color::from_rgb(0x80, 0x80, 0x80); c.fill_rect(0, edit_y, c.w, text_area_h, Color::from_rgb(0x80, 0x80, 0x80));
c.fill_rect(0, edit_y, c.w, text_area_h, page_bg); for (int pg = 0; pg < wp->page_count; pg++) {
int top = edit_y + WP_PAGE_TOP_MARGIN + pg * (WP_PAGE_H + WP_PAGE_GAP) - scroll_y;
// Simple offset shadow (same style as PDF viewer) if (top + WP_PAGE_H + 3 <= edit_y) continue;
Color shadow_col = Color::from_rgb(0x60, 0x60, 0x60); if (top >= edit_y + text_area_h) break;
c.fill_rect(page_x + 3, page_y + 3, WP_A4_PAGE_W, WP_A4_PAGE_H, shadow_col); c.fill_rect(page_x + 3, top + 3, WP_PAGE_W, WP_PAGE_H, Color::from_rgb(0x60, 0x60, 0x60));
c.fill_rect(page_x, top, WP_PAGE_W, WP_PAGE_H, colors::WHITE);
// White page background }
c.fill_rect(page_x, page_y, WP_A4_PAGE_W, WP_A4_PAGE_H, colors::WHITE);
} }
for (int li = 0; li < wp->wrap_line_count; li++) { for (int li = 0; li < wp->wrap_line_count; li++) {
WrapLine* wl = &wp->wrap_lines[li]; WrapLine* wl = &wp->wrap_lines[li];
int py = edit_y + wl->y - scroll_y; int py = edit_y + wl->y - scroll_y;
// In page view mode, offset y by page_y
if (in_page_view) {
py = page_y + wl->y - scroll_y;
}
if (py + wl->height <= edit_y) continue; if (py + wl->height <= edit_y) continue;
if (py >= edit_y + text_area_h) break; if (py >= edit_y + text_area_h) break;
// In page view mode, offset x by page_x for all drawing int draw_x = page_x + wl->x;
int draw_x = wl->x; int origin_x = page_x + (in_page_view ? WP_PAGE_MARGIN_X : WP_MARGIN);
if (in_page_view) {
draw_x = page_x + wl->x;
}
wp_draw_list_marker(c, wp, wl, py, in_page_view ? page_x : 0); wp_draw_list_marker(c, wp, wl, py, origin_x);
if (wl->divider_type != PARA_DIVIDER_NONE) if (wl->divider_type != PARA_DIVIDER_NONE)
wp_draw_divider_sample(c, draw_x, py, wl->width, wl->height, wl->divider_type, colors::TEXT_COLOR); wp_draw_divider_sample(c, draw_x, py, wl->width, wl->height, wl->divider_type, colors::TEXT_COLOR);
@@ -505,67 +493,32 @@ void wp_render() {
for (int ci = 0; ci < to_draw; ci++) { for (int ci = 0; ci < to_draw; ci++) {
char ch = r->text[ro + ci]; char ch = r->text[ro + ci];
int abs_ch = line_abs_start + char_idx; int abs_ch = line_abs_start + char_idx;
bool printable = ch != '\n' && (ch >= 32 || ch < 0);
bool selected = wp->has_selection && abs_ch >= sel_s && abs_ch < sel_e;
int char_adv = 0; int char_adv = 0;
if (ch != '\n' && (ch >= 32 || ch < 0)) { if (printable) {
CachedGlyph* g = font->get_glyph(gc, decode_single_byte_codepoint((unsigned char)ch)); CachedGlyph* g = font->get_glyph(gc, decode_single_byte_codepoint((unsigned char)ch));
char_adv = g ? g->advance : 8; char_adv = g ? g->advance : 0;
} }
if (wp->has_selection && abs_ch >= sel_s && abs_ch < sel_e) { if (selected) {
int sel_w = char_adv > 0 ? char_adv : 6; int sel_w = gui_min(x + (char_adv > 0 ? char_adv : 6), clip_right) - x;
// Clip selection to page bounds in page view mode
int sel_x = x;
int sel_y = py;
int sel_h = wl->height;
if (in_page_view) {
sel_x = gui_max(x, page_x);
int right = gui_min(x + sel_w, page_x + WP_A4_PAGE_W);
sel_w = right - sel_x;
sel_y = gui_max(py, page_y);
int bot = gui_min(py + sel_h, page_y + WP_A4_PAGE_H);
sel_h = bot - sel_y;
if (sel_w <= 0 || sel_h <= 0) sel_w = 0;
}
if (sel_w > 0) if (sel_w > 0)
c.fill_rect(sel_x, sel_y, sel_w, sel_h, sel_bg); c.fill_rect(x, py, sel_w, wl->height, sel_bg);
} }
if (abs_ch == cursor_abs) { if (abs_ch == cursor_abs && x + 2 <= cursor_right)
int cur_h = wl->height; c.fill_rect(x, py, 2, wl->height, colors::ACCENT);
int cur_x = x;
int cur_y = py;
int cur_w = 2;
if (in_page_view) {
// Clip cursor to page bounds
if (cur_x < page_x) { cur_w = cur_x + cur_w - page_x; cur_x = page_x; }
if (cur_x + cur_w > page_x + WP_A4_PAGE_W) cur_w = page_x + WP_A4_PAGE_W - cur_x;
cur_y = gui_max(cur_y, page_y);
int bot = gui_min(cur_y + cur_h, page_y + WP_A4_PAGE_H);
cur_h = bot - cur_y;
if (cur_h <= 0 || cur_w <= 0) cur_x = -1; // hide
}
if (cur_x >= 0 && py >= edit_y && py + cur_h <= edit_y + text_area_h)
c.fill_rect(cur_x, cur_y, cur_w, cur_h, colors::ACCENT);
}
if (ch != '\n' && (ch >= 32 || ch < 0)) { if (printable) {
// In page view mode, clip to page bounds if (x + char_adv <= clip_right) {
bool can_draw = (x < c.w - WP_SCROLLBAR_W - WP_MARGIN); Color text_col = selected ? Color::from_rgb(0x10, 0x10, 0x10) : colors::TEXT_COLOR;
if (in_page_view) { font->draw_char_to_buffer(
can_draw = (x + char_adv > page_x && x < page_x + WP_A4_PAGE_W &&
py + wl->height > page_y && py < page_y + WP_A4_PAGE_H);
}
if (can_draw) {
Color text_col = (wp->has_selection && abs_ch >= sel_s && abs_ch < sel_e)
? Color::from_rgb(0x10, 0x10, 0x10) : colors::TEXT_COLOR;
int adv = font->draw_char_to_buffer(
c.pixels, c.w, c.h, x, baseline, c.pixels, c.w, c.h, x, baseline,
decode_single_byte_codepoint((unsigned char)ch), text_col, gc); decode_single_byte_codepoint((unsigned char)ch), text_col, gc);
x += adv;
} else {
x += char_adv;
} }
x += char_adv;
} }
char_idx++; char_idx++;
@@ -579,23 +532,13 @@ void wp_render() {
} }
} }
if (line_abs_start + char_idx == cursor_abs && li == wp->wrap_line_count - 1) { if (line_abs_start + char_idx == cursor_abs && li == wp->wrap_line_count - 1 &&
int cur_h = wl->height; x + 2 <= cursor_right)
int cur_x = x; c.fill_rect(x, py, 2, wl->height, colors::ACCENT);
int cur_y = py;
if (in_page_view) {
if (cur_x < page_x) cur_x = page_x;
if (cur_x > page_x + WP_A4_PAGE_W) cur_x = -1;
cur_y = gui_max(cur_y, page_y);
int bot = gui_min(cur_y + cur_h, page_y + WP_A4_PAGE_H);
cur_h = bot - cur_y;
if (cur_h <= 0) cur_x = -1;
}
if (cur_x >= 0 && py >= edit_y && py + cur_h <= edit_y + text_area_h)
c.fill_rect(cur_x, cur_y, 2, cur_h, colors::ACCENT);
}
} }
wp_draw_chrome(c, wp, cur_para, sfh);
if (wp->scrollbar.content_height > wp->scrollbar.view_height) { if (wp->scrollbar.content_height > wp->scrollbar.view_height) {
Color sb_fg = (wp->scrollbar.hovered || wp->scrollbar.dragging) Color sb_fg = (wp->scrollbar.hovered || wp->scrollbar.dragging)
? wp->scrollbar.hover_fg : wp->scrollbar.fg; ? wp->scrollbar.hover_fg : wp->scrollbar.fg;
@@ -628,8 +571,12 @@ void wp_render() {
(int)cur_para->line_spacing); (int)cur_para->line_spacing);
} }
char status_right[32]; char status_right[48];
snprintf(status_right, sizeof(status_right), "%d chars ", wp->total_text_len); if (in_page_view)
snprintf(status_right, sizeof(status_right), "%d page%s | %d chars ",
wp->page_count, wp->page_count == 1 ? "" : "s", wp->total_text_len);
else
snprintf(status_right, sizeof(status_right), "%d chars ", wp->total_text_len);
int sr_w = text_width(g_ui_font, status_right, fonts::UI_SIZE); int sr_w = text_width(g_ui_font, status_right, fonts::UI_SIZE);
char status_combined[320]; char status_combined[320];
if (wp->status_msg[0]) snprintf(status_combined, sizeof(status_combined), "%s | %s", status_left, wp->status_msg); if (wp->status_msg[0]) snprintf(status_combined, sizeof(status_combined), "%s | %s", status_left, wp->status_msg);
+35 -23
View File
@@ -29,25 +29,32 @@ static constexpr int WP_PATHBAR_H = 32;
static constexpr int WP_STATUS_H = 24; static constexpr int WP_STATUS_H = 24;
static constexpr int WP_SCROLLBAR_W = 12; static constexpr int WP_SCROLLBAR_W = 12;
static constexpr int WP_MARGIN = 16; static constexpr int WP_MARGIN = 16;
// A4 page dimensions at 96 DPI screen resolution (210mm x 297mm = 8.27" x 11.69")
static constexpr int WP_A4_PAGE_W = 794; // 8.27" * 96 DPI
static constexpr int WP_A4_PAGE_H = 1122; // 11.69" * 96 DPI
static constexpr int WP_A4_MARGIN_X = 96; // 1 inch = 96px margins (same as print)
static constexpr int WP_A4_MARGIN_Y = 96;
// Content width inside A4 page (794 - 96*2 - 16*2 = 570... but we use WP_A4 margins internally)
// A4 text area = WP_A4_PAGE_W - WP_A4_MARGIN_X * 2 = 794 - 192 = 602
static constexpr int WP_A4_CONTENT_W = 602;
// Vertical page offset from top of edit area (page top at edit_y + WP_PAGE_TOP_MARGIN)
static constexpr int WP_PAGE_TOP_MARGIN = 20;
static constexpr int WP_PAGE_SHADOW_OFFSET = 6;
static constexpr int WP_MAX_RUNS = 1024; static constexpr int WP_MAX_RUNS = 1024;
static constexpr int WP_MAX_TEXT = 262144; static constexpr int WP_MAX_TEXT = 262144;
static constexpr int WP_MAX_WRAP_LINES = 4096; static constexpr int WP_MAX_WRAP_LINES = 32768;
static constexpr int WP_MAX_PARAGRAPHS = 4096; static constexpr int WP_MAX_PARAGRAPHS = 4096;
static constexpr int WP_DEFAULT_SIZE = 18; static constexpr int WP_DEFAULT_SIZE = 18;
static constexpr int WP_UNDO_MAX = 24; static constexpr int WP_UNDO_MAX = 64;
static constexpr int WP_UNDO_MAX_BYTES = 16 * 1024 * 1024;
static constexpr int WP_SCREEN_DPI = 96; static constexpr int WP_SCREEN_DPI = 96;
static constexpr int WP_PRINT_DPI = 150; static constexpr int WP_PRINT_DPI = 150;
// Printed page, in pixels at WP_PRINT_DPI. Print, PDF export and the on-screen
// page view all derive their geometry from these.
static constexpr int WP_PRINT_PAGE_W = 1275; // 8.5in
static constexpr int WP_PRINT_PAGE_H = 1650; // 11in
static constexpr int WP_PRINT_MARGIN_X = 96;
static constexpr int WP_PRINT_MARGIN_Y = 96;
static constexpr int wp_print_to_screen_px(int px) {
return (px * WP_SCREEN_DPI + WP_PRINT_DPI / 2) / WP_PRINT_DPI;
}
static constexpr int WP_PAGE_W = wp_print_to_screen_px(WP_PRINT_PAGE_W);
static constexpr int WP_PAGE_H = wp_print_to_screen_px(WP_PRINT_PAGE_H);
static constexpr int WP_PAGE_MARGIN_X = wp_print_to_screen_px(WP_PRINT_MARGIN_X);
static constexpr int WP_PAGE_MARGIN_Y = wp_print_to_screen_px(WP_PRINT_MARGIN_Y);
static constexpr int WP_PAGE_CONTENT_W = WP_PAGE_W - WP_PAGE_MARGIN_X * 2;
// Page view: gap above the first page and between pages.
static constexpr int WP_PAGE_TOP_MARGIN = 20;
static constexpr int WP_PAGE_GAP = 16;
static constexpr int WP_PARA_STEP = 12; static constexpr int WP_PARA_STEP = 12;
static constexpr int WP_SPACE_STEP = 6; static constexpr int WP_SPACE_STEP = 6;
static constexpr int WP_LIST_LEFT = 28; static constexpr int WP_LIST_LEFT = 28;
@@ -312,7 +319,7 @@ struct WpScrollbar {
return ms > 0 ? ms : 0; return ms > 0 ? ms : 0;
} }
void handle_mouse(int x, int y, uint8_t buttons, uint8_t prev_buttons, int scroll) { void handle_mouse(int x, int y, uint8_t buttons, uint8_t prev_buttons) {
if (content_height <= view_height) return; if (content_height <= view_height) return;
Rect thumb_rect = {bounds.x, thumb_y(), bounds.w, thumb_height()}; Rect thumb_rect = {bounds.x, thumb_y(), bounds.w, thumb_height()};
@@ -338,13 +345,6 @@ struct WpScrollbar {
if (!wp_left_held(buttons)) { if (!wp_left_held(buttons)) {
dragging = false; dragging = false;
} }
if (bounds.contains(x, y) && scroll != 0) {
scroll_offset += scroll * 20;
int ms = max_scroll();
if (scroll_offset < 0) scroll_offset = 0;
if (scroll_offset > ms) scroll_offset = ms;
}
} }
}; };
@@ -374,6 +374,9 @@ struct WordProcessorState {
bool wrap_dirty; bool wrap_dirty;
int last_wrap_width; int last_wrap_width;
int last_wrap_dpi; int last_wrap_dpi;
bool last_wrap_paged;
int page_count; // pages in the paged layout, 0 otherwise
bool scroll_to_cursor; // next render scrolls the cursor into view
ParagraphStyle paragraphs[WP_MAX_PARAGRAPHS]; ParagraphStyle paragraphs[WP_MAX_PARAGRAPHS];
int paragraph_count; int paragraph_count;
@@ -404,6 +407,8 @@ struct WordProcessorState {
UndoSnapshot undo[WP_UNDO_MAX]; UndoSnapshot undo[WP_UNDO_MAX];
int undo_count; int undo_count;
int undo_pos; int undo_pos;
bool undo_typing; // newest snapshot is a run of typed characters
bool undo_typing_word; // ... whose last character was not a space
}; };
extern int g_win_w; extern int g_win_w;
@@ -451,12 +456,14 @@ void wp_selected_paragraph_range(WordProcessorState* wp, int* out_start_para, in
void wp_insert_char(WordProcessorState* wp, char c); void wp_insert_char(WordProcessorState* wp, char c);
void wp_delete_char(WordProcessorState* wp); void wp_delete_char(WordProcessorState* wp);
void wp_delete_range(WordProcessorState* wp, int start, int end);
void wp_backspace(WordProcessorState* wp); void wp_backspace(WordProcessorState* wp);
void wp_cursor_left(WordProcessorState* wp); void wp_cursor_left(WordProcessorState* wp);
void wp_cursor_right(WordProcessorState* wp); void wp_cursor_right(WordProcessorState* wp);
void wp_cursor_up(WordProcessorState* wp); void wp_cursor_up(WordProcessorState* wp);
void wp_cursor_down(WordProcessorState* wp); void wp_cursor_down(WordProcessorState* wp);
void wp_cursor_page(WordProcessorState* wp, int dy);
void wp_clear_selection(WordProcessorState* wp); void wp_clear_selection(WordProcessorState* wp);
void wp_sel_range(WordProcessorState* wp, int* out_start, int* out_end); void wp_sel_range(WordProcessorState* wp, int* out_start, int* out_end);
@@ -477,7 +484,9 @@ char* wp_selection_plain_text_alloc(WordProcessorState* wp, int* out_len);
bool wp_insert_text(WordProcessorState* wp, const char* text, int len); bool wp_insert_text(WordProcessorState* wp, const char* text, int len);
bool wp_replace_selection_with_text(WordProcessorState* wp, const char* text, int len); bool wp_replace_selection_with_text(WordProcessorState* wp, const char* text, int len);
void wp_recompute_wrap(WordProcessorState* wp, int content_w, int layout_dpi); void wp_recompute_wrap(WordProcessorState* wp, int content_w, int layout_dpi, bool paged = false);
void wp_layout_screen(WordProcessorState* wp);
int wp_paginate(WordProcessorState* wp, int page_h, int margin_y, int* line_pages, int* line_y);
int wp_find_wrap_line(WordProcessorState* wp, int abs_pos); int wp_find_wrap_line(WordProcessorState* wp, int abs_pos);
int wp_wrap_line_start(WordProcessorState* wp, int line_idx); int wp_wrap_line_start(WordProcessorState* wp, int line_idx);
void wp_ensure_cursor_visible(WordProcessorState* wp, int view_h); void wp_ensure_cursor_visible(WordProcessorState* wp, int view_h);
@@ -485,11 +494,14 @@ void wp_ensure_cursor_visible(WordProcessorState* wp, int view_h);
void wp_set_filepath(WordProcessorState* wp, const char* path); void wp_set_filepath(WordProcessorState* wp, const char* path);
void wp_start_pathbar(WordProcessorState* wp, bool save_mode, const char* initial_text); void wp_start_pathbar(WordProcessorState* wp, bool save_mode, const char* initial_text);
void wp_open_save_pathbar(WordProcessorState* wp); void wp_open_save_pathbar(WordProcessorState* wp);
void wp_save_file(WordProcessorState* wp); bool wp_save_file(WordProcessorState* wp);
void wp_save_file_as(WordProcessorState* wp, const char* path);
void wp_set_status(WordProcessorState* wp, const char* msg);
void wp_load_file(WordProcessorState* wp, const char* path); void wp_load_file(WordProcessorState* wp, const char* path);
void wp_history_reset(WordProcessorState* wp); void wp_history_reset(WordProcessorState* wp);
void wp_history_checkpoint(WordProcessorState* wp); void wp_history_checkpoint(WordProcessorState* wp);
void wp_history_checkpoint_typing(WordProcessorState* wp, char c);
void wp_history_mark_saved(WordProcessorState* wp); void wp_history_mark_saved(WordProcessorState* wp);
bool wp_undo(WordProcessorState* wp); bool wp_undo(WordProcessorState* wp);
bool wp_redo(WordProcessorState* wp); bool wp_redo(WordProcessorState* wp);