Embed Workbook's own grid in other apps
workbook/grid draws the editor's column letters, row numbers, cell squares, theme colors and sheet tabs for a real workbook from workbook/engine, read-only, with column widths and count or money formats. The editor paints its grid through the same squares. It replaces workbook/sheet_table.
This commit is contained in:
1 parent
ff3e9f7d1b
commit
182515eab9
9 files changed
+528
-630
No files matched your search
+5
-2
@@ -2,8 +2,11 @@
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
- Add `workbook/sheet_table`, a read-only table other apps embed: counts,
|
||||
money, totals row, fitted columns, screen layout and CSV export.
|
||||
- Other Kryon apps embed Workbook's own grid with `workbook/grid` and
|
||||
`workbook/engine`: real cells and formulas, sheet tabs, column widths and
|
||||
count or money formats. The editor paints with the same grid squares. It
|
||||
replaces the short-lived `workbook/sheet_table`.
|
||||
- Workbook now lives at github.com/kryonlabs/workbook.
|
||||
- Hide selected rows from the row-number menu and click their thin lines to
|
||||
restore them, with undo and redo support.
|
||||
- Use `SUBTOTAL(109,…)` to leave hidden rows out of totals while preserving
|
||||
|
||||
@@ -11,7 +11,7 @@ DIST_DIR ?= dist
|
||||
|
||||
APP := build/workbook-desktop
|
||||
CELL := build/cell
|
||||
TESTS := gnumeric_test source_audit_test structure_test conversion_test differential_fuzz desktop_test parity_test service_test sheet_table_test
|
||||
TESTS := gnumeric_test source_audit_test structure_test conversion_test differential_fuzz desktop_test parity_test service_test grid_test
|
||||
TEST_PROGRAMS := $(TESTS:%=build/%)
|
||||
SOURCES := $(wildcard src/*.zi) ziran.toml ziran.lock
|
||||
|
||||
@@ -20,7 +20,7 @@ SOURCES := $(wildcard src/*.zi) ziran.toml ziran.lock
|
||||
# has the same layout.
|
||||
NATIVE_FLAGS := -std=c99 -O2 -ffunction-sections -fdata-sections -Wl,--gc-sections
|
||||
|
||||
.PHONY: build cell check run test test-ci sheet-table-test structure-test conversion-test gnumeric-test desktop-test hidden-rows-test parity fuzz audit install deb clean
|
||||
.PHONY: build cell check run test test-ci grid-test structure-test conversion-test gnumeric-test desktop-test hidden-rows-test parity fuzz audit install deb clean
|
||||
|
||||
build: $(APP) $(CELL)
|
||||
|
||||
@@ -67,7 +67,7 @@ desktop-test: build build/desktop_test
|
||||
hidden-rows-test: build build/desktop_test
|
||||
@env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS YUE_DESKTOP_RECOVERY=0 GSETTINGS_BACKEND=memory build/desktop_test --hidden-only
|
||||
|
||||
test-ci: build service-test sheet-table-test audit structure-test conversion-test gnumeric-test desktop-test
|
||||
test-ci: build service-test grid-test audit structure-test conversion-test gnumeric-test desktop-test
|
||||
|
||||
test: test-ci parity
|
||||
|
||||
@@ -138,9 +138,9 @@ deb: build
|
||||
clean:
|
||||
rm -rf build
|
||||
|
||||
# The read-only table other apps embed; see src/sheet_table.zi.
|
||||
sheet-table-test: build/sheet_table_test
|
||||
@build/sheet_table_test
|
||||
# The grid other apps embed; see src/grid.zi.
|
||||
grid-test: build/grid_test
|
||||
@build/grid_test
|
||||
|
||||
structure-test: build/structure_test
|
||||
@build/structure_test
|
||||
|
||||
@@ -158,28 +158,30 @@ finance JSON untouched and start with an empty sheet when no saved document exis
|
||||
git. `workbook.example.json` is safe sample data for demos.
|
||||
|
||||
|
||||
## Embedded tables
|
||||
## Embedded grid
|
||||
|
||||
Other Ziran apps can show a read-only, CSV-like table with the
|
||||
`workbook/sheet_table` export. It holds typed columns (text, counts, money)
|
||||
and rows of plain decimal text, adds a totals row, formats numbers, fits the
|
||||
column widths to the content, lays out the cells of one screen and writes
|
||||
CSV. The host app draws the cells, so the module needs no UI toolkit.
|
||||
Other Kryon apps embed Workbook's grid with the `workbook/grid` export: the
|
||||
editor's column letters, row numbers, cell squares, theme colors and sheet tabs,
|
||||
showing a real workbook from `workbook/engine`. Cells hold numbers, text and
|
||||
formulas such as `=SUM(B2:B9)`; columns can have their own width and a count
|
||||
(`1,250,734`) or money (`$12.85`) format. The view is read-only and scrolls with
|
||||
the wheel. The Workbook editor paints its grid with the same squares.
|
||||
|
||||
```jai
|
||||
using Sheet :: #import "workbook/sheet_table";
|
||||
table: SheetTable
|
||||
SheetAddColumn(*table, "Folder", SheetText)
|
||||
SheetAddColumn(*table, "Tokens", SheetNumber)
|
||||
SheetAddColumn(*table, "Cost", SheetMoney)
|
||||
table.total_label = "Total"
|
||||
row := SheetAddRow(*table)
|
||||
SheetSet(*table, row, 0, "aurea"); SheetSet(*table, row, 1, "4795296906"); SheetSet(*table, row, 2, "1475.75")
|
||||
SheetFit(*table, Measure, 13.0, 8.0, width)
|
||||
cells := SheetLayout(*table, x, y, 28.0, 30.0, height, first_row)
|
||||
Engine :: #import "workbook/engine";
|
||||
using Grid :: #import "workbook/grid";
|
||||
book := Engine.WorkbookNew()
|
||||
sheet := GridAddSheet(book, "Usage")
|
||||
unused GridSet(book, sheet, 0, 0, "Folder"); unused GridSet(book, sheet, 0, 1, "Tokens")
|
||||
unused GridSet(book, sheet, 1, 0, "aurea"); unused GridSet(book, sheet, 1, 1, "4795296906")
|
||||
unused GridSet(book, sheet, 2, 0, "Total"); unused GridSet(book, sheet, 2, 1, "=SUM(B2:B2)")
|
||||
Engine.WorkbookEvaluate(book)
|
||||
view: GridView
|
||||
view.key = 5000000; view.widths[0] = 180.0; view.formats[1] = GridCount
|
||||
GridDraw(session, bounds, book, *view, GridSample(session, bounds))
|
||||
```
|
||||
|
||||
`make sheet-table-test` checks it.
|
||||
`make grid-test` checks it.
|
||||
|
||||
|
||||
## Werkzeug app bundle
|
||||
|
||||
+34
-70
@@ -33,6 +33,8 @@
|
||||
Picker :: #import "kryon/file_picker";
|
||||
}
|
||||
Json :: #import "json_scan";
|
||||
// The grid's metrics, colors and cell squares, shared with embedded grids.
|
||||
Grid :: #import "grid";
|
||||
#if !#defined(KRYON_HOSTED_UI) {
|
||||
SystemStyle :: #import "kss/SystemStyle";
|
||||
SystemThemeLinux :: #import "SystemThemeLinux";
|
||||
@@ -83,11 +85,6 @@ using TextWrap;
|
||||
MaxRows :: 16777216; // GNM_MAX_ROWS
|
||||
MaxColumns :: 16384; // GNM_MAX_COLS
|
||||
CellTextMax :: 256;
|
||||
ColumnWidth :: 96.0;
|
||||
RowHeight :: 22.0;
|
||||
HiddenRowHeight :: 6.0;
|
||||
HeaderHeight :: 24.0;
|
||||
RowHeaderWidth :: 40.0;
|
||||
TableTop :: 86.0;
|
||||
Margin :: 0.0;
|
||||
// Distances from the window's bottom edge to the sheet tabs and the grid.
|
||||
@@ -1424,7 +1421,7 @@ visible_columns: s32 = 8;
|
||||
grid_body_height: float32 = 352.0;
|
||||
|
||||
RowHidden :: (row: s32) -> bool { return Engine.WorkbookRowHidden(workbook, sheet_index, row) != 0 }
|
||||
GridRowHeight :: (row: s32) -> float32 { return ifx RowHidden(row) then HiddenRowHeight else RowHeight }
|
||||
GridRowHeight :: (row: s32) -> float32 { return ifx RowHidden(row) then Grid.GridHiddenRowHeight else Grid.GridRowHeight }
|
||||
|
||||
NextVisibleRow :: (row: s32, step: s32) -> s32 {
|
||||
next: s32 = row + step
|
||||
@@ -1434,7 +1431,7 @@ NextVisibleRow :: (row: s32, step: s32) -> s32 {
|
||||
|
||||
GridRowAt :: (table: Rectangle, y: float32) -> s32 {
|
||||
row: s32 = scroll_row
|
||||
top: float32 = table.y + HeaderHeight
|
||||
top: float32 = table.y + Grid.GridHeaderHeight
|
||||
while row < scroll_row + visible_rows - 1 && y >= top + GridRowHeight(row) {
|
||||
top += GridRowHeight(row)
|
||||
row += 1
|
||||
@@ -1461,7 +1458,7 @@ last_click_column: s32 = -1;
|
||||
EnsureVisible :: () {
|
||||
if selected_row < scroll_row { scroll_row = selected_row }
|
||||
if selected_row >= scroll_row + visible_rows {
|
||||
scroll_row = Max(0, selected_row - Max(1, cast(s32)(grid_body_height / RowHeight)) + 1)
|
||||
scroll_row = Max(0, selected_row - Max(1, cast(s32)(grid_body_height / Grid.GridRowHeight)) + 1)
|
||||
}
|
||||
if selected_column < scroll_column { scroll_column = selected_column }
|
||||
if selected_column >= scroll_column + visible_columns {
|
||||
@@ -1474,9 +1471,9 @@ DrawGrid :: (session: Session, viewport: Rectangle) {
|
||||
viewport.width - Margin * 2.0, viewport.height - TableTop - GridBottom}
|
||||
grid_right = table.x + table.width
|
||||
grid_bottom = table.y + table.height
|
||||
grid_body_height = table.height - HeaderHeight
|
||||
visible_rows = cast(s32)((grid_body_height + RowHeight - 1.0) / RowHeight)
|
||||
visible_columns = cast(s32)((table.width - RowHeaderWidth + ColumnWidth - 1.0) / ColumnWidth)
|
||||
grid_body_height = table.height - Grid.GridHeaderHeight
|
||||
visible_rows = cast(s32)((grid_body_height + Grid.GridRowHeight - 1.0) / Grid.GridRowHeight)
|
||||
visible_columns = cast(s32)((table.width - Grid.GridRowHeaderWidth + Grid.GridColumnWidth - 1.0) / Grid.GridColumnWidth)
|
||||
rows: s32 = GridRows()
|
||||
columns: s32 = GridColumns()
|
||||
|
||||
@@ -1512,14 +1509,14 @@ DrawGrid :: (session: Session, viewport: Rectangle) {
|
||||
last_column: s32 = SelectionLastColumn()
|
||||
spans: bool = selection_axis == 0 && (first_row != last_row || first_column != last_column)
|
||||
|
||||
GridSquare(session, 10, Rectangle.{table.x, table.y, RowHeaderWidth, HeaderHeight},
|
||||
GridSquare(session, 10, Rectangle.{table.x, table.y, Grid.GridRowHeaderWidth, Grid.GridHeaderHeight},
|
||||
HeaderBackground(), "")
|
||||
column: s32 = scroll_column
|
||||
while column < scroll_column + visible_columns && column < columns {
|
||||
name: [8]u8
|
||||
AppendCellColumn(name[:], column)
|
||||
GridSquare(session, 20 + column - scroll_column, Rectangle.{ColumnX(table, column), table.y,
|
||||
ColumnWidth, HeaderHeight}, ifx here && selection_axis == 2 && column == selected_column then SelectedBackground() else HeaderBackground(), TextUntilNul(name[:]))
|
||||
Grid.GridColumnWidth, Grid.GridHeaderHeight}, ifx here && selection_axis == 2 && column == selected_column then SelectedBackground() else HeaderBackground(), TextUntilNul(name[:]))
|
||||
column += 1
|
||||
}
|
||||
shown: s32 = 0
|
||||
@@ -1527,7 +1524,7 @@ DrawGrid :: (session: Session, viewport: Rectangle) {
|
||||
row: s32 = scroll_row + shown
|
||||
y: float32 = RowY(table, row)
|
||||
if RowHidden(row) {
|
||||
GridSquare(session, 1000 + shown, Rectangle.{table.x, y, table.width, HiddenRowHeight}, HeaderBackground(), "")
|
||||
GridSquare(session, 1000 + shown, Rectangle.{table.x, y, table.width, Grid.GridHiddenRowHeight}, HeaderBackground(), "")
|
||||
strip: BoxProps
|
||||
strip.key = cast(u64)(500000 + shown)
|
||||
strip.bounds = Rectangle.{table.x, y + 2.0, table.width, 1.0}
|
||||
@@ -1539,7 +1536,7 @@ DrawGrid :: (session: Session, viewport: Rectangle) {
|
||||
number: [16]u8
|
||||
row_digits: [16]u8
|
||||
SetText(number[:], FormatInteger(cast(s64)row + 1, row_digits[:]))
|
||||
GridSquare(session, 1000 + shown, Rectangle.{table.x, y, RowHeaderWidth, RowHeight},
|
||||
GridSquare(session, 1000 + shown, Rectangle.{table.x, y, Grid.GridRowHeaderWidth, Grid.GridRowHeight},
|
||||
ifx here && selection_axis == 1 && row == selected_row then SelectedBackground() else HeaderBackground(), TextUntilNul(number[:]))
|
||||
column = scroll_column
|
||||
while column < scroll_column + visible_columns && column < columns {
|
||||
@@ -1557,7 +1554,7 @@ DrawGrid :: (session: Session, viewport: Rectangle) {
|
||||
in_columns: bool = selection_axis == 1 || (column >= first_column && column <= last_column)
|
||||
if here && in_rows && in_columns && !(spans && active) { fill = SelectedBackground() }
|
||||
GridSquare(session, 10000 + shown * visible_columns + column - scroll_column,
|
||||
Rectangle.{ColumnX(table, column), y, ColumnWidth, RowHeight},
|
||||
Rectangle.{ColumnX(table, column), y, Grid.GridColumnWidth, Grid.GridRowHeight},
|
||||
fill, TextUntilNul(display[:]))
|
||||
column += 1
|
||||
}
|
||||
@@ -1588,10 +1585,10 @@ DrawGrid :: (session: Session, viewport: Rectangle) {
|
||||
|
||||
if menus_block_input { return }
|
||||
pointer: PointerFrame = EditorPointer(session)
|
||||
body: Rectangle = Rectangle.{table.x + RowHeaderWidth, table.y + HeaderHeight,
|
||||
table.width - RowHeaderWidth, table.height - HeaderHeight}
|
||||
body: Rectangle = Rectangle.{table.x + Grid.GridRowHeaderWidth, table.y + Grid.GridHeaderHeight,
|
||||
table.width - Grid.GridRowHeaderWidth, table.height - Grid.GridHeaderHeight}
|
||||
hit_row: s32 = GridRowAt(table, pointer.y)
|
||||
hit_column: s32 = scroll_column + cast(s32)((pointer.x - body.x) / ColumnWidth)
|
||||
hit_column: s32 = scroll_column + cast(s32)((pointer.x - body.x) / Grid.GridColumnWidth)
|
||||
on_cell: bool = PointInBounds(pointer.x, pointer.y, body) && hit_row < rows &&
|
||||
hit_row < scroll_row + visible_rows && hit_column < columns && hit_column < scroll_column + visible_columns
|
||||
if pointer.pressed && !grid_menu_open {
|
||||
@@ -1649,7 +1646,7 @@ DrawGrid :: (session: Session, viewport: Rectangle) {
|
||||
if pointer.y > body.y { row = GridRowAt(table, pointer.y) }
|
||||
row = Min(row, Min(scroll_row + visible_rows - 1, MaxRows - 1))
|
||||
column = scroll_column
|
||||
if pointer.x > body.x { column = scroll_column + cast(s32)((pointer.x - body.x) / ColumnWidth) }
|
||||
if pointer.x > body.x { column = scroll_column + cast(s32)((pointer.x - body.x) / Grid.GridColumnWidth) }
|
||||
column = Min(column, Min(scroll_column + visible_columns - 1, MaxColumns - 1))
|
||||
if drag_mode == DragRange && !edit_focused && here {
|
||||
selection_axis = 0
|
||||
@@ -1685,7 +1682,7 @@ DrawGrid :: (session: Session, viewport: Rectangle) {
|
||||
// The secondary button opens the menu; outside the selection it first
|
||||
// selects the cell under it.
|
||||
row: s32 = GridRowAt(table, pointer.y)
|
||||
clicked: s32 = scroll_column + cast(s32)((pointer.x - body.x) / ColumnWidth)
|
||||
clicked: s32 = scroll_column + cast(s32)((pointer.x - body.x) / Grid.GridColumnWidth)
|
||||
if row < rows && !RowHidden(row) && clicked < columns && clicked < scroll_column + visible_columns {
|
||||
inside: bool = here && selection_axis == 0 && row >= first_row && row <= last_row &&
|
||||
clicked >= first_column && clicked <= last_column
|
||||
@@ -1709,8 +1706,8 @@ FillHandleBounds :: (table: Rectangle) -> Rectangle {
|
||||
if RowHidden(row) { return none }
|
||||
if row < scroll_row || row >= scroll_row + visible_rows ||
|
||||
column < scroll_column || column >= scroll_column + visible_columns { return none }
|
||||
x: float32 = ColumnX(table, column) + ColumnWidth
|
||||
y: float32 = RowY(table, row) + RowHeight
|
||||
x: float32 = ColumnX(table, column) + Grid.GridColumnWidth
|
||||
y: float32 = RowY(table, row) + Grid.GridRowHeight
|
||||
return Rectangle.{x - 6.0, y - 6.0, 11.0, 11.0}
|
||||
}
|
||||
|
||||
@@ -1799,12 +1796,12 @@ DrawCellEditor :: (session: Session, table: Rectangle) {
|
||||
x: float32 = ColumnX(table, selected_column)
|
||||
y: float32 = RowY(table, selected_row)
|
||||
width: float32 = cast(float32)(EditorGlyphWidth(text) + 16)
|
||||
if width < ColumnWidth { width = ColumnWidth }
|
||||
if width < Grid.GridColumnWidth { width = Grid.GridColumnWidth }
|
||||
limit: float32 = ColumnX(table, scroll_column + visible_columns) - x
|
||||
if width > limit { width = limit }
|
||||
box: BoxProps
|
||||
box.key = cast(u64)400500
|
||||
box.bounds = Rectangle.{x, y, width, RowHeight}
|
||||
box.bounds = Rectangle.{x, y, width, Grid.GridRowHeight}
|
||||
box.fill = CellBackground()
|
||||
box.border = AccentColor()
|
||||
Box(session, box)
|
||||
@@ -1813,14 +1810,14 @@ DrawCellEditor :: (session: Session, table: Rectangle) {
|
||||
label.text = text
|
||||
label.font = Text14
|
||||
label.wrap = cast(TextWrap)TextWrapNone
|
||||
label.bounds = Rectangle.{x + 4.0, y + 3.0, width - 8.0, RowHeight - 4.0}
|
||||
label.bounds = Rectangle.{x + 4.0, y + 3.0, width - 8.0, Grid.GridRowHeight - 4.0}
|
||||
Text(session, label)
|
||||
cursor: s32 = Min(edit_cursor, cast(s32)text.count)
|
||||
caret_x: float32 = x + 4.0 + cast(float32)EditorGlyphWidth(text[0:cursor])
|
||||
if caret_x < x + width - 2.0 {
|
||||
caret: BoxProps
|
||||
caret.key = cast(u64)400502
|
||||
caret.bounds = Rectangle.{caret_x, y + 5.0, 1.0, RowHeight - 10.0}
|
||||
caret.bounds = Rectangle.{caret_x, y + 5.0, 1.0, Grid.GridRowHeight - 10.0}
|
||||
caret.fill = AccentColor()
|
||||
caret.border = AccentColor()
|
||||
Box(session, caret)
|
||||
@@ -1838,7 +1835,7 @@ CellOutline :: (session: Session, key: s32, table: Rectangle, first_row: s32, fi
|
||||
if top > bottom || left > right { return }
|
||||
x: float32 = ColumnX(table, left)
|
||||
y: float32 = RowY(table, top)
|
||||
width: float32 = cast(float32)(right - left + 1) * ColumnWidth
|
||||
width: float32 = cast(float32)(right - left + 1) * Grid.GridColumnWidth
|
||||
height: float32 = RowY(table, bottom) + GridRowHeight(bottom) - y
|
||||
width = MinFloat(width, grid_right - x)
|
||||
height = MinFloat(height, grid_bottom - y)
|
||||
@@ -1943,18 +1940,18 @@ GridCommand :: (id: s32) {
|
||||
}
|
||||
|
||||
// The grid takes its colors from the installed style, so it follows the theme.
|
||||
HeaderBackground :: () -> Color { return EditorColor("surface") }
|
||||
CellBackground :: () -> Color { return EditorColor("card") }
|
||||
SelectedBackground :: () -> Color { return EditorColor("accent-soft") }
|
||||
AccentColor :: () -> Color { return EditorColor("accent") }
|
||||
GridLineColor :: () -> Color { return EditorColor("border-soft") }
|
||||
HeaderBackground :: () -> Color { return Grid.GridHeaderBackground() }
|
||||
CellBackground :: () -> Color { return Grid.GridCellBackground() }
|
||||
SelectedBackground :: () -> Color { return Grid.GridSelectedBackground() }
|
||||
AccentColor :: () -> Color { return Grid.GridAccentColor() }
|
||||
GridLineColor :: () -> Color { return Grid.GridLineColor() }
|
||||
|
||||
ColumnX :: (table: Rectangle, column: s32) -> float32 {
|
||||
return table.x + RowHeaderWidth + cast(float32)(column - scroll_column) * ColumnWidth
|
||||
return table.x + Grid.GridRowHeaderWidth + cast(float32)(column - scroll_column) * Grid.GridColumnWidth
|
||||
}
|
||||
|
||||
RowY :: (table: Rectangle, row: s32) -> float32 {
|
||||
y: float32 = table.y + HeaderHeight
|
||||
y: float32 = table.y + Grid.GridHeaderHeight
|
||||
at: s32 = scroll_row
|
||||
while at < row { y += GridRowHeight(at); at += 1 }
|
||||
return y
|
||||
@@ -1962,24 +1959,7 @@ RowY :: (table: Rectangle, row: s32) -> float32 {
|
||||
|
||||
// One grid square: a bordered box and its clipped text.
|
||||
GridSquare :: (session: Session, key: s32, bounds: Rectangle, fill: Color, text: string) {
|
||||
if bounds.x + bounds.width > grid_right { bounds.width = grid_right - bounds.x }
|
||||
if bounds.y + bounds.height > grid_bottom { bounds.height = grid_bottom - bounds.y }
|
||||
if bounds.width <= 0.0 || bounds.height <= 0.0 { return }
|
||||
box: BoxProps
|
||||
box.key = 1000000 + cast(u64)key * 2
|
||||
box.bounds = bounds
|
||||
box.fill = fill
|
||||
box.border = GridLineColor()
|
||||
Box(session, box)
|
||||
if text.count == 0 { return }
|
||||
label: TextProps
|
||||
label.key = 1000001 + cast(u64)key * 2
|
||||
label.text = text
|
||||
label.font = Text14
|
||||
label.wrap = cast(TextWrap)TextWrapNone
|
||||
label.bounds = Rectangle.{bounds.x + 4.0, bounds.y + 3.0,
|
||||
bounds.width - 8.0, bounds.height - 4.0}
|
||||
Text(session, label)
|
||||
Grid.GridSquare(session, 1000000 + cast(u64)key * 2, bounds, grid_right, grid_bottom, fill, text)
|
||||
}
|
||||
|
||||
|
||||
@@ -2389,23 +2369,7 @@ DrawMenus :: (session: Session, viewport: Rectangle) {
|
||||
EditorClipboardHasText :: () -> bool { return SystemClipboardHasText() }
|
||||
}
|
||||
|
||||
EditorColor :: (name: string) -> Color {
|
||||
#if #defined(KRYON_HOSTED_UI) {
|
||||
accent := Theme.AppThemeColor("focus-ring", Color.{37, 99, 235, 255})
|
||||
if name == "accent" { return accent }
|
||||
if name == "border-soft" { return Theme.AppThemeColor("border", Color.{208, 216, 224, 255}) }
|
||||
if name == "surface" { return Theme.AppThemeColor("button", Color.{236, 239, 243, 255}) }
|
||||
if name == "accent-soft" {
|
||||
base := Theme.AppThemeColor("card", Color.{255, 255, 255, 255})
|
||||
return Color.{cast(u8)((cast(s32)base.r * 4 + cast(s32)accent.r) / 5),
|
||||
cast(u8)((cast(s32)base.g * 4 + cast(s32)accent.g) / 5),
|
||||
cast(u8)((cast(s32)base.b * 4 + cast(s32)accent.b) / 5), 255}
|
||||
}
|
||||
return Theme.AppThemeColor(name, Color.{245, 245, 245, 255})
|
||||
} else {
|
||||
return StyleTokenColor(name)
|
||||
}
|
||||
}
|
||||
EditorColor :: (name: string) -> Color { return Grid.GridColor(name) }
|
||||
|
||||
EditorMenu :: (session: Session, props: MenuProps, groups: []MenuGroup, state: *MenuState) -> MenuResult {
|
||||
#if #defined(KRYON_HOSTED_UI) {
|
||||
|
||||
+403
@@ -0,0 +1,403 @@
|
||||
// Workbook's cell grid for any Kryon app: the editor's metrics, theme colors
|
||||
// and cell squares, and a read-only view of a workbook with column letters,
|
||||
// row numbers, the cells of one sheet and its sheet tabs. The Workbook editor
|
||||
// paints its own grid with the same squares and colors.
|
||||
#if #defined(KRYON_HOSTED_UI) {
|
||||
#import "geometry"
|
||||
#import "session"
|
||||
#import "drawing_props"
|
||||
#import "box_props"
|
||||
#import "box_widget"
|
||||
#import "text_props"
|
||||
#import "text_align"
|
||||
#import "text_widget"
|
||||
#import "tab_bar_props"
|
||||
#import "tab_bar_widget"
|
||||
Engine :: #import "portable_engine";
|
||||
Theme :: #import "kryon/app_theme";
|
||||
} else {
|
||||
using UI :: #import "kryon/Widgets";
|
||||
Engine :: #import "engine";
|
||||
#import "std/c_string"
|
||||
}
|
||||
#import "std/vec"
|
||||
#import "std/byte_text_linux"
|
||||
#import "std/text_buffer"
|
||||
|
||||
GridColumnWidth :: 96.0;
|
||||
GridRowHeight :: 22.0;
|
||||
GridHiddenRowHeight :: 6.0;
|
||||
GridHeaderHeight :: 24.0;
|
||||
GridRowHeaderWidth :: 40.0;
|
||||
GridTabsHeight :: 28.0;
|
||||
// Columns with their own width or number format.
|
||||
GridColumnsMax :: 64;
|
||||
|
||||
// Number formats of a column: as calculated, a whole count with thousands
|
||||
// separators, or dollars and cents.
|
||||
GridGeneral :: 0;
|
||||
GridCount :: 1;
|
||||
GridMoney :: 2;
|
||||
|
||||
// What a host keeps for one embedded grid between frames.
|
||||
GridView :: struct {
|
||||
// The first widget key; the view's widgets use keys from here up.
|
||||
key: u64;
|
||||
sheet: s32;
|
||||
scroll_row: s32;
|
||||
scroll_column: s32;
|
||||
// Zero keeps GridColumnWidth.
|
||||
widths: [64]float32;
|
||||
formats: [64]s32;
|
||||
// Filled in by GridDraw: the rows and columns that fit.
|
||||
visible_rows: s32;
|
||||
visible_columns: s32;
|
||||
}
|
||||
|
||||
// The host's pointer for this frame; wheel is positive when scrolled up.
|
||||
GridInput :: struct {
|
||||
x: float32;
|
||||
y: float32;
|
||||
wheel: float32;
|
||||
}
|
||||
|
||||
// The grid takes its colors from the installed style, so it follows the theme.
|
||||
GridColor :: (name: string) -> Color {
|
||||
#if #defined(KRYON_HOSTED_UI) {
|
||||
accent := Theme.AppThemeColor("focus-ring", Color.{37, 99, 235, 255})
|
||||
if name == "accent" { return accent }
|
||||
if name == "border-soft" { return Theme.AppThemeColor("border", Color.{208, 216, 224, 255}) }
|
||||
if name == "surface" { return Theme.AppThemeColor("button", Color.{236, 239, 243, 255}) }
|
||||
if name == "accent-soft" {
|
||||
base := Theme.AppThemeColor("card", Color.{255, 255, 255, 255})
|
||||
return Color.{cast(u8)((cast(s32)base.r * 4 + cast(s32)accent.r) / 5),
|
||||
cast(u8)((cast(s32)base.g * 4 + cast(s32)accent.g) / 5),
|
||||
cast(u8)((cast(s32)base.b * 4 + cast(s32)accent.b) / 5), 255}
|
||||
}
|
||||
return Theme.AppThemeColor(name, Color.{245, 245, 245, 255})
|
||||
} else {
|
||||
return StyleTokenColor(name)
|
||||
}
|
||||
}
|
||||
GridHeaderBackground :: () -> Color { return GridColor("surface") }
|
||||
GridCellBackground :: () -> Color { return GridColor("card") }
|
||||
GridSelectedBackground :: () -> Color { return GridColor("accent-soft") }
|
||||
GridAccentColor :: () -> Color { return GridColor("accent") }
|
||||
GridLineColor :: () -> Color { return GridColor("border-soft") }
|
||||
|
||||
// One grid square: a bordered box and its text, clipped at right and bottom.
|
||||
// It uses two widget keys, key and key + 1.
|
||||
GridSquare :: (session: Session, key: u64, bounds: Rectangle, right: float32, bottom: float32,
|
||||
fill: Color, text: string, align_end: bool = false) {
|
||||
if bounds.x + bounds.width > right { bounds.width = right - bounds.x }
|
||||
if bounds.y + bounds.height > bottom { bounds.height = bottom - bounds.y }
|
||||
if bounds.width <= 0.0 || bounds.height <= 0.0 { return }
|
||||
box: BoxProps
|
||||
box.key = key
|
||||
box.bounds = bounds
|
||||
box.fill = fill
|
||||
box.border = GridLineColor()
|
||||
Box(session, box)
|
||||
if text.count == 0 { return }
|
||||
label: TextProps
|
||||
label.key = key + 1
|
||||
label.text = text
|
||||
label.font = Text14
|
||||
label.wrap = TextWrap.TextWrapNone
|
||||
if align_end { label.align = TextAlign.TextAlignEnd }
|
||||
label.bounds = Rectangle.{bounds.x + 4.0, bounds.y + 3.0,
|
||||
bounds.width - 8.0, bounds.height - 4.0}
|
||||
Text(session, label)
|
||||
}
|
||||
|
||||
// Column letters: A to Z, then AA to ZZ.
|
||||
GridColumnName :: (column: s32) -> string {
|
||||
letters: [2]u8
|
||||
out: Vec(u8)
|
||||
if column < 26 {
|
||||
letters[0] = cast(u8)(65 + column)
|
||||
BuilderAppend(out, TextView(letters[0:1]))
|
||||
} else {
|
||||
letters[0] = cast(u8)(65 + column / 26 - 1)
|
||||
letters[1] = cast(u8)(65 + column % 26)
|
||||
BuilderAppend(out, TextView(letters[0:2]))
|
||||
}
|
||||
return BuilderFinish(out)
|
||||
}
|
||||
|
||||
GridWidth :: (view: *GridView, column: s32) -> float32 {
|
||||
if column >= 0 && column < GridColumnsMax && view.widths[column] > 0.0 { return view.widths[column] }
|
||||
return GridColumnWidth
|
||||
}
|
||||
|
||||
// The left edge of a shown column in a grid whose row numbers start at x.
|
||||
GridColumnX :: (view: *GridView, x: float32, column: s32) -> float32 {
|
||||
at := x + GridRowHeaderWidth
|
||||
index := view.scroll_column
|
||||
while index < column { at += GridWidth(view, index); index += 1 }
|
||||
return at
|
||||
}
|
||||
|
||||
GridFormat :: (view: *GridView, column: s32) -> s32 {
|
||||
return ifx column >= 0 && column < GridColumnsMax then view.formats[column] else GridGeneral
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ numbers
|
||||
|
||||
GridScale :: 1000000;
|
||||
|
||||
// Decimal text in millionths, rounded half up; no exponents.
|
||||
GridMicros :: (text: string) -> (value: s64, ok: bool) {
|
||||
if text == "" { return 0, false }
|
||||
at: s64 = 0
|
||||
negative := false
|
||||
if text[0] == 45 || text[0] == 43 {
|
||||
negative = text[0] == 45
|
||||
at = 1
|
||||
}
|
||||
whole: s64 = 0
|
||||
fraction: s64 = 0
|
||||
kept: s32 = 0
|
||||
digits: s32 = 0
|
||||
point := false
|
||||
up := false
|
||||
while at < text.count {
|
||||
c := text[at]
|
||||
at += 1
|
||||
if c == 46 && !point {
|
||||
point = true
|
||||
continue
|
||||
}
|
||||
if c < 48 || c > 57 { return 0, false }
|
||||
digit := cast(s64)(c - 48)
|
||||
digits += 1
|
||||
if !point {
|
||||
if whole > 922337203684 { return 0, false }
|
||||
whole = whole * 10 + digit
|
||||
} else if kept < 6 {
|
||||
fraction = fraction * 10 + digit
|
||||
kept += 1
|
||||
} else if kept == 6 {
|
||||
up = digit >= 5
|
||||
kept += 1
|
||||
}
|
||||
}
|
||||
if digits == 0 { return 0, false }
|
||||
places := ifx kept > 6 then 6 else kept
|
||||
while places < 6 {
|
||||
fraction *= 10
|
||||
places += 1
|
||||
}
|
||||
value := whole * GridScale + fraction
|
||||
if up { value += 1 }
|
||||
return ifx negative then -value else value, true
|
||||
}
|
||||
|
||||
GridGrouped :: (out: *Vec(u8), value: s64) {
|
||||
digits: [24]u8
|
||||
count: s32 = 0
|
||||
rest := value
|
||||
while true {
|
||||
digits[count] = cast(u8)(48 + rest % 10)
|
||||
count += 1
|
||||
rest /= 10
|
||||
if rest == 0 { break }
|
||||
}
|
||||
index := count - 1
|
||||
while index >= 0 {
|
||||
unused VecPush(out.*, digits[index])
|
||||
if index > 0 && index % 3 == 0 { unused VecPush(out.*, 44) }
|
||||
index -= 1
|
||||
}
|
||||
}
|
||||
|
||||
// Millionths rounded half away from zero to a whole unit.
|
||||
GridRound :: (micros: s64, unit: s64) -> s64 {
|
||||
half := unit / 2
|
||||
return ifx micros < 0 then -((-micros + half) / unit) else (micros + half) / unit
|
||||
}
|
||||
|
||||
GridCountText :: (micros: s64) -> string {
|
||||
out: Vec(u8)
|
||||
whole := GridRound(micros, GridScale)
|
||||
if whole < 0 { BuilderPrint(*out, "-") }
|
||||
GridGrouped(*out, ifx whole < 0 then -whole else whole)
|
||||
return BuilderFinish(out)
|
||||
}
|
||||
|
||||
GridDollars :: (micros: s64) -> string {
|
||||
out: Vec(u8)
|
||||
cents := GridRound(micros, 10000)
|
||||
magnitude := ifx cents < 0 then -cents else cents
|
||||
if cents < 0 { BuilderPrint(*out, "-") }
|
||||
BuilderPrint(*out, "$")
|
||||
GridGrouped(*out, magnitude / 100)
|
||||
BuilderPrint(*out, ".")
|
||||
if magnitude % 100 < 10 { BuilderPrint(*out, "0") }
|
||||
BuilderPrint(*out, "%", magnitude % 100)
|
||||
return BuilderFinish(out)
|
||||
}
|
||||
|
||||
// A calculated value in a column's format; other text is kept as it is.
|
||||
GridFormatText :: (format: s32, text: string) -> (text: string, number: bool) {
|
||||
micros, ok := GridMicros(text)
|
||||
if !ok { return text, false }
|
||||
if format == GridCount { return GridCountText(micros), true }
|
||||
if format == GridMoney { return GridDollars(micros), true }
|
||||
return text, true
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ workbook
|
||||
|
||||
// A new sheet at the end; its index, or -1.
|
||||
GridAddSheet :: (book: *Engine.Workbook, name: string) -> s32 {
|
||||
#if #defined(KRYON_HOSTED_UI) {
|
||||
return Engine.WorkbookAddSheet(book, name)
|
||||
} else {
|
||||
copy: [256]u8
|
||||
if !CopyCString(name, copy[:]) || Engine.WorkbookAddSheet(book, cast(*s8)*copy[0]) == null { return -1 }
|
||||
return Engine.WorkbookSheetCount(book) - 1
|
||||
}
|
||||
}
|
||||
|
||||
// Sets a cell's source: a number, text, or a formula starting with "=".
|
||||
GridSet :: (book: *Engine.Workbook, sheet: s32, row: s32, column: s32, source: string) -> bool {
|
||||
#if #defined(KRYON_HOSTED_UI) {
|
||||
return Engine.WorkbookSetCell(book, sheet, row, column, source) != 0
|
||||
} else {
|
||||
copy: [1024]u8
|
||||
if !CopyCString(source, copy[:]) { return false }
|
||||
return Engine.WorkbookSetCell(book, sheet, row, column, cast(*s8)*copy[0]) != 0
|
||||
}
|
||||
}
|
||||
|
||||
GridSheetName :: (book: *Engine.Workbook, sheet: s32) -> string {
|
||||
#if #defined(KRYON_HOSTED_UI) {
|
||||
return Engine.WorkbookSheetName(book, sheet)
|
||||
} else {
|
||||
return TextFromCString(cast(*u8)Engine.WorkbookSheetName(book, sheet))
|
||||
}
|
||||
}
|
||||
|
||||
// The calculated value of a cell as the grid shows it, in its column's
|
||||
// format; number tells whether it is a number. Call WorkbookEvaluate after
|
||||
// changing cells.
|
||||
GridCellText :: (book: *Engine.Workbook, view: *GridView, row: s32, column: s32, out: []u8) -> (text: string, number: bool) {
|
||||
out[0] = 0
|
||||
#if #defined(KRYON_HOSTED_UI) {
|
||||
Engine.WorkbookCellDisplay(book, view.sheet, row, column, out, cast(s32)out.count)
|
||||
} else {
|
||||
Engine.WorkbookCellDisplay(book, view.sheet, row, column, cast(*s8)*out[0], cast(s32)out.count)
|
||||
if book.native == null { Engine.RoundForDisplay(cast(*s8)*out[0], cast(s32)out.count) }
|
||||
}
|
||||
return GridFormatText(GridFormat(view, column), TextUntilNul(out))
|
||||
}
|
||||
|
||||
// Keeps the scroll position inside the sheet's rows and columns.
|
||||
GridClamp :: (book: *Engine.Workbook, view: *GridView) {
|
||||
count := Engine.WorkbookSheetCount(book)
|
||||
if view.sheet >= count { view.sheet = count - 1 }
|
||||
if view.sheet < 0 { view.sheet = 0 }
|
||||
rows := Engine.WorkbookSheetRows(book, view.sheet)
|
||||
last_row := rows - view.visible_rows
|
||||
if view.scroll_row > last_row { view.scroll_row = last_row }
|
||||
if view.scroll_row < 0 { view.scroll_row = 0 }
|
||||
columns := Engine.WorkbookSheetColumns(book, view.sheet)
|
||||
if view.scroll_column > columns - 1 { view.scroll_column = columns - 1 }
|
||||
if view.scroll_column < 0 { view.scroll_column = 0 }
|
||||
}
|
||||
|
||||
GridInside :: (x: float32, y: float32, bounds: Rectangle) -> bool {
|
||||
return x >= bounds.x && y >= bounds.y && x < bounds.x + bounds.width && y < bounds.y + bounds.height
|
||||
}
|
||||
|
||||
#if !#defined(KRYON_HOSTED_UI) {
|
||||
// The session's pointer and the wheel over bounds, taken for this grid.
|
||||
GridSample :: (session: Session, bounds: Rectangle) -> GridInput {
|
||||
pointer := PointerSample(session)
|
||||
input: GridInput
|
||||
input.x = pointer.x
|
||||
input.y = pointer.y
|
||||
input.wheel = PointerWheelTake(session, bounds)
|
||||
return input
|
||||
}
|
||||
}
|
||||
|
||||
// Draws the sheet tabs along the bottom when the workbook has more than one
|
||||
// sheet, and the shown sheet's grid above them, filling bounds with empty
|
||||
// rows and columns as the editor does. The wheel scrolls the rows.
|
||||
GridDraw :: (session: Session, bounds: Rectangle, book: *Engine.Workbook, view: *GridView, input: GridInput) {
|
||||
sheets := Engine.WorkbookSheetCount(book)
|
||||
if book == null || sheets < 1 { return }
|
||||
table := bounds
|
||||
if sheets > 1 { table.height -= GridTabsHeight }
|
||||
right := table.x + table.width
|
||||
bottom := table.y + table.height
|
||||
view.visible_rows = cast(s32)((table.height - GridHeaderHeight) / GridRowHeight)
|
||||
if view.visible_rows < 1 { view.visible_rows = 1 }
|
||||
if GridInside(input.x, input.y, table) {
|
||||
if input.wheel > 0.0 { view.scroll_row -= 1 }
|
||||
if input.wheel < 0.0 { view.scroll_row += 1 }
|
||||
}
|
||||
GridClamp(book, view)
|
||||
view.visible_columns = 0
|
||||
at := table.x + GridRowHeaderWidth
|
||||
while at < right && view.visible_columns < GridColumnsMax {
|
||||
at += GridWidth(view, view.scroll_column + view.visible_columns)
|
||||
view.visible_columns += 1
|
||||
}
|
||||
|
||||
GridSquare(session, view.key, Rectangle.{table.x, table.y, GridRowHeaderWidth, GridHeaderHeight}, right, bottom, GridHeaderBackground(), "")
|
||||
for shown: 0..view.visible_columns - 1 {
|
||||
column := view.scroll_column + shown
|
||||
name := GridColumnName(column)
|
||||
GridSquare(session, view.key + cast(u64)(2 + shown * 2), Rectangle.{GridColumnX(view, table.x, column), table.y, GridWidth(view, column), GridHeaderHeight},
|
||||
right, bottom, GridHeaderBackground(), name)
|
||||
}
|
||||
// Every row that fits, past the sheet's last row as well.
|
||||
shown_rows := cast(s32)((table.height - GridHeaderHeight + GridRowHeight - 1.0) / GridRowHeight)
|
||||
for shown: 0..shown_rows - 1 {
|
||||
row := view.scroll_row + shown
|
||||
y := table.y + GridHeaderHeight + cast(float32)shown * GridRowHeight
|
||||
number: Vec(u8)
|
||||
BuilderPrint(*number, "%", row + 1)
|
||||
GridSquare(session, view.key + cast(u64)(2000 + shown * 2), Rectangle.{table.x, y, GridRowHeaderWidth, GridRowHeight},
|
||||
right, bottom, GridHeaderBackground(), BuilderFinish(number))
|
||||
for index: 0..view.visible_columns - 1 {
|
||||
column := view.scroll_column + index
|
||||
display: [256]u8
|
||||
text, numeric := GridCellText(book, view, row, column, display[:])
|
||||
GridSquare(session, view.key + cast(u64)(100000 + (shown * GridColumnsMax + index) * 2),
|
||||
Rectangle.{GridColumnX(view, table.x, column), y, GridWidth(view, column), GridRowHeight},
|
||||
right, bottom, GridCellBackground(), text, numeric)
|
||||
}
|
||||
}
|
||||
|
||||
if sheets < 2 { return }
|
||||
count := ifx sheets > 16 then 16 else sheets
|
||||
tabs: [16]Tab
|
||||
for index: 0..count - 1 {
|
||||
tabs[index].key = view.key + cast(u64)(90000 + index)
|
||||
tabs[index].label = GridSheetName(book, index)
|
||||
}
|
||||
strip: BoxProps
|
||||
strip.key = view.key + 89998
|
||||
strip.bounds = Rectangle.{bounds.x, bottom, bounds.width, GridTabsHeight}
|
||||
strip.fill = GridHeaderBackground()
|
||||
strip.border = GridLineColor()
|
||||
Box(session, strip)
|
||||
bar: TabBarProps
|
||||
bar.key = view.key + 89999
|
||||
bar.placement = TabBarPlacement.TabBarBottom
|
||||
bar.selected_index = view.sheet
|
||||
width := 140.0 * cast(float32)count
|
||||
bar.bounds = Rectangle.{bounds.x, bottom, ifx width < bounds.width then width else bounds.width, GridTabsHeight}
|
||||
bar.min_tab_width = 96
|
||||
bar.max_tab_width = 160
|
||||
result := TabBar(session, bar, tabs[0:count])
|
||||
if result.selected_index != view.sheet && result.selected_index >= 0 {
|
||||
view.sheet = result.selected_index
|
||||
view.scroll_row = 0
|
||||
view.scroll_column = 0
|
||||
}
|
||||
}
|
||||
@@ -1,457 +0,0 @@
|
||||
// A read-only, CSV-like table for other apps to embed: typed columns, rows
|
||||
// of cells, a totals row, number and money formatting, column widths fitted
|
||||
// to the content, cell rectangles for a viewport, and CSV text. The host
|
||||
// draws the cells, so this module depends on no UI toolkit.
|
||||
//
|
||||
// Cell values are plain text: numeric columns hold decimal text such as
|
||||
// "1234" or "-0.25", and "" for unknown. The table keeps the strings it is
|
||||
// given without copying; they must outlive it. SheetFree releases only the
|
||||
// table's own storage.
|
||||
|
||||
SheetText :: 0;
|
||||
SheetNumber :: 1;
|
||||
SheetMoney :: 2;
|
||||
|
||||
SheetHeader :: 0;
|
||||
SheetBody :: 1;
|
||||
SheetStripe :: 2;
|
||||
SheetTotal :: 3;
|
||||
|
||||
SheetLeft :: 0;
|
||||
SheetRight :: 2;
|
||||
|
||||
SheetColumn :: struct {
|
||||
title: string
|
||||
kind: s32
|
||||
width: float32
|
||||
}
|
||||
|
||||
SheetTable :: struct {
|
||||
columns: Vec(SheetColumn)
|
||||
cells: Vec(string)
|
||||
rows: s64
|
||||
// The first column of the totals row; "" leaves the row out.
|
||||
total_label: string
|
||||
// Set by SheetFit when numbers must shorten to fit, as in 1.25M.
|
||||
compact: bool
|
||||
}
|
||||
|
||||
SheetCell :: struct {
|
||||
x: float32
|
||||
y: float32
|
||||
width: float32
|
||||
height: float32
|
||||
row: s64
|
||||
column: s64
|
||||
role: s32
|
||||
align: s32
|
||||
text: string
|
||||
}
|
||||
|
||||
// The host's text width at a font size.
|
||||
SheetMeasure :: #type (text: string, size: float32) -> float32;
|
||||
|
||||
SheetAddColumn :: (table: *SheetTable, title: string, kind: s32) {
|
||||
column: SheetColumn
|
||||
column.title = title
|
||||
column.kind = kind
|
||||
VecPush(table.columns, column)
|
||||
}
|
||||
|
||||
// Adds an empty row and returns its index.
|
||||
SheetAddRow :: (table: *SheetTable) -> s64 {
|
||||
for index: 0..table.columns.count - 1 { VecPush(table.cells, "") }
|
||||
table.rows += 1
|
||||
return table.rows - 1
|
||||
}
|
||||
|
||||
SheetSet :: (table: *SheetTable, row: s64, column: s64, value: string) {
|
||||
if row < 0 || row >= table.rows || column < 0 || column >= table.columns.count { return }
|
||||
table.cells[row * table.columns.count + column] = value
|
||||
}
|
||||
|
||||
SheetValue :: (table: *SheetTable, row: s64, column: s64) -> string {
|
||||
if row < 0 || row >= table.rows || column < 0 || column >= table.columns.count { return "" }
|
||||
return table.cells[row * table.columns.count + column]
|
||||
}
|
||||
|
||||
SheetFree :: (table: *SheetTable) {
|
||||
VecFree(table.columns)
|
||||
VecFree(table.cells)
|
||||
table.rows = 0
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ numbers
|
||||
|
||||
SheetScale :: 1000000;
|
||||
|
||||
// Decimal text in millionths, rounded half away from zero. Anything other
|
||||
// than an optional sign, digits and one point is not a number.
|
||||
SheetMicros :: (text: string) -> (value: s64, ok: bool) {
|
||||
if text == "" { return 0, false }
|
||||
at: s64 = 0
|
||||
negative := false
|
||||
if text[0] == 45 || text[0] == 43 {
|
||||
negative = text[0] == 45
|
||||
at = 1
|
||||
}
|
||||
whole: s64 = 0
|
||||
fraction: s64 = 0
|
||||
kept: s32 = 0
|
||||
digits: s32 = 0
|
||||
point := false
|
||||
up := false
|
||||
while at < text.count {
|
||||
c := text[at]
|
||||
at += 1
|
||||
if c == 46 && !point {
|
||||
point = true
|
||||
continue
|
||||
}
|
||||
if c < 48 || c > 57 { return 0, false }
|
||||
digit := cast(s64)(c - 48)
|
||||
digits += 1
|
||||
if !point {
|
||||
if whole > 922337203684 { return 0, false }
|
||||
whole = whole * 10 + digit
|
||||
} else if kept < 6 {
|
||||
fraction = fraction * 10 + digit
|
||||
kept += 1
|
||||
} else if kept == 6 {
|
||||
up = digit >= 5
|
||||
kept += 1
|
||||
}
|
||||
}
|
||||
if digits == 0 { return 0, false }
|
||||
places := ifx kept > 6 then 6 else kept
|
||||
while places < 6 {
|
||||
fraction *= 10
|
||||
places += 1
|
||||
}
|
||||
value := whole * SheetScale + fraction
|
||||
if up { value += 1 }
|
||||
return ifx negative then -value else value, true
|
||||
}
|
||||
|
||||
// Whole digits with a comma between each group of three.
|
||||
SheetGrouped :: (out: *Vec(u8), value: s64) {
|
||||
digits: [24]u8
|
||||
count: s32 = 0
|
||||
rest := value
|
||||
while true {
|
||||
digits[count] = cast(u8)(48 + rest % 10)
|
||||
count += 1
|
||||
rest /= 10
|
||||
if rest == 0 { break }
|
||||
}
|
||||
index := count - 1
|
||||
while index >= 0 {
|
||||
unused VecPush(out.*, digits[index])
|
||||
if index > 0 && index % 3 == 0 { unused VecPush(out.*, 44) }
|
||||
index -= 1
|
||||
}
|
||||
}
|
||||
|
||||
SheetRound :: (micros: s64, unit: s64) -> s64 {
|
||||
magnitude := ifx micros < 0 then -micros else micros
|
||||
rounded := (magnitude + unit / 2) / unit
|
||||
return ifx micros < 0 then -rounded else rounded
|
||||
}
|
||||
|
||||
// A count with separators, as in 1,234,567.
|
||||
SheetCount :: (micros: s64) -> string {
|
||||
out: Vec(u8)
|
||||
whole := SheetRound(micros, SheetScale)
|
||||
if whole < 0 { BuilderPrint(*out, "-") }
|
||||
SheetGrouped(*out, ifx whole < 0 then -whole else whole)
|
||||
return BuilderFinish(out)
|
||||
}
|
||||
|
||||
// A count shortened to three significant figures, as in 1.25B or 734K.
|
||||
SheetShort :: (micros: s64) -> string {
|
||||
whole := SheetRound(micros, SheetScale)
|
||||
magnitude := ifx whole < 0 then -whole else whole
|
||||
if magnitude < 1000 { return SheetCount(micros) }
|
||||
out: Vec(u8)
|
||||
if whole < 0 { BuilderPrint(*out, "-") }
|
||||
units: [4]string = .{"K", "M", "B", "T"}
|
||||
unit: s64 = 1000
|
||||
step: s32 = 0
|
||||
while step < 3 && magnitude >= unit * 1000 - unit / 2 {
|
||||
unit *= 1000
|
||||
step += 1
|
||||
}
|
||||
// Hundredths of the unit, then shown with as many decimals as fit in
|
||||
// three significant figures.
|
||||
hundredths := (magnitude * 100 + unit / 2) / unit
|
||||
whole_units := hundredths / 100
|
||||
if whole_units >= 100 {
|
||||
BuilderPrint(*out, "%", (hundredths + 50) / 100)
|
||||
} else if whole_units >= 10 {
|
||||
tenths := (hundredths + 5) / 10
|
||||
BuilderPrint(*out, "%.%", tenths / 10, tenths % 10)
|
||||
} else {
|
||||
BuilderPrint(*out, "%.", whole_units)
|
||||
if hundredths % 100 < 10 { BuilderPrint(*out, "0") }
|
||||
BuilderPrint(*out, "%", hundredths % 100)
|
||||
}
|
||||
BuilderPrint(*out, "%", units[step])
|
||||
return BuilderFinish(out)
|
||||
}
|
||||
|
||||
// Dollars and cents, as in $1,234.57 or -$0.25.
|
||||
SheetDollars :: (micros: s64) -> string {
|
||||
out: Vec(u8)
|
||||
cents := SheetRound(micros, 10000)
|
||||
magnitude := ifx cents < 0 then -cents else cents
|
||||
if cents < 0 { BuilderPrint(*out, "-") }
|
||||
BuilderPrint(*out, "$")
|
||||
SheetGrouped(*out, magnitude / 100)
|
||||
BuilderPrint(*out, ".")
|
||||
if magnitude % 100 < 10 { BuilderPrint(*out, "0") }
|
||||
BuilderPrint(*out, "%", magnitude % 100)
|
||||
return BuilderFinish(out)
|
||||
}
|
||||
|
||||
// Plain decimal text for CSV, with no trailing zeros after the point.
|
||||
SheetPlain :: (micros: s64) -> string {
|
||||
out: Vec(u8)
|
||||
magnitude := ifx micros < 0 then -micros else micros
|
||||
if micros < 0 { BuilderPrint(*out, "-") }
|
||||
BuilderPrint(*out, "%", magnitude / SheetScale)
|
||||
fraction := magnitude % SheetScale
|
||||
if fraction > 0 {
|
||||
digits: [6]u8
|
||||
for index: 0..5 {
|
||||
digits[5 - index] = cast(u8)(48 + fraction % 10)
|
||||
fraction /= 10
|
||||
}
|
||||
last: s32 = 5
|
||||
while digits[last] == 48 { last -= 1 }
|
||||
BuilderPrint(*out, ".")
|
||||
for index: 0..last { unused VecPush(out, digits[index]) }
|
||||
}
|
||||
return BuilderFinish(out)
|
||||
}
|
||||
|
||||
SheetFormat :: (table: *SheetTable, kind: s32, micros: s64) -> string {
|
||||
if kind == SheetMoney { return SheetDollars(micros) }
|
||||
if table.compact { return SheetShort(micros) }
|
||||
return SheetCount(micros)
|
||||
}
|
||||
|
||||
// The text a cell shows. Unknown numbers show a dash.
|
||||
SheetDisplay :: (table: *SheetTable, row: s64, column: s64) -> string {
|
||||
value := SheetValue(table, row, column)
|
||||
if column < 0 || column >= table.columns.count { return value }
|
||||
kind := table.columns[column].kind
|
||||
if kind == SheetText { return value }
|
||||
if value == "" { return "—" }
|
||||
micros, ok := SheetMicros(value)
|
||||
if !ok { return value }
|
||||
return SheetFormat(table, kind, micros)
|
||||
}
|
||||
|
||||
// The sum of a numeric column's known cells; false when none is known.
|
||||
SheetSum :: (table: *SheetTable, column: s64) -> (value: s64, ok: bool) {
|
||||
if column < 0 || column >= table.columns.count || table.columns[column].kind == SheetText { return 0, false }
|
||||
total: s64 = 0
|
||||
known := false
|
||||
for row: 0..table.rows - 1 {
|
||||
micros, ok := SheetMicros(SheetValue(table, row, column))
|
||||
if ok {
|
||||
total += micros
|
||||
known = true
|
||||
}
|
||||
}
|
||||
return total, known
|
||||
}
|
||||
|
||||
SheetTotalText :: (table: *SheetTable, column: s64) -> string {
|
||||
if column == 0 { return table.total_label }
|
||||
if column < 0 || column >= table.columns.count || table.columns[column].kind == SheetText { return "" }
|
||||
total, known := SheetSum(table, column)
|
||||
if !known { return "—" }
|
||||
return SheetFormat(table, table.columns[column].kind, total)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- layout
|
||||
|
||||
SheetWidest :: (table: *SheetTable, column: s64, measure: SheetMeasure, size: float32) -> float32 {
|
||||
widest := measure(table.columns[column].title, size)
|
||||
for row: 0..table.rows - 1 {
|
||||
text := SheetDisplay(table, row, column)
|
||||
width := measure(text, size)
|
||||
if width > widest { widest = width }
|
||||
}
|
||||
if table.total_label != "" {
|
||||
width := measure(SheetTotalText(table, column), size)
|
||||
if width > widest { widest = width }
|
||||
}
|
||||
return widest
|
||||
}
|
||||
|
||||
SheetNatural :: (table: *SheetTable, measure: SheetMeasure, size: float32, padding: float32) -> float32 {
|
||||
total: float32 = 0.0
|
||||
for column: 0..table.columns.count - 1 {
|
||||
table.columns[column].width = SheetWidest(table, column, measure, size) + padding * 2.0
|
||||
total += table.columns[column].width
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// Fits the columns to the available width. Numbers keep their full width
|
||||
// when the text columns can give way; otherwise they shorten. Spare width
|
||||
// goes to the first text column.
|
||||
SheetFit :: (table: *SheetTable, measure: SheetMeasure, size: float32, padding: float32, available: float32) {
|
||||
if table.columns.count == 0 { return }
|
||||
table.compact = false
|
||||
total := SheetNatural(table, measure, size, padding)
|
||||
floor := measure("Mmmmmmmm", size) + padding * 2.0
|
||||
flexible: float32 = 0.0
|
||||
fixed: float32 = 0.0
|
||||
for column: 0..table.columns.count - 1 {
|
||||
width := table.columns[column].width
|
||||
if table.columns[column].kind == SheetText && width > floor { flexible += width - floor; fixed += floor } else { fixed += width }
|
||||
}
|
||||
if total > available && fixed > available {
|
||||
table.compact = true
|
||||
total = SheetNatural(table, measure, size, padding)
|
||||
flexible = 0.0
|
||||
fixed = 0.0
|
||||
for column: 0..table.columns.count - 1 {
|
||||
width := table.columns[column].width
|
||||
if table.columns[column].kind == SheetText && width > floor { flexible += width - floor; fixed += floor } else { fixed += width }
|
||||
}
|
||||
}
|
||||
if total > available && flexible > 0.0 {
|
||||
// Text columns shrink toward their floor in proportion to their extra.
|
||||
keep := available - fixed
|
||||
if keep < 0.0 { keep = 0.0 }
|
||||
ratio := keep / flexible
|
||||
if ratio > 1.0 { ratio = 1.0 }
|
||||
for column: 0..table.columns.count - 1 {
|
||||
width := table.columns[column].width
|
||||
if table.columns[column].kind == SheetText && width > floor { table.columns[column].width = floor + (width - floor) * ratio }
|
||||
}
|
||||
return
|
||||
}
|
||||
if total < available {
|
||||
target: s64 = 0
|
||||
for column: 0..table.columns.count - 1 {
|
||||
if table.columns[column].kind == SheetText { target = column; break }
|
||||
}
|
||||
table.columns[target].width += available - total
|
||||
}
|
||||
}
|
||||
|
||||
SheetWidth :: (table: *SheetTable) -> float32 {
|
||||
total: float32 = 0.0
|
||||
for column: 0..table.columns.count - 1 { total += table.columns[column].width }
|
||||
return total
|
||||
}
|
||||
|
||||
// Whole body rows that fit under the header, above the totals row.
|
||||
SheetVisibleRows :: (table: *SheetTable, row_height: float32, header_height: float32, height: float32) -> s64 {
|
||||
room := height - header_height
|
||||
if table.total_label != "" { room -= row_height }
|
||||
if room <= 0.0 || row_height <= 0.0 { return 0 }
|
||||
return cast(s64)(room / row_height)
|
||||
}
|
||||
|
||||
// The first row that keeps a full page of rows on screen.
|
||||
SheetLastFirstRow :: (table: *SheetTable, visible: s64) -> s64 {
|
||||
last := table.rows - visible
|
||||
return ifx last > 0 then last else 0
|
||||
}
|
||||
|
||||
SheetPlace :: (cells: *Vec(SheetCell), table: *SheetTable, x: float32, y: float32, height: float32, row: s64, role: s32) {
|
||||
left := x
|
||||
for column: 0..table.columns.count - 1 {
|
||||
cell: SheetCell
|
||||
cell.x = left
|
||||
cell.y = y
|
||||
cell.width = table.columns[column].width
|
||||
cell.height = height
|
||||
cell.row = row
|
||||
cell.column = column
|
||||
cell.role = role
|
||||
cell.align = ifx table.columns[column].kind == SheetText then SheetLeft else SheetRight
|
||||
if role == SheetHeader { cell.text = table.columns[column].title }
|
||||
else if role == SheetTotal { cell.text = SheetTotalText(table, column) }
|
||||
else { cell.text = SheetDisplay(table, row, column) }
|
||||
VecPush(cells.*, cell)
|
||||
left += cell.width
|
||||
}
|
||||
}
|
||||
|
||||
// The cells of one screen: the header, whole body rows from first_row that
|
||||
// fit, and the totals row right under them. Header cells have row -1 and
|
||||
// totals cells row `rows`; every second body row is a stripe.
|
||||
SheetLayout :: (table: *SheetTable, x: float32, y: float32, row_height: float32, header_height: float32, height: float32, first_row: s64) -> Vec(SheetCell) {
|
||||
cells: Vec(SheetCell)
|
||||
SheetPlace(*cells, table, x, y, header_height, -1, SheetHeader)
|
||||
visible := SheetVisibleRows(table, row_height, header_height, height)
|
||||
first := first_row
|
||||
if first > SheetLastFirstRow(table, visible) { first = SheetLastFirstRow(table, visible) }
|
||||
if first < 0 { first = 0 }
|
||||
top := y + header_height
|
||||
shown: s64 = 0
|
||||
while shown < visible && first + shown < table.rows {
|
||||
row := first + shown
|
||||
SheetPlace(*cells, table, x, top, row_height, row, ifx row % 2 == 1 then SheetStripe else SheetBody)
|
||||
top += row_height
|
||||
shown += 1
|
||||
}
|
||||
if table.total_label != "" { SheetPlace(*cells, table, x, top, row_height, table.rows, SheetTotal) }
|
||||
return cells
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------- CSV
|
||||
|
||||
SheetField :: (out: *Vec(u8), text: string, protect: bool) {
|
||||
// A spreadsheet would run a text cell that starts like a formula.
|
||||
guard := protect && text.count > 0 && (text[0] == 61 || text[0] == 43 || text[0] == 45 || text[0] == 64)
|
||||
quote := guard
|
||||
for c: text {
|
||||
if c == 44 || c == 34 || c == 10 || c == 13 { quote = true }
|
||||
}
|
||||
if !quote {
|
||||
BuilderPrint(out, "%", text)
|
||||
return
|
||||
}
|
||||
unused VecPush(out.*, 34)
|
||||
if guard { unused VecPush(out.*, 39) }
|
||||
for c: text {
|
||||
if c == 34 { unused VecPush(out.*, 34) }
|
||||
unused VecPush(out.*, c)
|
||||
}
|
||||
unused VecPush(out.*, 34)
|
||||
}
|
||||
|
||||
// RFC 4180 text: the header, every row's values, then the totals row.
|
||||
SheetCsv :: (table: *SheetTable) -> string {
|
||||
out: Vec(u8)
|
||||
for column: 0..table.columns.count - 1 {
|
||||
if column > 0 { BuilderPrint(*out, ",") }
|
||||
SheetField(*out, table.columns[column].title, true)
|
||||
}
|
||||
BuilderPrint(*out, "\r\n")
|
||||
for row: 0..table.rows - 1 {
|
||||
for column: 0..table.columns.count - 1 {
|
||||
if column > 0 { BuilderPrint(*out, ",") }
|
||||
SheetField(*out, SheetValue(table, row, column), table.columns[column].kind == SheetText)
|
||||
}
|
||||
BuilderPrint(*out, "\r\n")
|
||||
}
|
||||
if table.total_label != "" {
|
||||
for column: 0..table.columns.count - 1 {
|
||||
if column > 0 { BuilderPrint(*out, ",") }
|
||||
if column == 0 { SheetField(*out, table.total_label, true); continue }
|
||||
total, known := SheetSum(table, column)
|
||||
if known { BuilderPrint(*out, "%", SheetPlain(total)) }
|
||||
}
|
||||
BuilderPrint(*out, "\r\n")
|
||||
}
|
||||
return BuilderFinish(out)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// The embeddable grid: real workbook cells and formulas, column formats,
|
||||
// column positions and scrolling, without a window.
|
||||
#load "test_support.zi";
|
||||
using Grid :: #import "grid";
|
||||
Engine :: #import "engine";
|
||||
|
||||
#program_export
|
||||
main :: () -> s32 {
|
||||
micros, ok := Grid.GridMicros("-12.3456785")
|
||||
Check(ok && micros == -12345679, "decimal text in millionths, rounded")
|
||||
unused_value, bad := Grid.GridMicros("1e5")
|
||||
Check(!bad && unused_value == 0, "exponents are not numbers")
|
||||
Check(Grid.GridCountText(4795296906000000) == "4,795,296,906" && Grid.GridCountText(-1000000000) == "-1,000", "counts with separators")
|
||||
Check(Grid.GridDollars(1475751234) == "$1,475.75" && Grid.GridDollars(-3204999) == "-$3.20" && Grid.GridDollars(5000) == "$0.01", "money rounds to cents")
|
||||
Check(Grid.GridColumnName(0) == "A" && Grid.GridColumnName(25) == "Z" && Grid.GridColumnName(27) == "AB", "column letters")
|
||||
|
||||
book := Engine.WorkbookNew()
|
||||
defer { Engine.WorkbookFree(book) }
|
||||
first := Grid.GridAddSheet(book, "Subscription")
|
||||
second := Grid.GridAddSheet(book, "API comparison")
|
||||
Check(first == 0 && second == 1 && Grid.GridSheetName(book, 1) == "API comparison", "named sheets")
|
||||
sources: [4][3]string = .{.{"Folder", "Tokens", "Cost"}, .{"aurea", "1250000", "12.345"}, .{"max", "734", "0.5"}, .{"Total", "=SUM(B2:B3)", "=SUM(C2:C3)"}}
|
||||
for row: 0..3 { for column: 0..2 { Check(Grid.GridSet(book, first, cast(s32)row, cast(s32)column, sources[row][column]), "cell set") } }
|
||||
Engine.WorkbookEvaluate(book)
|
||||
|
||||
view: Grid.GridView
|
||||
view.formats[1] = Grid.GridCount
|
||||
view.formats[2] = Grid.GridMoney
|
||||
view.widths[0] = 180.0
|
||||
out0: [256]u8
|
||||
out1: [256]u8
|
||||
out2: [256]u8
|
||||
out3: [256]u8
|
||||
out4: [256]u8
|
||||
text, number := Grid.GridCellText(book, *view, 3, 1, out0[:])
|
||||
Check(number && text == "1,250,734", "a SUM total in the count format")
|
||||
money, priced := Grid.GridCellText(book, *view, 3, 2, out1[:])
|
||||
Check(priced && money == "$12.85", "a SUM total in the money format")
|
||||
label, numeric := Grid.GridCellText(book, *view, 1, 0, out2[:])
|
||||
Check(!numeric && label == "aurea", "text stays text")
|
||||
header, counted := Grid.GridCellText(book, *view, 0, 1, out3[:])
|
||||
Check(!counted && header == "Tokens", "headers are text in a number column")
|
||||
empty, blank := Grid.GridCellText(book, *view, 9, 9, out4[:])
|
||||
Check(!blank && empty == "", "empty cells show nothing")
|
||||
|
||||
Check(Grid.GridWidth(*view, 0) == 180.0 && Grid.GridWidth(*view, 1) == Grid.GridColumnWidth, "column widths")
|
||||
Check(Grid.GridColumnX(*view, 10.0, 2) == 10.0 + Grid.GridRowHeaderWidth + 180.0 + Grid.GridColumnWidth, "columns follow their widths")
|
||||
view.visible_rows = 2
|
||||
view.scroll_row = 9
|
||||
view.sheet = 5
|
||||
Grid.GridClamp(book, *view)
|
||||
Check(view.sheet == 1, "sheet index stays in the workbook")
|
||||
view.sheet = 0
|
||||
view.scroll_row = 9
|
||||
Grid.GridClamp(book, *view)
|
||||
Check(view.scroll_row == 2, "scrolling stops at a full last screen")
|
||||
WriteText(1, "grid: ok\n")
|
||||
return 0
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
// The embeddable read-only table: number and money text, totals, fitted
|
||||
// column widths, the cells of one screen, and CSV text.
|
||||
#load "test_support.zi";
|
||||
using Sheet :: #import "sheet_table";
|
||||
|
||||
// Every character is 7 units wide at size 10.
|
||||
Fixed :: (text: string, size: float32) -> float32 {
|
||||
count: s32 = 0
|
||||
for c: text {
|
||||
if (c & 192) != 128 { count += 1 }
|
||||
}
|
||||
return cast(float32)count * size * 0.7
|
||||
}
|
||||
|
||||
Usage :: (table: *Sheet.SheetTable) {
|
||||
Sheet.SheetAddColumn(table, "Session", Sheet.SheetText)
|
||||
Sheet.SheetAddColumn(table, "Tokens", Sheet.SheetNumber)
|
||||
Sheet.SheetAddColumn(table, "API", Sheet.SheetMoney)
|
||||
table.total_label = "Total"
|
||||
rows: [3][3]string = .{.{"=SUM(A1), \"quoted\"", "1250000", "12.345"}, .{"Second", "734", ""}, .{"Third", "", "-0.5"}}
|
||||
for index: 0..2 {
|
||||
row := Sheet.SheetAddRow(table)
|
||||
for column: 0..2 { Sheet.SheetSet(table, row, column, rows[index][column]) }
|
||||
}
|
||||
}
|
||||
|
||||
#program_export
|
||||
main :: () -> s32 {
|
||||
micros, ok := Sheet.SheetMicros("-12.3456785")
|
||||
Check(ok && micros == -12345679, "decimal text in millionths, rounded")
|
||||
unused_value, bad := Sheet.SheetMicros("1e5")
|
||||
Check(!bad && unused_value == 0, "exponents are not numbers")
|
||||
Check(Sheet.SheetCount(4795296906000000) == "4,795,296,906", "count separators")
|
||||
Check(Sheet.SheetCount(999000000) == "999" && Sheet.SheetCount(-1000000000) == "-1,000", "short and negative counts")
|
||||
Check(Sheet.SheetDollars(1475751234) == "$1,475.75" && Sheet.SheetDollars(-3204999) == "-$3.20" && Sheet.SheetDollars(5000) == "$0.01", "money rounds to cents")
|
||||
Check(Sheet.SheetShort(1250000000000) == "1.25M" && Sheet.SheetShort(734000000) == "734" && Sheet.SheetShort(4795296906000000) == "4.80B" && Sheet.SheetShort(12400000000) == "12.4K" && Sheet.SheetShort(999600000000) == "1.00M", "compact counts")
|
||||
Check(Sheet.SheetPlain(1481803028) == "1481.803028" && Sheet.SheetPlain(-500000) == "-0.5" && Sheet.SheetPlain(2000000) == "2", "plain CSV decimals")
|
||||
|
||||
table: Sheet.SheetTable
|
||||
Usage(*table)
|
||||
defer { Sheet.SheetFree(*table) }
|
||||
Check(table.rows == 3 && Sheet.SheetValue(*table, 1, 0) == "Second" && Sheet.SheetValue(*table, 5, 0) == "", "rows and bounded reads")
|
||||
Check(Sheet.SheetDisplay(*table, 0, 1) == "1,250,000" && Sheet.SheetDisplay(*table, 0, 2) == "$12.35" && Sheet.SheetDisplay(*table, 1, 2) == "—" && Sheet.SheetDisplay(*table, 2, 2) == "-$0.50", "cells show formatted numbers and dashes for unknown")
|
||||
total, known := Sheet.SheetSum(*table, 2)
|
||||
Check(known && total == 11845000, "totals add the known cells")
|
||||
Check(Sheet.SheetTotalText(*table, 0) == "Total" && Sheet.SheetTotalText(*table, 1) == "1,250,734" && Sheet.SheetTotalText(*table, 2) == "$11.85", "totals row text")
|
||||
|
||||
// Natural widths fit: spare width goes to the text column.
|
||||
Sheet.SheetFit(*table, Fixed, 10.0, 6.0, 600.0)
|
||||
Check(!table.compact && Sheet.SheetWidth(*table) == 600.0, "columns fill the width")
|
||||
Check(table.columns[1].width == 9.0 * 7.0 + 12.0 && table.columns[2].width == 6.0 * 7.0 + 12.0, "numbers keep their natural width")
|
||||
// Too narrow: the text column gives way first.
|
||||
Sheet.SheetFit(*table, Fixed, 10.0, 6.0, 250.0)
|
||||
Check(!table.compact && Sheet.SheetWidth(*table) <= 250.5 && table.columns[0].width >= 8.0 * 7.0 + 12.0, "text column shrinks to its floor")
|
||||
// Far too narrow: numbers shorten.
|
||||
Sheet.SheetFit(*table, Fixed, 10.0, 6.0, 150.0)
|
||||
Check(table.compact && Sheet.SheetDisplay(*table, 0, 1) == "1.25M", "numbers shorten when nothing else gives way")
|
||||
|
||||
// A screen with room for two body rows under the header, then totals.
|
||||
Sheet.SheetFit(*table, Fixed, 10.0, 6.0, 600.0)
|
||||
Check(Sheet.SheetVisibleRows(*table, 20.0, 24.0, 84.0) == 2, "whole rows that fit")
|
||||
cells := Sheet.SheetLayout(*table, 10.0, 100.0, 20.0, 24.0, 84.0, 1)
|
||||
defer { VecFree(cells) }
|
||||
Check(cells.count == 12, "header, two rows and totals")
|
||||
Check(cells[0].role == Sheet.SheetHeader && cells[0].row == -1 && cells[0].text == "Session" && cells[0].x == 10.0 && cells[0].y == 100.0, "header first")
|
||||
Check(cells[3].row == 1 && cells[3].role == Sheet.SheetStripe && cells[3].y == 124.0 && cells[6].role == Sheet.SheetBody && cells[6].y == 144.0, "rows from the first row, striped")
|
||||
Check(cells[4].align == Sheet.SheetRight && cells[3].align == Sheet.SheetLeft && cells[4].x == 10.0 + table.columns[0].width, "numbers align right, columns follow")
|
||||
Check(cells[9].role == Sheet.SheetTotal && cells[9].row == 3 && cells[9].y == 164.0 && cells[10].text == "1,250,734", "totals under the last row")
|
||||
late := Sheet.SheetLayout(*table, 0.0, 0.0, 20.0, 24.0, 84.0, 9)
|
||||
defer { VecFree(late) }
|
||||
Check(late[3].row == 1, "first row stops at a full last page")
|
||||
|
||||
csv := Sheet.SheetCsv(*table)
|
||||
Check(csv == "Session,Tokens,API\r\n\"'=SUM(A1), \"\"quoted\"\"\",1250000,12.345\r\nSecond,734,\r\nThird,,-0.5\r\nTotal,1250734,11.845\r\n", "CSV quotes, protects formulas and adds totals")
|
||||
WriteText(1, "sheet_table: ok\n")
|
||||
return 0
|
||||
}
|
||||
+2
-1
@@ -38,4 +38,5 @@ categories = "Office;Spreadsheet;"
|
||||
|
||||
[exports]
|
||||
service = "src/service.zi"
|
||||
sheet_table = "src/sheet_table.zi"
|
||||
grid = "src/grid.zi"
|
||||
engine = "src/engine.zi"
|
||||
Reference in new issue
Block a user