222 lines
7.1 KiB
Ziran
222 lines
7.1 KiB
Ziran
// Reusable line wrapping and vertical viewport for a Tk text widget.
|
|
#import "std/vec"
|
|
#import "std/number_text"
|
|
using UTF :: #import "tk/text"
|
|
|
|
ROW_HEIGHT :: 19;
|
|
INSET :: 6;
|
|
FRACTION :: 1000000;
|
|
|
|
Tag :: struct { name: Text; release: Text; underline: bool; }
|
|
Range :: struct { tag: s64; first: s64; last: s64; }
|
|
|
|
State :: struct {
|
|
rows: Vec(s64);
|
|
tags: Vec(Tag);
|
|
ranges: Vec(Range);
|
|
columns: s32;
|
|
visible: s32;
|
|
top: s32;
|
|
appended: bool;
|
|
}
|
|
|
|
FindTag :: (view: *State, name: string) -> s64 {
|
|
for * view.tags { if TextView(it.name) == name { return it_index } }
|
|
return -1;
|
|
}
|
|
|
|
EnsureTag :: (view: *State, name: string) -> s64 {
|
|
index := FindTag(view, name);
|
|
if index >= 0 { return index }
|
|
index = view.tags.count;
|
|
if name.count == 0 || index >= 4096 { return -1 }
|
|
tag := Tag.{name = TextCopy(name)};
|
|
if !VecPush(view.tags, tag) { return -1 }
|
|
return index;
|
|
}
|
|
|
|
AddRange :: (view: *State, tag: s64, first: s64, last: s64) -> bool {
|
|
if first >= last { return true }
|
|
if view.ranges.count >= 8192 { return false }
|
|
return VecPush(view.ranges, Range.{tag = tag, first = first, last = last});
|
|
}
|
|
|
|
ClearRanges :: (view: *State, tag: s64) {
|
|
kept: s64;
|
|
for * view.ranges {
|
|
if it.tag == tag { continue }
|
|
view.ranges[kept] = <<it;
|
|
kept += 1;
|
|
}
|
|
unused VecResize(view.ranges, kept);
|
|
}
|
|
|
|
Reset :: (view: *State) {
|
|
unused VecResize(view.rows, 0);
|
|
unused VecResize(view.ranges, 0);
|
|
view.columns = 0;
|
|
view.top = 0;
|
|
view.appended = false;
|
|
}
|
|
|
|
Clamp :: (view: *State) {
|
|
bottom := cast(s32)view.rows.count - view.visible;
|
|
if view.top > bottom { view.top = bottom }
|
|
if view.top < 0 { view.top = 0 }
|
|
}
|
|
|
|
Reflow :: (view: *State, source: string, columns: s32, visible: s32) {
|
|
view.visible = visible;
|
|
same_width := columns == view.columns;
|
|
if same_width && !view.appended { Clamp(view); return }
|
|
before: s64;
|
|
if view.top < view.rows.count { before = view.rows[view.top] }
|
|
at: s64;
|
|
if same_width && view.rows.count > 0 {
|
|
// Appending can change the wrap of the last row, but all earlier row offsets still apply.
|
|
last := view.rows.count - 1;
|
|
at = view.rows[last];
|
|
unused VecResize(view.rows, last);
|
|
} else {
|
|
unused VecResize(view.rows, 0);
|
|
view.top = 0;
|
|
}
|
|
view.columns = columns;
|
|
view.appended = false;
|
|
while at < source.count {
|
|
if at <= before { view.top = cast(s32)view.rows.count }
|
|
if !VecPush(view.rows, at) { break }
|
|
start := at;
|
|
space: s64 = -1;
|
|
cells: s32;
|
|
while at < source.count && source[at] != 10 && cells < columns {
|
|
if source[at] == 32 && at > start { space = at }
|
|
at = UTF.Next(source, at);
|
|
cells += 1;
|
|
}
|
|
if at >= source.count { break }
|
|
if source[at] == 10 { at += 1 }
|
|
else if source[at] == 32 { at += 1 }
|
|
else if space >= 0 { at = space + 1 }
|
|
}
|
|
Clamp(view);
|
|
}
|
|
|
|
Row :: (view: *State, source: string, row: s32) -> string {
|
|
at := view.rows[row];
|
|
end := source.count;
|
|
if row + 1 < view.rows.count { end = view.rows[row+1] }
|
|
if end > at && source[end-1] == 10 { end -= 1 }
|
|
return source[at:end];
|
|
}
|
|
|
|
// Coordinates are relative to the wrapped content, after the widget inset.
|
|
Point :: (view: *State, source: string, x: s32, y: s32, width: s32) -> (at: s64, on_text: bool) {
|
|
if view.rows.count == 0 { return 0, false }
|
|
row := ifx y < 0 then 0 else y/ROW_HEIGHT;
|
|
column := ifx x < 0 then 0 else x/width;
|
|
last := cast(s32)view.rows.count - view.top - 1;
|
|
if last >= view.visible { last = view.visible - 1 }
|
|
if row > last { return view.rows[view.top+last] + Row(view, source, view.top+last).count, false }
|
|
index := view.top + row;
|
|
line := Row(view, source, index);
|
|
within, _ := UTF.Offset(line, column);
|
|
return view.rows[index] + within, x >= 0 && y >= 0 && column < view.columns && within < line.count;
|
|
}
|
|
|
|
See :: (view: *State, at: s64) {
|
|
low: s64;
|
|
high := view.rows.count;
|
|
while low + 1 < high {
|
|
middle := (low+high)/2;
|
|
if view.rows[middle] <= at { low = middle } else { high = middle }
|
|
}
|
|
row := cast(s32)low;
|
|
if row < view.top { view.top = row }
|
|
else if row >= view.top+view.visible { view.top = row-view.visible+1 }
|
|
Clamp(view);
|
|
}
|
|
|
|
Offset :: (source: string, line: s64, column: s64) -> s64 {
|
|
at: s64;
|
|
while line > 1 && at < source.count {
|
|
if source[at] == 10 { line -= 1 }
|
|
at += 1;
|
|
}
|
|
while column > 0 && at < source.count && source[at] != 10 {
|
|
at = UTF.Next(source, at);
|
|
column -= 1;
|
|
}
|
|
return at;
|
|
}
|
|
|
|
Index :: (source: string, at: s64, output: *Vec(u8)) {
|
|
line: s64 = 1;
|
|
start: s64;
|
|
for i: 0..at-1 {
|
|
if source[i] == 10 { line += 1; start = i+1 }
|
|
}
|
|
digits: [24]u8;
|
|
unused BuilderAppend(<<output, FormatInteger(line, digits[:]));
|
|
unused BuilderAppend(<<output, ".");
|
|
unused BuilderAppend(<<output, FormatInteger(UTF.Count(source[start:at]), digits[:]));
|
|
}
|
|
|
|
Match :: (source: string, pattern: string, start: s64, stop: s64, backwards: bool) -> s64 {
|
|
if pattern.count == 0 { return -1 }
|
|
at := start;
|
|
if backwards {
|
|
while at > stop {
|
|
at = UTF.Previous(source, at);
|
|
if at + pattern.count <= source.count && source[at] == pattern[0] && source[at:at+pattern.count] == pattern { return at }
|
|
}
|
|
} else {
|
|
while at < stop && at + pattern.count <= source.count {
|
|
if source[at] == pattern[0] && source[at:at+pattern.count] == pattern { return at }
|
|
at = UTF.Next(source, at);
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
Move :: (view: *State, lines: s64) {
|
|
next := cast(s64)view.top + lines;
|
|
if next < 0 { next = 0 }
|
|
if next > view.rows.count { next = view.rows.count }
|
|
view.top = cast(s32)next;
|
|
Clamp(view);
|
|
}
|
|
|
|
PageSize :: (view: *State) -> s32 { return ifx view.visible > 1 then view.visible-1 else 1 }
|
|
|
|
AddFraction :: (output: *Vec(u8), value: s64) {
|
|
if value >= FRACTION { unused BuilderAppend(<<output, "1"); return }
|
|
unused BuilderAppend(<<output, "0.");
|
|
digits: [24]u8;
|
|
number := FormatInteger(FRACTION + value, digits[:]);
|
|
unused BuilderAppend(<<output, number[1:]);
|
|
}
|
|
|
|
Position :: (view: *State, output: *Vec(u8)) {
|
|
if view.rows.count == 0 { unused BuilderAppend(<<output, "0 1"); return }
|
|
AddFraction(output, cast(s64)view.top * FRACTION / view.rows.count);
|
|
unused BuilderAppend(<<output, " ");
|
|
AddFraction(output, cast(s64)(view.top + view.visible) * FRACTION / view.rows.count);
|
|
}
|
|
|
|
Moveto :: (view: *State, fraction: string) -> bool {
|
|
if fraction == "1" || fraction == "1.0" { Move(view, view.rows.count); return true }
|
|
if fraction == "0" { view.top = 0; return true }
|
|
if fraction.count < 3 || fraction.count > 8 || fraction[0:2] != "0." { return false }
|
|
value: s64;
|
|
for i: 2..fraction.count-1 {
|
|
digit := fraction[i];
|
|
if digit < 48 || digit > 57 { return false }
|
|
value = value*10 + cast(s64)(digit-48);
|
|
}
|
|
for fraction.count..7 { value *= 10 }
|
|
view.top = cast(s32)((value * view.rows.count + FRACTION/2) / FRACTION);
|
|
Clamp(view);
|
|
return true;
|
|
}
|