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