summaryrefslogtreecommitdiff
path: root/plugins/Clist_nicer/src/Coolsb/coolscroll.cpp
blob: 80b0cd12c48ab80b47dad43454379ffd1a097b6d (plain)
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
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
/*
    Cool Scrollbar Library Version 1.2

    Module: coolscroll.c
	Copyright (c) J Brown 2001
	  
	This code is freeware, however, you may not publish
	this code elsewhere or charge any money for it. This code
	is supplied as-is. I make no guarantees about the suitability
	of this code - use at your own risk.
	
	It would be nice if you credited me, in the event
	that you use this code in a product.
	
	VERSION HISTORY:

	 V1.2: TreeView problem fixed by Diego Tartara
		   Small problem in thumbsize calculation also fixed (thanks Diego!)

	 V1.1: Added support for Right-left windows
	       Changed calling convention of APIs to WINAPI (__stdcall)
		   Completely standalone (no need for c-runtime)
		   Now supports ALL windows with appropriate USER32.DLL patching
		    (you provide!!)

	 V1.0: Apr 2001: Initial Version

  IMPORTANT:
	 This whole library is based around code for a horizontal scrollbar.
	 All "vertical" scrollbar drawing / mouse interaction uses the
	 horizontal scrollbar functions, but uses a trick to convert the vertical
	 scrollbar coordinates into horizontal equivelants. When I started this project,
	 I quickly realised that the code for horz/vert bars was IDENTICAL, apart
	 from the fact that horizontal code uses left/right coords, and vertical code
	 uses top/bottom coords. On entry to a "vertical" drawing function, for example,
	 the coordinates are "rotated" before the horizontal function is called, and
	 then rotated back once the function has completed. When something needs to
	 be drawn, the coords are converted back again before drawing.
	 
     This trick greatly reduces the amount of code required, and makes
	 maintanence much simpler. This way, only one function is needed to draw
	 a scrollbar, but this can be used for both horizontal and vertical bars
	 with careful thought.
*/

#include <commonheaders.h>
#include "coolscroll.h"
#include "userdefs.h"
#include "coolsb_internal.h"

//define some values if the new version of common controls
//is not available.
#ifndef NM_CUSTOMDRAW
#define NM_CUSTOMDRAW (NM_FIRST-12)
#define CDRF_DODEFAULT		0x0000
#define CDRF_SKIPDEFAULT	0x0004
#define CDDS_PREPAINT		0x0001
#define CDDS_POSTPAINT		0x0002
#endif

//
//	Special thumb-tracking variables
//
//
static UINT uCurrentScrollbar = COOLSB_NONE;	//SB_HORZ / SB_VERT
static UINT uCurrentScrollPortion = HTSCROLL_NONE;
static UINT uCurrentButton = 0;

static RECT rcThumbBounds;		//area that the scroll thumb can travel in
static int  nThumbSize;			//(pixels)
static int  nThumbPos;			//(pixels)
static int  nThumbMouseOffset;	//(pixels)
static int  nLastPos = -1;		//(scrollbar units)
static int  nThumbPos0;			//(pixels) initial thumb position

//
//	Temporary state used to auto-generate timer messages
//
static UINT_PTR uMouseOverId = 0;
static UINT uMouseOverScrollbar = COOLSB_NONE;
static UINT uHitTestPortion = HTSCROLL_NONE;
static UINT uLastHitTestPortion = HTSCROLL_NONE;
static RECT MouseOverRect;

static UINT uScrollTimerMsg = 0;
static UINT uScrollTimerPortion = HTSCROLL_NONE;
static UINT_PTR uScrollTimerId = 0;
static HWND hwndCurCoolSB = 0;

extern int  CustomDrawScrollBars(NMCSBCUSTOMDRAW *nmcsbcd);

LRESULT CALLBACK CoolSBWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

//
//	Provide this so there are NO dependencies on CRT
//
static void CoolSB_ZeroMemory(void *ptr, DWORD bytes)
{
	BYTE *bptr = (BYTE *)ptr;

	while(bytes--) *bptr++ = 0;
}

BOOL WINAPI CoolSB_IsThumbTracking(HWND hwnd)	
{ 
	SCROLLWND *sw;

	if ((sw = GetScrollWndFromHwnd(hwnd)) == NULL)
		return FALSE;
	else
		return sw->fThumbTracking; 
}

//
//	swap the rectangle's x coords with its y coords
//
static void __stdcall RotateRect(RECT *rect)
{
	int temp;
	temp = rect->left;
	rect->left = rect->top;
	rect->top = temp;

	temp = rect->right;
	rect->right = rect->bottom;
	rect->bottom = temp;
}

//
//	swap the coords if the scrollbar is a SB_VERT
//
static void __stdcall RotateRect0(SCROLLBAR *sb, RECT *rect)
{
	if (sb->nBarType == SB_VERT)
		RotateRect(rect);
}

//
//	Calculate if the SCROLLINFO members produce
//  an enabled or disabled scrollbar
//
static BOOL IsScrollInfoActive(SCROLLINFO *si)
{
	if ((si->nPage > (UINT)si->nMax
		|| si->nMax <= si->nMin || si->nMax == 0))
		return FALSE;
	else
		return TRUE;
}

//
//	Return if the specified scrollbar is enabled or not
//
static BOOL IsScrollbarActive(SCROLLBAR *sb)
{
	SCROLLINFO *si = &sb->scrollInfo;
	if (((sb->fScrollFlags & ESB_DISABLE_BOTH) == ESB_DISABLE_BOTH) ||
		!(sb->fScrollFlags & CSBS_THUMBALWAYS) && !IsScrollInfoActive(si))
		return FALSE;
	else
		return TRUE;
}

