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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
|
// Windows Template Library - WTL version 8.1
// Copyright (C) Microsoft Corporation. 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
// Common Public License 1.0 (http://opensource.org/licenses/cpl1.0.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
#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
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
#include <zmouse.h>
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
#ifndef GET_WHEEL_DELTA_WPARAM
#define GET_WHEEL_DELTA_WPARAM(wParam) ((short)HIWORD(wParam))
#endif
#ifndef WM_MOUSEHWHEEL
#define WM_MOUSEHWHEEL 0x020E
#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
#if (WINVER >= 0x0500)
#define SCRL_SMOOTHSCROLL 0x00000008
#endif // (WINVER >= 0x0500)
#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
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
// Note that this message must be forwarded from a top level window
UINT m_uMsgMouseWheel; // MSH_MOUSEWHEEL
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
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),
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
m_uMsgMouseWheel(0U),
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
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);
pT; // avoid level 4 warning
m_uScrollFlags = pT->uSCROLL_FLAGS | (IsScrollingChildren() ? SW_SCROLLCHILDREN : 0) | (IsErasingBackground() ? SW_ERASE : 0);
#if (WINVER >= 0x0500) && !defined(_WIN32_WCE)
m_uScrollFlags |= (IsSmoothScroll() ? SW_SMOOTHSCROLL : 0);
#endif // (WINVER >= 0x0500) && !defined(_WIN32_WCE)
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 = { 0 };
::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 = { 0 };
::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 = { 0 };
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 = { 0 };
::GetWindowRect(hWnd, &rect);
::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)
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
MESSAGE_HANDLER(m_uMsgMouseWheel, OnMouseWheel)
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
MESSAGE_HANDLER(WM_MOUSEHWHEEL, OnMouseHWheel)
MESSAGE_HANDLER(WM_SETTINGCHANGE, OnSettingChange)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
#ifndef _WIN32_WCE
MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
#endif // !_WIN32_WCE
// 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)
{
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));
#if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400) || defined(_WIN32_WCE)
uMsg;
int zDelta = (int)GET_WHEEL_DELTA_WPARAM(wParam);
#else
int zDelta = (uMsg == WM_MOUSEWHEEL) ? (int)GET_WHEEL_DELTA_WPARAM(wParam) : (int)wParam;
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400) || defined(_WIN32_WCE))
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 // 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));
m_sizeClient.cx = GET_X_LPARAM(lParam);
m_sizeClient.cy = GET_Y_LPARAM(lParam);
// 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);
}
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 DoScroll(int nType, int nScrollCode, int& cxyOffset, int cxySizeAll, int cxySizePage, int cxySizeLine)
{
T* pT = static_cast<T*>(this);
RECT rect = { 0 };
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()
{
#ifndef _WIN32_WCE
#ifndef SPI_GETWHEELSCROLLLINES
const UINT SPI_GETWHEELSCROLLLINES = 104;
#endif // !SPI_GETWHEELSCROLLLINES
::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &m_nWheelLines, 0);
#ifndef SPI_GETWHEELSCROLLCHARS
const UINT SPI_GETWHEELSCROLLCHARS = 0x006C;
#endif // !SPI_GETWHEELSCROLLCHARS
::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &m_nHWheelChars, 0);
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
if(m_uMsgMouseWheel != 0)
m_uMsgMouseWheel = ::RegisterWindowMessage(MSH_MOUSEWHEEL);
HWND hWndWheel = FindWindow(MSH_WHEELMODULE_CLASS, MSH_WHEELMODULE_TITLE);
if(::IsWindow(hWndWheel))
{
UINT uMsgScrollLines = ::RegisterWindowMessage(MSH_SCROLL_LINES);
if(uMsgScrollLines != 0)
m_nWheelLines = (int)::SendMessage(hWndWheel, uMsgScrollLines, 0, 0L);
}
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
#endif // !_WIN32_WCE
}
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;
}
#if (WINVER >= 0x0500)
bool IsSmoothScroll() const
{
return (m_dwExtendedStyle & SCRL_SMOOTHSCROLL) != 0;
}
#endif // (WINVER >= 0x0500)
};
///////////////////////////////////////////////////////////////////////////////
// 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:
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)
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
MESSAGE_HANDLER(m_uMsgMouseWheel, CScrollImpl< T >::OnMouseWheel)
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)) && !defined(_WIN32_WCE)
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)
#ifndef _WIN32_WCE
MESSAGE_HANDLER(WM_PRINTCLIENT, CScrollImpl< T >::OnPaint)
#endif // !_WIN32_WCE
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
#ifndef _WIN32_WCE
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 = 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 = { 0 };
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)
{
SetScrollSize(0, 0, size.cx, size.cy, bRedraw, bResetOffset);
}
void GetScrollSize(RECT& rcScroll) const
{
ATLASSERT(m_nMapMode >= MM_MIN && m_nMapMode <= MM_MAX_FIXEDSCALE);
rcScroll = m_rectLogAll;
}
// line operations
void SetScrollLine(int cxLine, int cyLine)
{
ATLASSERT(cxLine >= 0 && cyLine >= 0);
ATLASSERT(m_nMapMode >= MM_MIN && m_nMapMode <= MM_MAX_FIXEDSCALE);
m_sizeLogLine.cx = cxLine;
m_sizeLogLine.cy = cyLine;
SIZE sizeLine = m_sizeLogLine;
// block: convert logical to device units
{
CWindowDC dc(NULL);
dc.SetMapMode(m_nMapMode);
dc.LPtoDP(&sizeLine);
}
CScrollImpl< T >::SetScrollLine(sizeLine);
}
void SetScrollLine(SIZE sizeLine)
{
SetScrollLine(sizeLine.cx, sizeLine.cy);
}
void GetScrollLine(SIZE& sizeLine) const
{
ATLASSERT(m_nMapMode >= MM_MIN && m_nMapMode <= MM_MAX_FIXEDSCALE);
sizeLine = m_sizeLogLine;
}
// page operations
void SetScrollPage(int cxPage, int cyPage)
{
ATLASSERT(cxPage >= 0 && cyPage >= 0);
ATLASSERT(m_nMapMode >= MM_MIN && m_nMapMode <= MM_MAX_FIXEDSCALE);
m_sizeLogPage.cx = cxPage;
m_sizeLogPage.cy = cyPage;
SIZE sizePage = m_sizeLogPage;
// block: convert logical to device units
{
CWindowDC dc(NULL);
dc.SetMapMode(m_nMapMode);
dc.LPtoDP(&sizePage);
}
CScrollImpl< T >::SetScrollPage(sizePage);
}
void SetScrollPage(SIZE sizePage)
{
SetScrollPage(sizePage.cx, sizePage.cy);
}
void GetScrollPage(SIZE& sizePage) const
{
ATLASSERT(m_nMapMode >= MM_MIN && m_nMapMode <= MM_MAX_FIXEDSCALE);
sizePage = m_sizeLogPage;
}
BEGIN_MSG_MAP(CMapScrollImpl)
MESSAGE_HANDLER(WM_VSCROLL, CScrollImpl< T >::OnVScroll)
MESSAGE_HANDLER(WM_HSCROLL, CScrollImpl< T >::OnHScroll)
MESSAGE_HANDLER(WM_MOUSEWHEEL, CScrollImpl< T >::OnMouseWheel)
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
MESSAGE_HANDLER(m_uMsgMouseWheel, CScrollImpl< T >::OnMouseWheel)
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
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, OnPaint)
MESSAGE_HANDLER(WM_PRINTCLIENT, 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()
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;
int nMapModeSav = dc.GetMapMode();
dc.SetMapMode(m_nMapMode);
POINT ptViewportOrg = { 0, 0 };
if(m_nMapMode == MM_TEXT)
dc.SetViewportOrg(-m_ptOffset.x, -m_ptOffset.y, &ptViewportOrg);
else
dc.SetViewportOrg(-m_ptOffset.x, -m_ptOffset.y + m_sizeAll.cy, &ptViewportOrg);
POINT ptWindowOrg = { 0, 0 };
dc.SetWindowOrg(m_rectLogAll.left, m_rectLogAll.top, &ptWindowOrg);
pT->DoPaint(dc);
dc.SetMapMode(nMapModeSav);
dc.SetViewportOrg(ptViewportOrg);
dc.SetWindowOrg(ptWindowOrg);
}
else
{
CPaintDC dc(pT->m_hWnd);
dc.SetMapMode(m_nMapMode);
if(m_nMapMode == MM_TEXT)
dc.SetViewportOrg(-m_ptOffset.x, -m_ptOffset.y);
else
dc.SetViewportOrg(-m_ptOffset.x, -m_ptOffset.y + m_sizeAll.cy);
dc.SetWindowOrg(m_rectLogAll.left, m_rectLogAll.top);
pT->DoPaint(dc.m_hDC);
}
return 0;
}
};
#endif // !_WIN32_WCE
///////////////////////////////////////////////////////////////////////////////
// CMapScrollWindowImpl - Implements scrolling window with mapping
#ifndef _WIN32_WCE
template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
class ATL_NO_VTABLE CMapScrollWindowImpl : public ATL::CWindowImpl< T, TBase, TWinTraits >, public CMapScrollImpl< T >
{
public:
BEGIN_MSG_MAP(CMapScrollWindowImpl)
MESSAGE_HANDLER(WM_VSCROLL, CScrollImpl< T >::OnVScroll)
MESSAGE_HANDLER(WM_HSCROLL, CScrollImpl< T >::OnHScroll)
MESSAGE_HANDLER(WM_MOUSEWHEEL, CScrollImpl< T >::OnMouseWheel)
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
MESSAGE_HANDLER(m_uMsgMouseWheel, CScrollImpl< T >::OnMouseWheel)
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
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, CMapScrollImpl< T >::OnPaint)
MESSAGE_HANDLER(WM_PRINTCLIENT, CMapScrollImpl< 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()
};
#endif // !_WIN32_WCE
///////////////////////////////////////////////////////////////////////////////
// CFSBWindow - Use as a base instead of CWindow to get flat scroll bar support
#if defined(__ATLCTRLS_H__) && (_WIN32_IE >= 0x0400) && !defined(_WIN32_WCE)
template <class TBase = ATL::CWindow>
class CFSBWindowT : public TBase, public CFlatScrollBarImpl<CFSBWindowT< TBase > >
{
public:
// Constructors
CFSBWindowT(HWND hWnd = NULL) : TBase(hWnd)
{ }
CFSBWindowT< TBase >& operator =(HWND hWnd)
{
m_hWnd = hWnd;
return *this;
}
// CWindow overrides that use flat scroll bar API
// (only those methods that are used by scroll window classes)
int SetScrollPos(int nBar, int nPos, BOOL bRedraw = TRUE)
{
ATLASSERT(::IsWindow(m_hWnd));
return FlatSB_SetScrollPos(nBar, nPos, bRedraw);
}
BOOL GetScrollInfo(int nBar, LPSCROLLINFO lpScrollInfo)
{
ATLASSERT(::IsWindow(m_hWnd));
return FlatSB_GetScrollInfo(nBar, lpScrollInfo);
}
BOOL SetScrollInfo(int nBar, LPSCROLLINFO lpScrollInfo, BOOL bRedraw = TRUE)
{
ATLASSERT(::IsWindow(m_hWnd));
return FlatSB_SetScrollInfo(nBar, lpScrollInfo, bRedraw);
}
};
typedef CFSBWindowT<ATL::CWindow> CFSBWindow;
#endif // defined(__ATLCTRLS_H__) && (_WIN32_IE >= 0x0400) && !defined(_WIN32_WCE)
///////////////////////////////////////////////////////////////////////////////
// CZoomScrollImpl - Provides zooming and scrolling support to any window
#ifndef _WIN32_WCE
// The zoom modes that can be set with the SetZoomMode method
enum
{
ZOOMMODE_OFF,
ZOOMMODE_IN, // If left mouse button is clicked or dragged, zoom in on point clicked or rectangle dragged.
ZOOMMODE_OUT // If left mouse button clicked, zoom out on point clicked.
};
// Notification to parent that zoom scale changed as a result of user mouse action.
#define ZSN_ZOOMCHANGED (NM_FIRST - 50)
template <class T>
class CZoomScrollImpl : public CScrollImpl< T >
{
public:
enum { m_cxyMinZoomRect = 12 }; // min rect size to zoom in on rect.
// Data members
SIZE m_sizeLogAll;
SIZE m_sizeLogLine;
SIZE m_sizeLogPage;
float m_fZoomScale;
float m_fZoomScaleMin;
float m_fZoomDelta; // Used in ZOOMMODE_IN and ZOOMMODE_OUT on left-button click.
int m_nZoomMode;
RECT m_rcTrack;
bool m_bTracking;
// Constructor
CZoomScrollImpl():
m_fZoomScale(1.0),
m_fZoomScaleMin(0.5),
m_fZoomDelta(0.5),
m_nZoomMode(ZOOMMODE_OFF),
m_bTracking(false)
{
m_sizeLogAll.cx = 0;
m_sizeLogAll.cy = 0;
m_sizeLogPage.cx = 0;
m_sizeLogPage.cy = 0;
m_sizeLogLine.cx = 0;
m_sizeLogLine.cy = 0;
::SetRectEmpty(&m_rcTrack);
}
// Attributes & Operations
// size operations
void SetScrollSize(int cxLog, int cyLog, BOOL bRedraw = TRUE, bool bResetOffset = true)
{
ATLASSERT(cxLog >= 0 && cyLog >= 0);
// Set up the defaults
if (cxLog == 0 && cyLog == 0)
{
cxLog = 1;
cyLog = 1;
}
m_sizeLogAll.cx = cxLog;
m_sizeLogAll.cy = cyLog;
SIZE sizeAll = { 0 };
sizeAll.cx = (int)((float)m_sizeLogAll.cx * m_fZoomScale);
sizeAll.cy = (int)((float)m_sizeLogAll.cy * m_fZoomScale);
CScrollImpl< T >::SetScrollSize(sizeAll, bRedraw, bResetOffset);
}
void SetScrollSize(SIZE sizeLog, BOOL bRedraw = TRUE, bool bResetOffset = true)
{
SetScrollSize(sizeLog.cx, sizeLog.cy, bRedraw, bResetOffset);
}
void GetScrollSize(SIZE& sizeLog) const
{
sizeLog = m_sizeLogAll;
}
// line operations
void SetScrollLine(int cxLogLine, int cyLogLine)
{
ATLASSERT(cxLogLine >= 0 && cyLogLine >= 0);
m_sizeLogLine.cx = cxLogLine;
m_sizeLogLine.cy = cyLogLine;
SIZE sizeLine = { 0 };
sizeLine.cx = (int)((float)m_sizeLogLine.cx * m_fZoomScale);
sizeLine.cy = (int)((float)m_sizeLogLine.cy * m_fZoomScale);
CScrollImpl< T >::SetScrollLine(sizeLine);
}
void SetScrollLine(SIZE sizeLogLine)
{
SetScrollLine(sizeLogLine.cx, sizeLogLine.cy);
}
void GetScrollLine(SIZE& sizeLogLine) const
{
sizeLogLine = m_sizeLogLine;
}
// page operations
void SetScrollPage(int cxLogPage, int cyLogPage)
{
ATLASSERT(cxLogPage >= 0 && cyLogPage >= 0);
m_sizeLogPage.cx = cxLogPage;
m_sizeLogPage.cy = cyLogPage;
SIZE sizePage = { 0 };
sizePage.cx = (int)((float)m_sizeLogPage.cx * m_fZoomScale);
sizePage.cy = (int)((float)m_sizeLogPage.cy * m_fZoomScale);
CScrollImpl< T >::SetScrollPage(sizePage);
}
void SetScrollPage(SIZE sizeLogPage)
{
SetScrollPage(sizeLogPage.cx, sizeLogPage.cy);
}
void GetScrollPage(SIZE& sizeLogPage) const
{
sizeLogPage = m_sizeLogPage;
}
void SetZoomScale(float fZoomScale)
{
ATLASSERT(fZoomScale > 0);
if(fZoomScale > 0 && fZoomScale >= m_fZoomScaleMin)
m_fZoomScale = fZoomScale;
}
float GetZoomScale() const
{
return m_fZoomScale;
}
void SetZoomScaleMin(float fZoomScaleMin)
{
m_fZoomScaleMin = fZoomScaleMin;
}
float GetZoomScaleMin() const
{
return m_fZoomScaleMin;
}
void SetZoomDelta(float fZoomDelta)
{
ATLASSERT(fZoomDelta >= 0);
if(fZoomDelta >= 0)
m_fZoomDelta = fZoomDelta;
}
float GetZoomDelta() const
{
return m_fZoomDelta;
}
void SetZoomMode(int nZoomMode)
{
m_nZoomMode = nZoomMode;
}
int GetZoomMode() const
{
return m_nZoomMode;
}
void Zoom(int x, int y, float fZoomScale)
{
if(fZoomScale <= 0)
return;
fZoomScale = max(fZoomScale, m_fZoomScaleMin);
T* pT = static_cast<T*>(this);
POINT pt = { x, y };
if(!pT->PtInDevRect(pt))
return;
pT->ViewDPtoLP(&pt);
pT->Zoom(fZoomScale, false);
pT->CenterOnLogicalPoint(pt);
}
void Zoom(POINT pt, float fZoomScale)
{
T* pT = static_cast<T*>(this);
pT->Zoom(pt.x, pt.y, fZoomScale);
}
void Zoom(RECT& rc)
{
T* pT = static_cast<T*>(this);
RECT rcZoom = rc;
pT->NormalizeRect(rcZoom);
SIZE size = { rcZoom.right - rcZoom.left, rcZoom.bottom - rcZoom.top };
POINT pt = { rcZoom.left + size.cx / 2, rcZoom.top + size.cy / 2 };
if(size.cx < m_cxyMinZoomRect || size.cy < m_cxyMinZoomRect)
{
pT->Zoom(pt, m_fZoomScale + m_fZoomDelta);
return;
}
ATLASSERT(size.cx > 0 && size.cy > 0);
float fScaleH = (float)(m_sizeClient.cx + 1) / (float)size.cx;
float fScaleV = (float)(m_sizeClient.cy + 1) / (float)size.cy;
float fZoomScale = min(fScaleH, fScaleV) * m_fZoomScale;
pT->Zoom(pt, fZoomScale);
}
void Zoom(float fZoomScale, bool bCenter = true)
{
if(fZoomScale <= 0)
return;
fZoomScale = max(fZoomScale, m_fZoomScaleMin);
T* pT = static_cast<T*>(this);
POINT pt = { 0 };
if(bCenter)
{
RECT rc;
::GetClientRect(pT->m_hWnd, &rc);
pt.x = rc.right / 2;
pt.y = rc.bottom / 2;
pT->ViewDPtoLP(&pt);
}
// Modify the Viewport extent
m_fZoomScale = fZoomScale;
SIZE sizeAll = { 0 };
sizeAll.cx = (int)((float)m_sizeLogAll.cx * fZoomScale);
sizeAll.cy = (int)((float)m_sizeLogAll.cy * fZoomScale);
// Update scroll bars and window
CScrollImpl< T >::SetScrollSize(sizeAll);
if(bCenter)
pT->CenterOnLogicalPoint(pt);
}
// Helper functions
void PrepareDC(CDCHandle dc)
{
ATLASSERT(m_sizeAll.cx >= 0 && m_sizeAll.cy >= 0);
dc.SetMapMode(MM_ANISOTROPIC);
dc.SetWindowExt(m_sizeLogAll);
dc.SetViewportExt(m_sizeAll);
dc.SetViewportOrg(-m_ptOffset.x, -m_ptOffset.y);
}
void ViewDPtoLP(LPPOINT lpPoints, int nCount = 1)
{
ATLASSERT(lpPoints);
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
CWindowDC dc(pT->m_hWnd);
pT->PrepareDC(dc.m_hDC);
dc.DPtoLP(lpPoints, nCount);
}
void ViewLPtoDP(LPPOINT lpPoints, int nCount = 1)
{
ATLASSERT(lpPoints);
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
CWindowDC dc(pT->m_hWnd);
pT->PrepareDC(dc.m_hDC);
dc.LPtoDP(lpPoints, nCount);
}
void ClientToDevice(POINT &pt)
{
pt.x += m_ptOffset.x;
pt.y += m_ptOffset.y;
}
void DeviceToClient(POINT &pt)
{
pt.x -= m_ptOffset.x;
pt.y -= m_ptOffset.y;
}
void CenterOnPoint(POINT pt)
{
T* pT = static_cast<T*>(this);
RECT rect;
pT->GetClientRect(&rect);
int xOfs = pt.x - (rect.right / 2) + m_ptOffset.x;
if(xOfs < 0)
{
xOfs = 0;
}
else
{
int xMax = max((int)(m_sizeAll.cx - rect.right), 0);
if(xOfs > xMax)
xOfs = xMax;
}
int yOfs = pt.y - (rect.bottom / 2) + m_ptOffset.y;
if(yOfs < 0)
{
yOfs = 0;
}
else
{
int yMax = max((int)(m_sizeAll.cy - rect.bottom), 0);
if(yOfs > yMax)
yOfs = yMax;
}
CScrollImpl< T >::SetScrollOffset(xOfs, yOfs);
}
void CenterOnLogicalPoint(POINT ptLog)
{
T* pT = static_cast<T*>(this);
pT->ViewLPtoDP(&ptLog);
pT->DeviceToClient(ptLog);
pT->CenterOnPoint(ptLog);
}
BOOL PtInDevRect(POINT pt)
{
RECT rc = { 0, 0, m_sizeAll.cx, m_sizeAll.cy };
::OffsetRect(&rc, -m_ptOffset.x, -m_ptOffset.y);
return ::PtInRect(&rc, pt);
}
void NormalizeRect(RECT& rc)
{
if(rc.left > rc.right)
{
int r = rc.right;
rc.right = rc.left;
rc.left = r;
}
if(rc.top > rc.bottom)
{
int b = rc.bottom;
rc.bottom = rc.top;
rc.top = b;
}
}
void DrawTrackRect()
{
T* pT = static_cast<T*>(this);
const SIZE sizeLines = { 2, 2 };
RECT rc = m_rcTrack;
pT->NormalizeRect(rc);
if(!::IsRectEmpty(&rc))
{
::MapWindowPoints(pT->m_hWnd, NULL, (LPPOINT)&rc, 2);
CWindowDC dc(NULL);
dc.DrawDragRect(&rc, sizeLines, NULL, sizeLines);
}
}
void NotifyParentZoomChanged()
{
T* pT = static_cast<T*>(this);
int nId = pT->GetDlgCtrlID();
NMHDR nmhdr = { pT->m_hWnd, nId, ZSN_ZOOMCHANGED };
::SendMessage(pT->GetParent(), WM_NOTIFY, (WPARAM)nId, (LPARAM)&nmhdr);
}
BEGIN_MSG_MAP(CZoomScrollImpl)
MESSAGE_HANDLER(WM_SETCURSOR, OnSetCursor)
MESSAGE_HANDLER(WM_VSCROLL, CScrollImpl< T >::OnVScroll)
MESSAGE_HANDLER(WM_HSCROLL, CScrollImpl< T >::OnHScroll)
MESSAGE_HANDLER(WM_MOUSEWHEEL, CScrollImpl< T >::OnMouseWheel)
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
MESSAGE_HANDLER(m_uMsgMouseWheel, CScrollImpl< T >::OnMouseWheel)
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
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, OnPaint)
MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
MESSAGE_HANDLER(WM_CAPTURECHANGED, OnCaptureChanged)
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()
LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
T* pT = static_cast<T*>(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
ATLASSERT(m_sizeLogAll.cx >= 0 && m_sizeLogAll.cy >= 0);
ATLASSERT(m_sizeAll.cx >= 0 && m_sizeAll.cy >= 0);
if(wParam != NULL)
{
CDCHandle dc = (HDC)wParam;
int nMapModeSav = dc.GetMapMode();
dc.SetMapMode(MM_ANISOTROPIC);
SIZE szWindowExt = { 0, 0 };
dc.SetWindowExt(m_sizeLogAll, &szWindowExt);
SIZE szViewportExt = { 0, 0 };
dc.SetViewportExt(m_sizeAll, &szViewportExt);
POINT ptViewportOrg = { 0, 0 };
dc.SetViewportOrg(-m_ptOffset.x, -m_ptOffset.y, &ptViewportOrg);
pT->DoPaint(dc);
dc.SetMapMode(nMapModeSav);
dc.SetWindowExt(szWindowExt);
dc.SetViewportExt(szViewportExt);
dc.SetViewportOrg(ptViewportOrg);
}
else
{
CPaintDC dc(pT->m_hWnd);
pT->PrepareDC(dc.m_hDC);
pT->DoPaint(dc.m_hDC);
}
return 0;
}
LRESULT OnLButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
{
if(m_nZoomMode == ZOOMMODE_IN && !m_bTracking)
{
T* pT = static_cast<T*>(this);
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
if(pT->PtInDevRect(pt))
{
pT->SetCapture();
m_bTracking = true;
::SetRect(&m_rcTrack, pt.x, pt.y, pt.x, pt.y);
}
}
bHandled = FALSE;
return 0;
}
LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
{
if(m_bTracking)
{
T* pT = static_cast<T*>(this);
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
if(pT->PtInDevRect(pt))
{
pT->DrawTrackRect();
m_rcTrack.right = pt.x;
m_rcTrack.bottom = pt.y;
pT->DrawTrackRect();
}
}
bHandled = FALSE;
return 0;
}
LRESULT OnLButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
{
::ReleaseCapture();
if(m_nZoomMode == ZOOMMODE_OUT)
{
T* pT = static_cast<T*>(this);
pT->Zoom(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), m_fZoomScale - m_fZoomDelta);
pT->NotifyParentZoomChanged();
}
bHandled = FALSE;
return 0;
}
LRESULT OnCaptureChanged(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
if(m_bTracking)
{
m_bTracking = false;
T* pT = static_cast<T*>(this);
pT->DrawTrackRect();
pT->Zoom(m_rcTrack);
pT->NotifyParentZoomChanged();
::SetRectEmpty(&m_rcTrack);
}
bHandled = FALSE;
return 0;
}
LRESULT OnSetCursor(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if(LOWORD(lParam) == HTCLIENT && m_nZoomMode != ZOOMMODE_OFF)
{
T* pT = static_cast<T*>(this);
if((HWND)wParam == pT->m_hWnd)
{
DWORD dwPos = ::GetMessagePos();
POINT pt = { GET_X_LPARAM(dwPos), GET_Y_LPARAM(dwPos) };
pT->ScreenToClient(&pt);
if(pT->PtInDevRect(pt))
{
::SetCursor(::LoadCursor(NULL, IDC_CROSS));
return 1;
}
}
}
bHandled = FALSE;
return 0;
}
};
///////////////////////////////////////////////////////////////////////////////
// CZoomScrollWindowImpl - Implements scrolling window with zooming
template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
class ATL_NO_VTABLE CZoomScrollWindowImpl : public ATL::CWindowImpl< T, TBase, TWinTraits >, public CZoomScrollImpl< T >
{
public:
BEGIN_MSG_MAP(CZoomScrollWindowImpl)
MESSAGE_HANDLER(WM_SETCURSOR, CZoomScrollImpl< T >::OnSetCursor)
MESSAGE_HANDLER(WM_VSCROLL, CScrollImpl< T >::OnVScroll)
MESSAGE_HANDLER(WM_HSCROLL, CScrollImpl< T >::OnHScroll)
MESSAGE_HANDLER(WM_MOUSEWHEEL, CScrollImpl< T >::OnMouseWheel)
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
MESSAGE_HANDLER(m_uMsgMouseWheel, CScrollImpl< T >::OnMouseWheel)
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
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, CZoomScrollImpl< T >::OnPaint)
MESSAGE_HANDLER(WM_PRINTCLIENT, CZoomScrollImpl< T >::OnPaint)
MESSAGE_HANDLER(WM_LBUTTONDOWN, CZoomScrollImpl< T >::OnLButtonDown)
MESSAGE_HANDLER(WM_MOUSEMOVE, CZoomScrollImpl< T >::OnMouseMove)
MESSAGE_HANDLER(WM_LBUTTONUP, CZoomScrollImpl< T >::OnLButtonUp)
MESSAGE_HANDLER(WM_CAPTURECHANGED, CZoomScrollImpl< T >::OnCaptureChanged)
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()
};
#endif // !_WIN32_WCE
///////////////////////////////////////////////////////////////////////////////
// CScrollContainer
template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
class ATL_NO_VTABLE CScrollContainerImpl : public CScrollWindowImpl< T, TBase, TWinTraits >
{
public:
DECLARE_WND_CLASS_EX(NULL, 0, -1)
typedef CScrollWindowImpl< T, TBase, TWinTraits > _baseClass;
// Data members
ATL::CWindow m_wndClient;
bool m_bAutoSizeClient;
bool m_bDrawEdgeIfEmpty;
// Constructor
CScrollContainerImpl() : m_bAutoSizeClient(true), m_bDrawEdgeIfEmpty(false)
{
// Set CScrollWindowImpl extended style
SetScrollExtendedStyle(SCRL_SCROLLCHILDREN);
}
// Attributes
HWND GetClient() const
{
return m_wndClient;
}
HWND SetClient(HWND hWndClient, bool bClientSizeAsMin = true)
{
ATLASSERT(::IsWindow(m_hWnd));
HWND hWndOldClient = m_wndClient;
m_wndClient = hWndClient;
SetRedraw(FALSE);
SetScrollSize(1, 1, FALSE);
if(m_wndClient.m_hWnd != NULL)
{
m_wndClient.SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
if(bClientSizeAsMin)
{
RECT rect = { 0 };
m_wndClient.GetWindowRect(&rect);
if((rect.right - rect.left) > 0 && (rect.bottom - rect.top) > 0)
SetScrollSize(rect.right - rect.left, rect.bottom - rect.top, FALSE);
}
T* pT = static_cast<T*>(this);
pT->UpdateLayout();
}
SetRedraw(TRUE);
RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_FRAME | RDW_UPDATENOW | RDW_ALLCHILDREN);
return hWndOldClient;
}
// Message map and handlers
BEGIN_MSG_MAP(CScrollContainerImpl)
MESSAGE_HANDLER(WM_SETFOCUS, OnSetFocus)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
MESSAGE_HANDLER(WM_SIZE, OnSize)
CHAIN_MSG_MAP(_baseClass)
FORWARD_NOTIFICATIONS()
ALT_MSG_MAP(1)
CHAIN_MSG_MAP_ALT(_baseClass, 1)
END_MSG_MAP()
LRESULT OnSetFocus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
if(m_wndClient.m_hWnd != NULL)
m_wndClient.SetFocus();
return 0;
}
LRESULT OnEraseBackground(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
return 1; // no background needed
}
LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
BOOL bTmp = TRUE;
LRESULT lRet = _baseClass::OnSize(uMsg, wParam, lParam, bTmp);
T* pT = static_cast<T*>(this);
pT->UpdateLayout();
return lRet;
}
// Overrides for CScrollWindowImpl
void DoPaint(CDCHandle dc)
{
if(!m_bAutoSizeClient || m_wndClient.m_hWnd == NULL)
{
T* pT = static_cast<T*>(this);
RECT rect = { 0 };
pT->GetContainerRect(rect);
if(m_bDrawEdgeIfEmpty && m_wndClient.m_hWnd == NULL)
dc.DrawEdge(&rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
dc.FillRect(&rect, COLOR_APPWORKSPACE);
}
}
void ScrollToView(POINT pt)
{
CScrollWindowImpl< T, TBase, TWinTraits >::ScrollToView(pt);
}
void ScrollToView(RECT& rect)
{
CScrollWindowImpl< T, TBase, TWinTraits >::ScrollToView(rect);
}
void ScrollToView(HWND hWnd) // client window coordinates
{
T* pT = static_cast<T*>(this);
pT; // avoid level 4 warning
ATLASSERT(::IsWindow(pT->m_hWnd));
ATLASSERT(m_wndClient.IsWindow());
RECT rect = { 0 };
::GetWindowRect(hWnd, &rect);
::MapWindowPoints(NULL, m_wndClient.m_hWnd, (LPPOINT)&rect, 2);
ScrollToView(rect);
}
// Implementation - overrideable methods
void UpdateLayout()
{
ATLASSERT(::IsWindow(m_hWnd));
if(m_bAutoSizeClient && m_wndClient.m_hWnd != NULL)
{
T* pT = static_cast<T*>(this);
RECT rect = { 0 };
pT->GetContainerRect(rect);
m_wndClient.SetWindowPos(NULL, &rect, SWP_NOZORDER | SWP_NOMOVE);
}
else
{
Invalidate();
}
}
void GetContainerRect(RECT& rect)
{
GetClientRect(&rect);
if(rect.right < m_sizeAll.cx)
rect.right = m_sizeAll.cx;
if(rect.bottom < m_sizeAll.cy)
rect.bottom = m_sizeAll.cy;
}
};
class CScrollContainer : public CScrollContainerImpl<CScrollContainer>
{
public:
DECLARE_WND_CLASS_EX(_T("WTL_ScrollContainer"), 0, -1)
};
}; // namespace WTL
#endif // __ATLSCRL_H__
|