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 ()
 
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 Check (bool aCheck=true)
 Check the control. More...
 
void AcceptDragInAsClick (bool aAcceptDragIn=true)
 Accept mouse-up as click even if mouse-down happened outside of the control. More...
 

Protected Member Functions

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)
 

Private Attributes

wxBitmap m_normalBitmap
 < Bitmap shown when button is enabled More...
 
wxBitmap m_disabledBitmap
 Current state of the button. More...
 
int m_buttonState
 Padding on each side of the bitmap. More...
 
int m_padding
 Size without the padding. More...
 
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 39 of file bitmap_button.h.

Constructor & Destructor Documentation

◆ BITMAP_BUTTON()

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

34  :
35  wxPanel( aParent, aId, aPos, aSize, aStyles ),
36  m_buttonState( 0 ),
37  m_padding( 0 ),
39 {
40  if( aSize == wxDefaultSize )
41  SetMinSize( wxButton::GetDefaultSize() );
42 
43  Bind( wxEVT_PAINT, &BITMAP_BUTTON::OnPaint, this );
44  Bind( wxEVT_LEFT_UP, &BITMAP_BUTTON::OnLeftButtonUp, this );
45  Bind( wxEVT_LEFT_DOWN, &BITMAP_BUTTON::OnLeftButtonDown, this );
46  Bind( wxEVT_LEAVE_WINDOW, &BITMAP_BUTTON::OnMouseLeave, this );
47  Bind( wxEVT_ENTER_WINDOW, &BITMAP_BUTTON::OnMouseEnter, this );
48  Bind( wxEVT_KILL_FOCUS, &BITMAP_BUTTON::OnKillFocus, this );
49  Bind( wxEVT_SET_FOCUS, &BITMAP_BUTTON::OnSetFocus, this );
50 }
void OnLeftButtonUp(wxMouseEvent &aEvent)
void OnMouseLeave(wxEvent &aEvent)
void OnMouseEnter(wxEvent &aEvent)
bool m_acceptDraggedInClicks
void OnSetFocus(wxEvent &aEvent)
int m_padding
Size without the padding.
int m_buttonState
Padding on each side of the bitmap.
void OnKillFocus(wxEvent &aEvent)
void OnPaint(wxPaintEvent &aEvent)
void OnLeftButtonDown(wxMouseEvent &aEvent)

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

◆ ~BITMAP_BUTTON()

BITMAP_BUTTON::~BITMAP_BUTTON ( )

Definition at line 53 of file bitmap_button.cpp.

54 {
55 }

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

81 {
82  m_acceptDraggedInClicks = aAcceptDragIn;
83 }
bool m_acceptDraggedInClicks

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.

Parameters
aCheckis true to check, false to uncheck.

Definition at line 217 of file bitmap_button.cpp.

218 {
219  if( aCheck )
220  setFlag( wxCONTROL_CHECKED );
221  else
222  clearFlag( wxCONTROL_CHECKED );
223 
224  Refresh();
225 }
void setFlag(int aFlag)
Definition: bitmap_button.h:99
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void clearFlag(int aFlag)

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

◆ clearFlag()

void BITMAP_BUTTON::clearFlag ( int  aFlag)
inlineprotected

Definition at line 104 of file bitmap_button.h.

105  {
106  m_buttonState &= ~aFlag;
107  }
int m_buttonState
Padding on each side of the bitmap.

References m_buttonState.

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

◆ Enable()

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

Enable the button.

Parameters
aEnableis true to enable, false to disable.

Definition at line 198 of file bitmap_button.cpp.

199 {
200  // If the requested state is already the current state, don't do anything
201  if( aEnable != hasFlag( wxCONTROL_DISABLED ) )
202  return false;
203 
204  wxPanel::Enable( aEnable );
205 
206  if( aEnable )
207  clearFlag( wxCONTROL_DISABLED );
208  else
209  setFlag( wxCONTROL_DISABLED );
210 
211  Refresh();
212 
213  return true;
214 }
void setFlag(int aFlag)
Definition: bitmap_button.h:99
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void clearFlag(int aFlag)
bool hasFlag(int aFlag)

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

◆ hasFlag()

bool BITMAP_BUTTON::hasFlag ( int  aFlag)
inlineprotected

Definition at line 109 of file bitmap_button.h.

110  {
111  return m_buttonState & aFlag;
112  }
int m_buttonState
Padding on each side of the bitmap.

