#pragma once #include "core/xframe_core.h" //region GtkTreeView class GtkTreeView { private: Display *display_; int screen_; Window window_; GC gc_; SStyle_Manager *styles_; int x1_, y1_, x2_, y2_; std::string name_; std::string text_; std::string style_name_; // "" = manager's active style bool visible_ = true; bool enabled_ = true; bool focused_ = false; //region Tree data // Column and Node are public: set_columns/set_roots/get_roots // expose them in public signatures, so callers must be able to // name and construct them. public: struct Column { std::string title; int width; }; struct Node { std::vector cells; std::vector children; bool expanded = true; }; private: struct FlatRow { Node *node; int depth; }; std::vector columns_; std::vector roots_; int selected_row_ = 0; // index into the flattened visible rows int scroll_row_ = 0; // first visible row int header_height_ = 22; bool headers_visible_ = true; // Phase 3 (§2.17) int row_height_ = 20; // Phase 3 (§2.17): GridLines / RowSelect / MultiSelect / // SortType / Indent / ShowButtons / ShowLines / ShowRoot. bool grid_lines_ = false; bool row_select_ = true; bool multi_select_ = false; int sort_type_ = 0; // 0 none, 1 ascending, 2 descending int indent_ = 16; bool show_buttons_ = true; bool show_lines_ = true; bool show_root_ = true; std::vector multi_selected_; //endregion void flatten_rec(Node& node, int depth, std::vector& out) { // Phase 3 (§2.17): ShowRoot=false hides the root rows; their // children are promoted to depth 0. if (show_root_ || depth > 0) { FlatRow row; row.node = &node; row.depth = show_root_ ? depth : depth - 1; out.push_back(row); } if (node.expanded) { for (size_t i = 0; i < node.children.size(); ++i) { flatten_rec(node.children[i], depth + 1, out); } } } std::vector flatten() { std::vector out; for (size_t i = 0; i < roots_.size(); ++i) { flatten_rec(roots_[i], 0, out); } return out; } int header_h() { return headers_visible_ ? header_height_ : 0; } int visible_rows() { return std::max(1, (height() - header_h() - 4) / row_height_); } void keep_selection_visible() { if (selected_row_ < scroll_row_) { scroll_row_ = selected_row_; } if (selected_row_ >= scroll_row_ + visible_rows()) { scroll_row_ = selected_row_ - visible_rows() + 1; } if (scroll_row_ < 0) { scroll_row_ = 0; } } // Getter/setter pass: colours formerly hardcoded in draw()/helpers. Srgb header_fill_color_{225, 225, 225}; Srgb selection_fill_color_{70, 110, 180}; Srgb guide_color_{200, 200, 200}; Srgb text_color_{255, 255, 255}; public: std::function on_selection_changed; GtkTreeView(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) { display_ = display; screen_ = screen; window_ = window; gc_ = gc; styles_ = styles; x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height; name_ = "GtkTreeView"; text_ = "TreeView"; add_column("Name", 190); add_column("Size", 70); add_column("Kind", 90); // Sample tree: a little home folder. Node documents = make_node({"Documents", "", "Folder"}); documents.children.push_back(make_node({"report.odt", "24 KB", "Document"})); documents.children.push_back(make_node({"budget.ods", "18 KB", "Spreadsheet"})); Node pictures = make_node({"Pictures", "", "Folder"}); pictures.children.push_back(make_node({"holiday.png", "2.1 MB", "Image"})); pictures.children.push_back(make_node({"cat.jpg", "860 KB", "Image"})); Node music = make_node({"Music", "", "Folder"}); music.children.push_back(make_node({"song.flac", "31 MB", "Audio"})); roots_.push_back(documents); roots_.push_back(pictures); roots_.push_back(music); roots_.push_back(make_node({"notes.txt", "4 KB", "Text"})); } ~GtkTreeView() {} Node make_node(std::vector cells) { Node node; node.cells = cells; node.expanded = true; return node; } void set_bounds(int x, int y, int width, int height) { x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height; } int width() { return x2_ - x1_; } int height() { return y2_ - y1_; } bool contains(int x, int y) { return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; } void set_visible(bool v) { visible_ = v; } bool get_visible() { return visible_; } void set_enabled(bool e) { enabled_ = e; } bool get_enabled() { return enabled_; } void set_style_name(std::string s) { style_name_ = s; } void set_text(std::string t) { text_ = t; } std::string get_text() { return text_; } SStyle style() { return styles_->resolve(style_name_); } //region Tree specific API void add_column(std::string title, int col_width) { Column col; col.title = title; col.width = col_width; columns_.push_back(col); } int column_count() { return (int)columns_.size(); } void append_root(std::vector cells) { roots_.push_back(make_node(cells)); } void append_child(int root_index, std::vector cells) { if (root_index >= 0 && root_index < (int)roots_.size()) { roots_[root_index].children.push_back(make_node(cells)); } } void clear() { roots_.clear(); selected_row_ = 0; scroll_row_ = 0; } void expand_all() { set_expanded_rec(roots_, true); } void collapse_all() { set_expanded_rec(roots_, false); } void set_expanded_rec(std::vector& nodes, bool expanded) { for (size_t i = 0; i < nodes.size(); ++i) { nodes[i].expanded = expanded; set_expanded_rec(nodes[i].children, expanded); } } int get_selected_row() { return selected_row_; } void set_selected_row(int r) { std::vector rows = flatten(); if (r >= 0 && r < (int)rows.size()) { selected_row_ = r; multi_selected_.clear(); keep_selection_visible(); } } std::string get_selected_text() { std::vector rows = flatten(); if (selected_row_ >= 0 && selected_row_ < (int)rows.size() && !rows[selected_row_].node->cells.empty()) { return rows[selected_row_].node->cells[0]; } return ""; } void set_scroll_row(int row) { scroll_row_ = std::max(0, row); } int get_scroll_row() { return scroll_row_; } //endregion //region GtkTreeView colours (getter/setter pass: formerly hardcoded in draw()/helpers) void set_header_fill_color(Srgb c) { header_fill_color_ = c; } Srgb get_header_fill_color() { return header_fill_color_; } void set_header_fill_color_rgb(int r, int g, int b) { header_fill_color_ = Srgb{r, g, b}; } void set_selection_fill_color(Srgb c) { selection_fill_color_ = c; } Srgb get_selection_fill_color() { return selection_fill_color_; } void set_selection_fill_color_rgb(int r, int g, int b) { selection_fill_color_ = Srgb{r, g, b}; } void set_guide_color(Srgb c) { guide_color_ = c; } Srgb get_guide_color() { return guide_color_; } void set_guide_color_rgb(int r, int g, int b) { guide_color_ = Srgb{r, g, b}; } void set_text_color(Srgb c) { text_color_ = c; } Srgb get_text_color() { return text_color_; } void set_text_color_rgb(int r, int g, int b) { text_color_ = Srgb{r, g, b}; } //endregion //region GtkTreeView properties (getter/setter pass) void set_columns(const std::vector& v) { columns_ = v; } const std::vector& get_columns() { return columns_; } void set_header_height(int v) { header_height_ = v; } int get_header_height() { return header_height_; } void set_headers_visible(bool v) { headers_visible_ = v; } bool get_headers_visible() { return headers_visible_; } void set_roots(const std::vector& v) { roots_ = v; } const std::vector& get_roots() { return roots_; } // Phase 3 (§2.17): rebuild the tree from "Root/Child;Other" paths. void insert_path(std::vector& nodes, const std::vector& segs, size_t i) { if (i >= segs.size()) return; for (size_t k = 0; k < nodes.size(); ++k) { if (!nodes[k].cells.empty() && nodes[k].cells[0] == segs[i]) { insert_path(nodes[k].children, segs, i + 1); return; } } Node node = make_node({segs[i]}); nodes.push_back(node); insert_path(nodes.back().children, segs, i + 1); } void set_items(const std::string& items) { roots_.clear(); size_t pos = 0; while (true) { size_t semi = items.find(';', pos); std::string path = items.substr(pos, semi == std::string::npos ? semi : semi - pos); std::vector segs; size_t sp = 0; while (true) { size_t slash = path.find('/', sp); segs.push_back(path.substr(sp, slash == std::string::npos ? slash : slash - sp)); if (slash == std::string::npos) break; sp = slash + 1; } if (!(segs.size() == 1 && segs[0].empty())) { insert_path(roots_, segs, 0); } if (semi == std::string::npos) break; pos = semi + 1; } selected_row_ = 0; multi_selected_.clear(); scroll_row_ = 0; } void set_row_height(int v) { row_height_ = v; } int get_row_height() { return row_height_; } //region GtkTreeView view properties (Phase 3, §2.17) void set_grid_lines(bool v) { grid_lines_ = v; } bool get_grid_lines() { return grid_lines_; } void set_row_select(bool v) { row_select_ = v; } bool get_row_select() { return row_select_; } void set_multi_select(bool v) { multi_select_ = v; } bool get_multi_select() { return multi_select_; } void sort_nodes(std::vector& nodes) { if (sort_type_ == 0) return; std::sort(nodes.begin(), nodes.end(), [this](const Node& a, const Node& b) { std::string av = a.cells.empty() ? "" : a.cells[0]; std::string bv = b.cells.empty() ? "" : b.cells[0]; return sort_type_ == 1 ? av < bv : av > bv; }); for (size_t i = 0; i < nodes.size(); ++i) sort_nodes(nodes[i].children); } void set_sort_type(int s) { sort_type_ = s; sort_nodes(roots_); } int get_sort_type() { return sort_type_; } void set_indent(int v) { indent_ = v; } int get_indent() { return indent_; } void set_show_buttons(bool v) { show_buttons_ = v; } bool get_show_buttons() { return show_buttons_; } void set_show_lines(bool v) { show_lines_ = v; } bool get_show_lines() { return show_lines_; } void set_show_root(bool v) { show_root_ = v; } bool get_show_root() { return show_root_; } //endregion //endregion void draw() { if (!visible_) { return; } SStyle st = style(); xf::draw_polished(display_, window_, gc_, st, x1_, y1_, x2_, y2_); // Header band (Phase 3, §2.17: hidden when headers-visible is off). if (headers_visible_) { unsigned long header_fill = styles_->get_color_rgb(header_fill_color_.r, header_fill_color_.g, header_fill_color_.b); XSetForeground(display_, gc_, header_fill); XFillRectangle(display_, window_, gc_, x1_ + 1, y1_ + 1, width() - 2, header_height_); int col_x = x1_ + 4; for (size_t c = 0; c < columns_.size(); ++c) { xf::draw_text_left(display_, window_, gc_, st, col_x, y1_ + 1, y1_ + 1 + header_height_, columns_[c].title); col_x += columns_[c].width; if (c + 1 < columns_.size()) { XSetForeground(display_, gc_, st.lineColor_); XDrawLine(display_, window_, gc_, col_x - 4, y1_ + 3, col_x - 4, y1_ + header_height_ - 3); } } XSetForeground(display_, gc_, st.lineColor_); XDrawLine(display_, window_, gc_, x1_ + 1, y1_ + header_height_ + 1, x2_ - 1, y1_ + header_height_ + 1); } // Rows. std::vector rows = flatten(); unsigned long sel_fill = styles_->get_color_rgb(selection_fill_color_.r, selection_fill_color_.g, selection_fill_color_.b); unsigned long guide = styles_->get_color_rgb(guide_color_.r, guide_color_.g, guide_color_.b); int rows_top = y1_ + header_h() + 3; for (int i = scroll_row_; i < (int)rows.size(); ++i) { int ry = rows_top + (i - scroll_row_) * row_height_; if (ry + row_height_ > y2_ - 2) { break; } Node *node = rows[i].node; bool is_sel = (i == selected_row_) || (multi_select_ && std::find(multi_selected_.begin(), multi_selected_.end(), i) != multi_selected_.end()); if (is_sel) { XSetForeground(display_, gc_, sel_fill); // Phase 3 (§2.17): RowSelect=false highlights the first // column only. int fw = row_select_ ? width() - 4 : (columns_.empty() ? width() - 4 : columns_[0].width); XFillRectangle(display_, window_, gc_, x1_ + 2, ry, fw, row_height_); } SStyle row_style = st; if (is_sel) { row_style.text_color_ = styles_->get_color_rgb(text_color_.r, text_color_.g, text_color_.b); } int indent = rows[i].depth * indent_; // Indent guide line. if (show_lines_ && rows[i].depth > 0) { XSetForeground(display_, gc_, guide); XDrawLine(display_, window_, gc_, x1_ + 12 + indent - 8, ry, x1_ + 12 + indent - 8, ry + row_height_); } // Expander triangle. if (show_buttons_ && !node->children.empty()) { int cx = x1_ + 10 + indent; int cy = ry + row_height_ / 2; if (node->expanded) { xf::draw_triangle_down(display_, window_, gc_, row_style.text_color_, cx, cy - 3, 4); } else { xf::draw_triangle_right(display_, window_, gc_, row_style.text_color_, cx - 2, cy, 4); } } // Cells across the columns. int cell_x = x1_ + 4; for (size_t c = 0; c < columns_.size(); ++c) { std::string value = c < node->cells.size() ? node->cells[c] : ""; int tx = cell_x + (c == 0 ? 14 + indent : 0); xf::draw_text_left(display_, window_, gc_, row_style, tx, ry, ry + row_height_, value); cell_x += columns_[c].width; } if (grid_lines_) { XSetForeground(display_, gc_, guide); XDrawLine(display_, window_, gc_, x1_ + 2, ry + row_height_, x2_ - 2, ry + row_height_); } } if (focused_) { XSetForeground(display_, gc_, st.lineColor_); XDrawRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, width() - 4, height() - 4); } XFlush(display_); } bool on_button_press(int x, int y, unsigned int button) { if (!contains(x, y) || !enabled_) { return false; } focused_ = true; if (button == 4) { scroll_row_ = std::max(0, scroll_row_ - 3); return true; } if (button == 5) { scroll_row_ += 3; return true; } if (button != 1) { return false; } int rows_top = y1_ + header_h() + 3; if (y < rows_top) { return true; } int index = scroll_row_ + (y - rows_top) / row_height_; std::vector rows = flatten(); if (index < 0 || index >= (int)rows.size()) { return true; } Node *node = rows[index].node; int indent = rows[index].depth * indent_; // Click on the expander toggles the node instead of selecting. if (show_buttons_ && !node->children.empty() && x >= x1_ + 2 + indent && x <= x1_ + 18 + indent) { node->expanded = !node->expanded; if (selected_row_ >= (int)flatten().size()) { selected_row_ = (int)flatten().size() - 1; } return true; } // Phase 3 (§2.17): multi-select toggles the row in the set. if (multi_select_) { auto it = std::find(multi_selected_.begin(), multi_selected_.end(), index); if (it != multi_selected_.end()) { multi_selected_.erase(it); } else { multi_selected_.push_back(index); } selected_row_ = index; if (on_selection_changed) { on_selection_changed(); } } else if (index != selected_row_) { selected_row_ = index; if (on_selection_changed) { on_selection_changed(); } } return true; } bool on_button_release(int x, int y, unsigned int button) { (void)x; (void)y; (void)button; return false; } bool on_motion(int x, int y) { (void)x; (void)y; return false; } bool on_key(KeySym key, const char* str) { (void)str; if (!focused_ || !enabled_) { return false; } std::vector rows = flatten(); if (rows.empty()) { return false; } if (key == XK_Down && selected_row_ < (int)rows.size() - 1) { selected_row_++; keep_selection_visible(); if (on_selection_changed) { on_selection_changed(); } return true; } if (key == XK_Up && selected_row_ > 0) { selected_row_--; keep_selection_visible(); if (on_selection_changed) { on_selection_changed(); } return true; } if (key == XK_Right || key == XK_Left) { Node *node = rows[selected_row_].node; if (!node->children.empty()) { node->expanded = (key == XK_Right); return true; } } return false; } }; //endregion