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...
 

Protected Member Functions

void OnMouseLeave (wxMouseEvent &aEvent)
 
void OnMouseEnter (wxMouseEvent &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
 

Bitmap shown when button is disabled

More...
 
int m_buttonState
 

Current state of the button

More...
 
int m_padding
 

Padding on each side of the bitmap

More...
 
wxSize m_unadjustedMinSize
 

Size without the padding

More...
 

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 )
38 {
39  if( aSize == wxDefaultSize )
40  SetMinSize( wxButton::GetDefaultSize() );
41 
42  Bind( wxEVT_PAINT, &BITMAP_BUTTON::OnPaint, this );
43  Bind( wxEVT_LEFT_UP, &BITMAP_BUTTON::OnLeftButtonUp, this );
44  Bind( wxEVT_LEFT_DOWN, &BITMAP_BUTTON::OnLeftButtonDown, this );
45  Bind( wxEVT_LEAVE_WINDOW, &BITMAP_BUTTON::OnMouseLeave, this );
46  Bind( wxEVT_ENTER_WINDOW, &BITMAP_BUTTON::OnMouseEnter, this );
47 }
void OnLeftButtonUp(wxMouseEvent &aEvent)
void OnMouseEnter(wxMouseEvent &aEvent)
int m_padding
Padding on each side of the bitmap
int m_buttonState
Current state of the button
void OnMouseLeave(wxMouseEvent &aEvent)
void OnPaint(wxPaintEvent &aEvent)
void OnLeftButtonDown(wxMouseEvent &aEvent)

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

◆ ~BITMAP_BUTTON()

BITMAP_BUTTON::~BITMAP_BUTTON ( )

Definition at line 50 of file bitmap_button.cpp.

51 {
52 }

Member Function Documentation

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

193 {
194  if( aCheck )
195  setFlag( wxCONTROL_CHECKED );
196  else
197  clearFlag( wxCONTROL_CHECKED );
198 
199  Refresh();
200 }
void setFlag(int aFlag)
Definition: bitmap_button.h:89
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void clearFlag(int aFlag)
Definition: bitmap_button.h:94

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

◆ clearFlag()

void BITMAP_BUTTON::clearFlag ( int  aFlag)
inlineprotected

Definition at line 94 of file bitmap_button.h.

95  {
96  m_buttonState &= ~aFlag;
97  }
int m_buttonState
Current state of the button

References m_buttonState.

Referenced by Check(), Enable(), 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 173 of file bitmap_button.cpp.

174 {
175  // If the requested state is already the current state, don't do anything
176  if( aEnable != hasFlag( wxCONTROL_DISABLED ) )
177  return false;
178 
179  wxPanel::Enable( aEnable );
180 
181  if( aEnable )
182  clearFlag( wxCONTROL_DISABLED );
183  else
184  setFlag( wxCONTROL_DISABLED );
185 
186  Refresh();
187 
188  return true;
189 }
void setFlag(int aFlag)
Definition: bitmap_button.h:89
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void clearFlag(int aFlag)
Definition: bitmap_button.h:94
bool hasFlag(int aFlag)
Definition: bitmap_button.h:99

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

◆ hasFlag()

bool BITMAP_BUTTON::hasFlag ( int  aFlag)
inlineprotected

Definition at line 99 of file bitmap_button.h.

100  {
101  return m_buttonState & aFlag;
102  }
int m_buttonState
Current state of the button

References m_buttonState.

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

◆ OnLeftButtonDown()

void BITMAP_BUTTON::OnLeftButtonDown ( wxMouseEvent &  aEvent)
protected

Definition at line 119 of file bitmap_button.cpp.

120 {
121  setFlag( wxCONTROL_PRESSED );
122  Refresh();
123 
124  aEvent.Skip();
125 }
void setFlag(int aFlag)
Definition: bitmap_button.h:89
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 95 of file bitmap_button.cpp.

96 {
97  // Only create a button event when the control is enabled
98  if( !hasFlag( wxCONTROL_DISABLED ) )
99  {
100  wxEvtHandler* pEventHandler = GetEventHandler();
101  wxASSERT( pEventHandler );
102 
103  pEventHandler->CallAfter(
104  [=]()
105  {
106  wxCommandEvent evt( wxEVT_BUTTON, this->GetId() );
107  evt.SetEventObject( this );
108  GetEventHandler()->ProcessEvent( evt );
109  } );
110  }
111 
112  clearFlag( wxCONTROL_PRESSED );
113  Refresh();
114 
115  aEvent.Skip();
116 }
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void clearFlag(int aFlag)
Definition: bitmap_button.h:94
bool hasFlag(int aFlag)
Definition: bitmap_button.h:99

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

Referenced by BITMAP_BUTTON().

◆ OnMouseEnter()

void BITMAP_BUTTON::OnMouseEnter ( wxMouseEvent &  aEvent)
protected

Definition at line 86 of file bitmap_button.cpp.