References m_buttonState.

Referenced by Enable(), OnLeftButtonUp(), and OnPaint().

◆ OnKillFocus()

void BITMAP_BUTTON::OnKillFocus ( wxEvent &  aEvent)
protected

Definition at line 102 of file bitmap_button.cpp.

103 {
104  clearFlag( wxCONTROL_FOCUSED );
105  Refresh();
106  aEvent.Skip();
107 }
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void clearFlag(int aFlag)

References clearFlag(), and Refresh().

Referenced by BITMAP_BUTTON().

◆ OnLeftButtonDown()

void BITMAP_BUTTON::OnLeftButtonDown ( wxMouseEvent &  aEvent)
protected

Definition at line 144 of file bitmap_button.cpp.

145 {
146  setFlag( wxCONTROL_PRESSED );
147  Refresh();
148 
149  aEvent.Skip();
150 }
void setFlag(int aFlag)
Definition: bitmap_button.h:99
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...

References Refresh(), and setFlag().

Referenced by BITMAP_BUTTON().

◆ OnLeftButtonUp()

void BITMAP_BUTTON::OnLeftButtonUp ( wxMouseEvent &  aEvent)
protected

Definition at line 118 of file bitmap_button.cpp.

119 {
120  // Only create a button event when the control is enabled
121  // and only accept clicks that came without prior mouse-down if configured
122  if( !hasFlag( wxCONTROL_DISABLED ) &&
123  ( m_acceptDraggedInClicks || hasFlag( wxCONTROL_PRESSED | wxCONTROL_FOCUSED ) ) )
124  {
125  wxEvtHandler* pEventHandler = GetEventHandler();
126  wxASSERT( pEventHandler );
127 
128  pEventHandler->CallAfter(
129  [=]()
130  {
131  wxCommandEvent evt( wxEVT_BUTTON, GetId() );
132  evt.SetEventObject( this );
133  GetEventHandler()->ProcessEvent( evt );
134  } );
135  }
136 
137  clearFlag( wxCONTROL_PRESSED );
138  Refresh();
139 
140  aEvent.Skip();
141 }
bool m_acceptDraggedInClicks
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void clearFlag(int aFlag)
bool hasFlag(int aFlag)

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

Referenced by BITMAP_BUTTON().

◆ OnMouseEnter()

void BITMAP_BUTTON::OnMouseEnter ( wxEvent &  aEvent)
protected

Definition at line 94 of file bitmap_button.cpp.

95 {
96  setFlag( wxCONTROL_CURRENT );
97  Refresh();
98  aEvent.Skip();
99 }
void setFlag(int aFlag)
Definition: bitmap_button.h:99
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...

References Refresh(), and setFlag().

Referenced by BITMAP_BUTTON().

◆ OnMouseLeave()

void BITMAP_BUTTON::OnMouseLeave ( wxEvent &  aEvent)
protected

Definition at line 86 of file bitmap_button.cpp.

87 {
88  clearFlag( wxCONTROL_CURRENT | wxCONTROL_PRESSED );
89  Refresh();
90  aEvent.Skip();
91 }
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void clearFlag(int aFlag)

References clearFlag(), and Refresh().

Referenced by BITMAP_BUTTON().

◆ OnPaint()

void BITMAP_BUTTON::OnPaint ( wxPaintEvent &  aEvent)
protected

Definition at line 153 of file bitmap_button.cpp.

154 {
155  bool darkMode = KIPLATFORM::UI::IsDarkTheme();
156  wxColor highlightColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
157 
158  // The drawing rectangle
159  wxRect rect( wxPoint( 0, 0 ), GetSize() );
160  wxPaintDC dc( this );
161 
162  // This drawing is done so the button looks the same as an AUI toolbar button
163  if( !hasFlag( wxCONTROL_DISABLED ) )
164  {
165  if( hasFlag( wxCONTROL_PRESSED ) )
166  {
167  dc.SetPen( wxPen( highlightColor ) );
168  dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 20 : 150 ) ) );
169  dc.DrawRectangle( rect );
170  }
171  else if( hasFlag( wxCONTROL_CURRENT | wxCONTROL_FOCUSED ) )
172  {
173  dc.SetPen( wxPen( highlightColor ) );
174  dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
175 
176  // Checked items need a lighter hover rectangle
177  if( hasFlag( wxCONTROL_CHECKED ) )
178  dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 50 : 180 ) ) );
179 
180  dc.DrawRectangle( rect );
181  }
182  else if( hasFlag( wxCONTROL_CHECKED ) )
183  {
184  dc.SetPen( wxPen( highlightColor ) );
185  dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
186  dc.DrawRectangle( rect );
187  }
188  }
189 
190  const wxBitmap& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap;
191 
192  // Draw the bitmap with the upper-left corner offset by the padding
193  if( bmp.IsOk() )
194  dc.DrawBitmap( bmp, m_padding, m_padding, true );
195 }
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:31
int m_padding
Size without the padding.
wxBitmap m_disabledBitmap
Current state of the button.
wxBitmap m_normalBitmap
< Bitmap shown when button is enabled
bool hasFlag(int aFlag)

