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