KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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/textctrl.h>
33#include <wx/version.h>
34#include <kiplatform/ui.h>
35
36
37STD_BITMAP_BUTTON::STD_BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId,
38 const wxBitmap& aDummyBitmap, const wxPoint& aPos,
39 const wxSize& aSize, int aStyle ) :
40 wxPanel( aParent, aId, aPos, aSize, aStyle, wxS( "StdBitmapButton" ) )
41{
42 if( aSize == wxDefaultSize )
43 {
44#ifndef __WXMSW__
45 wxSize defaultSize = wxButton::GetDefaultSize( aParent );
46 defaultSize.IncBy( 1 );
47#else
48 // wxButton::GetDefaultSize does not work on Windows, it's based on some archiac
49 // ascii text size logic that does not hold true in modern Windows at hi dpi scaling
50 // instead spawn a button, grab it's size and then toss it :D
51 wxButton* dummyButton = new wxButton( this, wxID_ANY );
52 dummyButton->SetLabelText( "Z" );
53
54 wxSize defaultSize = dummyButton->GetSize();
55 dummyButton->Hide();
56 delete dummyButton;
57#endif
58 SetMinSize( defaultSize );
59 }
60
61 Bind( wxEVT_PAINT, &STD_BITMAP_BUTTON::OnPaint, this );
62 Bind( wxEVT_LEFT_UP, &STD_BITMAP_BUTTON::OnLeftButtonUp, this );
63 Bind( wxEVT_LEFT_DOWN, &STD_BITMAP_BUTTON::OnLeftButtonDown, this );
64 Bind( wxEVT_KILL_FOCUS, &STD_BITMAP_BUTTON::OnKillFocus, this );
65 Bind( wxEVT_LEAVE_WINDOW, &STD_BITMAP_BUTTON::OnMouseLeave, this );
66 Bind( wxEVT_ENTER_WINDOW, &STD_BITMAP_BUTTON::OnMouseEnter, this );
67
68 Bind( wxEVT_SYS_COLOUR_CHANGED,
69 wxSysColourChangedEventHandler( STD_BITMAP_BUTTON::onThemeChanged ),
70 this );
71}
72
73
75{
76}
77
78
79void STD_BITMAP_BUTTON::onThemeChanged( wxSysColourChangedEvent &aEvent )
80{
81 Refresh();
82}
83
84
85void STD_BITMAP_BUTTON::SetBitmap( const wxBitmapBundle& aBmp )
86{
87 m_bitmap = aBmp;
88
89#ifndef __WXMSW__
90 wxSize size = m_bitmap.GetDefaultSize();
91
92 SetMinSize( wxSize( size.GetWidth() + 8, size.GetHeight() + 8 ) );
93#else
94 wxSize size = m_bitmap.GetPreferredBitmapSizeFor( this );
95 size.IncBy( 8 ); // padding
96
97 // Now adjust the min size but don't reduce it
98 wxSize minSize = GetMinSize();
99
100 // only change the width
101 // we want to keep the height at the original determined button height
102 // or else forms will get funny
103 // additionally we prefer to make the button square
104 if( size.GetWidth() > minSize.GetHeight() )
105 minSize.SetWidth( size.GetWidth() );
106 else
107 minSize.SetWidth( minSize.GetHeight() );
108
109 SetMinSize( minSize );
110#endif
111}
112
113
114void STD_BITMAP_BUTTON::OnKillFocus( wxFocusEvent& aEvent )
115{
116 if( m_stateButton != 0 )
117 {
118 m_stateButton = 0;
119 Refresh();
120 }
121
122 aEvent.Skip();
123}
124
125
126void STD_BITMAP_BUTTON::OnMouseLeave( wxMouseEvent& aEvent )
127{
128 if( m_stateButton != 0 )
129 {
130 m_stateButton = 0;
131 Refresh();
132 }
133
134 aEvent.Skip();
135}
136
137
138void STD_BITMAP_BUTTON::OnMouseEnter( wxMouseEvent& aEvent )
139{
140 if( m_stateButton != wxCONTROL_CURRENT )
141 {
142 m_stateButton = wxCONTROL_CURRENT;
143 Refresh();
144 }
145
146 aEvent.Skip();
147}
148
149
150void STD_BITMAP_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
151{
152 m_stateButton = 0;
153
154 Refresh();
155
156 wxEvtHandler* pEventHandler = GetEventHandler();
157 wxASSERT( pEventHandler );
158
159 pEventHandler->CallAfter(
160 [this]()
161 {
162 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
163 evt.SetEventObject( this );
164 GetEventHandler()->ProcessEvent( evt );
165 } );
166
167 aEvent.Skip();
168}
169
170
171void STD_BITMAP_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
172{
173 m_stateButton = wxCONTROL_PRESSED;
174 Refresh();
175
176 aEvent.Skip();
177}
178
179
180void STD_BITMAP_BUTTON::OnPaint( wxPaintEvent& WXUNUSED( aEvent ) )
181{
182 wxPaintDC dc( this );
183 wxSize size = GetSize();
184
185#ifdef __WXMAC__
186 auto drawBackground =
187 [&]( wxRect aRect )
188 {
189 // wxWidgets doesn't have much support for dark mode on OSX; none of the
190 // system colours return the right values, nor does wxRendererNative draw
191 // the borders correctly. So we add some empirically chosen hacks here.
192
193 // NOTE: KEEP THESE HACKS IN SYNC WITH SPLIT_BUTTON
194
195 wxColor fg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT );
196 wxColor bg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
197
198 aRect.width += 1;
199 aRect.height += 1;
200
202 {
203 bg = bg.ChangeLightness( m_bIsEnable ? 130 : 120 );
204 dc.SetBrush( bg );
205 dc.SetPen( bg );
206 }
207 else
208 {
209 bg = bg.ChangeLightness( m_bIsEnable ? 200 : 160 );
210 dc.SetBrush( bg );
211 fg = fg.ChangeLightness( 180 );
212 dc.SetPen( fg );
213 }
214
215 dc.DrawRoundedRectangle( aRect, aRect.height / 4.0 );
216 };
217#endif
218
219 wxRect r1;
220 r1.x = 0;
221 r1.y = 0;
222 r1.width = size.GetWidth();
223 r1.height = size.GetHeight();
224
225#ifdef __WXMAC__
226 // wxRendereNative doesn't handle dark mode on OSX.
227 drawBackground( r1 );
228#else
229 #ifdef __WXMSW__
230 r1.width += 1;
231 #endif
232
233 wxRendererNative::Get().DrawPushButton( this, dc, r1, m_stateButton );
234#endif
235
236 if( m_bitmap.IsOk() )
237 {
238#ifndef __WXMSW__
239 wxSize bmpSize = m_bitmap.GetDefaultSize();
240#else
241 wxSize bmpSize = m_bitmap.GetPreferredBitmapSizeFor( this );
242#endif
243
244 r1.x = ( size.GetWidth() - bmpSize.GetWidth() ) / 2;
245
246 if( r1.x < 0 )
247 r1.x = 0;
248
249 r1.y += ( size.GetHeight() - bmpSize.GetHeight() ) / 2;
250
251 wxBitmap bm = m_bitmap.GetBitmapFor( this );
252
253 if( !m_bIsEnable )
254 bm.ConvertToDisabled();
255
256 dc.DrawBitmap( bm, r1.x, r1.y, true );
257 }
258}
259
260
261bool STD_BITMAP_BUTTON::Enable( bool aEnable )
262{
263 m_bIsEnable = aEnable;
264 wxPanel::Enable( m_bIsEnable );
265
266 if( m_bIsEnable && m_stateButton == wxCONTROL_DISABLED )
267 {
268 m_stateButton = 0;
269 Refresh();
270 }
271
272 if( !m_bIsEnable && m_stateButton != wxCONTROL_DISABLED )
273 {
274 m_stateButton = wxCONTROL_DISABLED;
275 Refresh();
276 }
277
278 return aEnable;
279}
void OnPaint(wxPaintEvent &WXUNUSED(aEvent))
void OnKillFocus(wxFocusEvent &aEvent)
void OnLeftButtonDown(wxMouseEvent &aEvent)
wxBitmapBundle m_bitmap
bool Enable(bool aEnable=true) override
void OnMouseEnter(wxMouseEvent &aEvent)
void SetBitmap(const wxBitmapBundle &aBmp)
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)
const int minSize
Push and Shove router track width and via size dialog.
static const wxSize defaultSize(FRAME_T aFrameType, wxWindow *aWindow)
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:48
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...