KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_striped_renderer.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <wx/colour.h>
23#include <wx/grid.h>
24#include <wx/generic/gridctrl.h>
25
26class wxGrid;
27
28template<typename T = wxGridCellStringRenderer>
30{
31public:
33
34 // Perfect forwarding constructor to handle any base renderer constructor arguments
35 template<typename... Args>
36 explicit STRIPED_CELL_RENDERER(Args&&... args) : T(std::forward<Args>(args)...) {}
37
38 void Draw( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
39 const wxRect& rect, int row, int col, bool isSelected ) override
40 {
41 // Draw the foreground content using the base renderer first
42 T::Draw( grid, attr, dc, rect, row, col, isSelected );
43
44 // Overlay striped background for empty cells
45 if( grid.GetCellValue( row, col ).IsEmpty() )
46 drawStripedBackground( dc, attr, rect, isSelected );
47 }
48
49 wxGridCellRenderer* Clone() const override
50 {
51 return new STRIPED_CELL_RENDERER<T>(*this);
52 }
53
54private:
55 void drawStripedBackground(wxDC& dc, wxGridCellAttr& attr, const wxRect& rect, bool isSelected) const
56 {
57 if( isSelected )
58 {
59 // For selected cells, use the selection color
60 dc.SetBrush( wxBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) ) );
61 dc.SetPen( *wxTRANSPARENT_PEN );
62 dc.DrawRectangle( rect );
63 return;
64 }
65
66 // First fill with background color
67 wxColour bgColor = attr.GetBackgroundColour();
68 dc.SetBrush( wxBrush( bgColor ) );
69 dc.SetPen( *wxTRANSPARENT_PEN );
70 dc.DrawRectangle( rect );
71
72 // Draw diagonal stripes
73 const int stripeSpacing = 20; // Distance between diagonal lines
74
75 wxColour stripeColor;
76
77 int bgLuminance = bgColor.GetLuminance();
78
79 if( bgLuminance < 128 )
80 stripeColor = wxColour( 220, 180, 180 ); // Light red for stripes
81 else
82 stripeColor = wxColour( 100, 10, 10 ); // Dark red for stripes
83
84 wxPen stripePen( stripeColor, 1, wxPENSTYLE_SOLID );
85 dc.SetPen( stripePen );
86
87 // Calculate the diagonal stripes from top-left to bottom-right
88 int startX = rect.GetLeft() - rect.GetHeight();
89 int endX = rect.GetRight() + rect.GetHeight();
90
91 // Draw diagonal lines spaced evenly
92 for( int x = startX; x < endX; x += stripeSpacing )
93 {
94 int x1 = x;
95 int y1 = rect.GetTop();
96 int x2 = x + rect.GetHeight();
97 int y2 = rect.GetBottom();
98
99 // Clip the line to the rectangle bounds
100 if( x1 < rect.GetLeft() )
101 {
102 int deltaY = rect.GetLeft() - x1;
103 x1 = rect.GetLeft();
104 y1 = rect.GetTop() + deltaY;
105 }
106
107 if( x2 > rect.GetRight() )
108 {
109 int deltaY = x2 - rect.GetRight();
110 x2 = rect.GetRight();
111 y2 = rect.GetBottom() - deltaY;
112 }
113
114 // Only draw if the line is within the rectangle
115 if( x1 <= rect.GetRight() && x2 >= rect.GetLeft() && y1 <= rect.GetBottom() && y2 >= rect.GetTop() )
116 {
117 dc.DrawLine( x1, y1, x2, y2 );
118 }
119 }
120 }
121};
122
123
wxGridCellRenderer * Clone() const override
void drawStripedBackground(wxDC &dc, wxGridCellAttr &attr, const wxRect &rect, bool isSelected) const
STRIPED_CELL_RENDERER(Args &&... args)
void Draw(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, const wxRect &rect, int row, int col, bool isSelected) override
STRIPED_CELL_RENDERER< wxGridCellStringRenderer > STRIPED_STRING_RENDERER
STRIPED_CELL_RENDERER< wxGridCellNumberRenderer > STRIPED_NUMBER_RENDERER
STRIPED_CELL_RENDERER< wxGridCellBoolRenderer > STRIPED_BOOL_RENDERER
STL namespace.