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.

271 lines
8.6 KiB

1 year ago
8 months ago
4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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 <wx/textctrl.h>
  25. #include <pgm_base.h>
  26. #include <settings/settings_manager.h>
  27. #include <settings/color_settings.h>
  28. #include <footprint_editor_settings.h>
  29. #include <board.h>
  30. #include <lset.h>
  31. #include <pcb_edit_frame.h>
  32. #include <pcb_layer_box_selector.h>
  33. #include <settings/color_settings.h>
  34. #include <widgets/layer_presentation.h>
  35. //-------- Custom wxGridCellRenderers --------------------------------------------------
  36. GRID_CELL_LAYER_RENDERER::GRID_CELL_LAYER_RENDERER( PCB_BASE_FRAME* aFrame ) :
  37. m_frame( aFrame )
  38. {
  39. }
  40. GRID_CELL_LAYER_RENDERER::~GRID_CELL_LAYER_RENDERER()
  41. {
  42. }
  43. void GRID_CELL_LAYER_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
  44. const wxRect& aRect, int aRow, int aCol, bool isSelected )
  45. {
  46. int value = aGrid.GetTable()->GetValueAsLong( aRow, aCol );
  47. COLOR_SETTINGS* cs = nullptr;
  48. wxRect rect = aRect;
  49. rect.Inflate( -1 );
  50. // erase background
  51. wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
  52. if( m_frame )
  53. {
  54. cs = m_frame->GetColorSettings();
  55. }
  56. else
  57. {
  58. SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
  59. FOOTPRINT_EDITOR_SETTINGS* cfg = mgr.GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>( "fpedit" );
  60. cs = mgr.GetColorSettings( cfg->m_ColorTheme );
  61. }
  62. // draw the swatch
  63. #ifdef __WXMAC__
  64. int size = 14;
  65. #else
  66. int size = KiROUND( 14 * aDC.GetContentScaleFactor() );
  67. #endif
  68. wxBitmap bitmap( size, size );
  69. LAYER_PRESENTATION::DrawColorSwatch( bitmap,
  70. cs->GetColor( ToLAYER_ID( LAYER_PCB_BACKGROUND ) ),
  71. cs->GetColor( ToLAYER_ID( value ) ) );
  72. aDC.DrawBitmap( bitmap, rect.GetLeft() + 4,
  73. rect.GetTop() + ( rect.GetHeight() - bitmap.GetHeight() ) / 2, true );
  74. // draw the text
  75. PCB_LAYER_ID layer = ToLAYER_ID( value );
  76. wxString layerName;
  77. if( m_frame )
  78. layerName = m_frame->GetBoard()->GetLayerName( layer );
  79. else
  80. layerName = BOARD::GetStandardLayerName( layer );
  81. rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 8 );
  82. SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
  83. aGrid.DrawTextRectangle( aDC, layerName, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
  84. }
  85. //-------- Custom wxGridCellEditors ----------------------------------------------------
  86. //
  87. // Note: this implementation is an adaptation of wxGridCellChoiceEditor
  88. GRID_CELL_LAYER_SELECTOR::GRID_CELL_LAYER_SELECTOR( PCB_BASE_FRAME* aFrame, const LSET& aMask,
  89. bool aShowNonActivated ) :
  90. m_frame( aFrame ),
  91. m_mask( aMask ),
  92. m_showNonActivated( aShowNonActivated ),
  93. m_value( 0 )
  94. {
  95. }
  96. wxGridCellEditor* GRID_CELL_LAYER_SELECTOR::Clone() const
  97. {
  98. return new GRID_CELL_LAYER_SELECTOR( m_frame, m_mask );
  99. }
  100. void GRID_CELL_LAYER_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
  101. wxEvtHandler* aEventHandler )
  102. {
  103. m_control = new PCB_LAYER_BOX_SELECTOR( aParent, aId, wxEmptyString,
  104. wxDefaultPosition, wxDefaultSize, 0, nullptr,
  105. wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | wxBORDER_NONE );
  106. LayerBox()->SetLayersHotkeys( false );
  107. LayerBox()->SetBoardFrame( m_frame );
  108. LayerBox()->SetNotAllowedLayerSet( m_mask );
  109. LayerBox()->ShowNonActivatedLayers( m_showNonActivated );
  110. wxGridCellEditor::Create(aParent, aId, aEventHandler);
  111. }
  112. wxString GRID_CELL_LAYER_SELECTOR::GetValue() const
  113. {
  114. if( LayerBox()->GetLayerSelection() != UNDEFINED_LAYER )
  115. {
  116. PCB_LAYER_ID layer = ToLAYER_ID( LayerBox()->GetLayerSelection() );
  117. if( m_frame )
  118. return m_frame->GetBoard()->GetLayerName( layer );
  119. else
  120. return BOARD::GetStandardLayerName( layer );
  121. }
  122. return wxEmptyString;
  123. }
  124. void GRID_CELL_LAYER_SELECTOR::SetSize( const wxRect& aRect )
  125. {
  126. wxRect rect( aRect );
  127. rect.Inflate( -1 );
  128. #if !defined( __WXMSW__ ) && !defined( __WXGTK__ )
  129. // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
  130. LayerBox()->SetButtonPosition( 0, 0, wxRIGHT, 0 );
  131. #endif
  132. #if defined( __WXMAC__ )
  133. rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
  134. #endif
  135. LayerBox()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
  136. }
  137. void GRID_CELL_LAYER_SELECTOR::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
  138. {
  139. auto* evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
  140. // Don't immediately end if we get a kill focus event within BeginEdit
  141. evtHandler->SetInSetFocus( true );
  142. // These event handlers are needed to properly dismiss the editor when the popup is closed
  143. m_control->Bind(wxEVT_COMBOBOX_DROPDOWN, &GRID_CELL_LAYER_SELECTOR::onComboDropDown, this);
  144. m_control->Bind(wxEVT_COMBOBOX_CLOSEUP, &GRID_CELL_LAYER_SELECTOR::onComboCloseUp, this);
  145. m_value = aGrid->GetTable()->GetValueAsLong( aRow, aCol );
  146. // Footprints are defined in a global context and may contain layers not enabled
  147. // on the current board. Check and display all layers if so.
  148. if( m_frame && !m_frame->GetBoard()->IsLayerEnabled( ToLAYER_ID( m_value ) ) )
  149. LayerBox()->ShowNonActivatedLayers( true );
  150. LayerBox()->SetNotAllowedLayerSet( m_mask );
  151. LayerBox()->Resync();
  152. LayerBox()->SetLayerSelection( m_value );
  153. LayerBox()->SetFocus();
  154. #ifdef __WXOSX_COCOA__
  155. // This is a work around for the combobox being simply dismissed when a
  156. // choice is made in it under OS X. The bug is almost certainly due to a
  157. // problem in focus events generation logic but it's not obvious to fix and
  158. // for now this at least allows one to use wxGrid.
  159. if( !LayerBox()->IsPopupShown() )
  160. LayerBox()->Popup();
  161. #endif
  162. // When dropping down the menu, a kill focus event
  163. // happens after this point, so we can't reset the flag yet.
  164. #if !defined(__WXGTK__)
  165. evtHandler->SetInSetFocus( false );
  166. #endif
  167. }
  168. bool GRID_CELL_LAYER_SELECTOR::EndEdit( int , int , const wxGrid* , const wxString& ,
  169. wxString *newval )
  170. {
  171. const int value = LayerBox()->GetLayerSelection();
  172. if ( value == m_value )
  173. return false;
  174. m_value = value;
  175. if ( newval )
  176. *newval = GetValue();
  177. return true;
  178. }
  179. void GRID_CELL_LAYER_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
  180. {
  181. aGrid->GetTable()->SetValueAsLong( aRow, aCol, (long) m_value );
  182. }
  183. void GRID_CELL_LAYER_SELECTOR::Reset()
  184. {
  185. LayerBox()->SetLayerSelection( m_value );
  186. }
  187. void GRID_CELL_LAYER_SELECTOR::onComboDropDown( wxCommandEvent& aEvent )
  188. {
  189. // On other platforms this is done in BeginEdit()
  190. #if defined(__WXGTK__)
  191. auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
  192. // Once the combobox is dropped, reset the flag to allow the focus-loss handler
  193. // to function and close the editor.
  194. evtHandler->SetInSetFocus( false );
  195. #endif
  196. }
  197. void GRID_CELL_LAYER_SELECTOR::onComboCloseUp( wxCommandEvent& aEvent )
  198. {
  199. auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
  200. // Forward the combobox close up event to the cell event handler as a focus kill event
  201. // so that the grid editor is dismissed when the combox closes, otherwise it leaves the
  202. // dropdown arrow visible in the cell.
  203. wxFocusEvent event( wxEVT_KILL_FOCUS, m_control->GetId() );
  204. event.SetEventObject( m_control );
  205. evtHandler->ProcessEvent( event );
  206. }