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