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 <pcb_edit_frame.h>
27 #include <class_board.h>
28 #include <wx/textctrl.h>
30 #include <pcb_layer_box_selector.h>
31 
32 
33 //-------- Custom wxGridCellRenderers --------------------------------------------------
34 
35 
37  m_frame( aFrame )
38 {
39 }
40 
41 
43 {
44 }
45 
46 
47 void GRID_CELL_LAYER_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
48  const wxRect& aRect, int aRow, int aCol, bool isSelected )
49 {
50  LAYER_NUM value = aGrid.GetTable()->GetValueAsLong( aRow, aCol );
51 
52  wxRect rect = aRect;
53  rect.Inflate( -1 );
54 
55  // erase background
56  wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
57 
58  // draw the swatch
59  wxBitmap bitmap( 14, 14 );
63  cds.GetLayerColor( ToLAYER_ID( value ) ) );
64  aDC.DrawBitmap( bitmap, rect.GetLeft() + 4, rect.GetTop() + 3, true );
65 
66  // draw the text
67  wxString text = m_frame->GetBoard()->GetLayerName( ToLAYER_ID( value ) );
68  rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 8 );
69  SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
70  aGrid.DrawTextRectangle( aDC, text, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
71 }
72 
73 
74 
75 //-------- Custom wxGridCellEditors ----------------------------------------------------
76 //
77 // Note: this implementation is an adaptation of wxGridCellChoiceEditor
78 
79 
81  m_frame( aFrame ),
82  m_mask( aMask )
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  return m_frame->GetBoard()->GetLayerName( ToLAYER_ID( LayerBox()->GetLayerSelection() ) );
110 }
111 
112 
113 void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
114 {
115  wxRect rect( aRect );
116  rect.Inflate( -1 );
117 
118 #if !defined( __WXMSW__ ) && !defined( __WXGTK20__ )
119  // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
120  LayerBox()->SetButtonPosition( 0, 0, wxRIGHT, 0 );
121 #endif
122 
123 #if defined( __WXMAC__ )
124  rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
125 #endif
126 
127  LayerBox()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
128 }
129 
130 
131 void GRID_CELL_LAYER_SELECTOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
132 {
133  auto* evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
134 
135  // Don't immediately end if we get a kill focus event within BeginEdit
136  evtHandler->SetInSetFocus( true );
137 
138  m_value = (LAYER_NUM) aGrid->GetTable()->GetValueAsLong( aRow, aCol );
139 
140  // Footprints are defined in a global context and may contain layers not enabled
141  // on the current board. Check and display all layers if so.
142  bool currentLayerEnabled = m_frame->GetBoard()->IsLayerEnabled( ToLAYER_ID( m_value ) );
143  LayerBox()->ShowNonActivatedLayers( !currentLayerEnabled );
144  LayerBox()->Resync();
145 
147  LayerBox()->SetFocus();
148 
149 #ifdef __WXOSX_COCOA__
150  // This is a work around for the combobox being simply dismissed when a
151  // choice is made in it under OS X. The bug is almost certainly due to a
152  // problem in focus events generation logic but it's not obvious to fix and
153  // for now this at least allows to use wxGrid.
154  if( !LayerBox()->IsPopupShown() )
155  LayerBox()->Popup();
156 #endif
157 
158  // When dropping down the menu, a kill focus event
159  // happens after this point, so we can't reset the flag yet.
160 #if !defined(__WXGTK20__)
161  evtHandler->SetInSetFocus( false );
162 #endif
163 }
164 
165 
166 bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
167  wxString *newval )
168 {
169  const LAYER_NUM value = LayerBox()->GetLayerSelection();
170 
171  if ( value == m_value )
172  return false;
173 
174  m_value = value;
175 
176  if ( newval )
177  *newval = GetValue();
178 
179  return true;
180 }
181 
182 
183 void GRID_CELL_LAYER_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
184 {
185  aGrid->GetTable()->SetValueAsLong( aRow, aCol, (long) m_value );
186 }
187 
188 
190 {
192 }
193 
194 
void SetNotAllowedLayerSet(LSET aMask)
wxString GetValue() const override
Class BOARD to handle a board.
bool IsLayerEnabled(PCB_LAYER_ID aLayer) const
Function IsLayerEnabled is a proxy function that calls the correspondent function in m_BoardSettings ...
Definition: class_board.h:445
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
BOARD * GetBoard() const
Class to display a pcb layer list in a wxBitmapComboBox.
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
GRID_CELL_LAYER_RENDERER(PCB_BASE_FRAME *aFrame)
PCB_LAYER_BOX_SELECTOR * LayerBox() const
Class LSET is a set of PCB_LAYER_IDs.
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
PCB_GENERAL_SETTINGS & Settings()
void ShowNonActivatedLayers(bool aShow)
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Function GetLayerName returns the name of a layer given by aLayer.
int SetLayerSelection(LAYER_NUM layer)
bool EndEdit(int, int, const wxGrid *, const wxString &, wxString *newval) override
COLORS_DESIGN_SETTINGS & Colors()
int LAYER_NUM
Type LAYER_NUM 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
GRID_CELL_LAYER_SELECTOR(PCB_BASE_FRAME *aFrame, LSET forbiddenLayers)
LAYER_NUM GetLayerSelection() const
COLOR4D GetLayerColor(LAYER_NUM aLayer) const
Function GetLayerColor.
Class COLORS_DESIGN_SETTINGS is a list of color settings for designs in Pcbnew.
void SetSize(const wxRect &aRect) override
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:810
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
wxGridCellEditor * Clone() const override