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 The 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 ),
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 ),
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( FromDIP( m_dipSize.x + m_padding * 2 ), wxButton::GetDefaultSize().y );
105
106 return FromDIP( m_dipSize + 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 aPaddingDIP )
120{
121 m_padding = aPaddingDIP;
122
124}
125
126
127void BITMAP_BUTTON::SetBitmap( const wxBitmapBundle& aBmp )
128{
129 m_normalBitmap = aBmp;
130 m_dipSize = m_normalBitmap.GetDefaultSize();
131
133}
134
135
136void BITMAP_BUTTON::SetDisabledBitmap( const wxBitmapBundle& aBmp )
137{
138 m_disabledBitmap = aBmp;
139}
140
141
142void BITMAP_BUTTON::AcceptDragInAsClick( bool aAcceptDragIn )
143{
144 m_acceptDraggedInClicks = aAcceptDragIn;
145}
146
147
148void BITMAP_BUTTON::OnMouseLeave( wxEvent& aEvent )
149{
150 if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED ) )
151 {
152 clearFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED );
153 Refresh();
154 }
155
156 aEvent.Skip();
157}
158
159
160void BITMAP_BUTTON::OnMouseEnter( wxEvent& aEvent )
161{
162 if( !hasFlag( wxCONTROL_CURRENT ) )
163 {
164 setFlag( wxCONTROL_CURRENT );
165 Refresh();
166 }
167
168 aEvent.Skip();
169}
170
171
172void BITMAP_BUTTON::OnKillFocus( wxEvent& aEvent )
173{
174 if( hasFlag( wxCONTROL_FOCUSED | wxCONTROL_CURRENT | wxCONTROL_PRESSED | wxCONTROL_SELECTED ) )
175 {
176 clearFlag( wxCONTROL_FOCUSED | wxCONTROL_CURRENT | wxCONTROL_PRESSED | wxCONTROL_SELECTED );
177 Refresh();
178 }
179
180 aEvent.Skip();
181}
182
183
184void BITMAP_BUTTON::OnSetFocus( wxEvent& aEvent )
185{
186 if( !hasFlag( wxCONTROL_CHECKABLE ) )
187 {
188 if( !hasFlag( wxCONTROL_FOCUSED ) )
189 {
190 setFlag( wxCONTROL_FOCUSED );
191 Refresh();
192 }
193 }
194
195 aEvent.Skip();
196}
197
198
199void BITMAP_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
200{
201 // Only create a button event when the control is enabled
202 // and only accept clicks that came without prior mouse-down if configured
203 if( !hasFlag( wxCONTROL_DISABLED )
204 && ( m_acceptDraggedInClicks || hasFlag( wxCONTROL_PRESSED | wxCONTROL_FOCUSED ) ) )
205 {
206 GetEventHandler()->CallAfter( [this]()
207 {
208 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
209 evt.SetEventObject( this );
210 GetEventHandler()->ProcessEvent( evt );
211 } );
212 }
213
214 clearFlag( wxCONTROL_PRESSED );
215 Refresh();
216
217 aEvent.Skip();
218}
219
220
221void BITMAP_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
222{
223 if( hasFlag( wxCONTROL_CHECKABLE ) )
224 {
225 if( hasFlag( wxCONTROL_CHECKED ) && !m_isRadioButton )
226 {
227 clearFlag( wxCONTROL_CHECKED );
228
229 GetEventHandler()->CallAfter(
230 [this]()
231 {
232 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
233 evt.SetEventObject( this );
234 evt.SetInt( 0 );
235 GetEventHandler()->ProcessEvent( evt );
236 } );
237 }
238 else
239 {
240 setFlag( wxCONTROL_CHECKED );
241
242 GetEventHandler()->CallAfter(
243 [this]()
244 {
245 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
246 evt.SetEventObject( this );
247 evt.SetInt( 1 );
248 GetEventHandler()->ProcessEvent( evt );
249 } );
250 }
251 }
252 else
253 {
254 setFlag( wxCONTROL_PRESSED );
255 }
256
257 Refresh();
258
259 aEvent.Skip();
260}
261
262
263void BITMAP_BUTTON::OnDPIChanged( wxDPIChangedEvent& aEvent )
264{
266 aEvent.Skip();
267}
268
269
270void BITMAP_BUTTON::OnPaint( wxPaintEvent& aEvent )
271{
272 bool darkMode = KIPLATFORM::UI::IsDarkTheme();
273 wxColor highlightColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
274
275 // The drawing rectangle
276 wxRect rect( wxPoint( 0, 0 ), GetSize() );
277 wxPaintDC dc( this );
278
280 {
281 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) ) );
282 dc.DrawLine( wxPoint( GetSize().x / 2, 0 ), wxPoint( GetSize().x / 2, GetSize().y ) );
283 return;
284 }
285
286 // This drawing is done so the button looks the same as an AUI toolbar button
287 if( !hasFlag( wxCONTROL_DISABLED ) )
288 {
289 if( hasFlag( wxCONTROL_PRESSED ) )
290 {
291 dc.SetPen( wxPen( highlightColor ) );
292 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 20 : 150 ) ) );
293 dc.DrawRectangle( rect );
294 }
295 else if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_FOCUSED ) )
296 {
297 dc.SetPen( wxPen( highlightColor ) );
298 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
299
300 // Checked items need a lighter hover rectangle
301 if( hasFlag( wxCONTROL_CHECKED ) )
302 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 50 : 180 ) ) );
303
304 dc.DrawRectangle( rect );
305 }
306 else if( hasFlag( wxCONTROL_CHECKED ) )
307 {
308 dc.SetPen( wxPen( highlightColor ) );
309 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
310 dc.DrawRectangle( rect );
311 }
312 }
313
314 const wxBitmapBundle& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap;
315
316 wxPoint drawBmpPos( m_padding, m_padding );
317 wxBitmap bmpImg;
318 wxSize bmSize;
319
320 if( bmp.IsOk() )
321 {
322 bmpImg = bmp.GetBitmap( ToPhys( FromDIP( m_dipSize ) ) );
323 bmSize = bmpImg.GetLogicalSize();
324 }
325
326 if( m_centerBitmap )
327 {
328 drawBmpPos.x = ( rect.width - bmSize.x ) / 2;
329 drawBmpPos.y = ( rect.height - bmSize.y ) / 2;
330 }
331
332 // Draw the bitmap with the upper-left corner offset by the padding
333 if( bmp.IsOk() )
334 dc.DrawBitmap( bmpImg, drawBmpPos, true );
335
336 // Draw the badge
337 if( m_showBadge )
338 {
339 dc.SetFont( m_badgeFont );
340
341 wxSize text_padding( 3, 1 );
342
343 if( m_padding )
344 text_padding *= 2;
345
346 wxSize box_size = dc.GetTextExtent( m_badgeText ) + text_padding;
347 wxSize box_offset = box_size;
348
349 if( m_padding != 0 )
350 box_offset += wxSize( m_padding / 3, m_padding / 3 );
351
352 dc.SetPen( wxPen( m_badgeColor ) );
353 dc.SetBrush( wxBrush( m_badgeColor ) );
354 dc.DrawRoundedRectangle( rect.GetRightBottom() - box_offset, box_size, -0.25 );
355
356 dc.SetTextForeground( m_badgeTextColor );
357 dc.DrawText( m_badgeText, rect.GetRightBottom() - box_offset + ( text_padding / 2 ) );
358 }
359}
360
361
362bool BITMAP_BUTTON::Enable( bool aEnable )
363{
364 // If the requested state is already the current state, don't do anything
365 if( aEnable != hasFlag( wxCONTROL_DISABLED ) )
366 return false;
367
368 wxPanel::Enable( aEnable );
369
370 if( aEnable && hasFlag( wxCONTROL_DISABLED ) )
371 {
372 clearFlag( wxCONTROL_DISABLED );
373 Refresh();
374 }
375
376 if( !aEnable && !hasFlag( wxCONTROL_DISABLED ) )
377 {
378 setFlag( wxCONTROL_DISABLED );
379 Refresh();
380 }
381
382 return true;
383}
384
385
387{
388 setFlag( wxCONTROL_CHECKABLE );
389}
390
391
393{
394 setFlag( wxCONTROL_CHECKABLE );
395 m_isRadioButton = true;
396}
397
398
400{
401 setFlag( wxCONTROL_SEPARATOR | wxCONTROL_DISABLED );
402
404}
405
406
407void BITMAP_BUTTON::Check( bool aCheck )
408{
409 wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), wxS( "Button is not a checkButton." ) );
410
411 if( aCheck && !hasFlag( wxCONTROL_CHECKED ) )
412 {
413 setFlag( wxCONTROL_CHECKED );
414 Refresh();
415 }
416
417 if( !aCheck && hasFlag( wxCONTROL_CHECKED ) )
418 {
419 clearFlag( wxCONTROL_CHECKED );
420 Refresh();
421 }
422}
423
424
426{
427 wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), wxS( "Button is not a checkButton." ) );
428
429 return hasFlag( wxCONTROL_CHECKED );
430}
#define wxCONTROL_SEPARATOR
void OnKillFocus(wxEvent &aEvent)
bool m_centerBitmap
Draw bitmap centered in the control.
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.
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()
void clearFlag(int aFlag)
bool m_acceptDraggedInClicks
Accept mouse-up as click even if mouse-down happened outside of the control.
wxBitmapBundle m_normalBitmap
void SetPadding(int aPaddingDIP)
Set the amount of padding present on each side of the bitmap.
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.
wxColor m_badgeColor
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...
see class PGM_BASE