-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatlscrl.h
2127 lines (1817 loc) · 55.9 KB
/
atlscrl.h
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Windows Template Library - WTL version 10.0
// Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
//
// This file is a part of the Windows Template Library.
// The use and distribution terms for this software are covered by the
// Microsoft Public License (http://opensource.org/licenses/MS-PL)
// which can be found in the file MS-PL.txt at the root folder.
#ifndef __ATLSCRL_H__
#define __ATLSCRL_H__
#pragma once
#ifndef __ATLAPP_H__
#error atlscrl.h requires atlapp.h to be included first
#endif
#ifndef __ATLWIN_H__
#error atlscrl.h requires atlwin.h to be included first
#endif
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CScrollImpl<T>
// CScrollWindowImpl<T, TBase, TWinTraits>
// CMapScrollImpl<T>
// CMapScrollWindowImpl<T, TBase, TWinTraits>
// CFSBWindowT<TBase>
// CZoomScrollImpl<T>
// CZoomScrollWindowImpl<T, TBase, TWinTraits>
// CScrollContainerImpl<T, TBase, TWinTraits>
// CScrollContainer
namespace WTL
{
///////////////////////////////////////////////////////////////////////////////
// CScrollImpl - Provides scrolling support to any window
// Scroll extended styles
#define SCRL_SCROLLCHILDREN 0x00000001
#define SCRL_ERASEBACKGROUND 0x00000002
#define SCRL_NOTHUMBTRACKING 0x00000004
#define SCRL_SMOOTHSCROLL 0x00000008
#define SCRL_DISABLENOSCROLLV 0x00000010
#define SCRL_DISABLENOSCROLLH 0x00000020
#define SCRL_DISABLENOSCROLL (SCRL_DISABLENOSCROLLV | SCRL_DISABLENOSCROLLH)
template <class T>
class CScrollImpl
{
public:
enum { uSCROLL_FLAGS = SW_INVALIDATE };
POINT m_ptOffset;
SIZE m_sizeAll;
SIZE m_sizeLine;
SIZE m_sizePage;
SIZE m_sizeClient;
int m_zDelta; // current wheel value
int m_nWheelLines; // number of lines to scroll on wheel
int m_zHDelta; // current horizontal wheel value
int m_nHWheelChars; // number of chars to scroll on horizontal wheel
UINT m_uScrollFlags;
DWORD m_dwExtendedStyle; // scroll specific extended styles
// Constructor
CScrollImpl() : m_zDelta(0), m_nWheelLines(3),
m_zHDelta(0), m_nHWheelChars(3),
m_uScrollFlags(0U), m_dwExtendedStyle(0)
{
m_ptOffset.x = 0;
m_ptOffset.y = 0;
m_sizeAll.cx = 0;
m_sizeAll.cy = 0;
m_sizePage.cx = 0;
m_sizePage.cy = 0;
m_sizeLine.cx = 0;
m_sizeLine.cy = 0;
m_sizeClient.cx = 0;
m_sizeClient.cy = 0;
SetScrollExtendedStyle(SCRL_SCROLLCHILDREN | SCRL_ERASEBACKGROUND);
}
// Attributes & Operations
DWORD GetScrollExtendedStyle() const
{
return m_dwExtendedStyle;
}
DWORD SetScrollExtendedStyle(DWORD dwExtendedStyle, DWORD dwMask = 0)
{
DWORD dwPrevStyle = m_dwExtendedStyle;
if(dwMask == 0)
m_dwExtendedStyle = dwExtendedStyle;
else
m_dwExtendedStyle = (m_dwExtendedStyle & ~dwMask) | (dwExtendedStyle & dwMask);
// cache scroll flags
T* pT = static_cast<T*>(this);
(void)pT; // avoid level 4 warning
m_uScrollFlags = pT->uSCROLL_FLAGS | (IsScrollingChildren() ? SW_SCROLLCHILDREN : 0) | (IsErasingBackground() ? SW_ERASE : 0);
m_uScrollFlags |= (IsSmoothScroll() ? SW_SMOOTHSCROLL : 0);
return dwPrevStyle;
}
// offset operations
void SetScrollOffset(int x, int y, BOOL bRedraw = TRUE)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->AdjustScrollOffset(x, y);
int dx = m_ptOffset.x - x;
int dy = m_ptOffset.y - y;
m_ptOffset.x = x;
m_ptOffset.y = y;
// block: set horizontal scroll bar
{
SCROLLINFO si = { sizeof(SCROLLINFO) };
si.fMask = SIF_POS;
if((m_dwExtendedStyle & SCRL_DISABLENOSCROLLH) != 0)
si.fMask |= SIF_DISABLENOSCROLL;
si.nPos = m_ptOffset.x;
pT->SetScrollInfo(SB_HORZ, &si, bRedraw);
}
// block: set vertical scroll bar
{
SCROLLINFO si = { sizeof(SCROLLINFO) };
si.fMask = SIF_POS;
if((m_dwExtendedStyle & SCRL_DISABLENOSCROLLV) != 0)
si.fMask |= SIF_DISABLENOSCROLL;
si.nPos = m_ptOffset.y;
pT->SetScrollInfo(SB_VERT, &si, bRedraw);
}
// Move all children if needed
if(IsScrollingChildren() && ((dx != 0) || (dy != 0)))
{
for(HWND hWndChild = ::GetWindow(pT->m_hWnd, GW_CHILD); hWndChild != NULL; hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT))
{
RECT rect = {};
::GetWindowRect(hWndChild, &rect);
::MapWindowPoints(NULL, pT->m_hWnd, (LPPOINT)&rect, 1);
::SetWindowPos(hWndChild, NULL, rect.left + dx, rect.top + dy, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}
}
if(bRedraw)
pT->Invalidate();
}
void SetScrollOffset(POINT ptOffset, BOOL bRedraw = TRUE)
{
SetScrollOffset(ptOffset.x, ptOffset.y, bRedraw);
}
void GetScrollOffset(POINT& ptOffset) const
{
ptOffset = m_ptOffset;
}
// size operations
void SetScrollSize(int cx, int cy, BOOL bRedraw = TRUE, bool bResetOffset = true)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
m_sizeAll.cx = cx;
m_sizeAll.cy = cy;
int x = 0;
int y = 0;
if(!bResetOffset)
{
x = m_ptOffset.x;
y = m_ptOffset.y;
pT->AdjustScrollOffset(x, y);
}
int dx = m_ptOffset.x - x;
int dy = m_ptOffset.y - y;
m_ptOffset.x = x;
m_ptOffset.y = y;
// block: set horizontal scroll bar
{
SCROLLINFO si = { sizeof(SCROLLINFO) };
si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
if((m_dwExtendedStyle & SCRL_DISABLENOSCROLLH) != 0)
si.fMask |= SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = m_sizeAll.cx - 1;
si.nPage = m_sizeClient.cx;
si.nPos = m_ptOffset.x;
pT->SetScrollInfo(SB_HORZ, &si, bRedraw);
}
// block: set vertical scroll bar
{
SCROLLINFO si = { sizeof(SCROLLINFO) };
si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
if((m_dwExtendedStyle & SCRL_DISABLENOSCROLLV) != 0)
si.fMask |= SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = m_sizeAll.cy - 1;
si.nPage = m_sizeClient.cy;
si.nPos = m_ptOffset.y;
pT->SetScrollInfo(SB_VERT, &si, bRedraw);
}
// Move all children if needed
if(IsScrollingChildren() && ((dx != 0) || (dy != 0)))
{
for(HWND hWndChild = ::GetWindow(pT->m_hWnd, GW_CHILD); hWndChild != NULL; hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT))
{
RECT rect = {};
::GetWindowRect(hWndChild, &rect);
::MapWindowPoints(NULL, pT->m_hWnd, (LPPOINT)&rect, 1);
::SetWindowPos(hWndChild, NULL, rect.left + dx, rect.top + dy, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}
}
SetScrollLine(0, 0);
SetScrollPage(0, 0);
if(bRedraw)
pT->Invalidate();
}
void SetScrollSize(SIZE size, BOOL bRedraw = TRUE, bool bResetOffset = true)
{
SetScrollSize(size.cx, size.cy, bRedraw, bResetOffset);
}
void GetScrollSize(SIZE& sizeWnd) const
{
sizeWnd = m_sizeAll;
}
// line operations
void SetScrollLine(int cxLine, int cyLine)
{
ATLASSERT((cxLine >= 0) && (cyLine >= 0));
ATLASSERT((m_sizeAll.cx != 0) && (m_sizeAll.cy != 0));
m_sizeLine.cx = T::CalcLineOrPage(cxLine, m_sizeAll.cx, 100);
m_sizeLine.cy = T::CalcLineOrPage(cyLine, m_sizeAll.cy, 100);
}
void SetScrollLine(SIZE sizeLine)
{
SetScrollLine(sizeLine.cx, sizeLine.cy);
}
void GetScrollLine(SIZE& sizeLine) const
{
sizeLine = m_sizeLine;
}
// page operations
void SetScrollPage(int cxPage, int cyPage)
{
ATLASSERT((cxPage >= 0) && (cyPage >= 0));
ATLASSERT((m_sizeAll.cx != 0) && (m_sizeAll.cy != 0));
m_sizePage.cx = T::CalcLineOrPage(cxPage, m_sizeAll.cx, 10);
m_sizePage.cy = T::CalcLineOrPage(cyPage, m_sizeAll.cy, 10);
}
void SetScrollPage(SIZE sizePage)
{
SetScrollPage(sizePage.cx, sizePage.cy);
}
void GetScrollPage(SIZE& sizePage) const
{
sizePage = m_sizePage;
}
// commands
void ScrollLineDown()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_VERT, SB_LINEDOWN, (int&)m_ptOffset.y, m_sizeAll.cy, m_sizePage.cy, m_sizeLine.cy);
}
void ScrollLineUp()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_VERT, SB_LINEUP, (int&)m_ptOffset.y, m_sizeAll.cy, m_sizePage.cy, m_sizeLine.cy);
}
void ScrollPageDown()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_VERT, SB_PAGEDOWN, (int&)m_ptOffset.y, m_sizeAll.cy, m_sizePage.cy, m_sizeLine.cy);
}
void ScrollPageUp()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_VERT, SB_PAGEUP, (int&)m_ptOffset.y, m_sizeAll.cy, m_sizePage.cy, m_sizeLine.cy);
}
void ScrollTop()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_VERT, SB_TOP, (int&)m_ptOffset.y, m_sizeAll.cy, m_sizePage.cy, m_sizeLine.cy);
}
void ScrollBottom()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_VERT, SB_BOTTOM, (int&)m_ptOffset.y, m_sizeAll.cy, m_sizePage.cy, m_sizeLine.cy);
}
void ScrollLineRight()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_HORZ, SB_LINEDOWN, (int&)m_ptOffset.x, m_sizeAll.cx, m_sizePage.cx, m_sizeLine.cx);
}
void ScrollLineLeft()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_HORZ, SB_LINEUP, (int&)m_ptOffset.x, m_sizeAll.cx, m_sizePage.cx, m_sizeLine.cx);
}
void ScrollPageRight()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_HORZ, SB_PAGEDOWN, (int&)m_ptOffset.x, m_sizeAll.cx, m_sizePage.cx, m_sizeLine.cx);
}
void ScrollPageLeft()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_HORZ, SB_PAGEUP, (int&)m_ptOffset.x, m_sizeAll.cx, m_sizePage.cx, m_sizeLine.cx);
}
void ScrollAllLeft()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_HORZ, SB_TOP, (int&)m_ptOffset.x, m_sizeAll.cx, m_sizePage.cx, m_sizeLine.cx);
}
void ScrollAllRight()
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_HORZ, SB_BOTTOM, (int&)m_ptOffset.x, m_sizeAll.cx, m_sizePage.cx, m_sizeLine.cx);
}
// scroll to make point/view/window visible
void ScrollToView(POINT pt)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
RECT rect = { pt.x, pt.y, pt.x, pt.y };
pT->ScrollToView(rect);
}
void ScrollToView(RECT& rect)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
RECT rcClient = {};
pT->GetClientRect(&rcClient);
int x = m_ptOffset.x;
if(rect.left < m_ptOffset.x)
x = rect.left;
else if(rect.right > (m_ptOffset.x + rcClient.right))
x = rect.right - rcClient.right;
int y = m_ptOffset.y;
if(rect.top < m_ptOffset.y)
y = rect.top;
else if(rect.bottom > (m_ptOffset.y + rcClient.bottom))
y = rect.bottom - rcClient.bottom;
SetScrollOffset(x, y);
}
void ScrollToView(HWND hWnd)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
RECT rect = {};
::GetWindowRect(hWnd, &rect);
::OffsetRect(&rect, m_ptOffset.x, m_ptOffset.y);
::MapWindowPoints(NULL, pT->m_hWnd, (LPPOINT)&rect, 2);
ScrollToView(rect);
}
BEGIN_MSG_MAP(CScrollImpl)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_VSCROLL, OnVScroll)
MESSAGE_HANDLER(WM_HSCROLL, OnHScroll)
MESSAGE_HANDLER(WM_MOUSEWHEEL, OnMouseWheel)
MESSAGE_HANDLER(WM_MOUSEHWHEEL, OnMouseHWheel)
MESSAGE_HANDLER(WM_SETTINGCHANGE, OnSettingChange)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
// standard scroll commands
ALT_MSG_MAP(1)
COMMAND_ID_HANDLER(ID_SCROLL_UP, OnScrollUp)
COMMAND_ID_HANDLER(ID_SCROLL_DOWN, OnScrollDown)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_UP, OnScrollPageUp)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_DOWN, OnScrollPageDown)
COMMAND_ID_HANDLER(ID_SCROLL_TOP, OnScrollTop)
COMMAND_ID_HANDLER(ID_SCROLL_BOTTOM, OnScrollBottom)
COMMAND_ID_HANDLER(ID_SCROLL_LEFT, OnScrollLeft)
COMMAND_ID_HANDLER(ID_SCROLL_RIGHT, OnScrollRight)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_LEFT, OnScrollPageLeft)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_RIGHT, OnScrollPageRight)
COMMAND_ID_HANDLER(ID_SCROLL_ALL_LEFT, OnScrollAllLeft)
COMMAND_ID_HANDLER(ID_SCROLL_ALL_RIGHT, OnScrollAllRight)
END_MSG_MAP()
LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
T* pT = static_cast<T*>(this);
pT->GetSystemSettings();
bHandled = FALSE;
return 1;
}
LRESULT OnVScroll(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_VERT, (int)(short)LOWORD(wParam), (int&)m_ptOffset.y, m_sizeAll.cy, m_sizePage.cy, m_sizeLine.cy);
return 0;
}
LRESULT OnHScroll(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoScroll(SB_HORZ, (int)(short)LOWORD(wParam), (int&)m_ptOffset.x, m_sizeAll.cx, m_sizePage.cx, m_sizeLine.cx);
return 0;
}
LRESULT OnMouseWheel(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
int zDelta = (int)GET_WHEEL_DELTA_WPARAM(wParam);
int nScrollCode = (m_nWheelLines == WHEEL_PAGESCROLL) ? ((zDelta > 0) ? SB_PAGEUP : SB_PAGEDOWN) : ((zDelta > 0) ? SB_LINEUP : SB_LINEDOWN);
m_zDelta += zDelta; // cumulative
int zTotal = (m_nWheelLines == WHEEL_PAGESCROLL) ? abs(m_zDelta) : abs(m_zDelta) * m_nWheelLines;
if(m_sizeAll.cy > m_sizeClient.cy)
{
for(int i = 0; i < zTotal; i += WHEEL_DELTA)
{
pT->DoScroll(SB_VERT, nScrollCode, (int&)m_ptOffset.y, m_sizeAll.cy, m_sizePage.cy, m_sizeLine.cy);
pT->UpdateWindow();
}
}
else if(m_sizeAll.cx > m_sizeClient.cx) // can't scroll vertically, scroll horizontally
{
for(int i = 0; i < zTotal; i += WHEEL_DELTA)
{
pT->DoScroll(SB_HORZ, nScrollCode, (int&)m_ptOffset.x, m_sizeAll.cx, m_sizePage.cx, m_sizeLine.cx);
pT->UpdateWindow();
}
}
m_zDelta %= WHEEL_DELTA;
return 0;
}
LRESULT OnMouseHWheel(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
int zDelta = (int)GET_WHEEL_DELTA_WPARAM(wParam);
int nScrollCode = (m_nHWheelChars == WHEEL_PAGESCROLL) ? ((zDelta > 0) ? SB_PAGERIGHT : SB_PAGELEFT) : ((zDelta > 0) ? SB_LINERIGHT : SB_LINELEFT);
m_zHDelta += zDelta; // cumulative
int zTotal = (m_nHWheelChars == WHEEL_PAGESCROLL) ? abs(m_zHDelta) : abs(m_zHDelta) * m_nHWheelChars;
if(m_sizeAll.cx > m_sizeClient.cx)
{
for(int i = 0; i < zTotal; i += WHEEL_DELTA)
{
pT->DoScroll(SB_HORZ, nScrollCode, (int&)m_ptOffset.x, m_sizeAll.cx, m_sizePage.cx, m_sizeLine.cx);
pT->UpdateWindow();
}
}
m_zHDelta %= WHEEL_DELTA;
return 0;
}
LRESULT OnSettingChange(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
GetSystemSettings();
return 0;
}
LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
pT->DoSize(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
bHandled = FALSE;
return 1;
}
LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
if(wParam != NULL)
{
CDCHandle dc = (HDC)wParam;
POINT ptViewportOrg = { 0, 0 };
dc.SetViewportOrg(-m_ptOffset.x, -m_ptOffset.y, &ptViewportOrg);
pT->DoPaint(dc);
dc.SetViewportOrg(ptViewportOrg);
}
else
{
CPaintDC dc(pT->m_hWnd);
dc.SetViewportOrg(-m_ptOffset.x, -m_ptOffset.y);
pT->DoPaint(dc.m_hDC);
}
return 0;
}
// scrolling handlers
LRESULT OnScrollUp(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollLineUp();
return 0;
}
LRESULT OnScrollDown(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollLineDown();
return 0;
}
LRESULT OnScrollPageUp(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollPageUp();
return 0;
}
LRESULT OnScrollPageDown(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollPageDown();
return 0;
}
LRESULT OnScrollTop(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollTop();
return 0;
}
LRESULT OnScrollBottom(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollBottom();
return 0;
}
LRESULT OnScrollLeft(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollLineLeft();
return 0;
}
LRESULT OnScrollRight(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollLineRight();
return 0;
}
LRESULT OnScrollPageLeft(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollPageLeft();
return 0;
}
LRESULT OnScrollPageRight(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollPageRight();
return 0;
}
LRESULT OnScrollAllLeft(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollAllLeft();
return 0;
}
LRESULT OnScrollAllRight(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
ScrollAllRight();
return 0;
}
// Overrideables
void DoPaint(CDCHandle /*dc*/)
{
// must be implemented in a derived class
ATLASSERT(FALSE);
}
// Implementation
void DoSize(int cx, int cy)
{
m_sizeClient.cx = cx;
m_sizeClient.cy = cy;
T* pT = static_cast<T*>(this);
// block: set horizontal scroll bar
{
SCROLLINFO si = { sizeof(SCROLLINFO) };
si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
si.nMin = 0;
si.nMax = m_sizeAll.cx - 1;
if((m_dwExtendedStyle & SCRL_DISABLENOSCROLLH) != 0)
si.fMask |= SIF_DISABLENOSCROLL;
si.nPage = m_sizeClient.cx;
si.nPos = m_ptOffset.x;
pT->SetScrollInfo(SB_HORZ, &si, TRUE);
}
// block: set vertical scroll bar
{
SCROLLINFO si = { sizeof(SCROLLINFO) };
si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
si.nMin = 0;
si.nMax = m_sizeAll.cy - 1;
if((m_dwExtendedStyle & SCRL_DISABLENOSCROLLV) != 0)
si.fMask |= SIF_DISABLENOSCROLL;
si.nPage = m_sizeClient.cy;
si.nPos = m_ptOffset.y;
pT->SetScrollInfo(SB_VERT, &si, TRUE);
}
int x = m_ptOffset.x;
int y = m_ptOffset.y;
if(pT->AdjustScrollOffset(x, y))
{
// Children will be moved in SetScrollOffset, if needed
pT->ScrollWindowEx(m_ptOffset.x - x, m_ptOffset.y - y, (m_uScrollFlags & ~SCRL_SCROLLCHILDREN));
SetScrollOffset(x, y, FALSE);
}
}
void DoScroll(int nType, int nScrollCode, int& cxyOffset, int cxySizeAll, int cxySizePage, int cxySizeLine)
{
T* pT = static_cast<T*>(this);
RECT rect = {};
pT->GetClientRect(&rect);
int cxyClient = (nType == SB_VERT) ? rect.bottom : rect.right;
int cxyMax = cxySizeAll - cxyClient;
if(cxyMax < 0) // can't scroll, client area is bigger
return;
bool bUpdate = true;
int cxyScroll = 0;
switch(nScrollCode)
{
case SB_TOP: // top or all left
cxyScroll = cxyOffset;
cxyOffset = 0;
break;
case SB_BOTTOM: // bottom or all right
cxyScroll = cxyOffset - cxyMax;
cxyOffset = cxyMax;
break;
case SB_LINEUP: // line up or line left
if(cxyOffset >= cxySizeLine)
{
cxyScroll = cxySizeLine;
cxyOffset -= cxySizeLine;
}
else
{
cxyScroll = cxyOffset;
cxyOffset = 0;
}
break;
case SB_LINEDOWN: // line down or line right
if(cxyOffset < cxyMax - cxySizeLine)
{
cxyScroll = -cxySizeLine;
cxyOffset += cxySizeLine;
}
else
{
cxyScroll = cxyOffset - cxyMax;
cxyOffset = cxyMax;
}
break;
case SB_PAGEUP: // page up or page left
if(cxyOffset >= cxySizePage)
{
cxyScroll = cxySizePage;
cxyOffset -= cxySizePage;
}
else
{
cxyScroll = cxyOffset;
cxyOffset = 0;
}
break;
case SB_PAGEDOWN: // page down or page right
if(cxyOffset < cxyMax - cxySizePage)
{
cxyScroll = -cxySizePage;
cxyOffset += cxySizePage;
}
else
{
cxyScroll = cxyOffset - cxyMax;
cxyOffset = cxyMax;
}
break;
case SB_THUMBTRACK:
if(IsNoThumbTracking())
break;
// else fall through
case SB_THUMBPOSITION:
{
SCROLLINFO si = { sizeof(SCROLLINFO), SIF_TRACKPOS };
if(pT->GetScrollInfo(nType, &si))
{
cxyScroll = cxyOffset - si.nTrackPos;
cxyOffset = si.nTrackPos;
}
}
break;
case SB_ENDSCROLL:
default:
bUpdate = false;
break;
}
if(bUpdate && (cxyScroll != 0))
{
pT->SetScrollPos(nType, cxyOffset, TRUE);
if(nType == SB_VERT)
pT->ScrollWindowEx(0, cxyScroll, m_uScrollFlags);
else
pT->ScrollWindowEx(cxyScroll, 0, m_uScrollFlags);
}
}
static int CalcLineOrPage(int nVal, int nMax, int nDiv)
{
if(nVal == 0)
{
nVal = nMax / nDiv;
if(nVal < 1)
nVal = 1;
}
else if(nVal > nMax)
{
nVal = nMax;
}
return nVal;
}
bool AdjustScrollOffset(int& x, int& y)
{
int xOld = x;
int yOld = y;
int cxMax = m_sizeAll.cx - m_sizeClient.cx;
if(x > cxMax)
x = (cxMax >= 0) ? cxMax : 0;
else if(x < 0)
x = 0;
int cyMax = m_sizeAll.cy - m_sizeClient.cy;
if(y > cyMax)
y = (cyMax >= 0) ? cyMax : 0;
else if(y < 0)
y = 0;
return ((x != xOld) || (y != yOld));
}
void GetSystemSettings()
{
::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &m_nWheelLines, 0);
#ifndef SPI_GETWHEELSCROLLCHARS
const UINT SPI_GETWHEELSCROLLCHARS = 0x006C;
#endif
::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &m_nHWheelChars, 0);
}
bool IsScrollingChildren() const
{
return (m_dwExtendedStyle & SCRL_SCROLLCHILDREN) != 0;
}
bool IsErasingBackground() const
{
return (m_dwExtendedStyle & SCRL_ERASEBACKGROUND) != 0;
}
bool IsNoThumbTracking() const
{
return (m_dwExtendedStyle & SCRL_NOTHUMBTRACKING) != 0;
}
bool IsSmoothScroll() const
{
return (m_dwExtendedStyle & SCRL_SMOOTHSCROLL) != 0;
}
};
///////////////////////////////////////////////////////////////////////////////
// CScrollWindowImpl - Implements a scrollable window
template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
class ATL_NO_VTABLE CScrollWindowImpl : public ATL::CWindowImpl<T, TBase, TWinTraits>, public CScrollImpl< T >
{
public:
BOOL SubclassWindow(HWND hWnd)
{
BOOL bRet = ATL::CWindowImpl< T, TBase, TWinTraits >::SubclassWindow(hWnd);
if(bRet != FALSE)
{
T* pT = static_cast<T*>(this);
pT->GetSystemSettings();
RECT rect = {};
this->GetClientRect(&rect);
pT->DoSize(rect.right, rect.bottom);
}
return bRet;
}
BEGIN_MSG_MAP(CScrollWindowImpl)
MESSAGE_HANDLER(WM_VSCROLL, CScrollImpl< T >::OnVScroll)
MESSAGE_HANDLER(WM_HSCROLL, CScrollImpl< T >::OnHScroll)
MESSAGE_HANDLER(WM_MOUSEWHEEL, CScrollImpl< T >::OnMouseWheel)
MESSAGE_HANDLER(WM_MOUSEHWHEEL, CScrollImpl< T >::OnMouseHWheel)
MESSAGE_HANDLER(WM_SETTINGCHANGE, CScrollImpl< T >::OnSettingChange)
MESSAGE_HANDLER(WM_SIZE, CScrollImpl< T >::OnSize)
MESSAGE_HANDLER(WM_PAINT, CScrollImpl< T >::OnPaint)
MESSAGE_HANDLER(WM_PRINTCLIENT, CScrollImpl< T >::OnPaint)
ALT_MSG_MAP(1)
COMMAND_ID_HANDLER(ID_SCROLL_UP, CScrollImpl< T >::OnScrollUp)
COMMAND_ID_HANDLER(ID_SCROLL_DOWN, CScrollImpl< T >::OnScrollDown)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_UP, CScrollImpl< T >::OnScrollPageUp)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_DOWN, CScrollImpl< T >::OnScrollPageDown)
COMMAND_ID_HANDLER(ID_SCROLL_TOP, CScrollImpl< T >::OnScrollTop)
COMMAND_ID_HANDLER(ID_SCROLL_BOTTOM, CScrollImpl< T >::OnScrollBottom)
COMMAND_ID_HANDLER(ID_SCROLL_LEFT, CScrollImpl< T >::OnScrollLeft)
COMMAND_ID_HANDLER(ID_SCROLL_RIGHT, CScrollImpl< T >::OnScrollRight)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_LEFT, CScrollImpl< T >::OnScrollPageLeft)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_RIGHT, CScrollImpl< T >::OnScrollPageRight)
COMMAND_ID_HANDLER(ID_SCROLL_ALL_LEFT, CScrollImpl< T >::OnScrollAllLeft)
COMMAND_ID_HANDLER(ID_SCROLL_ALL_RIGHT, CScrollImpl< T >::OnScrollAllRight)
END_MSG_MAP()
};
///////////////////////////////////////////////////////////////////////////////
// CMapScrollImpl - Provides mapping and scrolling support to any window
template <class T>
class CMapScrollImpl : public CScrollImpl< T >
{
public:
int m_nMapMode;
RECT m_rectLogAll;
SIZE m_sizeLogLine;
SIZE m_sizeLogPage;
// Constructor
CMapScrollImpl() : m_nMapMode(MM_TEXT)
{
::SetRectEmpty(&m_rectLogAll);
m_sizeLogPage.cx = 0;
m_sizeLogPage.cy = 0;
m_sizeLogLine.cx = 0;
m_sizeLogLine.cy = 0;
}
// Attributes & Operations
// mapping mode operations
void SetScrollMapMode(int nMapMode)
{
ATLASSERT((nMapMode >= MM_MIN) && (nMapMode <= MM_MAX_FIXEDSCALE));
m_nMapMode = nMapMode;
}
int GetScrollMapMode() const
{
ATLASSERT((m_nMapMode >= MM_MIN) && (m_nMapMode <= MM_MAX_FIXEDSCALE));
return m_nMapMode;
}
// offset operations
void SetScrollOffset(int x, int y, BOOL bRedraw = TRUE)
{
ATLASSERT((m_nMapMode >= MM_MIN) && (m_nMapMode <= MM_MAX_FIXEDSCALE));
POINT ptOff = { x, y };
// block: convert logical to device units
{
CWindowDC dc(NULL);
dc.SetMapMode(m_nMapMode);
dc.LPtoDP(&ptOff);
}
CScrollImpl< T >::SetScrollOffset(ptOff, bRedraw);
}
void SetScrollOffset(POINT ptOffset, BOOL bRedraw = TRUE)
{
SetScrollOffset(ptOffset.x, ptOffset.y, bRedraw);
}
void GetScrollOffset(POINT& ptOffset) const
{
ATLASSERT((m_nMapMode >= MM_MIN) && (m_nMapMode <= MM_MAX_FIXEDSCALE));
ptOffset = this->m_ptOffset;
// block: convert device to logical units
{
CWindowDC dc(NULL);
dc.SetMapMode(m_nMapMode);
dc.DPtoLP(&ptOffset);
}
}
// size operations
void SetScrollSize(int xMin, int yMin, int xMax, int yMax, BOOL bRedraw = TRUE, bool bResetOffset = true)
{
ATLASSERT((xMax > xMin) && (yMax > yMin));
ATLASSERT((m_nMapMode >= MM_MIN) && (m_nMapMode <= MM_MAX_FIXEDSCALE));
::SetRect(&m_rectLogAll, xMin, yMin, xMax, yMax);
SIZE sizeAll = {};
sizeAll.cx = xMax - xMin + 1;
sizeAll.cy = yMax - yMin + 1;
// block: convert logical to device units
{
CWindowDC dc(NULL);
dc.SetMapMode(m_nMapMode);
dc.LPtoDP(&sizeAll);
}
CScrollImpl< T >::SetScrollSize(sizeAll, bRedraw, bResetOffset);
SetScrollLine(0, 0);
SetScrollPage(0, 0);
}
void SetScrollSize(RECT& rcScroll, BOOL bRedraw = TRUE, bool bResetOffset = true)
{
SetScrollSize(rcScroll.left, rcScroll.top, rcScroll.right, rcScroll.bottom, bRedraw, bResetOffset);
}
void SetScrollSize(int cx, int cy, BOOL bRedraw = TRUE, bool bResetOffset = true)
{
SetScrollSize(0, 0, cx, cy, bRedraw, bResetOffset);
}
void SetScrollSize(SIZE size, BOOL bRedraw = TRUE, bool bResetOffset = true)
{