//
//	Draw a standard scrollbar arrow
//
static int DrawScrollArrow(SCROLLBAR *sbar, HDC hdc, RECT *rect, UINT arrow, BOOL fMouseDown, BOOL fMouseOver)
{
	UINT ret;
	UINT flags = arrow;

	//HACKY bit so this routine can be called by vertical and horizontal code
	if (sbar->nBarType == SB_VERT)
	{
		if (flags & DFCS_SCROLLLEFT)		flags = flags & ~DFCS_SCROLLLEFT  | DFCS_SCROLLUP;
		if (flags & DFCS_SCROLLRIGHT)	flags = flags & ~DFCS_SCROLLRIGHT | DFCS_SCROLLDOWN;
	}

	if (fMouseDown) flags |= (DFCS_FLAT | DFCS_PUSHED);

#ifdef FLAT_SCROLLBARS
	if (sbar->fFlatScrollbar != CSBS_NORMAL)
	{
		HDC hdcmem1, hdcmem2;
		HBITMAP hbm1, oldbm1;
		HBITMAP hbm2, oldbm2;
		RECT rc;
		int width, height;

		rc = *rect;
		width  = rc.right-rc.left;
		height = rc.bottom-rc.top;
		SetRect(&rc, 0, 0, width, height);

		//MONOCHROME bitmap to convert the arrow to black/white mask
		hdcmem1 = CreateCompatibleDC(hdc);
		hbm1    = CreateBitmap(width, height, 1, 1, NULL);
		UnrealizeObject(hbm1);
		oldbm1  = reinterpret_cast<HBITMAP>(SelectObject(hdcmem1, hbm1));
		

		//NORMAL bitmap to draw the arrow into
		hdcmem2 = CreateCompatibleDC(hdc);
		hbm2    = CreateCompatibleBitmap(hdc, width, height);
		UnrealizeObject(hbm2);
		oldbm2  = reinterpret_cast<HBITMAP>(SelectObject(hdcmem2, hbm2));
		

		flags = flags & ~DFCS_PUSHED | DFCS_FLAT;	//just in case
		DrawFrameControl(hdcmem2, &rc, DFC_SCROLL, flags);


#ifndef HOT_TRACKING
		if (fMouseDown)
		{
			//uncomment these to make the cool scrollbars
			//look like the common controls flat scrollbars
			//fMouseDown = FALSE;
			//fMouseOver = TRUE;
		}
#endif
		//draw a flat monochrome version of a scrollbar arrow (dark)
		if (fMouseDown)
		{
			SetBkColor(hdcmem2, GetSysColor(COLOR_BTNTEXT));
			BitBlt(hdcmem1, 0, 0, width, height, hdcmem2, 0, 0, SRCCOPY);
			SetBkColor(hdc, 0x00ffffff);
			SetTextColor(hdc, GetSysColor(COLOR_3DDKSHADOW));
			BitBlt(hdc, rect->left, rect->top, width, height, hdcmem1, 0, 0, SRCCOPY);
		}
		//draw a flat monochrome version of a scrollbar arrow (grey)
		else if (fMouseOver)
		{
			SetBkColor(hdcmem2, GetSysColor(COLOR_BTNTEXT));
			FillRect(hdcmem1, &rc, reinterpret_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
			BitBlt(hdcmem1, 0, 0, width, height, hdcmem2, 0, 0, SRCINVERT);

			SetBkColor(hdc, GetSysColor(COLOR_3DSHADOW));
			SetTextColor(hdc, 0x00ffffff);
			BitBlt(hdc, rect->left, rect->top, width, height, hdcmem1, 0, 0, SRCCOPY);
		}
		//draw the arrow normally
		else
		{
			BitBlt(hdc, rect->left, rect->top, width, height, hdcmem2, 0, 0, SRCCOPY);
		}

		SelectObject(hdcmem1, oldbm1);
		SelectObject(hdcmem2, oldbm2);
		DeleteObject(hbm1);
		DeleteObject(hbm2);
		DeleteDC(hdcmem1);
		DeleteDC(hdcmem2);

		ret = 0;
	}
	else
#endif
	ret = DrawFrameControl(hdc, rect, DFC_SCROLL, flags);

	return ret;
}

//
//	Return the size in pixels for the specified scrollbar metric,
//  for the specified scrollbar
//
static int GetScrollMetric(SCROLLBAR *sbar, int metric)
{
	if (sbar->nBarType == SB_HORZ)
	{
		if (metric == SM_CXHORZSB)
		{
			if (sbar->nArrowLength < 0)
				return -sbar->nArrowLength * GetSystemMetrics(SM_CXHSCROLL);
			else
				return sbar->nArrowLength;
		}
		else
		{
			if (sbar->nArrowWidth < 0)
				return -sbar->nArrowWidth * GetSystemMetrics(SM_CYHSCROLL);
			else
				return sbar->nArrowWidth;
		}
	}
	else if (sbar->nBarType == SB_VERT)
	{
		if (metric == SM_CYVERTSB)
		{
			if (sbar->nArrowLength < 0)
				return -sbar->nArrowLength * GetSystemMetrics(SM_CYVSCROLL);
			else
				return sbar->nArrowLength;
		}
		else
		{
			if (sbar->nArrowWidth < 0)
				return -sbar->nArrowWidth * GetSystemMetrics(SM_CXVSCROLL);
			else
				return sbar->nArrowWidth;
		}
	}

	return 0;
}

//
//	
//
static COLORREF GetSBForeColor(void)
{
	COLORREF c1 = GetSysColor(COLOR_3DHILIGHT);
	COLORREF c2 = GetSysColor(COLOR_WINDOW);

	if (c1 != 0xffffff && c1 == c2)
	{
		return GetSysColor(COLOR_BTNFACE);
	}
	else
	{
		return GetSysColor(COLOR_3DHILIGHT);
	}
}

static COLORREF GetSBBackColor(void)
{
	return GetSysColor(COLOR_SCROLLBAR);
}

//
//	Paint a checkered rectangle, with each alternate
//	pixel being assigned a different colour
//
static void DrawCheckedRect(HDC hdc, RECT *rect, COLORREF fg, COLORREF bg)
{
	static WORD wCheckPat[8] = 
	{ 
		0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555, 0xaaaa, 0x5555 
	};

	HBITMAP hbmp;
	HBRUSH  hbr, hbrold;
	COLORREF fgold, bgold;

	hbmp = CreateBitmap(8, 8, 1, 1, wCheckPat);
	hbr  = CreatePatternBrush(hbmp);

	UnrealizeObject(hbr);
	SetBrushOrgEx(hdc, rect->left, rect->top, 0);

	hbrold = (HBRUSH)SelectObject(hdc, hbr);

	fgold = SetTextColor(hdc, fg);
	bgold = SetBkColor(hdc, bg);
	
	PatBlt(hdc, rect->left, rect->top, 
				rect->right - rect->left, 
				rect->bottom - rect->top, 
				PATCOPY);
	
	SetBkColor(hdc, bgold);
	SetTextColor(hdc, fgold);
	
	SelectObject(hdc, hbrold);
	DeleteObject(hbr);
	DeleteObject(hbmp);
}

//
//	Fill the specifed rectangle using a solid colour
//
static void PaintRect(HDC hdc, RECT *rect, COLORREF color)
{
	COLORREF oldcol = SetBkColor(hdc, color);
	ExtTextOutA(hdc, 0, 0, ETO_OPAQUE, rect, "", 0, NULL);
	SetBkColor(hdc, oldcol);
}

//
//	Draw a simple blank scrollbar push-button. Can be used
//	to draw a push button, or the scrollbar thumb
//	drawflag - could set to BF_FLAT to make flat scrollbars
//
void DrawBlankButton(HDC hdc, const RECT *rect, UINT drawflag)
{
	RECT rc = *rect;
		
#ifndef FLAT_SCROLLBARS	
	drawflag &= ~BF_FLAT;
#endif
	
	DrawEdge(hdc, &rc, EDGE_RAISED, BF_RECT | drawflag | BF_ADJUST);
	FillRect(hdc, &rc, GetSysColorBrush(COLOR_BTNFACE));
}

//
//	Send a WM_VSCROLL or WM_HSCROLL message
//
static void SendScrollMessage(HWND hwnd, UINT scrMsg, UINT scrId, UINT pos)
{
	SendMessage(hwnd, scrMsg, MAKEWPARAM(scrId, pos), 0);
}

//
//	Calculate the screen coordinates of the area taken by
//  the horizontal scrollbar. Take into account the size
//  of the window borders
//
static BOOL GetHScrollRect(SCROLLWND *sw, HWND hwnd, RECT *rect)
{
	GetWindowRect(hwnd, rect);
	
	if (sw->fLeftScrollbar)
	{
		rect->left  += sw->cxLeftEdge + (sw->sbarVert.fScrollVisible ? 
					GetScrollMetric(&sw->sbarVert, SM_CXVERTSB) : 0);
		rect->right -= sw->cxRightEdge;
	}
	else
	{
		rect->left   += sw->cxLeftEdge;					//left window edge
	
		rect->right  -= sw->cxRightEdge +				//right window edge
					(sw->sbarVert.fScrollVisible ? 
					GetScrollMetric(&sw->sbarVert, SM_CXVERTSB) : 0);
	}
	
	rect->bottom -= sw->cyBottomEdge;				//bottom window edge
	
	rect->top	  = rect->bottom -
					(sw->sbarHorz.fScrollVisible ?
					GetScrollMetric(&sw->sbarHorz, SM_CYHORZSB) : 0);
	
	return TRUE;
}

//
//	Calculate the screen coordinates of the area taken by the
//  vertical scrollbar
//
static BOOL GetVScrollRect(SCROLLWND *sw, HWND hwnd, RECT *rect)
{
	GetWindowRect(hwnd, rect);
	rect->top	 += sw->cyTopEdge;						//top window edge
	
	rect->bottom -= sw->cyBottomEdge + 
					(sw->sbarHorz.fScrollVisible ?		//bottom window edge
					GetScrollMetric(&sw->sbarHorz, SM_CYHORZSB) : 0);

	if (sw->fLeftScrollbar)
	{
		rect->left	+= sw->cxLeftEdge;
		rect->right = rect->left + (sw->sbarVert.fScrollVisible ?
					GetScrollMetric(&sw->sbarVert, SM_CXVERTSB) : 0);
	}
	else
	{
		rect->right  -= sw->cxRightEdge;
		rect->left    = rect->right - (sw->sbarVert.fScrollVisible ?	
					GetScrollMetric(&sw->sbarVert, SM_CXVERTSB) : 0);
	}

	return TRUE;
}

//	Depending on what type of scrollbar nBar refers to, call the
//  appropriate Get?ScrollRect function
//
BOOL GetScrollRect(SCROLLWND *sw, UINT nBar, HWND hwnd, RECT *rect)
{
	if (nBar == SB_HORZ)
		return GetHScrollRect(sw, hwnd, rect);
	else if (nBar == SB_VERT)
		return GetVScrollRect(sw, hwnd, rect);
	else
		return FALSE;
}

//
//	This code is a prime candidate for splitting out into a separate
//  file at some stage
//
#ifdef INCLUDE_BUTTONS

//
//	Calculate the size in pixels of the specified button
//
static int GetSingleButSize(SCROLLBAR *sbar, SCROLLBUT *sbut)
{
	//multiple of the system button size
	//or a specific button size
	if (sbut->nSize < 0)
	{
		if (sbar->nBarType == SB_HORZ)
			return -sbut->nSize * GetSystemMetrics(SM_CXHSCROLL);
		else 
			return -sbut->nSize * GetSystemMetrics(SM_CYVSCROLL);
	}
	else
		return  sbut->nSize;
}

//
//	Find the size in pixels of all the inserted buttons,
//	either before or after the specified scrollbar
//
static int GetButtonSize(SCROLLBAR *sbar, HWND hwnd, UINT uBeforeAfter)
{
	int i;
	int nPixels = 0;

	SCROLLBUT *sbut = sbar->sbButtons;
	
	for (i = 0; i < sbar->nButtons; i++)
	{
		//only consider those buttons on the same side as nTopBottom says
		if (sbut[i].uPlacement == uBeforeAfter)
		{
			nPixels += GetSingleButSize(sbar, &sbut[i]);
		}
	}

	return nPixels;
}
#endif //INCLUDE_BUTTONS

//
//	Work out the scrollbar width/height for either type of scrollbar (SB_HORZ/SB_VERT)
//	rect - coords of the scrollbar.
//	store results into *thumbsize and *thumbpos
//
static int CalcThumbSize(SCROLLBAR *sbar, const RECT *rect, int *pthumbsize, int *pthumbpos)
{
	int scrollsize;			//total size of the scrollbar including arrow buttons
	int workingsize;		//working area (where the thumb can slide)
	int siMaxMin;
	int butsize;
	int startcoord;
	int thumbpos = 0, thumbsize = 0;

	static int count=0;

	//work out the width (for a horizontal) or the height (for a vertical)
	//of a standard scrollbar button
	butsize = GetScrollMetric(sbar, SM_SCROLL_LENGTH);

	scrollsize = rect->right - rect->left;
	startcoord = rect->left;

	SCROLLINFO *si = &sbar->scrollInfo;
	siMaxMin = si->nMax - si->nMin + 1;
	workingsize = scrollsize - butsize * 2;

	//
	// Work out the scrollbar thumb SIZE
	//
	if (si->nPage == 0)
	{
		thumbsize = butsize;
	}
	else if (siMaxMin > 0)
	{
		thumbsize = MulDiv(si->nPage, workingsize, siMaxMin);

		if (thumbsize < sbar->nMinThumbSize)
			thumbsize = sbar->nMinThumbSize;
	}

	//
	// Work out the scrollbar thumb position
	//
	if (siMaxMin > 0)
	{
		int pagesize = max(1, si->nPage);
		thumbpos = MulDiv(si->nPos - si->nMin, workingsize-thumbsize, siMaxMin - pagesize);
		
		if (thumbpos < 0)						
			thumbpos = 0;

		if (thumbpos >= workingsize-thumbsize)	
			thumbpos = workingsize-thumbsize;
	}

	thumbpos += startcoord + butsize;

	*pthumbpos  = thumbpos;
	*pthumbsize = thumbsize;

	return 1;
}

//
//	return a hit-test value for whatever part of the scrollbar x,y is located in
//	rect, x, y: SCREEN coordinates
//	the rectangle must not include space for any inserted buttons 
//	(i.e, JUST the scrollbar area)
//
static UINT GetHorzScrollPortion(SCROLLBAR *sbar, const RECT *rect, int x, int y)
{
	int thumbwidth, thumbpos;
	int butwidth = GetScrollMetric(sbar, SM_SCROLL_LENGTH);
	int scrollwidth  = rect->right-rect->left;
	int workingwidth = scrollwidth - butwidth*2;

	if (y < rect->top || y >= rect->bottom)
		return HTSCROLL_NONE;

	CalcThumbSize(sbar, rect, &thumbwidth, &thumbpos);

	//if we have had to scale the buttons to fit in the rect,
	//then adjust the button width accordingly
	if (scrollwidth <= butwidth * 2)
	{
		butwidth = scrollwidth / 2;	
	}

	//check for left button click
	if (x >= rect->left && x < rect->left + butwidth)
	{
		return HTSCROLL_LEFT;	
	}
	//check for right button click
	else if (x >= rect->right-butwidth && x < rect->right)
	{
		return HTSCROLL_RIGHT;
	}
	
	//if the thumb is too big to fit (i.e. it isn't visible)
	//then return a NULL scrollbar area
	if (thumbwidth >= workingwidth)
		return HTSCROLL_NONE;
	
	//check for point in the thumbbar
	if (x >= thumbpos && x < thumbpos+thumbwidth)
	{
		return HTSCROLL_THUMB;
	}	
	//check for left margin
	else if (x >= rect->left+butwidth && x < thumbpos)
	{
		return HTSCROLL_PAGELEFT;
	}
	else if (x >= thumbpos+thumbwidth && x < rect->right-butwidth)
	{
		return HTSCROLL_PAGERIGHT;
	}
	
	return HTSCROLL_NONE;
}

//
//	For vertical scrollbars, rotate all coordinates by -90 degrees
//	so that we can use the horizontal version of this function
//
static UINT GetVertScrollPortion(SCROLLBAR *sb, RECT *rect, int x, int y)
{
	UINT r;
	
	RotateRect(rect);
	r = GetHorzScrollPortion(sb, rect, y, x);
	RotateRect(rect);
	return r;
}

//
//	CUSTOM DRAW support
//	
static LRESULT PostCustomPrePostPaint(HWND hwnd, HDC hdc, SCROLLBAR *sb, UINT dwStage)
{
#ifdef CUSTOM_DRAW
	NMCSBCUSTOMDRAW	nmcd;

	CoolSB_ZeroMemory(&nmcd, sizeof nmcd);
	nmcd.hdr.hwndFrom = hwnd;
	nmcd.hdr.idFrom   = GetWindowLongPtr(hwnd, GWLP_ID);
	nmcd.hdr.code     = NM_COOLSB_CUSTOMDRAW;
	nmcd.nBar		  = sb->nBarType;
	nmcd.dwDrawStage  = dwStage;
	nmcd.hdc		  = hdc;

	hwnd = GetParent(hwnd);
	return CustomDrawScrollBars(&nmcd);
#else
	return 0;
#endif
}

static LRESULT PostCustomDrawNotify(HWND hwnd, HDC hdc, UINT nBar, RECT *prect, UINT nItem, BOOL fMouseDown, BOOL fMouseOver, BOOL fInactive)
{
#ifdef CUSTOM_DRAW
	NMCSBCUSTOMDRAW	nmcd;

    //fill in the standard header
	nmcd.hdr.hwndFrom = hwnd;
	nmcd.hdr.idFrom   = GetWindowLongPtr(hwnd, GWLP_ID);
	nmcd.hdr.code     = NM_COOLSB_CUSTOMDRAW;

	nmcd.dwDrawStage  = CDDS_ITEMPREPAINT;
	nmcd.nBar		  = nBar;
	nmcd.rect		  = *prect;
	nmcd.uItem		  = nItem;
	nmcd.hdc		  = hdc;

	if (fMouseDown) 
		nmcd.uState		  = CDIS_SELECTED;
	else if (fMouseOver)
		nmcd.uState		  = CDIS_HOT;
	else if (fInactive)
		nmcd.uState		  = CDIS_DISABLED;
	else
		nmcd.uState		  = CDIS_DEFAULT;

	hwnd = GetParent(hwnd);
	return CustomDrawScrollBars(&nmcd);
#else
	return 0;
#endif
}

// Depending on if we are supporting custom draw, either define
// a macro to the function name, or to nothing at all. If custom draw
// is turned off, then we can save ALOT of code space by binning all 
// calls to the custom draw support.

/*
#ifdef CUSTOM_DRAW
#define PostCustomDrawNotify	PostCustomDrawNotify0
#define PostCustomPrePostPaint	PostCustomPrePostPaint0
#else
#define PostCustomDrawNotify	1 ? (void)0 : PostCustomDrawNotify0
#define PostCustomPrePostPaint	1 ? (void)0 : PostCustomPrePostPaint0
#endif
*/

static LRESULT PostMouseNotify0(HWND hwnd, UINT nBar, RECT *prect, UINT nCmdId, POINT pt)
{
#ifdef NOTIFY_MOUSE
	NMCOOLBUTMSG	nmcb;

	//fill in the standard header
	nmcb.hdr.hwndFrom	= hwnd;
	nmcb.hdr.idFrom		= GetWindowLongPtr(hwnd, GWLP_ID);
	nmcb.hdr.code		= NM_CLICK;

	nmcb.nBar			= nBar;
	nmcb.uCmdId			= nCmdId;
	nmcb.uState			= 0;
	nmcb.rect			= *prect;
	nmcb.pt				= pt;

	hwnd = GetParent(hwnd);
	return SendMessage(hwnd, WM_NOTIFY, nmcb.hdr.idFrom, (LPARAM)&nmcb);
#else
	return 0;
#endif
}

#ifdef NOTIFY_MOUSE
#define PostMouseNotify			PostMouseNotify0
#else
#define PostMouseNotify			1 ? (void)0 : PostMouseNotify0
#endif



//
//	Draw a complete HORIZONTAL scrollbar in the given rectangle
//	Don't draw any inserted buttons in this procedure
//	
//	uDrawFlags - hittest code, to say if to draw the
//  specified portion in an active state or not.
//
//
static LRESULT NCDrawHScrollbar(SCROLLBAR *sb, HWND hwnd, HDC hdc, const RECT *rect, UINT uDrawFlags)
{
	SCROLLINFO *si;
	RECT ctrl, thumb;
	RECT sbm;
	int butwidth	 = GetScrollMetric(sb, SM_SCROLL_LENGTH);
	int scrollwidth  = rect->right-rect->left;
	int workingwidth = scrollwidth - butwidth*2;
	int thumbwidth   = 0, thumbpos = 0;
	int siMaxMin;

	BOOL fCustomDraw = 0;

	BOOL fMouseDownL = 0, fMouseOverL = 0, fBarHot = 0;
	BOOL fMouseDownR = 0, fMouseOverR = 0;

	COLORREF crCheck1   = GetSBForeColor();
	COLORREF crCheck2   = GetSBBackColor();
	COLORREF crInverse1 = InvertCOLORREF(crCheck1);
	COLORREF crInverse2 = InvertCOLORREF(crCheck2);

	UINT uDEFlat  = sb->fFlatScrollbar ? BF_FLAT   : 0;

	//drawing flags to modify the appearance of the scrollbar buttons
	UINT uLeftButFlags  = DFCS_SCROLLLEFT;
	UINT uRightButFlags = DFCS_SCROLLRIGHT;

	if (scrollwidth <= 0)
		return 0;

	si = &sb->scrollInfo;
	siMaxMin = si->nMax - si->nMin;

	if (hwnd != hwndCurCoolSB)
		uDrawFlags = HTSCROLL_NONE;
	//
	// work out the thumb size and position
	//
	CalcThumbSize(sb, rect, &thumbwidth, &thumbpos);
	
	if (sb->fScrollFlags & ESB_DISABLE_LEFT)		uLeftButFlags  |= DFCS_INACTIVE;
	if (sb->fScrollFlags & ESB_DISABLE_RIGHT)	uRightButFlags |= DFCS_INACTIVE;

	//if we need to grey the arrows because there is no data to scroll
	if ( !IsScrollInfoActive(si) && !(sb->fScrollFlags & CSBS_THUMBALWAYS))
	{
		uLeftButFlags  |= DFCS_INACTIVE;
		uRightButFlags |= DFCS_INACTIVE;
	}

	if (hwnd == hwndCurCoolSB)
	{
#ifdef FLAT_SCROLLBARS	
		BOOL ldis = !(uLeftButFlags & DFCS_INACTIVE);
		BOOL rdis = !(uRightButFlags & DFCS_INACTIVE);

		fBarHot = (sb->nBarType == (int)uMouseOverScrollbar && sb->fFlatScrollbar == CSBS_HOTTRACKED);
		
		fMouseOverL = uHitTestPortion == HTSCROLL_LEFT && fBarHot && ldis;		
		fMouseOverR = uHitTestPortion == HTSCROLL_RIGHT && fBarHot && rdis;
#endif
		fMouseDownL = (uDrawFlags == HTSCROLL_LEFT);
		fMouseDownR = (uDrawFlags == HTSCROLL_RIGHT);
	}


//#ifdef CUSTOM_DRAW
	fCustomDraw = ((PostCustomPrePostPaint(hwnd, hdc, sb, CDDS_PREPAINT)) == CDRF_SKIPDEFAULT);
//#endif

	//
	// Draw the scrollbar now
	//
	if (scrollwidth > butwidth*2)
	{
		//LEFT ARROW
		SetRect(&ctrl, rect->left, rect->top, rect->left + butwidth, rect->bottom);

		RotateRect0(sb, &ctrl);

		if (fCustomDraw)
			PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_LINELEFT, fMouseDownL, fMouseOverL, uLeftButFlags & DFCS_INACTIVE);
		else
			DrawScrollArrow(sb, hdc, &ctrl, uLeftButFlags, fMouseDownL, fMouseOverL);

		RotateRect0(sb, &ctrl);

		//MIDDLE PORTION
		//if we can fit the thumbbar in, then draw it
		if (thumbwidth > 0 && thumbwidth <= workingwidth
			&& IsScrollInfoActive(si) && ((sb->fScrollFlags & ESB_DISABLE_BOTH) != ESB_DISABLE_BOTH))
		{	
			//Draw the scrollbar margin above the thumb
			SetRect(&sbm, rect->left + butwidth, rect->top, thumbpos, rect->bottom);
			
			RotateRect0(sb, &sbm);
			
			if (fCustomDraw)
			{
				PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &sbm, SB_PAGELEFT, uDrawFlags == HTSCROLL_PAGELEFT, FALSE, FALSE);
			}
			else
			{
				if (uDrawFlags == HTSCROLL_PAGELEFT)
					DrawCheckedRect(hdc, &sbm, crInverse1, crInverse2);
				else
					DrawCheckedRect(hdc, &sbm, crCheck1, crCheck2);

			}

			RotateRect0(sb, &sbm);			
			
			//Draw the margin below the thumb
			sbm.left = thumbpos+thumbwidth;
			sbm.right = rect->right - butwidth;
			
			RotateRect0(sb, &sbm);
			if (fCustomDraw)
			{
				PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &sbm, SB_PAGERIGHT, uDrawFlags == HTSCROLL_PAGERIGHT, 0, 0);
			}
			else
			{
				if (uDrawFlags == HTSCROLL_PAGERIGHT)
					DrawCheckedRect(hdc, &sbm, crInverse1, crInverse2);
				else
					DrawCheckedRect(hdc, &sbm, crCheck1, crCheck2);
			
			}
			RotateRect0(sb, &sbm);
			
			//Draw the THUMB finally
			SetRect(&thumb, thumbpos, rect->top, thumbpos+thumbwidth, rect->bottom);

			RotateRect0(sb, &thumb);			

			if (fCustomDraw)
			{
				PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &thumb, SB_THUMBTRACK, uDrawFlags==HTSCROLL_THUMB, uHitTestPortion == HTSCROLL_THUMB && fBarHot, FALSE);
			}
			else
			{

#ifdef FLAT_SCROLLBARS	
				if (hwnd == hwndCurCoolSB && sb->fFlatScrollbar && (uDrawFlags == HTSCROLL_THUMB || 
				(uHitTestPortion == HTSCROLL_THUMB && fBarHot)))
				{	
					PaintRect(hdc, &thumb, GetSysColor(COLOR_3DSHADOW));
				}
				else
#endif
				{
					DrawBlankButton(hdc, &thumb, uDEFlat);
				}
			}
			RotateRect0(sb, &thumb);

		}
		//otherwise, just leave that whole area blank
		else
		{
			OffsetRect(&ctrl, butwidth, 0);
			ctrl.right = rect->right - butwidth;

			//if we always show the thumb covering the whole scrollbar,
			//then draw it that way
			if ( !IsScrollInfoActive(si)	&& (sb->fScrollFlags & CSBS_THUMBALWAYS) 
				&& ctrl.right - ctrl.left > sb->nMinThumbSize)
			{
				//leave a 1-pixel gap between the thumb + right button
				ctrl.right --;
				RotateRect0(sb, &ctrl);

				if (fCustomDraw)
					PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_THUMBTRACK, fMouseDownL, FALSE, FALSE);
				else
				{
#ifdef FLAT_SCROLLBARS	
					if (sb->fFlatScrollbar == CSBS_HOTTRACKED && uDrawFlags == HTSCROLL_THUMB)
						PaintRect(hdc, &ctrl, GetSysColor(COLOR_3DSHADOW));
					else
#endif
						DrawBlankButton(hdc, &ctrl, uDEFlat);

				}
				RotateRect0(sb, &ctrl);

				//draw the single-line gap
				ctrl.left = ctrl.right;
				ctrl.right += 1;
				
				RotateRect0(sb, &ctrl);
				
				if (fCustomDraw)
					PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_PAGERIGHT, 0, 0, 0);
				else
					PaintRect(hdc, &ctrl, GetSysColor(COLOR_SCROLLBAR));

				RotateRect0(sb, &ctrl);
			}
			//otherwise, paint a blank if the thumb doesn't fit in
			else
			{
				RotateRect0(sb, &ctrl);
	
				if (fCustomDraw)
					PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_PAGERIGHT, 0, 0, 0);
				else
					DrawCheckedRect(hdc, &ctrl, crCheck1, crCheck2);
				
				RotateRect0(sb, &ctrl);
			}
		}

		//RIGHT ARROW
		SetRect(&ctrl, rect->right - butwidth, rect->top, rect->right, rect->bottom);

		RotateRect0(sb, &ctrl);

		if (fCustomDraw)
			PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_LINERIGHT, fMouseDownR, fMouseOverR, uRightButFlags & DFCS_INACTIVE);
		else
			DrawScrollArrow(sb, hdc, &ctrl, uRightButFlags, fMouseDownR, fMouseOverR);

		RotateRect0(sb, &ctrl);
	}
	//not enough room for the scrollbar, so just draw the buttons (scaled in size to fit)
	else
	{
		butwidth = scrollwidth / 2;

		//LEFT ARROW
		SetRect(&ctrl, rect->left, rect->top, rect->left + butwidth, rect->bottom);

		RotateRect0(sb, &ctrl);
		if (fCustomDraw)
			PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_LINELEFT, fMouseDownL, fMouseOverL, uLeftButFlags & DFCS_INACTIVE);
		else	
			DrawScrollArrow(sb, hdc, &ctrl, uLeftButFlags, fMouseDownL, fMouseOverL);
		RotateRect0(sb, &ctrl);

		//RIGHT ARROW
		OffsetRect(&ctrl, scrollwidth - butwidth, 0);
		
		RotateRect0(sb, &ctrl);
		if (fCustomDraw)
			PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_LINERIGHT, fMouseDownR, fMouseOverR, uRightButFlags & DFCS_INACTIVE);
		else
			DrawScrollArrow(sb, hdc, &ctrl, uRightButFlags, fMouseDownR, fMouseOverR);		
		RotateRect0(sb, &ctrl);

		//if there is a gap between the buttons, fill it with a solid color
		//if (butwidth & 0x0001)
		if (ctrl.left != rect->left + butwidth)
		{
			ctrl.left --;
			ctrl.right -= butwidth;
			RotateRect0(sb, &ctrl);
			
			if (fCustomDraw)
				PostCustomDrawNotify(hwnd, hdc, sb->nBarType, &ctrl, SB_PAGERIGHT, 0, 0, 0);
			else
				DrawCheckedRect(hdc, &ctrl, crCheck1, crCheck2);

			RotateRect0(sb, &ctrl);
		}
			
	}

