#pragma once #include "engine/address.hpp" #include "engine/parser.hpp" #include "engine/value.hpp" #include #include #include #include #include namespace htab { class DependencyGraph; struct Cell { std::string raw_input; std::string number_format; Value value; std::shared_ptr ast; bool parse_error = false; }; struct Sheet { std::string name; std::unordered_map cells; std::unordered_map col_widths; std::unordered_map row_heights; }; struct CellEdit { int row = 0; int col = 0; std::string raw_input; std::optional number_format; }; struct ClipboardCell { int dr = 0; int dc = 0; std::string raw_input; std::string number_format; Value evaluated_value; }; struct ClipboardGrid { int src_sheet = 0; CellRange src_range; std::vector cells; bool is_cut = false; }; class Workbook { public: struct Command { virtual ~Command() = default; virtual void undo(Workbook& wb) = 0; virtual void redo(Workbook& wb) = 0; }; Workbook(); ~Workbook(); Workbook(Workbook&&) noexcept; Workbook& operator=(Workbook&&) noexcept; Workbook(const Workbook&) = delete; Workbook& operator=(const Workbook&) = delete; int sheet_count() const { return static_cast(sheets_.size()); } int active_sheet() const { return active_sheet_; } void set_active_sheet(int idx); const Sheet& sheet(int idx) const { return sheets_[static_cast(idx)]; } Sheet& sheet_mut(int idx) { return sheets_[static_cast(idx)]; } int find_sheet(std::string_view name) const; int add_sheet(std::string name = ""); bool rename_sheet(int sheet_idx, const std::string& new_name); bool delete_sheet(int sheet_idx); void set_cell_input(int sheet_idx, int row, int col, const std::string& raw); void apply_cell_edits(int sheet_idx, const std::vector& edits); void set_cell_format(int sheet_idx, int row, int col, const std::string& fmt); void apply_range_format(int sheet_idx, CellRange range, const std::string& fmt); void insert_rows(int sheet_idx, int row, int count = 1); void delete_rows(int sheet_idx, int row, int count = 1); void insert_cols(int sheet_idx, int col, int count = 1); void delete_cols(int sheet_idx, int col, int count = 1); void set_col_width(int sheet_idx, int col, int width); int get_col_width(int sheet_idx, int col, int default_width) const; void set_row_height(int sheet_idx, int row, int height); int get_row_height(int sheet_idx, int row, int default_height) const; bool can_undo() const { return !undo_stack_.empty(); } bool can_redo() const { return !redo_stack_.empty(); } bool undo(); bool redo(); void clear_history(); const Cell* get_cell(int sheet_idx, int row, int col) const; Value get_scalar_value(int sheet_idx, int row, int col) const; std::string get_formatted_value(int sheet_idx, int row, int col) const; bool is_spilled_cell(int sheet_idx, int row, int col) const; std::optional get_spill_range(int sheet_idx, int row, int col) const; ClipboardGrid copy_range(int sheet_idx, CellRange range, bool is_cut = false) const; void paste_range(int sheet_idx, int dest_row, int dest_col, const ClipboardGrid& grid, bool translate_refs = true); void autofill_range(int sheet_idx, CellRange src, CellRange dst); void replace_all_sheets(std::vector new_sheets, int active_sheet); void replace_sheet_cells(int sheet_idx, std::unordered_map new_cells); std::optional get_used_bounds(int sheet_idx) const; void recalculate_all(); const DependencyGraph& dag() const { return *dag_; } private: friend class EditCellsCmd; friend class SnapshotCmd; void parse_cell_input(Cell& cell, const std::string& raw); void set_cell_raw_no_recalc(int sheet_idx, int row, int col, const std::string& raw, const std::optional& fmt_override, std::vector& dirty_seeds); void push_command(std::unique_ptr cmd); void rewrite_all_formulas(const std::string& target_sheet, StructuralOp op, int index, int count, const std::string& new_name = ""); std::vector sheets_; int active_sheet_ = 0; std::unique_ptr dag_; std::vector> undo_stack_; std::vector> redo_stack_; }; } // namespace htab