178 lines
4.8 KiB
C++
178 lines
4.8 KiB
C++
#pragma once
|
|
|
|
#include "engine/workbook.hpp"
|
|
#include "ui/clipboard.hpp"
|
|
#include "ui/formula_bar.hpp"
|
|
#include "ui/grid_widget.hpp"
|
|
#include "ui/text_edit.hpp"
|
|
|
|
#include <gemui.h>
|
|
#include <cairo/cairo.h>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace htab::ui {
|
|
|
|
class AppWindow {
|
|
public:
|
|
int run(int req_w, int req_h, bool explicit_geometry, const std::string& initial_file);
|
|
|
|
private:
|
|
enum HitId {
|
|
HIT_NONE = 0,
|
|
HIT_MENU_BTN,
|
|
HIT_UNDO_BTN,
|
|
HIT_REDO_BTN,
|
|
HIT_FORMAT_BTN,
|
|
HIT_OPEN_BTN,
|
|
HIT_SAVE_BTN,
|
|
HIT_CSV_BTN,
|
|
HIT_INS_ROW_BTN,
|
|
HIT_DEL_ROW_BTN,
|
|
HIT_INS_COL_BTN,
|
|
HIT_DEL_COL_BTN,
|
|
HIT_ADDR_BOX,
|
|
HIT_FORMULA_INPUT,
|
|
HIT_SHEET_TAB,
|
|
HIT_SHEET_CLOSE,
|
|
HIT_ADD_SHEET,
|
|
HIT_HSCROLL,
|
|
HIT_MENU_ITEM,
|
|
HIT_FORMAT_ITEM,
|
|
HIT_CTX_ITEM,
|
|
HIT_MODAL_INPUT,
|
|
HIT_MODAL_OK,
|
|
HIT_MODAL_CANCEL,
|
|
HIT_MODAL_BACKDROP,
|
|
HIT_EXIT_SAVE,
|
|
HIT_EXIT_DISCARD,
|
|
HIT_EXIT_CANCEL,
|
|
};
|
|
|
|
enum class ModalKind {
|
|
Closed,
|
|
Menu,
|
|
ContextMenu,
|
|
FormatCell,
|
|
RenameSheet,
|
|
GoToCell,
|
|
Help,
|
|
ConfirmExit,
|
|
};
|
|
|
|
enum class CtxAction {
|
|
Copy,
|
|
Cut,
|
|
Paste,
|
|
ClearCells,
|
|
FormatCell,
|
|
InsertRowAbove,
|
|
InsertRowBelow,
|
|
DeleteRow,
|
|
InsertColLeft,
|
|
InsertColRight,
|
|
DeleteCol,
|
|
RenameSheet,
|
|
AddSheet,
|
|
DeleteSheet,
|
|
};
|
|
|
|
struct CtxItem {
|
|
const char* label;
|
|
CtxAction action;
|
|
};
|
|
|
|
struct Layout {
|
|
UiScale ui;
|
|
gemui_rect_t top_bar{0, 0, 0, 0};
|
|
gemui_rect_t formula_bar{0, 0, 0, 0};
|
|
gemui_rect_t grid{0, 0, 0, 0};
|
|
gemui_rect_t bottom_bar{0, 0, 0, 0};
|
|
gemui_rect_t tabs_area{0, 0, 0, 0};
|
|
gemui_rect_t hscroll{0, 0, 0, 0};
|
|
};
|
|
|
|
static void cb_draw(gemui_app_t* app, cairo_t* cr, int w, int h, void* ud);
|
|
static void cb_key(gemui_app_t* app, KeySym ks, unsigned int state, const char* utf8, void* ud);
|
|
static void cb_ptr(gemui_app_t* app, int type, int x, int y, unsigned int btn, void* ud);
|
|
static void cb_resize(gemui_app_t* app, int w, int h, void* ud);
|
|
static void cb_theme(gemui_app_t* app, void* ud);
|
|
static bool cb_close(gemui_app_t* app, void* ud);
|
|
static void cb_open_file_done(gemui_app_t* app, bool accepted, const char* path, void* ud);
|
|
static void cb_save_native_done(gemui_app_t* app, bool accepted, const char* path, void* ud);
|
|
static void cb_export_csv_values_done(gemui_app_t* app, bool accepted, const char* path, void* ud);
|
|
static void cb_export_csv_formulas_done(gemui_app_t* app, bool accepted, const char* path, void* ud);
|
|
|
|
Layout compute_layout(int w, int h) const;
|
|
void draw(cairo_t* cr, int w, int h);
|
|
void draw_top_bar(cairo_t* cr, const Layout& lay);
|
|
void draw_bottom_bar(cairo_t* cr, const Layout& lay);
|
|
void draw_modal(cairo_t* cr, int w, int h, const UiScale& ui);
|
|
|
|
void handle_key(KeySym ks, unsigned int state, const char* utf8);
|
|
void handle_modal_key(KeySym ks, unsigned int state, const char* utf8);
|
|
void handle_ptr(int type, int x, int y, unsigned int btn);
|
|
void handle_right_click(int x, int y);
|
|
|
|
void execute_menu_action(int index);
|
|
void execute_ctx_action(CtxAction action);
|
|
void accept_modal_dialog();
|
|
void commit_edit_if_active();
|
|
bool jump_to_address(const std::string& raw);
|
|
void open_modal(ModalKind kind, const std::string& initial_text = "");
|
|
void close_modal();
|
|
void request_quit();
|
|
void confirm_exit_save();
|
|
void open_file_dialog();
|
|
void save_native_dialog();
|
|
void export_csv_dialog(bool formulas);
|
|
std::string dialog_initial_dir() const;
|
|
std::string dialog_initial_name(const std::string& default_name,
|
|
const std::string& ext_override,
|
|
const std::string& suffix_before_ext = "") const;
|
|
|
|
bool load_file_auto(const std::string& path);
|
|
bool save_native_to(const std::string& path);
|
|
void export_csv_to(const std::string& path, bool formulas);
|
|
void do_copy(bool is_cut);
|
|
void do_paste();
|
|
void clear_selection();
|
|
void delete_selected_rows();
|
|
void delete_selected_cols();
|
|
void switch_sheet(int idx);
|
|
void add_new_sheet();
|
|
void close_sheet(int idx);
|
|
void update_title();
|
|
std::string display_filename() const;
|
|
|
|
Workbook wb_;
|
|
gemui_app_t* app_ = nullptr;
|
|
X11Clipboard clipboard_;
|
|
FormulaBar formula_bar_;
|
|
GridWidget grid_;
|
|
|
|
ModalKind modal_ = ModalKind::Closed;
|
|
int menu_sel_ = 0;
|
|
int menu_scroll_ = 0;
|
|
int format_sel_ = 0;
|
|
int help_scroll_ = 0;
|
|
int help_visible_rows_ = 12;
|
|
int exit_sel_ = 0;
|
|
bool quit_after_save_ = false;
|
|
std::vector<CtxItem> ctx_items_;
|
|
int ctx_x_ = 0;
|
|
int ctx_y_ = 0;
|
|
int ctx_sel_ = 0;
|
|
int ctx_target_sheet_ = 0;
|
|
TextEditor modal_editor_;
|
|
int tab_scroll_ = 0;
|
|
bool hscroll_dragging_ = false;
|
|
std::string current_path_;
|
|
bool dirty_ = false;
|
|
gemui_rect_t last_hscroll_r_{0, 0, 0, 0};
|
|
std::vector<std::pair<gemui_rect_t, int>> last_tab_rects_;
|
|
};
|
|
|
|
} // namespace htab::ui
|