//#ifdef CUSTOM_DRAW
	PostCustomPrePostPaint(hwnd, hdc, sb, CDDS_POSTPAINT);
//#endif

	return fCustomDraw;
}

//
//	Draw a vertical scrollbar using the horizontal draw routine, but
//	with the coordinates adjusted accordingly
//
static LRESULT NCDrawVScrollbar(SCROLLBAR *sb, HWND hwnd, HDC hdc, const RECT *rect, UINT uDrawFlags)
{
	LRESULT ret;
	RECT rc;

	rc = *rect;
	RotateRect(&rc);
	ret = NCDrawHScrollbar(sb, hwnd, hdc, &rc, uDrawFlags);
	RotateRect(&rc);
	
	return ret;
}

//
//	Generic wrapper function for the scrollbar drawing
//
static LRESULT NCDrawScrollbar(SCROLLBAR *sb, HWND hwnd, HDC hdc, const RECT *rect, UINT uDrawFlags)
{
	if (sb->nBarType == SB_HORZ)
		return NCDrawHScrollbar(sb, hwnd, hdc, rect, uDrawFlags);
	else
		return NCDrawVScrollbar(sb, hwnd, hdc, rect, uDrawFlags);
}

#ifdef INCLUDE_BUTTONS

//
//	Draw the specified bitmap centered in the rectangle
//
static void DrawImage(HDC hdc, HBITMAP hBitmap, RECT *rc)
{
	BITMAP bm;
	int cx;
	int cy;   
	HDC memdc;
	HBITMAP hOldBM;
	RECT  rcDest = *rc;   
	POINT p;
	SIZE  delta;
	COLORREF colorOld;

	if (hBitmap == NULL) 
		return;

	// center bitmap in caller's rectangle   
	GetObject(hBitmap, sizeof bm, &bm);   
	
	cx = bm.bmWidth;
	cy = bm.bmHeight;

	delta.cx = (rc->right-rc->left - cx) / 2;
	delta.cy = (rc->bottom-rc->top - cy) / 2;
	
	if (rc->right-rc->left > cx)
	{
		SetRect(&rcDest, rc->left+delta.cx, rc->top + delta.cy, 0, 0);   
		rcDest.right = rcDest.left + cx;
		rcDest.bottom = rcDest.top + cy;
		p.x = 0;
		p.y = 0;
	}
	else
	{
		p.x = -delta.cx;   
		p.y = -delta.cy;
	}
   
	// select checkmark into memory DC
	memdc = CreateCompatibleDC(hdc);
	hOldBM = (HBITMAP)SelectObject(memdc, hBitmap);
   
	// set BG color based on selected state   
	colorOld = SetBkColor(hdc, GetSysColor(COLOR_3DFACE));

	BitBlt(hdc, rcDest.left, rcDest.top, rcDest.right-rcDest.left, rcDest.bottom-rcDest.top, memdc, p.x, p.y, SRCCOPY);

	// restore
	SetBkColor(hdc, colorOld);
	SelectObject(memdc, hOldBM);
	DeleteDC(memdc);
}

