2018-03-28 17:14:04 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
2021-08-29 23:33:08 +00:00
|
|
|
#include <pgm_base.h>
|
|
|
|
#include <settings/settings_manager.h>
|
|
|
|
#include <settings/color_settings.h>
|
|
|
|
#include <footprint_editor_settings.h>
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <pcb_edit_frame.h>
|
2018-03-28 17:14:04 +00:00
|
|
|
#include <pcb_layer_box_selector.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <settings/color_settings.h>
|
2021-08-29 23:33:08 +00:00
|
|
|
#include <widgets/layer_box_selector.h>
|
2020-01-13 01:44:19 +00:00
|
|
|
#include <wx/textctrl.h>
|
2018-03-28 17:14:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
//-------- 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 )
|
|
|
|
{
|
2021-08-29 23:33:08 +00:00
|
|
|
int value = aGrid.GetTable()->GetValueAsLong( aRow, aCol );
|
|
|
|
COLOR_SETTINGS* cs = nullptr;
|
2018-03-28 17:14:04 +00:00
|
|
|
|
|
|
|
wxRect rect = aRect;
|
|
|
|
rect.Inflate( -1 );
|
|
|
|
|
|
|
|
// erase background
|
|
|
|
wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
|
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
if( m_frame )
|
|
|
|
{
|
|
|
|
cs = m_frame->GetColorSettings();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
|
|
|
|
FOOTPRINT_EDITOR_SETTINGS* settings = mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>();
|
|
|
|
cs = mgr.GetColorSettings( settings->m_ColorTheme );
|
|
|
|
}
|
|
|
|
|
2018-03-28 17:14:04 +00:00
|
|
|
// draw the swatch
|
|
|
|
wxBitmap bitmap( 14, 14 );
|
|
|
|
LAYER_SELECTOR::DrawColorSwatch( bitmap,
|
2020-01-13 01:44:19 +00:00
|
|
|
cs->GetColor( ToLAYER_ID( LAYER_PCB_BACKGROUND ) ),
|
|
|
|
cs->GetColor( ToLAYER_ID( value ) ) );
|
2018-03-28 17:14:04 +00:00
|
|
|
aDC.DrawBitmap( bitmap, rect.GetLeft() + 4, rect.GetTop() + 3, true );
|
|
|
|
|
|
|
|
// draw the text
|
2021-08-29 23:33:08 +00:00
|
|
|
PCB_LAYER_ID layer = ToLAYER_ID( value );
|
|
|
|
wxString layerName;
|
|
|
|
|
|
|
|
if( m_frame )
|
|
|
|
layerName = m_frame->GetBoard()->GetLayerName( layer );
|
|
|
|
else
|
|
|
|
layerName = BOARD::GetStandardLayerName( layer );
|
|
|
|
|
2018-03-28 17:14:04 +00:00
|
|
|
rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 8 );
|
|
|
|
SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
|
2021-08-29 23:33:08 +00:00
|
|
|
aGrid.DrawTextRectangle( aDC, layerName, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
|
2018-03-28 17:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------- Custom wxGridCellEditors ----------------------------------------------------
|
|
|
|
//
|
|
|
|
// Note: this implementation is an adaptation of wxGridCellChoiceEditor
|
|
|
|
|
|
|
|
|
|
|
|
GRID_CELL_LAYER_SELECTOR::GRID_CELL_LAYER_SELECTOR( PCB_BASE_FRAME* aFrame, LSET aMask ) :
|
2021-08-29 23:33:08 +00:00
|
|
|
m_frame( aFrame ),
|
|
|
|
m_mask( aMask ),
|
|
|
|
m_value( 0 )
|
2018-03-28 17:14:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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()->SetBoardFrame( m_frame );
|
|
|
|
LayerBox()->SetNotAllowedLayerSet( m_mask );
|
|
|
|
|
|
|
|
wxGridCellEditor::Create(aParent, aId, aEventHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString GRID_CELL_LAYER_SELECTOR::GetValue() const
|
|
|
|
{
|
2020-11-12 13:15:49 +00:00
|
|
|
if( LayerBox()->GetLayerSelection() != UNDEFINED_LAYER )
|
2021-08-29 23:33:08 +00:00
|
|
|
{
|
|
|
|
PCB_LAYER_ID layer = ToLAYER_ID( LayerBox()->GetLayerSelection() );
|
|
|
|
|
|
|
|
if( m_frame )
|
|
|
|
return m_frame->GetBoard()->GetLayerName( layer );
|
|
|
|
else
|
|
|
|
return BOARD::GetStandardLayerName( layer );
|
|
|
|
}
|
2020-11-12 13:15:49 +00:00
|
|
|
|
|
|
|
return wxEmptyString;
|
2018-03-28 17:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
|
|
|
|
{
|
|
|
|
wxRect rect( aRect );
|
|
|
|
rect.Inflate( -1 );
|
|
|
|
|
2023-04-12 16:18:47 +00:00
|
|
|
#if !defined( __WXMSW__ ) && !defined( __WXGTK__ )
|
2018-03-28 17:14:04 +00:00
|
|
|
// 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 );
|
|
|
|
|
2020-10-10 12:07:02 +00:00
|
|
|
// 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);
|
|
|
|
|
2021-07-21 23:14:56 +00:00
|
|
|
m_value = aGrid->GetTable()->GetValueAsLong( aRow, aCol );
|
2018-03-28 17:14:04 +00:00
|
|
|
|
|
|
|
// Footprints are defined in a global context and may contain layers not enabled
|
|
|
|
// on the current board. Check and display all layers if so.
|
2021-08-29 23:33:08 +00:00
|
|
|
if( m_frame && !m_frame->GetBoard()->IsLayerEnabled( ToLAYER_ID( m_value ) ) )
|
|
|
|
LayerBox()->ShowNonActivatedLayers( true );
|
2018-03-28 17:14:04 +00:00
|
|
|
|
2021-08-29 23:33:08 +00:00
|
|
|
LayerBox()->Resync();
|
2018-03-28 17:14:04 +00:00
|
|
|
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
|
2021-04-03 09:09:12 +00:00
|
|
|
// for now this at least allows one to use wxGrid.
|
2018-03-28 17:14:04 +00:00
|
|
|
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.
|
2023-04-12 16:18:47 +00:00
|
|
|
#if !defined(__WXGTK__)
|
2018-03-28 17:14:04 +00:00
|
|
|
evtHandler->SetInSetFocus( false );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
|
|
|
|
wxString *newval )
|
|
|
|
{
|
2021-07-21 23:14:56 +00:00
|
|
|
const int value = LayerBox()->GetLayerSelection();
|
2018-03-28 17:14:04 +00:00
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-10 12:07:02 +00:00
|
|
|
void GRID_CELL_LAYER_SELECTOR::onComboDropDown( wxCommandEvent& aEvent )
|
|
|
|
{
|
2021-02-23 17:19:55 +00:00
|
|
|
// On other platforms this is done in BeginEdit()
|
2023-04-12 16:18:47 +00:00
|
|
|
#if defined(__WXGTK__)
|
2020-10-10 12:07:02 +00:00
|
|
|
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 );
|
2021-02-23 17:19:55 +00:00
|
|
|
#endif
|
2020-10-10 12:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|