KiCad PCB EDA Suite
SPLIT_BUTTON Class Reference

#include <split_button.h>

Inheritance diagram for SPLIT_BUTTON:

Public Member Functions

 SPLIT_BUTTON (wxWindow *aParent, wxWindowID aId, const wxString &aLabel, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize)
 
 ~SPLIT_BUTTON ()
 
wxMenu * GetSplitButtonMenu ()
 
void SetBitmap (const wxBitmap &aBmp)
 
void SetMinSize (const wxSize &aSize) override
 
void SetWidthPadding (int aPadding)
 
void SetLabel (const wxString &aLabel) override
 

Protected Member Functions

void OnKillFocus (wxFocusEvent &aEvent)
 
void OnMouseLeave (wxMouseEvent &aEvent)
 
void OnMouseEnter (wxMouseEvent &aEvent)
 
void OnLeftButtonUp (wxMouseEvent &aEvent)
 
void OnLeftButtonDown (wxMouseEvent &aEvent)
 
void OnPaint (wxPaintEvent &WXUNUSED(aEvent))
 
void onThemeChanged (wxSysColourChangedEvent &aEvent)
 
bool Enable (bool aEnable=true) override
 

Private Attributes

int m_stateButton = 0
 
int m_stateMenu = 0
 
bool m_bIsEnable = true
 
const int m_arrowButtonWidth = 20
 
int m_widthPadding = 20
 
bool m_bLButtonDown = false
 
wxString m_label
 
wxMenu * m_pMenu = nullptr
 
wxBitmap m_bitmap
 
wxSize m_unadjustedMinSize
 

Detailed Description

Definition at line 34 of file split_button.h.

Constructor & Destructor Documentation

◆ SPLIT_BUTTON()

SPLIT_BUTTON::SPLIT_BUTTON ( wxWindow *  aParent,
wxWindowID  aId,
const wxString &  aLabel,
const wxPoint &  aPos = wxDefaultPosition,
const wxSize &  aSize = wxDefaultSize 
)

Definition at line 36 of file split_button.cpp.

37 :
38 wxPanel( aParent, aId, aPos, aSize, wxBORDER_NONE | wxTAB_TRAVERSAL, wxS( "DropDownButton" ) ),
39 m_label( aLabel )
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 wxSize textSize = GetTextExtent( m_label );
50 SetMinSize( wxSize( std::max( textSize.GetWidth(), defaultSize.GetWidth() + 1 ),
51 defaultSize.GetHeight() + 1 ) );
52 }
53
54 Bind( wxEVT_PAINT, &SPLIT_BUTTON::OnPaint, this );
55 Bind( wxEVT_LEFT_UP, &SPLIT_BUTTON::OnLeftButtonUp, this );
56 Bind( wxEVT_LEFT_DOWN, &SPLIT_BUTTON::OnLeftButtonDown, this );
57 Bind( wxEVT_KILL_FOCUS, &SPLIT_BUTTON::OnKillFocus, this );
58 Bind( wxEVT_LEAVE_WINDOW, &SPLIT_BUTTON::OnMouseLeave, this );
59 Bind( wxEVT_ENTER_WINDOW, &SPLIT_BUTTON::OnMouseEnter, this );
60
61 Bind( wxEVT_SYS_COLOUR_CHANGED, wxSysColourChangedEventHandler( SPLIT_BUTTON::onThemeChanged ),
62 this );
63
64 m_pMenu = new wxMenu();
65}
wxString m_label
Definition: split_button.h:66
void onThemeChanged(wxSysColourChangedEvent &aEvent)
void OnLeftButtonUp(wxMouseEvent &aEvent)
void OnKillFocus(wxFocusEvent &aEvent)
void OnMouseLeave(wxMouseEvent &aEvent)
wxMenu * m_pMenu
Definition: split_button.h:67
void OnLeftButtonDown(wxMouseEvent &aEvent)
void SetMinSize(const wxSize &aSize) override
void OnMouseEnter(wxMouseEvent &aEvent)
void OnPaint(wxPaintEvent &WXUNUSED(aEvent))
static const wxSize defaultSize(FRAME_T aFrameType)

References defaultSize(), m_label, m_pMenu, OnKillFocus(), OnLeftButtonDown(), OnLeftButtonUp(), OnMouseEnter(), OnMouseLeave(), OnPaint(), onThemeChanged(), and SetMinSize().

◆ ~SPLIT_BUTTON()

SPLIT_BUTTON::~SPLIT_BUTTON ( )