//
// Draw the specified metafile
//
static void DrawMetaFile(HDC hdc, HENHMETAFILE hemf, RECT *rect)
{
	RECT rc;
	POINT pt;

	SetRect(&rc, 0, 0, rect->right-rect->left, rect->bottom-rect->top);
	SetWindowOrgEx(hdc, -rect->left, -rect->top, &pt);
	PlayEnhMetaFile(hdc, hemf, &rc);
	SetWindowOrgEx(hdc, pt.x, pt.y, 0);
}

//
//	Draw a single scrollbar inserted button, in whatever style
//	it has been defined to use.	
//
static UINT DrawScrollButton(SCROLLBUT *sbut, HDC hdc, const RECT *pctrl, UINT flags)
{
	NMCSBCUSTOMDRAW	nmcd;
	HWND hwnd;
	RECT rect = *pctrl;
	UINT f;

	switch(sbut->uButType & SBBT_MASK)
	{
	case SBBT_OWNERDRAW:

		hwnd = WindowFromDC(hdc);

		//fill in the standard header
		nmcd.hdr.hwndFrom = hwnd;
		nmcd.hdr.idFrom   = GetWindowLongPtr(hwnd, GWLP_ID);
		nmcd.hdr.code     = NM_COOLSB_CUSTOMDRAW;

		nmcd.dwDrawStage  = CDDS_ITEMPREPAINT;
		nmcd.nBar		  = SB_INSBUT;
		nmcd.rect		  = *pctrl;
		nmcd.uItem		  = sbut->uCmdId;
		nmcd.hdc		  = hdc;
		nmcd.uState		  = flags;

		IntersectClipRect(hdc, rect.left, rect.top, rect.right, rect.bottom);
		SendMessage(GetParent(hwnd), WM_NOTIFY, nmcd.hdr.idFrom, (LPARAM)&nmcd);
		SelectClipRgn(hdc, NULL);

		break;

	case SBBT_FIXED:
		flags &= ~SBBS_PUSHED;

	case SBBT_TOGGLEBUTTON:
		if (sbut->uState != SBBS_NORMAL)
			flags |= SBBS_PUSHED;

		//intentionally fall through here...

	case SBBT_PUSHBUTTON: 
		
		f = flags & SBBS_PUSHED ? DFCS_PUSHED | DFCS_FLAT : 0;
		if (sbut->uButType & SBBM_LEFTARROW)
		{
			DrawFrameControl(hdc, &rect, DFC_SCROLL, DFCS_SCROLLLEFT | f);
		}
		else if (sbut->uButType & SBBM_RIGHTARROW)
		{
			DrawFrameControl(hdc, &rect, DFC_SCROLL, DFCS_SCROLLRIGHT | f);
		}
		else if (sbut->uButType & SBBM_UPARROW)
		{
			DrawFrameControl(hdc, &rect, DFC_SCROLL, DFCS_SCROLLUP | f);
		}
		else if (sbut->uButType & SBBM_DOWNARROW)
		{
			DrawFrameControl(hdc, &rect, DFC_SCROLL, DFCS_SCROLLDOWN | f);
		}
		else
		{
			//
			if (flags & SBBS_PUSHED)
			{
				if (sbut->uButType & SBBM_RECESSED)
				{
					InflateRect(&rect, -1, -1);
					DrawEdge(hdc, &rect, EDGE_SUNKEN, BF_RECT|BF_FLAT);
					InflateRect(&rect, 1, 1);

					FrameRect(hdc, &rect, GetSysColorBrush(COLOR_3DDKSHADOW));
					InflateRect(&rect, -2, -2);
				}
				else
				{
					DrawEdge(hdc, &rect, EDGE_SUNKEN, BF_RECT | BF_FLAT | BF_ADJUST);
					InflateRect(&rect, 1, 1);
				}
			}
			else
			{
				// draw the button borders
				if (sbut->uButType & SBBM_TYPE2)
				{
					DrawFrameControl(hdc, &rect, DFC_BUTTON, DFCS_BUTTONPUSH);
					InflateRect(&rect, -2, -2);
				}

				else if (sbut->uButType & SBBM_TYPE3)
				{
					DrawFrameControl(hdc, &rect, DFC_BUTTON, DFCS_BUTTONPUSH);
					InflateRect(&rect, -1, -1);
				}
				else
				{
					DrawEdge(hdc, &rect, EDGE_RAISED, BF_RECT | BF_ADJUST);
					rect.bottom++;
					rect.right++;
				}
			
				OffsetRect(&rect, -1, -1);
				rect.top++;	rect.left++;
			}

			if (sbut->hBmp)
			{
				PaintRect(hdc, &rect, GetSysColor(COLOR_3DFACE));

				if (flags & SBBS_PUSHED)	
				{
					rect.top++; rect.left++;
				}

				IntersectClipRect(hdc, rect.left, rect.top, rect.right,rect.bottom);
				DrawImage(hdc, sbut->hBmp, &rect);
				SelectClipRgn(hdc, 0);
			}
			else if (sbut->hEmf)
			{
				PaintRect(hdc, &rect, GetSysColor(COLOR_3DFACE));
				InflateRect(&rect, -1, -1);		

				if (flags & SBBS_PUSHED)	
				{
					rect.top++; rect.left++;
				}

				IntersectClipRect(hdc, rect.left, rect.top, rect.right,rect.bottom);
				DrawMetaFile(hdc, sbut->hEmf, &rect);
				SelectClipRgn(hdc, 0);
			}
			else
			{
				PaintRect(hdc, &rect, GetSysColor(COLOR_3DFACE));
			}
		}
			

		break;

	case SBBT_BLANK:
		PaintRect(hdc, &rect, GetSysColor(COLOR_3DFACE));
		break;

	case SBBT_FLAT:
		DrawBlankButton(hdc, &rect, BF_FLAT);
		break;

	case SBBT_DARK:
		PaintRect(hdc, &rect, GetSysColor(COLOR_3DDKSHADOW));
		break;
	}
	
	return 0;
}

//
//	Draw any buttons inserted into the horizontal scrollbar
//	assume that the button widths have already been calculated
//	Note: RECT *rect is the rectangle of the scrollbar
//	      leftright: 1 = left, 2 = right, 3 = both
//
static LRESULT DrawHorzButtons(SCROLLBAR *sbar, HDC hdc, const RECT *rect, int leftright)
{
	int i;
	int xposl, xposr;
	RECT ctrl;
	SCROLLBUT *sbut = sbar->sbButtons;
	
	xposl = rect->left - sbar->nButSizeBefore;
	xposr = rect->right;
	
	for (i = 0; i < sbar->nButtons; i++)
	{
		if ((leftright & SBBP_LEFT) && sbut[i].uPlacement == SBBP_LEFT)
		{
			int butwidth = GetSingleButSize(sbar, &sbut[i]);
			SetRect(&ctrl, xposl, rect->top, xposl + butwidth, rect->bottom);
			RotateRect0(sbar, &ctrl);
			DrawScrollButton(&sbut[i], hdc, &ctrl, SBBS_NORMAL);
			
			xposl += butwidth;
		}

		if ((leftright & SBBP_RIGHT) && sbut[i].uPlacement == SBBP_RIGHT)
		{
			int butwidth = GetSingleButSize(sbar, &sbut[i]);
			SetRect(&ctrl, xposr, rect->top, xposr + butwidth, rect->bottom);
			RotateRect0(sbar, &ctrl);
			DrawScrollButton(&sbut[i], hdc, &ctrl, SBBS_NORMAL);
			xposr += butwidth;
		}
	}
	return 0;
}

static LRESULT DrawVertButtons(SCROLLBAR *sbar, HDC hdc, const RECT *rect, int leftright)
{
	RECT rc = *rect;
	RotateRect(&rc);
	DrawHorzButtons(sbar, hdc, &rc, leftright);
	return 0;
}
#endif	// INCLUDE_BUTTONS

//
//	Define these two for proper processing of NCPAINT
//	NOT needed if we don't bother to mask the scrollbars we draw
//	to prevent the old window procedure from accidently drawing over them
//
HDC CoolSB_GetDC(HWND hwnd, WPARAM)
{
	// I just can't figure out GetDCEx, so I'll just use this:
	return GetWindowDC(hwnd);
}

static LRESULT NCPaint(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
{
	SCROLLBAR *sb;
	HDC hdc;
	RECT winrect, rect;
	HRGN clip = 0;
	BOOL fCustomDraw = FALSE;
	LRESULT ret;
	DWORD dwStyle;

	GetWindowRect(hwnd, &winrect);
	
	HRGN hrgn = (HRGN)wParam;
	
	//hdc = GetWindowDC(hwnd);
	hdc = CoolSB_GetDC(hwnd, wParam);

	//
	//	Only draw the horizontal scrollbar if the window is tall enough
	//
	sb = &sw->sbarHorz;
	if (sb->fScrollVisible)
	{
		//get the screen coordinates of the whole horizontal scrollbar area
		GetHScrollRect(sw, hwnd, &rect);

		//make the coordinates relative to the window for drawing
		OffsetRect(&rect, -winrect.left, -winrect.top);

		if (uCurrentScrollbar == SB_HORZ)
			fCustomDraw |= NCDrawHScrollbar(sb, hwnd, hdc, &rect, uScrollTimerPortion);
		else
			fCustomDraw |= NCDrawHScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_NONE);
	}

	//
	// Only draw the vertical scrollbar if the window is wide enough to accomodate it
	//
	sb = &sw->sbarVert;
	if (sb->fScrollVisible)
	{
		//get the screen cooridinates of the whole horizontal scrollbar area
		GetVScrollRect(sw, hwnd, &rect);

		//make the coordinates relative to the window for drawing
		OffsetRect(&rect, -winrect.left, -winrect.top);

		if (uCurrentScrollbar == SB_VERT)
			fCustomDraw |= NCDrawVScrollbar(sb, hwnd, hdc, &rect, uScrollTimerPortion);
		else
			fCustomDraw |= NCDrawVScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_NONE);
	}

	//Call the default window procedure for WM_NCPAINT, with the
	//new window region. ** region must be in SCREEN coordinates **
	dwStyle = GetWindowLongPtr(hwnd, GWL_STYLE);

	// If the window has WS_(H-V)SCROLL bits set, we should reset them
	// to avoid windows taking the scrollbars into account.
	// We temporarily set a flag preventing the subsecuent 
	// WM_STYLECHANGING/WM_STYLECHANGED to be forwarded to 
	// the original window procedure
	if ( dwStyle & (WS_VSCROLL|WS_HSCROLL))
	{
		sw->bPreventStyleChange = TRUE;
		SetWindowLongPtr(hwnd, GWL_STYLE, dwStyle & ~(WS_VSCROLL|WS_HSCROLL));
	}

	ret = mir_callNextSubclass(hwnd, CoolSBWndProc, WM_NCPAINT, (WPARAM)hrgn, lParam);

	if ( dwStyle & (WS_VSCROLL|WS_HSCROLL))
	{
		SetWindowLongPtr(hwnd, GWL_STYLE, dwStyle);
		sw->bPreventStyleChange = FALSE;
	}

	// DRAW THE DEAD AREA
	// only do this if the horizontal and vertical bars are visible
	if (sw->sbarHorz.fScrollVisible && sw->sbarVert.fScrollVisible)
	{
		GetWindowRect(hwnd, &rect);
		OffsetRect(&rect, -winrect.left, -winrect.top);

		rect.bottom -= sw->cyBottomEdge;
		rect.top  = rect.bottom - GetScrollMetric(&sw->sbarHorz, SM_CYHORZSB);

		if (sw->fLeftScrollbar)
		{
			rect.left += sw->cxLeftEdge;
			rect.right = rect.left + GetScrollMetric(&sw->sbarVert, SM_CXVERTSB);
		}
		else
		{
			rect.right -= sw->cxRightEdge;
			rect.left = rect.right  - GetScrollMetric(&sw->sbarVert, SM_CXVERTSB);
		}

		if (fCustomDraw)
			PostCustomDrawNotify(hwnd, hdc, SB_BOTH, &rect, 32, 0, 0, 0);
		else
		{
			//calculate the position of THIS window's dead area
			//with the position of the PARENT window's client rectangle.
			//if THIS window has been positioned such that its bottom-right
			//corner sits in the parent's bottom-right corner, then we should
			//show the sizing-grip.
			//Otherwise, assume this window is not in the right place, and
			//just draw a blank rectangle
			RECT parent;
			RECT rect2;
			HWND hwndParent = GetParent(hwnd);

			GetClientRect(hwndParent, &parent);
			MapWindowPoints(hwndParent, 0, (POINT *)&parent, 2);

			CopyRect(&rect2, &rect);
			OffsetRect(&rect2, winrect.left, winrect.top);

			if ( !sw->fLeftScrollbar && parent.right == rect2.right+sw->cxRightEdge && parent.bottom == rect2.bottom+sw->cyBottomEdge
				|| sw->fLeftScrollbar && parent.left  == rect2.left -sw->cxLeftEdge  && parent.bottom == rect2.bottom+sw->cyBottomEdge)
				DrawFrameControl(hdc, &rect, DFC_SCROLL, sw->fLeftScrollbar ? DFCS_SCROLLSIZEGRIPRIGHT : DFCS_SCROLLSIZEGRIP );
			else
				PaintRect(hdc, &rect, GetSysColor(COLOR_3DFACE));
		}
	}

	UNREFERENCED_PARAMETER(clip);
	ReleaseDC(hwnd, hdc);
	return ret;
}

