Files
htab/ui/grid_widget.cpp

834 lines
28 KiB
C++

#include "ui/grid_widget.hpp"
#include "engine/address.hpp"
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
namespace htab::ui {
namespace {
uint64_t monotonic_ms() {
struct timespec ts{};
clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<uint64_t>(ts.tv_sec) * 1000ULL +
static_cast<uint64_t>(ts.tv_nsec / 1000000L);
}
gemui_color_t blend_color(gemui_color_t bg, gemui_color_t fg, double alpha) {
gemui_color_t out;
out.r = bg.r * (1.0 - alpha) + fg.r * alpha;
out.g = bg.g * (1.0 - alpha) + fg.g * alpha;
out.b = bg.b * (1.0 - alpha) + fg.b * alpha;
out.a = 1.0;
out.argb = 0xFF000000U;
return out;
}
} // namespace
GridMetrics GridWidget::metrics_for(const UiScale& ui) {
if (ui.compact) {
return GridMetrics{
spx(24, ui),
spx(16, ui),
50,
17,
10,
spx(6, ui),
};
}
return GridMetrics{
spx(48, ui),
spx(26, ui),
96,
ui.touch ? 30 : 26,
13,
spx(12, ui),
};
}
int GridWidget::col_w(const Workbook& wb, int col) const {
if (drag_mode_ == DragMode::ResizeCol && resize_index_ == col) {
return resize_live_size_;
}
int logical_w = wb.get_col_width(wb.active_sheet(), col,
metrics_for(last_ui_).default_col_w_logical);
return spx(logical_w, last_ui_);
}
int GridWidget::row_h(const Workbook& wb, int row) const {
if (drag_mode_ == DragMode::ResizeRow && resize_index_ == row) {
return resize_live_size_;
}
int logical_h = wb.get_row_height(wb.active_sheet(), row,
metrics_for(last_ui_).default_row_h_logical);
return spx(logical_h, last_ui_);
}
CellRange GridWidget::selection_range(int sheet_idx) const {
return CellRange{
sheet_idx,
std::min(anchor_row_, active_row_),
std::min(anchor_col_, active_col_),
std::max(anchor_row_, active_row_),
std::max(anchor_col_, active_col_),
};
}
int GridWidget::max_scroll_rows(const Workbook& wb) const {
auto bounds = wb.get_used_bounds(wb.active_sheet());
int used_r = bounds ? bounds->r2 : 0;
return std::max({60, used_r + 30, active_row_ + 20, scroll_row_ + 20});
}
int GridWidget::max_scroll_cols(const Workbook& wb) const {
auto bounds = wb.get_used_bounds(wb.active_sheet());
int used_c = bounds ? bounds->c2 : 0;
return std::max({26, used_c + 10, active_col_ + 10, scroll_col_ + 10});
}
void GridWidget::reset_for_sheet(const Workbook& wb, int) {
editing_ = false;
formula_bar_focused_ = false;
scroll_row_ = 0;
scroll_col_ = 0;
active_row_ = 0;
active_col_ = 0;
anchor_row_ = 0;
anchor_col_ = 0;
sync_editor_from_cell(wb);
}
void GridWidget::sync_editor_from_cell(const Workbook& wb) {
const Cell* c = wb.get_cell(wb.active_sheet(), active_row_, active_col_);
editor_.set_text(c ? c->raw_input : "");
}
void GridWidget::set_active_cell(const Workbook& wb, int row, int col, bool keep_anchor) {
active_row_ = std::clamp(row, 0, kMaxRows - 1);
active_col_ = std::clamp(col, 0, kMaxCols - 1);
if (!keep_anchor) {
anchor_row_ = active_row_;
anchor_col_ = active_col_;
}
ensure_visible(wb, active_row_, active_col_);
if (!editing_) {
sync_editor_from_cell(wb);
}
}
void GridWidget::move_active(const Workbook& wb, int drow, int dcol, bool extend_selection) {
set_active_cell(wb, active_row_ + drow, active_col_ + dcol, extend_selection);
}
void GridWidget::ensure_visible(const Workbook& wb, int row, int col) {
if (row < scroll_row_) {
scroll_row_ = row;
} else if (last_data_rect_.h > 0) {
int y = 0;
int r = scroll_row_;
while (r <= row && y + row_h(wb, r) <= last_data_rect_.h) {
y += row_h(wb, r);
++r;
}
if (r <= row) {
int needed = 0;
int new_top = row;
while (new_top > 0) {
int h = row_h(wb, new_top);
if (needed + h > last_data_rect_.h) break;
needed += h;
--new_top;
}
if (needed + row_h(wb, new_top) <= last_data_rect_.h) {
scroll_row_ = new_top;
} else {
scroll_row_ = std::min(row, new_top + 1);
}
}
}
if (col < scroll_col_) {
scroll_col_ = col;
} else if (last_data_rect_.w > 0) {
int x = 0;
int c = scroll_col_;
while (c <= col && x + col_w(wb, c) <= last_data_rect_.w) {
x += col_w(wb, c);
++c;
}
if (c <= col) {
int needed = 0;
int new_left = col;
while (new_left > 0) {
int w = col_w(wb, new_left);
if (needed + w > last_data_rect_.w) break;
needed += w;
--new_left;
}
if (needed + col_w(wb, new_left) <= last_data_rect_.w) {
scroll_col_ = new_left;
} else {
scroll_col_ = std::min(col, new_left + 1);
}
}
}
}
void GridWidget::start_edit(const Workbook& wb,
bool focus_formula_bar,
const std::string* initial) {
editing_ = true;
formula_bar_focused_ = focus_formula_bar;
if (initial) {
editor_.set_text(*initial);
} else {
sync_editor_from_cell(wb);
}
}
void GridWidget::commit_edit(Workbook& wb) {
if (!editing_) return;
editing_ = false;
formula_bar_focused_ = false;
wb.set_cell_input(wb.active_sheet(), active_row_, active_col_, editor_.text);
sync_editor_from_cell(wb);
}
void GridWidget::cancel_edit(const Workbook& wb) {
editing_ = false;
formula_bar_focused_ = false;
sync_editor_from_cell(wb);
}
CellRange GridWidget::compute_fill_preview(int sheet_idx) const {
CellRange base = selection_range(sheet_idx);
int dr = 0;
if (fill_hover_row_ > base.r2) dr = fill_hover_row_ - base.r2;
else if (fill_hover_row_ < base.r1) dr = base.r1 - fill_hover_row_;
int dc = 0;
if (fill_hover_col_ > base.c2) dc = fill_hover_col_ - base.c2;
else if (fill_hover_col_ < base.c1) dc = base.c1 - fill_hover_col_;
if (dr >= dc && dr > 0) {
if (fill_hover_row_ > base.r2) base.r2 = fill_hover_row_;
else base.r1 = fill_hover_row_;
} else if (dc > 0) {
if (fill_hover_col_ > base.c2) base.c2 = fill_hover_col_;
else base.c1 = fill_hover_col_;
}
return base;
}
void GridWidget::draw(gemui_app_t* app,
cairo_t* cr,
const Workbook& wb,
gemui_rect_t rect,
const UiScale& ui) {
last_rect_ = rect;
last_ui_ = ui;
has_fill_handle_ = false;
if (rect.w < spx(40, ui) || rect.h < spx(30, ui)) return;
const gemui_theme_t* t = gemui_theme(app);
const GridMetrics m = metrics_for(ui);
const int sheet = wb.active_sheet();
gemui_rect_t vscroll_r{rect.x + rect.w - m.vscroll_w, rect.y + m.col_hdr_h,
m.vscroll_w, std::max(1, rect.h - m.col_hdr_h)};
last_vscroll_rect_ = vscroll_r;
gemui_rect_t data_r{rect.x + m.row_hdr_w, rect.y + m.col_hdr_h,
std::max(1, rect.w - m.row_hdr_w - m.vscroll_w),
std::max(1, rect.h - m.col_hdr_h)};
last_data_rect_ = data_r;
// Build visible column & row slices ONLY
std::vector<ColLayout> cols;
{
int x = data_r.x;
int c = scroll_col_;
int end_x = data_r.x + data_r.w;
while (x < end_x && c < kMaxCols) {
int w = col_w(wb, c);
cols.push_back({c, x, w});
x += w;
++c;
}
}
std::vector<RowLayout> rows;
{
int y = data_r.y;
int r = scroll_row_;
int end_y = data_r.y + data_r.h;
while (y < end_y && r < kMaxRows) {
int h = row_h(wb, r);
rows.push_back({r, y, h});
y += h;
++r;
}
}
CellRange sel = selection_range(sheet);
gemui_color_t sel_fill = blend_color(t->bg_main, t->sel_bg, 0.45);
gemui_color_t hdr_sel = blend_color(t->bg_header, t->accent, 0.22);
gemui_color_t spill_fill = blend_color(t->bg_main, t->info, 0.12);
int font_delta = pt_delta_for_ui(t, m.font_logical_px, ui);
gemui_fill_rect(cr, data_r, t->bg_main);
gemui_rect_t corner_r{rect.x, rect.y, m.row_hdr_w, m.col_hdr_h};
gemui_fill_rect(cr, corner_r, t->bg_header);
// Column headers
cairo_save(cr);
cairo_rectangle(cr, data_r.x, rect.y, data_r.w, m.col_hdr_h);
cairo_clip(cr);
for (const auto& cl : cols) {
bool in_sel = (cl.col >= sel.c1 && cl.col <= sel.c2);
gemui_rect_t hr{cl.x, rect.y, cl.w, m.col_hdr_h};
gemui_fill_rect(cr, hr, in_sel ? hdr_sel : t->bg_header);
std::string label = col_to_letters(cl.col);
int hpad = spx(1, ui);
gemui_draw_text(cr, t,
gemui_rect_t{hr.x + hpad, hr.y, std::max(1, hr.w - hpad * 2), hr.h},
label.c_str(), font_delta, in_sel, GEMUI_ALIGN_CENTER,
in_sel ? t->fg_bright : t->fg_muted);
}
cairo_restore(cr);
// Row headers
cairo_save(cr);
cairo_rectangle(cr, rect.x, data_r.y, m.row_hdr_w, data_r.h);
cairo_clip(cr);
for (const auto& rl : rows) {
bool in_sel = (rl.row >= sel.r1 && rl.row <= sel.r2);
gemui_rect_t hr{rect.x, rl.y, m.row_hdr_w, rl.h};
gemui_fill_rect(cr, hr, in_sel ? hdr_sel : t->bg_header);
std::string label = std::to_string(rl.row + 1);
int hpad = spx(1, ui);
gemui_draw_text(cr, t,
gemui_rect_t{hr.x + hpad, hr.y, std::max(1, hr.w - hpad * 2), hr.h},
label.c_str(), font_delta, in_sel, GEMUI_ALIGN_CENTER,
in_sel ? t->fg_bright : t->fg_muted);
}
cairo_restore(cr);
// Visible cells (clipped to data_r)
cairo_save(cr);
cairo_rectangle(cr, data_r.x, data_r.y, data_r.w, data_r.h);
cairo_clip(cr);
int cell_pad_x = spx(ui.compact ? 2 : 5, ui);
int cell_pad_y = std::max(1, spx(1, ui));
for (const auto& rl : rows) {
for (const auto& cl : cols) {
gemui_rect_t cr_rect{cl.x, rl.y, cl.w, rl.h};
bool in_sel = sel.contains(sheet, rl.row, cl.col);
bool is_active = (rl.row == active_row_ && cl.col == active_col_);
bool is_spilled = wb.is_spilled_cell(sheet, rl.row, cl.col);
if (in_sel && !is_active) {
gemui_fill_rect(cr, cr_rect, sel_fill);
} else if (is_spilled) {
gemui_fill_rect(cr, cr_rect, spill_fill);
}
if (editing_ && !formula_bar_focused_ && is_active) {
continue;
}
Value val = wb.get_scalar_value(sheet, rl.row, cl.col);
if (!val.is_empty()) {
std::string txt = wb.get_formatted_value(sheet, rl.row, cl.col);
if (!txt.empty()) {
int align = GEMUI_ALIGN_LEFT;
gemui_color_t fg = t->fg_main;
if (val.is_error()) {
align = GEMUI_ALIGN_CENTER;
fg = t->danger;
} else if (val.is_bool()) {
align = GEMUI_ALIGN_CENTER;
fg = t->info;
} else if (val.is_number()) {
align = GEMUI_ALIGN_RIGHT;
fg = t->fg_bright;
}
gemui_rect_t text_r{cr_rect.x + cell_pad_x, cr_rect.y + cell_pad_y,
std::max(1, cr_rect.w - cell_pad_x * 2),
std::max(1, cr_rect.h - cell_pad_y * 2)};
if (text_r.w > 4 && text_r.h > 4) {
cairo_save(cr);
cairo_rectangle(cr, cr_rect.x + 1, cr_rect.y + 1,
std::max(1, cr_rect.w - 2), std::max(1, cr_rect.h - 2));
cairo_clip(cr);
gemui_draw_text(cr, t, text_r, txt.c_str(), font_delta, false, align, fg);
cairo_restore(cr);
}
}
}
}
}
// Gridlines
double line_w = (ui.scale >= 1.5) ? 1.5 : 1.0;
gemui_set_source(cr, t->divider);
cairo_set_line_width(cr, line_w);
for (const auto& cl : cols) {
double lx = static_cast<double>(cl.x + cl.w) + 0.5;
cairo_move_to(cr, lx, data_r.y);
cairo_line_to(cr, lx, data_r.y + data_r.h);
}
for (const auto& rl : rows) {
double ly = static_cast<double>(rl.y + rl.h) + 0.5;
cairo_move_to(cr, data_r.x, ly);
cairo_line_to(cr, data_r.x + data_r.w, ly);
}
cairo_stroke(cr);
auto col_x_range = [&](int c_idx, int& out_x, int& out_w) -> bool {
for (const auto& cl : cols) {
if (cl.col == c_idx) { out_x = cl.x; out_w = cl.w; return true; }
}
return false;
};
auto row_y_range = [&](int r_idx, int& out_y, int& out_h) -> bool {
for (const auto& rl : rows) {
if (rl.row == r_idx) { out_y = rl.y; out_h = rl.h; return true; }
}
return false;
};
int sx1 = data_r.x, sy1 = data_r.y, sx2 = data_r.x, sy2 = data_r.y;
bool sel_vis = !cols.empty() && !rows.empty() &&
sel.c2 >= cols.front().col && sel.c1 <= cols.back().col &&
sel.r2 >= rows.front().row && sel.r1 <= rows.back().row;
if (sel_vis) {
if (sel.c1 < cols.front().col) sx1 = data_r.x;
else { int w_unused = 0; col_x_range(sel.c1, sx1, w_unused); }
if (sel.c2 > cols.back().col) sx2 = cols.back().x + cols.back().w;
else { int x0 = 0, w0 = 0; col_x_range(sel.c2, x0, w0); sx2 = x0 + w0; }
if (sel.r1 < rows.front().row) sy1 = data_r.y;
else { int h_unused = 0; row_y_range(sel.r1, sy1, h_unused); }
if (sel.r2 > rows.back().row) sy2 = rows.back().y + rows.back().h;
else { int y0 = 0, h0 = 0; row_y_range(sel.r2, y0, h0); sy2 = y0 + h0; }
gemui_set_source(cr, t->accent);
cairo_set_line_width(cr, std::max(1.5, 1.5 * ui.scale));
cairo_rectangle(cr, sx1 + 0.5, sy1 + 0.5, std::max(1, sx2 - sx1 - 1),
std::max(1, sy2 - sy1 - 1));
cairo_stroke(cr);
if (sel.c2 <= cols.back().col && sel.r2 <= rows.back().row) {
int hs = spx(ui.compact ? 5 : 7, ui);
last_fill_handle_ = gemui_rect_t{sx2 - hs / 2 - 1, sy2 - hs / 2 - 1, hs, hs};
has_fill_handle_ = true;
gemui_fill_rect(cr, last_fill_handle_, t->highlight);
}
}
int ax = 0, aw = 0, ay = 0, ah = 0;
if (col_x_range(active_col_, ax, aw) && row_y_range(active_row_, ay, ah)) {
if (editing_ && !formula_bar_focused_) {
int min_ed_w = spx(140, ui);
int edit_w = std::min(std::max(aw, min_ed_w), std::max(aw, data_r.x + data_r.w - ax));
draw_single_line_editor(cr, t, gemui_rect_t{ax, ay, edit_w, ah}, editor_, "", true,
m.font_logical_px, ui);
} else {
double active_lw = std::max(2.0, 2.0 * ui.scale);
gemui_set_source(cr, t->highlight);
cairo_set_line_width(cr, active_lw);
cairo_rectangle(cr, ax + 1.0, ay + 1.0, std::max(1, aw - 2), std::max(1, ah - 2));
cairo_stroke(cr);
}
}
if (drag_mode_ == DragMode::FillHandle && !cols.empty() && !rows.empty()) {
CellRange fp = compute_fill_preview(sheet);
int fx1 = data_r.x, fy1 = data_r.y, fx2 = data_r.x, fy2 = data_r.y;
if (fp.c1 < cols.front().col) fx1 = data_r.x;
else { int wu = 0; if (!col_x_range(fp.c1, fx1, wu)) fx1 = data_r.x; }
if (fp.c2 > cols.back().col) fx2 = cols.back().x + cols.back().w;
else { int x0 = 0, w0 = 0; if (col_x_range(fp.c2, x0, w0)) fx2 = x0 + w0; }
if (fp.r1 < rows.front().row) fy1 = data_r.y;
else { int hu = 0; if (!row_y_range(fp.r1, fy1, hu)) fy1 = data_r.y; }
if (fp.r2 > rows.back().row) fy2 = rows.back().y + rows.back().h;
else { int y0 = 0, h0 = 0; if (row_y_range(fp.r2, y0, h0)) fy2 = y0 + h0; }
const double dash[2] = {4.0 * ui.scale, 3.0 * ui.scale};
gemui_set_source(cr, t->highlight);
cairo_set_dash(cr, dash, 2, 0.0);
cairo_set_line_width(cr, std::max(1.5, 1.5 * ui.scale));
cairo_rectangle(cr, fx1 + 1.0, fy1 + 1.0, std::max(1, fx2 - fx1 - 2),
std::max(1, fy2 - fy1 - 2));
cairo_stroke(cr);
cairo_set_dash(cr, nullptr, 0, 0.0);
}
cairo_restore(cr);
// Header dividers
gemui_set_source(cr, t->border);
cairo_set_line_width(cr, line_w);
for (const auto& cl : cols) {
double lx = static_cast<double>(cl.x + cl.w) + 0.5;
cairo_move_to(cr, lx, rect.y);
cairo_line_to(cr, lx, rect.y + m.col_hdr_h);
}
for (const auto& rl : rows) {
double ly = static_cast<double>(rl.y + rl.h) + 0.5;
cairo_move_to(cr, rect.x, ly);
cairo_line_to(cr, rect.x + m.row_hdr_w, ly);
}
cairo_stroke(cr);
int div_px = std::max(1, spx(1, ui));
gemui_fill_rect(cr, gemui_rect_t{rect.x, rect.y + m.col_hdr_h - div_px, rect.w, div_px},
t->border);
gemui_fill_rect(cr, gemui_rect_t{rect.x + m.row_hdr_w - div_px, rect.y, div_px, rect.h},
t->border);
// Vertical scrollbar
gemui_fill_rect(cr, vscroll_r, t->bg_bar);
int total_rows = max_scroll_rows(wb);
int vis_rows = std::max(1, static_cast<int>(rows.size()));
int min_thumb_h = spx(14, ui);
int thumb_h = std::max(min_thumb_h, (vscroll_r.h * vis_rows) / std::max(1, total_rows));
if (thumb_h > vscroll_r.h) thumb_h = vscroll_r.h;
int max_top = std::max(1, total_rows - vis_rows);
int thumb_y = vscroll_r.y +
((vscroll_r.h - thumb_h) * std::min(scroll_row_, max_top)) / max_top;
int sb_pad = std::max(1, spx(1, ui));
gemui_fill_round(cr,
gemui_rect_t{vscroll_r.x + sb_pad, thumb_y + sb_pad,
std::max(2, vscroll_r.w - sb_pad * 2),
std::max(4, thumb_h - sb_pad * 2)},
2.0 * ui.scale, drag_mode_ == DragMode::ScrollV ? t->accent : t->border);
}
void GridWidget::draw_hscroll(gemui_app_t* app,
cairo_t* cr,
const Workbook& wb,
gemui_rect_t rect,
const UiScale& ui,
int hit_id_hscroll) {
if (rect.w < spx(12, ui) || rect.h < spx(6, ui)) return;
const gemui_theme_t* t = gemui_theme(app);
double rad = 2.0 * ui.scale;
gemui_fill_round(cr, rect, rad, t->bg_input);
gemui_stroke_round(cr, rect, rad, std::max(1.0, ui.scale), t->border);
int total_cols = max_scroll_cols(wb);
int vis_cols = 4;
if (last_data_rect_.w > 0) {
int def_col_px = std::max(1, spx(metrics_for(ui).default_col_w_logical, ui));
vis_cols = std::max(1, last_data_rect_.w / def_col_px);
}
int min_thumb_w = spx(12, ui);
int thumb_w = std::max(min_thumb_w, (rect.w * vis_cols) / std::max(1, total_cols));
if (thumb_w > rect.w) thumb_w = rect.w;
int max_left = std::max(1, total_cols - vis_cols);
int thumb_x = rect.x + ((rect.w - thumb_w) * std::min(scroll_col_, max_left)) / max_left;
int pad = std::max(1, spx(1, ui));
gemui_fill_round(cr,
gemui_rect_t{thumb_x + pad, rect.y + pad,
std::max(4, thumb_w - pad * 2),
std::max(3, rect.h - pad * 2)},
rad, drag_mode_ == DragMode::ScrollH ? t->accent : t->border);
gemui_hit_add(app, rect, hit_id_hscroll, 0);
}
void GridWidget::hit_cell_at(const Workbook& wb, int x, int y, int& out_row, int& out_col) const {
int cx = last_data_rect_.x;
int c = scroll_col_;
while (c + 1 < kMaxCols && cx + col_w(wb, c) <= x) {
cx += col_w(wb, c);
++c;
}
int cy = last_data_rect_.y;
int r = scroll_row_;
while (r + 1 < kMaxRows && cy + row_h(wb, r) <= y) {
cy += row_h(wb, r);
++r;
}
out_row = std::max(0, r);
out_col = std::max(0, c);
}
void GridWidget::update_scroll_v_from_y(const Workbook& wb, int y) {
if (last_vscroll_rect_.h <= 0) return;
double norm = static_cast<double>(y - last_vscroll_rect_.y) /
static_cast<double>(last_vscroll_rect_.h);
norm = std::clamp(norm, 0.0, 1.0);
int max_r = std::max(1, max_scroll_rows(wb) - 5);
scroll_row_ = static_cast<int>(std::llround(norm * max_r));
}
void GridWidget::handle_hscroll_drag(const Workbook& wb, int x, gemui_rect_t hscroll_rect) {
if (hscroll_rect.w <= 0) return;
double norm = static_cast<double>(x - hscroll_rect.x) / static_cast<double>(hscroll_rect.w);
norm = std::clamp(norm, 0.0, 1.0);
int max_c = std::max(1, max_scroll_cols(wb) - 2);
scroll_col_ = static_cast<int>(std::llround(norm * max_c));
}
GridHitRegion GridWidget::hit_region(const Workbook& wb,
int x,
int y,
int& out_row,
int& out_col) const {
if (x >= last_data_rect_.x && x < last_data_rect_.x + last_data_rect_.w &&
y >= last_data_rect_.y && y < last_data_rect_.y + last_data_rect_.h) {
hit_cell_at(wb, x, y, out_row, out_col);
return GridHitRegion::DataCell;
}
if (y >= last_rect_.y && y < last_data_rect_.y &&
x >= last_data_rect_.x && x < last_data_rect_.x + last_data_rect_.w) {
int cx = last_data_rect_.x;
int c = scroll_col_;
while (c + 1 < kMaxCols && cx + col_w(wb, c) <= x) {
cx += col_w(wb, c);
++c;
}
out_row = active_row_;
out_col = c;
return GridHitRegion::ColHeader;
}
if (x >= last_rect_.x && x < last_data_rect_.x &&
y >= last_data_rect_.y && y < last_data_rect_.y + last_data_rect_.h) {
int cy = last_data_rect_.y;
int r = scroll_row_;
while (r + 1 < kMaxRows && cy + row_h(wb, r) <= y) {
cy += row_h(wb, r);
++r;
}
out_row = r;
out_col = active_col_;
return GridHitRegion::RowHeader;
}
return GridHitRegion::Outside;
}
bool GridWidget::handle_pointer(Workbook& wb,
int type,
int x,
int y,
unsigned int btn,
gemui_rect_t hscroll_rect,
bool& edited_or_committed_out) {
edited_or_committed_out = false;
const GridMetrics m = metrics_for(last_ui_);
int hit_tol = spx(5, last_ui_);
auto in_rect = [](gemui_rect_t r, int px, int py) {
return px >= r.x && px < r.x + r.w && py >= r.y && py < r.y + r.h;
};
if (type == GEMUI_PTR_SCROLL_UP || type == GEMUI_PTR_SCROLL_DOWN) {
int delta = (type == GEMUI_PTR_SCROLL_UP) ? -3 : 3;
if (in_rect(hscroll_rect, x, y)) {
scroll_col_ = std::clamp(scroll_col_ + (delta > 0 ? 2 : -2), 0, kMaxCols - 1);
return true;
}
if (in_rect(last_rect_, x, y)) {
scroll_row_ = std::clamp(scroll_row_ + delta, 0, kMaxRows - 1);
return true;
}
return false;
}
if (type == GEMUI_PTR_DOWN) {
if (btn == 6) {
scroll_col_ = std::clamp(scroll_col_ - 2, 0, kMaxCols - 1);
return true;
}
if (btn == 7) {
scroll_col_ = std::clamp(scroll_col_ + 2, 0, kMaxCols - 1);
return true;
}
if (btn != Button1) {
return false;
}
if (in_rect(hscroll_rect, x, y)) {
drag_mode_ = DragMode::ScrollH;
handle_hscroll_drag(wb, x, hscroll_rect);
return true;
}
if (in_rect(last_vscroll_rect_, x, y)) {
drag_mode_ = DragMode::ScrollV;
update_scroll_v_from_y(wb, y);
return true;
}
if (!in_rect(last_rect_, x, y)) {
return false;
}
if (has_fill_handle_ &&
x >= last_fill_handle_.x - hit_tol &&
x <= last_fill_handle_.x + last_fill_handle_.w + hit_tol &&
y >= last_fill_handle_.y - hit_tol &&
y <= last_fill_handle_.y + last_fill_handle_.h + hit_tol) {
if (editing_) { commit_edit(wb); edited_or_committed_out = true; }
drag_mode_ = DragMode::FillHandle;
fill_hover_row_ = active_row_;
fill_hover_col_ = active_col_;
return true;
}
if (y >= last_rect_.y && y < last_data_rect_.y && x >= last_data_rect_.x) {
if (editing_) { commit_edit(wb); edited_or_committed_out = true; }
int cx = last_data_rect_.x;
int c = scroll_col_;
while (cx < last_data_rect_.x + last_data_rect_.w && c < kMaxCols) {
int w = col_w(wb, c);
if (std::abs(x - (cx + w)) <= hit_tol) {
drag_mode_ = DragMode::ResizeCol;
resize_index_ = c;
resize_start_px_ = x;
resize_orig_size_ = w;
resize_live_size_ = w;
return true;
}
if (x >= cx && x < cx + w) {
set_active_cell(wb, active_row_, c, false);
return true;
}
cx += w;
++c;
}
return true;
}
if (x >= last_rect_.x && x < last_data_rect_.x && y >= last_data_rect_.y) {
if (editing_) { commit_edit(wb); edited_or_committed_out = true; }
int cy = last_data_rect_.y;
int r = scroll_row_;
while (cy < last_data_rect_.y + last_data_rect_.h && r < kMaxRows) {
int h = row_h(wb, r);
if (std::abs(y - (cy + h)) <= hit_tol) {
drag_mode_ = DragMode::ResizeRow;
resize_index_ = r;
resize_start_px_ = y;
resize_orig_size_ = h;
resize_live_size_ = h;
return true;
}
if (y >= cy && y < cy + h) {
set_active_cell(wb, r, active_col_, false);
return true;
}
cy += h;
++r;
}
return true;
}
if (in_rect(last_data_rect_, x, y)) {
int r = 0, c = 0;
hit_cell_at(wb, x, y, r, c);
if (editing_ && (r != active_row_ || c != active_col_)) {
commit_edit(wb);
edited_or_committed_out = true;
}
uint64_t now = monotonic_ms();
bool dbl = (r == last_click_row_ && c == last_click_col_ && (now - last_click_ms_) <= 400);
last_click_row_ = r;
last_click_col_ = c;
last_click_ms_ = now;
if (dbl && !editing_) {
set_active_cell(wb, r, c, false);
start_edit(wb, false);
} else {
set_active_cell(wb, r, c, false);
drag_mode_ = DragMode::SelectRange;
}
return true;
}
return true;
}
if (type == GEMUI_PTR_MOVE) {
if (drag_mode_ == DragMode::Idle) return false;
if (drag_mode_ == DragMode::ScrollV) {
update_scroll_v_from_y(wb, y);
return true;
}
if (drag_mode_ == DragMode::ScrollH) {
handle_hscroll_drag(wb, x, hscroll_rect);
return true;
}
if (drag_mode_ == DragMode::ResizeCol) {
resize_live_size_ = std::clamp(resize_orig_size_ + (x - resize_start_px_),
spx(24, last_ui_), spx(360, last_ui_));
return true;
}
if (drag_mode_ == DragMode::ResizeRow) {
resize_live_size_ = std::clamp(resize_orig_size_ + (y - resize_start_px_),
spx(m.font_logical_px + 4, last_ui_), spx(160, last_ui_));
return true;
}
if (drag_mode_ == DragMode::SelectRange) {
int cx = std::clamp(x, last_data_rect_.x, last_data_rect_.x + last_data_rect_.w - 1);
int cy = std::clamp(y, last_data_rect_.y, last_data_rect_.y + last_data_rect_.h - 1);
int r = 0, c = 0;
hit_cell_at(wb, cx, cy, r, c);
if (y > last_data_rect_.y + last_data_rect_.h) ++r;
if (x > last_data_rect_.x + last_data_rect_.w) ++c;
set_active_cell(wb, r, c, true);
return true;
}
if (drag_mode_ == DragMode::FillHandle) {
int cx = std::clamp(x, last_data_rect_.x, last_data_rect_.x + last_data_rect_.w - 1);
int cy = std::clamp(y, last_data_rect_.y, last_data_rect_.y + last_data_rect_.h - 1);
hit_cell_at(wb, cx, cy, fill_hover_row_, fill_hover_col_);
return true;
}
return true;
}
if (type == GEMUI_PTR_UP) {
if (drag_mode_ == DragMode::Idle) return false;
if (drag_mode_ == DragMode::ResizeCol && resize_index_ >= 0) {
int logical_w = std::max(24, static_cast<int>(std::llround(
static_cast<double>(resize_live_size_) / last_ui_.scale)));
wb.set_col_width(wb.active_sheet(), resize_index_, logical_w);
edited_or_committed_out = true;
} else if (drag_mode_ == DragMode::ResizeRow && resize_index_ >= 0) {
int logical_h = std::max(14, static_cast<int>(std::llround(
static_cast<double>(resize_live_size_) / last_ui_.scale)));
wb.set_row_height(wb.active_sheet(), resize_index_, logical_h);
edited_or_committed_out = true;
} else if (drag_mode_ == DragMode::FillHandle) {
int sheet = wb.active_sheet();
CellRange src = selection_range(sheet);
CellRange dst = compute_fill_preview(sheet);
if (!(dst == src)) {
wb.autofill_range(sheet, src, dst);
anchor_row_ = dst.r1;
anchor_col_ = dst.c1;
active_row_ = dst.r2;
active_col_ = dst.c2;
sync_editor_from_cell(wb);
edited_or_committed_out = true;
}
}
drag_mode_ = DragMode::Idle;
resize_index_ = -1;
return true;
}
return false;
}
} // namespace htab::ui