WINDEX
slider.h
1 namespace wex
2 {
45 //deliberate error
46 
47 class slider : public gui
48 {
49 public:
50  slider( gui* parent )
51  : gui( parent )
52  , myPosition( 50 )
53  , myMin( 0 )
54  , myMax( 100 )
55  , fsliding( false )
56  , ftracking( false )
57  , fvertical( false )
58  , fsteps( false )
59  {
60  events().click([this]
61  {
62  focus();
63  RECT r;
64  GetClientRect( myHandle, &r );
65  int height = r.bottom - (r.bottom - r.top ) * myPosition / myMax;
66  auto m = getMouseStatus();
67  if( abs(m.y - height) > 10 )
68  return;
69  //std::cout << "slider2 click\n";
70  fsliding = true;
71  });
72  events().mouseMove([this](sMouse& m )
73  {
74  if( ! ftracking )
75  {
76  // first mouse movement in slider
77  // start tracking mouse movement
78  ftracking = true;
79 
80  // generate event when mouse leaves
81  TRACKMOUSEEVENT s;
82  s.cbSize = sizeof( s );
83  s.hwndTrack = myHandle;
84  s.dwFlags = TME_LEAVE;
85  TrackMouseEvent( & s );
86 
87  // run mouse enter event handler
88  events().onMouseEnter();
89  }
90  if( ! fsliding )
91  {
92  return;
93  }
94  RECT r;
95  GetClientRect( myHandle, &r );
96 // std::cout << "m.y " << m.y
97 // <<" "<< r.bottom <<" " << r.top <<" ";
98  if( fvertical )
99  {
100  if( m.y > r.bottom || m.y < r.top )
101  return;
102  myPosition = myMax * ( r.bottom - m.y ) / ( r.bottom - r.top );
103  }
104  else
105  {
106  if( m.x < r.left || m.x > r.right )
107  return;
108  myPosition = myMax * ( m.x - r.left ) / ( r.right - r.left );
109  }
110  if( myPosition > myMax )
111  myPosition = myMax;
112  if( fsteps )
113  myPosition = std::round( myPosition);
114 
115 // std::cout << myPosition << " ";
116  update();
117  events().onSlid( myPosition );
118  });
119  events().mouseUp([this]
120  {
121  fsliding = false;
122 
123  });
124  }
132  void range( int min, int max )
133  {
134  if( min < 0 )
135  throw std::runtime_error(
136  "wex::slider positions must be positive");
137  if( max < min )
138  throw std::runtime_error(
139  "wex::slider bad range parameters");
140 
141  myMin = min;
142  myMax = max;
143  myPosition = ( myMax + myMin ) / 2;
144  }
145  void stopTracking()
146  {
147  ftracking = false;
148  }
149  void draw( PAINTSTRUCT& ps )
150  {
151  RECT r;
152  GetClientRect( myHandle, &r );
153 
154  // track
155  wex::shapes s( ps );
156  if( ! myfEnabled )
157  s.color( 0xAAAAAA );
158  if( fvertical )
159  {
160  int center = r.left+(r.right-r.left)/2;
161  s.line( {center-1,r.top+5,center-1,r.bottom-5});
162  s.line( {center+1,r.top+5,center+1,r.bottom-5});
163  }
164  else
165  {
166  int center = r.top+(r.bottom-r.top)/2;
167  s.line({r.left+5,center-1,r.right-5,center-1});
168  s.line({r.left+5,center+1,r.right-5,center+1});
169  }
170 
171  // thumbnail
172  if( myfEnabled )
173  SelectObject(ps.hdc, GetStockObject(BLACK_BRUSH));
174  else
175  SelectObject(ps.hdc, GetStockObject(GRAY_BRUSH));
176  //std::cout << "position " << myPosition << "\n";
177  if( fvertical )
178  {
179  int height = r.bottom - (r.bottom - r.top ) * myPosition / myMax;
180  RoundRect(ps.hdc,
181  r.left, height -4,
182  r.right, height + 4,
183  5, 5);
184  }
185  else
186  {
187  int height = r.left + (r.right - r.left ) * myPosition / myMax;
188  RoundRect(ps.hdc,
189  height - 4, r.top,
190  height + 4, r.bottom,
191  5, 5);
192  }
193  }
194  void vertical(bool f = true)
195  {
196  fvertical = f;
197  }
198 
199  double position() const
200  {
201  return myPosition;
202  }
203  void position( double v )
204  {
205  myPosition = v;
206  }
207  void maximum( double max )
208  {
209  myMax = max;
210  }
213  void steps( bool f = true )
214  {
215  fsteps = f;
216  }
217 private:
218  double myPosition;
219  double myMin;
220  double myMax;
221  bool fsliding;
222  bool ftracking;
223  bool fvertical;
224  bool fsteps;
225 };
226 
227 }
void click(std::function< void(void)> f, bool propogate=false)
register click event handler
Definition: wex.h:276
The base class for all windex gui elements.
Definition: wex.h:824
void update()
force widget to redraw completely
Definition: wex.h:1575
eventhandler & events()
Get event handler.
Definition: wex.h:1649
bool myfEnabled
true if not disabled
Definition: wex.h:1698
sMouse getMouseStatus()
Get mouse status.
Definition: wex.h:1171
gui()
Construct top level window with no parent.
Definition: wex.h:831
A class that offers application code methods to draw on a window.
Definition: wex.h:525
A widget which user can drag to change a value.
Definition: slider.h:48
void steps(bool f=true)
Specify slider moves in steps to integer positions.
Definition: slider.h:213
void range(int min, int max)
Specify the range of values used.
Definition: slider.h:132
A structure containing the mouse status for event handlers.
Definition: wex.h:28