//
// Need to detect if we have clicked in the scrollbar region or not
//
static LRESULT NCHitTest(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
{
	RECT hrect;
	RECT vrect;
	POINT pt;

	pt.x = LOWORD(lParam);
	pt.y = HIWORD(lParam);
	
	//work out exactly where the Horizontal and Vertical scrollbars are
	GetHScrollRect(sw, hwnd, &hrect);
	GetVScrollRect(sw, hwnd, &vrect);
	
	//Clicked in the horizontal scrollbar area
	if (sw->sbarHorz.fScrollVisible && PtInRect(&hrect, pt))
		return HTHSCROLL;

	//Clicked in the vertical scrollbar area
	if (sw->sbarVert.fScrollVisible && PtInRect(&vrect, pt))
		return HTVSCROLL;

	//clicked somewhere else
	return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_NCHITTEST, wParam, lParam);
}

//
//	Return a HT* value indicating what part of the scrollbar was clicked
//	Rectangle is not adjusted
//
static UINT GetHorzPortion(SCROLLBAR *sb, RECT *rect, int x, int y)
{
	RECT rc = *rect;

	if (y < rc.top || y >= rc.bottom) return HTSCROLL_NONE;

#ifdef INCLUDE_BUTTONS

	if (sb->fButVisibleBefore) 
	{
		//clicked on the buttons to the left of the scrollbar
		if (x >= rc.left && x < rc.left + sb->nButSizeBefore)
			return HTSCROLL_INSERTED;

		//adjust the rectangle to exclude the left-side buttons, now that we
		//know we havn't clicked on them
		rc.left  += sb->nButSizeBefore;
	}

	if (sb->fButVisibleAfter)
	{
		//clicked on the buttons to the right of the scrollbar
		if (x >= rc.right - sb->nButSizeAfter && x < rc.right)
			return HTSCROLL_INSERTED;

		//adjust the rectangle to exclude the right-side buttons, now that we
		//know we havn't clicked on them
		rc.right -= sb->nButSizeAfter;
	}

#endif INCLUDE_BUTTONS

	//Now we have the rectangle for the scrollbar itself, so work out
	//what part we clicked on.
	return GetHorzScrollPortion(sb, &rc, x, y);
}

//
//	Just call the horizontal version, with adjusted coordinates
//
static UINT GetVertPortion(SCROLLBAR *sb, RECT *rect, int x, int y)
{
	UINT ret;
	RotateRect(rect);
	ret = GetHorzPortion(sb, rect, y, x);
	RotateRect(rect);
	return ret;
}

//
//	Wrapper function for GetHorzPortion and GetVertPortion
//
static UINT GetPortion(SCROLLBAR *sb, RECT *rect, int x, int y)
{
	if (sb->nBarType == SB_HORZ)
		return GetHorzPortion(sb, rect, x, y);
	else if (sb->nBarType == SB_VERT)
		return GetVertPortion(sb, rect, x, y);
	else
		return HTSCROLL_NONE;
}

//
//	Input: rectangle of the total scrollbar area
//	Output: adjusted to take the inserted buttons into account
//
static void GetRealHorzScrollRect(SCROLLBAR *sb, RECT *rect)
{
	if (sb->fButVisibleBefore) rect->left += sb->nButSizeBefore;
	if (sb->fButVisibleAfter)  rect->right -= sb->nButSizeAfter;
}

//
//	Input: rectangle of the total scrollbar area
//	Output: adjusted to take the inserted buttons into account
//
static void GetRealVertScrollRect(SCROLLBAR *sb, RECT *rect)
{
	if (sb->fButVisibleBefore) rect->top += sb->nButSizeBefore;
	if (sb->fButVisibleAfter)  rect->bottom -= sb->nButSizeAfter;
}

//
//	Decide which type of scrollbar we have before calling
//  the real function to do the job
//
static void GetRealScrollRect(SCROLLBAR *sb, RECT *rect)
{
	if (sb->nBarType == SB_HORZ)
	{
		GetRealHorzScrollRect(sb, rect);
	}
	else if (sb->nBarType == SB_VERT)
	{
		GetRealVertScrollRect(sb, rect);
	}
}

//
//	All button code shoule be collected together
//	
//
#ifdef INCLUDE_BUTTONS

//
//	Return the index of the button covering the specified point
//	rect		- rectangle of the whole scrollbar area
//	pt			- screen coords of the mouse
//	fReturnRect - do/don't modify the rect to return the button's area
//
static UINT GetHorzButtonFromPt(SCROLLBAR *sb, RECT *rect, POINT pt, BOOL fReturnRect)
{
	int leftpos = rect->left, rightpos = rect->right;
	int i;
	int butwidth;
	SCROLLBUT *sbut = sb->sbButtons;

	if ( !PtInRect(rect, pt))
		return -1;

	if (sb->fButVisibleAfter)
		rightpos -= sb->nButSizeAfter;

	for (i = 0; i < sb->nButtons; i++)
	{
		if (sb->fButVisibleBefore && sbut[i].uPlacement == SBBP_LEFT)
		{
			butwidth = GetSingleButSize(sb, &sbut[i]);
			
			//if the current button is under the specified point
			if (pt.x >= leftpos && pt.x < leftpos + butwidth)
			{
				//if the caller wants us to return the rectangle of the button
				if (fReturnRect)
				{
					rect->left  = leftpos;
					rect->right = leftpos + butwidth;
				}

				return i;
			}

			leftpos += butwidth;
		}
		else if (sb->fButVisibleAfter && sbut[i].uPlacement == SBBP_RIGHT)
		{
			butwidth = GetSingleButSize(sb, &sbut[i]);

			//if the current button is under the specified point
			if (pt.x >= rightpos && pt.x < rightpos + butwidth)
			{
				//if the caller wants us to return the rectangle of the button
				if (fReturnRect)
				{
					rect->left  = rightpos;
					rect->right = rightpos + butwidth;
				}
				return i;
			}

			rightpos += butwidth;
		}
	}

	return -1;
}


static UINT GetVertButtonFromPt(SCROLLBAR *sb, RECT *rect, POINT pt, BOOL fReturnRect)
{
	UINT ret;
	int temp;
	
	//swap the X/Y coords
	temp = pt.x;
	pt.x = pt.y;
	pt.y = temp;

	//swap the rectangle
	RotateRect(rect);
	
	ret = GetHorzButtonFromPt(sb, rect, pt, fReturnRect);

	RotateRect(rect);
	return ret;
}

//
//
//
static UINT GetButtonFromPt(SCROLLBAR *sb, RECT *rect, POINT pt, BOOL fReturnRect)
{
	if (sb->nBarType == SB_HORZ)
	{
		return GetHorzButtonFromPt(sb, rect, pt, fReturnRect);
	}
	else
	{
		return GetVertButtonFromPt(sb, rect, pt, fReturnRect);
	}
}

//
//	Find the coordinates (in RECT format) of the specified button index
//
static UINT GetHorzButtonRectFromId(SCROLLBAR *sb, RECT *rect, UINT index)
{
	UINT i;
	SCROLLBUT *sbut = sb->sbButtons;
	int leftpos = rect->left, rightpos = rect->right;

	if (sb->fButVisibleAfter)
		rightpos -= sb->nButSizeAfter;

	//find the particular button in question
	for (i = 0; i < index; i++)
	{
		if (sb->fButVisibleBefore && sbut[i].uPlacement == SBBP_LEFT)
		{
			leftpos += GetSingleButSize(sb, &sbut[i]);
		}
		else if (sb->fButVisibleAfter && sbut[i].uPlacement == SBBP_RIGHT)
		{
			rightpos += GetSingleButSize(sb, &sbut[i]);
		}
	}

	//now return the rectangle
	if (sbut[i].uPlacement == SBBP_LEFT)
	{
		rect->left  = leftpos;
		rect->right = leftpos + GetSingleButSize(sb, &sbut[i]);
	}
	else
	{
		rect->left  = rightpos;
		rect->right = rightpos + GetSingleButSize(sb, &sbut[i]);
	}

	return 0;
}

static UINT GetVertButtonRectFromId(SCROLLBAR *sb, RECT *rect, UINT index)
{
	UINT ret;
	RotateRect(rect);
	ret = GetHorzButtonRectFromId(sb, rect, index);
	RotateRect(rect);
	return ret;
}

static UINT GetButtonRectFromId(SCROLLBAR *sb, RECT *rect, UINT index)
{
	if (sb->nBarType == SB_HORZ)
	{
		return GetHorzButtonRectFromId(sb, rect, index);
	}
	else
	{
		return GetVertButtonRectFromId(sb, rect, index);
	}
}
#endif //INCLUDE_BUTTONS	

//
//	Left button click in the non-client area
//
static LRESULT NCLButtonDown(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
{
	RECT rect, winrect;
	HDC hdc;
	SCROLLBAR *sb;
	POINT pt;

	pt.x = LOWORD(lParam);
	pt.y = HIWORD(lParam);

	hwndCurCoolSB = hwnd;

	//
	//	HORIZONTAL SCROLLBAR PROCESSING
	//
	if (wParam == HTHSCROLL)
	{
		uScrollTimerMsg = WM_HSCROLL;
		uCurrentScrollbar = SB_HORZ;
		sb = &sw->sbarHorz;

		//get the total area of the normal Horz scrollbar area
		GetHScrollRect(sw, hwnd, &rect);
		uCurrentScrollPortion = GetHorzPortion(sb, &rect, LOWORD(lParam), HIWORD(lParam));
	}
	//
	//	VERTICAL SCROLLBAR PROCESSING
	//
	else if (wParam == HTVSCROLL)
	{
		uScrollTimerMsg = WM_VSCROLL;
		uCurrentScrollbar = SB_VERT;
		sb = &sw->sbarVert;

		//get the total area of the normal Horz scrollbar area
		GetVScrollRect(sw, hwnd, &rect);
		uCurrentScrollPortion = GetVertPortion(sb, &rect, LOWORD(lParam), HIWORD(lParam));
	}
	//
	//	NORMAL PROCESSING
	//
	else
	{
		uCurrentScrollPortion = HTSCROLL_NONE;
		return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_NCLBUTTONDOWN, wParam, lParam);
	}

	//
	// we can now share the same code for vertical
	// and horizontal scrollbars
	//
	switch(uCurrentScrollPortion)
	{
	//inserted buttons to the left/right
#ifdef INCLUDE_BUTTONS
	case HTSCROLL_INSERTED:  

#ifdef HOT_TRACKING
		KillTimer(hwnd, uMouseOverId);
		uMouseOverId = 0;
		uMouseOverScrollbar = COOLSB_NONE;
#endif

		//find the index of the button that has been clicked
		//adjust the rectangle to give the button's rectangle
		uCurrentButton = GetButtonFromPt(sb, &rect, pt, TRUE);

		sbut = &sb->sbButtons[uCurrentButton];
		
		//post a notification message
		PostMouseNotify(hwnd, NM_CLICK, sb->nBarType, &rect, sbut->uCmdId, pt);

		GetWindowRect(hwnd, &winrect);
		OffsetRect(&rect, -winrect.left, -winrect.top);
		hdc = GetWindowDC(hwnd);
			
		DrawScrollButton(sbut, hdc, &rect, SBBS_PUSHED);

		ReleaseDC(hwnd, hdc);
	
		break;
#endif	//INCLUDE_BUTTONS

	case HTSCROLL_THUMB: 

		//if the scrollbar is disabled, then do no further processing
		if ( !IsScrollbarActive(sb))
			return 0;
		
		GetRealScrollRect(sb, &rect);
		RotateRect0(sb, &rect);
		CalcThumbSize(sb, &rect, &nThumbSize, &nThumbPos);
		RotateRect0(sb, &rect);
		
		//remember the bounding rectangle of the scrollbar work area
		rcThumbBounds = rect;
		
		sw->fThumbTracking = TRUE;
		sb->scrollInfo.nTrackPos = sb->scrollInfo.nPos;
		
		if (wParam == HTVSCROLL) 
			nThumbMouseOffset = pt.y - nThumbPos;
		else
			nThumbMouseOffset = pt.x - nThumbPos;

		nLastPos = -sb->scrollInfo.nPos;
		nThumbPos0 = nThumbPos;
	
		//if (sb->fFlatScrollbar)
		//{
			GetWindowRect(hwnd, &winrect);
			OffsetRect(&rect, -winrect.left, -winrect.top);
			hdc = GetWindowDC(hwnd);
			NCDrawScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_THUMB);
			ReleaseDC(hwnd, hdc);
		//}

		break;

		//Any part of the scrollbar
	case HTSCROLL_LEFT:  
		if (sb->fScrollFlags & ESB_DISABLE_LEFT)		return 0;
		else										goto target1;
	
	case HTSCROLL_RIGHT: 
		if (sb->fScrollFlags & ESB_DISABLE_RIGHT)	return 0;
		else										goto target1;

		goto target1;	

	case HTSCROLL_PAGELEFT:  case HTSCROLL_PAGERIGHT:

		target1:

		//if the scrollbar is disabled, then do no further processing
		if ( !IsScrollbarActive(sb))
			break;

		//ajust the horizontal rectangle to NOT include
		//any inserted buttons
		GetRealScrollRect(sb, &rect);

		SendScrollMessage(hwnd, uScrollTimerMsg, uCurrentScrollPortion, 0);

		// Check what area the mouse is now over :
		// If the scroll thumb has moved under the mouse in response to 
		// a call to SetScrollPos etc, then we don't hilight the scrollbar margin
		if (uCurrentScrollbar == SB_HORZ)
			uScrollTimerPortion = GetHorzScrollPortion(sb, &rect, pt.x, pt.y);
		else
			uScrollTimerPortion = GetVertScrollPortion(sb, &rect, pt.x, pt.y);

		GetWindowRect(hwnd, &winrect);
		OffsetRect(&rect, -winrect.left, -winrect.top);
		hdc = GetWindowDC(hwnd);
			
#ifndef HOT_TRACKING
		//if we aren't hot-tracking, then don't highlight 
		//the scrollbar thumb unless we click on it
		if (uScrollTimerPortion == HTSCROLL_THUMB)
			uScrollTimerPortion = HTSCROLL_NONE;
#endif
		NCDrawScrollbar(sb, hwnd, hdc, &rect, uScrollTimerPortion);
		ReleaseDC(hwnd, hdc);

		//Post the scroll message!!!!
		uScrollTimerPortion = uCurrentScrollPortion;

		//set a timer going on the first click.
		//if this one expires, then we can start off a more regular timer
		//to generate the auto-scroll behaviour
		uScrollTimerId = SetTimer(hwnd, COOLSB_TIMERID1, COOLSB_TIMERINTERVAL1, 0);
		break;
	default:
		return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_NCLBUTTONDOWN, wParam, lParam);
		//return 0;
	}
		
	SetCapture(hwnd);
	return 0;
}

