-
-
Notifications
You must be signed in to change notification settings - Fork 486
/
grid_layer_box_helpers.cpp
263 lines (198 loc) · 8.28 KB
/
grid_layer_box_helpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <grid_layer_box_helpers.h>
#include <wx/textctrl.h>
#include <pgm_base.h>
#include <settings/settings_manager.h>
#include <settings/color_settings.h>
#include <footprint_editor_settings.h>
#include <board.h>
#include <lset.h>
#include <pcb_edit_frame.h>
#include <pcb_layer_box_selector.h>
#include <settings/color_settings.h>
#include <widgets/layer_presentation.h>
//-------- Custom wxGridCellRenderers --------------------------------------------------
GRID_CELL_LAYER_RENDERER::GRID_CELL_LAYER_RENDERER( PCB_BASE_FRAME* aFrame ) :
m_frame( aFrame )
{
}
GRID_CELL_LAYER_RENDERER::~GRID_CELL_LAYER_RENDERER()
{
}
void GRID_CELL_LAYER_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
const wxRect& aRect, int aRow, int aCol, bool isSelected )
{
int value = aGrid.GetTable()->GetValueAsLong( aRow, aCol );
COLOR_SETTINGS* cs = nullptr;
wxRect rect = aRect;
rect.Inflate( -1 );
// erase background
wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
if( m_frame )
{
cs = m_frame->GetColorSettings();
}
else
{
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
FOOTPRINT_EDITOR_SETTINGS* cfg = mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" );
cs = mgr.GetColorSettings( cfg->m_ColorTheme );
}
// draw the swatch
int size = KiROUND( 14 * aDC.GetContentScaleFactor() );
wxBitmap bitmap( size, size );
LAYER_PRESENTATION::DrawColorSwatch( bitmap,
cs->GetColor( ToLAYER_ID( LAYER_PCB_BACKGROUND ) ),
cs->GetColor( ToLAYER_ID( value ) ) );
aDC.DrawBitmap( bitmap, rect.GetLeft() + 4,
rect.GetTop() + ( rect.GetHeight() - bitmap.GetHeight() ) / 2, true );
// draw the text
PCB_LAYER_ID layer = ToLAYER_ID( value );
wxString layerName;
if( m_frame )
layerName = m_frame->GetBoard()->GetLayerName( layer );
else
layerName = BOARD::GetStandardLayerName( layer );
rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 8 );
SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
aGrid.DrawTextRectangle( aDC, layerName, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
}
//-------- Custom wxGridCellEditors ----------------------------------------------------
//
// Note: this implementation is an adaptation of wxGridCellChoiceEditor
GRID_CELL_LAYER_SELECTOR::GRID_CELL_LAYER_SELECTOR( PCB_BASE_FRAME* aFrame, LSET aMask ) :
m_frame( aFrame ),
m_mask( aMask ),
m_value( 0 )
{
}
wxGridCellEditor* GRID_CELL_LAYER_SELECTOR::Clone() const
{
return new GRID_CELL_LAYER_SELECTOR( m_frame, m_mask );
}
void GRID_CELL_LAYER_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
wxEvtHandler* aEventHandler )
{
m_control = new PCB_LAYER_BOX_SELECTOR( aParent, aId, wxEmptyString,
wxDefaultPosition, wxDefaultSize, 0, nullptr,
wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxBORDER_NONE );
LayerBox()->SetLayersHotkeys( false );
LayerBox()->SetBoardFrame( m_frame );
LayerBox()->SetNotAllowedLayerSet( m_mask );
wxGridCellEditor::Create(aParent, aId, aEventHandler);
}
wxString GRID_CELL_LAYER_SELECTOR::GetValue() const
{
if( LayerBox()->GetLayerSelection() != UNDEFINED_LAYER )
{
PCB_LAYER_ID layer = ToLAYER_ID( LayerBox()->GetLayerSelection() );
if( m_frame )
return m_frame->GetBoard()->GetLayerName( layer );
else
return BOARD::GetStandardLayerName( layer );
}
return wxEmptyString;
}
void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
{
wxRect rect( aRect );
rect.Inflate( -1 );
#if !defined( __WXMSW__ ) && !defined( __WXGTK__ )
// Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
LayerBox()->SetButtonPosition( 0, 0, wxRIGHT, 0 );
#endif
#if defined( __WXMAC__ )
rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
#endif
LayerBox()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
}
void GRID_CELL_LAYER_SELECTOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
{
auto* evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
// Don't immediately end if we get a kill focus event within BeginEdit
evtHandler->SetInSetFocus( true );
// These event handlers are needed to properly dismiss the editor when the popup is closed
m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &GRID_CELL_LAYER_SELECTOR::onComboDropDown, this);
m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &GRID_CELL_LAYER_SELECTOR::onComboCloseUp, this);
m_value = aGrid->GetTable()->GetValueAsLong( aRow, aCol );
// Footprints are defined in a global context and may contain layers not enabled
// on the current board. Check and display all layers if so.
if( m_frame && !m_frame->GetBoard()->IsLayerEnabled( ToLAYER_ID( m_value ) ) )
LayerBox()->ShowNonActivatedLayers( true );
LayerBox()->Resync();
LayerBox()->SetLayerSelection( m_value );
LayerBox()->SetFocus();
#ifdef __WXOSX_COCOA__
// This is a work around for the combobox being simply dismissed when a
// choice is made in it under OS X. The bug is almost certainly due to a
// problem in focus events generation logic but it's not obvious to fix and
// for now this at least allows one to use wxGrid.
if( !LayerBox()->IsPopupShown() )
LayerBox()->Popup();
#endif
// When dropping down the menu, a kill focus event
// happens after this point, so we can't reset the flag yet.
#if !defined(__WXGTK__)
evtHandler->SetInSetFocus( false );
#endif
}
bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
wxString *newval )
{
const int value = LayerBox()->GetLayerSelection();
if ( value == m_value )
return false;
m_value = value;
if ( newval )
*newval = GetValue();
return true;
}
void GRID_CELL_LAYER_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
{
aGrid->GetTable()->SetValueAsLong( aRow, aCol, (long) m_value );
}
void GRID_CELL_LAYER_SELECTOR::Reset()
{
LayerBox()->SetLayerSelection( m_value );
}
void GRID_CELL_LAYER_SELECTOR::onComboDropDown( wxCommandEvent& aEvent )
{
// On other platforms this is done in BeginEdit()
#if defined(__WXGTK__)
auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
// Once the combobox is dropped, reset the flag to allow the focus-loss handler
// to function and close the editor.
evtHandler->SetInSetFocus( false );
#endif
}
void GRID_CELL_LAYER_SELECTOR::onComboCloseUp( wxCommandEvent& aEvent )
{
auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
// Forward the combobox close up event to the cell event handler as a focus kill event
// so that the grid editor is dismissed when the combox closes, otherwise it leaves the
// dropdown arrow visible in the cell.
wxFocusEvent event( wxEVT_KILL_FOCUS, m_control->GetId() );
event.SetEventObject( m_control );
evtHandler->ProcessEvent( event );
}