KiCad PCB EDA Suite
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-2022 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>
27#include <wx/button.h>
28#include <wx/dcclient.h>
29#include <wx/renderer.h>
30#include <wx/settings.h>
31
32#define wxCONTROL_SEPARATOR wxCONTROL_SPECIAL
33
34
35BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos,
36 const wxSize& aSize, int aStyles ) :
37 wxPanel( aParent, aId, aPos, aSize, aStyles ),
38 m_isRadioButton( false ),
39 m_showBadge( false ),
40 m_badgeColor( wxColor( 210, 0, 0, 0 ) ), // dark red
41 m_badgeTextColor( wxColor( wxT( "white" ) ) ),
42 m_buttonState( 0 ),
43 m_padding( 0 ),
44 m_acceptDraggedInClicks( false )
45{
46 if( aSize == wxDefaultSize )
47 SetMinSize( wxButton::GetDefaultSize() + wxSize( m_padding * 2, m_padding * 2) );
48
49 m_badgeFont = GetFont().Smaller().MakeBold();
50
52}
53
54
55BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxBitmap& aDummyBitmap,
56 const wxPoint& aPos, const wxSize& aSize, int aStyles ) :
57 wxPanel( aParent, aId, aPos, aSize, aStyles ),
58 m_isRadioButton( false ),
59 m_showBadge( false ),
60 m_buttonState( 0 ),
61 m_padding( 5 ),
62 m_acceptDraggedInClicks( false )
63{
64 if( aSize == wxDefaultSize )
65 SetMinSize( wxButton::GetDefaultSize() + wxSize( m_padding * 2, m_padding * 2) );
66
68}
69
70
72{
73 Bind( wxEVT_PAINT, &BITMAP_BUTTON::OnPaint, this );
74 Bind( wxEVT_LEFT_UP, &BITMAP_BUTTON::OnLeftButtonUp, this );
75 Bind( wxEVT_LEFT_DOWN, &BITMAP_BUTTON::OnLeftButtonDown, this );
76 Bind( wxEVT_LEAVE_WINDOW, &BITMAP_BUTTON::OnMouseLeave, this );
77 Bind( wxEVT_ENTER_WINDOW, &BITMAP_BUTTON::OnMouseEnter, this );
78 Bind( wxEVT_KILL_FOCUS, &BITMAP_BUTTON::OnKillFocus, this );
79 Bind( wxEVT_SET_FOCUS, &BITMAP_BUTTON::OnSetFocus, this );
80}
81
82
84{
85}
86
87
88void BITMAP_BUTTON::SetPadding( int aPadding )
89{
90 m_padding = aPadding;
91 SetMinSize( m_unadjustedMinSize + wxSize( aPadding * 2, aPadding * 2 ) );
92}
93
94
95void BITMAP_BUTTON::SetBitmap( const wxBitmap& aBmp )
96{
97 m_normalBitmap = aBmp;
98 m_unadjustedMinSize = aBmp.GetSize();
99
100 SetMinSize( wxSize( aBmp.GetWidth() + ( m_padding * 2 ), aBmp.GetHeight() + ( m_padding * 2 ) ) );
101}
102
103
104void BITMAP_BUTTON::SetDisabledBitmap( const wxBitmap& aBmp )
105{
106 m_disabledBitmap = aBmp;
107}
108
109
110void BITMAP_BUTTON::AcceptDragInAsClick( bool aAcceptDragIn )
111{
112 m_acceptDraggedInClicks = aAcceptDragIn;
113}
114
115
116void BITMAP_BUTTON::OnMouseLeave( wxEvent& aEvent )
117{
118 clearFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED );
119 Refresh();
120 aEvent.Skip();
121}
122
123
124void BITMAP_BUTTON::OnMouseEnter( wxEvent& aEvent )
125{
126 setFlag( wxCONTROL_CURRENT );
127 Refresh();
128 aEvent.Skip();
129}
130
131
132void BITMAP_BUTTON::OnKillFocus( wxEvent& aEvent )
133{
134 clearFlag( wxCONTROL_FOCUSED );
135 Refresh();
136 aEvent.Skip();
137}
138
139
140void BITMAP_BUTTON::OnSetFocus( wxEvent& aEvent )
141{
142 if( !hasFlag( wxCONTROL_CHECKABLE ) )
143 setFlag( wxCONTROL_FOCUSED );
144
145 Refresh();
146 aEvent.Skip();
147}
148
149
150void BITMAP_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
151{
152 // Only create a button event when the control is enabled
153 // and only accept clicks that came without prior mouse-down if configured
154 if( !hasFlag( wxCONTROL_DISABLED )
155 && ( m_acceptDraggedInClicks || hasFlag( wxCONTROL_PRESSED | wxCONTROL_FOCUSED ) ) )
156 {
157 GetEventHandler()->CallAfter( [=]()
158 {
159 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
160 evt.SetEventObject( this );
161 GetEventHandler()->ProcessEvent( evt );
162 } );
163 }
164
165 clearFlag( wxCONTROL_PRESSED );
166 Refresh();
167
168 aEvent.Skip();
169}
170
171
172void BITMAP_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
173{
174 if( hasFlag( wxCONTROL_CHECKABLE ) )
175 {
176 if( hasFlag( wxCONTROL_CHECKED ) && !m_isRadioButton )
177 {
178 clearFlag( wxCONTROL_CHECKED );
179
180 GetEventHandler()->CallAfter(
181 [=]()
182 {
183 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
184 evt.SetEventObject( this );
185 evt.SetInt( 0 );
186 GetEventHandler()->ProcessEvent( evt );
187 } );
188 }
189 else
190 {
191 setFlag( wxCONTROL_CHECKED );
192
193 GetEventHandler()->CallAfter(
194 [=]()
195 {
196 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
197 evt.SetEventObject( this );
198 evt.SetInt( 1 );
199 GetEventHandler()->ProcessEvent( evt );
200 } );
201 }
202 }
203 else
204 {
205 setFlag( wxCONTROL_PRESSED );
206 }
207
208 Refresh();
209
210 aEvent.Skip();
211}
212
213
214void BITMAP_BUTTON::OnPaint( wxPaintEvent& aEvent )
215{
216 bool darkMode = KIPLATFORM::UI::IsDarkTheme();
217 wxColor highlightColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
218
219 // The drawing rectangle
220 wxRect rect( wxPoint( 0, 0 ), GetSize() );
221 wxPaintDC dc( this );
222
224 {
225 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) ) );
226 dc.DrawLine( wxPoint( GetSize().x / 2, 0 ), wxPoint( GetSize().x / 2, GetSize().y ) );
227 return;
228 }
229
230 // This drawing is done so the button looks the same as an AUI toolbar button
231 if( !hasFlag( wxCONTROL_DISABLED ) )
232 {
233 if( hasFlag( wxCONTROL_PRESSED ) )
234 {
235 dc.SetPen( wxPen( highlightColor ) );
236 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 20 : 150 ) ) );
237 dc.DrawRectangle( rect );
238 }
239 else if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_FOCUSED ) )
240 {
241 dc.SetPen( wxPen( highlightColor ) );
242 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
243
244 // Checked items need a lighter hover rectangle
245 if( hasFlag( wxCONTROL_CHECKED ) )
246 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 50 : 180 ) ) );
247
248 dc.DrawRectangle( rect );
249 }
250 else if( hasFlag( wxCONTROL_CHECKED ) )
251 {
252 dc.SetPen( wxPen( highlightColor ) );
253 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
254 dc.DrawRectangle( rect );
255 }
256 }
257
258 const wxBitmap& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap;
259
260 // Draw the bitmap with the upper-left corner offset by the padding
261 if( bmp.IsOk() )
262 dc.DrawBitmap( bmp, m_padding, m_padding, true );
263
264 // Draw the badge
265 if( m_showBadge )
266 {
267 dc.SetFont( m_badgeFont );
268
269 wxSize box_size = dc.GetTextExtent( m_badgeText ) + wxSize( 6, 2 );
270 wxSize box_offset = box_size + wxSize( m_padding - 2, m_padding );
271 wxSize text_offset = box_offset - wxSize( 3, 1 );
272
273 dc.SetPen( wxPen( m_badgeColor ) );
274 dc.SetBrush( wxBrush( m_badgeColor ) );
275 dc.DrawRoundedRectangle( rect.GetRightBottom() - box_offset, box_size, -0.25 );
276
277 dc.SetTextForeground( m_badgeTextColor );
278 dc.DrawText( m_badgeText, rect.GetRightBottom() - text_offset );
279 }
280}
281
282
283bool BITMAP_BUTTON::Enable( bool aEnable )
284{
285 // If the requested state is already the current state, don't do anything
286 if( aEnable != hasFlag( wxCONTROL_DISABLED ) )
287 return false;
288
289 wxPanel::Enable( aEnable );
290
291 if( aEnable )
292 clearFlag( wxCONTROL_DISABLED );
293 else
294 setFlag( wxCONTROL_DISABLED );
295
296 Refresh();
297
298 return true;
299}
300
301
303{
304 setFlag( wxCONTROL_CHECKABLE );
305}
306
307
309{
310 setFlag( wxCONTROL_CHECKABLE );
311 m_isRadioButton = true;
312}
313
314
316{
317 setFlag( wxCONTROL_SEPARATOR | wxCONTROL_DISABLED );
318 SetMinSize( wxSize( m_padding * 2, wxButton::GetDefaultSize().y ) );
319}
320
321
322void BITMAP_BUTTON::Check( bool aCheck )
323{
324 wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), "Button is not a checkButton." );
325
326 if( aCheck )
327 setFlag( wxCONTROL_CHECKED );
328 else
329 clearFlag( wxCONTROL_CHECKED );
330
331 Refresh();
332}
333
334
336{
337 wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), "Button is not a checkButton." );
338
339 return hasFlag( wxCONTROL_CHECKED );
340}
#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)
void SetBitmap(const wxBitmap &aBmp)
Set the bitmap shown when the button is enabled.
bool Enable(bool aEnable=true) override
Enable the button.
void setFlag(int aFlag)
void Check(bool aCheck=true)
Check the control.
bool hasFlag(int aFlag) const
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.
wxSize m_unadjustedMinSize
Accept mouse-up as click even if mouse-down happened outside of the control.
void clearFlag(int aFlag)
bool m_acceptDraggedInClicks
wxBitmap m_disabledBitmap
wxBitmap m_normalBitmap
void OnPaint(wxPaintEvent &aEvent)
void OnMouseEnter(wxEvent &aEvent)
void OnLeftButtonUp(wxMouseEvent &aEvent)
void SetIsCheckButton()
Setup the control as a two-state button (checked or unchecked).
wxColor m_badgeColor
void SetPadding(int aPadding)
Set the amount of padding present on each side of the bitmap.
void SetDisabledBitmap(const wxBitmap &aBmp)
Set the bitmap shown when the button is disabled.
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:31
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...