//
//	Left button released
//
static LRESULT LButtonUp(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
{
	RECT rect;
	//UINT thisportion;
	HDC hdc;
	POINT pt;
	RECT winrect;
	
	//current scrollportion is the button that we clicked down on
	if (uCurrentScrollPortion != HTSCROLL_NONE)
	{
		SCROLLBAR *sb = &sw->sbarHorz;
		lParam = GetMessagePos();
		ReleaseCapture();

		GetWindowRect(hwnd, &winrect);
		pt.x = LOWORD(lParam);
		pt.y = HIWORD(lParam);

		//emulate the mouse input on a scrollbar here...
		if (uCurrentScrollbar == SB_HORZ)
		{
			//get the total area of the normal Horz scrollbar area
			sb = &sw->sbarHorz;
			GetHScrollRect(sw, hwnd, &rect);
		}
		else if (uCurrentScrollbar == SB_VERT)
		{
			//get the total area of the normal Horz scrollbar area
			sb = &sw->sbarVert;
			GetVScrollRect(sw, hwnd, &rect);
		}

		//we need to do different things depending on if the
		//user is activating the scrollbar itself, or one of
		//the inserted buttons
		switch(uCurrentScrollPortion)
		{
#ifdef INCLUDE_BUTTONS
		//inserted buttons are being clicked
		case HTSCROLL_INSERTED:
			
			//get the rectangle of the ACTIVE button 
			buttonIdx = GetButtonFromPt(sb, &rect, pt, FALSE);
			GetButtonRectFromId(sb, &rect, uCurrentButton);
	
			OffsetRect(&rect, -winrect.left, -winrect.top);

			//Send the notification BEFORE we redraw, so the
			//bitmap can be changed smoothly by the user if they require
			if (uCurrentButton == buttonIdx)
			{
				SCROLLBUT *sbut = &sb->sbButtons[buttonIdx];
				UINT cmdid = sbut->uCmdId;
				
				if ((sbut->uButType & SBBT_MASK) == SBBT_TOGGLEBUTTON)
					sbut->uState ^= 1;

				//send a notify??				
				//only post a message if the command id is valid
				if (cmdid != -1 && cmdid > 0)
					SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(cmdid, CSBN_CLICKED), 0);
			
				//user might have deleted this button, so redraw whole area
				NCPaint(sw, hwnd, 1, 0);
			}
			else
			{
				//otherwise, just redraw the button in its new state
				hdc = GetWindowDC(hwnd);
				DrawScrollButton(&sb->sbButtons[uCurrentButton], hdc, &rect, SBBS_NORMAL);
				ReleaseDC(hwnd, hdc);
			}
	
			break;
#endif	// INCLUDE_BUTTONS

		//The scrollbar is active
		case HTSCROLL_LEFT:  case HTSCROLL_RIGHT: 
		case HTSCROLL_PAGELEFT:  case HTSCROLL_PAGERIGHT: 
		case HTSCROLL_NONE:
			
			KillTimer(hwnd, uScrollTimerId);

		case HTSCROLL_THUMB: 
	
			//In case we were thumb tracking, make sure we stop NOW
			if (sw->fThumbTracking == TRUE)
			{
				SendScrollMessage(hwnd, uScrollTimerMsg, SB_THUMBPOSITION, nLastPos);
				sw->fThumbTracking = FALSE;
			}

			//send the SB_ENDSCROLL message now that scrolling has finished
			SendScrollMessage(hwnd, uScrollTimerMsg, SB_ENDSCROLL, 0);

			//adjust the total scroll area to become where the scrollbar
			//really is (take into account the inserted buttons)
			GetRealScrollRect(sb, &rect);
			OffsetRect(&rect, -winrect.left, -winrect.top);
			hdc = GetWindowDC(hwnd);
			
			//draw whichever scrollbar sb is
			NCDrawScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_NORMAL);

			ReleaseDC(hwnd, hdc);
			break;
		}

		//reset our state to default
		uCurrentScrollPortion = HTSCROLL_NONE;
		uScrollTimerPortion	  = HTSCROLL_NONE;
		uScrollTimerId		  = 0;

		uScrollTimerMsg       = 0;
		uCurrentScrollbar     = COOLSB_NONE;

		return 0;
	}
	else
	{
		/*
		// Can't remember why I did this!
		if (GetCapture() == hwnd)
		{
			ReleaseCapture();
		}*/
	}

	return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_LBUTTONUP, wParam, lParam);
}

//
//	This function is called whenever the mouse is moved and 
//  we are dragging the scrollbar thumb about.
//
static LRESULT ThumbTrackHorz(SCROLLBAR *sbar, HWND hwnd, int x, int y)
{
	POINT pt;
	RECT rc, winrect, rc2;
	COLORREF crCheck1 = GetSBForeColor();
	COLORREF crCheck2 = GetSBBackColor();
	HDC hdc;
	int thumbpos = nThumbPos;
	int pos;
	int siMaxMin = 0;
	UINT flatflag = sbar->fFlatScrollbar ? BF_FLAT : 0;
	BOOL fCustomDraw = FALSE;

	SCROLLINFO *si;
	si = &sbar->scrollInfo;

	pt.x = x;
	pt.y = y;

	//draw the thumb at whatever position
	rc = rcThumbBounds;

	SetRect(&rc2, rc.left -  THUMBTRACK_SNAPDIST*2, rc.top -    THUMBTRACK_SNAPDIST, 
				  rc.right + THUMBTRACK_SNAPDIST*2, rc.bottom + THUMBTRACK_SNAPDIST);

	rc.left +=  GetScrollMetric(sbar, SM_CXHORZSB);
	rc.right -= GetScrollMetric(sbar, SM_CXHORZSB);

	//if the mouse is not in a suitable distance of the scrollbar,
	//then "snap" the thumb back to its initial position
#ifdef SNAP_THUMB_BACK
	if ( !PtInRect(&rc2, pt))
	{
		thumbpos = nThumbPos0;
	}
	//otherwise, move the thumb to where the mouse is
	else
#endif //SNAP_THUMB_BACK
	{
		//keep the thumb within the scrollbar limits
		thumbpos = pt.x - nThumbMouseOffset;
		if (thumbpos < rc.left) thumbpos = rc.left;
		if (thumbpos > rc.right - nThumbSize) thumbpos = rc.right - nThumbSize;
	}

	GetWindowRect(hwnd, &winrect);

	if (sbar->nBarType == SB_VERT)
		RotateRect(&winrect);
	
	hdc = GetWindowDC(hwnd);
		
//#ifdef CUSTOM_DRAW
	fCustomDraw = PostCustomPrePostPaint(hwnd, hdc, sbar, CDDS_PREPAINT) == CDRF_SKIPDEFAULT;
//#endif

	OffsetRect(&rc, -winrect.left, -winrect.top);
	thumbpos -= winrect.left;

	//draw the margin before the thumb
	SetRect(&rc2, rc.left, rc.top, thumbpos, rc.bottom);
	RotateRect0(sbar, &rc2);

	if (fCustomDraw)
		PostCustomDrawNotify(hwnd, hdc, sbar->nBarType, &rc2, SB_PAGELEFT, 0, 0, 0);
	else
		DrawCheckedRect(hdc, &rc2, crCheck1, crCheck2);
	
	RotateRect0(sbar, &rc2);

	//draw the margin after the thumb 
	SetRect(&rc2, thumbpos+nThumbSize, rc.top, rc.right, rc.bottom);
	
	RotateRect0(sbar, &rc2);
	
	if (fCustomDraw)
		PostCustomDrawNotify(hwnd, hdc, sbar->nBarType, &rc2, SB_PAGERIGHT, 0, 0, 0);
	else
		DrawCheckedRect(hdc, &rc2, crCheck1, crCheck2);
	
	RotateRect0(sbar, &rc2);
	
	//finally draw the thumb itelf. This is how it looks on win2000, anyway
	SetRect(&rc2, thumbpos, rc.top, thumbpos+nThumbSize, rc.bottom);
	
	RotateRect0(sbar, &rc2);

	if (fCustomDraw)
		PostCustomDrawNotify(hwnd, hdc, sbar->nBarType, &rc2, SB_THUMBTRACK, TRUE, TRUE, FALSE);
	else
	{

#ifdef FLAT_SCROLLBARS	
		if (sbar->fFlatScrollbar)
			PaintRect(hdc, &rc2, GetSysColor(COLOR_3DSHADOW));
		else
#endif
		{
				DrawBlankButton(hdc, &rc2, flatflag);
		}
	}

	RotateRect0(sbar, &rc2);
	ReleaseDC(hwnd, hdc);

	//post a SB_TRACKPOS message!!!
	siMaxMin = si->nMax - si->nMin;

	if (siMaxMin > 0)
		pos = MulDiv(thumbpos-rc.left, siMaxMin-si->nPage + 1, rc.right-rc.left-nThumbSize);
	else
		pos = thumbpos - rc.left;

	if (pos != nLastPos)
	{
		si->nTrackPos = pos;	
		SendScrollMessage(hwnd, uScrollTimerMsg, SB_THUMBTRACK, pos);
	}

	nLastPos = pos;

//#ifdef CUSTOM_DRAW
	PostCustomPrePostPaint(hwnd, hdc, sbar, CDDS_POSTPAINT);
//#endif
	
	return 0;
}

//
//	remember to rotate the thumb bounds rectangle!!
//
static LRESULT ThumbTrackVert(SCROLLBAR *sb, HWND hwnd, int x, int y)
{
	//sw->swapcoords = TRUE;
	RotateRect(&rcThumbBounds);
	ThumbTrackHorz(sb, hwnd, y, x);
	RotateRect(&rcThumbBounds);
	//sw->swapcoords = FALSE;

	return 0;
}

