KiCad PCB EDA Suite
Loading...
Searching...
No Matches
split_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/dcmemory.h>
30#include <wx/menu.h>
31#include <wx/renderer.h>
32#include <wx/settings.h>
33#include <wx/version.h>
34#include <kiplatform/ui.h>
35
36SPLIT_BUTTON::SPLIT_BUTTON( wxWindow* aParent, wxWindowID aId, const wxString& aLabel,
37 const wxPoint& aPos, const wxSize& aSize ) :
38 wxPanel( aParent, aId, aPos, aSize, wxBORDER_NONE | wxTAB_TRAVERSAL, wxS( "DropDownButton" ) ),
39 m_label( aLabel )
40{
41 m_arrowButtonWidth = FromDIP( 20 ); // just a fixed eyeballed constant width
42 m_widthPadding = FromDIP( 10 );
43
44 if( aSize == wxDefaultSize )
45 {
46 wxSize defaultSize = wxButton::GetDefaultSize( aParent );
47 wxSize textSize = GetTextExtent( m_label );
48 SetMinSize( wxSize( std::max( textSize.GetWidth(), defaultSize.GetWidth() + 1 ),
49 defaultSize.GetHeight() + 1 ) );
50 }
51
52 Bind( wxEVT_PAINT, &SPLIT_BUTTON::OnPaint, this );
53 Bind( wxEVT_LEFT_UP, &SPLIT_BUTTON::OnLeftButtonUp, this );
54 Bind( wxEVT_LEFT_DOWN, &SPLIT_BUTTON::OnLeftButtonDown, this );
55 Bind( wxEVT_KILL_FOCUS, &SPLIT_BUTTON::OnKillFocus, this );
56 Bind( wxEVT_LEAVE_WINDOW, &SPLIT_BUTTON::OnMouseLeave, this );
57 Bind( wxEVT_ENTER_WINDOW, &SPLIT_BUTTON::OnMouseEnter, this );
58
59 Bind( wxEVT_SYS_COLOUR_CHANGED, wxSysColourChangedEventHandler( SPLIT_BUTTON::onThemeChanged ),
60 this );
61
62 m_pMenu = new wxMenu();
63}
64
65
67{
68 delete m_pMenu;
69 m_pMenu = nullptr;
70}
71
72
73void SPLIT_BUTTON::onThemeChanged( wxSysColourChangedEvent &aEvent )
74{
75 Refresh();
76}
77
78
79void SPLIT_BUTTON::SetMinSize( const wxSize& aSize )
80{
81 m_unadjustedMinSize = aSize;
82 wxPanel::SetMinSize( wxSize( aSize.GetWidth() + m_arrowButtonWidth + m_widthPadding,
83 aSize.GetHeight() ) );
84}
85
86
88{
89 m_widthPadding = aPadding;
91}
92
93
94void SPLIT_BUTTON::SetBitmap( const wxBitmapBundle& aBmp )
95{
96 m_bitmap = aBmp;
97
98#ifndef __WXMSW__
99 SetMinSize( m_bitmap.GetDefaultSize() );
100#else
101 SetMinSize( m_bitmap.GetPreferredBitmapSizeFor( this ) );
102#endif
103}
104
105
106void SPLIT_BUTTON::SetLabel( const wxString& aLabel )
107{
108 if( m_label != aLabel )
109 {
110 m_label = aLabel;
111 Refresh();
112 }
113}
114
115
117{
118 return m_pMenu;
119}
120
121
122void SPLIT_BUTTON::OnKillFocus( wxFocusEvent& aEvent )
123{
124 if( m_stateButton != 0 || m_stateMenu != 0 )
125 {
126 m_stateButton = 0;
127 m_stateMenu = 0;
128 Refresh();
129 }
130
131 aEvent.Skip();
132}
133
134
135void SPLIT_BUTTON::OnMouseLeave( wxMouseEvent& aEvent )
136{
137 if( m_stateButton != 0 || m_stateMenu != 0 )
138 {
139 m_stateButton = 0;
140 m_stateMenu = 0;
141 Refresh();
142 }
143
144 aEvent.Skip();
145}
146
147
148void SPLIT_BUTTON::OnMouseEnter( wxMouseEvent& aEvent )
149{
150 if( m_stateButton != wxCONTROL_CURRENT || m_stateMenu != wxCONTROL_CURRENT )
151 {
152 m_stateButton = wxCONTROL_CURRENT;
153 m_stateMenu = wxCONTROL_CURRENT;
154 Refresh();
155 }
156
157 aEvent.Skip();
158}
159
160
161void SPLIT_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
162{
163 m_stateButton = 0;
164 m_stateMenu = 0;
165
166 Refresh();
167
168 int x = -1;
169 int y = -1;
170 aEvent.GetPosition( &x, &y );
171
172 if( x < ( GetSize().GetWidth() - m_arrowButtonWidth ) )
173 {
174 wxEvtHandler* pEventHandler = GetEventHandler();
175 wxASSERT( pEventHandler );
176
177 pEventHandler->CallAfter(
178 [this]()
179 {
180 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
181 evt.SetEventObject( this );
182 GetEventHandler()->ProcessEvent( evt );
183 } );
184 }
185
186 m_bLButtonDown = false;
187
188 aEvent.Skip();
189}
190
191
192void SPLIT_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
193{
194 m_bLButtonDown = true;
195
196 int x = -1;
197 int y = -1;
198 aEvent.GetPosition( &x, &y );
199
200 if( x >= ( GetSize().GetWidth() - m_arrowButtonWidth ) )
201 {
202 m_stateButton = 0;
203 m_stateMenu = wxCONTROL_PRESSED;
204 Refresh();
205
206 wxSize size = GetSize();
207 wxPoint position;
208 position.x = 0;
209 position.y = size.GetHeight();
210 PopupMenu( m_pMenu, position );
211
212 m_stateMenu = 0;
213 Refresh();
214 }
215 else
216 {
217 m_stateButton = wxCONTROL_PRESSED;
218 m_stateMenu = wxCONTROL_PRESSED;
219 Refresh();
220 }
221
222 aEvent.Skip();
223}
224
225
226void SPLIT_BUTTON::OnPaint( wxPaintEvent& WXUNUSED( aEvent ) )
227{
228 wxPaintDC dc( this );
229 wxSize size = GetSize();
230 const int width = size.GetWidth() - m_arrowButtonWidth;
231
232#ifdef __WXMAC__
233 auto drawBackground =
234 [&]( wxRect aRect )
235 {
236 // wxWidgets doesn't have much support for dark mode on OSX; none of the
237 // system colours return the right values, nor does wxRendererNative draw
238 // the borders correctly. So we add some empirically chosen hacks here.
239
240 // NOTE: KEEP THESE HACKS IN SYNC WITH STD_BITMAP_BUTTON
241
242 wxColor fg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT );
243 wxColor bg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
244
245 aRect.width += 1;
246 aRect.height += 1;
247
249 {
250 bg = bg.ChangeLightness( m_bIsEnable ? 130 : 120 );
251 dc.SetBrush( bg );
252 dc.SetPen( bg );
253 }
254 else
255 {
256 bg = bg.ChangeLightness( m_bIsEnable ? 200 : 160 );
257 dc.SetBrush( bg );
258 fg = fg.ChangeLightness( 180 );
259 dc.SetPen( fg );
260 }
261
262 dc.DrawRoundedRectangle( aRect, aRect.height / 4.0 );
263 };
264#endif
265
266 // Draw first part of button
267 wxRect r1;
268 r1.x = 0;
269 r1.y = 0;
270 r1.width = width;
271 r1.height = size.GetHeight();
272
273#ifdef __WXMAC__
274 // wxRendereNative doesn't handle dark mode on OSX.
275 drawBackground( r1 );
276#else
277 #ifdef _WXMSW_
278 r1.width += 2;
279 #endif
280
281 wxRendererNative::Get().DrawPushButton( this, dc, r1, m_stateButton );
282#endif
283
284 SetForegroundColour( m_bIsEnable ? wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT )
285 : wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
286
287 if( m_bitmap.IsOk() )
288 {
289#ifndef __WXMSW__
290 wxSize bmpSize = m_bitmap.GetDefaultSize();
291#else
292 wxSize bmpSize = m_bitmap.GetPreferredBitmapSizeFor( this );
293#endif
294 wxBitmap bmp = m_bitmap.GetBitmapFor( this );
295 wxMemoryDC mdc( bmp );
296
297 r1.x = ( width - bmpSize.GetWidth() ) / 2;
298
299 if( r1.x < 0 )
300 r1.x = 0;
301
302 r1.y += ( size.GetHeight() - bmpSize.GetHeight() ) / 2;
303
304 dc.Blit( wxPoint( r1.x, r1.y ), bmpSize, &mdc, wxPoint( 0, 0 ), wxCOPY, true );
305 }
306 else
307 {
308 r1.y += ( ( size.GetHeight() - GetCharHeight() ) / 2 ) - 1;
309 dc.DrawLabel( m_label, r1, wxALIGN_CENTER_HORIZONTAL );
310 }
311
312 // Draw second part of button
313 wxRect r2;
314 r2.x = width;
315 r2.y = 0;
316 r2.width = m_arrowButtonWidth;
317 r2.height = size.GetHeight();
318
319#ifdef __WXMAC__
320 // wxRendereNative doesn't handle dark mode on OSX.
321 drawBackground( r2 );
322#else
323 r2.x -= 2;
324 wxRendererNative::Get().DrawPushButton( this, dc, r2, m_stateMenu );
325#endif
326
327 wxRendererNative::Get().DrawDropArrow( this, dc, r2, m_stateMenu );
328}
329
330
331bool SPLIT_BUTTON::Enable( bool aEnable )
332{
333 m_bIsEnable = aEnable;
334 wxPanel::Enable( m_bIsEnable );
335
336 if( m_bIsEnable
337 && ( m_stateButton == wxCONTROL_DISABLED || m_stateMenu == wxCONTROL_DISABLED ) )
338 {
339 m_stateButton = 0;
340 m_stateMenu = 0;
341 Refresh();
342 }
343
344 if( !m_bIsEnable
345 && ( m_stateButton != wxCONTROL_DISABLED || m_stateMenu != wxCONTROL_DISABLED ) )
346 {
347 m_stateButton = wxCONTROL_DISABLED;
348 m_stateMenu = wxCONTROL_DISABLED;
349 Refresh();
350 }
351
352 return aEnable;
353}
int m_arrowButtonWidth
Definition: split_button.h:63
void SetLabel(const wxString &aLabel) override
wxString m_label
Definition: split_button.h:66
void onThemeChanged(wxSysColourChangedEvent &aEvent)
bool m_bLButtonDown
Definition: split_button.h:65
void OnLeftButtonUp(wxMouseEvent &aEvent)
void OnKillFocus(wxFocusEvent &aEvent)
void OnMouseLeave(wxMouseEvent &aEvent)
wxBitmapBundle m_bitmap
Definition: split_button.h:68
void SetBitmap(const wxBitmapBundle &aBmp)
void SetWidthPadding(int aPadding)
wxMenu * GetSplitButtonMenu()
wxMenu * m_pMenu
Definition: split_button.h:67
void OnLeftButtonDown(wxMouseEvent &aEvent)
void SetMinSize(const wxSize &aSize) override
bool Enable(bool aEnable=true) override
wxSize m_unadjustedMinSize
Definition: split_button.h:69
void OnMouseEnter(wxMouseEvent &aEvent)
void OnPaint(wxPaintEvent &WXUNUSED(aEvent))
bool m_bIsEnable
Definition: split_button.h:62
SPLIT_BUTTON(wxWindow *aParent, wxWindowID aId, const wxString &aLabel, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize)
int m_widthPadding
Definition: split_button.h:64
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...