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