cxWidgets 1.0
cxGrid.h
Go to the documentation of this file.
1// Copyright (c) 2026 E. Oulashin
2#ifndef __CXGRID_H__
3#define __CXGRID_H__
4
5// cxGrid: A text-based grid/spreadsheet control derived from cxWindow.
6// Similar in concept to wxGrid from wxWidgets. Each cell contains a
7// std::shared_ptr<cxWindow>, defaulting to cxInput. Cells can be any
8// cxWidgets control (cxInput, cxMultiLineInput, cxComboBox, cxLabel, etc.).
9//
10// The grid displays:
11// - Column headers (A, B, C, ... or custom labels) across the top
12// - Row headers (1, 2, 3, ... or custom labels) down the left side
13// - A scrollable grid of cells, each containing a cxWindow-derived widget
14//
15// Navigation: Tab/Shift+Tab or arrow keys move between cells.
16
17#include "cxWindow.h"
18#include "cxInput.h"
19#include "cxLabel.h"
20#include <string>
21#include <vector>
22#include <memory>
23
24namespace cx {
25
30class cxGrid : public cxWindow
31{
32 public:
33
48 explicit cxGrid(cxWindow *pParentWindow = nullptr,
49 int pRow = 0, int pCol = 0,
50 int pHeight = DEFAULT_HEIGHT, int pWidth = DEFAULT_WIDTH,
51 int pNumRows = 10, int pNumCols = 5,
52 int pDefaultColWidth = 12,
53 const std::string& pTitle = "",
54 eBorderStyle pBorderStyle = eBS_SINGLE_LINE);
55
59 cxGrid(const cxGrid& pThatGrid);
60
64 virtual ~cxGrid();
65
66 // Overridden virtual functions from cxWindow
67
68 virtual long show(bool pBringToTop = false, bool pShowSubwindows = true) override;
69 virtual long showModal(bool pShowSelf = true, bool pBringToTop = true, bool pShowSubwindows = true) override;
70 virtual bool modalGetsKeypress() const override;
71 virtual void draw() override;
72 virtual void hide(bool pHideSubwindows = true) override;
73 virtual void unhide(bool pUnhideSubwindows = true) override;
74 virtual void erase(bool pEraseSubwindows = true) override;
75 virtual bool move(int pNewRow, int pNewCol, bool pRefresh = true) override;
76 virtual void resize(int pNewHeight, int pNewWidth, bool pRefresh = true) override;
77 virtual void clear(bool pRefresh = false) override;
78 virtual void bringToTop(bool pRefresh = true) override;
79 virtual bool hasFocus() const override;
80 virtual void setColor(e_WidgetItems pItem, e_cxColors pColor) override;
81 virtual void setBorderStyle(eBorderStyle pBorderStyle) override;
82 virtual void setLastKey(int pLastKey) override;
83 virtual void quitNow() override;
84 virtual void exitNow() override;
85 virtual bool setKeyFunction(int pKey, const std::shared_ptr<cxFunction>& pFunction) override;
86 virtual void clearKeyFunction(int pKey) override;
87 virtual void clearKeyFunctions() override;
88 virtual void removeQuitKey(int pKey) override;
89 virtual void removeExitKey(int pKey) override;
90 virtual void setEnabled(bool pEnabled) override;
91 virtual void setDisableCursorOnShow(bool pDisableCursorOnShow) override;
92 virtual std::string cxTypeStr() const override;
93 virtual cxWindow* getParent() const override;
94 virtual void addAttr(e_WidgetItems pItem, attr_t pAttr) override;
95 virtual void setAttr(e_WidgetItems pItem, attr_t pAttr) override;
96 virtual void removeAttr(e_WidgetItems pItem, attr_t pAttr) override;
97 virtual void removeAttrs(e_WidgetItems pItem) override;
98 virtual void getAttrs(e_WidgetItems pItem, std::set<attr_t>& pAttrs) const override;
99 virtual void enableAttrs(WINDOW *pWin, e_WidgetItems pItem) override;
100 virtual void disableAttrs(WINDOW *pWin, e_WidgetItems pItem) override;
101 virtual e_cxColors getItemColor(e_WidgetItems pItem) const override;
102
103 // Grid-specific functions
104
119 virtual long showModal(int pFocusRow, int pFocusCol, bool pShowSelf = true, bool pBringToTop = true, bool pShowSubwindows = true);
120
124 int getNumRows() const;
125
129 int getNumCols() const;
130
135 void setNumRows(int pNumRows);
136
141 void setNumCols(int pNumCols);
142
149 std::shared_ptr<cxWindow> getCell(int pRow, int pCol) const;
150
158 bool setCell(int pRow, int pCol, const std::shared_ptr<cxWindow>& pWidget);
159
166 std::string getCellValue(int pRow, int pCol) const;
167
175 bool setCellValue(int pRow, int pCol, const std::string& pValue);
176
182 void setColLabel(int pCol, const std::string& pLabel);
183
189 std::string getColLabel(int pCol) const;
190
196 void setRowLabel(int pRow, const std::string& pLabel);
197
203 std::string getRowLabel(int pRow) const;
204
210 void setColWidth(int pCol, int pWidth);
211
217 int getColWidth(int pCol) const;
218
223 void setRowHeaderWidth(int pWidth);
224
229 int getRowHeaderWidth() const;
230
235 int getFocusRow() const;
236
241 int getFocusCol() const;
242
248 void setFocusCell(int pRow, int pCol);
249
254 void setHeaderColor(e_cxColors pColor);
255
260 void setGridLineColor(e_cxColors pColor);
261
266 void showColHeaders(bool pShow);
267
272 bool getShowColHeaders() const;
273
278 void showRowHeaders(bool pShow);
279
284 bool getShowRowHeaders() const;
285
286 private:
287
288 // Grid dimensions (data rows/cols, not including headers)
289 int mNumRows;
290 int mNumCols;
291
292 // The 2D grid of cell widgets. Indexed as mCells[row][col].
293 std::vector<std::vector<std::shared_ptr<cxWindow>>> mCells;
294
295 // Column widths (one per column)
296 std::vector<int> mColWidths;
297
298 // Column header labels (one per column)
299 std::vector<std::string> mColLabels;
300
301 // Row header labels (one per row)
302 std::vector<std::string> mRowLabels;
303
304 // Width of the row header column (on the left)
305 int mRowHeaderWidth;
306
307 // Currently focused cell
308 int mFocusRow;
309 int mFocusCol;
310
311 // Scroll offsets (for when the grid is larger than the visible area)
312 int mScrollRow; // First visible data row
313 int mScrollCol; // First visible data column
314
315 // Whether to show headers
316 bool mShowColHeaders;
317 bool mShowRowHeaders;
318
319 // Colors for grid elements
320 short mHeaderColorPair;
321 short mGridLineColorPair;
322
323 // Default column width for new columns
324 int mDefaultColWidth;
325
326 // Helper functions
327
333 static std::string defaultColLabel(int pCol);
334
340 static std::string defaultRowLabel(int pRow);
341
348 std::shared_ptr<cxWindow> createDefaultCell(int pRow, int pCol);
349
353 void initGrid();
354
358 void drawColHeaders();
359
363 void drawRowHeaders();
364
368 void drawGridLines();
369
373 void positionCells();
374
379 int visibleRows() const;
380
385 int visibleCols() const;
386
390 int innerTop() const;
391
395 int innerLeft() const;
396
400 void ensureFocusVisible();
401
405 void focusNextCell();
406
410 void focusPrevCell();
411
420 bool findCellAt(int pY, int pX, int& pRow, int& pCol) const;
421
426 long inputLoop();
427};
428
429} // namespace cx
430
431#endif // __CXGRID_H__
This is a class representing a grid of data, similar to a spreadsheet, with rows & columns of cells w...
Definition cxGrid.h:31
std::string getCellValue(int pRow, int pCol) const
Gets the value of a cell (as a string). Works for cxInput cells.
Definition cxGrid.cpp:1166
virtual bool setKeyFunction(int pKey, const std::shared_ptr< cxFunction > &pFunction) override
Sets a function to be called when a key is pressed.
Definition cxGrid.cpp:949
virtual cxWindow * getParent() const override
Returns a pointer to the parent window.
Definition cxGrid.cpp:997
virtual void draw() override
Fills the member ncurses window structure with the current.
Definition cxGrid.cpp:464
virtual void removeExitKey(int pKey) override
Removes an exit key.
Definition cxGrid.cpp:969
virtual void addAttr(e_WidgetItems pItem, attr_t pAttr) override
Adds an ncurses attribute to use for one of the items in the.
Definition cxGrid.cpp:1002
virtual void removeAttr(e_WidgetItems pItem, attr_t pAttr) override
Removes an ncurses attribute from one of the item lists.
Definition cxGrid.cpp:1012
virtual void setColor(e_WidgetItems pItem, e_cxColors pColor) override
Sets the color of one of the window items.
Definition cxGrid.cpp:915
void setFocusCell(int pRow, int pCol)
Sets the focus to a specific cell.
Definition cxGrid.cpp:1275
int getNumRows() const
Returns the number of data rows in the grid.
Definition cxGrid.cpp:1053
virtual void removeQuitKey(int pKey) override
Removes a quit key.
Definition cxGrid.cpp:964
void showRowHeaders(bool pShow)
Toggles whether row headers are displayed.
Definition cxGrid.cpp:1304
int getFocusCol() const
Gets the currently focused cell column.
Definition cxGrid.cpp:1270
virtual void unhide(bool pUnhideSubwindows=true) override
Un-hides the window.
Definition cxGrid.cpp:832
void setHeaderColor(e_cxColors pColor)
Sets the color for the column/row header labels.
Definition cxGrid.cpp:1284
bool setCellValue(int pRow, int pCol, const std::string &pValue)
Sets the value of a cell (as a string). Works for cxInput cells.
Definition cxGrid.cpp:1184
virtual void clear(bool pRefresh=false) override
Clears the text from the window.
Definition cxGrid.cpp:870
virtual void hide(bool pHideSubwindows=true) override
Hides the window.
Definition cxGrid.cpp:818
virtual void setLastKey(int pLastKey) override
Sets the last keypress.
Definition cxGrid.cpp:934
virtual void setAttr(e_WidgetItems pItem, attr_t pAttr) override
Sets the ncurses attribute to use for one of the items in the.
Definition cxGrid.cpp:1007
virtual void getAttrs(e_WidgetItems pItem, std::set< attr_t > &pAttrs) const override
Returns the set of ncurses attributes for a given item.
Definition cxGrid.cpp:1022
bool setCell(int pRow, int pCol, const std::shared_ptr< cxWindow > &pWidget)
Sets (adds/replaces) the cell widget at the specified row and column.
Definition cxGrid.cpp:1145
int getRowHeaderWidth() const
Gets the width of the row header column.
Definition cxGrid.cpp:1260
virtual void disableAttrs(WINDOW *pWin, e_WidgetItems pItem) override
Disables the attributes for one of the m*Attrs sets for an ncurses window.
Definition cxGrid.cpp:1032
std::shared_ptr< cxWindow > getCell(int pRow, int pCol) const
Gets the cell widget at the specified row and column.
Definition cxGrid.cpp:1136
void setRowHeaderWidth(int pWidth)
Sets the width of the row header column.
Definition cxGrid.cpp:1252
virtual void erase(bool pEraseSubwindows=true) override
Erases the window.
Definition cxGrid.cpp:838
std::string getColLabel(int pCol) const
Gets a column header label.
Definition cxGrid.cpp:1209
int getNumCols() const
Returns the number of data columns in the grid.
Definition cxGrid.cpp:1058
virtual void clearKeyFunctions() override
Clears the list of external functions fired by hotkeys.
Definition cxGrid.cpp:959
int getFocusRow() const
Gets the currently focused cell row.
Definition cxGrid.cpp:1265
virtual void setEnabled(bool pEnabled) override
Enables or disables the window. If the window is.
Definition cxGrid.cpp:974
virtual bool modalGetsKeypress() const override
Returns whether or not a call to showModal() will wait for a.
Definition cxGrid.cpp:812
virtual void setBorderStyle(eBorderStyle pBorderStyle) override
Sets the border style.
Definition cxGrid.cpp:929
virtual std::string cxTypeStr() const override
Returns the name of the cxWidgets class. This is overridden.
Definition cxGrid.cpp:992
virtual void removeAttrs(e_WidgetItems pItem) override
Removes all attributes for a given window item.
Definition cxGrid.cpp:1017
virtual void bringToTop(bool pRefresh=true) override
Brings the window to the top.
Definition cxGrid.cpp:884
void setNumCols(int pNumCols)
Sets the number of columns in the grid. Adds or removes columns as needed.
Definition cxGrid.cpp:1096
virtual void resize(int pNewHeight, int pNewWidth, bool pRefresh=true) override
Changes the window's width and height. The window's upper-left.
Definition cxGrid.cpp:863
virtual e_cxColors getItemColor(e_WidgetItems pItem) const override
Returns the color of one of the items in a window.
Definition cxGrid.cpp:1037
void setNumRows(int pNumRows)
Sets the number of rows in the grid. Adds or removes rows as needed.
Definition cxGrid.cpp:1063
virtual void clearKeyFunction(int pKey) override
Removes a function pointer for a keypress so that it will no.
Definition cxGrid.cpp:954
int getColWidth(int pCol) const
Gets the width of a specific column.
Definition cxGrid.cpp:1243
virtual void exitNow() override
This is mainly for deriving classes. It tells the object.
Definition cxGrid.cpp:944
bool getShowRowHeaders() const
Returns whether row headers are displayed.
Definition cxGrid.cpp:1309
void setColLabel(int pCol, const std::string &pLabel)
Sets a column header label.
Definition cxGrid.cpp:1201
virtual ~cxGrid()
Destructor.
Definition cxGrid.cpp:133
bool getShowColHeaders() const
Returns whether column headers are displayed.
Definition cxGrid.cpp:1299
virtual void setDisableCursorOnShow(bool pDisableCursorOnShow) override
Sets whether the window should disable the cursor.
Definition cxGrid.cpp:987
cxGrid(cxWindow *pParentWindow=nullptr, int pRow=0, int pCol=0, int pHeight=DEFAULT_HEIGHT, int pWidth=DEFAULT_WIDTH, int pNumRows=10, int pNumCols=5, int pDefaultColWidth=12, const std::string &pTitle="", eBorderStyle pBorderStyle=eBS_SINGLE_LINE)
Constructor.
virtual void enableAttrs(WINDOW *pWin, e_WidgetItems pItem) override
Enables the attributes for one of the m*Attrs sets for an ncurses window.
Definition cxGrid.cpp:1027
virtual long showModal(bool pShowSelf=true, bool pBringToTop=true, bool pShowSubwindows=true) override
Shows the window and waits for input.
Definition cxGrid.cpp:783
void showColHeaders(bool pShow)
Toggles whether column headers are displayed.
Definition cxGrid.cpp:1294
void setRowLabel(int pRow, const std::string &pLabel)
Sets a row header label.
Definition cxGrid.cpp:1218
void setColWidth(int pCol, int pWidth)
Sets the width of a specific column.
Definition cxGrid.cpp:1235
virtual bool move(int pNewRow, int pNewCol, bool pRefresh=true) override
Changes the window's position, based on a new upper-left corner.
Definition cxGrid.cpp:852
std::string getRowLabel(int pRow) const
Gets a row header label.
Definition cxGrid.cpp:1226
virtual void quitNow() override
This is mainly for deriving classes. This causes the object.
Definition cxGrid.cpp:939
virtual bool hasFocus() const override
Returns true if window has focus, false otherwise.
Definition cxGrid.cpp:903
void setGridLineColor(e_cxColors pColor)
Sets the color for the grid lines/separators.
Definition cxGrid.cpp:1289
virtual long show(bool pBringToTop=false, bool pShowSubwindows=true) override
Shows the window.
Definition cxGrid.cpp:479
Represents a text-based window on the screen. Can contain a title, status, and a message to appear wi...
Definition cxWindow.h:195
#define DEFAULT_HEIGHT
Definition cxWindow.h:68
#define DEFAULT_WIDTH
Definition cxWindow.h:69
cxBorderChars.h - Defines border characters to be used in drawing a box (i.e., in cxWindow and all it...
Definition cxApp.cpp:5
e_WidgetItems
Definition cxWidgetItems.h:42
eBorderStyle
Definition cxBorderStyles.h:26
@ eBS_SINGLE_LINE
Definition cxBorderStyles.h:28
e_cxColors
Definition cxColors.h:46