You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.1 KiB

  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. #include <grid_layer_box_helpers.h>
  24. #include <pcb_edit_frame.h>
  25. #include <class_board.h>
  26. #include <wx/textctrl.h>
  27. #include <widgets/layer_box_selector.h>
  28. #include <pcb_layer_box_selector.h>
  29. //-------- Custom wxGridCellRenderers --------------------------------------------------
  30. GRID_CELL_LAYER_RENDERER::GRID_CELL_LAYER_RENDERER( PCB_BASE_FRAME* aFrame ) :
  31. m_frame( aFrame )
  32. {
  33. }
  34. GRID_CELL_LAYER_RENDERER::~GRID_CELL_LAYER_RENDERER()
  35. {
  36. }
  37. void GRID_CELL_LAYER_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
  38. const wxRect& aRect, int aRow, int aCol, bool isSelected )
  39. {
  40. LAYER_NUM value = aGrid.GetTable()->GetValueAsLong( aRow, aCol );
  41. wxRect rect = aRect;
  42. rect.Inflate( -1 );
  43. // erase background
  44. wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
  45. // draw the swatch
  46. wxBitmap bitmap( 14, 14 );
  47. const COLORS_DESIGN_SETTINGS& cds = m_frame->Settings().Colors();
  48. LAYER_SELECTOR::DrawColorSwatch( bitmap,
  49. cds.GetLayerColor( ToLAYER_ID( LAYER_PCB_BACKGROUND ) ),
  50. cds.GetLayerColor( ToLAYER_ID( value ) ) );
  51. aDC.DrawBitmap( bitmap, rect.GetLeft() + 4, rect.GetTop() + 3, true );
  52. // draw the text
  53. wxString text = m_frame->GetBoard()->GetLayerName( ToLAYER_ID( value ) );
  54. rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 8 );
  55. SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
  56. aGrid.DrawTextRectangle( aDC, text, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
  57. }
  58. //-------- Custom wxGridCellEditors ----------------------------------------------------
  59. //
  60. // Note: this implementation is an adaptation of wxGridCellChoiceEditor
  61. GRID_CELL_LAYER_SELECTOR::GRID_CELL_LAYER_SELECTOR( PCB_BASE_FRAME* aFrame, LSET aMask ) :
  62. m_frame( aFrame ), m_mask( aMask ), m_value( 0 )
  63. {
  64. }
  65. wxGridCellEditor* GRID_CELL_LAYER_SELECTOR::Clone() const
  66. {
  67. return new GRID_CELL_LAYER_SELECTOR( m_frame, m_mask );
  68. }
  69. void GRID_CELL_LAYER_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
  70. wxEvtHandler* aEventHandler )
  71. {
  72. m_control = new PCB_LAYER_BOX_SELECTOR( aParent, aId, wxEmptyString,
  73. wxDefaultPosition, wxDefaultSize, 0, nullptr,
  74. wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxBORDER_NONE );
  75. LayerBox()->SetBoardFrame( m_frame );
  76. LayerBox()->SetNotAllowedLayerSet( m_mask );
  77. wxGridCellEditor::Create(aParent, aId, aEventHandler);
  78. }
  79. wxString GRID_CELL_LAYER_SELECTOR::GetValue() const
  80. {
  81. return m_frame->GetBoard()->GetLayerName( ToLAYER_ID( LayerBox()->GetLayerSelection() ) );
  82. }
  83. void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
  84. {
  85. wxRect rect( aRect );
  86. rect.Inflate( -1 );
  87. #if !defined( __WXMSW__ ) && !defined( __WXGTK20__ )
  88. // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
  89. LayerBox()->SetButtonPosition( 0, 0, wxRIGHT, 0 );
  90. #endif
  91. #if defined( __WXMAC__ )
  92. rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
  93. #endif
  94. LayerBox()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
  95. }
  96. void GRID_CELL_LAYER_SELECTOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
  97. {
  98. auto* evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
  99. // Don't immediately end if we get a kill focus event within BeginEdit
  100. evtHandler->SetInSetFocus( true );
  101. m_value = (LAYER_NUM) aGrid->GetTable()->GetValueAsLong( aRow, aCol );
  102. // Footprints are defined in a global context and may contain layers not enabled
  103. // on the current board. Check and display all layers if so.
  104. bool currentLayerEnabled = m_frame->GetBoard()->IsLayerEnabled( ToLAYER_ID( m_value ) );
  105. LayerBox()->ShowNonActivatedLayers( !currentLayerEnabled );
  106. LayerBox()->Resync();
  107. LayerBox()->SetLayerSelection( m_value );
  108. LayerBox()->SetFocus();
  109. #ifdef __WXOSX_COCOA__
  110. // This is a work around for the combobox being simply dismissed when a
  111. // choice is made in it under OS X. The bug is almost certainly due to a
  112. // problem in focus events generation logic but it's not obvious to fix and
  113. // for now this at least allows to use wxGrid.
  114. if( !LayerBox()->IsPopupShown() )
  115. LayerBox()->Popup();
  116. #endif
  117. // When dropping down the menu, a kill focus event
  118. // happens after this point, so we can't reset the flag yet.
  119. #if !defined(__WXGTK20__)
  120. evtHandler->SetInSetFocus( false );
  121. #endif
  122. }
  123. bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
  124. wxString *newval )
  125. {
  126. const LAYER_NUM value = LayerBox()->GetLayerSelection();
  127. if ( value == m_value )
  128. return false;
  129. m_value = value;
  130. if ( newval )
  131. *newval = GetValue();
  132. return true;
  133. }
  134. void GRID_CELL_LAYER_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
  135. {
  136. aGrid->GetTable()->SetValueAsLong( aRow, aCol, (long) m_value );
  137. }
  138. void GRID_CELL_LAYER_SELECTOR::Reset()
  139. {
  140. LayerBox()->SetLayerSelection( m_value );
  141. }