//
//	Called when we have set the capture from the NCLButtonDown(...)
//	
static LRESULT MouseMove(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
{
	RECT rect;
	UINT thisportion;
	HDC hdc;
	static UINT lastportion = 0;
	static UINT lastbutton = 0;
	POINT pt;
	RECT winrect;
	UINT buttonIdx = 0;

	if (sw->fThumbTracking == TRUE)
	{
		int x, y;
		lParam = GetMessagePos();
		x = LOWORD(lParam);
		y = HIWORD(lParam);

		if (uCurrentScrollbar == SB_HORZ)
			return ThumbTrackHorz(&sw->sbarHorz, hwnd, x,y);

		else if (uCurrentScrollbar == SB_VERT)
			return ThumbTrackVert(&sw->sbarVert, hwnd, x,y);
	}

	if (uCurrentScrollPortion == HTSCROLL_NONE)
		return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_MOUSEMOVE, wParam, lParam);

	LPARAM nlParam;
	SCROLLBAR *sb = &sw->sbarHorz;

	nlParam = GetMessagePos();

	GetWindowRect(hwnd, &winrect);

	pt.x = LOWORD(nlParam);
	pt.y = HIWORD(nlParam);

	//emulate the mouse input on a scrollbar here...
	if (uCurrentScrollbar == SB_HORZ)
	{
		sb = &sw->sbarHorz;
	}
	else if (uCurrentScrollbar == SB_VERT)
	{
		sb = &sw->sbarVert;
	}

	//get the total area of the normal scrollbar area
	GetScrollRect(sw, sb->nBarType, hwnd, &rect);
		
	//see if we clicked in the inserted buttons / normal scrollbar
	//thisportion = GetPortion(sb, hwnd, &rect, LOWORD(lParam), HIWORD(lParam));
	thisportion = GetPortion(sb, &rect, pt.x, pt.y);
		
	//we need to do different things depending on if the
	//user is activating the scrollbar itself, or one of
	//the inserted buttons
	switch(uCurrentScrollPortion) {
#ifdef INCLUDE_BUTTONS
	//inserted buttons are being clicked
	case HTSCROLL_INSERTED:
			
		//find the index of the button that has been clicked
		//Don't adjust the rectangle though
		buttonIdx = GetButtonFromPt(sb, &rect, pt, FALSE);
						
		//Get the rectangle of the active button
		GetButtonRectFromId(sb, &rect, uCurrentButton);

		//if the button to the LEFT of the current 
		//button is resizable, then resize it
#ifdef RESIZABLE_BUTTONS
		if (uCurrentButton > 0)
		{
			sbut = &sb->sbButtons[uCurrentButton - 1];
			
			//only resize if BOTH buttons are on same side of scrollbar
			if (sbut->uPlacement == (sbut+1)->uPlacement && (sbut->uButType & SBBM_RESIZABLE))
			{
				int oldsize = sbut->nSize;
				int butsize1, butsize2;
				RECT rect2;
				int scrollsize;

				if (uCurrentScrollbar == SB_HORZ)
				{
					rect.left -= GetSingleButSize(sb, sbut);
					sbut->nSize = pt.x - rect.left;
				}
				else
				{
					rect.top -= GetSingleButSize(sb, sbut);
					sbut->nSize = pt.y - rect.top;
				}

				//if (sbut->nSize < 0)	sbut->nSize = 0;
				if (sbut->nSize < (int)sbut->nMinSize)
					sbut->nSize = sbut->nMinSize;

				if ((UINT)sbut->nSize > (UINT)sbut->nMaxSize)
					sbut->nSize = sbut->nMaxSize;
					
				GetScrollRect(sw, uCurrentScrollbar, hwnd, &rect2);
					
				if (uCurrentScrollbar == SB_HORZ)
					scrollsize = rect2.right-rect2.left;
				else
					scrollsize = rect2.bottom-rect2.top;

				butsize1 = GetButtonSize(sb, hwnd, SBBP_LEFT);
				butsize2 = GetButtonSize(sb, hwnd, SBBP_RIGHT);

				//adjust the button size if it gets too big
				if (butsize1 + butsize2 > scrollsize  - MINSCROLLSIZE)
				{
					sbut->nSize -= (butsize1+butsize2) - (scrollsize - MINSCROLLSIZE);
				}
					
				//remember what size the USER set the button to
				sbut->nSizeReserved = sbut->nSize;
				NCPaint(sw, hwnd, 1, 0);
				return 0;
			}
		}
#endif	//RESIZABLE_BUTTONS			
			
		OffsetRect(&rect, -winrect.left, -winrect.top);

		hdc = GetWindowDC(hwnd);
			
		//if the button under the mouse is not the active button,
		//then display the active button in its normal state
		if (buttonIdx != uCurrentButton 
			//include this if toggle buttons always stay depressed
			//if they are being activated
			&& (sb->sbButtons[uCurrentButton].uButType & SBBT_MASK) != SBBT_TOGGLEBUTTON)
		{
			if (lastbutton != buttonIdx)
				DrawScrollButton(&sb->sbButtons[uCurrentButton], hdc, &rect, SBBS_NORMAL);
		}
		//otherwise, depress the active button if the mouse is over
		//it (just like a normal scroll button works)
		else
		{
			if (lastbutton != buttonIdx)
				DrawScrollButton(&sb->sbButtons[uCurrentButton], hdc, &rect, SBBS_PUSHED);
		}

		ReleaseDC(hwnd, hdc);
		return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_MOUSEMOVE, wParam, lParam);
		//break;

#endif	//INCLUDE_BUTTONS

	//The scrollbar is active
	case HTSCROLL_LEFT:		 case HTSCROLL_RIGHT:case HTSCROLL_THUMB: 
	case HTSCROLL_PAGELEFT:  case HTSCROLL_PAGERIGHT: 
	case HTSCROLL_NONE:
		//adjust the total scroll area to become where the scrollbar
		//really is (take into account the inserted buttons)
		GetRealScrollRect(sb, &rect);

		OffsetRect(&rect, -winrect.left, -winrect.top);
		hdc = GetWindowDC(hwnd);
		
		if (thisportion != uCurrentScrollPortion)
		{
			uScrollTimerPortion = HTSCROLL_NONE;

			if (lastportion != thisportion)
				NCDrawScrollbar(sb, hwnd, hdc, &rect, HTSCROLL_NORMAL);
		}
		//otherwise, draw the button in its depressed / clicked state
		else
		{
			uScrollTimerPortion = uCurrentScrollPortion;

			if (lastportion != thisportion)
				NCDrawScrollbar(sb, hwnd, hdc, &rect, thisportion);
		}

		ReleaseDC(hwnd, hdc);
		break;
	}

	lastportion = thisportion;
	lastbutton  = buttonIdx;
	return 0;
}

#ifdef INCLUDE_BUTTONS
#ifdef RESIZABLE_BUTTONS
//
//	Any resizable buttons must be shrunk to fit if the window is made too small
//
static void ResizeButtonsToFit(SCROLLWND *sw, SCROLLBAR *sbar, HWND hwnd)
{
	int butsize1, butsize2;
	RECT rc;
	int scrollsize;
	int i;
	SCROLLBUT *sbut;

	//make sure that the scrollbar can fit into space, by
	//shrinking any resizable buttons
	GetScrollRect(sw, sbar->nBarType, hwnd, &rc);

	if (sbar->nBarType == SB_HORZ)
		scrollsize = rc.right-rc.left;
	else
		scrollsize = rc.bottom-rc.top;

	//restore any resizable buttons to their user-defined sizes,
	//before shrinking them to fit. This means when we make the window
	//bigger, the buttons will restore to their initial sizes
	for (i = 0; i < sbar->nButtons; i++)
	{
		sbut = &sbar->sbButtons[i];
		if (sbut->uButType & SBBM_RESIZABLE)
		{
			sbut->nSize = sbut->nSizeReserved;
		}
	}
	
	butsize1 = GetButtonSize(sbar, hwnd, SBBP_LEFT);
	butsize2 = GetButtonSize(sbar, hwnd, SBBP_RIGHT);

	if (butsize1 + butsize2 > scrollsize - MINSCROLLSIZE)
	{
		i = 0;
		while(i < sbar->nButtons && 
			butsize1 + butsize2 > scrollsize - MINSCROLLSIZE)
		{
			sbut = &sbar->sbButtons[i++];
			if (sbut->uButType & SBBM_RESIZABLE)
			{
				int oldsize = sbut->nSize;
				sbut->nSize -= (butsize1+butsize2) - (scrollsize-MINSCROLLSIZE);

				if (sbut->nSize < (int)sbut->nMinSize)
					sbut->nSize = sbut->nMinSize;

				if ((UINT)sbut->nSize > (UINT)sbut->nMaxSize)
					sbut->nSize = sbut->nMaxSize;

				
				butsize1 -= (oldsize - sbut->nSize);
			}
		}
	}

}
#endif
#endif

//
//	We must allocate from in the non-client area for our scrollbars
//	Call the default window procedure first, to get the borders (if any)
//	allocated some space, then allocate the space for the scrollbars
//	if they fit
//
static LRESULT NCCalcSize(SCROLLWND *sw, HWND hwnd, WPARAM wParam, LPARAM lParam)
{
	NCCALCSIZE_PARAMS *nccsp;
	RECT *rect;
	RECT oldrect;
	SCROLLBAR *sb;
	LRESULT ret;
	DWORD dwStyle;

	//Regardless of the value of fCalcValidRects, the first rectangle 
	//in the array specified by the rgrc structure member of the 
	//NCCALCSIZE_PARAMS structure contains the coordinates of the window,
	//so we can use the exact same code to modify this rectangle, when
	//wParam is TRUE and when it is FALSE.
	nccsp = (NCCALCSIZE_PARAMS *)lParam;
	rect = &nccsp->rgrc[0];
	oldrect = *rect;

	dwStyle = GetWindowLongPtr(hwnd, GWL_STYLE);

	// TURN OFF SCROLL-STYLES.
    if ( dwStyle & (WS_VSCROLL|WS_HSCROLL))
    {
        sw->bPreventStyleChange = TRUE;
        SetWindowLongPtr(hwnd, GWL_STYLE, dwStyle & ~(WS_VSCROLL|WS_HSCROLL));
    }
	
	//call the default procedure to get the borders allocated
	ret = mir_callNextSubclass(hwnd, CoolSBWndProc, WM_NCCALCSIZE, wParam, lParam);

	// RESTORE PREVIOUS STYLES (if present at all)
    if ( dwStyle & (WS_VSCROLL|WS_HSCROLL))
    {
        SetWindowLongPtr(hwnd, GWL_STYLE, dwStyle);
        sw->bPreventStyleChange = FALSE;
    }

	// calculate what the size of each window border is,
	sw->cxLeftEdge   = rect->left     - oldrect.left;
	sw->cxRightEdge  = oldrect.right  - rect->right;
	sw->cyTopEdge    = rect->top      - oldrect.top;
	sw->cyBottomEdge = oldrect.bottom - rect->bottom;

	sb = &sw->sbarHorz;

	//if there is room, allocate some space for the horizontal scrollbar
	//NOTE: Change the ">" to a ">=" to make the horz bar totally fill the
	//window before disappearing
	if ((sb->fScrollFlags & CSBS_VISIBLE) && 
#ifdef COOLSB_FILLWINDOW
		rect->bottom - rect->top >= GetScrollMetric(sb, SM_CYHORZSB))
#else
		rect->bottom - rect->top > GetScrollMetric(sb, SM_CYHORZSB))
#endif
	{
		rect->bottom -= GetScrollMetric(sb, SM_CYHORZSB);
		sb->fScrollVisible = TRUE;
	}
	else
		sb->fScrollVisible = FALSE;

	sb = &sw->sbarVert;

	//if there is room, allocate some space for the vertical scrollbar
	if ((sb->fScrollFlags & CSBS_VISIBLE) && 
		rect->right - rect->left >= GetScrollMetric(sb, SM_CXVERTSB))
	{
		if (sw->fLeftScrollbar)
			rect->left  += GetScrollMetric(sb, SM_CXVERTSB);
		else
			rect->right -= GetScrollMetric(sb, SM_CXVERTSB);

		sb->fScrollVisible = TRUE;
	}
	else
		sb->fScrollVisible = FALSE;

#ifdef INCLUDE_BUTTONS
#ifdef RESIZABLE_BUTTONS
	ResizeButtonsToFit(sw, &sw->sbarHorz, hwnd);
	ResizeButtonsToFit(sw, &sw->sbarVert, hwnd);
#endif
#endif

	//don't return a value unless we actually modify the other rectangles
	//in the NCCALCSIZE_PARAMS structure. In this case, we return 0
	//no matter what the value of fCalcValidRects is
	return ret;//FALSE;
}

//
//	used for hot-tracking over the scroll buttons
//
static LRESULT NCMouseMove(SCROLLWND *sw, HWND hwnd, WPARAM wHitTest, LPARAM lParam)
{
	//install a timer for the mouse-over events, if the mouse moves
	//over one of the scrollbars
#ifdef HOT_TRACKING
	hwndCurCoolSB = hwnd;
	if (wHitTest == HTHSCROLL)
	{
		if (uMouseOverScrollbar == SB_HORZ)
			return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_NCMOUSEMOVE, wHitTest, lParam);

		uLastHitTestPortion = HTSCROLL_NONE;
		uHitTestPortion     = HTSCROLL_NONE;
		GetScrollRect(sw, SB_HORZ, hwnd, &MouseOverRect);
		uMouseOverScrollbar = SB_HORZ;
		uMouseOverId = SetTimer(hwnd, COOLSB_TIMERID3, COOLSB_TIMERINTERVAL3, 0);

		NCPaint(sw, hwnd, 1, 0);
	}
	else if (wHitTest == HTVSCROLL)
	{
		if (uMouseOverScrollbar == SB_VERT)
			return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_NCMOUSEMOVE, wHitTest, lParam);

		uLastHitTestPortion = HTSCROLL_NONE;
		uHitTestPortion     = HTSCROLL_NONE;
		GetScrollRect(sw, SB_VERT, hwnd, &MouseOverRect);
		uMouseOverScrollbar = SB_VERT;
		uMouseOverId = SetTimer(hwnd, COOLSB_TIMERID3, COOLSB_TIMERINTERVAL3, 0);

		NCPaint(sw, hwnd, 1, 0);
	}

#endif //HOT_TRACKING
	return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_NCMOUSEMOVE, wHitTest, lParam);
}

//
//	Timer routine to generate scrollbar messages
//
static LRESULT CoolSB_Timer(SCROLLWND *swnd, HWND hwnd, WPARAM wTimerId, LPARAM lParam)
{
	//let all timer messages go past if we don't have a timer installed ourselves
	if (uScrollTimerId == 0 && uMouseOverId == 0)
		return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_TIMER, wTimerId, lParam);

