#pragma once #include "engine/workbook.hpp" #include "ui/text_edit.hpp" #include #include #include #include #include namespace htab::ui { enum class GridHitRegion { Outside, DataCell, ColHeader, RowHeader, }; struct GridMetrics { int row_hdr_w = 48; int col_hdr_h = 26; int default_col_w_logical = 96; int default_row_h_logical = 26; int font_logical_px = 13; int vscroll_w = 12; }; class GridWidget { public: static GridMetrics metrics_for(const UiScale& ui); int active_row() const { return active_row_; } int active_col() const { return active_col_; } int scroll_row() const { return scroll_row_; } int scroll_col() const { return scroll_col_; } CellRange selection_range(int sheet_idx) const; bool is_editing() const { return editing_; } bool is_formula_bar_focused() const { return formula_bar_focused_; } TextEditor& editor() { return editor_; } const TextEditor& editor() const { return editor_; } void reset_for_sheet(const Workbook& wb, int sheet_idx); void set_active_cell(const Workbook& wb, int row, int col, bool keep_anchor = false); void move_active(const Workbook& wb, int drow, int dcol, bool extend_selection); void ensure_visible(const Workbook& wb, int row, int col); void start_edit(const Workbook& wb, bool focus_formula_bar, const std::string* initial = nullptr); void commit_edit(Workbook& wb); void cancel_edit(const Workbook& wb); void sync_editor_from_cell(const Workbook& wb); void draw(gemui_app_t* app, cairo_t* cr, const Workbook& wb, gemui_rect_t rect, const UiScale& ui); void draw_hscroll(gemui_app_t* app, cairo_t* cr, const Workbook& wb, gemui_rect_t rect, const UiScale& ui, int hit_id_hscroll); bool handle_pointer(Workbook& wb, int type, int x, int y, unsigned int btn, gemui_rect_t hscroll_rect, bool& edited_or_committed_out); GridHitRegion hit_region(const Workbook& wb, int x, int y, int& out_row, int& out_col) const; void handle_hscroll_drag(const Workbook& wb, int x, gemui_rect_t hscroll_rect); private: enum class DragMode { Idle, SelectRange, FillHandle, ResizeCol, ResizeRow, ScrollV, ScrollH, }; struct ColLayout { int col = 0; int x = 0; int w = 0; }; struct RowLayout { int row = 0; int y = 0; int h = 0; }; int col_w(const Workbook& wb, int col) const; int row_h(const Workbook& wb, int row) const; int max_scroll_rows(const Workbook& wb) const; int max_scroll_cols(const Workbook& wb) const; CellRange compute_fill_preview(int sheet_idx) const; void hit_cell_at(const Workbook& wb, int x, int y, int& out_row, int& out_col) const; void update_scroll_v_from_y(const Workbook& wb, int y); int scroll_row_ = 0; int scroll_col_ = 0; int active_row_ = 0; int active_col_ = 0; int anchor_row_ = 0; int anchor_col_ = 0; bool editing_ = false; bool formula_bar_focused_ = false; TextEditor editor_; gemui_rect_t last_rect_{0, 0, 0, 0}; gemui_rect_t last_data_rect_{0, 0, 0, 0}; gemui_rect_t last_vscroll_rect_{0, 0, 0, 0}; gemui_rect_t last_fill_handle_{0, 0, 0, 0}; UiScale last_ui_{}; bool has_fill_handle_ = false; DragMode drag_mode_ = DragMode::Idle; int resize_index_ = -1; int resize_start_px_ = 0; int resize_orig_size_ = 0; int resize_live_size_ = 0; int fill_hover_row_ = 0; int fill_hover_col_ = 0; int last_click_row_ = -1; int last_click_col_ = -1; uint64_t last_click_ms_ = 0; }; } // namespace htab::ui