KiCad PCB EDA Suite
bitmap_button.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2020 Ian McInerney <ian.s.mcinerney at ieee dot org>
5  * Copyright (C) 2020 Kicad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <kiplatform/ui.h>
26 #include <widgets/bitmap_button.h>
27 #include <wx/button.h>
28 #include <wx/dcclient.h>
29 #include <wx/renderer.h>
30 #include <wx/settings.h>
31 
32 
33 BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos,
34  const wxSize& aSize, int aStyles ) :
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 }
48 
49 
51 {
52 }
53 
54 
55 void BITMAP_BUTTON::SetPadding( int aPadding )
56 {
57  m_padding = aPadding;
58  SetMinSize( m_unadjustedMinSize + wxSize( aPadding * 2, aPadding * 2) );
59 }
60 
61 
62 void BITMAP_BUTTON::SetBitmap( const wxBitmap& aBmp )
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 }
69 
70 
71 void BITMAP_BUTTON::SetDisabledBitmap( const wxBitmap& aBmp )
72 {
73  m_disabledBitmap = aBmp;
74 }
75 
76 
77 void BITMAP_BUTTON::OnMouseLeave( wxMouseEvent& aEvent )
78 {
79  clearFlag( wxCONTROL_CURRENT );
80  Refresh();
81 
82  aEvent.Skip();
83 }
84 
85 
86 void BITMAP_BUTTON::OnMouseEnter( wxMouseEvent& aEvent )
87 {
88  setFlag( wxCONTROL_CURRENT );
89  Refresh();
90 
91  aEvent.Skip();
92 }
93 
94 
95 void BITMAP_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
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 }
117 
118 
119 void BITMAP_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
120 {
121  setFlag( wxCONTROL_PRESSED );
122  Refresh();
123 
124  aEvent.Skip();
125 }
126 
127 
128 void BITMAP_BUTTON::OnPaint( wxPaintEvent& aEvent )
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 }
171 
172 
173 bool BITMAP_BUTTON::Enable( bool aEnable )
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 }
190 
191 
192 void BITMAP_BUTTON::Check( bool aCheck )
193 {
194  if( aCheck )
195  setFlag( wxCONTROL_CHECKED );
196  else
197  clearFlag( wxCONTROL_CHECKED );
198 
199  Refresh();
200 }
void OnLeftButtonUp(wxMouseEvent &aEvent)
wxSize m_unadjustedMinSize
Size without the padding
void OnMouseEnter(wxMouseEvent &aEvent)
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:31
bool Enable(bool aEnable=true) override
Enable the button.
void setFlag(int aFlag)
Definition: bitmap_button.h:89
int m_padding
Padding on each side of the bitmap
void Check(bool aCheck=true)
Check the control.
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
void SetPadding(int aPadding)
Set the amount of padding present on each side of the bitmap.
wxBitmap m_disabledBitmap
Bitmap shown when button is disabled
void SetBitmap(const wxBitmap &aBmp)
Set the bitmap shown when the button is enabled.
wxBitmap m_normalBitmap
Bitmap shown when button is enabled
void OnMouseLeave(wxMouseEvent &aEvent)
BITMAP_BUTTON(wxWindow *aParent, wxWindowID aId, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, int aStyles=wxBORDER_NONE|wxTAB_TRAVERSAL)
void OnPaint(wxPaintEvent &aEvent)
void SetDisabledBitmap(const wxBitmap &aBmp)
Set the bitmap shown when the button is disabled.
bool hasFlag(int aFlag)
Definition: bitmap_button.h:99
void OnLeftButtonDown(wxMouseEvent &aEvent)