KiCad PCB EDA Suite
BITMAP_BUTTON Class Reference

A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn. More...

#include <bitmap_button.h>

Inheritance diagram for BITMAP_BUTTON:

Public Member Functions

 BITMAP_BUTTON (wxWindow *aParent, wxWindowID aId, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, int aStyles=wxBORDER_NONE|wxTAB_TRAVERSAL)
 
 BITMAP_BUTTON (wxWindow *aParent, wxWindowID aId, const wxBitmap &aDummyBitmap, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, int aStyles=wxBORDER_NONE|wxTAB_TRAVERSAL)
 
 ~BITMAP_BUTTON ()
 
void SetPadding (int aPadding)
 Set the amount of padding present on each side of the bitmap. More...
 
void SetBitmap (const wxBitmap &aBmp)
 Set the bitmap shown when the button is enabled. More...
 
void SetDisabledBitmap (const wxBitmap &aBmp)
 Set the bitmap shown when the button is disabled. More...
 
bool Enable (bool aEnable=true) override
 Enable the button. More...
 
void SetIsCheckButton ()
 Setup the control as a two-state button (checked or unchecked). More...
 
void SetIsRadioButton ()
 
void Check (bool aCheck=true)
 Check the control. More...
 
bool IsChecked () const
 
void SetIsSeparator ()
 Render button as a toolbar separator. More...
 
void AcceptDragInAsClick (bool aAcceptDragIn=true)
 Accept mouse-up as click even if mouse-down happened outside of the control. More...
 
void SetShowBadge (bool aShowBadge)
 
void SetBadgeText (const wxString &aText)
 
void SetBadgeColors (const wxColor &aBadgeColor, const wxColor &aBadgeTextColor)
 

Protected Member Functions

void setupEvents ()
 
void OnMouseLeave (wxEvent &aEvent)
 
void OnMouseEnter (wxEvent &aEvent)
 
void OnKillFocus (wxEvent &aEvent)
 
void OnSetFocus (wxEvent &aEvent)
 
void OnLeftButtonUp (wxMouseEvent &aEvent)
 
void OnLeftButtonDown (wxMouseEvent &aEvent)
 
void OnPaint (wxPaintEvent &aEvent)
 
void setFlag (int aFlag)
 
void clearFlag (int aFlag)
 
bool hasFlag (int aFlag) const
 

Private Attributes

wxBitmap m_normalBitmap
 
wxBitmap m_disabledBitmap
 
bool m_isRadioButton
 
bool m_showBadge
 
wxString m_badgeText
 
wxColor m_badgeColor
 
wxColor m_badgeTextColor
 
wxFont m_badgeFont
 
int m_buttonState
 
int m_padding
 
wxSize m_unadjustedMinSize
 Accept mouse-up as click even if mouse-down happened outside of the control. More...
 
bool m_acceptDraggedInClicks
 

Detailed Description

A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.

Specifically:

  • It has no border
  • It has a rectangle highlight when the mouse is hovering/pressed
  • It has the ability to be checked/toggled

Definition at line 40 of file bitmap_button.h.

Constructor & Destructor Documentation

◆ BITMAP_BUTTON() [1/2]

BITMAP_BUTTON::BITMAP_BUTTON ( wxWindow *  aParent,
wxWindowID  aId,
const wxPoint &  aPos = wxDefaultPosition,
const wxSize &  aSize = wxDefaultSize,
int  aStyles = wxBORDER_NONE | wxTAB_TRAVERSAL 
)

Definition at line 35 of file bitmap_button.cpp.

36 :
37 wxPanel( aParent, aId, aPos, aSize, aStyles ),
38 m_isRadioButton( false ),
39 m_showBadge( false ),
40 m_badgeColor( wxColor( 210, 0, 0, 0 ) ), // dark red
41 m_badgeTextColor( wxColor( wxT( "white" ) ) ),
42 m_buttonState( 0 ),
43 m_padding( 0 ),
45{
46 if( aSize == wxDefaultSize )
47 SetMinSize( wxButton::GetDefaultSize() + wxSize( m_padding * 2, m_padding * 2) );
48
49 m_badgeFont = GetFont().Smaller().MakeBold();
50
52}
wxFont m_badgeFont
wxColor m_badgeTextColor
bool m_acceptDraggedInClicks
wxColor m_badgeColor

References m_badgeFont, m_padding, and setupEvents().

◆ BITMAP_BUTTON() [2/2]

