File: VirtualShellToolBar.pas

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (4355 lines) | stat: -rw-r--r-- 135,785 bytes parent folder | download
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
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
unit VirtualShellToolBar;

// Version 1.1.17
//   The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.mozilla.org/MPL/
//
//   Software distributed under the License is distributed on an
// " AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or
// implied. See the License for the specific language governing rights
// and limitations under the License.
//
//
//   Alternatively, the contents of this file may be used under
// the terms of the GNU General Public License Version 2 or later
// (the "GPL"), in which case the provisions of the GPL are applicable
// instead of those above. If you wish to allow use of your version of
// this file only under the terms of the GPL and not to allow others to
// use your version of this file under the MPL, indicate your decision
// by deleting the provisions above and replace them with the notice and
// other provisions required by the GPL. If you do not delete the provisions
// above, a recipient may use your version of this file under either the
// MPL or the GPL.
//
// The initial developer of this code is Jim Kueneman <jimdk@mindspring.com>
//
//----------------------------------------------------------------------------
//

interface

{$include Compilers.inc}
{$include ..\Include\VSToolsAddIns.inc}

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Buttons, VirtualShellUtilities, VirtualWideStrings, ShellAPI, CommCtrl, ShlObj, ActiveX,
  Imglist, VirtualExplorerTree, ActnList, ToolWin, VirtualDataObject, VirtualPIDLTools,
  VirtualUtilities, VirtualShellTypes, VirtualResources, VirtualUnicodeDefines,
  VirtualSystemImageLists,
  {$IFDEF SHELLNOTIFIER} VirtualShellNotifier, {$ENDIF}
  {$IFDEF THREADEDICONS} VirtualIconThread, {$ENDIF}
  {$IFDEF DELPHI_7_UP}
  Themes,
  {$ELSE}
  TMSchema, // Windows XP themes support for D5-D6. Get these units from www.delphi-gems.com.
  {$ENDIF}
  UxTheme;  // Windows XP themes support for D5-D6. Get these units from www.delphi-gems.com.

const

  HOTTRACKDELAY = 100;         // Every 100ms look for the mouse to be out of the window
  CFSTR_VSTSHELLTOOLBAR = 'VST ShellToolbar';

type
   TCaptionOption = (
    coFolderName,     // Button will show the folder name for the caption
    coFolderPath,     // Button will show the full path of the folder
    coNoExtension,    // Button will show the folder name minus the extension
    coDriveLetterOnly // Only shows the drive letter for DriveToolbar
  );
  TCaptionOptions = set of TCaptionOption;


  TButtonPaintState = (
    psNormal,         // Button is in its normal state
    psHot,            // Button is in a Hot state, i.e. the mouse is over it
    psPressed,        // Button is Pressed
    psDropTarget      // Button is a drop target
  );

  TButtonState = (
    bsMouseInButton,
    bsThemesActive,
    bsRMouseButtonDown
  );
  TButtonStates = set of TButtonState;

  TShellToolbarContent = (
    stcFolders,     //  Folders are allowed to be dropped into toolbar
    stcFiles,       //  Any file is allowed to be dropped into toolbar
    stcPrograms     //  Program files (exe, bat, com) files are allowed to dropped into toolbar
  );
  TShellToolbarContents = set of TShellToolbarContent;

  TVirtualToolbarOption = (
    toAnimateHot,            // Pops the image and text "up" on mouse over
    toContextMenu,           // Allows RightClick ContextMenu on the buttons
    toCustomizable,          // The user can rearrange the buttons by drag drop
    toEqualWidth,            // In horz mode all button are the same size
    toFlat,                  // Draws the buttons in a flat state
    toInsertDropable,        // The user can add more buttons to the toolbar by drag and drop
    toLaunchDropable,        // The user can drop an object on the button to launch to button object
    {$IFDEF SHELLNOTIFIER}
    toShellNotifyThread,     // Monitor shell activing to keep refreshed
    {$ENDIF}
    toThemeAware,            // Use Themes in XP
    {$IFDEF THREADEDICONS}
    toThreadedImages,
    {$ENDIF}
    toTile,                  // The buttons are tiled to fit in the window
    toTransparent,           // The background is not painted
    toUserDefinedClickAction,
    toLargeButtons           // Use large button images instead of small ones.
  );
  TVirtualToolbarOptions = set of TVirtualToolbarOption;

  TVirtualToolbarState = (
    tsBackBitsStale,         // The background bitmap (for transparency) needs refreshing
    tsThemesActive,          // Cached value since UsesThemes is slow in the theme units
    tsShellIDListValid,      // The CF_SHELLIDLIST format is available in the dataObject and contains the right content to be droppped
    tsVSTShellToobarValid,   // The CF_VSTSHELLTOOLBAR format is available in the dataObject and can be used
    tsDragInLaunchZone,      // Current position of drag is on a button where a drop will launch the dropped files
    tsDragInDropZone,        // Current postion of drag is in an area where a drop will be interperted as an insert request
    tsInsertImageVisible     // Set if the InsertImage is currently being shown
  );
  TVirtualToolbarStates = set of TVirtualToolbarState;

  TDriveSpecialFolder = (
    dsfDesktop,              // Show Desktop in TXPDriveToolbar
    dsfMyComputer,           // Show MyDesktop in TVirtualDriveToolbar
    dsfNetworkNeighborhood,  // Show NetworkNeighborhood in TVirtualDriveToolbar
    dsfRemovable,            // Show all Removable drives
    dsfReadOnly,             // Show all ReadOnly drives (CD for example)
    dsfFixedDrive             // Show all fixed drives
  );
  TDriveSpecialFolders = set of TDriveSpecialFolder;

  TSpecialFolder = ( // Defines what special folder to show in TSpecialFolderToolbar
    sfAdminTools,
    sfAltStartup,
    sfAppData,
    sfBitBucket,
    sfControlPanel,
    sfCookies,
    sfDesktop,
    sfDesktopDirectory,
    sfDrives,
    sfFavorites,
    sfFonts,
    sfHistory,
    sfInternet,
    sfInternetCache,
    sfLocalAppData,
    sfMyPictures,
    sfNetHood,
    sfNetwork,
    sfPersonal,
    sfPrinters,
    sfPrintHood,
    sfProfile,
    sfProgramFiles,
    sfCommonProgramFiles,
    sfPrograms,
    sfRecent,
    sfSendTo,
    sfStartMenu,
    sfStartUp,
    sfSystem,
    sfTemplate,
    sfWindows
  );
  TSpecialFolders = set of TSpecialFolder;

  TSpecialCommonFolder = (  // Had to split this off TSpecialFolders, more than 32 items
    sfCommonAdminTools,
    sfCommonAltStartup,
    sfCommonAppData,
    sfCommonDesktopDirectory,
    sfCommonDocuments,
    sfCommonFavorties,
    sfCommonPrograms,
    sfCommonStartMenu,
    sfCommonStartup,
    sfCommonTemplates
  );
  TSpecialCommonFolders = set of TSpecialCommonFolder;

