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.

225 lines
7.4 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 <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. if( LayerBox()->GetLayerSelection() != UNDEFINED_LAYER )
  83. return m_frame->GetBoard()->GetLayerName( ToLAYER_ID( LayerBox()->GetLayerSelection() ) );
  84. return wxEmptyString;
  85. }
  86. void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
  87. {
  88. wxRect rect( aRect );
  89. rect.Inflate( -1 );
  90. #if !defined( __WXMSW__ ) && !defined( __WXGTK20__ )
  91. // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
  92. LayerBox()->SetButtonPosition( 0, 0, wxRIGHT, 0 );
  93. #endif
  94. #if defined( __WXMAC__ )
  95. rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
  96. #endif
  97. LayerBox()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
  98. }
  99. void GRID_CELL_LAYER_SELECTOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
  100. {
  101. auto* evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
  102. // Don't immediately end if we get a kill focus event within BeginEdit
  103. evtHandler->SetInSetFocus( true );
  104. // These event handlers are needed to properly dismiss the editor when the popup is closed
  105. m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &GRID_CELL_LAYER_SELECTOR::onComboDropDown, this);
  106. m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &GRID_CELL_LAYER_SELECTOR::onComboCloseUp, this);
  107. m_value = (LAYER_NUM) aGrid->GetTable()->GetValueAsLong( aRow, aCol );
  108. // Footprints are defined in a global context and may contain layers not enabled
  109. // on the current board. Check and display all layers if so.
  110. bool currentLayerEnabled = m_frame->GetBoard()->IsLayerEnabled( ToLAYER_ID( m_value ) );
  111. LayerBox()->ShowNonActivatedLayers( !currentLayerEnabled );
  112. LayerBox()->Resync();
  113. LayerBox()->SetLayerSelection( m_value );
  114. LayerBox()->SetFocus();
  115. #ifdef __WXOSX_COCOA__
  116. // This is a work around for the combobox being simply dismissed when a
  117. // choice is made in it under OS X. The bug is almost certainly due to a
  118. // problem in focus events generation logic but it's not obvious to fix and
  119. // for now this at least allows one to use wxGrid.
  120. if( !LayerBox()->IsPopupShown() )
  121. LayerBox()->Popup();
  122. #endif
  123. // When dropping down the menu, a kill focus event
  124. // happens after this point, so we can't reset the flag yet.
  125. #if !defined(__WXGTK20__)
  126. evtHandler->SetInSetFocus( false );
  127. #endif
  128. }
  129. bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
  130. wxString *newval )
  131. {
  132. const LAYER_NUM value = LayerBox()->GetLayerSelection();
  133. if ( value == m_value )
  134. return false;
  135. m_value = value;
  136. if ( newval )
  137. *newval = GetValue();
  138. return true;
  139. }
  140. void GRID_CELL_LAYER_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
  141. {
  142. aGrid->GetTable()->SetValueAsLong( aRow, aCol, (long) m_value );
  143. }
  144. void GRID_CELL_LAYER_SELECTOR::Reset()
  145. {
  146. LayerBox()->SetLayerSelection( m_value );
  147. }
  148. void GRID_CELL_LAYER_SELECTOR::onComboDropDown( wxCommandEvent& aEvent )
  149. {
  150. // On other platforms this is done in BeginEdit()
  151. #if defined(__WXGTK20__)
  152. auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
  153. // Once the combobox is dropped, reset the flag to allow the focus-loss handler
  154. // to function and close the editor.
  155. evtHandler->SetInSetFocus( false );
  156. #endif
  157. }
  158. void GRID_CELL_LAYER_SELECTOR::onComboCloseUp( wxCommandEvent& aEvent )
  159. {
  160. auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
  161. // Forward the combobox close up event to the cell event handler as a focus kill event
  162. // so that the grid editor is dismissed when the combox closes, otherwise it leaves the
  163. // dropdown arrow visible in the cell.
  164. wxFocusEvent event( wxEVT_KILL_FOCUS, m_control->GetId() );
  165. event.SetEventObject( m_control );
  166. evtHandler->ProcessEvent( event );
  167. }