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 ), m_mask( aMask ), m_value( 0 )
82 {
83 }
84 
85 
86 wxGridCellEditor* GRID_CELL_LAYER_SELECTOR::Clone() const
87 {
89 }
90 
91 
92 void GRID_CELL_LAYER_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
93  wxEvtHandler* aEventHandler )
94 {
95  m_control = new PCB_LAYER_BOX_SELECTOR( aParent, aId, wxEmptyString,
96  wxDefaultPosition, wxDefaultSize, 0, nullptr,
97  wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxBORDER_NONE );
98 
101 
102  wxGridCellEditor::Create(aParent, aId, aEventHandler);
103 }
104 
105 
107 {
108  return m_frame->GetBoard()->GetLayerName( ToLAYER_ID( LayerBox()->GetLayerSelection() ) );
109 }
110 
111 
112 void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
113 {
114  wxRect rect( aRect );
115  rect.Inflate( -1 );
116 
117 #if !defined( __WXMSW__ ) && !defined( __WXGTK20__ )
118  // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
119  LayerBox()->SetButtonPosition( 0, 0, wxRIGHT, 0 );
120 #endif
121 
122 #if defined( __WXMAC__ )
123  rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
124 #endif
125 
126  LayerBox()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
127 }
128 
129 
130 void GRID_CELL_LAYER_SELECTOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
131 {
132  auto* evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
133 
134  // Don't immediately end if we get a kill focus event within BeginEdit
135  evtHandler->SetInSetFocus( true );
136 
137  m_value = (LAYER_NUM) aGrid->GetTable()->GetValueAsLong( aRow, aCol );
138 
139  // Footprints are defined in a global context and may contain layers not enabled
140  // on the current board. Check and display all layers if so.
141  bool currentLayerEnabled = m_frame->GetBoard()->IsLayerEnabled( ToLAYER_ID( m_value ) );
142  LayerBox()->ShowNonActivatedLayers( !currentLayerEnabled );
143  LayerBox()->Resync();
144 
146  LayerBox()->SetFocus();
147 
148 #ifdef __WXOSX_COCOA__
149  // This is a work around for the combobox being simply dismissed when a
150  // choice is made in it under OS X. The bug is almost certainly due to a
151  // problem in focus events generation logic but it's not obvious to fix and
152  // for now this at least allows to use wxGrid.
153  if( !LayerBox()->IsPopupShown() )
154  LayerBox()->Popup();
155 #endif
156 
157  // When dropping down the menu, a kill focus event
158  // happens after this point, so we can't reset the flag yet.
159 #if !defined(__WXGTK20__)
160  evtHandler->SetInSetFocus( false );
161 #endif
162 }
163 
164 
165 bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
166  wxString *newval )
167 {
168  const LAYER_NUM value = LayerBox()->GetLayerSelection();
169 
170  if ( value == m_value )
171  return false;
172 
173  m_value = value;
174 
175  if ( newval )
176  *newval = GetValue();
177 
178  return true;
179 }
180 
181 
182 void GRID_CELL_LAYER_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
183 {
184  aGrid->GetTable()->SetValueAsLong( aRow, aCol, (long) m_value );
185 }
186 
187 
189 {
191 }
192 
193 
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Function GetLayerName returns the name of a layer given by aLayer.
void SetNotAllowedLayerSet(LSET aMask)
wxString GetValue() const override
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
Function IsLayerEnabled is a proxy function that calls the correspondent function in m_BoardSettings ...
Definition: class_board.h:438
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
COLOR4D GetLayerColor(LAYER_NUM aLayer) const
Function GetLayerColor.
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)
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)
Class COLORS_DESIGN_SETTINGS is a list of color settings for designs in Pcbnew.
BOARD * GetBoard() const
void SetSize(const wxRect &aRect) override
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:813
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
wxGridCellEditor * Clone() const override