type
  TCustomVirtualToolbar = class;              // forward
  TCustomWideSpeedButton = class;             // forward
  TShellToolButton = class;                   // forward

  PClipRec = ^TClipRec;
  TClipRec = packed record
    ButtonInstance: Pointer;
    Process: Cardinal;
    PIDLSize: integer;
    PIDL: array[0..0] of Byte;
  end;

  TVirtualButtonList = class(TList)
  private
    FToolbar: TCustomVirtualToolbar;  // Toolbar associated with this list
    FUpdateCount: integer;       // Used to stop any screen updates until EndUpdate is call and FUpdateCount goes to 0

    function GetItems(Index: integer): TCustomWideSpeedButton;
    procedure SetItems(Index: integer; const Value: TCustomWideSpeedButton);
  protected
    function CreateToolButton: TCustomWideSpeedButton; virtual;
  public
    function AddButton(Index: integer = -1): TCustomWideSpeedButton; virtual;
    procedure RemoveButton(Button: TCustomWideSpeedButton);
    procedure BeginUpdate;
    procedure EndUpdate;

    procedure Clear; override;

    property Items[Index: integer]: TCustomWideSpeedButton read GetItems write SetItems; default;
    property Toolbar: TCustomVirtualToolbar read FToolbar write FToolbar;
  end;

  {$IFNDEF T2H}
  TVirtualShellButtonList = class(TVirtualButtonList)
  private
    function GetItems(Index: integer): TShellToolButton;
    procedure SetItems(Index: integer; const Value: TShellToolButton);
  protected
    function CreateToolButton: TCustomWideSpeedButton; override;

  public
    function AddButton(Index: integer = -1): TCustomWideSpeedButton; override;

    property Items[Index: integer]: TShellToolButton read GetItems write SetItems; default;
  end;
  {$ENDIF T2H}

  // Basic Toolbutton that can be created an placed on a TVirtualToolbar
  TCustomWideSpeedButton = class(TGraphicControl, IDropSource)
  private
    FCaption: WideString;             // The caption to be displayed
    FImageIndex: integer;             // Index of the image in the Toolbar's ImageList
    FPaintState: TButtonPaintState;   // Flags to determine how the button is painted
    FSpacing: integer;
    FMargin: integer;
    FLayout: TButtonLayout;
    FImageList: TCustomImageList;
    FOldOnFontChange: TNotifyEvent;
    FFlat: Boolean;
    FThemeAware: Boolean;
    FTransparent: Boolean;
    FOnDblClk: TNotifyEvent;
    FOnMouseLeave: TNotifyEvent;
    FOnMouseEnter: TNotifyEvent;
    FThemeToolbar: HTheme;
    FTimer: THandle;
    FTimerStub: Pointer;
    FState: TButtonStates;
    FDragging: Boolean;
    FImageListChangeLink: TChangeLink;
    FHotAnimate: Boolean;
    FOLEDraggable: Boolean;

    function GetBottom: integer;
    function GetCaption: WideString; virtual;
    function GetImageIndex: integer; virtual;
    function GetImageList: TCustomImageList;
    function GetRight: integer;
    function GetThemeToolbar: HTheme;
    procedure ReadCaption(Reader: TReader);
    procedure SetCaption(const Value: WideString);
    procedure SetFlat(const Value: Boolean);
    procedure SetImageIndex(const Value: integer);
    procedure SetImageList(const Value: TCustomImageList);
    procedure SetLayout(const Value: TButtonLayout);
    procedure SetMargin(const Value: integer);
    procedure SetPaintState(const Value: TButtonPaintState);
    procedure SetSpacing(const Value: integer);
    procedure SetTransparent(const Value: Boolean);
    procedure SetThemeAware(const Value: Boolean);
    procedure WriteCaption(Writer: TWriter);

  protected
    procedure ActionChange(Sender: TObject; CheckDefaults: Boolean); override;
    procedure CalcButtonLayout(DC: HDC; const Client: TRect; const Offset: TPoint;
      const Caption: WideString; Layout: TButtonLayout; Margin, Spacing: Integer;
      var GlyphPos: TPoint; var TextBounds: TRect; BiDiFlags: Integer);
    function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
    procedure DoDblClk; virtual;
    procedure DoMouseLeave; virtual;
    procedure DoMouseEnter; virtual;
    function DragEnter(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; virtual;
    function DragOverOLE(grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; virtual;
    function DragLeave: HResult; virtual;
    procedure DrawButtonText(DC: HDC; const Caption: WideString; TextBounds: TRect;
      Enabled: Boolean; BiDiFlags: Longint);
    function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; virtual;
    procedure FontChange(Sender: TObject);
    procedure FreeThemes; dynamic;
    function GiveFeedback(dwEffect: Longint): HResult; virtual; stdcall;
    procedure ImageListChange(Sender: TObject);
    function LoadFromDataObject(const DataObject: IVirtualDataObject): Boolean; virtual;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    procedure Paint; override;
    procedure PaintButton(DC: HDC; ForDragImage: Boolean = False); virtual;
    function QueryContinueDrag(fEscapePressed: BOOL; grfKeyState: Longint): HResult; virtual; stdcall;
    function SaveToDataObject(const DataObject: IVirtualDataObject): Boolean; virtual;
    procedure SetBoundsR(Rect: TRect); // Sets the bounds using a TRect
    procedure SetParent(AParent: TWinControl); override;
    procedure StartHotTimer;
    procedure TimerStubProc(Wnd: HWnd; uMsg: UINT; idEvent: UINT; dwTime: DWORD); stdcall;

    procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
    procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
    procedure WMRButtonDown(var Message: TWMRButtonDown); message WM_RBUTTONDOWN;
    procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LBUTTONDBLCLK;
    procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
    procedure WMRButtonUp(var Message: TWMRButtonUp); message WM_RBUTTONUP;
    procedure WMMouseMove(var Message: TWMMouseMove); message WM_MOUSEMOVE;
    procedure WMThemeChanged(var Message: TMessage); message WM_THEMECHANGED;

    property Caption: WideString read GetCaption write SetCaption stored False; // Stored in DefineProperties
    property Flat: Boolean read FFlat write SetFlat default False;
    property HotAnimate: Boolean read FHotAnimate write FHotAnimate default False;
    property ImageIndex: integer read GetImageIndex write SetImageIndex default -1;
    property ImageList: TCustomImageList read GetImageList write SetImageList;
    property ImageListChangeLink: TChangeLink read FImageListChangeLink write FImageListChangeLink;
    property Layout: TButtonLayout read FLayout write SetLayout default blGlyphLeft;
    property Margin: integer read FMargin write SetMargin default -1;
    property OLEDraggable: Boolean read FOLEDraggable write FOLEDraggable default False;
    property OnDblClk: TNotifyEvent read FOnDblClk write FOnDblClk;
    property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
    property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
    property PaintState: TButtonPaintState read FPaintState write SetPaintState;
    property Spacing: integer read FSpacing write SetSpacing default 4;
    property State: TButtonStates read FState write FState;
    property ThemeAware: Boolean read FThemeAware write SetThemeAware default True;
    property ThemeToolbar: HTheme read GetThemeToolbar;
    property Timer: THandle read FTimer;
    property TimerStub: Pointer read FTimerStub;
    property Transparent: Boolean read FTransparent write SetTransparent default False;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure AddToUpdateRgn; virtual;
    procedure Click; override;
    procedure DefineProperties(Filer: TFiler); override;
    function Dragging: Boolean;
    function CalcMaxExtentRect(Font: TFont): TRect; virtual;
    function CalcMaxExtentSize(Font: TFont): TSize; virtual;
    procedure LoadFromStream(S: TStream); virtual;
    procedure RebuildButton;
    procedure SaveToStream(S: TStream); virtual;

    property Bottom: integer read GetBottom;
    property Right: integer read GetRight;
  end;

  TWideSpeedButton = class(TCustomWideSpeedButton)
    property Action;
    property AutoSize;
    property Caption;
    property Color;
    property Constraints;
    property Enabled;
    property Flat;
    property Font;
    property ImageIndex;
    property ImageList;
    property HotAnimate;
    property Layout;
    property Margin;
    property ParentBiDiMode;
    property ParentColor;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property Spacing;
    property Tag;
    property ThemeAware;
    property Transparent;
    property Visible;
    property Width;

    property OnClick;
    property OnDblClk;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnMouseLeave;
    property OnMouseEnter;
  end;

  // Specialized Toolbutton that just display static text for a toolbar caption
  {$IFNDEF T2H}
  TCaptionButton = class(TCustomWideSpeedButton)
  protected
    procedure PaintButton(DC: HDC; ForDragImage: Boolean = False); override;
    function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
  public
    constructor Create(AOwner: TComponent); override;
    function CalcMaxExtentRect(Font: TFont): TRect; override;
  end;
  {$ENDIF T2H}

  // Specialized Toolbutton that display Shell Namespace object through a TNamespace object
  TShellToolButton = class(TCustomWideSpeedButton)
  private
    FNamespace: TNamespace;
    FCaptionOptions: TCaptionOptions;
    function GetCaption: WideString; override;
    function GetImageIndex: integer; override;
    procedure SetCaptionOptions(const Value: TCaptionOptions);

  protected
    function DragEnter(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; override;
    function DragOverOLE(grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; override;
    function DragLeave: HResult; override;
    function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; override;

    function SaveToDataObject(const DataObject: IVirtualDataObject): Boolean; override;

    procedure WMContextMenu(var Message: TWMContextMenu); message WM_CONTEXTMENU;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Click; override;
    procedure LoadFromStream(S: TStream); override;
    procedure SaveToStream(S: TStream); override;


    property Action;
    property AutoSize;
    property Caption: WideString read GetCaption;
    property CaptionOptions: TCaptionOptions read FCaptionOptions write SetCaptionOptions default [];
    property Color;
    property Constraints;
    property Enabled;
    property Flat;
    property Font;
    property ImageIndex: integer read GetImageIndex default -1;
    property HotAnimate;
    property Layout;
    property Margin default 2;
    property Namespace: TNamespace read FNamespace write FNamespace;
    property ParentBiDiMode;
    property ParentColor;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property Spacing;
    property Tag;
    property ThemeAware;
    property Transparent;
    property Visible;
    property Width;
    property OnClick;
    property OnDblClk;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnMouseLeave;
    property OnMouseEnter;
  end;

  TCustomVirtualToolbar = class(TToolWindow, IDropTarget, IDropSource)
  private
    FBackBits: TBitmap;                     // Bitmap that caches the background for the toolbar (mainly for transparent mode)
    FButtonList: TVirtualButtonList;        // Array of buttons in order they are displayed
    FCaptionButton: TCaptionButton;         // Button instance that handles the static toolbar caption
    FHotTrackTimer: THandle;                // Timer started when mouse enters control to make sure all hot track are removed on mouse leave
    FLockUpdateCount: integer;              // Locks the toolbar so no button layout calculation or screen update are performed
    FOldFontChangeEvent: TNotifyEvent;      // Stores the old method associated with the OnChange event of the Font (if one was associated)
    FOptions: TVirtualToolbarOptions;        // Options for Toolbar
    FScrollBtnSize: integer;                // Width of the scroll button
    FStates: TVirtualToolbarStates;         // Dynamic state of Toolbar
    FThemeToolbar: HTHEME;                  // Toolbar control theme handle

    FButtonSpacing: integer;
    FButtonMargin: integer;
    FButtonLayout: TButtonLayout;
    FDropTargetHelper: IDropTargetHelper;
    FDropTarget: TCustomWideSpeedButton;
    FDragDropDataObj: IDataObject;
    FDropMarkRect: TRect;
    FContent: TShellToolbarContents;
    FDropInsertMargin: integer;
    {$IFDEF THREADEDICONS}
    FThreadedImagesEnabled: Boolean;
    {$ENDIF}
    {$IFDEF SHELLNOTIFIER}
    FChangeNotifierEnabled: Boolean;
    {$ENDIF}
    FMalloc: IMalloc;
    FOnRecreateButtons: TNotifyEvent;
    FOnCreateButtons: TNotifyEvent;

    function GetAlign: TAlign;
    function GetBkGndParent: TWinControl;
    function GetEdgeBorders: TEdgeBorders;
    function GetEdgeInner: TEdgeStyle;
    function GetEdgeOuter: TEdgeStyle;
    function GetWideCaption: WideString;
    procedure ReadCaption(Reader: TReader);
    procedure SetAlign(const Value: TAlign);
    procedure SetBkGndParent(const Value: TWinControl);
    procedure SetEdgeBorders(const Value: TEdgeBorders);
    procedure SetEdgeOuter(const Value: TEdgeStyle);
    procedure SetEdgeInner(const Value: TEdgeStyle);
    procedure SetOptions(const Value: TVirtualToolbarOptions);
    procedure SetWideCaption(const Value: WideString);
    procedure WriteCaption(Writer: TWriter);
    function GetViewportBounds: TRect;
    procedure SetButtonLayout(const Value: TButtonLayout);
    procedure SetButtonSpacing(const Value: integer);
    procedure SetButtonMargin(const Value: integer);
    {$IFDEF THREADEDICONS}
    procedure SetThreadedImagesEnabled(const Value: Boolean);
    {$ENDIF}
    {$IFDEF SHELLNOTIFIER}
    procedure SetChangeNotiferEnabled(const Value: Boolean);
    {$ENDIF}
  protected
    FVirtualExplorerTree: TCustomVirtualExplorerTree; // Make D6 and CBuilder happy

    procedure ArrangeButtons;
    function CalcMaxButtonSize(Font: TFont): TSize;
    function CanAutoSize(var NewWidth, NewHeight: Integer): Boolean; override;
    function ClosestButtonEdge(ScreenPoint: TPoint): TRect;
    procedure CreateButtons; virtual;
    function CreateButtonList: TVirtualButtonList; virtual;
    procedure CreateParams(var Params: TCreateParams); override;
    procedure CreateWnd; override;
    procedure DefineProperties(Filer: TFiler); override;
    procedure DestroyWnd; override;
    procedure DoCreateButtons; virtual;
    procedure DoRecreateButtons; virtual;
    function DragEnter(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; virtual; stdcall;
    function IDropTarget.DragOver = DragOverOLE; // Naming Clash
    function DragOverOLE(grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; virtual; stdcall;
    function DragLeave: HResult; virtual; stdcall;
    procedure DrawDropMarker(MousePos: PPoint; ForceDraw: Boolean);
    function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; virtual; stdcall;
    procedure FontChangeNotify(Sender: TObject);
    procedure FreeThemes;
    function GiveFeedback(dwEffect: Longint): HResult; virtual; stdcall;
    function IsValidIDListData(DataObject: IDataObject): Boolean;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    procedure PaintToolbar(DC: HDC); virtual;
    procedure PaintWindow(DC: HDC); override;
    function PointToButton(ScreenPt: TPoint): TCustomWideSpeedButton;
    function PointToInsertIndex(ScreenPt: TPoint): integer;
    function QueryContinueDrag(fEscapePressed: BOOL; grfKeyState: Longint): HResult; virtual; stdcall;
    procedure ReCreateButtons;
    procedure ResizeCaptionButton;
    procedure SetName(const Value: TComponentName); override;
    procedure StoreBackGndBitmap;
    procedure UpdateDropStates(ScreenMousePos: TPoint);
    procedure CMColorChanged(var Message: TMessage); message CM_COLORCHANGED;
    procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
    procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
    procedure WMMouseMove(var Message: TWMMouseMove); message WM_MOUSEMOVE;
    procedure WMNCDestroy(var Message: TWMNCDestroy); message WM_NCDESTROY;
    procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
    procedure WMPrint(var Message: TWMPrint); message WM_PRINT;
    procedure WMPrintClient(var Message: TWMPrintClient); message WM_PRINTCLIENT;
    procedure WMRemoveButton(var Message: TMessage); message WM_REMOVEBUTTON;
    procedure WMSize(var Message: TWMSize); message WM_SIZE;
    procedure WMThemeChanged(var Message: TMessage); message WM_THEMECHANGED;
    procedure WMTimer(var Message: TMessage); message WM_TIMER;
    procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;

    property Align: TAlign read GetAlign write SetAlign default alTop;
    property BackBits: TBitmap read FBackBits write FBackBits;
    property BkGndParent: TWinControl read GetBkGndParent write SetBkGndParent;
    property ButtonLayout: TButtonLayout read FButtonLayout write SetButtonLayout default blGlyphLeft;
    property ButtonMargin: integer read FButtonMargin write SetButtonMargin default -1;
    property ButtonSpacing: integer read FButtonSpacing write SetButtonSpacing default 4;
    property Caption: WideString read GetWideCaption write SetWideCaption stored False; // Never let VCL store Widestring, done in DefineProperites
    property CaptionButton: TCaptionButton read FCaptionButton write FCaptionButton;
    {$IFDEF SHELLNOTIFIER}
    property ChangeNotifierEnabled: Boolean read FChangeNotifierEnabled write SetChangeNotiferEnabled;
    {$ENDIF}
    property Content: TShellToolbarContents read FContent write FContent default [stcFolders, stcFiles, stcPrograms];
    property DragDropDataObj: IDataObject read FDragDropDataObj;
    property DropInsertMargin: integer read FDropInsertMargin write FDropInsertMargin default 4;
    property DropMarkRect: TRect read FDropMarkRect;
    property DropTarget: TCustomWideSpeedButton read FDropTarget;
    property DropTargetHelper: IDropTargetHelper read FDropTargetHelper;
    property EdgeBorders: TEdgeBorders read GetEdgeBorders write SetEdgeBorders default [ebTop];
    property EdgeInner: TEdgeStyle read GetEdgeInner write SetEdgeInner default esRaised;
    property EdgeOuter: TEdgeStyle read GetEdgeOuter write SetEdgeOuter default esLowered;
    property HotTrackTimer: THandle read FHotTrackTimer write FHotTrackTimer;
    property Malloc: IMalloc read FMalloc;
    property OnCreateButtons: TNotifyEvent read FOnCreateButtons write FOnCreateButtons;
    property OnRecreateButtons: TNotifyEvent read FOnRecreateButtons write FOnRecreateButtons;
    property Options: TVirtualToolbarOptions read FOptions write SetOptions default [toThemeAware];
    property States: TVirtualToolbarStates read FStates;
    property ThemeToolbar: HTHEME read FThemeToolbar write FThemeToolbar default 0;
    {$IFDEF THREADEDICONS}
    property ThreadedImagesEnabled: Boolean read FThreadedImagesEnabled write SetThreadedImagesEnabled;
    {$ENDIF}
    property ViewportBounds: TRect read GetViewportBounds;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure BeginUpdate;
    procedure EndUpdate;
    procedure LoadFromFile(FileName: WideString); virtual;
    procedure LoadFromStream(S: TStream); virtual;
    procedure Loaded; override;
    procedure RebuildToolbar;
    procedure RecreateToolbar;
    procedure SaveToFile(FileName: WideString); virtual;
    procedure SaveToStream(S: TStream); virtual;

    property ButtonList: TVirtualButtonList read FButtonList;
  end;

  TVirtualToolbar = class(TCustomVirtualToolbar)
  published
    property Align;
    property Anchors;
    property AutoSize;
    property BiDiMode;
    property BkGndParent;
    property ButtonLayout;
    property ButtonMargin;
    property ButtonSpacing;
    property Caption;
    property Color;
    property Constraints;
    property Content;
    property DropInsertMargin;
    property EdgeBorders;
    property EdgeInner;
    property EdgeOuter;
    property Font;
    property Options;

    property OnClick;
    property OnCreateButtons;
    property OnRecreateButtons;
  end;


  TVSTOnAddButtonEvent = procedure(Sender: TCustomVirtualToolbar; Namespace: TNamespace; var Allow: Boolean) of object;

  TCustomVirtualShellToolbar = class(TCustomVirtualToolbar)
  private
    FButtonCaptionOptions: TCaptionOptions;
    {$IFDEF EXPLORERCOMBOBOX}
    FVirtualExplorerComboBox: TCustomVirtualExplorerComboBox;
    {$ENDIF}
    FOnAddButton: TVSTOnAddButtonEvent;

    procedure SetButtonCaptionOptions(const Value: TCaptionOptions);
    procedure SetVirtualExplorerTree(const Value: TCustomVirtualExplorerTree);
    {$IFDEF EXPLORERCOMBOBOX}
    procedure SetVirtualExplorerComboBox(const Value: TCustomVirtualExplorerComboBox);
    {$ENDIF}
  protected
    procedure ChangeLinkDispatch(PIDL: PItemIDList); virtual;
    function CreateButtonList: TVirtualButtonList; override;
    procedure DoAddButton(Namespace: TNamespace; var Allow: Boolean);
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    {$IFDEF SHELLNOTIFIER}
    procedure WMShellNotify(var Msg: TMessage); message WM_SHELLNOTIFY;
    {$ENDIF}
    {$IFDEF THREADEDICONS}
    procedure WMVTSetIconIndex(var Msg: TWMVTSetIconIndex); message WM_VTSETICONINDEX;
    {$ENDIF}
    property ButtonCaptionOptions: TCaptionOptions read FButtonCaptionOptions write SetButtonCaptionOptions default [];
    property OnAddButton: TVSTOnAddButtonEvent read FOnAddButton write FOnAddButton;
    {$IFDEF EXPLORERCOMBOBOX}
    property VirtualExplorerComboBox: TCustomVirtualExplorerComboBox read FVirtualExplorerComboBox
      write SetVirtualExplorerComboBox;
    {$ENDIF}
    property VirtualExplorerTree: TCustomVirtualExplorerTree read FVirtualExplorerTree
      write SetVirtualExplorerTree;

  public
    procedure ChangeLinkFreeing(ChangeLink: IVETChangeLink); dynamic;
  end;

  TVirtualShellToolbar = class(TCustomVirtualShellToolbar)
  published
    property Align;
    property AutoSize;
    property BiDiMode;
    property BkGndParent;
    property ButtonCaptionOptions;
    property ButtonLayout;
    property ButtonMargin;
    property ButtonSpacing;
    property Caption;
    property Color;
    property Constraints;
    property EdgeBorders;
    property EdgeInner;
    property EdgeOuter;
    property Font;
    property Options;
    {$IFDEF EXPLORERCOMBOBOX}
    property VirtualExplorerComboBox;
    {$ENDIF}
    property VirtualExplorerTree;
    property Visible;

    property OnClick;
    property OnCreateButtons;
    property OnRecreateButtons;
  end;

  TCustomVirtualDriveToolbar = class(TCustomVirtualShellToolbar)
  private
    FDriveSpecialFolders: TDriveSpecialFolders;

    function GetOptions: TVirtualToolbarOptions;
    procedure SetOptions(const Value: TVirtualToolbarOptions);
    procedure SetSpecialDriveFolders(const Value: TDriveSpecialFolders);
    procedure WMRemoveButton(var Message: TMessage); message WM_REMOVEBUTTON;
  protected
    procedure CreateButtons; override;

    property Options: TVirtualToolbarOptions read GetOptions write SetOptions default [toThemeAware];
    property SpecialDriveFolders: TDriveSpecialFolders read FDriveSpecialFolders
      write SetSpecialDriveFolders default [dsfRemovable, dsfReadOnly, dsfFixedDrive];
  public
    constructor Create(AOwner: TComponent); override;
  end;

  TVirtualDriveToolbar = class(TCustomVirtualDriveToolbar)
  published
    property Align;
    property AutoSize;
    property BiDiMode;
    property BkGndParent;
    property ButtonCaptionOptions;
    property ButtonLayout;
    property ButtonMargin;
    property ButtonSpacing;
    property Caption;
    property Color;
    property Constraints;
    property DropInsertMargin;
    property EdgeBorders;
    property EdgeInner;
    property EdgeOuter;
    property Font;
    property OnAddButton;
    property Options;
    property SpecialDriveFolders;
    {$IFDEF EXPLORERCOMBOBOX}
    property VirtualExplorerComboBox;
    {$ENDIF}
    property VirtualExplorerTree;
    property Visible;

    property OnClick;
    property OnCreateButtons;
    property OnRecreateButtons;
  end;

  TCustomVirtualSpecialFolderToolbar = class(TCustomVirtualDriveToolbar)
  private
    FSpecialFolders: TSpecialFolders;
    FSpecialCommonFolders: TSpecialCommonFolders;
    procedure SetSpecialFolders(const Value: TSpecialFolders);
    procedure SetSpecialCommonFolders(const Value: TSpecialCommonFolders);
  protected

    procedure CreateButtons; override;

    property SpecialFolders: TSpecialFolders read FSpecialFolders write SetSpecialFolders;
    property SpecialCommonFolders: TSpecialCommonFolders read FSpecialCommonFolders
      write SetSpecialCommonFolders;
  end;

  TVirtualSpecialFolderToolbar = class(TCustomVirtualSpecialFolderToolbar)
  published
    property Align;
    property AutoSize;
    property BiDiMode;
    property BkGndParent;
    property ButtonCaptionOptions;
    property ButtonLayout;
    property ButtonMargin;
    property ButtonSpacing;
    property Caption;
    property Color;
    property Constraints;
    property DropInsertMargin;
    property EdgeBorders;
    property EdgeInner;
    property EdgeOuter;
    property Font;
    property OnAddButton;
    property Options;
    property SpecialFolders;
    property SpecialCommonFolders;
    {$IFDEF EXPLORERCOMBOBOX}
    property VirtualExplorerComboBox;
    {$ENDIF}
    property VirtualExplorerTree;
    property Visible;

    property OnClick;
    property OnCreateButtons;
    property OnRecreateButtons;
  end;

type
  TVSTShellToolbar = class(TClipboardFormat)
  private
    FProcess: Cardinal;
    FPIDL: PItemIDList;
    function GetPIDLSize: integer;
  protected
    function GetFormatEtc: TFormatEtc; override;
  public
    function LoadFromDataObject(DataObject: IDataObject): Boolean; override;
    function SaveToDataObject(DataObject: IDataObject): Boolean; override;

    property Process: Cardinal read FProcess;
    property PIDLSize: integer read GetPIDLSize;
    property PIDL: PItemIDList read FPIDL write FPIDL;
  end;

var
  CF_VSTSHELLTOOLBAR: TClipFormat;

implementation



function RectWidth(ARect: TRect): integer;
begin
  Result := ARect.Right - ARect.Left
end;

function RectHeight(ARect: TRect): integer;
begin
  Result := ARect.Bottom - ARect.Top
end;

{ TCustomWideSpeedButton }

procedure TCustomWideSpeedButton.ActionChange(Sender: TObject; CheckDefaults: Boolean);
begin
  inherited;
  with TCustomAction(Sender) do
  begin
    if not CheckDefaults or (Self.Caption = '') or (Self.Caption = Self.Name) then
      Self.Caption := Caption;
    Self.ImageList := ActionList.Images;
    Self.ImageIndex := ImageIndex
  end;
  RebuildButton
end;

procedure TCustomWideSpeedButton.AddToUpdateRgn;

// Adds the button to the update region of the owner toolbar, taking into account
// any scrolling that the toolbar may be using it maps the physical coordinates into
// logical coordinates

var
  R: TRect;
begin
  R := BoundsRect;
  if Assigned(Parent) and Parent.HandleAllocated then
    InvalidateRect(Parent.Handle, @R, False);
end;

procedure TCustomWideSpeedButton.CalcButtonLayout(DC: HDC; const Client: TRect;
  const Offset: TPoint; const Caption: WideString; Layout: TButtonLayout;
  Margin, Spacing: Integer; var GlyphPos: TPoint; var TextBounds: TRect;
  BiDiFlags: Integer);
var
  TextPos: TPoint;
  ClientSize, GlyphSize, TextSize: TPoint;
  TotalSize: TPoint;
  CaptionANSI: string;
begin
  if (BiDiFlags and DT_RIGHT) = DT_RIGHT then
  begin
    if Layout = blGlyphLeft then
      Layout := blGlyphRight
    else
    if Layout = blGlyphRight then
      Layout := blGlyphLeft;
  end;

  // calculate the item sizes
  ClientSize := Point(Client.Right - Client.Left, Client.Bottom -
    Client.Top);

  if Assigned(ImageList) and (ImageIndex > -1) then
    with ImageList do
      GlyphSize := Point(Width, Height)
  else
    GlyphSize := Point(0, 0);

  if Length(Caption) > 0 then
  begin
    TextBounds := Rect(0, 0, Client.Right - Client.Left, 0);
    if IsUnicode then
      DrawTextW_VST(DC, PWideChar(Caption), Length(Caption), TextBounds,
        DT_CALCRECT or BiDiFlags)
    else begin
      CaptionANSI := Caption;
      DrawText(DC, PChar(CaptionANSI), Length(CaptionANSI), TextBounds,
        DT_CALCRECT or BiDiFlags)
    end;
    TextSize := Point(TextBounds.Right - TextBounds.Left, TextBounds.Bottom -
      TextBounds.Top);
  end
  else
  begin
    TextBounds := Rect(0, 0, 0, 0);
    TextSize := Point(0,0);
  end;

  // If the layout has the glyph on the right or the left, then both the
  // text and the glyph are centered vertically.  If the glyph is on the top
  // or the bottom, then both the text and the glyph are centered horizontally.
  if Layout in [blGlyphLeft, blGlyphRight] then
  begin
    GlyphPos.Y := (ClientSize.Y - GlyphSize.Y + 1) div 2;
    TextPos.Y := (ClientSize.Y - TextSize.Y + 1) div 2;
  end
  else
  begin
    GlyphPos.X := (ClientSize.X - GlyphSize.X + 1) div 2;
    TextPos.X := (ClientSize.X - TextSize.X + 1) div 2;
  end;

  // if there is no text or no bitmap, then Spacing is irrelevant
  if (TextSize.X = 0) or (GlyphSize.X = 0) then
    Spacing := 0;

  // adjust Margin and Spacing
  if Margin = -1 then
  begin
    if Spacing = -1 then
    begin
      TotalSize := Point(GlyphSize.X + TextSize.X, GlyphSize.Y + TextSize.Y);
      if Layout in [blGlyphLeft, blGlyphRight] then
        Margin := (ClientSize.X - TotalSize.X) div 3
      else
        Margin := (ClientSize.Y - TotalSize.Y) div 3;
      Spacing := Margin;
    end
    else
    begin
      TotalSize := Point(GlyphSize.X + Spacing + TextSize.X, GlyphSize.Y +
        Spacing + TextSize.Y);
      if Layout in [blGlyphLeft, blGlyphRight] then
        Margin := (ClientSize.X - TotalSize.X + 1) div 2
      else
        Margin := (ClientSize.Y - TotalSize.Y + 1) div 2;
    end;
  end
  else
  begin
    if Spacing = -1 then
    begin
      TotalSize := Point(ClientSize.X - (Margin + GlyphSize.X), ClientSize.Y -
        (Margin + GlyphSize.Y));
      if Layout in [blGlyphLeft, blGlyphRight] then
        Spacing := (TotalSize.X - TextSize.X) div 2
      else
        Spacing := (TotalSize.Y - TextSize.Y) div 2;
    end;
  end;

  case Layout of
    blGlyphLeft:
      begin
        GlyphPos.X := Margin;
        TextPos.X := GlyphPos.X + GlyphSize.X + Spacing;
      end;
    blGlyphRight:
      begin
        GlyphPos.X := ClientSize.X - Margin - GlyphSize.X;
        TextPos.X := GlyphPos.X - Spacing - TextSize.X;
      end;
    blGlyphTop:
      begin
        GlyphPos.Y := Margin;
        TextPos.Y := GlyphPos.Y + GlyphSize.Y + Spacing;
      end;
    blGlyphBottom:
      begin
        GlyphPos.Y := ClientSize.Y - Margin - GlyphSize.Y;
        TextPos.Y := GlyphPos.Y - Spacing - TextSize.Y;
      end;
  end;

  // fixup the result variables
  with GlyphPos do
  begin
    Inc(X, Client.Left + Offset.X);
    Inc(Y, Client.Top + Offset.Y);
  end;
  OffsetRect(TextBounds, TextPos.X + Client.Left + Offset.X,
    TextPos.Y + Client.Top + Offset.X);
end;

function TCustomWideSpeedButton.CalcMaxExtentRect(Font: TFont): TRect;

// Calculates the size of the rectangle that is necessary to completely display
// the Caption, Glyph, user defined Margin, user defined Spacing, and any button
// frame or border. Also takes into acount if the image is above/below the caption
// or right/left of caption

const
  BorderMargin = 6;
var
  Size: TSize;
  Delta: integer;
begin
  Delta := 0;
  if Margin > -1 then
    Inc(Delta, Margin);
  if Spacing > -1 then
    Inc(Delta, Spacing);

  Size := TextExtentW(PWideChar(Caption), Font);

  if Assigned(ImageList) then
  begin
    if Layout in [blGlyphLeft, blGlyphRight] then
      Size.cx := Size.cx + ImageList.Width
    else
      Size.cy := Size.cy + ImageList.Height
  end;

  if Layout in [blGlyphLeft, blGlyphRight] then
    SetRect(Result, 0, 0, Size.cx + Delta + BorderMargin, Size.cy + BorderMargin)
  else
    SetRect(Result, 0, 0, Size.cx + BorderMargin, Size.cy + + BorderMargin + Delta);
end;

function TCustomWideSpeedButton.CalcMaxExtentSize(Font: TFont): TSize;
var
  R: TRect;
begin
  R := CalcMaxExtentRect(Font);
  Result.cx := RectWidth(R);
  Result.cy := RectHeight(R);
end;

function TCustomWideSpeedButton.CanAutoSize(var NewWidth, NewHeight: Integer): Boolean;
var
  Size: TSize;
begin
  Result := inherited CanAutoSize(NewWidth, NewHeight);
  Size := CalcMaxExtentSize(Font);
  if NewWidth <> Size.cx then
    NewWidth := Size.cx;
  if NewHeight <> Size.cy then
    NewHeight := Size.cy
end;

procedure TCustomWideSpeedButton.Click;
// Fires the OnClick event in the parent toolbar
begin
  if not Dragging then
    inherited
end;

procedure TCustomWideSpeedButton.CMMouseEnter(var Message: TMessage);
begin
  if (csLButtonDown in ControlState) then
    PaintState := psPressed;
  Include(FState, bsMouseInButton);
  if Transparent and HotAnimate then
    Invalidate;
  DoMouseEnter
end;

procedure TCustomWideSpeedButton.CMMouseLeave(var Message: TMessage);
begin
  inherited;
  PaintState := psNormal;
  Exclude(FState, bsMouseInButton);
  if Transparent and (Flat or HotAnimate) then
    Invalidate;
  DoMouseLeave
end;

constructor TCustomWideSpeedButton.Create(AOwner: TComponent);
begin
  inherited;
  FMargin := -1;
  FSpacing := 4;
  Width := 23;
  Height := 22;
  Enabled := True;
  ImageIndex := -1;
  Visible := True;
  ThemeAware := True;
  ControlStyle := ControlStyle - [csDoubleClicks]; // Map DblClicks to a click
  FOldOnFontChange := Font.OnChange;
  Font.OnChange := FontChange;
  Constraints.MinHeight := 22;
  Constraints.MinWidth := 23;
  FImageListChangeLink := TChangeLink.Create;
  FImageListChangeLink.OnChange := ImageListChange;
  FTimerStub := CreateStub(Self, @TCustomWideSpeedButton.TimerStubProc);
end;

procedure TCustomWideSpeedButton.DefineProperties(Filer: TFiler);

// Defines the WideString as a custom property with the custom name WideText.
// Acorrding to Mike Lischke the VCL streaming screws up streaming of WideStrings

begin
  inherited;
  Filer.DefineProperty('WideText', ReadCaption, WriteCaption, Caption <> '');
end;

destructor TCustomWideSpeedButton.Destroy;
begin
  Font.OnChange := FOldOnFontChange;
  FreeThemes;
  if TimerStub <> nil then
    DisposeStub(TimerStub);
  ImageListChangeLink.Free;
  inherited
end;

procedure TCustomWideSpeedButton.DoDblClk;
begin
  if Assigned(OnDblClk) then
    OnDblClk(Self)
end;

procedure TCustomWideSpeedButton.DoMouseEnter;
begin
  if Assigned(OnMouseEnter) then
    OnMouseEnter(Self)
end;

procedure TCustomWideSpeedButton.DoMouseLeave;
begin
  if Assigned(OnMouseLeave) then
    OnMouseLeave(Self)
end;

function TCustomWideSpeedButton.DragEnter(const dataObj: IDataObject;
  grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
begin
  PaintState := psDropTarget;
  Result := S_OK;
end;

function TCustomWideSpeedButton.Dragging: Boolean;
begin
  Result := FDragging;
end;

function TCustomWideSpeedButton.DragLeave: HResult;
begin
  PaintState := psNormal;
  Result := S_OK;
end;

function TCustomWideSpeedButton.DragOverOLE(grfKeyState: Integer;
  pt: TPoint; var dwEffect: Integer): HResult;
begin
  Result := S_OK;
end;

procedure TCustomWideSpeedButton.DrawButtonText(DC: HDC; const Caption: WideString;
  TextBounds: TRect; Enabled: Boolean; BiDiFlags: Integer);
var
  CaptionANSI: string;
  OldColor, Flags, OldMode: Longword;
begin
  Flags := 0;
  OldMode := SetBkMode(DC, Windows.TRANSPARENT);
  if IsUnicode then
  begin
    Flags := {DT_CENTER or }DT_VCENTER or BiDiFlags;
    if not Enabled then
    begin
      OffsetRect(TextBounds, 1, 1);
      OldColor := SetTextColor(DC, ColorToRGB(clBtnHighlight));
      DrawTextW_VST(DC, PWideChar(Caption), Length(Caption), TextBounds, Flags);
      OffsetRect(TextBounds, -1, -1);
      SetTextColor(DC, ColorToRGB(clBtnShadow));
      DrawTextW_VST(DC, PWideChar(Caption), Length(Caption), TextBounds, Flags);
      SetTextColor(DC, ColorToRGB(OldColor));
    end else
      DrawTextW_VST(DC, PWideChar(Caption), Length(Caption), TextBounds, Flags);
  end else
  begin
    CaptionANSI := Caption;
    if not Enabled then
    begin
      OffsetRect(TextBounds, 1, 1);
      OldColor := SetTextColor(DC, ColorToRGB(clBtnHighlight));
      DrawText(DC, PChar(CaptionANSI), Length(Caption), TextBounds, Flags);
      OffsetRect(TextBounds, -1, -1);
      SetTextColor(DC, ColorToRGB(clBtnShadow));
      DrawText(DC, PChar(CaptionANSI), Length(Caption), TextBounds, Flags);
      SetTextColor(DC, ColorToRGB(OldColor));
    end else
      DrawText(DC, PChar(CaptionANSI), Length(Caption), TextBounds, Flags);
  end;
  SetBkMode(DC, OldMode);
end;


function TCustomWideSpeedButton.Drop(const dataObj: IDataObject;
  grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
begin
  PaintState := psNormal;
  Invalidate;
  Update;
  Result := S_OK;
end;

procedure TCustomWideSpeedButton.FontChange(Sender: TObject);
begin
  if Assigned(FOldOnFontChange) then
    FOldOnFontChange(Sender);
  RebuildButton
end;

procedure TCustomWideSpeedButton.FreeThemes;
begin
  if FThemeToolbar <> 0 then
  begin
    CloseThemeData(FThemeToolbar);
    FThemeToolbar := 0;
    Exclude(FState, bsThemesActive)
  end;
end;

function TCustomWideSpeedButton.GetBottom: integer;
begin
  Result := Top + Height
end;

function TCustomWideSpeedButton.GetCaption: WideString;
begin
  Result := FCaption;
end;

function TCustomWideSpeedButton.GetImageIndex: integer;
begin
  Result := FImageIndex
end;

function TCustomWideSpeedButton.GetImageList: TCustomImageList;
begin
  Result := FImageList;
end;

function TCustomWideSpeedButton.GetRight: integer;
begin
  Result := Left + Width
end;

function TCustomWideSpeedButton.GetThemeToolbar: HTheme;
begin
  if (FThemeToolbar = 0) and Assigned(Parent) and Parent.HandleAllocated then
    FThemeToolbar := OpenThemeData(Parent.Handle, 'toolbar');
  Result := FThemeToolbar
end;

function TCustomWideSpeedButton.GiveFeedback(dwEffect: Integer): HResult;
begin
  Result := DRAGDROP_S_USEDEFAULTCURSORS
end;

procedure TCustomWideSpeedButton.ImageListChange(Sender: TObject);
begin
  //The Image list is changed (something has been added, deleted or moved).
  RebuildButton
end;

function TCustomWideSpeedButton.LoadFromDataObject(const DataObject: IVirtualDataObject): Boolean;
begin
  Result := False;
end;

procedure TCustomWideSpeedButton.LoadFromStream(S: TStream);
begin
  // Load here
end;

procedure TCustomWideSpeedButton.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FImageList) then
  begin
    FImageList := nil;
    RebuildButton
  end;
end;

procedure TCustomWideSpeedButton.Paint;
begin
  Canvas.Font.Assign(Font);
  PaintButton(Canvas.Handle);
end;

procedure TCustomWideSpeedButton.PaintButton(DC: HDC; ForDragImage: Boolean = False);

  function GetCColor(Color1, Color2: TColor; total, weight2 :Integer): TColor;
  var //weight1: 0->Color1, total->Color2
    C1, C2: TColor;
    R1, G1, B1, R2, G2, B2: Byte;
    Nr, Ng, Nb: Integer;
  begin
    C1:=ColorToRGB(Color1);
    C2:=ColorToRGB(Color2);
    R1:=PByte(@C1)^;
    G1:=PByte(Integer(@C1)+1)^;
    B1:=PByte(Integer(@C1)+2)^;
    R2:=PByte(@C2)^;
    G2:=PByte(Integer(@C2)+1)^;
    B2:=PByte(Integer(@C2)+2)^;
    if total<>0 then
      begin
        Nr:=(R1+(R2-R1)*weight2 div total);
        Ng:=(G1+(G2-G1)*weight2 div total);
        Nb:=(B1+(B2-B1)*weight2 div total);
        if (Nr<0)   then Nr:=0;
        if (Nr>255) then Nr:=255;
        if (Ng<0)   then Ng:=0;
        if (Ng>255) then Ng:=255;
        if (Nb<0)   then Nb:=0;
        if (Nb>255) then Nb:=255;
        Result:=RGB(Nr, Ng, Nb);
      end
    else
      Result:=Color1;
  end;

// This Paint method makes the assumption that the HDC has shifted its viewport such
// to compenstate for any scrolling in the parent window.  The button will paint itself
// to the canvas at the Bounds rect of the button which may be positioned far off the
// screen if the viewport of the DC is not shifted

var
  R, TempR: TRect;
  PartType, PartState: Longword;
  Brush: TBrush;

  Offset, GlyphPos: TPoint;
  TextRect, GlyphRect: TRect;
  BiDiFlags, dwTextFlags2, rgbFg: Longword;
// OldOrg: TPoint;
begin
  if Visible then
  begin

  // set up some structures that are needed regardless if Theme drawing or not
    R := ClientRect;

    // Used in the icon and text positioning calculations
    if PaintState = psPressed then
    begin
      Offset.x := 1;
      Offset.y := 1;
    end else
    if (PaintState = psHot) and HotAnimate then
    begin
      Offset.x := -1;
      Offset.y := -1;
    end else
    begin
      Offset.x := 0;
      Offset.y := 0;
    end;

    // See if we are in Right to Left mode
    BiDiFlags := DrawTextBiDiModeFlags(0);

    // Themes for toolbars does not define the background so paint it for either
    if not Transparent or ForDragImage then
    begin
      Brush := TBrush.Create;
      if Enabled then
        Brush.Color := Color
      else
        Brush.Color := GetCColor(clBtnFace, Color, 100, 10);
      FillRect(DC, R, Brush.Handle);
      Brush.Free
    end;

    // Draw in Themes?
    if ThemeAware and (bsThemesActive in State) then
    begin

      PartType := TP_BUTTON;

      case PaintState of
        psHot:      PartState := TS_HOT;
        psPressed:  PartState := TS_PRESSED;
      else
        PartState := TS_NORMAL
      end;

      if ForDragImage then
        PartState := TS_HOT;

      if not Enabled then
        PartState := TS_DISABLED;

      DrawThemeBackground(ThemeToolbar, DC, PartType, PartState, R, nil);
      GetThemeBackgroundContentRect(ThemeToolbar, DC, PartType, PartState, R, @R);

      CalcButtonLayout(DC, R, Offset, Caption, Layout, Margin, Spacing, GlyphPos, TextRect, BiDiFlags);
      if Assigned(ImageList) and (ImageIndex > -1) then
      begin
        // Create an image rectangle from the position and the imagelist size
        SetRect(GlyphRect, GlyphPos.x, GlyphPos.y, GlyphPos.x + ImageList.Width, GlyphPos.y + ImageList.Height);

        // For some reason DrawThemeIcon won't work in the IDE
        if csDesigning in ComponentState then
          ImageList_DrawEx(ImageList.Handle, ImageIndex, DC, GlyphPos.X, GlyphPos.Y, 0, 0, CLR_NONE, CLR_NONE, ILD_TRANSPARENT)
        else
          DrawThemeIcon(ThemeToolbar, DC, PartType, PartState, GlyphRect, ImageList.Handle, ImageIndex);
      end;

      if not Enabled then
        dwTextFlags2 := DTT_GRAYED
      else
        dwTextFlags2 := 0;

      DrawThemeText(ThemeToolbar, DC, PartType, PartState, PWideChar(Caption),
        Length(Caption), BiDiFlags, dwTextFlags2, TextRect);
    end else // No Themes
    begin

      // if Flat and the the button is hot so 3D frame it to "raise" it up
      if ((PaintState = psHot) or (csDesigning in ComponentState)) and Flat then
        DrawEdge(DC, R, BDR_RAISEDINNER, BF_RECT);

      // if not Flat always draw the edge the hardway so the buttons can be truly
      // transparent
      if not Flat and (PaintState <> psPressed) or ForDragImage then
      begin
        TempR := R;
        InflateRect(TempR, 1, 1);
        OffsetRect(TempR, -2, -2);
        FrameRect(DC, TempR, GetStockObject(GRAY_BRUSH));
        OffsetRect(TempR, 3, 3);
        FrameRect(DC, TempR, GetStockObject(WHITE_BRUSH));
        OffsetRect(TempR, -2, -2);
        FrameRect(DC, TempR, GetStockObject(BLACK_BRUSH));
      end;

      // If pressed draw the button pushed "into" the toolbar
      if PaintState = psPressed then
        DrawEdge(DC, R, EDGE_SUNKEN, BF_RECT);

      // Use the extra variable
      dwTextFlags2 := ILD_TRANSPARENT;
      rgbFg := CLR_NONE;

      // If it is not enabled blend it with to backgound to make it weaker looking
      if not Enabled then
      begin
        rgbFg := ColorToRGB(Color);
        dwTextFlags2 :=  dwTextFlags2 or ILD_BLEND50;
      end;

      //  We know that the Draw Edge may have used 2 pixels account for them in the rect
      InflateRect(R, -2, -2);

      // Calcuate the rectangle for the Text and the postion of the Glyph
      CalcButtonLayout(DC, R, Offset, Caption, Layout, Margin, Spacing, GlyphPos, TextRect, BiDiFlags);

      // Draw the Glyph and the Text
      if Assigned(ImageList) and (ImageIndex > -1) then
        ImageList_DrawEx(ImageList.Handle, ImageIndex, DC, GlyphPos.X, GlyphPos.Y, 0, 0, CLR_NONE, rgbFg, dwTextFlags2);
      DrawButtonText(DC, Caption, TextRect, Enabled, BiDiFlags);
    end
  end;

  if PaintState = psDropTarget then
    DrawFocusRect(DC, ClientRect)
end;

function TCustomWideSpeedButton.QueryContinueDrag(fEscapePressed: BOOL;
  grfKeyState: Integer): HResult;
begin
  Result := S_OK;

  if fEscapePressed then
    Result := DRAGDROP_S_CANCEL
  else
{  if MouseManager.IsButtonDown(mbLeft) then
  begin
    if grfKeyState and MK_LBUTTON > 0 then  // is the LButton flag set?
      Result := S_OK                        // Button is still down
    else
      Result := DRAGDROP_S_DROP;            // Button has been released
  end;  else  if MouseButtonMgr.MouseDown(mbLeft)      }
  if bsRMouseButtonDown in State then
  begin
    if grfKeyState and MK_RBUTTON > 0 then  // is the RButton flag set?
      Result := S_OK                        // Button is still down
    else
      Result := DRAGDROP_S_DROP;            // Button has been released
  end
 {
  begin
    if grfKeyState and MK_MBUTTON > 0 then // is the MButton flag set?
      Result := S_OK                       // Button is still down
    else
      Result := DRAGDROP_S_DROP            // Button has been released
  end// else  if MouseButtonMgr.MouseDown(mbMiddle)
  //  Result := S_OK;     }
end;

procedure TCustomWideSpeedButton.ReadCaption(Reader: TReader);

// The Read procedure for the DefineProperty "WideText"

begin
  case Reader.NextValue of
    vaLString, vaString:
      Caption := Reader.ReadString;
  else
    Caption := Reader.ReadWideString;
  end;
end;

procedure TCustomWideSpeedButton.RebuildButton;
begin
  if AutoSize then
    AdjustSize;
  AddToUpdateRgn;
  Perform(WM_THEMECHANGED, 0, 0);
end;

function TCustomWideSpeedButton.SaveToDataObject(const DataObject: IVirtualDataObject): Boolean;
begin
  Result := False;
end;

procedure TCustomWideSpeedButton.SaveToStream(S: TStream);
begin
  // Save stuff here
end;

procedure TCustomWideSpeedButton.SetBoundsR(Rect: TRect);
begin
  SetBounds(Rect.Left, Rect.Top, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top)
end;

procedure TCustomWideSpeedButton.SetCaption(const Value: WideString);
begin
  if FCaption <> Value then
  begin
    FCaption := Value;
    RebuildButton
  end
end;

procedure TCustomWideSpeedButton.SetFlat(const Value: Boolean);
begin
  if FFlat <> Value then
  begin
    FFlat := Value;
    RebuildButton
  end
end;

procedure TCustomWideSpeedButton.SetImageIndex(const Value: integer);
begin
  if Value <> FImageIndex then
  begin
    FImageIndex := Value;
    RebuildButton
  end
end;

procedure TCustomWideSpeedButton.SetImageList(const Value: TCustomImageList);
begin
  if FImageList <> Value then
  begin
    if FImageList <> nil then
      FImageList.UnRegisterChanges(FImageListChangeLink);
    FImageList := Value;
    if ImageList <> nil then
    begin
      ImageList.RegisterChanges(FImageListChangeLink);
      ImageList.FreeNotification(Self); // Incase the image list is in a different form
    end;
    ImageListChange(Self);
  end
end;

procedure TCustomWideSpeedButton.SetLayout(const Value: TButtonLayout);
begin
  if FLayout <> Value then
  begin
    FLayout := Value;
    RebuildButton
  end
end;

procedure TCustomWideSpeedButton.SetMargin(const Value: integer);
begin
  if FMargin <> Value then
  begin
    FMargin := Value;
    RebuildButton
  end
end;

procedure TCustomWideSpeedButton.SetPaintState(const Value: TButtonPaintState);
begin
  FPaintState := Value;
  if HotAnimate or Flat or ThemeAware then  // Stop flicker if nothing to repaint
    AddToUpdateRgn
end;

procedure TCustomWideSpeedButton.SetParent(AParent: TWinControl);
begin
  inherited;
  Perform(WM_THEMECHANGED, 0, 0);
end;

procedure TCustomWideSpeedButton.SetSpacing(const Value: integer);
begin
  if FSpacing <> Value then
  begin
   FSpacing := Value;
    RebuildButton
  end
end;

procedure TCustomWideSpeedButton.SetThemeAware(const Value: Boolean);
begin
  if FThemeAware <> Value then
  begin
    FThemeAware := Value;
    RebuildButton
  end
end;

procedure TCustomWideSpeedButton.StartHotTimer;
// Start timer to monitor the Hot state and clear it if we miss a CM_MOUSELEAVE
// It is necessary to use the Stub and the TimerProc so we get timers during OLE
// drag and drop
begin
  // Set a timer to remove the Hot track if we miss the CM_MOUSELEAVE
  if (FTimer = 0) and (Flat or (bsThemesActive in State)) then
   // The timerID uses object address for a unique value
    FTimer := SetTimer(Parent.Handle, integer(Self), HOTTRACKDELAY, TimerStub);
end;

procedure TCustomWideSpeedButton.TimerStubProc(Wnd: HWnd; uMsg, idEvent: UINT;
  dwTime: DWORD);
var
  Temp: integer;
begin
  if not PtInRect(ClientRect, ScreenToClient(Mouse.CursorPos)) and (FTimer <> 0)  then
  begin
    Temp := FTimer;
    FTimer := 0;
    KillTimer(Parent.Handle, Temp);
    if bsMouseInButton in State then
      Perform(CM_MOUSELEAVE, 0, 0);
  end
end;

procedure TCustomWideSpeedButton.SetTransparent(const Value: Boolean);
begin
  if FTransparent <> Value then
  begin
    FTransparent := Value;

    // Don't let the parent clip the DC before drawing the window below the button
    if Value then
      ControlStyle := ControlStyle - [csOpaque]
    else
      ControlStyle := ControlStyle + [csOpaque];

    RebuildButton;
    Visible := False;
    if Assigned(Parent) and Parent.HandleAllocated then
      Parent.Invalidate;
    Visible := True;
  end
end;

procedure TCustomWideSpeedButton.WMLButtonDblClk(var Message: TWMLButtonDblClk);
begin
  inherited;
  if not Dragging then
    DoDblClk;
end;

procedure TCustomWideSpeedButton.WMRButtonDown(var Message: TWMRButtonDown);
var
  DataObject: IVirtualDataObject;
  dwOkEffects, dwEffect: integer;
  DragImage: TBitmap;
  LogicalPerformedDropEffect: TLogicalPerformedDropEffect;
begin
  Include(FState, bsRMouseButtonDown);
  inherited;
  if OLEDraggable then
  begin
    FDragging := DragDetectPlus(Parent.Handle, ClientToScreen(SmallPointToPoint(Message.Pos)));
    if Dragging then
      try
        PaintState := psNormal;
        Parent.Invalidate;
        Parent.Update;

        DataObject := TVirtualDataObject.Create as IVirtualDataObject;

        DragImage := TBitmap.Create;
        DragImage.Width := Width;
        DragImage.Height := Height;
        PaintButton(DragImage.Canvas.Handle, True);
        DataObject.AssignDragImage(DragImage, Point(Message.XPos, Message.YPos), Color);
        FreeAndNil(DragImage);

        dwOkEffects := DROPEFFECT_COPY or DROPEFFECT_MOVE or DROPEFFECT_LINK;

        if SaveToDataObject(DataObject) then
          DoDragDrop(DataObject, Self, dwOkEffects, dwEffect);


        LogicalPerformedDropEffect := TLogicalPerformedDropEffect.Create;
        if LogicalPerformedDropEffect.LoadFromDataObject(DataObject) then
        begin
          if LogicalPerformedDropEffect.Action = effectMove then
            PostMessage(Parent.Handle, WM_REMOVEBUTTON, integer(Self), 0);
        end else
          PostMessage(Parent.Handle, WM_REMOVEBUTTON, integer(Self), 0);
    finally
      FDragging := False;
    end else
      // Reaquire the capture that DragDetectPlus released
      Mouse.Capture := Parent.Handle;
  end
end;

procedure TCustomWideSpeedButton.WMLButtonDown(var Message: TWMLButtonDown);
begin
  inherited;
  PaintState := psPressed;
  Invalidate;
  Update;
end;

procedure TCustomWideSpeedButton.WMRButtonUp(var Message: TWMRButtonUp);
begin
  inherited;
  Exclude(FState, bsRMouseButtonDown);
end;

procedure TCustomWideSpeedButton.WMLButtonUp(var Message: TWMLButtonUp);
begin
  if not Dragging then
    inherited
  else
    Mouse.Capture := 0;
  if HotAnimate and (bsMouseInButton in State) then
    PaintState := psHot
  else
    PaintState := psNormal;
  FDragging := False;
  Invalidate;
end;

procedure TCustomWideSpeedButton.WMMouseMove(var Message: TWMMouseMove);
begin
  inherited;
  if not Dragging then
  begin
    if not(csLButtonDown in ControlState) and (PaintState <> psHot) then
      PaintState := psHot;
    StartHotTimer;
  end
end;

procedure TCustomWideSpeedButton.WMThemeChanged(var Message: TMessage);
begin
  inherited;
  FreeThemes;  // Force reload of Theme Handles on next call
  if UseThemes then
    Include(FState, bsThemesActive);
end;

procedure TCustomWideSpeedButton.WriteCaption(Writer: TWriter);

// The Write procedure for the DefineProperty "WideText"

begin
  Writer.WriteWideString(Caption);
end;

{ TCustomVirtualToolbar }

procedure TCustomVirtualToolbar.BeginUpdate;
begin
  Inc(FLockUpdateCount)
end;

function TCustomVirtualToolbar.CalcMaxButtonSize(Font: TFont): TSize;

// Runs through the ButtonList looking for the widest/highest button extent in the list
// and returns the larget value found for each direction

var
  i: integer;
  R: TRect;
begin
  FillChar(Result, SizeOf(Result), 0);
  if ButtonList.Count = 0 then
    Result := CaptionButton.CalcMaxExtentSize(Font)
  else
    for i := 0 to ButtonList.Count - 1 do
    begin
      R := ButtonList[i].CalcMaxExtentRect(Font);
      if RectWidth(R) > Result.cx then
        Result.cx := RectWidth(R);
      if RectHeight(R) > Result.cy then
        Result.cy := RectHeight(R);
    end
end;

function TCustomVirtualToolbar.CanAutoSize(var NewWidth, NewHeight: Integer): Boolean;

// Called if the AutoSize property is set before any sizing is allowed

var
  R: TRect;
begin
  Result := inherited CanAutoSize(NewWidth, NewHeight);
  if Result then
  begin
    R := ViewportBounds;
    if NewWidth <> RectWidth(R) then
      NewWidth := RectWidth(R);
    if NewHeight <> RectHeight(R) then
      NewHeight := RectHeight(R);
  end
end;

function TCustomVirtualToolbar.ClosestButtonEdge(ScreenPoint: TPoint): TRect;
var
  ClientPt: TPoint;
  Button: TCustomWideSpeedButton;
begin
  Result := Rect(0, 0, 0, 0);
  if ButtonList.Count > 0 then
  begin
    ClientPt := ScreenToClient(ScreenPoint);
    Button := PointToButton(ScreenPoint);

    if Align in [alTop, alBottom] then
    begin
      if Assigned(Button) then
      begin
        if ClientPt.x < Button.Left + (Button.Right - Button.Left) div 2 then
          Result := Rect(Button.Left, Button.Top, Button.Left, Button.Bottom)
        else
          Result := Rect(Button.Right, Button.Top, Button.Right, Button.Bottom)
      end else
      begin
        if ClientPt.x < ButtonList[0].Left then
        begin
          Button := ButtonList[0];
          Result := Rect(Button.Left, Button.Top, Button.Left, Button.Bottom)
        end else
        begin
          Button := ButtonList[ButtonList.Count - 1];
          Result := Rect(Button.Right, Button.Top, Button.Right, Button.Bottom)
        end
      end
    end else
    begin
      if Assigned(Button) then
      begin
        if ClientPt.y < Button.Top + (Button.Bottom - Button.Top) div 2 then
          Result := Rect(Button.Left, Button.Top, Button.Right, Button.Top)
        else
          Result := Rect(Button.Left, Button.Bottom, Button.Right, Button.Bottom)
      end else
      begin
        if ClientPt.y < ButtonList[0].Top then
        begin
          Button := ButtonList[0];
          Result := Rect(Button.Left, Button.Top, Button.Right, Button.Top)
        end else
        begin
          Button := ButtonList[ButtonList.Count - 1];
          Result := Rect(Button.Left, Button.Bottom, Button.Right, Button.Bottom)
        end
      end
    end
  end
end;

procedure TCustomVirtualToolbar.CreateButtons;
begin
  DoCreateButtons
end;

procedure TCustomVirtualToolbar.CMColorChanged(var Message: TMessage);
begin
  inherited;
  Include(FStates, tsBackBitsStale);
  Invalidate
end;

constructor TCustomVirtualToolbar.Create(AOwner: TComponent);
begin
  inherited;
  SHGetMalloc(FMalloc);
  ControlState := ControlState + [csCreating];
  BeginUpdate;  // Lock the update until the window is created;
  ControlState := ControlState + [csCreating];
  FButtonList := CreateButtonList;
  FButtonList.Toolbar := Self;
  FCaptionButton := TCaptionButton.Create(Self);
  FCaptionButton.Parent := Self;
  BackBits := TBitmap.Create;
  Align := alTop;
  Height := 24;
  FScrollBtnSize := 12;
  FButtonLayout := blGlyphLeft;
  FButtonMargin := -1;
  FButtonSpacing := 4;
  FOldFontChangeEvent := Font.OnChange;
  Font.OnChange := FontChangeNotify;
  EdgeBorders := [ebTop];
  EdgeInner := esRaised;
  EdgeOuter := esLowered;
  ShowHint := True;
  TabStop := False;
  ControlState := ControlState - [csCreating];
  DoubleBuffered := True;
  FOptions := [toThemeAware];
  FContent := [stcFolders, stcFiles, stcPrograms];
  FDropInsertMargin := 4;
  CoCreateInstance(CLSID_DragDropHelper, nil, CLSCTX_INPROC_SERVER, IID_IDropTargetHelper, FDropTargetHelper);
  ControlState := ControlState - [csCreating]
end;

function TCustomVirtualToolbar.CreateButtonList: TVirtualButtonList;
// Overridable to create a new ButtonAttribute class decendant
begin
  Result := TVirtualButtonList.Create
end;

procedure TCustomVirtualToolbar.CreateParams(var Params: TCreateParams);
begin
  inherited;
end;

procedure TCustomVirtualToolbar.CreateWnd;
begin
  inherited;
  Perform(WM_THEMECHANGED, 0, 0);
  {$IFDEF THREADEDICONS}
  ThreadedImagesEnabled := toThreadedImages in Options;
  {$ENDIF}
   {$IFDEF SHELLNOTIFIER}
  ChangeNotifierEnabled := toShellNotifyThread in Options;
  {$ENDIF}
  CreateButtons;
  Include(FStates, tsBackBitsStale); // Force the Background bits to be updated to new size
  // Now we can rebuild the toolbar and set things up (BeginUpdate is in the constructor)
  EndUpdate;
  if not (csDesigning in ComponentState) then
    RegisterDragDrop(Handle, Self);
end;

procedure TCustomVirtualToolbar.DefineProperties(Filer: TFiler);

// Per Mike Liscke the VCL screws up streaming WideStrings so define a new property
// type 'WideText'

begin
  inherited;
  Filer.DefineProperty('WideText', ReadCaption, WriteCaption, Caption <> '');
end;

destructor TCustomVirtualToolbar.Destroy;
begin
  {$IFDEF THREADEDICONS}
  ThreadedImagesEnabled := False;
  {$ENDIF}
  {$IFDEF SHELLNOTIFIER}
  ChangeNotifierEnabled := False;
  {$ENDIF}
  BackBits.Free;
  ButtonList.Free;
  FreeThemes;
  FMalloc := nil;
  inherited;
end;

procedure TCustomVirtualToolbar.DestroyWnd;
begin
  inherited;
  ButtonList.Clear;
  if not (csDesigning in ComponentState) then
    RevokeDragDrop(Handle);
end;

function TCustomVirtualToolbar.DragEnter(const dataObj: IDataObject;
  grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
var
  FormatEtc: TFormatEtc;
begin
  if Assigned(DropTargetHelper) then
    DropTargetHelper.DragEnter(Handle, dataObj, Pt, dwEffect);

  FDragDropDataObj := dataObj;  // increase its ref count

  if ((toLaunchDropable in Options) or (toInsertDropable in Options)) and (IsValidIDListData(dataObj)) then
    Include(FStates, tsShellIDListValid);

  FormatEtc := FillFormatEtc(CF_VSTSHELLTOOLBAR, nil, DVASPECT_CONTENT, -1, TYMED_ISTREAM);
  if (toCustomizable in Options) and Succeeded( dataObj.QueryGetData(FormatEtc)) then
    Include(FStates, tsVSTShellToobarValid);

  UpdateDropStates(pt);

  // Just easier not to have to deal with this here.  Wait for a DragOver to handle Drag issues
  FDropTarget := nil;

  dwEffect := DROPEFFECT_NONE; // Think negative!
  if States * [tsVSTShellToobarValid, tsShellIDListValid] <> [] then
  begin
    if tsDragInLaunchZone in States then
      dwEffect := DROPEFFECT_MOVE
    else
    if tsDragInDropZone in States then
      dwEffect := DROPEFFECT_LINK
  end;


  Result := S_OK
end;

function TCustomVirtualToolbar.DragLeave: HResult;
begin
  Result := S_OK;
  try
    if Assigned(DropTargetHelper) then
      DropTargetHelper.DragLeave;

    FDragDropDataObj := nil;  // decrease its ref count

    if States * [tsVSTShellToobarValid, tsShellIDListValid] <> [] then
      if Assigned(FDropTarget) then
        Result := FDropTarget.DragLeave
  finally
    Exclude(FStates, tsVSTShellToobarValid);
    Exclude(FStates, tsShellIDListValid);
    Exclude(FStates, tsDragInDropZone);
    Exclude(FStates, tsDragInLaunchZone);
    FDropTarget := nil;
    DrawDropMarker(nil, False);
    Invalidate
  end
end;

function TCustomVirtualToolbar.DragOverOLE(grfKeyState: Integer; pt: TPoint;
  var dwEffect: Integer): HResult;
var
  OldTarget: TCustomWideSpeedButton;
begin
  // Update any drag image
  if Assigned(DropTargetHelper) then
    DropTargetHelper.DragOver(pt, dwEffect);

  Result := S_OK;

  // In the DragEnter we decided if the DataObject contained anything we could use
  if States * [tsVSTShellToobarValid, tsShellIDListValid] <> [] then
  begin
    OldTarget := FDropTarget;

    UpdateDropStates(pt);

    // If it is in an insert mode than do not allow the DropTarget property to be
    // valid since this operation is dependant on the toolbar to execute and not
    // on the button to execute as it will be during a launch drop.
    if tsDragInDropZone in States then
      FDropTarget := nil
    else
      FDropTarget := PointToButton(Pt);


    // If a different DropTarget then do a DragEnter/DragLeave on the buttons
    // Notice here we are using FDropTarget. If the mouse is in a DropZone we don't
    // want the button to think it is a drop target if the mode is insert
    if OldTarget <> FDropTarget then
    begin
      // Leave the old DropTarget
      if Assigned(OldTarget) then
        OldTarget.DragLeave;

      // Enter the new DropTarget
      if Assigned(FDropTarget) then
        FDropTarget.DragEnter(DragDropDataObj, grfKeyState, pt, dwEffect);
    end else
    begin
      // Same DropTarget to just move the mouse in it.
      if Assigned(FDropTarget) then
        Result := FDropTarget.DragOverOLE(grfKeyState, pt, dwEffect);
    end;
  end else
    dwEffect := DROPEFFECT_NONE;

  if not Assigned(FDropTarget) then
  begin
    dwEffect := DROPEFFECT_NONE;
    // In an insert (DropZone) button mode
    if (tsDragInDropZone in States) then
    begin
      // ShellIDLists are always "links"
      if tsShellIDListValid in States then
        dwEffect := DROPEFFECT_LINK
      else begin
        // Internal button moving can either copy or move
        if grfKeyState and MK_CONTROL <> 0 then
          dwEffect := DROPEFFECT_COPY
        else
          dwEffect := DROPEFFECT_MOVE
      end
    end
  end;

  UpdateWindow(Handle);
  DrawDropMarker(@Pt, False);
end;

procedure TCustomVirtualToolbar.DrawDropMarker(MousePos: PPoint; ForceDraw: Boolean);
// Draws the InsertMark in the Toolbar.  R should come in as a zero width (or height)
// rectangle that is inflated to the correct with.  Ususally it is the dividing line
// between to buttons or the first / last edge of button 0 or MaxButton

    function CalcDropMarkRect(Pt: PPoint): TRect;
    var
      Temp: TPoint;
    begin
      if not Assigned(Pt) then
        Temp := Mouse.CursorPos
      else
        Temp := Pt^;
      Result := ClosestButtonEdge(Temp);

      // Inflate the Line to at rectangle to contain the InsertMark image
      if Align in [alTop, alBottom] then
        InflateRect(Result, 4, 0)
      else
        InflateRect(Result, 0, 4);
    end;

var
  Bitmap, Bits: TBitmap;
  ImageList: TImageList;
  DC: hDc;
  R: TRect;
  i: integer;
begin

  if tsInsertImageVisible in States then
  begin
    R := CalcDropMarkRect(MousePos);

    // If the If the DropRect is in a new spot or the Mouse is no longer over a DropZone erase the Mark
    if not (EqualRect(R, FDropMarkRect) and (tsDragInDropZone in States)) then
    begin
      Exclude(FStates, tsInsertImageVisible);
      InvalidateRect(Handle, @FDropMarkRect, True);
      SetRect(FDropMarkRect, 0, 0, 0, 0);
    end
  end;

  if ((tsDragInDropZone in States) and IsRectEmpty(FDropMarkRect)) or ForceDraw then
  begin
    R := CalcDropMarkRect(MousePos);
    DC := 0;
    ImageList := TImageList.Create(nil);
    Bitmap := TBitmap.Create;
    try
      Bitmap.Width := RectWidth(R);
      Bitmap.Height := RectHeight(R);
      Bitmap.Canvas.Brush.Color := clFuchsia;
      Bitmap.Canvas.FillRect(Rect(0, 0, Bitmap.Width, Bitmap.Height));

      Bits := TBitmap.Create;
      try
        if Align in [alTop, alBottom] then
        begin
          Bits.LoadFromResourceName(hInstance, 'VERTCENTER');
          i := 0;
          while i < RectHeight(R) do
          begin
            Bitmap.Canvas.Draw(2, i, Bits);
            Inc(i)
          end;
          Bits.LoadFromResourceName(hInstance, 'VERTTOP');
          Bitmap.Canvas.Draw(0, 0, Bits);
          Bits.LoadFromResourceName(hInstance, 'VERTBOTTOM');
          Bitmap.Canvas.Draw(0, RectHeight(R) - 3, Bits);
        end else
        if Align in [alLeft, alRight] then
        begin
          Bits.LoadFromResourceName(hInstance, 'HORZCENTER');
          i := 0;
          while i < RectWidth(R) do
          begin
            Bitmap.Canvas.Draw(i, 2, Bits);
            Inc(i)
          end;
          Bits.LoadFromResourceName(hInstance, 'HORZLEFT');
          Bitmap.Canvas.Draw(0, 0, Bits);
          Bits.LoadFromResourceName(hInstance, 'HORZRIGHT');
          Bitmap.Canvas.Draw(RectWidth(R) - 3, 0, Bits);
        end;
      finally
        Bits.Free
      end;

      ImageList.Width := Bitmap.Width;
      ImageList.Height := Bitmap.Height;

      ImageList.AddMasked(Bitmap, clFuchsia);
      DC := GetDC(Handle);
      if DC <> 0 then
        ImageList_DrawEx(ImageList.Handle, 0, DC, R.Left, R.Top, ImageList.Width,
         ImageList.Height, CLR_NONE, CLR_NONE, ILD_TRANSPARENT);

      Include(FStates, tsInsertImageVisible);
    finally
      FDropMarkRect := R;
      if DC <> 0 then
        ReleaseDC(Handle, DC);
      Bitmap.Free;
      ImageList.Free;
    end
  end
end;

function TCustomVirtualToolbar.Drop(const dataObj: IDataObject;
  grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
var
  ShellIDList: TShellIDList;
  VSTShellToolbar: TVSTShellToolbar;
  LogicalPerformedDropEffect: TLogicalPerformedDropEffect;
  i, DropIndex: integer;
  NewButton: TShellToolButton;
begin
  try
    if Assigned(DropTargetHelper) then
      DropTargetHelper.Drop(dataObj, pt, dwEffect);

    if States * [tsVSTShellToobarValid, tsShellIDListValid] <> [] then
    begin
      if Assigned(FDropTarget) then
        Result := FDropTarget.Drop(dataObj, grfKeyState, pt, dwEffect)
      else begin
        Result := S_OK;

        DropIndex := PointToInsertIndex( Pt);

        if tsShellIDListValid in States then
        begin
          BeginUpdate;
          ShellIDList := TShellIDList.Create;
          try
            ShellIDList.LoadFromDataObject(dataObj);
            for i := 0 to ShellIDList.PIDLCount - 1 do
            begin
              NewButton := TShellToolButton( ButtonList.AddButton(DropIndex));
              NewButton.Namespace := TNamespace.Create(ShellIDList.AbsolutePIDL(i), nil);
            end
          finally
            ShellIDList.Free;
            EndUpdate;
          end
        end else
        if tsVSTShellToobarValid in States then
        begin
          VSTShellToolbar := TVSTShellToolbar.Create;
          try
            BeginUpdate;
            if VSTShellToolbar.LoadFromDataObject(dataObj) then
            begin
              NewButton := TShellToolButton( ButtonList.AddButton(DropIndex));
              NewButton.Namespace := TNamespace.Create(VSTShellToolbar.PIDL, nil);

              // Let the source know what happened
              LogicalPerformedDropEffect := TLogicalPerformedDropEffect.Create;
              try
                if grfKeyState and MK_CONTROL <> 0 then
                  LogicalPerformedDropEffect.Action := effectCopy
                else
                  LogicalPerformedDropEffect.Action := effectMove;

                LogicalPerformedDropEffect.SaveToDataObject(dataObj);
              finally
                LogicalPerformedDropEffect.Free
              end

            end
          finally
            VSTShellToolbar.Free;
            EndUpdate;
          end
        end
      end
    end else
    begin
      dwEffect := DROPEFFECT_NONE;
      Result := S_OK
    end;

  finally
    Exclude(FStates, tsVSTShellToobarValid);
    Exclude(FStates, tsShellIDListValid);
    Exclude(FStates, tsDragInDropZone);
    Exclude(FStates, tsDragInLaunchZone);
    FDragDropDataObj := nil;  // decrease its ref count
    FDropTarget := nil;
    DrawDropMarker(@Pt, False);  
    DoCreateButtons
  end
end;

procedure TCustomVirtualToolbar.EndUpdate;
begin
  Dec(FLockUpdateCount);
  if FLockUpdateCount < 0 then
    FLockUpdateCount := 0;
  if FLockUpdateCount = 0 then
  begin
    RebuildToolbar;
    Invalidate;
    Update
  end
end;

procedure TCustomVirtualToolbar.FontChangeNotify(Sender: TObject);

// Hook the Font Change chain so we are notified when any Font attributes are
// changed

begin
  // don't break the chain
  if Assigned(FOldFontChangeEvent) then
    FOldFontChangeEvent(Sender);
  RebuildToolbar
end;

procedure TCustomVirtualToolbar.FreeThemes;

// Frees theme handles

begin
  if ThemeToolbar <> 0 then
  begin
    CloseThemeData(FThemeToolbar);
    FThemeToolbar := 0
  end;
end;

function TCustomVirtualToolbar.GetAlign: TAlign;
begin
  Result := inherited Align
end;

function TCustomVirtualToolbar.GetBkGndParent: TWinControl;
begin
  Result := Parent
end;

function TCustomVirtualToolbar.GetEdgeBorders: TEdgeBorders;
begin
  Result := inherited EdgeBorders
end;

function TCustomVirtualToolbar.GetEdgeInner: TEdgeStyle;
begin
  Result := inherited EdgeInner
end;

function TCustomVirtualToolbar.GetEdgeOuter: TEdgeStyle;
begin
   Result := inherited EdgeOuter
end;

function TCustomVirtualToolbar.GetViewportBounds: TRect;

// Finds the smallest rectangle that will contain all the buttons and caption
// Used for Autosizing the parent window

var
  i: integer;
begin
 // Result := CaptionButton.BoundsRect;
  SetRect(Result, 0, 0, 0, 0);
  for i := 0 to ButtonList.Count - 1 do
    UnionRect(Result, Result, ButtonList[i].BoundsRect);
  if Assigned(CaptionButton) then
    UnionRect(Result, Result, CaptionButton.BoundsRect);

  if EdgeInner in [esLowered, esRaised] then
  begin
    if ebLeft in EdgeBorders then
      Inc(Result.Right);
    if ebRight in EdgeBorders then
      Inc(Result.Right);
    if ebTop in EdgeBorders then
      Inc(Result.Bottom);
    if ebBottom in EdgeBorders then
      Inc(Result.Bottom);
  end;
   if EdgeOuter in [esLowered, esRaised] then
   begin
    if ebLeft in EdgeBorders then
      Inc(Result.Right);
    if ebRight in EdgeBorders then
      Inc(Result.Right);
    if ebTop in EdgeBorders then
      Inc(Result.Bottom);
    if ebBottom in EdgeBorders then
      Inc(Result.Bottom);
  end;
end;

function TCustomVirtualToolbar.GetWideCaption: WideString;
begin
  Result := CaptionButton.Caption
end;

procedure TCustomVirtualToolbar.Loaded;
begin
  inherited;
end;

procedure TCustomVirtualToolbar.LoadFromFile(FileName: WideString);
var
  F: TWideFileStream;
begin
  F := TWideFileStream.Create(FileName, fmOpenRead or fmShareExclusive);
  try
    LoadFromStream(F)
  finally
    F.Free
  end
end;

procedure TCustomVirtualToolbar.LoadFromStream(S: TStream);
var
  Count, i: integer;
begin
  BeginUpdate;
  try
    ButtonList.Clear;
    S.read(Count, SizeOf(Count));
    for i := 0 to Count - 1 do
      ButtonList.AddButton;
    for i := 0 to ButtonList.Count - 1 do
      ButtonList[i].LoadFromStream(S);
  finally
    DoCreateButtons;
    EndUpdate
  end
end;

function TCustomVirtualToolbar.GiveFeedback(dwEffect: Integer): HResult;
begin
  Result := DRAGDROP_S_USEDEFAULTCURSORS;
end;

function TCustomVirtualToolbar.IsValidIDListData(DataObject: IDataObject): Boolean;
var
  ShellIDList: TShellIDList;
  i: integer;
  NS: TNamespace;
  IsFolder, IsFile, IsExe: Boolean;
  Ext: WideString;
begin
  Result := False;
  ShellIDList := TShellIDList.Create;
  try
    try
      if ShellIDList.LoadFromDataObject(DataObject as IDataObject) then
      begin
        // If all are defined then it will always accept the dataobject
        if not (Content = [stcFolders, stcFiles, stcPrograms]) then
        begin
          i := 0;
          while not Result and (i < ShellIDList.PIDLCount) do
          begin
            NS := TNamespace.Create(ShellIDList.AbsolutePIDL(i), nil);
            try
              IsFolder := NS.Folder;
              IsFile := not NS.Folder;
              Ext := ExtractFileExtW(NS.NameParseAddress);
              Ext := StrLowerW( PWideChar(Ext));
              IsExe := (Ext = '.exe') or (Ext = '.bat') or (Ext = '.com');
              if Content = [stcFolders, stcPrograms] then
                Result := IsFolder or IsExe
              else
              if Content = [stcFiles, stcPrograms] then
                Result := not IsFolder and IsFile
              else
              if Content = [stcFolders, stcFiles] then
                Result := (IsFolder or IsFile) and not IsExe
              else
              if Content = [stcFolders] then
                Result := IsFolder and not IsFile and not IsExe
              else
              if Content = [stcFiles] then
                Result := not IsFolder and IsFile and not IsExe
              else
              if Content = [stcPrograms] then
                Result := not IsFolder and IsFile and IsExe
              else
                Result := False;
            finally
              Inc(i);
              FreeAndNil(NS)
            end
          end
        end else
          Result := True;
      end
    finally
      ShellIDList.Free;
    end
  except
    Result := False
  end
end;

procedure TCustomVirtualToolbar.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = BkGndParent) then
    BkGndParent := nil
end;

procedure TCustomVirtualToolbar.ReadCaption(Reader: TReader);
begin
  case Reader.NextValue of
    vaLString, vaString:
      Caption := Reader.ReadString;
  else
    Caption := Reader.ReadWideString;
  end;
end;

procedure TCustomVirtualToolbar.RebuildToolbar;
var
  i: integer;
begin
  if not (csCreating in ControlState) and (FLockUpdateCount = 0) then
  begin
    Include(FStates, tsBackBitsStale); // Rebuild the background image
    for i := 0 to ButtonList.Count - 1 do
      ButtonList[i].RebuildButton;
    ArrangeButtons;
    if AutoSize then
      AdjustSize;
    Invalidate
  end
end;

procedure TCustomVirtualToolbar.SaveToFile(FileName: WideString);
var
  F: TWideFileStream;
begin
  F := TWideFileStream.Create(FileName, fmCreate or fmShareExclusive);
  try
    SaveToStream(F)
  finally
    F.Free
  end
end;

procedure TCustomVirtualToolbar.SaveToStream(S: TStream);
var
  i: integer;
begin
  i := ButtonList.Count;
  S.write(i, SizeOf(i));
  for i := 0 to ButtonList.Count - 1 do
    ButtonList[i].SaveToStream(S);
end;

procedure TCustomVirtualToolbar.SetAlign(const Value: TAlign);
begin
  if inherited Align <> Value then
  begin
    inherited Align := Value;
    if (Value = alLeft) or (Value = alRight) then
    begin
      CaptionButton.Align := alTop;
      ResizeCaptionButton;
    end else
    if (Value = alTop) or (Value = alBottom) then
    begin
      CaptionButton.Align := alLeft;
      ResizeCaptionButton;
    end;
    RebuildToolbar
  end
end;

procedure TCustomVirtualToolbar.SetBkGndParent(const Value: TWinControl);
begin
  if (Value <> Parent) and (Value <> Self) then
  begin
    if Assigned(Value) then
      Parent := Value
    else
      Parent := GetParentForm(Self);
    RebuildToolbar
  end;
end;

procedure TCustomVirtualToolbar.SetButtonLayout(const Value: TButtonLayout);
var
  i: integer;
begin
  if FButtonLayout <> Value then
  begin
    FButtonLayout := Value;
    for i := 0 to ButtonList.Count - 1 do
      ButtonList[i].Layout := Value;
    RebuildToolbar
  end
end;

procedure TCustomVirtualToolbar.SetButtonMargin(const Value: integer);
var
  i: integer;
begin
  if FButtonMargin <> Value then
  begin
    FButtonMargin := Value;
    for i := 0 to ButtonList.Count - 1 do
      ButtonList[i].Margin := Value;
    RebuildToolbar
  end
end;

procedure TCustomVirtualToolbar.SetButtonSpacing(const Value: integer);
var
  i: integer;
begin
  if FButtonSpacing <> Value then
  begin
    FButtonSpacing := Value;
    for i := 0 to ButtonList.Count - 1 do
      ButtonList[i].Spacing := Value;
    RebuildToolbar
  end
end;

procedure TCustomVirtualToolbar.SetEdgeBorders(const Value: TEdgeBorders);
begin
  inherited EdgeBorders := Value;
  RebuildToolbar
end;

procedure TCustomVirtualToolbar.SetEdgeInner(const Value: TEdgeStyle);
begin
  inherited EdgeInner := Value;
  RebuildToolbar
end;

procedure TCustomVirtualToolbar.SetEdgeOuter(const Value: TEdgeStyle);
begin
  inherited EdgeOuter := Value;
  RebuildToolbar
end;

procedure TCustomVirtualToolbar.SetName(const Value: TComponentName);
var
  ChangeText: Boolean;
begin
  // Do it just like TControl but I have created a new Caption (widestring) so I
  // have to duplicate it.
  ChangeText := (csSetCaption in ControlStyle) and
    not (csLoading in ComponentState) and (Caption = Text) and
    ((Owner = nil) or not (Owner is TControl) or
    not (csLoading in TControl(Owner).ComponentState));
  inherited;
  if ChangeText then
    Caption := Text
end;

procedure TCustomVirtualToolbar.PaintToolbar(DC: HDC);
begin
  if FLockUpdateCount = 0 then
  begin
    if tsBackBitsStale in States then
      StoreBackGndBitmap;
    BitBlt(DC, 0, 0, Width, Height, BackBits.Canvas.Handle, 0, 0, SRCCOPY);
  end
end;

procedure TCustomVirtualToolbar.PaintWindow(DC: HDC);
begin
  PaintToolbar(DC)
end;

function TCustomVirtualToolbar.PointToButton(ScreenPt: TPoint): TCustomWideSpeedButton;
var
  i: integer;
begin
  Result := nil;
  i := 0;
  ScreenPt := ScreenToClient(ScreenPt);
  while not Assigned(Result) and (i < ButtonList.Count) do
  begin
    if PtInRect(ButtonList[i].BoundsRect, ScreenPt) then
      Result :=  ButtonList[i];
    inc(i)
  end
end;

function TCustomVirtualToolbar.QueryContinueDrag(fEscapePressed: BOOL;
  grfKeyState: Integer): HResult;
begin
  if fEscapePressed then
  begin
    Result := DRAGDROP_S_CANCEL;
    Exit
  end;
  Result := S_OK
end;

procedure TCustomVirtualToolbar.ReCreateButtons;

// Frees all buttons then ReCreates them not allowing the window to resize or flicker

var
  WasAutoSize: Boolean;
begin
  WasAutoSize := AutoSize;
  BeginUpdate;
  try
    AutoSize := False;
    ButtonList.Clear;
    CreateButtons;
    ArrangeButtons;
  finally
    EndUpdate;
    AutoSize := WasAutoSize;
  end
end;

procedure TCustomVirtualToolbar.SetOptions(const Value: TVirtualToolbarOptions);

    function BitChanged(New, Old: TVirtualToolbarOptions; Bit: TVirtualToolbarOption): Boolean;
    begin
      Result := ((Bit in New) and not (Bit in Old)) or ((Bit in Old) and not (Bit in New))
    end;

var
  OldOptions: TVirtualToolbarOptions;
  i: integer;
  ImageList: TCustomImageList;
  
begin
  if FOptions <> Value then
  begin
    OldOptions := FOptions;
    FOptions := Value;

    // What transparent is and what should be transparent becomes unclear with
    // Themes so just don't use them with transparency
    if BitChanged(Value, OldOptions, toTransparent) then
    begin

    end;

    if BitChanged(Value, OldOptions, toThemeAware) then
    begin

    end;

    {$IFDEF SHELLNOTIFIER}
    if BitChanged(Value, OldOptions, toShellNotifyThread) then
    begin
      if (toShellNotifyThread in Value) and not (ComponentState * [csDesigning, csLoading] <> []) then
        ChangeNotifierEnabled := True
      else
        ChangeNotifierEnabled := False
    end;
    {$ENDIF}

    if BitChanged(Value, OldOptions, toFlat) then
    begin
      for i := 0 to ButtonList.Count - 1 do
        ButtonList[i].Flat := toFlat in Value
    end;

    if BitChanged(Value, OldOptions, toThemeAware) then
    begin
      for i := 0 to ButtonList.Count - 1 do
        ButtonList[i].ThemeAware := toThemeAware in Value
    end;

    if BitChanged(Value, OldOptions, toTransparent) then
    begin
      for i := 0 to ButtonList.Count - 1 do
        ButtonList[i].Transparent := toTransparent in Value
    end;

    {$IFDEF THREADEDICONS}
    if BitChanged(Value, OldOptions, toThreadedImages) then
    begin
      if toThreadedImages in Value then
        ThreadedImagesEnabled := True
      else
        ThreadedImagesEnabled := False;                     
    end;
    {$ENDIF}

    if BitChanged(Value, OldOptions, toLargeButtons) then
    begin
      if toLargeButtons in FOptions then
        ImageList := LargeSysImages
      else
        ImageList := SmallSysImages;
      for i := 0 to ButtonList.Count - 1 do
        ButtonList[i].ImageList := ImageList;
    end;

    RebuildToolbar
  end;
end;

procedure TCustomVirtualToolbar.SetWideCaption(const Value: WideString);
begin
  CaptionButton.Caption := Value;
  RebuildToolbar;
end;

procedure TCustomVirtualToolbar.StoreBackGndBitmap;

// Stores a bitmap that is used for the backgound of the toolbar.  Its main use
// is to call WM_PRINT of its owner when the toolbar is to be transparent but since
// it is here if not transparent the normal solid brush is drawn.  The image is cached
// and to force a reload of the image set BackBitsStale to True then the next time
// PaintWindow is called the backgound image will be updated.

      procedure PaintBackGround(BkGndDC: hDc);
      const
        // If we print children we will be trying to print ourselves from within
        // a paint message ending in infinate recursion
        PrintFlags = PRF_CHECKVISIBLE {or PRF_CHILDREN} or PRF_CLIENT or PRF_ERASEBKGND;
      var
        Brush, OldBrush: hBrush;
        OldPt: TPoint;
      begin
        if toTransparent in Options then
        begin
         // Shift the viewport so the area under the control is painted to the bitmap
         // at (0, 0)
          SetViewportOrgEx(BkGndDC, -Left, -Top, @OldPt);
          Parent.Perform(WM_PRINT, Integer(BkGndDC), Integer(PrintFlags));
          SetViewportOrgEx(BkGndDC, OldPt.x, OldPt.y, nil);
        end else
        begin
          Brush := CreateSolidBrush(ColorToRGB(Color));
          OldBrush := SelectObject(BkGndDC, Brush);
          FillRect(BkGndDC, ClientRect, Brush);
          SelectObject(BkGndDC, OldBrush);
          DeleteObject(Brush);
        end
      end;

begin
  BackBits.Canvas.Lock;
  try
    BackBits.Width := Width;
    BackBits.Height := Height;
    PaintBackGround(BackBits.Canvas.Handle);
  finally
    BackBits.Canvas.UnLock;
    Exclude(FStates, tsBackBitsStale);
  end
end;

procedure TCustomVirtualToolbar.WMEraseBkGnd(var Message: TWMEraseBkGnd);
begin
  // We are handling the background
  Message.Result := 1;
end;

procedure TCustomVirtualToolbar.WMLButtonUp(var Message: TWMLButtonUp);
begin
  inherited;
end;

procedure TCustomVirtualToolbar.WMMouseMove(var Message: TWMMouseMove);
begin
  inherited;
  // Set a timer to catch any missed CM_MOUSELEAVE messages that leave hot images
  // behind.  CM_MOUSEENTER can miss it too so put this here
  if HotTrackTimer = 0 then
    // The timerID uses object address for a unique value
    HotTrackTimer := SetTimer(Handle, integer(Self), HOTTRACKDELAY, nil);
end;

procedure TCustomVirtualToolbar.WMPaint(var Message: TWMPaint);
begin
  inherited;
  DrawDropMarker(nil, False)
end;

procedure TCustomVirtualToolbar.WMPrint(var Message: TWMPrint);
begin
  PaintWindow(Message.DC);
end;

procedure TCustomVirtualToolbar.WMPrintClient(var Message: TWMPrintClient);
begin
  PaintWindow(Message.DC);
end;

procedure TCustomVirtualToolbar.WMRemoveButton(var Message: TMessage);
begin
  if Message.wParam <> 0 then
  begin
    ButtonList.RemoveButton(Pointer( Message.wParam));
    RebuildToolbar;
  end
end;

procedure TCustomVirtualToolbar.WMSize(var Message: TWMSize);
begin
  inherited;
  // WM_WindowPosChanging is tough to disinguish when the dx, dy params are
  // valid.  Here I know they are
  RebuildToolbar;
end;

procedure TCustomVirtualToolbar.WMThemeChanged(var Message: TMessage);
begin
  inherited;
  FreeThemes;
  if UseThemes then
  begin
    Include(FStates, tsThemesActive);
    ThemeToolbar := OpenThemeData(Handle, 'toolbar');
    RedrawWindow(Handle, nil, 0, RDW_FRAME or RDW_INVALIDATE or RDW_NOERASE or RDW_NOCHILDREN);
  end else
    Exclude(FStates, tsThemesActive);
end;

procedure TCustomVirtualToolbar.WMTimer(var Message: TMessage);
var
  Pt: TPoint;
begin
  // The timerID uses object address for a unique value
  if Message.wParam = Integer(Self) then
  begin
    Pt := ScreenToClient(Mouse.CursorPos);
    if not PtInRect(ClientRect, Pt) then
    begin
      KillTimer(Handle, HotTrackTimer);
      HotTrackTimer := 0;
      Perform(CM_MOUSELEAVE, 0, 0);
    end
  end
end;

procedure TCustomVirtualToolbar.WMWindowPosChanging(var Message: TWMWindowPosChanging);
const
  BackgroundValid = SWP_NOSIZE or SWP_NOMOVE;
begin
  with Message.WindowPos^ do
  begin
    if not (Flags and SWP_NOSIZE) <> 0 then
    begin
      Include(FStates, tsBackBitsStale);
    end;
    inherited;
  end
end;

procedure TCustomVirtualToolbar.WriteCaption(Writer: TWriter);
begin
  Writer.WriteWideString(Caption);
end;

//------------------------------------------------------------------------------


procedure TCustomVirtualToolbar.ArrangeButtons;
var
  i, DeltaX, DeltaY: integer;
  ButtonArea: TRect;
  Size: TSize;
begin
  ButtonArea := ClientRect;

  if ButtonList.Count > 0 then
  begin
    // The buttons need to be checked for size before they can be aligned
    Size := CalcMaxButtonSize(Font);
    for i := 0 to ButtonList.Count - 1 do
    begin
      ButtonList[i].AutoSize := True;
      if Align in [alLeft, alRight] then
      begin
        ButtonList[i].AutoSize := False;
        ButtonList[i].Width := ClientWidth; //maximum Width
      end
      else begin
        if toEqualWidth in Options then
        begin
          ButtonList[i].AutoSize := False;
          ButtonList[i].Width := Size.cx
        end;
        if not (toTile in Options) then
        begin
          ButtonList[i].AutoSize := False;
          ButtonList[i].Height := ClientHeight; //maximum Height
        end;
      end;
    end;


    if Align in [alLeft, alRight] then
    begin
      if toTile in Options then
      begin
        ButtonArea.Top := CaptionButton.Bottom;

        DeltaY := 0;
        DeltaX := 0;
        for i := 0 to ButtonList.Count - 1 do
        begin
          ButtonList[i].Left := ButtonArea.Left + DeltaX;
          ButtonList[i].Top := ButtonArea.Top + DeltaY;
          DeltaY := DeltaY + ButtonList[i].Height;

          if i < ButtonList.Count - 1 then
          begin
            if (ButtonArea.Top + DeltaY + ButtonList[i+1].Height) > (Top + Height) then
            begin
              DeltaX := DeltaX + ButtonList[i].Width;
              DeltaY := 0;
            end
          end
        end
      end else
      begin
        ButtonArea.Top := CaptionButton.Bottom;

        DeltaY := 0;
        for i := 0 to ButtonList.Count - 1 do
        begin
          ButtonList[i].Left := ButtonArea.Left;
          ButtonList[i].Top := ButtonArea.Top + DeltaY;
          DeltaY := DeltaY + ButtonList[i].Height
        end
      end
    end else
    begin
      if toTile in Options then
      begin
        ButtonArea.Left := CaptionButton.Right;

        DeltaX := 0;
        DeltaY := 0;
        for i := 0 to ButtonList.Count - 1 do
        begin
          ButtonList[i].Top := ButtonArea.Top + DeltaY;
          ButtonList[i].Left := ButtonArea.Left + DeltaX;
          DeltaX := DeltaX + ButtonList[i].Width;

          if i < ButtonList.Count - 1 then
          begin
            if (ButtonArea.Left + DeltaX + ButtonList[i+1].Width) > (Left + Width) then
            begin
              DeltaY := DeltaY + ButtonList[i].Height;
              DeltaX := 0;
            end
          end
        end
      end else
      begin
        ButtonArea.Left := CaptionButton.Right;

        DeltaX := 0;
        for i := 0 to ButtonList.Count - 1 do
        begin
          ButtonList[i].Top := ButtonArea.Top;
          ButtonList[i].Left := ButtonArea.Left + DeltaX;
          DeltaX := DeltaX + ButtonList[i].Width
        end

      end
    end
  end // ButtonList.Count > 0
end;


procedure TCustomVirtualToolbar.ResizeCaptionButton;
var
  Size: TSize;
begin
  Size := CaptionButton.CalcMaxExtentSize(Font);
  if Align in [alLeft, alRight] then
  begin
    CaptionButton.Height := Size.cy;
    CaptionButton.Width := Width
  end else
  begin
    CaptionButton.Width := Size.cy;
    CaptionButton.Height := Height
  end;
end;

procedure TCustomVirtualToolbar.UpdateDropStates(ScreenMousePos: TPoint);
// Updates the State property by checking and marking the correct state the drop
// would produce if dropped on ScreenMousePos.  Either it would be an insert of
// a new button or a drop of file(s) to launch on an existing button
var
  BtnClientPt: TPoint;
begin
  // Do we have any formats we can use? These are set in DragEnter
  if States * [tsVSTShellToobarValid, tsShellIDListValid] <> [] then
  begin
    FDropTarget := PointToButton(ScreenMousePos);
    if Assigned(FDropTarget) then
    begin
      BtnClientPt := FDropTarget.ScreenToClient(ScreenMousePos);
      if Align in [alTop, alBottom] then
      begin
        // This is really confusing....  Ok
        // If the tsVSTShellToobarValid is in States if is always true
        // If the toolbar is toInsertDropable and the mouse is within the defined
        // margin at the front or tail edge of the button then the if is true
        if (tsVSTShellToobarValid in States) or
           ((toInsertDropable in Options) and
           ((BtnClientPt.x < DropInsertMargin) or
           (BtnClientPt.x > FDropTarget.Width - DropInsertMargin))) then
        begin
          Include(FStates, tsDragInDropZone);
          Exclude(FStates, tsDragInLaunchZone);
        end else
        begin
          Exclude(FStates, tsDragInDropZone);
          if (toLaunchDropable in Options) then
            Include(FStates, tsDragInLaunchZone)
          else
            Include(FStates, tsDragInLaunchZone)
        end
      end else
      begin
        // This is really confusing....  Ok
        // If the tsVSTShellToobarValid is in States if is always true
        // If the toolbar is toInsertDropable and the mouse is within the defined
        // margin at the front or tail edge of the button then the if is true
        if (tsVSTShellToobarValid in States) or
           ((toInsertDropable in Options) and
           ((BtnClientPt.y < DropInsertMargin) or
           (BtnClientPt.y > FDropTarget.Height - DropInsertMargin))) then
        begin
          Include(FStates, tsDragInDropZone);
          Exclude(FStates, tsDragInLaunchZone);
        end else
        begin
          Exclude(FStates, tsDragInDropZone);
          if (toLaunchDropable in Options) then
            Include(FStates, tsDragInLaunchZone)
          else
            Include(FStates, tsDragInLaunchZone)
        end
      end
    end else
    begin
      Exclude(FStates, tsDragInLaunchZone);
      if toInsertDropable in Options then
        Include(FStates, tsDragInDropZone)
      else
        Exclude(FStates, tsDragInDropZone);
    end
  end
end;

function TCustomVirtualToolbar.PointToInsertIndex(ScreenPt: TPoint): integer;
// Finds the index of where to insert a new button based on the point passed to the
// method.  The point must be in Screen Coordinates.  First finds the closest edge
// of the button then uses that edge to determine the insert point.
var
  R: TRect;
  Index: integer;
  Done: Boolean;
begin
  Result := 0;
  Done := False;

  R := ClosestButtonEdge(ScreenPt);

  if Align in [alTop, alBottom] then
  begin
    Index := 0;
    while not Done and (Index < ButtonList.Count) do
    begin
      if (ButtonList[Index].Left = R.Left) and (ButtonList[Index].Top >= R.Top)
        and (ButtonList[Index].Bottom <= R.Bottom)  then
      begin
        Result := Index;
        Done := True
      end else
      if (ButtonList[Index].Right = R.Left)  and (ButtonList[Index].Top >= R.Top)
        and (ButtonList[Index].Bottom <= R.Bottom) then
      begin
        Result := Index + 1;
        Done := True
      end;
      Inc(Index)
    end;
  end else
  if Align in [alLeft, alRight] then
  begin
    Index := 0;
    while not Done and (Index < ButtonList.Count) do
    begin
      if (ButtonList[Index].Top = R.Top) and (ButtonList[Index].Left >= R.Left)
        and (ButtonList[Index].Right <= R.Right)  then
      begin
        Result := Index;
        Done := True
      end else
      if (ButtonList[Index].Bottom = R.Top)  and (ButtonList[Index].Left >= R.Left)
        and (ButtonList[Index].Right <= R.Right) then
      begin
        Result := Index + 1;
        Done := True
      end;
      Inc(Index)
    end
  end
end;

{$IFDEF THREADEDICONS}
procedure TCustomVirtualToolbar.SetThreadedImagesEnabled(const Value: Boolean);
begin
  if (ComponentState * [csDesigning, csLoading] = []) and not (csCreating in ControlState) then
  begin
    if FThreadedImagesEnabled <> Value then
    begin
      if Value then
      begin
        ImageThreadManager.RegisterControl(Self);
        FThreadedImagesEnabled := True
      end else
      begin
        ImageThreadManager.ClearPendingItems(Self, WM_VTSETICONINDEX, Malloc);
        ImageThreadManager.UnRegisterControl(Self);
        FThreadedImagesEnabled := False
      end
    end
  end
end;
{$ENDIF}

procedure TCustomVirtualToolbar.WMNCDestroy(var Message: TWMNCDestroy);
begin
  {$IFDEF THREADEDICONS}
  ThreadedImagesEnabled := False;
  {$ENDIF}
  {$IFDEF SHELLNOTIFIER}
  ChangeNotifierEnabled := False;
  {$ENDIF}
  inherited;
end;

{$IFDEF SHELLNOTIFIER}
procedure TCustomVirtualToolbar.SetChangeNotiferEnabled(const Value: Boolean);
begin
  if FChangeNotifierEnabled <> Value then
  begin
    if Value and not (ComponentState * [csDesigning, csLoading] <> []) then
    begin
      ChangeNotifier.RegisterShellChangeNotify(Self);
      FChangeNotifierEnabled := True
    end else
    begin
      ChangeNotifier.UnRegisterShellChangeNotify(Self);
      FChangeNotifierEnabled := False
    end
  end
end;
{$ENDIF}

procedure TCustomVirtualToolbar.RecreateToolbar;
begin
  ReCreateButtons;
  DoRecreateButtons
end;

procedure TCustomVirtualToolbar.DoRecreateButtons;
begin
  if Assigned(OnRecreateButtons) then
    OnReCreateButtons(Self)
end;

procedure TCustomVirtualToolbar.DoCreateButtons;
begin
  if Assigned(OnCreateButtons) then
    OnCreateButtons(Self)
end;

{ TVirtualButtonList }

function TVirtualButtonList.AddButton(Index: integer = -1): TCustomWideSpeedButton;

// Creates a new Toobar Button and adds it to the list based on the Index parameter
// If -1 then the button is added to the end of the list.
// Note CreateToolButton may be overridden to create different TCustomWideSpeedButton decendants

begin
  Toolbar.BeginUpdate;
  Result := CreateToolButton;
  Result.Flat := toFlat in Toolbar.Options;
  Result.Transparent := toTransparent in Toolbar.Options;
  Result.ThemeAware := toThemeAware in Toolbar.Options;
  Result.HotAnimate := toAnimateHot in Toolbar.Options;
  Result.Layout := Toolbar.ButtonLayout;
  Result.Margin := Toolbar.ButtonMargin;
  Result.Spacing := Toolbar.ButtonSpacing;
  Result.OLEDraggable := toCustomizable in Toolbar.Options;

  case Index of
    -1: Add(Result);
  else
    Insert(Index, Result)
  end;
  Toolbar.EndUpdate;
end;

procedure TVirtualButtonList.BeginUpdate;

// Locks any screen updating during any button maniuplation

begin
  Inc(FUpdateCount);
end;

procedure TVirtualButtonList.Clear;

// Overridden to automaticlly free the contents of the List (TCustomWideSpeedButton or
// decendants)

var
  i: integer;
begin
  for i := 0 to Count - 1 do
  begin
    // During Design time the buttons are owned by the Toolbar so they can't
    // be selected in the IDE.  That means we must unassociate them from the
    // toolbar first.
    if csDesigning in Toolbar.ComponentState then
      Toolbar.RemoveComponent(Items[i]);
    Items[i].Free;
  end;
  inherited;
end;

function TVirtualButtonList.CreateToolButton: TCustomWideSpeedButton;

// Simply creates a toolbutton but it is a virtual method so decendants of TCustomWideSpeedButton
// can be created by deriving from TVirtualButtonList, overriding this method to create
// decendants of TCustomWideSpeedButton.

begin
  // During Design time the buttons are owned by the Toolbar so they can't be selected in the IDE
  if csDesigning in Toolbar.ComponentState then
    Result := TCustomWideSpeedButton.Create(Toolbar)  // This list owns the buttons not the parent
  else
    Result := TCustomWideSpeedButton.Create(nil);  // This list owns the buttons not the parent
  Result.Parent := Toolbar
end;

procedure TVirtualButtonList.EndUpdate;

// Rebuilds the Toolbar after FUpdateCount falls to 0 so Adding or Deleting buttons
// can be done without taking lenghtly breaks to update the screen after each

begin
  Dec(FUpdateCount);
  if FUpdateCount < 0 then
    FUpdateCount := 0;
  if (FUpdateCount = 0) and Assigned(Toolbar) then
    Toolbar.RebuildToolbar
end;

function TVirtualButtonList.GetItems(Index: integer): TCustomWideSpeedButton;

// Override of TList to return a TCustomWideSpeedButton type

begin
  Result := TCustomWideSpeedButton( inherited Items[Index]);
end;

procedure TVirtualButtonList.RemoveButton(Button: TCustomWideSpeedButton);
begin
  if IndexOf(Button) <> -1 then
  begin
    Button.Free;
    Remove(Button)
  end
end;

procedure TVirtualButtonList.SetItems(Index: integer; const Value: TCustomWideSpeedButton);

// Override of TList to set a TCustomWideSpeedButton type (not necessary but good practice
// in overriding properties)

begin
  inherited Items[Index] := Value
end;


{ TVirtualShellButtonList }

function TVirtualShellButtonList.AddButton(Index: integer): TCustomWideSpeedButton;
begin
  Toolbar.BeginUpdate;
  Result := inherited AddButton(Index);
  (Result as TShellToolButton).CaptionOptions := (Toolbar as TCustomVirtualShellToolbar).ButtonCaptionOptions;
  Toolbar.EndUpdate;
end;

function TVirtualShellButtonList.CreateToolButton: TCustomWideSpeedButton;

// Override of TVirtualButtonList to create a TCustomWideSpeedButton decendant.  Now calls to
// AddButton in a TVirtualShellButtonList object will create a TShellToolButton

begin
  // During Design time the buttons are owned by the Toolbar so they can't be selected in the IDE
  if csDesigning in Toolbar.ComponentState then
    Result := TShellToolButton.Create(Toolbar)  // This list owns the buttons not the parent
  else
    Result := TShellToolButton.Create(nil); // This list owns the buttons not the parent
  Result.Parent := Toolbar;
end;

function TVirtualShellButtonList.GetItems(Index: integer): TShellToolButton;

// Override of TVirtualButtonList to return a TCustomWideSpeedButton type decendant, TShellToolButton

begin
  Result := TShellToolButton( inherited Items[Index])
end;

procedure TVirtualShellButtonList.SetItems(Index: integer; const Value: TShellToolButton);

// Override of TVirtualButtonList to set a TShellToolButton type (not necessary but
// good practice in overriding properties)

begin
  inherited Items[Index] := Value
end;

{ TCaptionButton }

function TCaptionButton.CalcMaxExtentRect(Font: TFont): TRect;

// Calculates the size of the rectangle that is necessary to completely display
// the Caption.  The BorderMargin is an aritifical number just so the text does
// not touch the adjust button edge

const
  BorderMargin = 4;
var
  Size: TSize;
begin
  if Caption <> '' then
  begin
    Size := TextExtentW(PWideChar(Caption), Font);
    SetRect(Result, 0, 0, Size.cx + BorderMargin, Size.cy + BorderMargin);
  end else
    SetRect(Result, 0, 0, 0, 0)
end;

function TCaptionButton.CanAutoSize(var NewWidth,
  NewHeight: Integer): Boolean;
begin
  Result := inherited CanAutoSize(NewWidth, NewHeight);
end;

constructor TCaptionButton.Create(AOwner: TComponent);
begin
  inherited;
  Constraints.MinHeight := 0;
  Constraints.MinWidth := 0;
  AutoSize := True;
  Align := alLeft;
  Transparent := True
end;

procedure TCaptionButton.PaintButton(DC: HDC; ForDragImage: Boolean = False);

// Overriden method to paint the static text "button"

var
  BiDiFlags: Longword;
  TextBounds, R: TRect;
  CaptionANSI: string;
  OldMode: integer;
  PartType, PartState, dwTextFlags1, dwTextFlags2: Longword;
begin
  if Caption <> '' then
  begin
    BiDiFlags := DrawTextBiDiModeFlags(0);

    if ThemeAware and (bsThemesActive in State) then
    begin
      R := ClientRect;
      PartType := TP_BUTTON;
      PartState := TS_NORMAL;
      DrawThemeBackground(ThemeToolbar, DC, PartType, PartState, R, nil);
      GetThemeBackgroundContentRect(ThemeToolbar, DC, PartType, PartState, R, @R);

      dwTextFlags1 := DT_CENTER or DT_SINGLELINE or DT_VCENTER;
      dwTextFlags2 := 0;
      DrawThemeText(ThemeToolbar, DC, PartType, PartState, PWideChar(Caption),
        Length(Caption), dwTextFlags1 or BiDiFlags, dwTextFlags2, R);
    end else
    begin
      TextBounds := ClientRect;
      OldMode := SetBkMode(DC, Windows.TRANSPARENT);
      InflateRect(TextBounds, -2, -2);
      dwTextFlags1 := DT_CENTER or DT_SINGLELINE or DT_VCENTER;
      if IsUnicode then
        DrawTextW_VST(DC, PWideChar(Caption), Length(Caption), TextBounds,
          dwTextFlags1 or BiDiFlags)
      else begin
        CaptionANSI := Caption;
        DrawText(DC, PChar(CaptionANSI), Length(CaptionANSI), TextBounds,
          dwTextFlags1 or BiDiFlags)
      end;
      SetBkMode(DC, OldMode)
    end
  end
end;

{ TCustomVirtualDriveToolbar }

constructor TCustomVirtualDriveToolbar.Create(AOwner: TComponent);
begin
  inherited;
  ControlState := ControlState + [csCreating];
  FDriveSpecialFolders := [dsfRemovable, dsfReadOnly, dsfFixedDrive];
  ControlState := ControlState - [csCreating]
end;

procedure TCustomVirtualDriveToolbar.CreateButtons;
var
  Desktop, Folder: IShellFolder;
  EnumIDList: IEnumIDList;
  PIDL, SubPIDL, TempPIDL: PItemIDList;
  celtFetched: LongWord;
  Button: TShellToolButton;
  NS: TNamespace;
  IsFloppy, IsReadOnly, Allow: boolean;
begin
  BeginUpdate;
  try
    ButtonList.Clear;
    SHGetDesktopFolder(Desktop);
    if dsfDesktop in SpecialDriveFolders then
    begin
      Button := TShellToolButton( ButtonList.AddButton);
      Button.Namespace := TNamespace.Create(nil, nil);
      DoAddButton(Button.Namespace, Allow);
      if not Allow then
        ButtonList.RemoveButton(Button);
    end;

    // Always get this PIDL and keep it for enumeration later
    SHGetSpecialFolderLocation(0, CSIDL_DRIVES, PIDL);
    if (dsfMyComputer in SpecialDriveFolders) and Assigned(PIDL) then
    begin
      Button := TShellToolButton( ButtonList.AddButton);
      Button.Namespace := TNamespace.Create(PIDLMgr.CopyPIDL(PIDL), nil);
      Button.CaptionOptions := ButtonCaptionOptions;
      DoAddButton(Button.Namespace, Allow);
      if not Allow then
        ButtonList.RemoveButton(Button);
    end;

    if dsfNetworkNeighborhood in SpecialDriveFolders then
    begin
      SHGetSpecialFolderLocation(0, CSIDL_NETWORK, TempPIDL);
      if Assigned(TempPIDL) then
      begin
        Button := TShellToolButton( ButtonList.AddButton);
        Button.Namespace := TNamespace.Create(TempPIDL, nil);
        DoAddButton(Button.Namespace, Allow);
        if not Allow then
          ButtonList.RemoveButton(Button);
      end;
    end;

    if Assigned(PIDL) then
    begin
      if Desktop.BindToObject(PIDL, nil, IShellFolder, Pointer(Folder)) = S_OK then
      begin
        if Folder.EnumObjects(0, 0, EnumIDList) = NOERROR then
        begin
          while EnumIDList.Next(1, SubPIDL, celtFetched) = NOERROR do
          begin
            NS := TNamespace.Create(PIDLMgr.AppendPIDL(PIDL, SubPIDL), nil);

            IsFloppy := IsDriveW(NS.NameForParsing) and (Char(NS.NameForParsing[1]) in ['A', 'B']);
            IsReadOnly := (not IsFloppy) and NS.ReadOnly;
            if ((dsfRemovable in SpecialDriveFolders) and NS.Removable) or
               ((dsfReadOnly in SpecialDriveFolders) and IsReadOnly) or
               ((dsfFixedDrive in SpecialDriveFolders) and ((not IsReadOnly) and (not NS.Removable))) then
            begin
              Button := TShellToolButton( ButtonList.AddButton);
              Button.Namespace := NS;
              Button.Hint := Button.Namespace.NameParseAddress;
              DoAddButton(Button.Namespace, Allow);
              if not Allow then
                ButtonList.RemoveButton(Button);
            end else
              NS.Free;
            CoTaskMemFree(SubPIDL);
          end
        end
      end;
      CoTaskMemFree(PIDL);
    end;
  finally
    EndUpdate;
    DoCreateButtons
  end
end;

function TCustomVirtualDriveToolbar.GetOptions: TVirtualToolbarOptions;
begin
  Result := inherited Options
end;

procedure TCustomVirtualDriveToolbar.SetOptions(const Value: TVirtualToolbarOptions);
var
  Temp: TVirtualToolbarOptions;
begin
  Temp := Value;
  // Can't allow these two options to occur with the Shell Buttons
  if Temp * [toCustomizable, toInsertDropable] <> [] then
    Beep;
  Temp := Temp - [toCustomizable, toInsertDropable];
  inherited Options := Temp;
end;

procedure TCustomVirtualDriveToolbar.SetSpecialDriveFolders(const Value: TDriveSpecialFolders);
begin
  if FDriveSpecialFolders <> Value then
  begin
    FDriveSpecialFolders := Value;
    CreateButtons;
  end
end;

procedure TCustomVirtualDriveToolbar.WMRemoveButton(var Message: TMessage);
begin
  // Just eat this we can't remove button from this toolbar
end;

{ TShellToolButton }

procedure TShellToolButton.Click;
begin
  AddToUpdateRgn;
  Update;
  if Assigned(Namespace) then
    if Assigned((Parent as TCustomVirtualShellToolbar).VirtualExplorerTree) and Namespace.Folder then
      (Parent as TCustomVirtualShellToolbar).ChangeLinkDispatch(Namespace.AbsolutePIDL)
    else
    if not (toUserDefinedClickAction in (Parent as TCustomVirtualToolbar).Options) then
      Namespace.ShellExecuteNamespace('', '', True);
  inherited;
end;

constructor TShellToolButton.Create(AOwner: TComponent);
begin
  inherited;
  Margin := 2;
  AutoSize := True;
  FImageList := SmallSysImages;
end;

destructor TShellToolButton.Destroy;
begin
  Namespace.Free;
  inherited;
end;

function TShellToolButton.DragEnter(const dataObj: IDataObject;
  grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
begin
  inherited DragEnter(dataObj, grfKeyState, pt, dwEffect);
  if Assigned(Namespace) then
    Result := Namespace.DragEnter(dataObj, grfKeyState, pt, dwEffect)
  else
    Result := S_OK
end;

function TShellToolButton.DragLeave: HResult;
begin
  inherited DragLeave;
  if Assigned(Namespace) then
    Result := Namespace.DragLeave
  else
    Result := S_OK
end;

function TShellToolButton.DragOverOLE(grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
begin
  inherited DragOverOLE(grfKeyState, pt, dwEffect);
  if Assigned(Namespace) then
    Result := Namespace.DragOver(grfKeyState, pt, dwEffect)
  else
    Result := S_OK
end;

function TShellToolButton.Drop(const dataObj: IDataObject;
  grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
begin
  inherited Drop(dataObj, grfKeyState, pt, dwEffect);
  if Assigned(Namespace) then
    Result := Namespace.Drop(dataObj, grfKeyState, pt, dwEffect)
  else
    Result := S_OK
end;

function TShellToolButton.GetCaption: WideString;

// Overrides the Caption property getter to automaticlly retrieve the name of the
// Shell Object from the Namespace property.  To refresh the Caption set it to an
// empty string '' and the next call to retrieve it will force a reload from the
// namespace

begin
  if (FCaption = '') and Assigned(Namespace) then
  begin
    if coFolderName in CaptionOptions then
      FCaption := Namespace.NameNormal
    else
    if coFolderPath in CaptionOptions then
      FCaption := Namespace.NameParseAddress
    else
    if coDriveLetterOnly in CaptionOptions then
    begin
      if (Namespace.FileSystem and IsDriveW(Namespace.NameParseAddress)) then
        FCaption := Namespace.NameParseAddress[1]
      else
      if Namespace.IsNetworkNeighborhood then
        FCaption := '\'
      else
        FCaption := '';
    end;

    if (coNoExtension in CaptionOptions) and (Length(FCaption) > 0) and not (coDriveLetterOnly in CaptionOptions) then
      StripExtW(FCaption);


    Hint := Namespace.NameParseAddress;
  end;
  Result := FCaption
end;

function TShellToolButton.GetImageIndex: integer;

// Overrides the ImageIndex property getter to automaticlly retrieve the SystemImageList
// index of the Shell Object from the Namespace property.  To refresh the ImageIndex set
// it to -1 and the next call to retrieve it will force a reload from the namespace
begin
  {$IFDEF THREADEDICONS}
  if Parent is TCustomVirtualShellToolbar then
  begin
    if TCustomVirtualShellToolbar(Parent).ThreadedImagesEnabled then
    begin
      if not Namespace.ThreadedIconLoaded then
      begin
        if not Namespace.ThreadIconLoading then
        begin
          Namespace.ThreadIconLoading := True;
          ImageThreadManager.AddNewItem(TCustomVirtualShellToolbar(Parent),
            WM_VTSETICONINDEX, Namespace.AbsolutePIDL, True, Self, 0);
          Result := 0
        end else
          Result := 0
      end else
        Result := Namespace.GetIconIndex(False, icSmall, True);
    end else
    begin
      // Not using threaded images
      Result := Namespace.GetIconIndex(False, icSmall, True);
    end
  end else
    Result := Namespace.GetIconIndex(False, icSmall, True);
  {$ELSE}
    Result := Namespace.GetIconIndex(False, icSmall, True);
  {$ENDIF}
end;

procedure TShellToolButton.LoadFromStream(S: TStream);
begin
  inherited;
  if Assigned(Namespace) then
    FreeAndNil(FNamespace);
  Namespace := TNamespace.Create(PIDLMgr.LoadFromStream(S), nil)
end;

function TShellToolButton.SaveToDataObject(const DataObject: IVirtualDataObject): Boolean;
var
  VSTShellToolbar: TVSTShellToolbar;
begin
  VSTShellToolbar := TVSTShellToolbar.Create;
  try
    VSTShellToolbar.PIDL := Namespace.AbsolutePIDL;
    Result := VSTShellToolbar.SaveToDataObject(DataObject);
  finally
    VSTShellToolbar.Free
  end;
end;

procedure TShellToolButton.SaveToStream(S: TStream);
begin
  inherited;
  if Assigned(Namespace) then
    PIDLMgr.SaveToStream(S, Namespace.AbsolutePIDL);
end;

procedure TShellToolButton.SetCaptionOptions(const Value: TCaptionOptions);

    function BitChanged(A, B: TCaptionOptions; Bit: TCaptionOption): Boolean;
    begin
      Result := ((Bit in A) and not(Bit in B)) or ((Bit in B) and not(Bit in A))
    end;

var
  Temp: TCaptionOptions;
begin
  if FCaptionOptions <> Value then
  begin
    Temp := Value;
    if BitChanged(Value, FCaptionOptions, coFolderName) then
      Temp := Temp - [coFolderPath, coDriveLetterOnly]
    else
    if BitChanged(Value, FCaptionOptions, coFolderPath) then
      Temp := Temp - [coFolderName, coDriveLetterOnly]
    else
    if BitChanged(Value, FCaptionOptions, coDriveLetterOnly) then
      Temp := Temp - [coFolderPath, coFolderName];

    FCaptionOptions := Temp;
    // Force the caption to be reread
    FCaption := '';
    RebuildButton
  end
end;

procedure TShellToolButton.WMContextMenu(var Message: TWMContextMenu);
begin
  if Assigned(Namespace) and (toContextMenu in (Parent as TCustomVirtualToolbar).Options) then
    Namespace.ShowContextMenu(Parent, nil, nil, nil);
end;

{ TCustomVirtualShellToolbar }

procedure TCustomVirtualShellToolbar.ChangeLinkDispatch(PIDL: PItemIDList);
begin
  VETChangeDispatch.DispatchChange(Self, PIDL);
end;

procedure TCustomVirtualShellToolbar.ChangeLinkFreeing(ChangeLink: IVETChangeLink);
begin
  if ChangeLink.ChangeLinkServer = Self then
  begin
    if ChangeLink.ChangeLinkClient = FVirtualExplorerTree then
      FVirtualExplorerTree := nil;
  end
end;

function TCustomVirtualShellToolbar.CreateButtonList: TVirtualButtonList;
begin
  Result := TVirtualShellButtonList.Create
end;

procedure TCustomVirtualShellToolbar.DoAddButton(Namespace: TNamespace;
  var Allow: Boolean);
begin
  Allow := True;
  if Assigned(OnAddButton) then
    OnAddButton(Self, Namespace, Allow)
end;

procedure TCustomVirtualShellToolbar.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  {$IFDEF EXPLORERCOMBOBOX}
  if (AComponent = FVirtualExplorerComboBox) and (Operation = opRemove) then
    VirtualExplorerComboBox := nil;
  {$ENDIF}
  if (AComponent = FVirtualExplorerTree) and (Operation = opRemove) then
    VirtualExplorerTree := nil;
end;

procedure TCustomVirtualShellToolbar.SetButtonCaptionOptions(
  const Value: TCaptionOptions);
var
  i: integer;
begin
  if FButtonCaptionOptions <> Value then
  begin
    FButtonCaptionOptions := Value;
    for i := 0 to ButtonList.Count - 1 do
    begin
      TShellToolButton(ButtonList[i]).CaptionOptions := Value;
      // Some choices are mutually exclusive so mirror what the button does
      FButtonCaptionOptions := TShellToolButton(ButtonList[i]).CaptionOptions;
    end
  end;
  RebuildToolbar
end;

{$IFDEF EXPLORERCOMBOBOX}
procedure TCustomVirtualShellToolbar.SetVirtualExplorerComboBox(
  const Value: TCustomVirtualExplorerComboBox);
begin
  if FVirtualExplorerComboBox <> Value then
  begin
    { We need to set us up as the server for Toolbars so this is a bit backwards }
    { from other VSTools components                                              }
    if Assigned(FVirtualExplorerComboBox) then
      VETChangeDispatch.UnRegisterChangeLink(Self, FVirtualExplorerComboBox, utLink );
    FVirtualExplorerComboBox := Value;
    if Assigned(FVirtualExplorerComboBox) then
      VETChangeDispatch.RegisterChangeLink(Self, FVirtualExplorerComboBox,
        FVirtualExplorerComboBox.ChangeLinkChanging, ChangeLinkFreeing);
  end
end;
{$ENDIF}

procedure TCustomVirtualShellToolbar.SetVirtualExplorerTree(
  const Value: TCustomVirtualExplorerTree);
begin
  if FVirtualExplorerTree <> Value then
  begin
    { We need to set us up as the server for Toolbars so this is a bit backwards }
    { from other VSTools components                                              }
    if Assigned(FVirtualExplorerTree) then
      VETChangeDispatch.UnRegisterChangeLink(Self, FVirtualExplorerTree, utLink );
    FVirtualExplorerTree := Value;
    if Assigned(FVirtualExplorerTree) then
      VETChangeDispatch.RegisterChangeLink(Self, FVirtualExplorerTree,
        FVirtualExplorerTree.ChangeLinkChanging, ChangeLinkFreeing);
  end
end;

{$IFDEF SHELLNOTIFIER}

procedure TCustomVirtualShellToolbar.WMShellNotify(var Msg: TMessage);
var
  Count: Integer;
  ShellEventList: TVirtualShellEventList;
  ShellEvent: TVirtualShellEvent;
  i: integer;
  List: TList;
  ReCreatedOnce: Boolean;
  NS: TNamespace;
  PIDL: PItemIDList;
  Flags: Longword;
begin
  BeginUpdate;
  try
    ReCreatedOnce := False;
    ShellEventList := TVirtualShellEventList( Msg.wParam);
    List := ShellEventList.LockList;
    try
      Count := List.Count;
      for i := 0 to Count - 1 do
      begin
        ShellEvent := TVirtualShellEvent(List.Items[i]);
        case ShellEvent.ShellNotifyEvent of
          // This group of notifications requires a reread of the parent folder.
          // They may have been the result of an item creation or deletion.
          // The NotifyThread removes redundant calls to these items so VET does not
          // need to respond to 5000 notifications if 5000 files are deleted.

          // Commented out events don't have any bearing on the toolbars state

     //     sneCreate,          // Creating a File
     //     sneDelete,          // Deleting a File
          vsneDriveAdd,          // Mapping a network drive
          vsneDriveAddGUI,       // CD inserted shell should create new window
          vsneDriveRemoved,      // UnMapping a network drive
     //     sneMkDir,           // Creating a Directory
     //     sneRmDir,           // Deleting a Directory
          vsneUpdateDir:       // Create or Delete File/Directory overload
            begin
              // WinVirtual sends a sneUpdateDir to remove a mapped Drive ???? Weird
              if not ReCreatedOnce then
              begin
                ReCreateButtons;
                ReCreatedOnce := True
              end
            end;
          // This group of notifications is special in that both PIDLs are used.  if
          // they have the same parent folder it is a rename, if not it is a move.
          // NT4 calls a deletion to the RecycleBin a move while Win2k calls it a
          // delete so the meaning varies depending on platform.
          vsneRenameFolder,  // Folder renamed or Moved; depends on Win version
          vsneRenameItem:    // File renamed or Moved; depends on Win version
            begin
            end;
          // This notification is sent when a namespace has been mapped to a
          // different image.
          vsneUpdateImage:   // New image has been mapped to the item
            begin
              FlushImageLists;
              if not ReCreatedOnce then
              begin
                ReCreateButtons;
                ReCreatedOnce := True
              end
            end;
          // This group of notifications is based on an existing namespace that has
          // had its properties changed.  As such the PIDL must be refreshed to read
          // in the new properties stored in the PIDL.
      //    sneAttributes,       // Printer properties changed and ???
          vsneMediaInserted,      // New CD inserted.
          vsneMediaRemoved,       // CD removed
          vsneNetShare,           // Folder being shared or unshared
          vsneNetUnShare,         // ?? Should be the opposite of NetShare
          vsneServerDisconnect:
       //   sneUpdateItem:       // Properties of file OR dir changed
            begin
              // M$ Hack to get Win9x to change the image and name of removable
              // drives when the media changes
              NS := TNamespace.Create(ShellEvent.PIDL1, nil);
              NS.FreePIDLOnDestroy := False;
              PIDL := NS.RelativePIDL;
              Flags := SFGAO_VALIDATE;
              NS.ParentShellFolder.GetAttributesOf(0, PIDL, Flags);
              NS.Free;
              if not ReCreatedOnce then
              begin
                ReCreateButtons;
                ReCreatedOnce := True
              end
            end;
          // This notification is sent when the freespace on a drive has changed.
          // for now it appears the only thing this my impact is the disk size
          // details under MyComputer.  Refresh if necessary.
          vsneFreeSpace:
            begin
            end;
          // This notification is sent when the shell has changed an assocciation of
          // file type was involved.
          vsneAssoccChanged:
            begin
              FlushImageLists;
              if not ReCreatedOnce then
              begin
                ReCreateButtons;
                ReCreatedOnce := True
              end
            end;
        end
      end;
    finally
      ShellEventList.UnlockList;
      ShellEventList.Release;
    end
  finally
    EndUpdate
  end
end;

{$ENDIF}


{ TCustomSpecialFolderToolbar }

procedure TCustomVirtualSpecialFolderToolbar.CreateButtons;

  procedure CreateSpecialButton(CSIDL_BUTTON: Longword);
  var
    PIDL: PItemIDList;
    Button: TShellToolButton;
    Allow: Boolean;
  begin
    SHGetSpecialFolderLocation(0, CSIDL_BUTTON, PIDL);
    if Assigned(PIDL) then
    begin
      Button := TShellToolButton( ButtonList.AddButton);
      Button.Namespace := TNamespace.Create(PIDL, nil);
      Button.CaptionOptions := ButtonCaptionOptions;
      if toLargeButtons in FOptions then
        Button.ImageList := LargeSysImages
      else
        Button.ImageList := SmallSysImages;
      DoAddButton(Button.Namespace, Allow);
      if not Allow then
        ButtonList.RemoveButton(Button);
    end
  end;

begin
  BeginUpdate;
  try
    ButtonList.Clear;
    if sfAdminTools in SpecialFolders then
      CreateSpecialButton(CSIDL_ADMINTOOLS);
    if sfAltStartup in SpecialFolders then
      CreateSpecialButton(CSIDL_ALTSTARTUP);
    if sfAppData in SpecialFolders then
      CreateSpecialButton(CSIDL_APPDATA);
    if sfBitBucket in SpecialFolders then
      CreateSpecialButton(CSIDL_BITBUCKET);
    if sfCommonAdminTools in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_ADMINTOOLS);
    if sfCommonAltStartup in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_ALTSTARTUP);
    if sfCommonAppData in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_APPDATA);
    if sfCommonDesktopDirectory in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_DESKTOPDIRECTORY);
    if sfCommonDocuments in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_DOCUMENTS);
    if sfCommonFavorties in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_FAVORITES);
    if sfCommonPrograms in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_PROGRAMS);
    if sfCommonStartMenu in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_STARTMENU);
    if sfCommonStartup in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_STARTUP);
    if sfCommonTemplates in SpecialCommonFolders then
      CreateSpecialButton(CSIDL_COMMON_TEMPLATES);
    if sfControlPanel in SpecialFolders then
      CreateSpecialButton(CSIDL_CONTROLS);
    if sfCookies in SpecialFolders then
      CreateSpecialButton(CSIDL_COOKIES);
    if sfDesktop in SpecialFolders then
      CreateSpecialButton(CSIDL_DESKTOP);
    if sfDesktopDirectory in SpecialFolders then
      CreateSpecialButton(CSIDL_DESKTOPDIRECTORY);
    if sfDrives in SpecialFolders then
      CreateSpecialButton(CSIDL_DRIVES);
    if sfFavorites in SpecialFolders then
      CreateSpecialButton(CSIDL_FAVORITES);
    if sfFonts in SpecialFolders then
      CreateSpecialButton(CSIDL_FONTS);
    if sfHistory in SpecialFolders then
      CreateSpecialButton(CSIDL_HISTORY);
    if sfInternet in SpecialFolders then
      CreateSpecialButton(CSIDL_INTERNET);
    if sfInternetCache in SpecialFolders then
      CreateSpecialButton(CSIDL_INTERNET_CACHE);
    if sfLocalAppData in SpecialFolders then
      CreateSpecialButton(CSIDL_LOCAL_APPDATA);
    if sfMyPictures in SpecialFolders then
      CreateSpecialButton(CSIDL_MYPICTURES);
    if sfNetHood in SpecialFolders then
      CreateSpecialButton(CSIDL_NETHOOD);
    if sfNetwork in SpecialFolders then
      CreateSpecialButton(CSIDL_NETWORK);
    if sfPersonal in SpecialFolders then
      CreateSpecialButton(CSIDL_PERSONAL);
    if sfPrinters in SpecialFolders then
      CreateSpecialButton(CSIDL_PRINTERS);
    if sfPrintHood in SpecialFolders then
      CreateSpecialButton(CSIDL_PRINTHOOD);
    if sfProfile in SpecialFolders then
      CreateSpecialButton(CSIDL_PROFILE);
    if sfProgramFiles in SpecialFolders then
      CreateSpecialButton(CSIDL_PROGRAM_FILES);
    if sfCommonProgramFiles in SpecialFolders then
      CreateSpecialButton(CSIDL_PROGRAM_FILES_COMMON);
    if sfPrograms in SpecialFolders then
      CreateSpecialButton(CSIDL_PROGRAMS);
    if sfRecent in SpecialFolders then
      CreateSpecialButton(CSIDL_RECENT);
    if sfSendTo in SpecialFolders then
      CreateSpecialButton(CSIDL_SENDTO);
    if sfStartMenu in SpecialFolders then
      CreateSpecialButton(CSIDL_STARTMENU);
    if sfStartUp in SpecialFolders then
      CreateSpecialButton(CSIDL_STARTUP);
    if sfSystem in SpecialFolders then
      CreateSpecialButton(CSIDL_SYSTEM);
    if sfTemplate in SpecialFolders then
      CreateSpecialButton(CSIDL_TEMPLATES);
    if sfWindows in SpecialFolders then
      CreateSpecialButton(CSIDL_WINDOWS);
  finally
    EndUpdate
  end;
  DoCreateButtons;
end;

procedure TCustomVirtualSpecialFolderToolbar.SetSpecialCommonFolders(
  const Value: TSpecialCommonFolders);
begin
  if FSpecialCommonFolders <> Value then
  begin
    FSpecialCommonFolders := Value;
    // Get too much recreating of buttons and there is no point since the button will be created
    // when the window is created
    if not(csLoading in ComponentState) then
      CreateButtons;
//    if csDesigning in ComponentState then
      RebuildToolbar
  end
end;

procedure TCustomVirtualSpecialFolderToolbar.SetSpecialFolders(
  const Value: TSpecialFolders);
begin
  if FSpecialFolders <> Value then
  begin
    FSpecialFolders := Value;
    // Get too much recreating of buttons and there is no point since the button will be created
    // when the window is created
    if not(csLoading in ComponentState) then
      CreateButtons;
 //   if csDesigning in ComponentState then
      RebuildToolbar
  end
end;

{$IFDEF THREADEDICONS}
procedure TCustomVirtualShellToolbar.WMVTSetIconIndex(
  var Msg: TWMVTSetIconIndex);
var
  i: integer;
  Found: Boolean;
begin
  if Assigned(Msg.IconInfo) then
  begin
    i := 0;
    Found := False;
    while not Found and (i < ButtonList.Count) do
    begin
      if ButtonList[i] = Msg.IconInfo.UserData then
      begin
        TShellToolButton(ButtonList[i]).Namespace.SetIconIndexByThread(Msg.IconInfo.IconIndex, True);
        ImageThreadManager.ReleaseItem(Msg.IconInfo, Malloc);
        TShellToolButton(ButtonList[i]).Invalidate;
        Found := True
      end;
      Inc(i)
    end
  end
end;
{$ENDIF}

{ TVSTShellToolbar }

function TVSTShellToolbar.GetFormatEtc: TFormatEtc;
begin
  Result.cfFormat := CF_VSTSHELLTOOLBAR;
  Result.ptd := nil;
  Result.dwAspect := DVASPECT_CONTENT;
  Result.lindex := -1;
  Result.tymed := TYMED_ISTREAM;
end;

function TVSTShellToolbar.GetPIDLSize: integer;
begin
  Result := PIDLMgr.PIDLSize(FPIDL)
end;

function TVSTShellToolbar.LoadFromDataObject(DataObject: IDataObject): Boolean;
var
  StgMedium: TStgMedium;
  Stream: IStream;
  BytesRead: LongInt;
  NewPos: Largeint;
  LocalPIDLSize: integer;
  Malloc: IMalloc;
begin
  Result := False;
  if Succeeded(DataObject.QueryGetData(GetFormatEtc)) then
    if Succeeded(DataObject.GetData(GetFormatEtc, StgMedium)) then
    begin
      Stream := IStream( StgMedium.stm);
      Stream.Seek(0, STREAM_SEEK_SET, NewPos);
      Stream.read(@FProcess, SizeOf(Process), @BytesRead);
      Stream.read(@LocalPIDLSize, SizeOf(LocalPIDLSize), @BytesRead);
      SHGetMalloc(Malloc);
      PIDL := Malloc.Alloc(LocalPIDLSize);
      Stream.read(@PIDL^, LocalPIDLSize, @BytesRead);
      Result := True;
    end
end;

function TVSTShellToolbar.SaveToDataObject(DataObject: IDataObject): Boolean;
var
  StgMedium: TStgMedium;
  hMem: THandle;
  Stream: IStream;
  NewPos: Largeint;
  Int: integer;
  BytesWritten: LongInt;
begin
  Result := True;
  FillChar(StgMedium, SizeOf(TStgMedium), #0);

  StgMedium.tymed := TYMED_ISTREAM;
  hMem := GlobalAlloc(GMEM_MOVEABLE or GMEM_SHARE or GMEM_ZEROINIT, 0);
  if hMem <> 0 then
    if Succeeded(CreateStreamOnHGlobal(hMem, True, IStream( StgMedium.stm))) then
    begin
      Stream := IStream( StgMedium.stm);
      Stream.Seek(0, STREAM_SEEK_SET, NewPos);

      Int := GetCurrentProcess;
      Stream.write(@Int, SizeOf(Int), @BytesWritten);
      Int := PIDLSize;
      Stream.write(@Int, SizeOf(Int), @BytesWritten);
      Stream.write(@FPIDL^, PIDLSize, @BytesWritten);

      // Give it to the data object
      Result := Succeeded(DataObject.SetData(GetFormatEtc, StgMedium, True))
    end
end;

initialization
  CF_VSTSHELLTOOLBAR := RegisterClipboardFormat(CFSTR_VSTSHELLTOOLBAR);

end.