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.

185 lines
4.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <functional>
  20. #include <widgets/wx_combobox.h>
  21. #include <wx/dc.h>
  22. #include <wx/pen.h>
  23. #include <wx/settings.h>
  24. #define SEPARATOR wxT( "---" )
  25. WX_COMBOBOX::WX_COMBOBOX( wxWindow* aParent, int aId, const wxString& aValue, const wxPoint& aPos,
  26. const wxSize& aSize, int n, const wxString choices[], long style ) :
  27. wxOwnerDrawnComboBox( aParent, aId, aValue, aPos, aSize, n, choices, style ),
  28. m_lastSelection( 0 )
  29. {
  30. }
  31. WX_COMBOBOX::~WX_COMBOBOX()
  32. {
  33. }
  34. void WX_COMBOBOX::DoSetPopupControl( wxComboPopup* aPopup )
  35. {
  36. using namespace std::placeholders;
  37. wxOwnerDrawnComboBox::DoSetPopupControl( aPopup );
  38. // Bind events to intercept selections, so the separator can be made nonselectable.
  39. GetVListBoxComboPopup()->Bind( wxEVT_MOTION, &WX_COMBOBOX::TryVetoMouse, this );
  40. GetVListBoxComboPopup()->Bind( wxEVT_LEFT_DOWN, &WX_COMBOBOX::TryVetoMouse, this );
  41. GetVListBoxComboPopup()->Bind( wxEVT_LEFT_UP, &WX_COMBOBOX::TryVetoMouse, this );
  42. GetVListBoxComboPopup()->Bind( wxEVT_LEFT_DCLICK, &WX_COMBOBOX::TryVetoMouse, this );
  43. GetVListBoxComboPopup()->Bind( wxEVT_LISTBOX, std::bind( &WX_COMBOBOX::TryVetoSelect,
  44. this, _1, true ) );
  45. Bind( wxEVT_COMBOBOX, std::bind( &WX_COMBOBOX::TryVetoSelect, this, _1, false ) );
  46. }
  47. void WX_COMBOBOX::OnDrawItem( wxDC& aDC, wxRect const& aRect, int aItem, int aFlags ) const
  48. {
  49. wxString text = GetMenuText( aItem );
  50. if( text == SEPARATOR )
  51. {
  52. wxPen pen( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ), 1 );
  53. aDC.SetPen( pen );
  54. aDC.DrawLine( aRect.x, aRect.y + aRect.height / 2, aRect.x + aRect.width,
  55. aRect.y + aRect.height / 2 );
  56. }
  57. else
  58. {
  59. wxCoord x, y;
  60. if( aFlags & wxODCB_PAINTING_CONTROL )
  61. {
  62. x = aRect.x + GetMargins().x;
  63. y = ( aRect.height - aDC.GetCharHeight() ) / 2 + aRect.y;
  64. }
  65. else
  66. {
  67. x = aRect.x + 2;
  68. y = aRect.y;
  69. }
  70. aDC.DrawText( text, x, y );
  71. }
  72. }
  73. int WX_COMBOBOX::GetCharHeight() const
  74. {
  75. return wxOwnerDrawnComboBox::GetCharHeight() + 2;
  76. }
  77. wxCoord WX_COMBOBOX::OnMeasureItem( size_t aItem ) const
  78. {
  79. if( GetMenuText( aItem ) == SEPARATOR )
  80. return 11;
  81. else
  82. return wxOwnerDrawnComboBox::OnMeasureItem( aItem );
  83. }
  84. wxCoord WX_COMBOBOX::OnMeasureItemWidth( size_t aItem ) const
  85. {
  86. if( GetMenuText( aItem ) == SEPARATOR )
  87. return GetTextRect().GetWidth() - 2;
  88. else
  89. return wxOwnerDrawnComboBox::OnMeasureItemWidth( aItem );
  90. }
  91. void WX_COMBOBOX::TryVetoMouse( wxMouseEvent& aEvent )
  92. {
  93. int item = GetVListBoxComboPopup()->VirtualHitTest( aEvent.GetPosition().y );
  94. if( GetMenuText( item ) != SEPARATOR )
  95. aEvent.Skip();
  96. }
  97. void WX_COMBOBOX::TryVetoSelect( wxCommandEvent& aEvent, bool aInner )
  98. {
  99. int sel = GetSelectionEither( aInner );
  100. if( sel >= 0 && sel < (int) GetCount() )
  101. {
  102. wxString text = GetMenuText( sel );
  103. if( text == SEPARATOR )
  104. {
  105. SetSelectionEither( aInner, m_lastSelection );
  106. }
  107. else
  108. {
  109. m_lastSelection = sel;
  110. aEvent.Skip();
  111. }
  112. }
  113. }
  114. void WX_COMBOBOX::Append( const wxString& aText, const wxString& aMenuText )
  115. {
  116. if( !aMenuText.IsEmpty() )
  117. m_menuText[ GetCount() ] = aMenuText;
  118. wxOwnerDrawnComboBox::Append( aText );
  119. }
  120. wxString WX_COMBOBOX::GetMenuText( int aItem ) const
  121. {
  122. if( m_menuText.count( aItem ) )
  123. return m_menuText.at( aItem );
  124. else if( aItem >= 0 && aItem < (int) GetCount() )
  125. return GetVListBoxComboPopup()->GetString( aItem );
  126. else
  127. return wxEmptyString;
  128. }
  129. int WX_COMBOBOX::GetSelectionEither( bool aInner ) const
  130. {
  131. if( aInner )
  132. return GetVListBoxComboPopup()->wxVListBox::GetSelection();
  133. else
  134. return GetSelection();
  135. }
  136. void WX_COMBOBOX::SetSelectionEither( bool aInner, int aSel )
  137. {
  138. if( aSel >= 0 && aSel < (int) GetCount() )
  139. {
  140. if( aInner )
  141. return GetVListBoxComboPopup()->wxVListBox::SetSelection( aSel );
  142. else
  143. return SetSelection( aSel );
  144. }
  145. }