Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
table.hpp
Go to the documentation of this file.
1#pragma once
2
3// TableListBox — sortable, scrollable table with columns and cell renderers.
4// Each column has a header, width, and renderer. Rows are provided by a data model.
5
6#include <pulp/view/view.hpp>
7#include <string>
8#include <vector>
9#include <functional>
10#include <optional>
11#include <algorithm>
12
13namespace pulp::view {
14
17 std::string header; // Column header text
18 float width = 100.0f; // Column width in pixels
19 bool sortable = true; // Whether the column can be sorted
20 bool resizable = true; // Whether the user can resize the column
21
22 enum class Align { left, center, right };
24};
25
28public:
29 virtual ~TableModel() = default;
30
32 virtual int row_count() const = 0;
33
35 virtual std::string cell_text(int row, int column) const = 0;
36
38 virtual bool sort(int column, bool ascending) { (void)column; (void)ascending; return false; }
39
41 virtual void row_selected(int row) { (void)row; }
42
44 virtual void row_double_clicked(int row) { (void)row; }
45};
46
49public:
50 void set_data(std::vector<std::vector<std::string>> rows) {
51 rows_ = std::move(rows);
52 }
53
54 void add_row(std::vector<std::string> row) {
55 rows_.push_back(std::move(row));
56 }
57
58 int row_count() const override { return static_cast<int>(rows_.size()); }
59
60 std::string cell_text(int row, int column) const override {
61 if (row < 0 || row >= row_count()) return "";
62 auto& r = rows_[static_cast<size_t>(row)];
63 if (column < 0 || column >= static_cast<int>(r.size())) return "";
64 return r[static_cast<size_t>(column)];
65 }
66
67 bool sort(int column, bool ascending) override {
68 if (column < 0) return false;
69 auto col = static_cast<size_t>(column);
70 std::sort(rows_.begin(), rows_.end(),
71 [col, ascending](const auto& a, const auto& b) {
72 std::string va = col < a.size() ? a[col] : "";
73 std::string vb = col < b.size() ? b[col] : "";
74 return ascending ? va < vb : va > vb;
75 });
76 return true;
77 }
78
79private:
80 std::vector<std::vector<std::string>> rows_;
81};
82
84class TableListBox : public View {
85public:
86 // No AccessRole::table here, deliberately. Rows and cells are PAINTED, not
87 // child Views, so a `table` role would export an AXTable / UIA Table with
88 // zero rows: assistive tech announces "table, 0 rows" and its grid
89 // navigation lands on nothing. A container role is only true once the
90 // children that make it true exist (per-row `row` / `cell` elements — the
91 // follow-up recorded in docs/guides/modules/view.md). Until then the widget
92 // stays out of the tree, exactly as it was before. VirtualList, which DOES
93 // hold child Views, exposes list/list_item today.
95
97 void set_model(TableModel* model) { model_ = model; }
98
100 void add_column(TableColumn column) { columns_.push_back(std::move(column)); }
101
103 void clear_columns() { columns_.clear(); }
104
106 void set_selected_row(int row) { selected_row_ = row; }
107 int selected_row() const { return selected_row_; }
108
110 void set_row_height(float h) { row_height_ = h; }
111 float row_height() const { return row_height_; }
112
114 void set_header_height(float h) { header_height_ = h; }
115
117 int column_count() const { return static_cast<int>(columns_.size()); }
118
120 std::function<void(int row)> on_selection_changed;
121
122 void paint(canvas::Canvas& canvas) override;
123 void on_mouse_down(Point pos) override;
124 bool wants_mouse_input() const override { return true; }
125
126private:
127 TableModel* model_ = nullptr;
128 std::vector<TableColumn> columns_;
129 int selected_row_ = -1;
130 int sort_column_ = -1;
131 bool sort_ascending_ = true;
132 float row_height_ = 24.0f;
133 float header_height_ = 28.0f;
134 float scroll_offset_ = 0.0f;
135};
136
145
146} // namespace pulp::view
Definition canvas.hpp:240
Simple in-memory table model backed by a vector of string vectors.
Definition table.hpp:48
void set_data(std::vector< std::vector< std::string > > rows)
Definition table.hpp:50
void add_row(std::vector< std::string > row)
Definition table.hpp:54
std::string cell_text(int row, int column) const override
Cell text for a given row and column index.
Definition table.hpp:60
int row_count() const override
Number of rows in the table.
Definition table.hpp:58
bool sort(int column, bool ascending) override
Optional: sort the data by column. Return true if handled.
Definition table.hpp:67
Scrollable table with sortable columns.
Definition table.hpp:84
void on_mouse_down(Point pos) override
TableListBox()
Definition table.hpp:94
int column_count() const
Number of columns.
Definition table.hpp:117
bool wants_mouse_input() const override
True if this widget handles mouse down/drag through virtual methods even when it does not expose JS p...
Definition table.hpp:124
void set_row_height(float h)
Row height.
Definition table.hpp:110
void add_column(TableColumn column)
Add a column definition.
Definition table.hpp:100
void clear_columns()
Clear all columns.
Definition table.hpp:103
float row_height() const
Definition table.hpp:111
void set_model(TableModel *model)
Set the data model (not owned — caller must keep alive).
Definition table.hpp:97
std::function< void(int row)> on_selection_changed
Called when selection changes.
Definition table.hpp:120
void paint(canvas::Canvas &canvas) override
void set_header_height(float h)
Header height.
Definition table.hpp:114
int selected_row() const
Definition table.hpp:107
void set_selected_row(int row)
Get/set selected row (-1 = none).
Definition table.hpp:106
Data model for TableListBox — implement this to provide row data.
Definition table.hpp:27
virtual bool sort(int column, bool ascending)
Optional: sort the data by column. Return true if handled.
Definition table.hpp:38
virtual std::string cell_text(int row, int column) const =0
Cell text for a given row and column index.
virtual int row_count() const =0
Number of rows in the table.
const TableColumn & column(std::size_t i) const
Definition table_model.hpp:72
virtual void row_double_clicked(int row)
Optional: called when a row is double-clicked.
Definition table.hpp:44
virtual void row_selected(int row)
Optional: called when a row is selected.
Definition table.hpp:41
virtual ~TableModel()=default
@ row
Definition view.hpp:991
void set_focusable(bool f)
Definition view.hpp:669
Definition affine_transform.hpp:24
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
TableListBox Table
Design-system alias.
Definition table.hpp:144
@ a
Definition input_events.hpp:39
@ h
Definition input_events.hpp:39
@ r
Definition input_events.hpp:41
@ b
Definition input_events.hpp:39
@ column
Definition design_ir.hpp:53
@ row
Definition design_ir.hpp:53
Definition geometry.hpp:20
Column definition for TableListBox.
Definition table.hpp:16
Align align
Definition table.hpp:23
bool resizable
Definition table.hpp:20
Align
Definition table.hpp:22
@ right
Definition table.hpp:22
@ left
Definition table.hpp:22
@ center
Definition table.hpp:22
std::string header
Definition table.hpp:17
bool sortable
Definition table.hpp:19
float width
Definition table.hpp:18