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-2022 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 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 int position = m_names.Index( value );
58
59 if( position < (int) m_icons.size() && position != wxNOT_FOUND )
60 {
61 bitmap = KiBitmap( m_icons[ position ] );
62 aDC.DrawBitmap( bitmap, rect.GetLeft() + 3, rect.GetTop() + 2, true );
63 }
64 else // still need a bitmap to fetch the width
65 {
66 bitmap = KiBitmap( m_icons[ 0 ] );
67 }
68
69 // draw the text
70 rect.SetLeft( rect.GetLeft() + bitmap.GetWidth() + 7 );
71 SetTextColoursAndFont( aGrid, aAttr, aDC, isSelected );
72 aGrid.DrawTextRectangle( aDC, value, rect, wxALIGN_LEFT, wxALIGN_CENTRE );
73}
74
75wxSize GRID_CELL_ICON_TEXT_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
76 int row, int col )
77{
78 wxBitmap bitmap = KiBitmap( m_icons[ row ] );
79 wxString text = grid.GetCellValue( row, col );
80 wxSize size = wxGridCellStringRenderer::DoGetBestSize( attr, dc, text );
81
82 size.x += bitmap.GetWidth() + 6;
83
84 return size;
85}
86
87
89 m_icon( icon )
90{
91}
92
93
94void GRID_CELL_ICON_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
95 const wxRect& aRect, int aRow, int aCol, bool isSelected )
96{
97 wxRect rect = aRect;
98 rect.Inflate( -1 );
99
100 // erase background
101 wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
102
103 // Draw icon
104 if( m_icon.IsOk() )
105 {
106 aDC.DrawBitmap( m_icon,
107 rect.GetLeft() + ( rect.GetWidth() - m_icon.GetWidth() ) / 2,
108 rect.GetTop() + ( rect.GetHeight() - m_icon.GetHeight() ) / 2,
109 true );
110 }
111}
112
113
114wxSize GRID_CELL_ICON_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
115 int row, int col )
116{
117 return wxSize( m_icon.GetWidth() + 6, m_icon.GetHeight() + 4 );
118}
119
120
121wxGridCellRenderer* GRID_CELL_ICON_RENDERER::Clone() const
122{
123 return new GRID_CELL_ICON_RENDERER( m_icon );
124}
125
126
127//---- Grid helpers: custom wxGridCellRenderer that renders just an icon ----------------
128//
129// Note: this renderer is supposed to be used with read only cells
130
132 m_status( aStatus )
133{
134 if( m_status != 0 )
135 {
136 m_bitmap = wxArtProvider::GetBitmap( wxArtProvider::GetMessageBoxIconId( m_status ),
137 wxART_BUTTON );
138 }
139 else
140 {
141 // Dummy bitmap for size
142 m_bitmap = wxArtProvider::GetBitmap( wxArtProvider::GetMessageBoxIconId( wxICON_INFORMATION ),
143 wxART_BUTTON );
144 }
145}
146
147
148void GRID_CELL_STATUS_ICON_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
149 const wxRect& aRect, int aRow, int aCol,
150 bool isSelected )
151{
152 wxRect rect = aRect;
153 rect.Inflate( -1 );
154
155 // erase background
156 wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
157
158 // Draw icon
159 if( ( m_status != 0 ) && m_bitmap.IsOk() )
160 {
161 aDC.DrawBitmap( m_bitmap,
162 rect.GetLeft() + ( rect.GetWidth() - m_bitmap.GetWidth() ) / 2,
163 rect.GetTop() + ( rect.GetHeight() - m_bitmap.GetHeight() ) / 2,
164 true );
165 }
166}
167
168
169wxSize GRID_CELL_STATUS_ICON_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
170 int row, int col )
171{
172 return wxSize( m_bitmap.GetWidth() + 6, m_bitmap.GetHeight() + 4 );
173}
174
175
176wxGridCellRenderer* GRID_CELL_STATUS_ICON_RENDERER::Clone() const
177{
179}
180
181
183 const wxArrayString& names ) :
184 m_icons( icons ),
185 m_names( names )
186{
187}
188
189
190wxGridCellEditor* GRID_CELL_ICON_TEXT_POPUP::Clone() const
191{
193}
194
195
196void GRID_CELL_ICON_TEXT_POPUP::Create( wxWindow* aParent, wxWindowID aId,
197 wxEvtHandler* aEventHandler )
198{
199 m_control = new wxBitmapComboBox( aParent, aId, wxEmptyString, wxDefaultPosition,
200 wxDefaultSize, 0, nullptr,
201 wxCB_READONLY | wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB | 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() && m_icons[ i ] != BITMAPS::INVALID_BITMAP )
208 Combo()->Append( m_names[ i ], KiBitmapBundle( 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
223void GRID_CELL_ICON_TEXT_POPUP::SetSize( const wxRect& aRect )
224{
225 wxRect rect( aRect );
226 rect.Inflate( -1 );
227
228#if !defined( __WXMSW__ ) && !defined( __WXGTK__ )
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
241void 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(__WXGTK__)
264 evtHandler->SetInSetFocus( false );
265#endif
266}
267
268
269bool 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
286void 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
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
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:104
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