KiCad PCB EDA Suite
Loading...
Searching...
No Matches
bitmap_button.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) 2020 Ian McInerney <ian.s.mcinerney at ieee dot org>
5 * Copyright (C) 2020-2024 Kicad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <kiplatform/ui.h>
26#include <pgm_base.h>
29#include <wx/button.h>
30#include <wx/dcclient.h>
31#include <wx/renderer.h>
32#include <wx/settings.h>
33
34#define wxCONTROL_SEPARATOR wxCONTROL_SPECIAL
35
36
37BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos,
38 const wxSize& aSize, int aStyles ) :
39 wxPanel( aParent, aId, aPos, aSize, aStyles ),
40 m_isRadioButton( false ),
41 m_showBadge( false ),
42 m_badgeColor( wxColor( 210, 0, 0 ) ), // dark red
43 m_badgeTextColor( wxColor( wxT( "white" ) ) ),
44 m_buttonState( 0 ),
45 m_padding( 0 ),
46 m_isToolbarButton( false ),
47 m_acceptDraggedInClicks( false ),
48 m_centerBitmap( true )
49{
50 m_badgeFont = GetFont().Smaller().MakeBold();
51
53}
54
55
56BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxBitmap& aDummyBitmap,
57 const wxPoint& aPos, const wxSize& aSize, int aStyles ) :
58 wxPanel( aParent, aId, aPos, aSize, aStyles ),
59 m_isRadioButton( false ),
60 m_showBadge( false ),
61 m_badgeColor( wxColor( 210, 0, 0 ) ), // dark red
62 m_badgeTextColor( wxColor( wxT( "white" ) ) ),
63 m_buttonState( 0 ),
64 m_padding( 5 ),
65 m_isToolbarButton( false ),
66 m_acceptDraggedInClicks( false ),
67 m_centerBitmap( true )
68{
69 m_badgeFont = GetFont().Smaller().MakeBold();
70
72}
73
74
76{
77 Bind( wxEVT_DPI_CHANGED, &BITMAP_BUTTON::OnDPIChanged, this );
78 Bind( wxEVT_PAINT, &BITMAP_BUTTON::OnPaint, this );
79 Bind( wxEVT_LEFT_UP, &BITMAP_BUTTON::OnLeftButtonUp, this );
80 Bind( wxEVT_LEFT_DOWN, &BITMAP_BUTTON::OnLeftButtonDown, this );
81 Bind( wxEVT_LEAVE_WINDOW, &BITMAP_BUTTON::OnMouseLeave, this );
82 Bind( wxEVT_ENTER_WINDOW, &BITMAP_BUTTON::OnMouseEnter, this );
83 Bind( wxEVT_KILL_FOCUS, &BITMAP_BUTTON::OnKillFocus, this );
84 Bind( wxEVT_SET_FOCUS, &BITMAP_BUTTON::OnSetFocus, this );
85}
86
87
89{
90 Unbind( wxEVT_DPI_CHANGED, &BITMAP_BUTTON::OnDPIChanged, this );
91 Unbind( wxEVT_PAINT, &BITMAP_BUTTON::OnPaint, this );
92 Unbind( wxEVT_LEFT_UP, &BITMAP_BUTTON::OnLeftButtonUp, this );
93 Unbind( wxEVT_LEFT_DOWN, &BITMAP_BUTTON::OnLeftButtonDown, this );
94 Unbind( wxEVT_LEAVE_WINDOW, &BITMAP_BUTTON::OnMouseLeave, this );
95 Unbind( wxEVT_ENTER_WINDOW, &BITMAP_BUTTON::OnMouseEnter, this );
96 Unbind( wxEVT_KILL_FOCUS, &BITMAP_BUTTON::OnKillFocus, this );
97 Unbind( wxEVT_SET_FOCUS, &BITMAP_BUTTON::OnSetFocus, this );
98}
99
100
102{
104 return wxSize( m_unadjustedMinSize.x + m_padding * 2, wxButton::GetDefaultSize().y );
105
106 return m_unadjustedMinSize + wxSize( m_padding * 2, m_padding * 2 );
107}
108
109
111{
112 // Uncomment to override wxFB sizes
113 // SetMinSize( DoGetBestSize() );
114
115 InvalidateBestSize();
116}
117
118
119void BITMAP_BUTTON::SetPadding( int aPadding )
120{
121 m_padding = aPadding;
122
124}
125
126
127void BITMAP_BUTTON::SetBitmap( const wxBitmapBundle& aBmp )
128{
129 m_normalBitmap = aBmp;
130
131 // This is a bit of a hack, but fixes button scaling issues on some platforms when those buttons
132 // use KiScaledBitmap. When that method is retired, this can probably be revisited.
134 {
135 m_unadjustedMinSize = m_normalBitmap.GetPreferredBitmapSizeFor( this );
136 }
137 else
138 {
139#ifndef __WXMSW__
140 m_unadjustedMinSize = m_normalBitmap.GetDefaultSize();
141#else
142 m_unadjustedMinSize = m_normalBitmap.GetPreferredBitmapSizeFor( this );
143#endif
144 }
145
147}
148
149
150void BITMAP_BUTTON::SetDisabledBitmap( const wxBitmapBundle& aBmp )
151{
152 m_disabledBitmap = aBmp;
153}
154
155
156void BITMAP_BUTTON::AcceptDragInAsClick( bool aAcceptDragIn )
157{
158 m_acceptDraggedInClicks = aAcceptDragIn;
159}
160
161
162void BITMAP_BUTTON::OnMouseLeave( wxEvent& aEvent )
163{
164 if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED ) )
165 {
166 clearFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED );
167 Refresh();
168 }
169
170 aEvent.Skip();
171}
172
173
174void BITMAP_BUTTON::OnMouseEnter( wxEvent& aEvent )
175{
176 if( !hasFlag( wxCONTROL_CURRENT ) )
177 {
178 setFlag( wxCONTROL_CURRENT );
179 Refresh();
180 }
181
182 aEvent.Skip();
183}
184
185
186void BITMAP_BUTTON::OnKillFocus( wxEvent& aEvent )
187{
188 if( hasFlag( wxCONTROL_FOCUSED | wxCONTROL_CURRENT | wxCONTROL_PRESSED | wxCONTROL_SELECTED ) )
189 {
190 clearFlag( wxCONTROL_FOCUSED | wxCONTROL_CURRENT | wxCONTROL_PRESSED | wxCONTROL_SELECTED );
191 Refresh();
192 }
193
194 aEvent.Skip();
195}
196
197
198void BITMAP_BUTTON::OnSetFocus( wxEvent& aEvent )
199{
200 if( !hasFlag( wxCONTROL_CHECKABLE ) )
201 {
202 if( !hasFlag( wxCONTROL_FOCUSED ) )
203 {
204 setFlag( wxCONTROL_FOCUSED );
205 Refresh();
206 }
207 }
208
209 aEvent.Skip();
210}
211
212
213void BITMAP_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
214{
215 // Only create a button event when the control is enabled
216 // and only accept clicks that came without prior mouse-down if configured
217 if( !hasFlag( wxCONTROL_DISABLED )
218 && ( m_acceptDraggedInClicks || hasFlag( wxCONTROL_PRESSED | wxCONTROL_FOCUSED ) ) )
219 {
220 GetEventHandler()->CallAfter( [this]()
221 {
222 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
223 evt.SetEventObject( this );
224 GetEventHandler()->ProcessEvent( evt );
225 } );
226 }
227
228 clearFlag( wxCONTROL_PRESSED );
229 Refresh();
230
231 aEvent.Skip();
232}
233
234
235void BITMAP_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
236{
237 if( hasFlag( wxCONTROL_CHECKABLE ) )
238 {
239 if( hasFlag( wxCONTROL_CHECKED ) && !m_isRadioButton )
240 {
241 clearFlag( wxCONTROL_CHECKED );
242
243 GetEventHandler()->CallAfter(
244 [this]()
245 {
246 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
247 evt.SetEventObject( this );
248 evt.SetInt( 0 );
249 GetEventHandler()->ProcessEvent( evt );
250 } );
251 }
252 else
253 {
254 setFlag( wxCONTROL_CHECKED );
255
256 GetEventHandler()->CallAfter(
257 [this]()
258 {
259 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
260 evt.SetEventObject( this );
261 evt.SetInt( 1 );
262 GetEventHandler()->ProcessEvent( evt );
263 } );
264 }
265 }
266 else
267 {
268 setFlag( wxCONTROL_PRESSED );
269 }
270
271 Refresh();
272
273 aEvent.Skip();
274}
275
276
277void BITMAP_BUTTON::OnDPIChanged( wxDPIChangedEvent& aEvent )
278{
279 wxSize newBmSize = m_normalBitmap.GetPreferredBitmapSizeFor( this );
280
281 if( newBmSize != m_unadjustedMinSize )
282 {
283 m_unadjustedMinSize = newBmSize;
285 }
286
287 aEvent.Skip();
288}
289
290
291void BITMAP_BUTTON::OnPaint( wxPaintEvent& aEvent )
292{
293 bool darkMode = KIPLATFORM::UI::IsDarkTheme();
294 wxColor highlightColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
295
296 // The drawing rectangle
297 wxRect rect( wxPoint( 0, 0 ), GetSize() );
298 wxPaintDC dc( this );
299
301 {
302 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) ) );
303 dc.DrawLine( wxPoint( GetSize().x / 2, 0 ), wxPoint( GetSize().x / 2, GetSize().y ) );
304 return;
305 }
306
307 // This drawing is done so the button looks the same as an AUI toolbar button
308 if( !hasFlag( wxCONTROL_DISABLED ) )
309 {
310 if( hasFlag( wxCONTROL_PRESSED ) )
311 {
312 dc.SetPen( wxPen( highlightColor ) );
313 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 20 : 150 ) ) );
314 dc.DrawRectangle( rect );
315 }
316 else if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_FOCUSED ) )
317 {
318 dc.SetPen( wxPen( highlightColor ) );
319 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
320
321 // Checked items need a lighter hover rectangle
322 if( hasFlag( wxCONTROL_CHECKED ) )
323 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 50 : 180 ) ) );
324
325 dc.DrawRectangle( rect );
326 }
327 else if( hasFlag( wxCONTROL_CHECKED ) )
328 {
329 dc.SetPen( wxPen( highlightColor ) );
330 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
331 dc.DrawRectangle( rect );
332 }
333 }
334
335 const wxBitmapBundle& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap;
336
337 wxPoint drawBmpPos( m_padding, m_padding );
338 wxBitmap bmpImg;
340 wxSize bmSize;
341
343 {
345 bmSize = wxSize( size, size );
346 bmpImg = bmp.GetBitmap( bmSize * scale );
347
348 // wxBitmapBundle::GetBitmap thinks we need this rescaled to match the base size
349 if( bmpImg.IsOk() )
350 bmpImg.SetScaleFactor( scale );
351 }
352 else if( bmp.IsOk() )
353 {
354 bmpImg = bmp.GetBitmap( ToPhys( m_unadjustedMinSize ) );
355 bmSize = bmpImg.GetLogicalSize();
356 }
357
358 if( m_centerBitmap )
359 {
360 drawBmpPos.x = ( rect.width - bmSize.x ) / 2;
361 drawBmpPos.y = ( rect.height - bmSize.y ) / 2;
362 }
363
364 // Draw the bitmap with the upper-left corner offset by the padding
365 if( bmp.IsOk() )
366 dc.DrawBitmap( bmpImg, drawBmpPos, true );
367
368 // Draw the badge
369 if( m_showBadge )
370 {
371 dc.SetFont( m_badgeFont );
372
373 wxSize text_padding( 3, 1 );
374
375 if( m_padding )
376 text_padding *= 2;
377
378 wxSize box_size = dc.GetTextExtent( m_badgeText ) + text_padding;
379 wxSize box_offset = box_size;
380
381 if( m_padding != 0 )
382 box_offset += wxSize( m_padding / 3, m_padding / 3 );
383
384 dc.SetPen( wxPen( m_badgeColor ) );
385 dc.SetBrush( wxBrush( m_badgeColor ) );
386 dc.DrawRoundedRectangle( rect.GetRightBottom() - box_offset, box_size, -0.25 );
387
388 dc.SetTextForeground( m_badgeTextColor );
389 dc.DrawText( m_badgeText, rect.GetRightBottom() - box_offset + ( text_padding / 2 ) );
390 }
391}
392
393
394bool BITMAP_BUTTON::Enable( bool aEnable )
395{
396 // If the requested state is already the current state, don't do anything
397 if( aEnable != hasFlag( wxCONTROL_DISABLED ) )
398 return false;
399
400 wxPanel::Enable( aEnable );
401
402 if( aEnable && hasFlag( wxCONTROL_DISABLED ) )
403 {
404 clearFlag( wxCONTROL_DISABLED );
405 Refresh();
406 }
407
408 if( !aEnable && !hasFlag( wxCONTROL_DISABLED ) )
409 {
410 setFlag( wxCONTROL_DISABLED );
411 Refresh();
412 }
413
414 return true;
415}
416
417
419{
420 setFlag( wxCONTROL_CHECKABLE );
421}
422
423
425{
426 setFlag( wxCONTROL_CHECKABLE );
427 m_isRadioButton = true;
428}
429
430
432{
433 setFlag( wxCONTROL_SEPARATOR | wxCONTROL_DISABLED );
434
436}
437
438
439void BITMAP_BUTTON::Check( bool aCheck )
440{
441 wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), wxS( "Button is not a checkButton." ) );
442
443 if( aCheck && !hasFlag( wxCONTROL_CHECKED ) )
444 {
445 setFlag( wxCONTROL_CHECKED );
446 Refresh();
447 }
448
449 if( !aCheck && hasFlag( wxCONTROL_CHECKED ) )
450 {
451 clearFlag( wxCONTROL_CHECKED );
452 Refresh();
453 }
454}
455
456
458{
459 wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), wxS( "Button is not a checkButton." ) );
460
461 return hasFlag( wxCONTROL_CHECKED );
462}
#define wxCONTROL_SEPARATOR
void OnKillFocus(wxEvent &aEvent)
void SetIsRadioButton()
bool IsChecked() const
void OnSetFocus(wxEvent &aEvent)
void AcceptDragInAsClick(bool aAcceptDragIn=true)
Accept mouse-up as click even if mouse-down happened outside of the control.
wxFont m_badgeFont
void OnMouseLeave(wxEvent &aEvent)
bool Enable(bool aEnable=true) override
Enable the button.
void setFlag(int aFlag)
wxBitmapBundle m_disabledBitmap
void Check(bool aCheck=true)
Check the control.
bool hasFlag(int aFlag) const
void SetDisabledBitmap(const wxBitmapBundle &aBmp)
Set the bitmap shown when the button is disabled.
BITMAP_BUTTON(wxWindow *aParent, wxWindowID aId, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, int aStyles=wxBORDER_NONE|wxTAB_TRAVERSAL)
wxColor m_badgeTextColor
void OnLeftButtonDown(wxMouseEvent &aEvent)
wxString m_badgeText
void SetIsSeparator()
Render button as a toolbar separator.
void invalidateBestSize()
wxSize m_unadjustedMinSize
void clearFlag(int aFlag)
bool m_acceptDraggedInClicks
Draws bitmap centered in the control.
wxBitmapBundle m_normalBitmap
virtual wxSize DoGetBestSize() const override
void OnPaint(wxPaintEvent &aEvent)
void OnMouseEnter(wxEvent &aEvent)
void OnDPIChanged(wxDPIChangedEvent &aEvent)
void OnLeftButtonUp(wxMouseEvent &aEvent)
void SetIsCheckButton()
Setup the control as a two-state button (checked or unchecked).
void SetBitmap(const wxBitmapBundle &aBmp)
Set the bitmap shown when the button is enabled.
bool m_isToolbarButton
Accept mouse-up as click even if mouse-down happened outside of the control.
wxColor m_badgeColor
void SetPadding(int aPadding)
Set the amount of padding present on each side of the bitmap.
APPEARANCE m_Appearance
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:679
double GetPixelScaleFactor(const wxWindow *aWindow)
Tries to determine the pixel scaling factor currently in use for the window.
Definition: wxgtk/ui.cpp:175
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: wxgtk/ui.cpp:48
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
const int scale