KiCad PCB EDA Suite
stepped_slider.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) 2018 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <widgets/stepped_slider.h>
21 #include <wx/event.h>
22 
23 
24 BEGIN_EVENT_TABLE( STEPPED_SLIDER, wxSlider )
25  EVT_SCROLL( STEPPED_SLIDER::OnScroll )
26 END_EVENT_TABLE()
27 
28 
29 STEPPED_SLIDER::STEPPED_SLIDER( wxWindow* aParent, wxWindowID aId, int aValue, int aMinValue,
30  int aMaxValue, const wxPoint& aPos, const wxSize& aSize,
31  long aStyle, const wxValidator& aValidator,
32  const wxString& aName ) :
33  wxSlider( aParent, aId, aValue, aMinValue, aMaxValue, aPos, aSize,
34  ( aStyle | wxSL_AUTOTICKS | wxSL_MIN_MAX_LABELS ),
35  aValidator,
36  aName ),
37  m_step( 1 )
38 {}
39 
40 
42 {}
43 
44 
45 void STEPPED_SLIDER::SetStep( int aSize )
46 {
47  wxASSERT( aSize > 0 );
48  m_step = ( aSize > 0 ) ? aSize : 1;
49 
50  // configure pg up/down to increment by our steps
51  SetPageSize( aSize );
52 
53  //configure arrows to incrment by our steps
54  SetLineSize( aSize );
55 
56 #ifdef __WXMSW__
57  ClearTicks();
58 
59  if( aSize > 1 )
60  SetTickFreq( aSize );
61 #endif // __WXMSW__
62 }
63 
64 
66 {
67  return m_step;
68 }
69 
70 
71 void STEPPED_SLIDER::OnScroll( wxScrollEvent& aEvent )
72 {
73  // On Windows, moving the thumb can generate multiple subevents and wx is blindly
74  // calling SetValue for each one before calling this event handler
75  // We need to explicitly wait until the "final" sub event or else we end up fighting
76  // wx on the value it is setting and extreme glitchlyness will occur
77  // Not sure if other platforms have this issue
78 #ifdef __WXMSW__
79  if( aEvent.GetEventType() == wxEVT_SCROLL_CHANGED )
80  {
81 #endif // __WXMSW__
82  const int value = GetValue();
83  const int rounded = value - value % m_step;
84  SetValue( rounded );
85 #ifdef __WXMSW__
86  }
87 #endif // __WXMSW__
88  aEvent.Skip();
89 }
void OnScroll(wxScrollEvent &aEvent)
virtual ~STEPPED_SLIDER()
int GetStep() const
Get the step size.
Customized wxSlider with forced stepping.
void SetStep(int aSize)
Set the step size.