87 {
88  setFlag( wxCONTROL_CURRENT );
89  Refresh();
90 
91  aEvent.Skip();
92 }
void setFlag(int aFlag)
Definition: bitmap_button.h:89
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 ( wxMouseEvent &  aEvent)
protected

Definition at line 77 of file bitmap_button.cpp.

78 {
79  clearFlag( wxCONTROL_CURRENT );
80  Refresh();
81 
82  aEvent.Skip();
83 }
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void clearFlag(int aFlag)
Definition: bitmap_button.h:94

References clearFlag(), and Refresh().

Referenced by BITMAP_BUTTON().

◆ OnPaint()

void BITMAP_BUTTON::OnPaint ( wxPaintEvent &  aEvent)
protected

Definition at line 128 of file bitmap_button.cpp.

129 {
130  bool darkMode = KIPLATFORM::UI::IsDarkTheme();
131  wxColor highlightColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT );
132 
133  // The drawing rectangle
134  wxRect rect( wxPoint( 0, 0 ), GetSize() );
135  wxPaintDC dc( this );
136 
137  // This drawing is done so the button looks the same as an AUI toolbar button
138  if( !hasFlag( wxCONTROL_DISABLED ) )
139  {
140  if( hasFlag( wxCONTROL_PRESSED ) )
141  {
142  dc.SetPen( wxPen( highlightColor ) );
143  dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 20 : 150 ) ) );
144  dc.DrawRectangle( rect );
145  }
146  else if( hasFlag( wxCONTROL_CURRENT ) )
147  {
148  dc.SetPen( wxPen( highlightColor ) );
149  dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
150 
151  // Checked items need a lighter hover rectangle
152  if( hasFlag( wxCONTROL_CHECKED ) )
153  dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 50 : 180 ) ) );
154 
155  dc.DrawRectangle( rect );
156  }
157  else if( hasFlag( wxCONTROL_CHECKED ) )
158  {
159  dc.SetPen( wxPen( highlightColor ) );
160  dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) );
161  dc.DrawRectangle( rect );
162  }
163  }
164 
165  const wxBitmap& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap;
166 
167  // Draw the bitmap with the upper-left corner offset by the padding
168  if( bmp.IsOk() )
169  dc.DrawBitmap( bmp, m_padding, m_padding, true );
170 }
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
Padding on each side of the bitmap
wxBitmap m_disabledBitmap
Bitmap shown when button is disabled
wxBitmap m_normalBitmap
Bitmap shown when button is enabled
bool hasFlag(int aFlag)
Definition: bitmap_button.h:99

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

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

63 {
64  m_normalBitmap = aBmp;
65  m_unadjustedMinSize = aBmp.GetSize();
66 
67  SetMinSize( wxSize( aBmp.GetWidth() + ( m_padding * 2 ), aBmp.GetHeight() + ( m_padding * 2 ) ) );
68 }
wxSize m_unadjustedMinSize
Size without the padding
int m_padding
Padding on each side of the bitmap
wxBitmap m_normalBitmap
Bitmap shown when button is enabled

References m_normalBitmap, m_padding, and m_unadjustedMinSize.

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

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

72 {
73  m_disabledBitmap = aBmp;
74 }
wxBitmap m_disabledBitmap
Bitmap shown when button is disabled

References m_disabledBitmap.

Referenced by ACTION_TOOLBAR_PALETTE::AddAction().

◆ setFlag()

void BITMAP_BUTTON::setFlag ( int  aFlag)
inlineprotected

Definition at line 89 of file bitmap_button.h.

90  {
91  m_buttonState |= aFlag;
92  }
int m_buttonState
Current state of the button

References m_buttonState.

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

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

56 {
57  m_padding = aPadding;
58  SetMinSize( m_unadjustedMinSize + wxSize( aPadding * 2, aPadding * 2) );
59 }
wxSize m_unadjustedMinSize
Size without the padding
int m_padding
Padding on each side of the bitmap

References m_padding, and m_unadjustedMinSize.

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

Member Data Documentation

◆ m_buttonState

int BITMAP_BUTTON::m_buttonState
private

Current state of the button

Definition at line 112 of file bitmap_button.h.

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

◆ m_disabledBitmap

wxBitmap BITMAP_BUTTON::m_disabledBitmap
private

Bitmap shown when button is disabled

Definition at line 109 of file bitmap_button.h.

Referenced by OnPaint(), and SetDisabledBitmap().

◆ m_normalBitmap

wxBitmap BITMAP_BUTTON::m_normalBitmap
private

Bitmap shown when button is enabled

Definition at line 106 of file bitmap_button.h.

Referenced by OnPaint(), and SetBitmap().

◆ m_padding

int BITMAP_BUTTON::m_padding
private

Padding on each side of the bitmap

Definition at line 115 of file bitmap_button.h.

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

◆ m_unadjustedMinSize

wxSize BITMAP_BUTTON::m_unadjustedMinSize
private

Size without the padding

Definition at line 118 of file bitmap_button.h.

Referenced by SetBitmap(), and SetPadding().


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