#ifdef HOT_TRACKING
	//mouse-over timer
	if (wTimerId == COOLSB_TIMERID3)
	{
		POINT pt;
		RECT rect, winrect;
		HDC hdc;
		SCROLLBAR *sbar;

		if (swnd->fThumbTracking)
			return 0;

		//if the mouse moves outside the current scrollbar,
		//then kill the timer..
		GetCursorPos(&pt);

		if ( !PtInRect(&MouseOverRect, pt))
		{
			KillTimer(hwnd, uMouseOverId);
			uMouseOverId = 0;
			uMouseOverScrollbar = COOLSB_NONE;
			uLastHitTestPortion = HTSCROLL_NONE;

			uHitTestPortion = HTSCROLL_NONE;
			NCPaint(swnd, hwnd, 1, 0);
		}
		else
		{
			if (uMouseOverScrollbar == SB_HORZ)
			{
				sbar = &swnd->sbarHorz;
				uHitTestPortion = GetHorzPortion(sbar, &MouseOverRect, pt.x, pt.y);
			}
			else
			{
				sbar = &swnd->sbarVert;
				uHitTestPortion = GetVertPortion(sbar, &MouseOverRect, pt.x, pt.y);
			}

			if (uLastHitTestPortion != uHitTestPortion)
			{
				rect = MouseOverRect;
				GetRealScrollRect(sbar, &rect);

				GetWindowRect(hwnd, &winrect);
				OffsetRect(&rect, -winrect.left, -winrect.top);

				hdc = GetWindowDC(hwnd);
				NCDrawScrollbar(sbar, hwnd, hdc, &rect, HTSCROLL_NONE);
				ReleaseDC(hwnd, hdc);
			}
			
			uLastHitTestPortion = uHitTestPortion;
		}

		return 0;
	}
#endif // HOT_TRACKING

	//if the first timer goes off, then we can start a more
	//regular timer interval to auto-generate scroll messages
	//this gives a slight pause between first pressing the scroll arrow, and the
	//actual scroll starting
	if (wTimerId == COOLSB_TIMERID1)
	{
		KillTimer(hwnd, uScrollTimerId);
		uScrollTimerId = SetTimer(hwnd, COOLSB_TIMERID2, COOLSB_TIMERINTERVAL2, 0);
		return 0;
	}
	//send the scrollbar message repeatedly
	else if (wTimerId == COOLSB_TIMERID2)
	{
		//need to process a spoof WM_MOUSEMOVE, so that
		//we know where the mouse is each time the scroll timer goes off.
		//This is so we can stop sending scroll messages if the thumb moves
		//under the mouse.
		POINT pt;
		GetCursorPos(&pt);
		ScreenToClient(hwnd, &pt);
		
		MouseMove(swnd, hwnd, MK_LBUTTON, MAKELPARAM(pt.x, pt.y));

		if (uScrollTimerPortion != HTSCROLL_NONE)
			SendScrollMessage(hwnd, uScrollTimerMsg, uScrollTimerPortion, 0);
		
		return 0;
	}

	return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_TIMER, wTimerId, lParam);
}

//
//	We must intercept any calls to SetWindowLongPtr, to check if
//  left-scrollbars are taking effect or not
//
static LRESULT CoolSB_StyleChange(SCROLLWND *swnd, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	STYLESTRUCT *ss = (STYLESTRUCT *)lParam;

	if (wParam == GWL_EXSTYLE)
	{
		if (ss->styleNew & WS_EX_LEFTSCROLLBAR)
			swnd->fLeftScrollbar = TRUE;
		else
			swnd->fLeftScrollbar = FALSE;
	}

	return mir_callNextSubclass(hwnd, CoolSBWndProc, msg, wParam, lParam);
}

static UINT curTool = -1;
static LRESULT CoolSB_Notify(SCROLLWND* /*swnd*/, HWND hwnd, WPARAM wParam, LPARAM lParam)
{
#ifdef COOLSB_TOOLTIPS

	NMTTDISPINFO *nmdi = (NMTTDISPINFO *)lParam;

	if (nmdi->hdr.hwndFrom == swnd->hwndToolTip &&
		nmdi->hdr.code == TTN_GETDISPINFO)
	{
		//convert the tooltip notify from a "ISHWND" style
		//request to an id-based request. 
		//We do this because our tooltip is a window-style
		//tip, with no tools, and the GETDISPINFO request must
		//indicate which button to retrieve the text for
		//nmdi->hdr.idFrom = curTool;
		nmdi->hdr.idFrom = curTool;
		nmdi->hinst = GetModuleHandle(0);
		nmdi->uFlags &= ~TTF_IDISHWND;
	}
#endif	//COOLSB_TOOLTIPS

	return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_NOTIFY, wParam, lParam);	
}

static LRESULT SendToolTipMessage0(HWND hwndTT, UINT message, WPARAM wParam, LPARAM lParam)
{
	return SendMessage(hwndTT, message, wParam, lParam);
}

#ifdef COOLSB_TOOLTIPS
#define SendToolTipMessage		SendToolTipMessage0
#else
#define SendToolTipMessage		1 ? (void)0 : SendToolTipMessage0
#endif


//
//	We must intercept any calls to SetWindowLongPtr, to make sure that
//	the user does not set the WS_VSCROLL or WS_HSCROLL styles
//
static LRESULT CoolSB_SetCursor(SCROLLWND* /*swnd*/, HWND hwnd, WPARAM wParam, LPARAM lParam)
{
#ifdef INCLUDE_BUTTONS
	UINT lo = LOWORD(lParam);
	UINT hi = HIWORD(lParam);
	UINT xy;
	RECT rect;
	SCROLLBAR *sbar;
	SCROLLBUT *sbut;
	POINT pt;
	UINT id;
	static UINT lastid;

#ifdef HIDE_CURSOR_AFTER_MOUSEUP
	static UINT lastmsg;
	if (lastmsg == WM_LBUTTONDOWN)
	{
		lastmsg =  hi;
		return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_SETCURSOR, wParam, lParam);	
	}
	else
		lastmsg =  hi;
#endif
	
	//if we are over either or our scrollbars
	if (lo == HTHSCROLL || lo == HTVSCROLL)
	{
		xy = GetMessagePos();
		pt.x = LOWORD(xy);
		pt.y = HIWORD(xy);

		if (lo == HTHSCROLL)
		{
			sbar = &swnd->sbarHorz;
			GetScrollRect(swnd, SB_HORZ, hwnd, &rect);
			id = GetHorzPortion(sbar, hwnd, &rect, pt.x, pt.y);
		}
		else
		{
			sbar = &swnd->sbarVert;
			GetScrollRect(swnd, SB_VERT, hwnd, &rect);
			id = GetVertPortion(sbar, hwnd, &rect, pt.x, pt.y);
		}

		if (id != HTSCROLL_INSERTED)
		{
			if (swnd->hwndToolTip != 0)
			{
				SendToolTipMessage(swnd->hwndToolTip, TTM_ACTIVATE, FALSE, 0);
				SendToolTipMessage(swnd->hwndToolTip, TTM_POP, 0, 0);
			}

			return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_SETCURSOR, wParam, lParam);
		}
		
		if (swnd->hwndToolTip != 0)
		{
			SendToolTipMessage(swnd->hwndToolTip, TTM_ACTIVATE, TRUE, 0);
		}

		//set the cursor if one has been specified
		if ((id = GetButtonFromPt(sbar, &rect, pt, TRUE)) != -1)
		{
			sbut = &sbar->sbButtons[id];
			curTool = sbut->uCmdId;

			if (lastid != id && swnd->hwndToolTip != 0)
			{
				if (IsWindowVisible(swnd->hwndToolTip))
					SendToolTipMessage(swnd->hwndToolTip, TTM_UPDATE, TRUE, 0);
			}

			lastid = id;

			if (sbut->hCurs != 0)
			{
				SetCursor(sbut->hCurs);
				return 0;
			}
		}
		else
		{
			curTool = -1;
			lastid = -1;
		}
	}
	else if (swnd->hwndToolTip != 0)
	{
		SendToolTipMessage(swnd->hwndToolTip, TTM_ACTIVATE, FALSE, 0);
		SendToolTipMessage(swnd->hwndToolTip, TTM_POP, 0, 0);
	}

#endif	//INCLUDE_BUTTONS
	return mir_callNextSubclass(hwnd, CoolSBWndProc, WM_SETCURSOR, wParam, lParam);
}


//
//	Send the specified message to the tooltip control
//
static void __stdcall RelayMouseEvent(HWND hwnd, HWND hwndToolTip, UINT event)
{
#ifdef COOLSB_TOOLTIPS
	MSG msg;

	CoolSB_ZeroMemory(&msg, sizeof(MSG));
	msg.hwnd = hwnd;
	msg.message = event;

	SendMessage(hwndToolTip, TTM_RELAYEVENT, 0, (LONG)&msg);
#else
	UNREFERENCED_PARAMETER(hwnd);
	UNREFERENCED_PARAMETER(hwndToolTip);
	UNREFERENCED_PARAMETER(event);
#endif
}


//
//  CoolScrollbar subclass procedure.
//	Handle all messages needed to mimick normal windows scrollbars
//
LRESULT CALLBACK CoolSBWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	SCROLLWND *swnd = GetScrollWndFromHwnd(hwnd);
	static int count;

	switch(message) {
	case WM_NCDESTROY:
		//this should NEVER be called, because the user
		//should have called Uninitialize() themselves.

		//However, if the user tries to call Uninitialize().. 
		//after this window is destroyed, this window's entry in the lookup
		//table will not be there, and the call will fail
		UninitializeCoolSB(hwnd);
		
		//we must call the original window procedure, otherwise it
		//will never get the WM_NCDESTROY message, and it wouldn't
		//be able to clean up etc.
		return mir_callNextSubclass(hwnd, CoolSBWndProc, message, wParam, lParam);

    case WM_NCCALCSIZE:
		return NCCalcSize(swnd, hwnd, wParam, lParam);

	case WM_NCPAINT:
		return NCPaint(swnd, hwnd, wParam, lParam);	

	case WM_NCHITTEST:
		return NCHitTest(swnd, hwnd, wParam, lParam);

	case WM_NCRBUTTONDOWN: case WM_NCRBUTTONUP: 
	case WM_NCMBUTTONDOWN: case WM_NCMBUTTONUP: 
		RelayMouseEvent(hwnd, swnd->hwndToolTip, (WM_MOUSEMOVE-WM_NCMOUSEMOVE) + (message));
		if (wParam == HTHSCROLL || wParam == HTVSCROLL) 
			return 0;
		else 
			break;

	case WM_NCLBUTTONDBLCLK:
		//TRACE("WM_NCLBUTTONDBLCLK %d\n", count++);
		if (wParam == HTHSCROLL || wParam == HTVSCROLL)
			return NCLButtonDown(swnd, hwnd, wParam, lParam);
		else
			break;

	case WM_NCLBUTTONDOWN:
		//TRACE("WM_NCLBUTTONDOWN%d\n", count++);
		RelayMouseEvent(hwnd, swnd->hwndToolTip, WM_LBUTTONDOWN);
		return NCLButtonDown(swnd, hwnd, wParam, lParam);


	case WM_LBUTTONUP:
		//TRACE("WM_LBUTTONUP %d\n", count++);
		RelayMouseEvent(hwnd, swnd->hwndToolTip, WM_LBUTTONUP);
		return LButtonUp(swnd, hwnd, wParam, lParam);

	case WM_NOTIFY:
		return CoolSB_Notify(swnd, hwnd, wParam, lParam);

	//Mouse moves are received when we set the mouse capture,
	//even when the mouse moves over the non-client area
	case WM_MOUSEMOVE: 
		//TRACE("WM_MOUSEMOVE %d\n", count++);
		return MouseMove(swnd, hwnd, wParam, lParam);
	
	case WM_TIMER:
		return CoolSB_Timer(swnd, hwnd, wParam, lParam);

	//case WM_STYLECHANGING:
	//	return CoolSB_StyleChange(swnd, hwnd, WM_STYLECHANGING, wParam, lParam);
	case WM_STYLECHANGED:

		if (swnd->bPreventStyleChange)
		{
			// the NCPAINT handler has told us to eat this message!
			return 0;
		}
		else
		{
            if (message == WM_STYLECHANGED) 
				return CoolSB_StyleChange(swnd, hwnd, WM_STYLECHANGED, wParam, lParam);
			else
				break;
		}

	case WM_NCMOUSEMOVE: 
		{
			static LONG_PTR lastpos = -1;

			//TRACE("WM_NCMOUSEMOVE %d\n", count++);

			//The problem with NCMOUSEMOVE is that it is sent continuously
			//even when the mouse is stationary (under win2000 / win98)
			//
			//Tooltips don't like being sent a continous stream of mouse-moves
			//if the cursor isn't moving, because they will think that the mouse
			//is moving position, and the internal timer will never expire
			//
			if (lastpos != lParam)
			{
				RelayMouseEvent(hwnd, swnd->hwndToolTip, WM_MOUSEMOVE);
				lastpos = lParam;
			}
		}

		return NCMouseMove(swnd, hwnd, wParam, lParam);


	case WM_SETCURSOR:
		return CoolSB_SetCursor(swnd, hwnd, wParam, lParam);

	case WM_CAPTURECHANGED:
		break;

	default:
		break;
	}
	
	return mir_callNextSubclass(hwnd, CoolSBWndProc, message, wParam, lParam);
}