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.

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