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.

255 lines
8.1 KiB

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