Definition at line 68 of file split_button.cpp.

69{
70 delete m_pMenu;
71 m_pMenu = nullptr;
72}

References m_pMenu.

Member Function Documentation

◆ Enable()

bool SPLIT_BUTTON::Enable ( bool  aEnable = true)
overrideprotected

Definition at line 323 of file split_button.cpp.

324{
325 m_bIsEnable = aEnable;
326 wxPanel::Enable( m_bIsEnable );
327
328 if( m_bIsEnable
329 && ( m_stateButton == wxCONTROL_DISABLED || m_stateMenu == wxCONTROL_DISABLED ) )
330 {
331 m_stateButton = 0;
332 m_stateMenu = 0;
333 Refresh();
334 }
335
336 if( !m_bIsEnable
337 && ( m_stateButton != wxCONTROL_DISABLED || m_stateMenu != wxCONTROL_DISABLED ) )
338 {
339 m_stateButton = wxCONTROL_DISABLED;
340 m_stateMenu = wxCONTROL_DISABLED;
341 Refresh();
342 }
343
344 return aEnable;
345}
bool m_bIsEnable
Definition: split_button.h:62
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...

References m_bIsEnable, m_stateButton, m_stateMenu, and Refresh().

◆ GetSplitButtonMenu()

wxMenu * SPLIT_BUTTON::GetSplitButtonMenu ( )

◆ OnKillFocus()

void SPLIT_BUTTON::OnKillFocus ( wxFocusEvent &  aEvent)
protected

Definition at line 120 of file split_button.cpp.

121{
122 if( m_stateButton != 0 || m_stateMenu != 0 )
123 {
124 m_stateButton = 0;
125 m_stateMenu = 0;
126 Refresh();
127 }
128
129 aEvent.Skip();
130}

References m_stateButton, m_stateMenu, and Refresh().

Referenced by SPLIT_BUTTON().

◆ OnLeftButtonDown()

void SPLIT_BUTTON::OnLeftButtonDown ( wxMouseEvent &  aEvent)
protected

Definition at line 190 of file split_button.cpp.

191{
192 m_bLButtonDown = true;
193
194 int x = -1;
195 int y = -1;
196 aEvent.GetPosition( &x, &y );
197
198 if( x >= ( GetSize().GetWidth() - m_arrowButtonWidth ) )
199 {
200 m_stateButton = 0;
201 m_stateMenu = wxCONTROL_PRESSED;
202 Refresh();
203
204 wxSize size = GetSize();
205 wxPoint position;
206 position.x = 0;
207 position.y = size.GetHeight();
208 PopupMenu( m_pMenu, position );
209
210 m_stateMenu = 0;
211 Refresh();
212 }
213 else
214 {
215 m_stateButton = wxCONTROL_PRESSED;
216 m_stateMenu = wxCONTROL_PRESSED;
217 Refresh();
218 }
219
220 aEvent.Skip();
221}
const int m_arrowButtonWidth
Definition: split_button.h:63
bool m_bLButtonDown
Definition: split_button.h:65

References m_arrowButtonWidth, m_bLButtonDown, m_pMenu, m_stateButton, m_stateMenu, and Refresh().

Referenced by SPLIT_BUTTON().

◆ OnLeftButtonUp()

void SPLIT_BUTTON::OnLeftButtonUp ( wxMouseEvent &  aEvent)
protected

Definition at line 159 of file split_button.cpp.

160{
161 m_stateButton = 0;
162 m_stateMenu = 0;
163
164 Refresh();
165
166 int x = -1;
167 int y = -1;
168 aEvent.GetPosition( &x, &y );
169
170 if( x < ( GetSize().GetWidth() - m_arrowButtonWidth ) )
171 {
172 wxEvtHandler* pEventHandler = GetEventHandler();
173 wxASSERT( pEventHandler );
174
175 pEventHandler->CallAfter(
176 [=]()
177 {
178 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
179 evt.SetEventObject( this );
180 GetEventHandler()->ProcessEvent( evt );
181 } );
182 }
183
184 m_bLButtonDown = false;
185
186 aEvent.Skip();
187}

References m_arrowButtonWidth, m_bLButtonDown, m_stateButton, m_stateMenu, and Refresh().

Referenced by SPLIT_BUTTON().

◆ OnMouseEnter()

void SPLIT_BUTTON::OnMouseEnter ( wxMouseEvent &  aEvent)
protected

Definition at line 146 of file split_button.cpp.