BITMAP_BUTTON::BITMAP_BUTTON ( wxWindow *  aParent,
wxWindowID  aId,
const wxBitmap &  aDummyBitmap,
const wxPoint &  aPos = wxDefaultPosition,
const wxSize &  aSize = wxDefaultSize,
int  aStyles = wxBORDER_NONE | wxTAB_TRAVERSAL 
)

Definition at line 55 of file bitmap_button.cpp.

56 :
57 wxPanel( aParent, aId, aPos, aSize, aStyles ),
58 m_isRadioButton( false ),
59 m_showBadge( false ),
60 m_buttonState( 0 ),
61 m_padding( 5 ),
63{
64 if( aSize == wxDefaultSize )
65 SetMinSize( wxButton::GetDefaultSize() + wxSize( m_padding * 2, m_padding * 2) );
66
68}

References m_padding, and setupEvents().

◆ ~BITMAP_BUTTON()

BITMAP_BUTTON::~BITMAP_BUTTON ( )

Definition at line 83 of file bitmap_button.cpp.

84{
85}

Member Function Documentation

◆ AcceptDragInAsClick()

void BITMAP_BUTTON::AcceptDragInAsClick ( bool  aAcceptDragIn = true)

Accept mouse-up as click even if mouse-down happened outside of the control.

Parameters
aAcceptDragInis true to allow drag in, false to ignore lone mouse-up events

Definition at line 110 of file bitmap_button.cpp.

111{
112 m_acceptDraggedInClicks = aAcceptDragIn;
113}

References m_acceptDraggedInClicks.

Referenced by ACTION_TOOLBAR_PALETTE::AddAction().

◆ Check()

void BITMAP_BUTTON::Check ( bool  aCheck = true)

Check the control.

This is the equivalent to toggling a toolbar button.

Definition at line 322 of file bitmap_button.cpp.

323{
324 wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), "Button is not a checkButton." );
325
326 if( aCheck )
327 setFlag( wxCONTROL_CHECKED );
328 else
329 clearFlag( wxCONTROL_CHECKED );
330
331 Refresh();
332}
void setFlag(int aFlag)
bool hasFlag(int aFlag) const
void clearFlag(int aFlag)
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...

References clearFlag(), hasFlag(), Refresh(), and setFlag().

Referenced by PROPERTIES_FRAME::CopyPrmsFromItemToPanel(), DIALOG_DIMENSION_PROPERTIES::onFontSelected(), DIALOG_TEXT_PROPERTIES::onFontSelected(), DIALOG_TEXTBOX_PROPERTIES::onFontSelected(), DIALOG_DIMENSION_PROPERTIES::onThickness(), DIALOG_TEXT_PROPERTIES::onThickness(), DIALOG_TEXTBOX_PROPERTIES::onThickness(), DIALOG_FIELD_PROPERTIES::TransferDataToWindow(), DIALOG_LABEL_PROPERTIES::TransferDataToWindow(), DIALOG_LIB_TEXT_PROPERTIES::TransferDataToWindow(), DIALOG_LIB_TEXTBOX_PROPERTIES::TransferDataToWindow(), DIALOG_SHEET_PIN_PROPERTIES::TransferDataToWindow(), DIALOG_TEXT_PROPERTIES::TransferDataToWindow(), DIALOG_DIMENSION_PROPERTIES::TransferDataToWindow(), and DIALOG_TEXTBOX_PROPERTIES::TransferDataToWindow().

◆ clearFlag()

void BITMAP_BUTTON::clearFlag ( int  aFlag)
inlineprotected

Definition at line 134 of file bitmap_button.h.

135 {
136 m_buttonState &= ~aFlag;
137 }

References m_buttonState.

Referenced by Check(), Enable(), OnKillFocus(), OnLeftButtonDown(), OnLeftButtonUp(), and OnMouseLeave().

◆ Enable()

◆ hasFlag()

bool BITMAP_BUTTON::hasFlag ( int  aFlag) const
inlineprotected

Definition at line 139 of file bitmap_button.h.

140 {
141 return m_buttonState & aFlag;
142 }

References m_buttonState.

Referenced by Check(), Enable(), IsChecked(), OnLeftButtonDown(), OnLeftButtonUp(), OnPaint(), and OnSetFocus().

◆ IsChecked()

◆ OnKillFocus()

void BITMAP_BUTTON::OnKillFocus ( wxEvent &  aEvent)
protected

Definition at line 132 of file bitmap_button.cpp.

133{
134 clearFlag( wxCONTROL_FOCUSED );
135 Refresh();
136 aEvent.Skip();
137}

References clearFlag(), and Refresh().

Referenced by setupEvents().

◆ OnLeftButtonDown()

