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 
24 #include <grid_layer_box_helpers.h>
25 
26 #include <board.h>
27 #include <pcb_edit_frame.h>
28 #include <pcb_layer_box_selector.h>
31 #include <wx/textctrl.h>
32 
33 
34 //-------- Custom wxGridCellRenderers --------------------------------------------------
35 
36 
38  m_frame( aFrame )
39 {
40 }
41 
42 
44 {
45 }
46 
47 
48 void GRID_CELL_LAYER_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
49  const wxRect& aRect, int aRow, int aCol, bool isSelected )
50 {
51  LAYER_NUM value = aGrid.GetTable()->GetValueAsLong( aRow, aCol );
52 
53  wxRect rect = aRect;
54  rect.Inflate( -1 );
55 
56  // erase background
57  wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
58 
59  // draw the swatch
60  wxBitmap bitmap( 14, 14 );
64  cs->GetColor( ToLAYER_ID( value ) ) );
65  aDC.DrawBitmap( bitmap, rect.GetLeft() + 4, rect.GetTop() + 3, true );
66 
67  // draw the text
68  wxString text = m_frame->GetBoard()->GetLayerName( ToLAYER_ID( value ) );
69  rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 8 );
70  SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
71  aGrid.DrawTextRectangle( aDC, text, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
72 }
73 
74 
75 
76 //-------- Custom wxGridCellEditors ----------------------------------------------------
77 //
78 // Note: this implementation is an adaptation of wxGridCellChoiceEditor
79 
80 
82  m_frame( aFrame ), m_mask( aMask ), m_value( 0 )
83 {
84 }
85 
86 
87 wxGridCellEditor* GRID_CELL_LAYER_SELECTOR::Clone() const
88 {
90 }
91 
92 
93 void GRID_CELL_LAYER_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
94  wxEvtHandler* aEventHandler )
95 {
96  m_control = new PCB_LAYER_BOX_SELECTOR( aParent, aId, wxEmptyString,
97  wxDefaultPosition, wxDefaultSize, 0, nullptr,
98  wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxBORDER_NONE );
99 
102 
103  wxGridCellEditor::Create(aParent, aId, aEventHandler);
104 }
105 
106 
108 {
109  if( LayerBox()->GetLayerSelection() != UNDEFINED_LAYER )
110  return m_frame->GetBoard()->GetLayerName( ToLAYER_ID( LayerBox()->GetLayerSelection() ) );
111 
112  return wxEmptyString;
113 }
114 
115 
116 void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
117 {
118  wxRect rect( aRect );
119  rect.Inflate( -1 );
120 
121 #if !defined( __WXMSW__ ) && !defined( __WXGTK20__ )
122  // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
123  LayerBox()->SetButtonPosition( 0, 0, wxRIGHT, 0 );
124 #endif
125 
126 #if defined( __WXMAC__ )
127  rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
128 #endif
129 
130  LayerBox()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
131 }
132 
133 
134 void GRID_CELL_LAYER_SELECTOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
135 {
136  auto* evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
137 
138  // Don't immediately end if we get a kill focus event within BeginEdit
139  evtHandler->SetInSetFocus( true );
140 
141  // These event handlers are needed to properly dismiss the editor when the popup is closed
142  m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &GRID_CELL_LAYER_SELECTOR::onComboDropDown, this);
143  m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &GRID_CELL_LAYER_SELECTOR::onComboCloseUp, this);
144 
145  m_value = (LAYER_NUM) aGrid->GetTable()->GetValueAsLong( aRow, aCol );
146 
147  // Footprints are defined in a global context and may contain layers not enabled
148  // on the current board. Check and display all layers if so.
149  bool currentLayerEnabled = m_frame->GetBoard()->IsLayerEnabled( ToLAYER_ID( m_value ) );
150  LayerBox()->ShowNonActivatedLayers( !currentLayerEnabled );
151  LayerBox()->Resync();
152 
154  LayerBox()->SetFocus();
155 
156 #ifdef __WXOSX_COCOA__
157  // This is a work around for the combobox being simply dismissed when a
158  // choice is made in it under OS X. The bug is almost certainly due to a
159  // problem in focus events generation logic but it's not obvious to fix and
160  // for now this at least allows to use wxGrid.
161  if( !LayerBox()->IsPopupShown() )
162  LayerBox()->Popup();
163 #endif
164 
165  // When dropping down the menu, a kill focus event
166  // happens after this point, so we can't reset the flag yet.
167 #if !defined(__WXGTK20__)
168  evtHandler->SetInSetFocus( false );
169 #endif
170 }
171 
172 
173 bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
174  wxString *newval )
175 {
176  const LAYER_NUM value = LayerBox()->GetLayerSelection();
177 
178  if ( value == m_value )
179  return false;
180 
181  m_value = value;
182 
183  if ( newval )
184  *newval = GetValue();
185 
186  return true;
187 }
188 
189 
190 void GRID_CELL_LAYER_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
191 {
192  aGrid->GetTable()->SetValueAsLong( aRow, aCol, (long) m_value );
193 }
194 
195 
197 {
199 }
200 
201 
202 void GRID_CELL_LAYER_SELECTOR::onComboDropDown( wxCommandEvent& aEvent )
203 {
204  auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
205 
206  // Once the combobox is dropped, reset the flag to allow the focus-loss handler
207  // to function and close the editor.
208  evtHandler->SetInSetFocus( false );
209 }
210 
211 
212 void GRID_CELL_LAYER_SELECTOR::onComboCloseUp( wxCommandEvent& aEvent )
213 {
214  auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
215 
216  // Forward the combobox close up event to the cell event handler as a focus kill event
217  // so that the grid editor is dismissed when the combox closes, otherwise it leaves the
218  // dropdown arrow visible in the cell.
219  wxFocusEvent event( wxEVT_KILL_FOCUS, m_control->GetId() );
220  event.SetEventObject( m_control );
221  evtHandler->ProcessEvent( event );
222 }
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:332
void onComboCloseUp(wxCommandEvent &aEvent)
void SetNotAllowedLayerSet(LSET aMask)
wxString GetValue() const override
virtual COLOR_SETTINGS * GetColorSettings() override
Helper to retrieve the current color settings.
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
Class to display a pcb layer list in a wxBitmapComboBox.
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
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.h:481
void onComboDropDown(wxCommandEvent &aEvent)
PCB_LAYER_BOX_SELECTOR * LayerBox() const
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
GRID_CELL_LAYER_RENDERER(PCB_BASE_FRAME *aFrame)
LAYER_NUM GetLayerSelection() const
LSET is a set of PCB_LAYER_IDs.
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
void ShowNonActivatedLayers(bool aShow)
int SetLayerSelection(LAYER_NUM layer)
bool EndEdit(int, int, const wxGrid *, const wxString &, wxString *newval) override
int LAYER_NUM
This can be replaced with int and removed.
static void DrawColorSwatch(wxBitmap &aLayerbmp, COLOR4D aBackground, COLOR4D aColor)
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, const wxRect &aRect, int aRow, int aCol, bool isSelected) override
COLOR4D GetColor(int aLayer) const
GRID_CELL_LAYER_SELECTOR(PCB_BASE_FRAME *aFrame, LSET forbiddenLayers)
Color settings are a bit different than most of the settings objects in that there can be more than o...
BOARD * GetBoard() const
void SetSize(const wxRect &aRect) override
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:898
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
wxGridCellEditor * Clone() const override