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