References hasFlag(), KIPLATFORM::UI::IsDarkTheme(), m_disabledBitmap, m_normalBitmap, and m_padding.

Referenced by BITMAP_BUTTON().

◆ OnSetFocus()

void BITMAP_BUTTON::OnSetFocus ( wxEvent &  aEvent)
protected

Definition at line 110 of file bitmap_button.cpp.

111 {
112  setFlag( wxCONTROL_FOCUSED );
113  Refresh();
114  aEvent.Skip();
115 }
void setFlag(int aFlag)
Definition: bitmap_button.h:99
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...

References Refresh(), and setFlag().

Referenced by BITMAP_BUTTON().

◆ SetBitmap()

void BITMAP_BUTTON::SetBitmap ( const wxBitmap &  aBmp)

Set the bitmap shown when the button is enabled.

Parameters
aBmpis the enabled bitmap.

Definition at line 65 of file bitmap_button.cpp.

66 {
67  m_normalBitmap = aBmp;
68  m_unadjustedMinSize = aBmp.GetSize();
69 
70  SetMinSize( wxSize( aBmp.GetWidth() + ( m_padding * 2 ), aBmp.GetHeight() + ( m_padding * 2 ) ) );
71 }
wxSize m_unadjustedMinSize
Accept mouse-up as click even if mouse-down happened outside of the control.
int m_padding
Size without the padding.
wxBitmap m_normalBitmap
< Bitmap shown when button is enabled

References m_normalBitmap, m_padding, and m_unadjustedMinSize.

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

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

75 {
76  m_disabledBitmap = aBmp;
77 }
wxBitmap m_disabledBitmap
Current state of the button.

References m_disabledBitmap.

Referenced by ACTION_TOOLBAR_PALETTE::AddAction().

◆ setFlag()

void BITMAP_BUTTON::setFlag ( int  aFlag)
inlineprotected

Definition at line 99 of file bitmap_button.h.

100  {
101  m_buttonState |= aFlag;
102  }
int m_buttonState
Padding on each side of the bitmap.

References m_buttonState.

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

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

59 {
60  m_padding = aPadding;
61  SetMinSize( m_unadjustedMinSize + wxSize( aPadding * 2, aPadding * 2) );
62 }
wxSize m_unadjustedMinSize
Accept mouse-up as click even if mouse-down happened outside of the control.
int m_padding
Size without the padding.

References m_padding, and m_unadjustedMinSize.

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

Member Data Documentation

◆ m_acceptDraggedInClicks

bool BITMAP_BUTTON::m_acceptDraggedInClicks
private

Definition at line 131 of file bitmap_button.h.

Referenced by AcceptDragInAsClick(), and OnLeftButtonUp().

◆ m_buttonState

int BITMAP_BUTTON::m_buttonState
private

Padding on each side of the bitmap.

Definition at line 122 of file bitmap_button.h.

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

◆ m_disabledBitmap

wxBitmap BITMAP_BUTTON::m_disabledBitmap
private

Current state of the button.

Definition at line 119 of file bitmap_button.h.

Referenced by OnPaint(), and SetDisabledBitmap().

◆ m_normalBitmap

wxBitmap BITMAP_BUTTON::m_normalBitmap
private

< Bitmap shown when button is enabled

Bitmap shown when button is disabled

Definition at line 116 of file bitmap_button.h.

Referenced by OnPaint(), and SetBitmap().

◆ m_padding

int BITMAP_BUTTON::m_padding
private

Size without the padding.

Definition at line 125 of file bitmap_button.h.

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

◆ 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 128 of file bitmap_button.h.

Referenced by SetBitmap(), and SetPadding().


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