-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRangeSlider.cpp
694 lines (592 loc) · 19.4 KB
/
RangeSlider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// RangeSlider.cpp : implementation file
//
/* Written By and (C) 2002 Jens Scheidtmann
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
alternatively (at your option) you can apply the Gnu Public License V 2 or above (GPL).
*/
#include "stdafx.h"
#include "RangeSlider.h"
#include "MemDC.h"
#include <math.h> // for fabs
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define RANGESLIDER_CLASSNAME "MFCRangeSlider"
const UINT RANGE_CHANGED =
::RegisterWindowMessage(_T("RANGE_CHANGED{2E7439A4-12DB-42a2-BB95-3462B3EC39CE}CRangeSlider"));
/////////////////////////////////////////////////////////////////////////////
// CRangeSlider
CRangeSlider::CRangeSlider()
{
RegisterWindowClass();
m_Min = 0.0;
m_Max = 1.0;
m_Left = 0.0;
m_Right = 1.0;
m_bHorizontal = TRUE;
m_nArrowWidth = 10;
m_bTracking = FALSE;
m_bVisualMinMax = FALSE;
m_bInvertedMode = FALSE;
}
CRangeSlider::~CRangeSlider()
{
}
// Relay call to create
void CRangeSlider::Create(DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID, CCreateContext *pContext) {
CWnd::Create(NULL, "", dwStyle, rect, pParentWnd, nID, pContext);
}
// Register the window class, so you can use this class as a custom control.
BOOL CRangeSlider::RegisterWindowClass(void) {
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetInstanceHandle();
if (!(::GetClassInfo(hInst, RANGESLIDER_CLASSNAME, &wndcls)))
{
// otherwise we need to register a new class
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
wndcls.hbrBackground = NULL; // No Background brush (gives flicker)
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = RANGESLIDER_CLASSNAME;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
BEGIN_MESSAGE_MAP(CRangeSlider, CWnd)
//{{AFX_MSG_MAP(CRangeSlider)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_KILLFOCUS()
ON_WM_KEYDOWN()
ON_WM_SETFOCUS()
ON_WM_GETDLGCODE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRangeSlider message handlers
void CRangeSlider::OnPaint()
{
CPaintDC RawDC(this); // device context for painting
CMemDC dc(&RawDC);
dc.SetMapMode(MM_TEXT);
if (m_bHorizontal) {
OnPaintHorizontal(dc);
} else { // Vertical Mode
OnPaintVertical(dc);
}
}
// Paint in Horizontal Mode.
void CRangeSlider::OnPaintHorizontal(CDC &dc) {
ASSERT(m_bHorizontal);
// Calculate Arrow width and x-axis width.
CRect ClientRect;
GetClientRect(&ClientRect);
int dy = ClientRect.Height();
m_nArrowWidth = dy * 3 / 4;
m_dx = ClientRect.Width() - 2 * m_nArrowWidth;
CRect ClipBox;
dc.GetClipBox(&ClipBox);
// Erase Background.
CGdiObject *pBr = dc.SelectStockObject(GRAY_BRUSH);
CGdiObject *pPen = dc.SelectStockObject(BLACK_PEN);
dc.Rectangle(&ClipBox);
// Check if [Left, Right] is contained in [Min, Max].
ASSERT(m_Min <= m_Left);
ASSERT(m_Left <= m_Right);
ASSERT(m_Right <= m_Max);
// Position of Left and Right
int x1 = static_cast<int>((m_Left - m_Min)/(m_Max - m_Min) * m_dx + 0.5);
int x2 = static_cast<int>((m_Right - m_Min)/(m_Max - m_Min) * m_dx + 0.5);
dc.SelectStockObject(BLACK_PEN);
CBrush ButtonFace;
ButtonFace.CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));
dc.SelectObject(ButtonFace);
// Draw Left Arrow
m_RectLeft = CRect (CPoint(x1,0), CSize(m_nArrowWidth, dy));
{
CRect & rect = m_RectLeft;
dc.Rectangle(&rect);
rect.DeflateRect(1,1,2,2);
// Make appearance 3D
if (!m_bTracking || m_TrackMode!= TRACK_LEFT) {
CPen pen1;
pen1.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNHIGHLIGHT));
dc.SelectObject(pen1);
dc.MoveTo(rect.left, rect.bottom);
dc.LineTo(rect.left, rect.top);
dc.LineTo(rect.right, rect.top);
CPen pen2;
pen2.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNSHADOW));
dc.SelectObject(pen2);
dc.LineTo(rect.right, rect.bottom);
dc.LineTo(rect.left, rect.bottom);
}
// Draw arrow
dc.SelectStockObject(BLACK_PEN);
dc.SelectStockObject(BLACK_BRUSH);
CPoint poly [] = {
CPoint(x1 + m_nArrowWidth / 3, dy / 3),
CPoint(x1 + m_nArrowWidth / 3, dy*2/3),
CPoint(x1 + m_nArrowWidth*2/3, dy / 2),
};
dc.Polygon(poly, 3);
}
// Draw Right Arrow
m_RectRight = CRect(CPoint(m_nArrowWidth + x2, 0), CSize(m_nArrowWidth, dy));
{
CRect & rect = m_RectRight;
dc.SelectObject(ButtonFace);
dc.Rectangle(&rect);
// Make appearance 3D
if (!m_bTracking || m_TrackMode!= TRACK_RIGHT) {
rect.DeflateRect(1,1,2,2);
CPen pen1;
pen1.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNHIGHLIGHT));
dc.SelectObject(pen1);
dc.MoveTo(rect.left, rect.bottom);
dc.LineTo(rect.left, rect.top);
dc.LineTo(rect.right, rect.top);
CPen pen2;
pen2.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNSHADOW));
dc.SelectObject(pen2);
dc.LineTo(rect.right, rect.bottom);
dc.LineTo(rect.left, rect.bottom);
}
// Draw Arrow
dc.SelectStockObject(BLACK_PEN);
dc.SelectStockObject(BLACK_BRUSH);
CPoint poly [] = {
CPoint(m_nArrowWidth + x2 + m_nArrowWidth * 2 / 3, dy / 3),
CPoint(m_nArrowWidth + x2 + m_nArrowWidth * 2 / 3, dy * 2 / 3),
CPoint(m_nArrowWidth + x2 + m_nArrowWidth / 3, dy / 2),
};
dc.Polygon(poly, 3);
}
dc.SelectStockObject(WHITE_BRUSH);
// Draw Area in between.
CRect rect = CRect(CPoint(m_nArrowWidth + x1 + 1, 1), CPoint (m_nArrowWidth + x2, dy - 1));
dc.SelectStockObject(NULL_PEN);
dc.SelectStockObject(WHITE_BRUSH);
dc.Rectangle(&rect);
// Store value for drawing the focus rectangle
CRect focusRect = rect;
// Draw Area of InnerMin, Max.
if (m_bVisualMinMax) {
ASSERT(m_Min <= m_VisualMax);
ASSERT(m_VisualMin <= m_VisualMax);
ASSERT(m_VisualMax <= m_Max);
// See wether [InnerMin, InnerMax] geschnitten [Left, Right] is non-empty.
// Then Draw
if (m_VisualMin < m_Right && m_VisualMax > m_Left) {
int visualMin = static_cast<int>((m_VisualMin - m_Min)/(m_Max - m_Min) * m_dx + 0.5);
int visualMax = static_cast<int>((m_VisualMax - m_Min)/(m_Max - m_Min) * m_dx + 0.5);
// Stay in [m_Left, m_Right] intervall.
visualMin = max(visualMin + m_nArrowWidth, x1 + m_nArrowWidth);
visualMax = min(visualMax + m_nArrowWidth, x2 + m_nArrowWidth);
rect = CRect(visualMin, 1, visualMax, dy - 1);
CBrush br;
//br.CreateSolidBrush(RGB(255,255,150)); // light yellow;
br.CreateSolidBrush(RGB(50,50,200)); //
dc.SelectObject(br);
dc.SelectStockObject(NULL_PEN);
dc.Rectangle(&rect);
CPen pen;
dc.SelectStockObject(HOLLOW_BRUSH);
}
}
if (GetFocus() == this) {
focusRect.DeflateRect(3,3);
dc.DrawFocusRect(&focusRect);
}
dc.SelectObject(pBr);
dc.SelectObject(pPen);
}
// Paint in Vertical Mode.
void CRangeSlider::OnPaintVertical(CDC &dc) {
ASSERT(!m_bHorizontal);
CRect ClientRect;
GetClientRect(&ClientRect);
int dy = ClientRect.Width();
m_nArrowWidth = dy * 3 / 4;
m_dx = ClientRect.Height() - 2 * m_nArrowWidth;
CRect ClipBox;
dc.GetClipBox(&ClipBox);
// Erase Background.
CGdiObject *pBr = dc.SelectStockObject(GRAY_BRUSH);
CGdiObject *pPen = dc.SelectStockObject(BLACK_PEN);
dc.Rectangle(&ClipBox);
// Check if [Left, Right] is contained in [Min, Max].
ASSERT(m_Min <= m_Left);
ASSERT(m_Left <= m_Right);
ASSERT(m_Right <= m_Max);
// Position of Left and Right
int x1 = static_cast<int>((m_Left - m_Min)/(m_Max - m_Min) * m_dx + 0.5);
int x2 = static_cast<int>((m_Right - m_Min)/(m_Max - m_Min) * m_dx + 0.5);
dc.SelectStockObject(BLACK_PEN);
CBrush ButtonFace;
ButtonFace.CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));
dc.SelectObject(ButtonFace);
// Draw Left Arrow
m_RectLeft = CRect (CPoint(0, x1), CSize(dy, m_nArrowWidth));
{
CRect & rect = m_RectLeft;
dc.Rectangle(&rect);
// Make appearance 3D
if (!m_bTracking || m_TrackMode != TRACK_LEFT) {
rect.DeflateRect(1,1,2,2);
CPen pen1;
pen1.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNHIGHLIGHT));
dc.SelectObject(pen1);
dc.MoveTo(rect.left, rect.bottom);
dc.LineTo(rect.left, rect.top);
dc.LineTo(rect.right, rect.top);
CPen pen2;
pen2.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNSHADOW));
dc.SelectObject(pen2);
dc.LineTo(rect.right, rect.bottom);
dc.LineTo(rect.left, rect.bottom);
}
// Draw Arrow
dc.SelectStockObject(BLACK_PEN);
dc.SelectStockObject(BLACK_BRUSH);
CPoint poly [] = {
CPoint(dy / 3, x1 + m_nArrowWidth / 3),
CPoint(dy*2/3, x1 + m_nArrowWidth / 3),
CPoint(dy / 2, x1 + m_nArrowWidth*2/3),
};
dc.Polygon(poly, 3);
}
// Draw Right Arrow
m_RectRight = CRect(CPoint(0,m_nArrowWidth + x2), CSize(dy, m_nArrowWidth));
{
CRect & rect = m_RectRight;
dc.SelectObject(ButtonFace);
dc.Rectangle(&rect);
// Make appearance 3D
if (!m_bTracking || m_TrackMode != TRACK_RIGHT) {
rect.DeflateRect(1,1,2,2);
CPen pen1;
pen1.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNHIGHLIGHT));
dc.SelectObject(pen1);
dc.MoveTo(rect.left, rect.bottom);
dc.LineTo(rect.left, rect.top);
dc.LineTo(rect.right, rect.top);
CPen pen2;
pen2.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNSHADOW));
dc.SelectObject(pen2);
dc.LineTo(rect.right, rect.bottom);
dc.LineTo(rect.left, rect.bottom);
}
// Draw Arrow
dc.SelectStockObject(BLACK_PEN);
dc.SelectStockObject(BLACK_BRUSH);
CPoint poly [] = {
CPoint(dy / 3, m_nArrowWidth + x2 + m_nArrowWidth * 2 / 3),
CPoint(dy*2/3, m_nArrowWidth + x2 + m_nArrowWidth * 2 / 3),
CPoint(dy / 2, m_nArrowWidth + x2 + m_nArrowWidth / 3),
};
dc.Polygon(poly, 3);
}
dc.SelectStockObject(WHITE_BRUSH);
// Draw Area in between.
CRect rect = CRect(CPoint(1, m_nArrowWidth + x1 + 1), CPoint (dy - 1, m_nArrowWidth + x2));
dc.SelectStockObject(NULL_PEN);
dc.SelectStockObject(WHITE_BRUSH);
dc.Rectangle(&rect);
// Store value for drawing the focus rectangle
CRect focusRect = rect;
// Draw Area of InnerMin, Max.
if (m_bVisualMinMax) {
ASSERT(m_Min <= m_VisualMax);
ASSERT(m_VisualMin <= m_VisualMax);
ASSERT(m_VisualMax <= m_Max);
// See wether [InnerMin, InnerMax] geschnitten [Left, Right] is non-empty.
// Then Draw
if (m_VisualMin < m_Right && m_VisualMax > m_Left) {
int visualMin = static_cast<int>((m_VisualMin - m_Min)/(m_Max - m_Min) * m_dx + 0.5);
int visualMax = static_cast<int>((m_VisualMax - m_Min)/(m_Max - m_Min) * m_dx + 0.5);
// Stay in [m_Left, m_Right] intervall.
visualMin = max(visualMin + m_nArrowWidth, x1 + m_nArrowWidth);
visualMax = min(visualMax + m_nArrowWidth, x2 + m_nArrowWidth);
rect = CRect(dy - 1, visualMin, 1, visualMax);
CBrush br;
br.CreateSolidBrush(RGB(255,255,150)); // light yellow;
dc.SelectObject(br);
dc.SelectStockObject(NULL_PEN);
dc.Rectangle(&rect);
dc.SelectStockObject(HOLLOW_BRUSH);
}
}
if (GetFocus() == this) {
focusRect.DeflateRect(3,3);
dc.DrawFocusRect(&focusRect);
}
dc.SelectObject(pBr);
dc.SelectObject(pPen);
}
void CRangeSlider::OnLButtonDown(UINT nFlags, CPoint point)
{
TRACE("Down Point %d, %d\n", point.x, point.y);
SetFocus();
Invalidate();
if (!m_bTracking) {
// Hit Testing into Rects.
// Left, Middle or Right?
CRect rect;
GetClientRect(&rect);
CRect middleRect (0,0,0,0);
if (m_bHorizontal)
middleRect = CRect(m_RectLeft.right + 1, 0, m_RectRight.left - 1, rect.bottom);
else
middleRect = CRect(0, m_RectLeft.bottom + 1, m_RectLeft.right, m_RectRight.top - 1);
if (m_RectLeft.PtInRect(point)) {
m_bTracking = TRUE;
m_TrackMode = TRACK_LEFT;
m_ClickOffset = point - m_RectLeft.CenterPoint();
} else if (m_RectRight.PtInRect(point)) {
m_bTracking = TRUE;
m_TrackMode = TRACK_RIGHT;
m_ClickOffset = point - m_RectRight.CenterPoint();
} else if (middleRect.PtInRect(point)) {
m_bTracking = TRUE;
m_TrackMode = TRACK_MIDDLE;
m_ClickOffset = point - middleRect.CenterPoint();
}
if (m_bTracking)
SetCapture();
}
CWnd::OnLButtonDown(nFlags, point);
}
void CRangeSlider::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bTracking) {
int x = 0;
if (m_bHorizontal)
x = point.x - m_ClickOffset.x;
else
x = point.y - m_ClickOffset.y;
CRect rect;
WPARAM changed = 0;
switch (m_TrackMode) {
case TRACK_LEFT: {
double oldLeft = m_Left;
m_Left = static_cast<double>(x - m_nArrowWidth / 2) / m_dx * (m_Max - m_Min) + m_Min;
if (m_Left >= m_Right)
m_Left = m_Right;
if (m_Left <= m_Min)
m_Left = m_Min;
if (oldLeft != m_Left)
changed = RS_LEFTCHANGED;
rect = m_RectLeft;
} break;
case TRACK_RIGHT: {
double oldRight = m_Right;
m_Right = static_cast<double>(x - m_nArrowWidth * 3 / 2) / m_dx * (m_Max - m_Min) + m_Min;
if (m_Right <= m_Left)
m_Right = m_Left;
if (m_Right >= m_Max)
m_Right = m_Max;
if (oldRight != m_Right)
changed = RS_RIGHTCHANGED;
rect = m_RectRight;
} break;
case TRACK_MIDDLE:
{
double delta = m_Right - m_Left;
ASSERT(delta >= 0.0);
m_Left = static_cast<double>(x - m_nArrowWidth) / m_dx * (m_Max - m_Min) + m_Min - delta/2.0;
m_Right = static_cast<double>(x - m_nArrowWidth) / m_dx * (m_Max - m_Min) + m_Min + delta/2.0;
if (m_Left <= m_Min) {
m_Left = m_Min;
m_Right = m_Left + delta;
}
if (m_Right >= m_Max) {
m_Right = m_Max;
m_Left = m_Right - delta;
}
changed = RS_BOTHCHANGED;
}
break;
default:
TRACE("Unknown Track Mode\n");
ASSERT(FALSE);
break;
}
::SendMessage(GetParent()->GetSafeHwnd(), RANGE_CHANGED, changed, static_cast<LPARAM>(0));
Invalidate();
}
CWnd::OnMouseMove(nFlags, point);
}
void CRangeSlider::OnLButtonUp(UINT nFlags, CPoint point)
{
if (m_bTracking) {
m_bTracking = FALSE;
::ReleaseCapture();
Invalidate();
}
CWnd::OnLButtonUp(nFlags, point);
}
void CRangeSlider::SetMinMax(double min, double max) {
if (min > max) {
double x = max;
max = min;
min = x;
}
m_Min = (m_bInvertedMode) ? -max : min;
m_Max = (m_bInvertedMode) ? -min : max;
Normalize();
Invalidate();
}
void CRangeSlider::SetRange(double left, double right) {
m_Left = (m_bInvertedMode) ? -right : left;
m_Right = (m_bInvertedMode) ? -left : right;
Normalize();
Invalidate();
}
void CRangeSlider::NormalizeVisualMinMax(void) {
// Exchange if reversed.
if (m_VisualMax < m_VisualMin) {
double dummy = m_VisualMin;
m_VisualMin = m_VisualMax;
m_VisualMax = dummy;
}
if (m_VisualMin < m_Min) {
m_VisualMin = m_Min;
if (m_VisualMax < m_VisualMin)
m_VisualMax = m_VisualMin;
}
if (m_VisualMax > m_Max) {
m_VisualMax = m_Max;
if (m_VisualMin > m_VisualMax)
m_VisualMin = m_VisualMax;
}
}
void CRangeSlider::Normalize(void) {
if (m_Left < m_Min) {
m_Left = m_Min;
::SendMessage(GetParent()->GetSafeHwnd(), RANGE_CHANGED, RS_LEFTCHANGED, 0);
if (m_Right < m_Left) {
m_Right = m_Left;
::SendMessage(GetParent()->GetSafeHwnd(), RANGE_CHANGED, RS_RIGHTCHANGED, 0);
}
}
if (m_Right > m_Max) {
m_Right = m_Max;
::SendMessage(GetParent()->GetSafeHwnd(), RANGE_CHANGED, RS_RIGHTCHANGED, 0);
if (m_Left > m_Right) {
m_Left = m_Right;
::SendMessage(GetParent()->GetSafeHwnd(), RANGE_CHANGED, RS_LEFTCHANGED, 0);
}
}
}
void CRangeSlider::SetVisualMode(BOOL bVisualMinMax) {
if (m_bVisualMinMax != bVisualMinMax)
Invalidate();
m_bVisualMinMax = bVisualMinMax;
}
void CRangeSlider::SetVisualMinMax(double VisualMin, double VisualMax) {
m_VisualMin = (m_bInvertedMode) ? -VisualMax : VisualMin;
m_VisualMax = (m_bInvertedMode) ? -VisualMin : VisualMax;
NormalizeVisualMinMax();
Invalidate();
}
UINT CRangeSlider::OnGetDlgCode()
{
return DLGC_WANTARROWS;
}
void CRangeSlider::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
BOOL bCtrl = (::GetKeyState(VK_CONTROL) & 0x8000)!= 0; // T if Ctrl is pressed.
UINT Key1, Key2;
if (m_bHorizontal) {
Key1 = VK_RIGHT; Key2 = VK_LEFT;
} else {
Key1 = VK_DOWN; Key2 = VK_UP;
}
if ((nChar == Key1 || nChar == Key2) && !bCtrl) {
double dx = (m_Max - m_Min) / m_dx;
if (dx != 0.0 ) {
int left = static_cast<int>((m_Left - m_Min) / dx + 0.5);
int right = static_cast<int>((m_Right - m_Min) / dx + 0.5);
BOOL bShift = ::GetKeyState(VK_SHIFT) < 0; // T if Shift is pressed.
if (nChar == Key2) {
if (!bShift) // Shift not pressed => move intervall
left--;
right--;
} else {
if (!bShift) // Shift not pressed => move intervall
left++;
right++;
}
if (left >= 0 && right <= m_dx) {
m_Left = m_Min + left * dx;
m_Right = m_Min + right * dx;
}
if (!bShift)
::SendMessage(GetParent()->GetSafeHwnd(), RANGE_CHANGED, RS_BOTHCHANGED, 0);
else
::SendMessage(GetParent()->GetSafeHwnd(), RANGE_CHANGED, RS_LEFTCHANGED, 0);
Invalidate();
}
}
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CRangeSlider::OnKillFocus(CWnd* pNewWnd)
{
CWnd::OnKillFocus(pNewWnd);
Invalidate();
}
void CRangeSlider::OnSetFocus(CWnd* pOldWnd)
{
CWnd::OnSetFocus(pOldWnd);
Invalidate();
}
void CRangeSlider::SetVerticalMode(BOOL bVerticalMode) {
if (m_bHorizontal != bVerticalMode)
return;
m_bHorizontal = !bVerticalMode;
Invalidate();
}
void CRangeSlider::SetInvertedMode(BOOL bInvertedMode) {
if (m_bInvertedMode == bInvertedMode)
return;
m_bInvertedMode = bInvertedMode;
double dummy = m_Left;
m_Left = - m_Right;
m_Right = -dummy;
dummy = m_Min;
m_Min = - m_Max;
m_Max = - dummy;
dummy = m_VisualMin;
m_VisualMin = - m_VisualMax;
m_VisualMax = - dummy;
Invalidate();
}