KiCad PCB EDA Suite
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 (C) 2018 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#include <pgm_base.h>
29#include <board.h>
30#include <pcb_edit_frame.h>
34#include <wx/textctrl.h>
35
36
37//-------- Custom wxGridCellRenderers --------------------------------------------------
38
39
41 m_frame( aFrame )
42{
43}
44
45
47{
48}
49
50
51void GRID_CELL_LAYER_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
52 const wxRect& aRect, int aRow, int aCol, bool isSelected )
53{
54 int value = aGrid.GetTable()->GetValueAsLong( aRow, aCol );
55 COLOR_SETTINGS* cs = nullptr;
56
57 wxRect rect = aRect;
58 rect.Inflate( -1 );
59
60 // erase background
61 wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
62
63 if( m_frame )
64 {
66 }
67 else
68 {
69 SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
71 cs = mgr.GetColorSettings( settings->m_ColorTheme );
72 }
73
74 // draw the swatch
75 wxBitmap bitmap( 14, 14 );
78 cs->GetColor( ToLAYER_ID( value ) ) );
79 aDC.DrawBitmap( bitmap, rect.GetLeft() + 4, rect.GetTop() + 3, true );
80
81 // draw the text
82 PCB_LAYER_ID layer = ToLAYER_ID( value );
83 wxString layerName;
84
85 if( m_frame )
86 layerName = m_frame->GetBoard()->GetLayerName( layer );
87 else
88 layerName = BOARD::GetStandardLayerName( layer );
89
90 rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 8 );
91 SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
92 aGrid.DrawTextRectangle( aDC, layerName, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
93}
94
95
96
97//-------- Custom wxGridCellEditors ----------------------------------------------------
98//
99// Note: this implementation is an adaptation of wxGridCellChoiceEditor
100
101
103 m_frame( aFrame ),
104 m_mask( aMask ),
105 m_value( 0 )
106{
107}
108
109
110wxGridCellEditor* GRID_CELL_LAYER_SELECTOR::Clone() const
111{
113}
114
115
116void GRID_CELL_LAYER_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
117 wxEvtHandler* aEventHandler )
118{
119 m_control = new PCB_LAYER_BOX_SELECTOR( aParent, aId, wxEmptyString,
120 wxDefaultPosition, wxDefaultSize, 0, nullptr,
121 wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxBORDER_NONE );
122
125
126 wxGridCellEditor::Create(aParent, aId, aEventHandler);
127}
128
129
131{
132 if( LayerBox()->GetLayerSelection() != UNDEFINED_LAYER )
133 {
134 PCB_LAYER_ID layer = ToLAYER_ID( LayerBox()->GetLayerSelection() );
135
136 if( m_frame )
137 return m_frame->GetBoard()->GetLayerName( layer );
138 else
139 return BOARD::GetStandardLayerName( layer );
140 }
141
142 return wxEmptyString;
143}
144
145
146void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
147{
148 wxRect rect( aRect );
149 rect.Inflate( -1 );
150
151#if !defined( __WXMSW__ ) && !defined( __WXGTK20__ )
152 // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
153 LayerBox()->SetButtonPosition( 0, 0, wxRIGHT, 0 );
154#endif
155
156#if defined( __WXMAC__ )
157 rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
158#endif
159
160 LayerBox()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
161}
162
163
164void GRID_CELL_LAYER_SELECTOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
165{
166 auto* evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
167
168 // Don't immediately end if we get a kill focus event within BeginEdit
169 evtHandler->SetInSetFocus( true );
170
171 // These event handlers are needed to properly dismiss the editor when the popup is closed
172 m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &GRID_CELL_LAYER_SELECTOR::onComboDropDown, this);
173 m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &GRID_CELL_LAYER_SELECTOR::onComboCloseUp, this);
174
175 m_value = aGrid->GetTable()->GetValueAsLong( aRow, aCol );
176
177 // Footprints are defined in a global context and may contain layers not enabled
178 // on the current board. Check and display all layers if so.
181
182 LayerBox()->Resync();
184 LayerBox()->SetFocus();
185
186#ifdef __WXOSX_COCOA__
187 // This is a work around for the combobox being simply dismissed when a
188 // choice is made in it under OS X. The bug is almost certainly due to a
189 // problem in focus events generation logic but it's not obvious to fix and
190 // for now this at least allows one to use wxGrid.
191 if( !LayerBox()->IsPopupShown() )
192 LayerBox()->Popup();
193#endif
194
195 // When dropping down the menu, a kill focus event
196 // happens after this point, so we can't reset the flag yet.
197#if !defined(__WXGTK20__)
198 evtHandler->SetInSetFocus( false );
199#endif
200}
201
202
203bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
204 wxString *newval )
205{
206 const int value = LayerBox()->GetLayerSelection();
207
208 if ( value == m_value )
209 return false;
210
211 m_value = value;
212
213 if ( newval )
214 *newval = GetValue();
215
216 return true;
217}
218
219
220void GRID_CELL_LAYER_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
221{
222 aGrid->GetTable()->SetValueAsLong( aRow, aCol, (long) m_value );
223}
224
225
227{
229}
230
231
232void GRID_CELL_LAYER_SELECTOR::onComboDropDown( wxCommandEvent& aEvent )
233{
234 // On other platforms this is done in BeginEdit()
235#if defined(__WXGTK20__)
236 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
237
238 // Once the combobox is dropped, reset the flag to allow the focus-loss handler
239 // to function and close the editor.
240 evtHandler->SetInSetFocus( false );
241#endif
242}
243
244
245void GRID_CELL_LAYER_SELECTOR::onComboCloseUp( wxCommandEvent& aEvent )
246{
247 auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
248
249 // Forward the combobox close up event to the cell event handler as a focus kill event
250 // so that the grid editor is dismissed when the combox closes, otherwise it leaves the
251 // dropdown arrow visible in the cell.
252 wxFocusEvent event( wxEVT_KILL_FOCUS, m_control->GetId() );
253 event.SetEventObject( m_control );
254 evtHandler->ProcessEvent( event );
255}
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:184
static wxString GetStandardLayerName(PCB_LAYER_ID aLayerId)
Return an "English Standard" name of a PCB layer when given aLayerNumber.
Definition: board.h:663
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:528
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:401
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
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
GRID_CELL_LAYER_SELECTOR(PCB_BASE_FRAME *aFrame, LSET forbiddenLayers)
void SetSize(const wxRect &aRect) override
int SetLayerSelection(int layer)
static void DrawColorSwatch(wxBitmap &aLayerbmp, const COLOR4D &aBackground, const COLOR4D &aColor)
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:529
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 SetNotAllowedLayerSet(LSET aMask)
void ShowNonActivatedLayers(bool aShow)
T * GetAppSettings(bool aLoadNow=true)
Returns a handle to the a given settings by type If the settings have already been loaded,...
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
@ LAYER_PCB_BACKGROUND
PCB background color.
Definition: layer_ids.h:220
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ UNDEFINED_LAYER
Definition: layer_ids.h:60
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:926
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111