diff --git a/samples/grid/griddemo.cpp b/samples/grid/griddemo.cpp index 037d9cc6fb..364a30448a 100644 --- a/samples/grid/griddemo.cpp +++ b/samples/grid/griddemo.cpp @@ -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); +} diff --git a/samples/grid/griddemo.h b/samples/grid/griddemo.h index 0957d40b8d..da903b2daf 100644 --- a/samples/grid/griddemo.h +++ b/samples/grid/griddemo.h @@ -49,6 +49,8 @@ class GridFrame : public wxFrame void AutoSizeCols( wxCommandEvent& ); void CellOverflow( wxCommandEvent& ); void ResizeCell( wxCommandEvent& ); + void ToggleCheckeredCells( wxCommandEvent& ); + void ToggleColouredCells( wxCommandEvent& ); void SetLabelColour( wxCommandEvent& ); void SetLabelTextColour( wxCommandEvent& ); void SetLabelFont(wxCommandEvent &); @@ -153,6 +155,8 @@ public: ID_TOGGLEGRIDLINES, ID_AUTOSIZECOLS, ID_CELLOVERFLOW, + ID_TOGGLE_CHECKERED_CELLS, + ID_TOGGLE_COLOURED_CELLS, ID_HIDECOL, ID_SHOWCOL, ID_HIDEROW,