KiCad PCB EDA Suite
std_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) 2016 Anil8735(https://stackoverflow.com/users/3659387/anil8753)
5 * from https://stackoverflow.com/a/37274011
6 * Copyright (C) 2020-2022 Kicad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27#include <wx/button.h>
28#include <wx/dcclient.h>
29#include <wx/menu.h>
30#include <wx/renderer.h>
31#include <wx/settings.h>
32#include <wx/version.h>
33#include <kiplatform/ui.h>
34
35
36STD_BITMAP_BUTTON::STD_BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId,
37 const wxBitmap& aDummyBitmap, const wxPoint& aPos,
38 const wxSize& aSize, int aStyle ) :
39 wxPanel( aParent, aId, aPos, aSize, aStyle, wxS( "StdBitmapButton" ) )
40{
41 if( aSize == wxDefaultSize )
42 {
43 #if wxCHECK_VERSION( 3, 1, 3 )
44 wxSize defaultSize = wxButton::GetDefaultSize( aParent );
45 #else
46 wxSize defaultSize = wxButton::GetDefaultSize();
47 #endif
48
49 SetMinSize( wxSize( defaultSize.GetWidth() + 1, defaultSize.GetHeight() + 1 ) );
50 }
51
52 Bind( wxEVT_PAINT, &STD_BITMAP_BUTTON::OnPaint, this );
53 Bind( wxEVT_LEFT_UP, &STD_BITMAP_BUTTON::OnLeftButtonUp, this );
54 Bind( wxEVT_LEFT_DOWN, &STD_BITMAP_BUTTON::OnLeftButtonDown, this );
55 Bind( wxEVT_KILL_FOCUS, &STD_BITMAP_BUTTON::OnKillFocus, this );
56 Bind( wxEVT_LEAVE_WINDOW, &STD_BITMAP_BUTTON::OnMouseLeave, this );
57 Bind( wxEVT_ENTER_WINDOW, &STD_BITMAP_BUTTON::OnMouseEnter, this );
58
59 Bind( wxEVT_SYS_COLOUR_CHANGED,
60 wxSysColourChangedEventHandler( STD_BITMAP_BUTTON::onThemeChanged ),
61 this );
62}
63
64
66{
67}
68
69
70void STD_BITMAP_BUTTON::onThemeChanged( wxSysColourChangedEvent &aEvent )
71{
72 Refresh();
73}
74
75
76void STD_BITMAP_BUTTON::SetBitmap( const wxBitmap& aBmp )
77{
78 m_bitmap = aBmp;
79
80 SetMinSize( wxSize( m_bitmap.GetWidth() + 8, m_bitmap.GetHeight() + 8 ) );
81}
82
83
84void STD_BITMAP_BUTTON::OnKillFocus( wxFocusEvent& aEvent )
85{
86 if( m_stateButton != 0 )
87 {
88 m_stateButton = 0;
89 Refresh();
90 }
91
92 aEvent.Skip();
93}
94
95
96void STD_BITMAP_BUTTON::OnMouseLeave( wxMouseEvent& aEvent )
97{
98 if( m_stateButton != 0 )
99 {
100 m_stateButton = 0;
101 Refresh();
102 }
103
104 aEvent.Skip();
105}
106
107
108void STD_BITMAP_BUTTON::OnMouseEnter( wxMouseEvent& aEvent )
109{
110 if( m_stateButton != wxCONTROL_CURRENT )
111 {
112 m_stateButton = wxCONTROL_CURRENT;
113 Refresh();
114 }
115
116 aEvent.Skip();
117}
118
119
120void STD_BITMAP_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
121{
122 m_stateButton = 0;
123
124 Refresh();
125
126 wxEvtHandler* pEventHandler = GetEventHandler();
127 wxASSERT( pEventHandler );
128
129 pEventHandler->CallAfter(
130 [=]()
131 {
132 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
133 evt.SetEventObject( this );
134 GetEventHandler()->ProcessEvent( evt );
135 } );
136
137 aEvent.Skip();
138}
139
140
141void STD_BITMAP_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
142{
143 m_stateButton = wxCONTROL_PRESSED;
144 Refresh();
145
146 aEvent.Skip();
147}
148
149
150void STD_BITMAP_BUTTON::OnPaint( wxPaintEvent& WXUNUSED( aEvent ) )
151{
152 wxPaintDC dc( this );
153 wxSize size = GetSize();
154
155#ifdef __WXMAC__
156 auto drawBackground =
157 [&]( wxRect aRect )
158 {
159 // wxWidgets doesn't have much support for dark mode on OSX; none of the
160 // system colours return the right values, nor does wxRendererNative draw
161 // the borders correctly. So we add some empirically chosen hacks here.
162
163 // NOTE: KEEP THESE HACKS IN SYNC WITH SPLIT_BUTTON
164
165 wxColor fg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT );
166 wxColor bg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
167
168 aRect.width += 1;
169 aRect.height += 1;
170
172 {
173 bg = bg.ChangeLightness( m_bIsEnable ? 130 : 120 );
174 dc.SetBrush( bg );
175 dc.SetPen( bg );
176 }
177 else
178 {
179 bg = bg.ChangeLightness( m_bIsEnable ? 200 : 160 );
180 dc.SetBrush( bg );
181 fg = fg.ChangeLightness( 180 );
182 dc.SetPen( fg );
183 }
184
185 dc.DrawRoundedRectangle( aRect, aRect.height / 4.0 );
186 };
187#endif
188
189 wxRect r1;
190 r1.x = 0;
191 r1.y = 0;
192 r1.width = size.GetWidth();
193 r1.height = size.GetHeight();
194
195#ifdef __WXMAC__
196 // wxRendereNative doesn't handle dark mode on OSX.
197 drawBackground( r1 );
198#else
199 #ifdef __WXMSW__
200 r1.width += 1;
201 #endif
202
203 wxRendererNative::Get().DrawPushButton( this, dc, r1, m_stateButton );
204#endif
205
206 if( m_bitmap.IsOk() )
207 {
208 r1.x = ( size.GetWidth() - m_bitmap.GetWidth() ) / 2;
209
210 if( r1.x < 0 )
211 r1.x = 0;
212
213 r1.y += ( size.GetHeight() - m_bitmap.GetHeight() ) / 2;
214
215 dc.DrawBitmap( m_bIsEnable ? m_bitmap : m_bitmap.ConvertToDisabled(), r1.x, r1.y, true );
216 }
217}
218
219
220bool STD_BITMAP_BUTTON::Enable( bool aEnable )
221{
222 m_bIsEnable = aEnable;
223 wxPanel::Enable( m_bIsEnable );
224
225 if( m_bIsEnable && m_stateButton == wxCONTROL_DISABLED )
226 {
227 m_stateButton = 0;
228 Refresh();
229 }
230
231 if( !m_bIsEnable && m_stateButton != wxCONTROL_DISABLED )
232 {
233 m_stateButton = wxCONTROL_DISABLED;
234 Refresh();
235 }
236
237 return aEnable;
238}
void OnPaint(wxPaintEvent &WXUNUSED(aEvent))
void OnKillFocus(wxFocusEvent &aEvent)
void SetBitmap(const wxBitmap &aBmp)
void OnLeftButtonDown(wxMouseEvent &aEvent)
bool Enable(bool aEnable=true) override
void OnMouseEnter(wxMouseEvent &aEvent)
void onThemeChanged(wxSysColourChangedEvent &aEvent)
STD_BITMAP_BUTTON(wxWindow *aParent, wxWindowID aId, const wxBitmap &aDummyBitmap, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, int aStyle=0)
void OnLeftButtonUp(wxMouseEvent &aEvent)
void OnMouseLeave(wxMouseEvent &aEvent)
static const wxSize defaultSize(FRAME_T aFrameType)
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...