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 344 of file bitmap_button.cpp.

345{
346 wxASSERT_MSG( hasFlag( wxCONTROL_CHECKABLE ), wxS( "Button is not a checkButton." ) );
347
348 if( aCheck && !hasFlag( wxCONTROL_CHECKED ) )
349 {
350 setFlag( wxCONTROL_CHECKED );
351 Refresh();
352 }
353
354 if( !aCheck && hasFlag( wxCONTROL_CHECKED ) )
355 {
356 clearFlag( wxCONTROL_CHECKED );
357 Refresh();
358 }
359}
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()

bool BITMAP_BUTTON::Enable ( bool  aEnable = true)
override

◆ 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(), OnKillFocus(), OnLeftButtonDown(), OnLeftButtonUp(), OnMouseEnter(), OnMouseLeave(), OnPaint(), and OnSetFocus().

◆ IsChecked()

◆ OnKillFocus()

void BITMAP_BUTTON::OnKillFocus ( wxEvent &  aEvent)
protected

Definition at line 140 of file bitmap_button.cpp.

141{
142 if( hasFlag( wxCONTROL_FOCUSED | wxCONTROL_CURRENT | wxCONTROL_PRESSED ) )
143 {
144 clearFlag( wxCONTROL_FOCUSED | wxCONTROL_CURRENT | wxCONTROL_PRESSED );
145 Refresh();
146 }
147
148 aEvent.Skip();
149}

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

Referenced by setupEvents().

◆ OnLeftButtonDown()

void BITMAP_BUTTON::OnLeftButtonDown ( wxMouseEvent &  aEvent)
protected

Definition at line 189 of file bitmap_button.cpp.

190{
191 if( hasFlag( wxCONTROL_CHECKABLE ) )
192 {
193 if( hasFlag( wxCONTROL_CHECKED ) && !m_isRadioButton )
194 {
195 clearFlag( wxCONTROL_CHECKED );
196
197 GetEventHandler()->CallAfter(
198 [=]()
199 {
200 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
201 evt.SetEventObject( this );
202 evt.SetInt( 0 );
203 GetEventHandler()->ProcessEvent( evt );
204 } );
205 }
206 else
207 {
208 setFlag( wxCONTROL_CHECKED );
209
210 GetEventHandler()->CallAfter(
211 [=]()
212 {
213 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
214 evt.SetEventObject( this );
215 evt.SetInt( 1 );
216 GetEventHandler()->ProcessEvent( evt );
217 } );
218 }
219 }
220 else
221 {
222 setFlag( wxCONTROL_PRESSED );
223 }
224
225 Refresh();
226
227 aEvent.Skip();
228}

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

Referenced by setupEvents().

◆ OnLeftButtonUp()

void BITMAP_BUTTON::OnLeftButtonUp ( wxMouseEvent &  aEvent)
protected

Definition at line 167 of file bitmap_button.cpp.

168{
169 // Only create a button event when the control is enabled
170 // and only accept clicks that came without prior mouse-down if configured
171 if( !hasFlag( wxCONTROL_DISABLED )
172 && ( m_acceptDraggedInClicks || hasFlag( wxCONTROL_PRESSED | wxCONTROL_FOCUSED ) ) )
173 {
174 GetEventHandler()->CallAfter( [=]()
175 {
176 wxCommandEvent evt( wxEVT_BUTTON, GetId() );
177 evt.SetEventObject( this );
178 GetEventHandler()->ProcessEvent( evt );
179 } );
180 }
181
182 clearFlag( wxCONTROL_PRESSED );
183 Refresh();
184
185 aEvent.Skip();
186}

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

Referenced by setupEvents().

◆ OnMouseEnter()

void BITMAP_BUTTON::OnMouseEnter ( wxEvent &  aEvent)
protected

Definition at line 128 of file bitmap_button.cpp.

129{
130 if( !hasFlag( wxCONTROL_CURRENT ) )
131 {
132 setFlag( wxCONTROL_CURRENT );
133 Refresh();
134 }
135
136 aEvent.Skip();
137}

References hasFlag(), 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 if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED ) )
119 {
120 clearFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED );
121 Refresh();
122 }
123
124 aEvent.Skip();
125}

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

Referenced by setupEvents().

◆ OnPaint()

void BITMAP_BUTTON::OnPaint ( wxPaintEvent &  aEvent)
protected

Definition at line 231 of file bitmap_button.cpp.

232{
233 bool darkMode = KIPLATFORM::UI::IsDarkTheme();
234 wxColor highlightColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
235
236 // The drawing rectangle
237 wxRect rect( wxPoint( 0, 0 ), GetSize() );
238 wxPaintDC dc( this );
239
241 {
242 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) ) );
243 dc.DrawLine( wxPoint( GetSize().x / 2, 0 ), wxPoint( GetSize().x / 2, GetSize().y ) );
244 return;
245 }
246
247 // This drawing is done so the button looks the same as an AUI toolbar button
248 if( !hasFlag( wxCONTROL_DISABLED ) )
249 {
250 if( hasFlag( wxCONTROL_PRESSED ) )
251 {
252 dc.SetPen( wxPen( highlightColor ) );
253 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 20 : 150 ) ) );
254 dc.DrawRectangle( rect );
255 }
256 else if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_FOCUSED ) )
257 {
258 dc.SetPen( wxPen( highlightColor ) );
259 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
260
261 // Checked items need a lighter hover rectangle
262 if( hasFlag( wxCONTROL_CHECKED ) )
263 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 50 : 180 ) ) );
264
265 dc.DrawRectangle( rect );
266 }
267 else if( hasFlag( wxCONTROL_CHECKED ) )
268 {
269 dc.SetPen( wxPen( highlightColor ) );
270 dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
271 dc.DrawRectangle( rect );
272 }
273 }
274
275 const wxBitmap& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap;
276
277 // Draw the bitmap with the upper-left corner offset by the padding
278 if( bmp.IsOk() )
279 dc.DrawBitmap( bmp, m_padding, m_padding, true );
280
281 // Draw the badge
282 if( m_showBadge )
283 {
284 dc.SetFont( m_badgeFont );
285
286 wxSize box_size = dc.GetTextExtent( m_badgeText ) + wxSize( 6, 2 );
287 wxSize box_offset = box_size + wxSize( m_padding - 2, m_padding );
288 wxSize text_offset = box_offset - wxSize( 3, 1 );
289
290 dc.SetPen( wxPen( m_badgeColor ) );
291 dc.SetBrush( wxBrush( m_badgeColor ) );
292 dc.DrawRoundedRectangle( rect.GetRightBottom() - box_offset, box_size, -0.25 );
293
294 dc.SetTextForeground( m_badgeTextColor );
295 dc.DrawText( m_badgeText, rect.GetRightBottom() - text_offset );
296 }
297}
#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 152 of file bitmap_button.cpp.

153{
154 if( !hasFlag( wxCONTROL_CHECKABLE ) )
155 {
156 if( !hasFlag( wxCONTROL_FOCUSED ) )
157 {
158 setFlag( wxCONTROL_FOCUSED );
159 Refresh();
160 }
161 }
162
163 aEvent.Skip();
164}

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: