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