KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
grid_layer_box_helpers.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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <wx/textctrl.h>
27
28#include <pgm_base.h>
32#include <board.h>
33#include <lset.h>
34#include <pcb_edit_frame.h>
38
39
40//-------- Custom wxGridCellRenderers --------------------------------------------------
41
42
44 m_frame( aFrame )
45{
46}
47
48
50{
51}
52
53
54void GRID_CELL_LAYER_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
55 const wxRect& aRect, int aRow, int aCol, bool isSelected )
56{
57 int value = aGrid.GetTable()->GetValueAsLong( aRow, aCol );
58 COLOR_SETTINGS* cs = nullptr;
59
60 wxRect rect = aRect;
61 rect.Inflate( -1 );
62
63 // erase background
64 wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
65
66 if( m_frame )
67 {
69 }
70 else
71 {
74 cs = mgr.GetColorSettings( cfg->m_ColorTheme );
75 }
76
77 // draw the swatch
78#ifdef __WXMAC__
79 int size = 14;
80#else
81 int size = KiROUND( 14 * aDC.GetContentScaleFactor() );
82#endif
83 wxBitmap bitmap( size, size );
84
87 cs->GetColor( ToLAYER_ID( value ) ) );
88
89 aDC.DrawBitmap( bitmap, rect.GetLeft() + 4,
90 rect.GetTop() + ( rect.GetHeight() - bitmap.GetHeight() ) / 2, true );
91
92 // draw the text
93 PCB_LAYER_ID layer = ToLAYER_ID( value );
94 wxString layerName;
95
96 if( m_frame )
97 layerName = m_frame->GetBoard()->GetLayerName( layer );
98 else
99 layerName = BOARD::GetStandardLayerName( layer );
100
101 rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 8 );
102 SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
103 aGrid.DrawTextRectangle( aDC, layerName, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
104}
105
106
107
108//-------- Custom wxGridCellEditors ----------------------------------------------------
109//
110// Note: this implementation is an adaptation of wxGridCellChoiceEditor
111
112
114 bool aShowNonActivated ) :
115 m_frame( aFrame ),
116 m_mask( aMask ),
117 m_showNonActivated( aShowNonActivated ),
118 m_value( 0 )
119{
120}
121
122
123wxGridCellEditor* GRID_CELL_LAYER_SELECTOR::Clone() const
124{
126}
127
128
129void GRID_CELL_LAYER_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
130 wxEvtHandler* aEventHandler )
131{
132 m_control = new PCB_LAYER_BOX_SELECTOR( aParent, aId, wxEmptyString,
133 wxDefaultPosition, wxDefaultSize, 0, nullptr,
134 wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxBORDER_NONE );
135
136 LayerBox()->SetLayersHotkeys( false );
140
141 wxGridCellEditor::Create(aParent, aId, aEventHandler);
142}
143
144
146{
147 if( LayerBox()->GetLayerSelection() != UNDEFINED_LAYER )
148 {
149 PCB_LAYER_ID layer = ToLAYER_ID( LayerBox()->GetLayerSelection() );
150
151 if( m_frame )
152 return m_frame->GetBoard()->GetLayerName( layer );
153 else
154 return BOARD::GetStandardLayerName( layer );
155 }
156
157 return wxEmptyString;
158}
159
160
161void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
162{
163 wxRect rect( aRect );
164 rect.Inflate( -1 );
165
166#if !defined( __WXMSW__ ) && !defined( __WXGTK__ )
167 // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
168 LayerBox()->SetButtonPosition( 0, 0, wxRIGHT, 0 );
169#endif
170
171#if defined( __WXMAC__ )
172 rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
173#endif
174
175 LayerBox()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
176}
177
178
179void GRID_CELL_LAYER_SELECTOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
180{
181 auto* evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
182
183 // Don't immediately end if we get a kill focus event within BeginEdit
184 evtHandler->SetInSetFocus( true );
185
186 // These event handlers are needed to properly dismiss the editor when the popup is closed
187 m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &GRID_CELL_LAYER_SELECTOR::onComboDropDown, this);
188 m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &GRID_CELL_LAYER_SELECTOR::onComboCloseUp, this);
189
190 m_value = aGrid->GetTable()->GetValueAsLong( aRow, aCol );
191
192 // Footprints are defined in a global context and may contain layers not enabled
193 // on the current board. Check and display all layers if so.
196
198 LayerBox()->Resync();
200 LayerBox()->SetFocus();
201
202#ifdef __WXOSX_COCOA__
203 // This is a work around for the combobox being simply dismissed when a
204 // choice is made in it under OS X. The bug is almost certainly due to a
205 // problem in focus events generation logic but it's not obvious to fix and
206 // for now this at least allows one to use wxGrid.
207 if( !LayerBox()->IsPopupShown() )
208 LayerBox()->Popup();
209#endif
210
211 // When dropping down the menu, a kill focus event
212 // happens after this point, so we can't reset the flag yet.
213#if !defined(__WXGTK__)
214 evtHandler->SetInSetFocus( false );
215#endif
216}
217
218
219bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
220 wxString *newval )
221{
222 const int value = LayerBox()->GetLayerSelection();
223
224 if ( value == m_value )
225 return false;
226
227 m_value = value;
228
229 if ( newval )
230 *newval = GetValue();
231
232 return true;
233}
234
235
236void GRID_CELL_LAYER_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
237{
238 aGrid->GetTable()->SetValueAsLong( aRow, aCol, (long) m_value );
239}
240
241
243{
245}
246
247
248void GRID_CELL_LAYER_SELECTOR::onComboDropDown( wxCommandEvent& aEvent )
249{
250 // On other platforms this is done in BeginEdit()
251#if defined(__WXGTK__)
252 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
253
254 // Once the combobox is dropped, reset the flag to allow the focus-loss handler
255 // to function and close the editor.
256 evtHandler->SetInSetFocus( false );
257#endif
258}
259
260
261void GRID_CELL_LAYER_SELECTOR::onComboCloseUp( wxCommandEvent& aEvent )
262{
263 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
264
265 // Forward the combobox close up event to the cell event handler as a focus kill event
266 // so that the grid editor is dismissed when the combox closes, otherwise it leaves the
267 // dropdown arrow visible in the cell.
268 wxFocusEvent event( wxEVT_KILL_FOCUS, m_control->GetId() );
269 event.SetEventObject( m_control );
270 evtHandler->ProcessEvent( event );
271}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:216
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition: board.h:812
bool IsLayerEnabled(PCB_LAYER_ID aLayer) const
A proxy function that calls the correspondent function in m_BoardSettings tests whether a given layer...
Definition: board.cpp:855
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:614
Color settings are a bit different than most of the settings objects in that there can be more than o...
COLOR4D GetColor(int aLayer) const
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, const wxRect &aRect, int aRow, int aCol, bool isSelected) override
GRID_CELL_LAYER_RENDERER(PCB_BASE_FRAME *aFrame)
bool EndEdit(int, int, const wxGrid *, const wxString &, wxString *newval) override
wxGridCellEditor * Clone() const override
GRID_CELL_LAYER_SELECTOR(PCB_BASE_FRAME *aFrame, const LSET &forbiddenLayers, bool aShowNonActivated=false)
void onComboCloseUp(wxCommandEvent &aEvent)
void onComboDropDown(wxCommandEvent &aEvent)
PCB_LAYER_BOX_SELECTOR * LayerBox() const
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
wxString GetValue() const override
void SetSize(const wxRect &aRect) override
int SetLayerSelection(int layer)
static void DrawColorSwatch(wxBitmap &aLayerbmp, const COLOR4D &aBackground, const COLOR4D &aColor)
bool SetLayersHotkeys(bool value)
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:37
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
BOARD * GetBoard() const
virtual COLOR_SETTINGS * GetColorSettings(bool aForceRefresh=false) const override
Helper to retrieve the current color settings.
Class to display a pcb layer list in a wxBitmapComboBox.
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
void ShowNonActivatedLayers(bool aShow)
void SetNotAllowedLayerSet(const LSET &aMask)
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:125
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieve a color settings object that applications can read colors from.
T * GetAppSettings(const wxString &aFilename)
Return a handle to the a given settings by type.
@ LAYER_PCB_BACKGROUND
PCB background color.
Definition: layer_ids.h:280
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:723
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1071
see class PGM_BASE