void BITMAP_BUTTON::OnLeftButtonDown ( wxMouseEvent &  aEvent)
protected

Definition at line 172 of file bitmap_button.cpp.

173{
174 if( hasFlag( wxCONTROL_CHECKABLE ) )
175 {
176 if( hasFlag( wxCONTROL_CHECKED ) && !m_isRadioButton )
177 {
178 clearFlag( wxCONTROL_CHECKED );
179
180 GetEventHandler()->CallAfter(
181 [=]()
182 {
183 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
184 evt.SetEventObject( this );
185 evt.SetInt( 0 );
186 GetEventHandler()->ProcessEvent( evt );
187 } );
188 }
189 else
190 {
191 setFlag( wxCONTROL_CHECKED );
192
193 GetEventHandler()->CallAfter(
194 [=]()
195 {
196 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
197 evt.SetEventObject( this );
198 evt.SetInt( 1 );
199 GetEventHandler()->ProcessEvent( evt );
200 } );
201 }
202 }
203 else
204 {
205 setFlag( wxCONTROL_PRESSED );
206 }
207
208 Refresh();
209
210 aEvent.Skip();
211}

References clearFlag(), hasFlag(), m_isRadioButton, Refresh(), and setFlag().

Referenced by setupEvents().

◆ OnLeftButtonUp()

void BITMAP_BUTTON::OnLeftButtonUp ( wxMouseEvent &  aEvent)
protected

Definition at line 150 of file bitmap_button.cpp.

151{
152 // Only create a button event when the control is enabled
153 // and only accept clicks that came without prior mouse-down if configured
154 if( !hasFlag( wxCONTROL_DISABLED )
155 && ( m_acceptDraggedInClicks || hasFlag( wxCONTROL_PRESSED | wxCONTROL_FOCUSED ) ) )
156 {
157 GetEventHandler()->CallAfter( [=]()
158 {
159 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
160 evt.SetEventObject( this );
161 GetEventHandler()->ProcessEvent( evt );
162 } );
163 }
164
165 clearFlag( wxCONTROL_PRESSED );
166 Refresh();
167
168 aEvent.Skip();
169}

References clearFlag(), hasFlag(), m_acceptDraggedInClicks, and Refresh().

Referenced by setupEvents().

◆ OnMouseEnter()

void BITMAP_BUTTON::OnMouseEnter ( wxEvent &  aEvent)
protected

Definition at line 124 of file bitmap_button.cpp.

125{
126 setFlag( wxCONTROL_CURRENT );
127 Refresh();
128 aEvent.Skip();
129}

References Refresh(), and setFlag().

Referenced by setupEvents().

◆ OnMouseLeave()

void BITMAP_BUTTON::OnMouseLeave ( wxEvent &  aEvent)
protected

Definition at line 116 of file bitmap_button.cpp.

117{
118 clearFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED );
119 Refresh();
120 aEvent.Skip();
121}

References clearFlag(), and Refresh().

Referenced by setupEvents().

◆ OnPaint()

void BITMAP_BUTTON::OnPaint ( wxPaintEvent &  aEvent)
protected

Definition at line 214 of file bitmap_button.cpp.

215{
216 bool darkMode = KIPLATFORM::UI::IsDarkTheme();
217 wxColor highlightColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
218
219 // The drawing rectangle
220 wxRect rect( wxPoint( 0, 0 ), GetSize() );
221 wxPaintDC dc( this );
222
224 {
225 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) ) );
226 dc.DrawLine( wxPoint( GetSize().x / 2, 0 ), wxPoint( GetSize().x / 2, GetSize().y ) );
227 return;
228 }
229
230 // This drawing is done so the button looks the same as an AUI toolbar button
231 if( !hasFlag( wxCONTROL_DISABLED ) )
232 {
233 if( hasFlag( wxCONTROL_PRESSED ) )
234 {
235 dc.SetPen( wxPen( highlightColor ) );
236 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 20 : 150 ) ) );
237 dc.DrawRectangle( rect );
238 }
239 else if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_FOCUSED ) )
240 {
241 dc.SetPen( wxPen( highlightColor ) );
242 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
243
244 // Checked items need a lighter hover rectangle
245 if( hasFlag( wxCONTROL_CHECKED ) )
246 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 50 : 180 ) ) );
247
248 dc.DrawRectangle( rect );
249 }
250 else if( hasFlag( wxCONTROL_CHECKED ) )
251 {
252 dc.SetPen( wxPen( highlightColor ) );
253 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
254 dc.DrawRectangle( rect );
255 }
256 }
257
258 const wxBitmap& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap;
259
260 // Draw the bitmap with the upper-left corner offset by the padding
261 if( bmp.IsOk() )
262 dc.DrawBitmap( bmp, m_padding, m_padding, true );
263
264 // Draw the badge
265 if( m_showBadge )
266 {
267 dc.SetFont( m_badgeFont );
268
269 wxSize box_size = dc.GetTextExtent( m_badgeText ) + wxSize( 6, 2 );
270 wxSize box_offset = box_size + wxSize( m_padding - 2, m_padding );
271 wxSize text_offset = box_offset - wxSize( 3, 1 );
272
273 dc.SetPen( wxPen( m_badgeColor ) );
274 dc.SetBrush( wxBrush( m_badgeColor ) );
275 dc.DrawRoundedRectangle( rect.GetRightBottom() - box_offset, box_size, -0.25 );
276
277 dc.SetTextForeground( m_badgeTextColor );
278 dc.DrawText( m_badgeText, rect.GetRightBottom() - text_offset );
279 }
280}
#define wxCONTROL_SEPARATOR
wxString m_badgeText
wxBitmap m_disabledBitmap
wxBitmap m_normalBitmap
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:31

