KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_checkbox.cpp
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 2
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#include "widgets/ui_common.h"
22#include <wx/renderer.h>
23
24
25//-------- Renderer ---------------------------------------------------------------------
26//
27// Augments wxGridBoolRenderer to support indeterminate state
28
29// ------------ This section copied verbatim from wxwidgets/src/generic/grid.cpp (which
30// is sadly private)
31
32// the space between the cell edge and the checkbox mark
34
35wxRect
36wxGetContentRect(wxSize contentSize,
37 const wxRect& cellRect,
38 int hAlign,
39 int vAlign)
40{
41 // Keep square aspect ratio for the checkbox, but ensure that it fits into
42 // the available space, even if it's smaller than the standard size.
43 const wxCoord minSize = wxMin(cellRect.width, cellRect.height);
44 if ( contentSize.x >= minSize || contentSize.y >= minSize )
45 {
46 // It must still have positive size, however.
47 const int fittingSize = wxMax(1, minSize - 2*GRID_CELL_CHECKBOX_MARGIN);
48
49 contentSize.x =
50 contentSize.y = fittingSize;
51 }
52
53 wxRect contentRect(contentSize);
54
55 if ( hAlign & wxALIGN_CENTER_HORIZONTAL )
56 {
57 contentRect = contentRect.CentreIn(cellRect, wxHORIZONTAL);
58 }
59 else if ( hAlign & wxALIGN_RIGHT )
60 {
61 contentRect.SetX(cellRect.x + cellRect.width
62 - contentSize.x - GRID_CELL_CHECKBOX_MARGIN);
63 }
64 else // ( hAlign == wxALIGN_LEFT ) and invalid alignment value
65 {
66 contentRect.SetX(cellRect.x + GRID_CELL_CHECKBOX_MARGIN);
67 }
68
69 if ( vAlign & wxALIGN_CENTER_VERTICAL )
70 {
71 contentRect = contentRect.CentreIn(cellRect, wxVERTICAL);
72 }
73 else if ( vAlign & wxALIGN_BOTTOM )
74 {
75 contentRect.SetY(cellRect.y + cellRect.height
76 - contentSize.y - GRID_CELL_CHECKBOX_MARGIN);
77 }
78 else // wxALIGN_TOP
79 {
80 contentRect.SetY(cellRect.y + GRID_CELL_CHECKBOX_MARGIN);
81 }
82
83 return contentRect;
84}
85
86// ------------ End copied section
87
88// ------------ This section copied from wxwidgets/src/generic/gridctrl.cpp, with changes
89// noted
90
92 wxGridCellAttr& attr,
93 wxDC& dc,
94 const wxRect& rect,
95 int row, int col,
96 bool isSelected)
97{
98 wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
99
100 int hAlign = wxALIGN_LEFT;
101 int vAlign = wxALIGN_CENTRE_VERTICAL;
102 attr.GetNonDefaultAlignment(&hAlign, &vAlign);
103
104 const wxRect checkBoxRect =
105 wxGetContentRect(GetBestSize(grid, attr, dc, row, col),
106 rect, hAlign, vAlign);
107
108 bool value;
109 if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) )
110 {
111 value = grid.GetTable()->GetValueAsBool(row, col);
112 }
113 else
114 {
115 wxString cellval( grid.GetTable()->GetValue(row, col) );
116 value = wxGridCellBoolEditor::IsTrueValue(cellval);
117 }
118
119 int flags = wxCONTROL_CELL;
120#if 1 // KiCad change
121 if( grid.GetTable()->GetValue( row, col ) == INDETERMINATE_STATE )
122 flags |= wxCONTROL_UNDETERMINED;
123 else
124#endif
125 if (value)
126 flags |= wxCONTROL_CHECKED;
127
128 wxRendererNative::Get().DrawCheckBox( &grid, dc, checkBoxRect, flags );
129}
130
131// ------------ End copied section
132
133
134
135//-------- Editor -----------------------------------------------------------------------
136//
137// None required; interactivity delegated to GRID_TRICKS.
138
139
void Draw(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, const wxRect &rect, int row, int col, bool isSelected) override
const int minSize
Push and Shove router track width and via size dialog.
const int GRID_CELL_CHECKBOX_MARGIN
wxRect wxGetContentRect(wxSize contentSize, const wxRect &cellRect, int hAlign, int vAlign)
Functions to provide common constants and other functions to assist in making a consistent UI.
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition ui_common.h:46