Demonstrate intensive attribute usage in grid sample

Add options to fill grid either half (checkered cells) or fully (all
cells coloured) with attributes. Available from Grid > "Toggle Checkered
Cells" and "Toggle Coloured Cells".
This commit is contained in:
Dimitri Schoolwerth
2021-02-09 19:48:29 +01:00
parent b5edfa2872
commit 823d79ce06
2 changed files with 93 additions and 0 deletions

View File

@@ -293,6 +293,8 @@ wxBEGIN_EVENT_TABLE( GridFrame, wxFrame )
EVT_MENU( ID_AUTOSIZECOLS, GridFrame::AutoSizeCols )
EVT_MENU( ID_CELLOVERFLOW, GridFrame::CellOverflow )
EVT_MENU( ID_RESIZECELL, GridFrame::ResizeCell )
EVT_MENU( ID_TOGGLE_CHECKERED_CELLS, GridFrame::ToggleCheckeredCells )
EVT_MENU( ID_TOGGLE_COLOURED_CELLS, GridFrame::ToggleColouredCells )
EVT_MENU( ID_SETLABELCOLOUR, GridFrame::SetLabelColour )
EVT_MENU( ID_SETLABELTEXTCOLOUR, GridFrame::SetLabelTextColour )
EVT_MENU( ID_SETLABEL_FONT, GridFrame::SetLabelFont )
@@ -443,6 +445,10 @@ GridFrame::GridFrame()
viewMenu->AppendCheckItem(ID_AUTOSIZECOLS, "&Auto-size cols");
viewMenu->AppendCheckItem(ID_CELLOVERFLOW, "&Overflow cells");
viewMenu->AppendCheckItem(ID_RESIZECELL, "&Resize cell (7,1)");
viewMenu->AppendCheckItem(ID_TOGGLE_CHECKERED_CELLS,
"Toggle Chec&kered Cells\tCtrl+Shift+K");
viewMenu->AppendCheckItem(ID_TOGGLE_COLOURED_CELLS,
"Toggle Co&loured Cells\tCtrl+Shift+L");
viewMenu->Append(ID_HIDECOL, "&Hide column A");
viewMenu->Append(ID_SHOWCOL, "&Show column A");
viewMenu->Append(ID_HIDEROW, "&Hide row 2");
@@ -2850,3 +2856,86 @@ void GridFrame::ShowRow( wxCommandEvent& WXUNUSED(event) )
{
grid->ShowRow(1);
}
namespace
{
// Toggle status of either checkered or coloured cells in the grid.
// Note that, for shared attribute testing purposes, with checkered cells
// a cell's complete attribute is destructively overwritten (except merged
// cells which are skipped). While with coloured cells only the background
// colour changes.
void ToggleGridCells(wxGrid* grid, bool useCheckered)
{
static bool s_checkeredOn, s_colouredOn;
if ( useCheckered )
s_checkeredOn = !s_checkeredOn;
else
s_colouredOn = !s_colouredOn;
wxGridCellAttrPtr attr;
if ( useCheckered && s_checkeredOn )
{
attr = wxGridCellAttrPtr(new wxGridCellAttr);
attr->SetBackgroundColour(*wxLIGHT_GREY);
}
wxColour bgCol = grid->GetDefaultCellBackgroundColour();
for ( int row = 0; row < grid->GetNumberRows(); ++row )
{
for ( int col = 0; col < grid->GetNumberCols(); ++col )
{
if ( useCheckered && (row ^ col) & 1 )
continue;
// Skip overwriting attributes of merged cells.
int rows, cols;
if ( useCheckered
&& grid->GetCellSize(row, col, &rows, &cols)
!= wxGrid::CellSpan_None )
{
continue;
}
if ( useCheckered )
{
grid->SetAttr(row, col, attr.get());
if ( s_checkeredOn )
attr->IncRef();
}
else
{
if ( s_colouredOn )
{
const int factor = 256 / 64;
unsigned char r, g, b;
r = (127 + row * factor + col / factor) & 0xff;
g = (col * factor + row / (factor * 2)) & 0xff;
b = ((row ^ col) * factor) & 0xff;
bgCol.Set(r < 128 ? r * 2 : 255 - r * 2,
g < 128 ? g * 2 : 255 - g * 2,
b < 128 ? b * 2 : 255 - b * 2);
}
grid->SetCellBackgroundColour(row, col, bgCol);
}
}
}
grid->Refresh();
}
} // anoymous namespace
void GridFrame::ToggleCheckeredCells( wxCommandEvent& WXUNUSED(event) )
{
ToggleGridCells(grid, /* useCheckered = */ true);
}
void GridFrame::ToggleColouredCells( wxCommandEvent& WXUNUSED(event) )
{
ToggleGridCells(grid, /* useCheckered = */ false);
}