References hasFlag(), KIPLATFORM::UI::IsDarkTheme(), m_badgeColor, m_badgeFont, m_badgeText, m_badgeTextColor, m_disabledBitmap, m_normalBitmap, m_padding, m_showBadge, and wxCONTROL_SEPARATOR.

Referenced by setupEvents().

◆ OnSetFocus()

void BITMAP_BUTTON::OnSetFocus ( wxEvent &  aEvent)
protected

Definition at line 140 of file bitmap_button.cpp.

141{
142 if( !hasFlag( wxCONTROL_CHECKABLE ) )
143 setFlag( wxCONTROL_FOCUSED );
144
145 Refresh();
146 aEvent.Skip();
147}

References hasFlag(), Refresh(), and setFlag().

Referenced by setupEvents().

◆ SetBadgeColors()

void BITMAP_BUTTON::SetBadgeColors ( const wxColor &  aBadgeColor,
const wxColor &  aBadgeTextColor 
)
inline

Definition at line 112 of file bitmap_button.h.

113 {
114 m_badgeColor = aBadgeColor;
115 m_badgeTextColor = aBadgeTextColor;
116 }

References m_badgeColor, and m_badgeTextColor.

◆ SetBadgeText()

void BITMAP_BUTTON::SetBadgeText ( const wxString &  aText)
inline

Definition at line 110 of file bitmap_button.h.

110{ m_badgeText = aText; }

References m_badgeText.

Referenced by KICAD_MANAGER_FRAME::updatePcmButtonBadge().

◆ SetBitmap()

◆ SetDisabledBitmap()

void BITMAP_BUTTON::SetDisabledBitmap ( const wxBitmap &  aBmp)

Set the bitmap shown when the button is disabled.

Parameters
aBmpis the disabled bitmap.

Definition at line 104 of file bitmap_button.cpp.

105{
106 m_disabledBitmap = aBmp;
107}

References m_disabledBitmap.

Referenced by ACTION_TOOLBAR_PALETTE::AddAction(), and PANEL_KICAD_LAUNCHER::CreateLaunchers().

◆ setFlag()

void BITMAP_BUTTON::setFlag ( int  aFlag)
inlineprotected

Definition at line 129 of file bitmap_button.h.

130 {
131 m_buttonState |= aFlag;
132 }

References m_buttonState.

Referenced by Check(), Enable(), OnLeftButtonDown(), OnMouseEnter(), OnSetFocus(), SetIsCheckButton(), SetIsRadioButton(), and SetIsSeparator().

◆ SetIsCheckButton()

◆ SetIsRadioButton()

◆ SetIsSeparator()

◆ SetPadding()

void BITMAP_BUTTON::SetPadding ( int  aPadding)

Set the amount of padding present on each side of the bitmap.

Parameters
aPaddingis the amount in px of padding for each side.

Definition at line 88 of file bitmap_button.cpp.

89{
90 m_padding = aPadding;
91 SetMinSize( m_unadjustedMinSize + wxSize( aPadding * 2, aPadding * 2 ) );
92}

References m_padding, and m_unadjustedMinSize.

Referenced by ACTION_TOOLBAR_PALETTE::AddAction(), APPEARANCE_CONTROLS::APPEARANCE_CONTROLS(), and PANEL_KICAD_LAUNCHER::CreateLaunchers().

◆ SetShowBadge()

