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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 */
20
21#pragma once
22
23#include <wx/colour.h>
24#include <wx/grid.h>
25#include <wx/generic/gridctrl.h>
26
27class wxGrid;
28
29template<typename T = wxGridCellStringRenderer>
31{
32public:
34
35 // Perfect forwarding constructor to handle any base renderer constructor arguments
36 template<typename... Args>
37 explicit STRIPED_CELL_RENDERER(Args&&... args) : T(std::forward<Args>(args)...) {}
38
39 void Draw( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
40 const wxRect& rect, int row, int col, bool isSelected ) override
41 {
42 // Draw the foreground content using the base renderer first
43 T::Draw( grid, attr, dc, rect, row, col, isSelected );
44
45 // Overlay striped background for empty cells
46 if( grid.GetCellValue( row, col ).IsEmpty() )
47 drawStripedBackground( dc, attr, rect, isSelected );
48 }
49
50 wxGridCellRenderer* Clone() const override
51 {
52 return new STRIPED_CELL_RENDERER<T>(*this);
53 }
54
55private:
56 void drawStripedBackground(wxDC& dc, wxGridCellAttr& attr, const wxRect& rect, bool isSelected) const
57 {
58 if( isSelected )
59 {
60 // For selected cells, use the selection color
61 dc.SetBrush( wxBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) ) );
62 dc.SetPen( *wxTRANSPARENT_PEN );
63 dc.DrawRectangle( rect );
64 return;
65 }
66
67 // First fill with background color
68 wxColour bgColor = attr.GetBackgroundColour();
69 dc.SetBrush( wxBrush( bgColor ) );
70 dc.SetPen( *wxTRANSPARENT_PEN );
71 dc.DrawRectangle( rect );
72
73 // Draw diagonal stripes
74 const int stripeSpacing = 20; // Distance between diagonal lines
75
76 wxColour stripeColor;
77
78 int bgLuminance = bgColor.GetLuminance();
79
80 if( bgLuminance < 128 )
81 stripeColor = wxColour( 220, 180, 180 ); // Light red for stripes
82 else
83 stripeColor = wxColour( 100, 10, 10 ); // Dark red for stripes
84
85 wxPen stripePen( stripeColor, 1, wxPENSTYLE_SOLID );
86 dc.SetPen( stripePen );
87
88 // Calculate the diagonal stripes from top-left to bottom-right
89 int startX = rect.GetLeft() - rect.GetHeight();
90 int endX = rect.GetRight() + rect.GetHeight();
91
92 // Draw diagonal lines spaced evenly
93 for( int x = startX; x < endX; x += stripeSpacing )
94 {
95 int x1 = x;
96 int y1 = rect.GetTop();
97 int x2 = x + rect.GetHeight();
98 int y2 = rect.GetBottom();
99
100 // Clip the line to the rectangle bounds
101 if( x1 < rect.GetLeft() )
102 {
103 int deltaY = rect.GetLeft() - x1;
104 x1 = rect.GetLeft();
105 y1 = rect.GetTop() + deltaY;
106 }
107
108 if( x2 > rect.GetRight() )
109 {
110 int deltaY = x2 - rect.GetRight();
111 x2 = rect.GetRight();
112 y2 = rect.GetBottom() - deltaY;
113 }
114
115 // Only draw if the line is within the rectangle
116 if( x1 <= rect.GetRight() && x2 >= rect.GetLeft() && y1 <= rect.GetBottom() && y2 >= rect.GetTop() )
117 {
118 dc.DrawLine( x1, y1, x2, y2 );
119 }
120 }
121 }
122};
123
124
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.