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.

83 lines
2.8 KiB

  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 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. #ifndef WX_COMBOBOX_H
  20. #define WX_COMBOBOX_H
  21. #include <map>
  22. #include <wx/odcombo.h>
  23. /**
  24. * Fix some issues with wxCombobox:
  25. * - setting the value of a non-read-only combobox doesn't work on MSW
  26. * - rollover highlighting in the dropdown doesn't work on OSX
  27. * - separators don't work anywhere
  28. */
  29. class WX_COMBOBOX : public wxOwnerDrawnComboBox
  30. {
  31. public:
  32. WX_COMBOBOX( wxWindow* aParent, int aId = wxID_ANY, const wxString& aValue = wxEmptyString,
  33. const wxPoint& aPos = wxDefaultPosition, const wxSize& aSize = wxDefaultSize,
  34. int n = 0, const wxString choices[] = nullptr, long style = 0 );
  35. virtual ~WX_COMBOBOX();
  36. void Append( const wxString& aText, const wxString& aMenuText = wxEmptyString );
  37. int GetCharHeight() const override;
  38. protected:
  39. virtual void DoSetPopupControl( wxComboPopup* aPopup ) override;
  40. virtual void OnDrawItem( wxDC& aDC, const wxRect& aRect, int aItem, int aFlags ) const override;
  41. virtual wxCoord OnMeasureItem( size_t aItem ) const override;
  42. virtual wxCoord OnMeasureItemWidth( size_t aItem ) const override;
  43. /// Veto a mouseover event if in the separator
  44. void TryVetoMouse( wxMouseEvent& aEvent );
  45. /**
  46. * Veto a select event for the separator
  47. *
  48. * @param aEvent - the wxCommandEvent caller
  49. * @param aInner - true if event was called for the inner list (ie the popup)
  50. */
  51. void TryVetoSelect( wxCommandEvent& aEvent, bool aInner );
  52. /**
  53. * Safely get a string for an item, returning wxEmptyString if the item doesn't exist.
  54. */
  55. wxString GetMenuText( int aItem ) const;
  56. /**
  57. * Get selection from either the outer (combo box) or inner (popup) list.
  58. */
  59. int GetSelectionEither( bool aInner ) const;
  60. /**
  61. * Safely set selection for either the outer (combo box) or inner (popup) list, doing nothing
  62. * for invalid selections.
  63. */
  64. void SetSelectionEither( bool aInner, int aSel );
  65. private:
  66. std::map<int, wxString> m_menuText;
  67. int m_lastSelection;
  68. };
  69. #endif // WX_COMBOBOX_H