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