147{
148 if( m_stateButton != wxCONTROL_CURRENT || m_stateMenu != wxCONTROL_CURRENT )
149 {
150 m_stateButton = wxCONTROL_CURRENT;
151 m_stateMenu = wxCONTROL_CURRENT;
152 Refresh();
153 }
154
155 aEvent.Skip();
156}

References m_stateButton, m_stateMenu, and Refresh().

Referenced by SPLIT_BUTTON().

◆ OnMouseLeave()

void SPLIT_BUTTON::OnMouseLeave ( wxMouseEvent &  aEvent)
protected

Definition at line 133 of file split_button.cpp.

134{
135 if( m_stateButton != 0 || m_stateMenu != 0 )
136 {
137 m_stateButton = 0;
138 m_stateMenu = 0;
139 Refresh();
140 }
141
142 aEvent.Skip();
143}

References m_stateButton, m_stateMenu, and Refresh().

Referenced by SPLIT_BUTTON().

◆ OnPaint()

void SPLIT_BUTTON::OnPaint ( wxPaintEvent &  WXUNUSEDaEvent)
protected

Definition at line 224 of file split_button.cpp.

225{
226 wxPaintDC dc( this );
227 wxSize size = GetSize();
228 const int width = size.GetWidth() - m_arrowButtonWidth;
229
230#ifdef __WXMAC__
231 auto drawBackground =
232 [&]( wxRect aRect )
233 {
234 // wxWidgets doesn't have much support for dark mode on OSX; none of the
235 // system colours return the right values, nor does wxRendererNative draw
236 // the borders correctly. So we add some empirically chosen hacks here.
237
238 // NOTE: KEEP THESE HACKS IN SYNC WITH STD_BITMAP_BUTTON
239
240 wxColor fg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT );
241 wxColor bg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
242
243 aRect.width += 1;
244 aRect.height += 1;
245
247 {
248 bg = bg.ChangeLightness( m_bIsEnable ? 130 : 120 );
249 dc.SetBrush( bg );
250 dc.SetPen( bg );
251 }
252 else
253 {
254 bg = bg.ChangeLightness( m_bIsEnable ? 200 : 160 );
255 dc.SetBrush( bg );
256 fg = fg.ChangeLightness( 180 );
257 dc.SetPen( fg );
258 }
259
260 dc.DrawRoundedRectangle( aRect, aRect.height / 4.0 );
261 };
262#endif
263
264 // Draw first part of button
265 wxRect r1;
266 r1.x = 0;
267 r1.y = 0;
268 r1.width = width;
269 r1.height = size.GetHeight();
270
271#ifdef __WXMAC__
272 // wxRendereNative doesn't handle dark mode on OSX.
273 drawBackground( r1 );
274#else
275 #ifdef _WXMSW_
276 r1.width += 2;
277 #endif
278
279 wxRendererNative::Get().DrawPushButton( this, dc, r1, m_stateButton );
280#endif
281
282 SetForegroundColour( m_bIsEnable ? wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT )
283 : wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
284
285 if( m_bitmap.IsOk() )
286 {
287 wxMemoryDC mdc( m_bitmap );
288
289 r1.x = ( width - m_bitmap.GetWidth() ) / 2;
290
291 if( r1.x < 0 )
292 r1.x = 0;
293
294 r1.y += ( size.GetHeight() - m_bitmap.GetHeight() ) / 2;
295
296 dc.Blit( wxPoint( r1.x, r1.y ), m_bitmap.GetSize(), &mdc, wxPoint( 0, 0 ), wxCOPY, true );
297 }
298 else
299 {
300 r1.y += ( ( size.GetHeight() - GetCharHeight() ) / 2 ) - 1;
301 dc.DrawLabel( m_label, r1, wxALIGN_CENTER_HORIZONTAL );
302 }
303
304 // Draw second part of button
305 wxRect r2;
306 r2.x = width;
307 r2.y = 0;
308 r2.width = m_arrowButtonWidth;
309 r2.height = size.GetHeight();
310
311#ifdef __WXMAC__
312 // wxRendereNative doesn't handle dark mode on OSX.
313 drawBackground( r2 );
314#else
315 r2.x -= 2;
316 wxRendererNative::Get().DrawPushButton( this, dc, r2, m_stateMenu );
317#endif
318
319 wxRendererNative::Get().DrawDropArrow( this, dc, r2, m_stateMenu );
320}
wxBitmap m_bitmap
Definition: split_button.h:68
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:31

