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