KiCad PCB EDA Suite
split_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) 2016 Anil8735(https://stackoverflow.com/users/3659387/anil8753)
5  * from https://stackoverflow.com/a/37274011
6  * Copyright (C) 2020-2021 Kicad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <widgets/split_button.h>
27 #include <wx/button.h>
28 #include <wx/dcclient.h>
29 #include <wx/dcmemory.h>
30 #include <wx/menu.h>
31 #include <wx/renderer.h>
32 
33 
34 SPLIT_BUTTON::SPLIT_BUTTON( wxWindow* aParent, wxWindowID aId, const wxString& aLabel,
35  const wxPoint& aPos, const wxSize& aSize ) :
36  wxPanel( aParent, aId, aPos, aSize, wxBORDER_NONE | wxTAB_TRAVERSAL, "DropDownButton" ),
37  m_label( aLabel )
38 {
39  m_colorNormal = GetForegroundColour();
40  m_colorDisabled = GetForegroundColour().MakeDisabled();
41 
42  if( aSize == wxDefaultSize )
43  {
44  wxSize defaultSize = wxButton::GetDefaultSize();
45 
46  wxSize textSize = GetTextExtent( m_label );
47  SetMinSize( wxSize( textSize.GetWidth(), defaultSize.GetHeight() ) );
48  }
49 
50  Bind( wxEVT_PAINT, &SPLIT_BUTTON::OnPaint, this );
51  Bind( wxEVT_LEFT_UP, &SPLIT_BUTTON::OnLeftButtonUp, this );
52  Bind( wxEVT_LEFT_DOWN, &SPLIT_BUTTON::OnLeftButtonDown, this );
53  Bind( wxEVT_KILL_FOCUS, &SPLIT_BUTTON::OnKillFocus, this );
54  Bind( wxEVT_LEAVE_WINDOW, &SPLIT_BUTTON::OnMouseLeave, this );
55  Bind( wxEVT_ENTER_WINDOW, &SPLIT_BUTTON::OnMouseEnter, this );
56 
57  m_pMenu = new wxMenu();
58 }
59 
60 
62 {
63  delete m_pMenu;
64  m_pMenu = nullptr;
65 }
66 
67 
68 void SPLIT_BUTTON::SetMinSize( const wxSize& aSize )
69 {
70  m_unadjustedMinSize = aSize;
71  wxPanel::SetMinSize( wxSize( aSize.GetWidth() + m_arrowButtonWidth + m_widthPadding,
72  aSize.GetHeight() ) );
73 }
74 
75 
76 void SPLIT_BUTTON::SetWidthPadding( int aPadding )
77 {
78  m_widthPadding = aPadding;
80 }
81 
82 
83 void SPLIT_BUTTON::SetBitmap( const wxBitmap& aBmp )
84 {
85  m_bitmap = aBmp;
86 
87  SetMinSize( wxSize( m_bitmap.GetWidth(), m_bitmap.GetHeight() ) );
88 }
89 
90 
92 {
93  return m_pMenu;
94 }
95 
96 
97 void SPLIT_BUTTON::OnKillFocus( wxFocusEvent& aEvent )
98 {
99  m_stateButton = wxCONTROL_CURRENT;
100  m_stateMenu = wxCONTROL_CURRENT;
101  Refresh();
102 
103  aEvent.Skip();
104 }
105 
106 
107 void SPLIT_BUTTON::OnMouseLeave( wxMouseEvent& aEvent )
108 {
109  m_stateButton = 0;
110  m_stateMenu = 0;
111  Refresh();
112 
113  aEvent.Skip();
114 }
115 
116 
117 void SPLIT_BUTTON::OnMouseEnter( wxMouseEvent& aEvent )
118 {
119  m_stateButton = wxCONTROL_CURRENT;
120  m_stateMenu = wxCONTROL_CURRENT;
121  Refresh();
122 
123  aEvent.Skip();
124 }
125 
126 
127 void SPLIT_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
128 {
129  m_stateButton = 0;
130  m_stateMenu = 0;
131 
132  Refresh();
133 
134  int x = -1;
135  int y = -1;
136  aEvent.GetPosition( &x, &y );
137 
138  if( x < ( GetSize().GetWidth() - m_arrowButtonWidth ) )
139  {
140  wxEvtHandler* pEventHandler = GetEventHandler();
141  wxASSERT( pEventHandler );
142 
143  pEventHandler->CallAfter(
144  [=]()
145  {
146  wxCommandEvent evt( wxEVT_BUTTON, GetId() );
147  evt.SetEventObject( this );
148  GetEventHandler()->ProcessEvent( evt );
149  } );
150  }
151 
152  m_bLButtonDown = false;
153 
154  aEvent.Skip();
155 }
156 
157 
158 void SPLIT_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
159 {
160  m_bLButtonDown = true;
161 
162  int x = -1;
163  int y = -1;
164  aEvent.GetPosition( &x, &y );
165 
166  if( x >= ( GetSize().GetWidth() - m_arrowButtonWidth ) )
167  {
168  m_stateButton = 0;
169  m_stateMenu = wxCONTROL_PRESSED;
170  Refresh();
171 
172  wxSize size = GetSize();
173  wxPoint position;
174  position.x = 0;
175  position.y = size.GetHeight();
176  PopupMenu( m_pMenu, position );
177 
178  m_stateMenu = 0;
179  Refresh();
180  }
181  else
182  {
183  m_stateButton = wxCONTROL_PRESSED;
184  m_stateMenu = wxCONTROL_PRESSED;
185  Refresh();
186  }
187 
188  aEvent.Skip();
189 }
190 
191 
192 void SPLIT_BUTTON::OnPaint( wxPaintEvent& WXUNUSED( aEvent ) )
193 {
194  wxPaintDC dc( this );
195  wxSize size = GetSize();
196  const int width = size.GetWidth() - m_arrowButtonWidth;
197 
198  // Draw first part of button
199  wxRect r1;
200  r1.x = 0;
201  r1.y = 0;
202  r1.width = width + 2;
203  r1.height = size.GetHeight();
204 
205  wxRendererNative::Get().DrawPushButton( this, dc, r1, m_stateButton );
206 
207  SetForegroundColour( m_bIsEnable ? m_colorNormal : m_colorDisabled );
208 
209  if( m_bitmap.IsOk() )
210  {
211  wxMemoryDC mdc( m_bitmap );
212 
213  r1.x = ( width - m_bitmap.GetWidth() ) / 2;
214 
215  if( r1.x < 0 )
216  r1.x = 0;
217 
218  r1.y += ( size.GetHeight() - m_bitmap.GetHeight() ) / 2;
219 
220  dc.Blit( wxPoint( r1.x, r1.y ), m_bitmap.GetSize(), &mdc, wxPoint( 0, 0 ), wxCOPY, true );
221  }
222  else
223  {
224  r1.y += ( size.GetHeight() - GetCharHeight() ) / 2;
225  dc.DrawLabel( m_label, r1, wxALIGN_CENTER_HORIZONTAL );
226  }
227 
228  // Draw second part of button
229  wxRect r2;
230  r2.x = width - 2;
231  r2.y = 0;
232  r2.width = m_arrowButtonWidth;
233  r2.height = size.GetHeight();
234 
235  wxRendererNative::Get().DrawPushButton( this, dc, r2, m_stateMenu );
236  wxRendererNative::Get().DrawDropArrow( this, dc, r2, m_stateMenu );
237 }
238 
239 
240 bool SPLIT_BUTTON::Enable( bool aEnable )
241 {
242  m_bIsEnable = aEnable;
243  wxPanel::Enable( m_bIsEnable );
244 
245  if( m_bIsEnable )
246  {
247  m_stateButton = 0;
248  m_stateMenu = 0;
249  }
250  else
251  {
252  m_stateButton = wxCONTROL_DISABLED;
253  m_stateMenu = wxCONTROL_DISABLED;
254  }
255 
256  Refresh();
257 
258  return aEnable;
259 }
int m_widthPadding
Definition: split_button.h:64
bool m_bIsEnable
Definition: split_button.h:60
void OnPaint(wxPaintEvent &WXUNUSED(aEvent))
bool m_bLButtonDown
Definition: split_button.h:65
void SetBitmap(const wxBitmap &aBmp)
wxMenu * GetSplitButtonMenu()
wxSize m_unadjustedMinSize
Definition: split_button.h:69
wxBitmap m_bitmap
Definition: split_button.h:68
void SetMinSize(const wxSize &aSize) override
void OnKillFocus(wxFocusEvent &aEvent)
void OnLeftButtonUp(wxMouseEvent &aEvent)
void OnMouseLeave(wxMouseEvent &aEvent)
void OnLeftButtonDown(wxMouseEvent &aEvent)
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
static const wxSize defaultSize(FRAME_T aFrameType)
wxColor m_colorNormal
Definition: split_button.h:61
wxMenu * m_pMenu
Definition: split_button.h:67
void SetWidthPadding(int aPadding)
const int m_arrowButtonWidth
Definition: split_button.h:63
SPLIT_BUTTON(wxWindow *aParent, wxWindowID aId, const wxString &aLabel, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize)
bool Enable(bool aEnable=true) override
wxString m_label
Definition: split_button.h:66
void OnMouseEnter(wxMouseEvent &aEvent)
wxColor m_colorDisabled
Definition: split_button.h:62