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.

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