References KIPLATFORM::UI::IsDarkTheme(), m_arrowButtonWidth, m_bIsEnable, m_bitmap, m_label, m_stateButton, and m_stateMenu.

Referenced by SPLIT_BUTTON().

◆ onThemeChanged()

void SPLIT_BUTTON::onThemeChanged ( wxSysColourChangedEvent &  aEvent)
protected

Definition at line 75 of file split_button.cpp.

76{
77 Refresh();
78}

References Refresh().

Referenced by SPLIT_BUTTON().

◆ SetBitmap()

void SPLIT_BUTTON::SetBitmap ( const wxBitmap &  aBmp)

Definition at line 96 of file split_button.cpp.

97{
98 m_bitmap = aBmp;
99
100 SetMinSize( wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() ) );
101}

References m_bitmap, and SetMinSize().

Referenced by DIALOG_MANAGE_REPOSITORIES::DIALOG_MANAGE_REPOSITORIES(), and PANEL_FP_LIB_TABLE::PANEL_FP_LIB_TABLE().

◆ SetLabel()

void SPLIT_BUTTON::SetLabel ( const wxString &  aLabel)
override

Definition at line 104 of file split_button.cpp.

105{
106 if( m_label != aLabel )
107 {
108 m_label = aLabel;
109 Refresh();
110 }
111}

References m_label, and Refresh().

Referenced by PANEL_PACKAGE::PANEL_PACKAGE(), and PANEL_PACKAGE::SetState().

◆ SetMinSize()

void SPLIT_BUTTON::SetMinSize ( const wxSize &  aSize)
override

Definition at line 81 of file split_button.cpp.

82{
83 m_unadjustedMinSize = aSize;
84 wxPanel::SetMinSize( wxSize( aSize.GetWidth() + m_arrowButtonWidth + m_widthPadding,
85 aSize.GetHeight() ) );
86}
wxSize m_unadjustedMinSize
Definition: split_button.h:69
int m_widthPadding
Definition: split_button.h:64

References m_arrowButtonWidth, m_unadjustedMinSize, and m_widthPadding.

Referenced by DIALOG_MANAGE_REPOSITORIES::DIALOG_MANAGE_REPOSITORIES(), PANEL_FP_LIB_TABLE::PANEL_FP_LIB_TABLE(), SetBitmap(), SetWidthPadding(), and SPLIT_BUTTON().

◆ SetWidthPadding()

void SPLIT_BUTTON::SetWidthPadding ( int  aPadding)

Member Data Documentation

◆ m_arrowButtonWidth

const int SPLIT_BUTTON::m_arrowButtonWidth = 20
private

Definition at line 63 of file split_button.h.

Referenced by OnLeftButtonDown(), OnLeftButtonUp(), OnPaint(), and SetMinSize().

◆ m_bIsEnable

bool SPLIT_BUTTON::m_bIsEnable = true
private

Definition at line 62 of file split_button.h.

Referenced by Enable(), and OnPaint().

◆ m_bitmap

wxBitmap SPLIT_BUTTON::m_bitmap
private

Definition at line 68 of file split_button.h.

Referenced by OnPaint(), and SetBitmap().

◆ m_bLButtonDown

bool SPLIT_BUTTON::m_bLButtonDown = false
private

Definition at line 65 of file split_button.h.

Referenced by OnLeftButtonDown(), and OnLeftButtonUp().

◆ m_label

wxString SPLIT_BUTTON::m_label
private

Definition at line 66 of file split_button.h.

Referenced by OnPaint(), SetLabel(), and SPLIT_BUTTON().

◆ m_pMenu

wxMenu* SPLIT_BUTTON::m_pMenu = nullptr
private

Definition at line 67 of file split_button.h.

Referenced by GetSplitButtonMenu(), OnLeftButtonDown(), SPLIT_BUTTON(), and ~SPLIT_BUTTON().

◆ m_stateButton

int SPLIT_BUTTON::m_stateButton = 0
private

◆ m_stateMenu

int SPLIT_BUTTON::m_stateMenu = 0
private

◆ m_unadjustedMinSize

wxSize SPLIT_BUTTON::m_unadjustedMinSize
private

Definition at line 69 of file split_button.h.

Referenced by SetMinSize(), and SetWidthPadding().

◆ m_widthPadding

int SPLIT_BUTTON::m_widthPadding = 20
private

Definition at line 64 of file split_button.h.

Referenced by SetMinSize(), and SetWidthPadding().


The documentation for this class was generated from the following files: