KiCad PCB EDA Suite
grid_icon_text_helpers.cpp
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-2021 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 
25 
26 #include <wx/artprov.h>
27 #include <wx/defs.h>
28 #include <wx/textctrl.h>
29 #include <wx/dc.h>
30 
31 #include <bitmaps.h>
32 
33 
35  const wxArrayString& names ) :
36  m_icons( icons ),
37  m_names( names )
38 {
39 }
40 
41 
42 void GRID_CELL_ICON_TEXT_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
43  const wxRect& aRect, int aRow, int aCol, bool isSelected )
44 {
45  wxString value = aGrid.GetCellValue( aRow, aCol );
46  wxBitmap bitmap;
47 
48  wxRect rect = aRect;
49  rect.Inflate( -1 );
50 
51  // erase background
52  wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
53 
54  // draw the icon
55  // note that the set of icons might be smaller than the set of labels if the last
56  // label is <...>.
57  auto position = m_names.Index( value );
58  if( position < (int) m_icons.size() && position != wxNOT_FOUND )
59  {
60  bitmap = KiBitmap( m_icons[ position ] );
61  aDC.DrawBitmap( bitmap, rect.GetLeft() + 3, rect.GetTop() + 2, true );
62  }
63  else // still need a bitmap to fetch the width
64  {
65  bitmap = KiBitmap( m_icons[ 0 ] );
66  }
67 
68  // draw the text
69  rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 7 );
70  SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
71  aGrid.DrawTextRectangle( aDC, value, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
72 }
73 
74 wxSize GRID_CELL_ICON_TEXT_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
75  int row, int col )
76 {
77  wxBitmap bitmap = KiBitmap( m_icons[ row ] );
78  wxString text = grid.GetCellValue( row, col );
79  wxSize size = wxGridCellStringRenderer::DoGetBestSize( attr, dc, text );
80 
81  size.x += bitmap.GetWidth() + 6;
82 
83  return size;
84 }
85 
86 
88  m_icon( icon )
89 {
90 }
91 
92 
93 void GRID_CELL_ICON_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
94  const wxRect& aRect, int aRow, int aCol, bool isSelected )
95 {
96  wxRect rect = aRect;
97  rect.Inflate( -1 );
98 
99  // erase background
100  wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
101 
102  // Draw icon
103  if( m_icon.IsOk() )
104  {
105  aDC.DrawBitmap( m_icon,
106  rect.GetLeft() + ( rect.GetWidth() - m_icon.GetWidth() ) / 2,
107  rect.GetTop() + ( rect.GetHeight() - m_icon.GetHeight() ) / 2,
108  true );
109  }
110 }
111 
112 
113 wxSize GRID_CELL_ICON_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
114  int row, int col )
115 {
116  return wxSize( m_icon.GetWidth() + 6, m_icon.GetHeight() + 4 );
117 }
118 
119 
120 wxGridCellRenderer* GRID_CELL_ICON_RENDERER::Clone() const
121 {
122  return new GRID_CELL_ICON_RENDERER( m_icon );
123 }
124 
125 
126 //---- Grid helpers: custom wxGridCellRenderer that renders just an icon ----------------
127 //
128 // Note: this renderer is supposed to be used with read only cells
129 
131  m_status( aStatus )
132 {
133  if( m_status != 0 )
134  {
135  m_bitmap = wxArtProvider::GetBitmap( wxArtProvider::GetMessageBoxIconId( m_status ),
136  wxART_BUTTON );
137  }
138  else
139  {
140  // Dummy bitmap for size
141  m_bitmap = wxArtProvider::GetBitmap( wxArtProvider::GetMessageBoxIconId( wxICON_INFORMATION ),
142  wxART_BUTTON );
143  }
144 }
145 
146 
147 void GRID_CELL_STATUS_ICON_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
148  const wxRect& aRect, int aRow, int aCol,
149  bool isSelected )
150 {
151  wxRect rect = aRect;
152  rect.Inflate( -1 );
153 
154  // erase background
155  wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
156 
157  // Draw icon
158  if( ( m_status != 0 ) && m_bitmap.IsOk() )
159  {
160  aDC.DrawBitmap( m_bitmap,
161  rect.GetLeft() + ( rect.GetWidth() - m_bitmap.GetWidth() ) / 2,
162  rect.GetTop() + ( rect.GetHeight() - m_bitmap.GetHeight() ) / 2,
163  true );
164  }
165 }
166 
167 
168 wxSize GRID_CELL_STATUS_ICON_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
169  int row, int col )
170 {
171  return wxSize( m_bitmap.GetWidth() + 6, m_bitmap.GetHeight() + 4 );
172 }
173 
174 
175 wxGridCellRenderer* GRID_CELL_STATUS_ICON_RENDERER::Clone() const
176 {
178 }
179 
180 
181 GRID_CELL_ICON_TEXT_POPUP::GRID_CELL_ICON_TEXT_POPUP( const std::vector<BITMAPS>& icons,
182  const wxArrayString& names ) :
183  m_icons( icons ),
184  m_names( names )
185 {
186 }
187 
188 
189 wxGridCellEditor* GRID_CELL_ICON_TEXT_POPUP::Clone() const
190 {
192 }
193 
194 
195 void GRID_CELL_ICON_TEXT_POPUP::Create( wxWindow* aParent, wxWindowID aId,
196  wxEvtHandler* aEventHandler )
197 {
198  m_control = new wxBitmapComboBox( aParent, aId, wxEmptyString, wxDefaultPosition,
199  wxDefaultSize, 0, nullptr,
200  wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB |
201  wxBORDER_NONE );
202 
203  for( unsigned i = 0; i < m_names.size(); ++i )
204  {
205  // note that the set of icons might be smaller than the set of labels if
206  // the last label is <...>.
207  if( i < m_icons.size() )
208  Combo()->Append( m_names[ i ], KiBitmap( m_icons[ i ] ) );
209  else
210  Combo()->Append( m_names[ i ] );
211  }
212 
213  wxGridCellEditor::Create(aParent, aId, aEventHandler);
214 }
215 
216 
218 {
219  return Combo()->GetValue();
220 }
221 
222 
223 void GRID_CELL_ICON_TEXT_POPUP::SetSize( const wxRect& aRect )
224 {
225  wxRect rect( aRect );
226  rect.Inflate( -1 );
227 
228 #if !defined( __WXMSW__ ) && !defined( __WXGTK20__ )
229  // Only implemented in generic wxBitmapComboBox; MSW and GTK use native controls
230  Combo()->SetButtonPosition( 0, 0, wxRIGHT, 2 );
231 #endif
232 
233 #if defined( __WXMAC__ )
234  rect.Inflate( 3 ); // no FOCUS_RING, even on Mac
235 #endif
236 
237  Combo()->SetSize( rect, wxSIZE_ALLOW_MINUS_ONE );
238 }
239 
240 
241 void GRID_CELL_ICON_TEXT_POPUP::BeginEdit( int aRow, int aCol, wxGrid* aGrid )
242 {
243  auto evtHandler = static_cast<wxGridCellEditorEvtHandler*>( m_control->GetEventHandler() );
244 
245  // Don't immediately end if we get a kill focus event within BeginEdit
246  evtHandler->SetInSetFocus( true );
247 
248  m_value = aGrid->GetTable()->GetValue( aRow, aCol );
249 
250  Combo()->SetSelection( Combo()->FindString( m_value ) );
251  Combo()->SetFocus();
252 
253 #ifdef __WXOSX_COCOA__
254  // This is a work around for the combobox being simply dismissed when a
255  // choice is made in it under OS X. The bug is almost certainly due to a
256  // problem in focus events generation logic but it's not obvious to fix and
257  // for now this at least allows one to use wxGrid.
258  Combo()->Popup();
259 #endif
260 
261  // When dropping down the menu, a kill focus event
262  // happens after this point, so we can't reset the flag yet.
263 #if !defined(__WXGTK20__)
264  evtHandler->SetInSetFocus( false );
265 #endif
266 }
267 
268 
269 bool GRID_CELL_ICON_TEXT_POPUP::EndEdit( int , int , const wxGrid* , const wxString& ,
270  wxString *aNewVal )
271 {
272  const wxString value = Combo()->GetValue();
273 
274  if( value == m_value )
275  return false;
276 
277  m_value = value;
278 
279  if( aNewVal )
280  *aNewVal = value;
281 
282  return true;
283 }
284 
285 
286 void GRID_CELL_ICON_TEXT_POPUP::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
287 {
288  aGrid->GetTable()->SetValue( aRow, aCol, m_value );
289 }
290 
291 
293 {
294  Combo()->SetSelection( Combo()->FindString( m_value ) );
295 }
296 
297 
wxBitmapComboBox * Combo() const
wxGridCellRenderer * Clone() const override
wxGridCellEditor * Clone() const override
wxString GetValue() const override
GRID_CELL_ICON_TEXT_POPUP(const std::vector< BITMAPS > &icons, const wxArrayString &names)
wxGridCellRenderer * Clone() const override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
std::vector< BITMAPS > m_icons
bool EndEdit(int, int, const wxGrid *, const wxString &, wxString *aNewVal) override
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, const wxRect &aRect, int aRow, int aCol, bool isSelected) override
GRID_CELL_ICON_TEXT_RENDERER(const std::vector< BITMAPS > &icons, const wxArrayString &names)
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
void SetSize(const wxRect &aRect) override
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
GRID_CELL_ICON_RENDERER(const wxBitmap &icon)
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, const wxRect &aRect, int aRow, int aCol, bool isSelected) override
wxSize GetBestSize(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col) override
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, const wxRect &aRect, int aRow, int aCol, bool isSelected) override
wxSize GetBestSize(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col) override
wxSize GetBestSize(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col) override