void BITMAP_BUTTON::SetShowBadge ( bool  aShowBadge)
inline

Definition at line 108 of file bitmap_button.h.

108{ m_showBadge = aShowBadge; }

References m_showBadge.

Referenced by KICAD_MANAGER_FRAME::updatePcmButtonBadge().

◆ setupEvents()

void BITMAP_BUTTON::setupEvents ( )
protected

Definition at line 71 of file bitmap_button.cpp.

72{
73 Bind( wxEVT_PAINT, &BITMAP_BUTTON::OnPaint, this );
74 Bind( wxEVT_LEFT_UP, &BITMAP_BUTTON::OnLeftButtonUp, this );
75 Bind( wxEVT_LEFT_DOWN, &BITMAP_BUTTON::OnLeftButtonDown, this );
76 Bind( wxEVT_LEAVE_WINDOW, &BITMAP_BUTTON::OnMouseLeave, this );
77 Bind( wxEVT_ENTER_WINDOW, &BITMAP_BUTTON::OnMouseEnter, this );
78 Bind( wxEVT_KILL_FOCUS, &BITMAP_BUTTON::OnKillFocus, this );
79 Bind( wxEVT_SET_FOCUS, &BITMAP_BUTTON::OnSetFocus, this );
80}
void OnKillFocus(wxEvent &aEvent)
void OnSetFocus(wxEvent &aEvent)
void OnMouseLeave(wxEvent &aEvent)
void OnLeftButtonDown(wxMouseEvent &aEvent)
void OnPaint(wxPaintEvent &aEvent)
void OnMouseEnter(wxEvent &aEvent)
void OnLeftButtonUp(wxMouseEvent &aEvent)

References OnKillFocus(), OnLeftButtonDown(), OnLeftButtonUp(), OnMouseEnter(), OnMouseLeave(), OnPaint(), and OnSetFocus().

Referenced by BITMAP_BUTTON().

Member Data Documentation

◆ m_acceptDraggedInClicks

bool BITMAP_BUTTON::m_acceptDraggedInClicks
private

Definition at line 159 of file bitmap_button.h.

Referenced by AcceptDragInAsClick(), and OnLeftButtonUp().

◆ m_badgeColor

wxColor BITMAP_BUTTON::m_badgeColor
private

Definition at line 151 of file bitmap_button.h.

Referenced by OnPaint(), and SetBadgeColors().

◆ m_badgeFont

wxFont BITMAP_BUTTON::m_badgeFont
private

Definition at line 153 of file bitmap_button.h.

Referenced by BITMAP_BUTTON(), and OnPaint().

◆ m_badgeText

wxString BITMAP_BUTTON::m_badgeText
private

Definition at line 150 of file bitmap_button.h.

Referenced by OnPaint(), and SetBadgeText().

◆ m_badgeTextColor

wxColor BITMAP_BUTTON::m_badgeTextColor
private

Definition at line 152 of file bitmap_button.h.

Referenced by OnPaint(), and SetBadgeColors().

◆ m_buttonState

int BITMAP_BUTTON::m_buttonState
private

Definition at line 154 of file bitmap_button.h.

Referenced by clearFlag(), hasFlag(), and setFlag().

◆ m_disabledBitmap

wxBitmap BITMAP_BUTTON::m_disabledBitmap
private

Definition at line 146 of file bitmap_button.h.

Referenced by OnPaint(), and SetDisabledBitmap().

◆ m_isRadioButton

bool BITMAP_BUTTON::m_isRadioButton
private

Definition at line 148 of file bitmap_button.h.

Referenced by OnLeftButtonDown(), and SetIsRadioButton().

◆ m_normalBitmap

wxBitmap BITMAP_BUTTON::m_normalBitmap
private

Definition at line 145 of file bitmap_button.h.

Referenced by OnPaint(), and SetBitmap().

◆ m_padding

int BITMAP_BUTTON::m_padding
private

Definition at line 155 of file bitmap_button.h.

Referenced by BITMAP_BUTTON(), OnPaint(), SetBitmap(), SetIsSeparator(), and SetPadding().

◆ m_showBadge

bool BITMAP_BUTTON::m_showBadge
private

Definition at line 149 of file bitmap_button.h.

Referenced by OnPaint(), and SetShowBadge().

◆ m_unadjustedMinSize

wxSize BITMAP_BUTTON::m_unadjustedMinSize
private

Accept mouse-up as click even if mouse-down happened outside of the control.

Definition at line 156 of file bitmap_button.h.

Referenced by SetBitmap(), and SetPadding().


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