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.

125 lines
4.1 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. #ifndef GRID_ICON_TEXT_HELPERS_H
  24. #define GRID_ICON_TEXT_HELPERS_H
  25. #include <wx/bitmap.h>
  26. #include <wx/bmpcbox.h>
  27. #include <wx/generic/gridctrl.h>
  28. #include <wx/generic/grideditors.h>
  29. #include <vector>
  30. class wxGrid;
  31. enum class BITMAPS : unsigned int;
  32. //---- Grid helpers: custom wxGridCellRenderer that renders icon and a label ------------
  33. class GRID_CELL_ICON_TEXT_RENDERER : public wxGridCellStringRenderer
  34. {
  35. public:
  36. GRID_CELL_ICON_TEXT_RENDERER( const std::vector<BITMAPS>& icons, const wxArrayString& names );
  37. void Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
  38. const wxRect& aRect, int aRow, int aCol, bool isSelected ) override;
  39. wxSize GetBestSize( wxGrid & grid, wxGridCellAttr & attr, wxDC & dc, int row, int col ) override;
  40. private:
  41. std::vector<BITMAPS> m_icons;
  42. wxArrayString m_names;
  43. };
  44. //---- Grid helpers: custom wxGridCellRenderer that renders just an icon ----------------
  45. //
  46. // Note: use with read only cells
  47. class GRID_CELL_ICON_RENDERER : public wxGridCellRenderer
  48. {
  49. public:
  50. GRID_CELL_ICON_RENDERER( const wxBitmap& icon );
  51. void Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
  52. const wxRect& aRect, int aRow, int aCol, bool isSelected ) override;
  53. wxSize GetBestSize( wxGrid & grid, wxGridCellAttr & attr, wxDC & dc, int row, int col ) override;
  54. wxGridCellRenderer* Clone() const override;
  55. private:
  56. const wxBitmap& m_icon;
  57. };
  58. //---- Grid helpers: custom wxGridCellRenderer that renders just an icon from wxArtprovider -
  59. //
  60. // Note: use with read only cells
  61. class GRID_CELL_STATUS_ICON_RENDERER : public wxGridCellRenderer
  62. {
  63. public:
  64. GRID_CELL_STATUS_ICON_RENDERER( int aStatus );
  65. void Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
  66. const wxRect& aRect, int aRow, int aCol, bool isSelected ) override;
  67. wxSize GetBestSize( wxGrid & grid, wxGridCellAttr & attr, wxDC & dc, int row, int col ) override;
  68. wxGridCellRenderer* Clone() const override;
  69. private:
  70. int m_status;
  71. wxBitmap m_bitmap;
  72. };
  73. //---- Grid helpers: custom wxGridCellEditor ------------------------------------------
  74. //
  75. // Note: this implementation is an adaptation of wxGridCellChoiceEditor
  76. class GRID_CELL_ICON_TEXT_POPUP : public wxGridCellEditor
  77. {
  78. public:
  79. GRID_CELL_ICON_TEXT_POPUP( const std::vector<BITMAPS>& icons, const wxArrayString& names );
  80. wxGridCellEditor* Clone() const override;
  81. void Create( wxWindow* aParent, wxWindowID aId, wxEvtHandler* aEventHandler ) override;
  82. wxString GetValue() const override;
  83. void SetSize( const wxRect& aRect ) override;
  84. void BeginEdit( int aRow, int aCol, wxGrid* aGrid ) override;
  85. bool EndEdit( int , int , const wxGrid* , const wxString& , wxString *aNewVal ) override;
  86. void ApplyEdit( int aRow, int aCol, wxGrid* aGrid ) override;
  87. void Reset() override;
  88. protected:
  89. wxBitmapComboBox* Combo() const { return static_cast<wxBitmapComboBox*>( m_control ); }
  90. std::vector<BITMAPS> m_icons;
  91. wxArrayString m_names;
  92. wxString m_value;
  93. wxDECLARE_NO_COPY_CLASS( GRID_CELL_ICON_TEXT_POPUP );
  94. };
  95. #endif // GRID_ICON_TEXT_HELPERS_H