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
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "chrome/views/chrome_menu.h"
#include <windows.h>
#include <uxtheme.h>
#include <Vssym32.h>
#include "base/base_drag_source.h"
#include "base/gfx/native_theme.h"
#include "base/gfx/skia_utils.h"
#include "base/message_loop.h"
#include "base/task.h"
#include "base/timer.h"
#include "base/win_util.h"
#include "chrome/browser/drag_utils.h"
#include "chrome/common/gfx/chrome_canvas.h"
#include "chrome/common/gfx/color_utils.h"
#include "chrome/common/l10n_util.h"
#include "chrome/common/os_exchange_data.h"
#include "chrome/views/border.h"
#include "chrome/views/hwnd_view_container.h"
#include "generated_resources.h"
// Margins between the top of the item and the label.
static const int kItemTopMargin = 3;
// Margins between the bottom of the item and the label.
static const int kItemBottomMargin = 4;
// Margins between the left of the item and the icon.
static const int kItemLeftMargin = 4;
// Padding between the label and submenu arrow.
static const int kLabelToArrowPadding = 10;
// Padding between the arrow and the edge.
static const int kArrowToEdgePadding = 5;
// Padding between the icon and label.
static const int kIconToLabelPadding = 8;
// Padding between the gutter and label.
static const int kGutterToLabel = 5;
// Height of the scroll arrow.
// This goes up to 4 with large fonts, but this is close enough for now.
static const int kScrollArrowHeight = 3;
// Size of the check. This comes from the OS.
static int check_width;
static int check_height;
// Size of the submenu arrow. This comes from the OS.
static int arrow_width;
static int arrow_height;
// Width of the gutter. Only used if render_gutter is true.
static int gutter_width;
// Margins between the right of the item and the label.
static int item_right_margin;
// X-coordinate of where the label starts.
static int label_start;
// Height of the separator.
static int separator_height;
// Padding around the edges of the submenu.
static const int kSubmenuBorderSize = 3;
// Amount to inset submenus.
static const int kSubmenuHorizontalInset = 3;
// Delay, in ms, between when menus are selected are moused over and the menu
// appears.
static const int kShowDelay = 400;
// Amount of time from when the drop exits the menu and the menu is hidden.
static const int kCloseOnExitTime = 1200;
// Height of the drop indicator. This should be an event number.
static const int kDropIndicatorHeight = 2;
// Color of the drop indicator.
static const SkColor kDropIndicatorColor = SK_ColorBLACK;
// Whether or not the gutter should be rendered. The gutter is specific to
// Vista.
static bool render_gutter = false;
// Max width of a menu. There does not appear to be an OS value for this, yet
// both IE and FF restrict the max width of a menu.
static const LONG kMaxMenuWidth = 400;
// Period of the scroll timer (in milliseconds).
static const int kScrollTimerMS = 30;
// Preferred height of menu items. Reset every time a menu is run.
static int pref_menu_height;
using gfx::NativeTheme;
namespace ChromeViews {
// Calculates all sizes that we can from the OS.
//
// This is invoked prior to Running a menu.
void UpdateMenuPartSizes() {
HDC dc = GetDC(NULL);
RECT bounds = { 0, 0, 200, 200 };
SIZE check_size;
if (NativeTheme::instance()->GetThemePartSize(
NativeTheme::MENU, dc, MENU_POPUPCHECK, MC_CHECKMARKNORMAL, &bounds,
TS_TRUE, &check_size) == S_OK) {
check_width = check_size.cx;
check_height = check_size.cy;
} else {
check_width = GetSystemMetrics(SM_CXMENUCHECK);
check_height = GetSystemMetrics(SM_CYMENUCHECK);
}
SIZE arrow_size;
if (NativeTheme::instance()->GetThemePartSize(
NativeTheme::MENU, dc, MENU_POPUPSUBMENU, MSM_NORMAL, &bounds,
TS_TRUE, &arrow_size) == S_OK) {
arrow_width = arrow_size.cx;
arrow_height = arrow_size.cy;
} else {
// Sadly I didn't see a specify metrics for this.
arrow_width = GetSystemMetrics(SM_CXMENUCHECK);
arrow_height = GetSystemMetrics(SM_CYMENUCHECK);
}
SIZE gutter_size;
if (NativeTheme::instance()->GetThemePartSize(
NativeTheme::MENU, dc, MENU_POPUPGUTTER, MSM_NORMAL, &bounds,
TS_TRUE, &gutter_size) == S_OK) {
gutter_width = gutter_size.cx;
render_gutter = true;
} else {
gutter_width = 0;
render_gutter = false;
}
SIZE separator_size;
if (NativeTheme::instance()->GetThemePartSize(
NativeTheme::MENU, dc, MENU_POPUPSEPARATOR, MSM_NORMAL, &bounds,
TS_TRUE, &separator_size) == S_OK) {
separator_height = separator_size.cy;
} else {
separator_height = GetSystemMetrics(SM_CYMENU) / 2;
}
item_right_margin = kLabelToArrowPadding + arrow_width + kArrowToEdgePadding;
label_start = kItemLeftMargin + check_width + kIconToLabelPadding;
if (render_gutter)
label_start += gutter_width + kGutterToLabel;
ReleaseDC(NULL, dc);
CSize pref;
MenuItemView menu_item(NULL);
menu_item.SetTitle(L"blah"); // Text doesn't matter here.
menu_item.GetPreferredSize(&pref);
pref_menu_height = pref.cy;
}
namespace {
// Convenience for scrolling the view such that the origin is visible.
static void ScrollToVisible(View* view) {
view->ScrollRectToVisible(0, 0, view->GetWidth(), view->GetHeight());
}
// MenuScrollTask --------------------------------------------------------------
// MenuScrollTask is used when the SubmenuView does not all fit on screen and
// the mouse is over the scroll up/down buttons. MenuScrollTask schedules itself
// with the TimerManager. When Run is invoked MenuScrollTask scrolls
// appropriately.
class MenuScrollTask : public Task {
public:
MenuScrollTask() : submenu_(NULL) {
pixels_per_second_ = pref_menu_height * 20;
}
virtual ~MenuScrollTask() {
StopScrolling();
}
virtual void Run() {
DCHECK(submenu_);
gfx::Rect vis_rect = submenu_->GetVisibleBounds();
const int delta_y = static_cast<int>(
(Time::Now() - start_scroll_time_).InMilliseconds() *
pixels_per_second_ / 1000);
int target_y = start_y_;
if (is_scrolling_up_)
target_y = std::max(0, target_y - delta_y);
else
target_y = std::min(submenu_->GetHeight() - vis_rect.height(),
target_y + delta_y);
submenu_->ScrollRectToVisible(vis_rect.x(), target_y, vis_rect.width(),
vis_rect.height());
}
void Update(const MenuController::MenuPart& part) {
if (!part.is_scroll()) {
StopScrolling();
return;
}
DCHECK(part.submenu);
SubmenuView* new_menu = part.submenu;
bool new_is_up = (part.type == MenuController::MenuPart::SCROLL_UP);
if (new_menu == submenu_ && is_scrolling_up_ == new_is_up)
return;
start_scroll_time_ = Time::Now();
start_y_ = part.submenu->GetVisibleBounds().y();
submenu_ = new_menu;
is_scrolling_up_ = new_is_up;
if (!scrolling_timer_.get()) {
scrolling_timer_.reset(new Timer(kScrollTimerMS, this, true));
TimerManager* tm = MessageLoop::current()->timer_manager();
tm->StartTimer(scrolling_timer_.get());
}
}
void StopScrolling() {
if (scrolling_timer_.get()) {
TimerManager* tm = MessageLoop::current()->timer_manager();
tm->StopTimer(scrolling_timer_.get());
scrolling_timer_.reset(NULL);
submenu_ = NULL;
}
}
// The menu being scrolled. Returns null if not scrolling.
SubmenuView* submenu() const { return submenu_; }
private:
// SubmenuView being scrolled.
SubmenuView* submenu_;
// Direction scrolling.
bool is_scrolling_up_;
// Timer to periodically scroll.
scoped_ptr<Timer> scrolling_timer_;
// Time we started scrolling at.
Time start_scroll_time_;
// How many pixels to scroll per second.
int pixels_per_second_;
// Y-coordinate of submenu_view_ when scrolling started.
int start_y_;
DISALLOW_EVIL_CONSTRUCTORS(MenuScrollTask);
};
// MenuScrollButton ------------------------------------------------------------
// MenuScrollButton is used for the scroll buttons when not all menu items fit
// on screen. MenuScrollButton forwards appropriate events to the
// MenuController.
class MenuScrollButton : public View {
public:
explicit MenuScrollButton(SubmenuView* host, bool is_up)
: host_(host),
is_up_(is_up),
// Make our height the same as that of other MenuItemViews.
pref_height_(pref_menu_height) {
}
virtual void GetPreferredSize(CSize* out) {
out->cx = kScrollArrowHeight * 2 - 1;
out->cy = pref_height_;
}
virtual bool CanDrop(const OSExchangeData& data) {
DCHECK(host_->GetMenuItem()->GetMenuController());
return true; // Always return true so that drop events are targeted to us.
}
virtual void OnDragEntered(const DropTargetEvent& event) {
DCHECK(host_->GetMenuItem()->GetMenuController());
host_->GetMenuItem()->GetMenuController()->OnDragEnteredScrollButton(
host_, is_up_);
}
virtual int OnDragUpdated(const DropTargetEvent& event) {
return DragDropTypes::DRAG_NONE;
}
virtual void OnDragExited() {
DCHECK(host_->GetMenuItem()->GetMenuController());
host_->GetMenuItem()->GetMenuController()->OnDragExitedScrollButton(host_);
}
virtual int OnPerformDrop(const DropTargetEvent& event) {
return DragDropTypes::DRAG_NONE;
}
virtual void Paint(ChromeCanvas* canvas) {
HDC dc = canvas->beginPlatformPaint();
// The background.
RECT item_bounds = { 0, 0, GetWidth(), GetHeight() };
NativeTheme::instance()->PaintMenuItemBackground(
NativeTheme::MENU, dc, MENU_POPUPITEM, MPI_NORMAL, false,
&item_bounds);
// Then the arrow.
int x = GetWidth() / 2;
int y = (GetHeight() - kScrollArrowHeight) / 2;
int delta_y = 1;
if (!is_up_) {
delta_y = -1;
y += kScrollArrowHeight;
}
SkColor arrow_color = color_utils::GetSysSkColor(COLOR_MENUTEXT);
for (int i = 0; i < kScrollArrowHeight; ++i, --x, y += delta_y)
canvas->FillRectInt(arrow_color, x, y, (i * 2) + 1, 1);
canvas->endPlatformPaint();
}
private:
// SubmenuView we were created for.
SubmenuView* host_;
// Direction of the button.
bool is_up_;
// Preferred height.
int pref_height_;
DISALLOW_EVIL_CONSTRUCTORS(MenuScrollButton);
};
// MenuScrollView --------------------------------------------------------------
// MenuScrollView is a viewport for the SubmenuView. It's reason to exist is so
// that ScrollRectToVisible works.
//
// NOTE: It is possible to use ScrollView directly (after making it deal with
// null scrollbars), but clicking on a child of ScrollView forces the window to
// become active, which we don't want. As we really only need a fraction of
// what ScrollView does, we use a one off variant.
class MenuScrollView : public View {
public:
explicit MenuScrollView(View* child) {
AddChildView(child);
}
virtual void ScrollRectToVisible(int x, int y, int width, int height) {
// NOTE: this assumes we only want to scroll in the y direction.
View* child = GetContents();
// Convert y to view's coordinates.
y -= child->GetY();
CSize pref;
child->GetPreferredSize(&pref);
// Constrain y to make sure we don't show past the bottom of the view.
y = std::max(0, std::min(static_cast<int>(pref.cy) - GetHeight(), y));
child->SetY(-y);
}
// Returns the contents, which is the SubmenuView.
View* GetContents() {
return GetChildViewAt(0);
}
private:
DISALLOW_EVIL_CONSTRUCTORS(MenuScrollView);
};
// MenuScrollViewContainer -----------------------------------------------------
// MenuScrollViewContainer contains the SubmenuView (through a MenuScrollView)
// and two scroll buttons. The scroll buttons are only visible and enabled if
// the preferred height of the SubmenuView is bigger than our bounds.
class MenuScrollViewContainer : public View {
public:
explicit MenuScrollViewContainer(SubmenuView* content_view) {
scroll_up_button_ = new MenuScrollButton(content_view, true);
scroll_down_button_ = new MenuScrollButton(content_view, false);
AddChildView(scroll_up_button_);
AddChildView(scroll_down_button_);
scroll_view_ = new MenuScrollView(content_view);
AddChildView(scroll_view_);
SetBorder(
Border::CreateEmptyBorder(kSubmenuBorderSize, kSubmenuBorderSize,
kSubmenuBorderSize, kSubmenuBorderSize));
}
virtual void Paint(ChromeCanvas* canvas) {
HDC dc = canvas->beginPlatformPaint();
CRect bounds(0, 0, GetWidth(), GetHeight());
NativeTheme::instance()->PaintMenuBackground(
NativeTheme::MENU, dc, MENU_POPUPBACKGROUND, 0, &bounds);
canvas->endPlatformPaint();
}
View* scroll_down_button() { return scroll_down_button_; }
View* scroll_up_button() { return scroll_up_button_; }
virtual void Layout() {
gfx::Insets insets = GetInsets();
int x = insets.left();
int y = insets.top();
int width = GetWidth() - insets.width();
int content_height = GetHeight() - insets.height();
if (!scroll_up_button_->IsVisible()) {
scroll_view_->SetBounds(x, y, width, content_height);
scroll_view_->Layout();
return;
}
CSize pref;
scroll_up_button_->GetPreferredSize(&pref);
scroll_up_button_->SetBounds(x, y, width, pref.cy);
content_height -= pref.cy;
const int scroll_view_y = y + pref.cy;
scroll_down_button_->GetPreferredSize(&pref);
scroll_down_button_->SetBounds(x, GetHeight() - pref.cy - insets.top(),
width, pref.cy);
content_height -= pref.cy;
scroll_view_->SetBounds(x, scroll_view_y, width, content_height);
scroll_view_->Layout();
}
virtual void DidChangeBounds(const CRect& previous, const CRect& current) {
CSize content_pref;
scroll_view_->GetContents()->GetPreferredSize(&content_pref);
scroll_up_button_->SetVisible(content_pref.cy > GetHeight());
scroll_down_button_->SetVisible(content_pref.cy > GetHeight());
}
virtual void GetPreferredSize(CSize* out) {
scroll_view_->GetContents()->GetPreferredSize(out);
gfx::Insets insets = GetInsets();
out->cx += insets.width();
out->cy += insets.height();
}
private:
// The scroll buttons.
View* scroll_up_button_;
View* scroll_down_button_;
// The scroll view.
MenuScrollView* scroll_view_;
DISALLOW_EVIL_CONSTRUCTORS(MenuScrollViewContainer);
};
// MenuSeparator ---------------------------------------------------------------
// Renders a separator.
class MenuSeparator : public View {
public:
MenuSeparator() {
}
void Paint(ChromeCanvas* canvas) {
// The gutter is rendered before the background.
int start_x = 0;
int start_y = GetHeight() / 3;
HDC dc = canvas->beginPlatformPaint();
if (render_gutter) {
// If render_gutter is true, we're on Vista and need to render the
// gutter, then indent the separator from the gutter.
RECT gutter_bounds = { label_start - kGutterToLabel - gutter_width, 0, 0,
GetHeight() };
gutter_bounds.right = gutter_bounds.left + gutter_width;
NativeTheme::instance()->PaintMenuGutter(dc, MENU_POPUPGUTTER, MPI_NORMAL,
&gutter_bounds);
start_x = gutter_bounds.left + gutter_width;
start_y = 0;
}
RECT separator_bounds = { start_x, start_y, GetWidth(), GetHeight() };
NativeTheme::instance()->PaintMenuSeparator(dc, MENU_POPUPSEPARATOR,
MPI_NORMAL, &separator_bounds);
canvas->endPlatformPaint();
}
void GetPreferredSize(CSize* out) {
out->cx = 10; // Just in case we're the only item in a menu.
out->cy = separator_height;
}
private:
DISALLOW_EVIL_CONSTRUCTORS(MenuSeparator);
};
// MenuHostRootView ----------------------------------------------------------
// MenuHostRootView is the RootView of the window showing the menu.
// SubmenuView's scroll view is added as a child of MenuHostRootView.
// MenuHostRootView forwards relevant events to the MenuController.
//
// As all the menu items are owned by the root menu item, care must be taken
// such that when MenuHostRootView is deleted it doesn't delete the menu items.
class MenuHostRootView : public RootView {
public:
explicit MenuHostRootView(ViewContainer* container,
SubmenuView* submenu)
: RootView(container, true),
submenu_(submenu),
forward_drag_to_menu_controller_(true),
suspend_events_(false) {
#ifdef DEBUG_MENU
DLOG(INFO) << " new MenuHostRootView " << this;
#endif
}
virtual bool OnMousePressed(const MouseEvent& event) {
if (suspend_events_)
return true;
forward_drag_to_menu_controller_ =
((event.GetX() < 0 || event.GetY() < 0 || event.GetX() >= GetWidth() ||
event.GetY() >= GetHeight()) ||
!RootView::OnMousePressed(event));
if (forward_drag_to_menu_controller_)
GetMenuController()->OnMousePressed(submenu_, event);
return true;
}
virtual bool OnMouseDragged(const MouseEvent& event) {
if (suspend_events_)
return true;
if (forward_drag_to_menu_controller_) {
#ifdef DEBUG_MENU
DLOG(INFO) << " MenuHostRootView::OnMouseDragged source=" << submenu_;
#endif
GetMenuController()->OnMouseDragged(submenu_, event);
return true;
}
return RootView::OnMouseDragged(event);
}
virtual void OnMouseReleased(const MouseEvent& event, bool canceled) {
if (suspend_events_)
return;
RootView::OnMouseReleased(event, canceled);
if (forward_drag_to_menu_controller_) {
if (canceled) {
GetMenuController()->Cancel(true);
} else {
GetMenuController()->OnMouseReleased(submenu_, event);
}
forward_drag_to_menu_controller_ = false;
}
}
virtual void OnMouseMoved(const MouseEvent& event) {
if (suspend_events_)
return;
RootView::OnMouseMoved(event);
GetMenuController()->OnMouseMoved(submenu_, event);
}
virtual void ProcessOnMouseExited() {
if (suspend_events_)
return;
RootView::ProcessOnMouseExited();
}
virtual bool ProcessMouseWheelEvent(const MouseWheelEvent& e) {
// RootView::ProcessMouseWheelEvent forwards to the focused view. We don't
// have a focused view, so we need to override this then forward to
// the menu.
return submenu_->OnMouseWheel(e);
}
void SuspendEvents() {
suspend_events_ = true;
}
private:
MenuController* GetMenuController() {
return submenu_->GetMenuItem()->GetMenuController();
}
/// The SubmenuView we contain.
SubmenuView* submenu_;
// Whether mouse dragged/released should be forwarded to the MenuController.
bool forward_drag_to_menu_controller_;
// Whether events are suspended. If true, no events are forwarded to the
// MenuController.
bool suspend_events_;
DISALLOW_EVIL_CONSTRUCTORS(MenuHostRootView);
};
// MenuHost ------------------------------------------------------------------
// MenuHost is the window responsible for showing a single menu.
//
// Similar to MenuHostRootView, care must be taken such that when MenuHost is
// deleted, it doesn't delete the menu items. MenuHost is closed via a
// DelayedClosed, which avoids timing issues with deleting the window while
// capture or events are directed at it.
class MenuHost : public HWNDViewContainer {
public:
MenuHost(SubmenuView* submenu)
: closed_(false),
submenu_(submenu),
owns_capture_(false) {
set_window_style(WS_POPUP);
set_initial_class_style(
(win_util::GetWinVersion() < win_util::WINVERSION_XP) ?
0 : CS_DROPSHADOW);
is_mouse_down_ =
((GetKeyState(VK_LBUTTON) & 0x80) ||
(GetKeyState(VK_RBUTTON) & 0x80) ||
(GetKeyState(VK_MBUTTON) & 0x80) ||
(GetKeyState(VK_XBUTTON1) & 0x80) ||
(GetKeyState(VK_XBUTTON2) & 0x80));
// Mouse clicks shouldn't give us focus.
set_window_ex_style(WS_EX_TOPMOST | WS_EX_NOACTIVATE);
}
void Init(HWND parent,
const gfx::Rect& bounds,
View* contents_view,
bool do_capture) {
HWNDViewContainer::Init(parent, bounds, true);
SetContentsView(contents_view);
// We don't want to take focus away from the hosting window.
ShowWindow(SW_SHOWNA);
owns_capture_ = do_capture;
if (do_capture) {
SetCapture();
has_capture_ = true;
#ifdef DEBUG_MENU
DLOG(INFO) << "Doing capture";
#endif
}
}
virtual void Hide() {
if (closed_) {
// We're already closed, nothing to do.
// This is invoked twice if the first time just hid us, and the second
// time deleted Closed (deleted) us.
return;
}
// The menus are freed separately, and possibly before the window is closed,
// remove them so that View doesn't try to access deleted objects.
static_cast<MenuHostRootView*>(GetRootView())->SuspendEvents();
GetRootView()->RemoveAllChildViews(false);
closed_ = true;
ReleaseCapture();
HWNDViewContainer::Hide();
}
virtual void OnCaptureChanged(HWND hwnd) {
HWNDViewContainer::OnCaptureChanged(hwnd);
owns_capture_ = false;
#ifdef DEBUG_MENU
DLOG(INFO) << "Capture changed";
#endif
}
void ReleaseCapture() {
if (owns_capture_) {
#ifdef DEBUG_MENU
DLOG(INFO) << "released capture";
#endif
owns_capture_ = false;
::ReleaseCapture();
}
}
protected:
// Overriden to create MenuHostRootView.
virtual RootView* CreateRootView() {
return new MenuHostRootView(this, submenu_);
}
virtual void OnCancelMode() {
if (!closed_) {
#ifdef DEBUG_MENU
DLOG(INFO) << "OnCanceMode, closing menu";
#endif
submenu_->GetMenuItem()->GetMenuController()->Cancel(true);
}
}
// Overriden to return false, we do NOT want to release capture on mouse
// release.
virtual bool ReleaseCaptureOnMouseReleased() {
return false;
}
private:
// If true, we've been closed.
bool closed_;
// If true, we own the capture and need to release it.
bool owns_capture_;
// The view we contain.
SubmenuView* submenu_;
DISALLOW_EVIL_CONSTRUCTORS(MenuHost);
};
// EmptyMenuMenuItem ---------------------------------------------------------
// EmptyMenuMenuItem is used when a menu has no menu items. EmptyMenuMenuItem
// is itself a MenuItemView, but it uses a different ID so that it isn't
// identified as a MenuItemView.
class EmptyMenuMenuItem : public MenuItemView {
public:
// ID used for EmptyMenuMenuItem.
static const int kEmptyMenuItemViewID;
EmptyMenuMenuItem(MenuItemView* parent) : MenuItemView(parent, 0, NORMAL) {
SetTitle(l10n_util::GetString(IDS_MENU_EMPTY_SUBMENU));
// Set this so that we're not identified as a normal menu item.
SetID(kEmptyMenuItemViewID);
SetEnabled(false);
}
private:
DISALLOW_EVIL_CONSTRUCTORS(EmptyMenuMenuItem);
};
// static
const int EmptyMenuMenuItem::kEmptyMenuItemViewID =
MenuItemView::kMenuItemViewID + 1;
} // namespace
// SubmenuView ---------------------------------------------------------------
SubmenuView::SubmenuView(MenuItemView* parent)
: parent_menu_item_(parent),
host_(NULL),
drop_item_(NULL),
scroll_view_container_(NULL) {
DCHECK(parent);
// We'll delete ourselves, otherwise the ScrollView would delete us on close.
SetParentOwned(false);
}
SubmenuView::~SubmenuView() {
DCHECK(!host_);
delete scroll_view_container_;
}
int SubmenuView::GetMenuItemCount() {
int count = 0;
for (int i = 0; i < GetChildViewCount(); ++i) {
if (GetChildViewAt(i)->GetID() == MenuItemView::kMenuItemViewID)
count++;
}
return count;
}
MenuItemView* SubmenuView::GetMenuItemAt(int index) {
for (int i = 0, count = 0; i < GetChildViewCount(); ++i) {
if (GetChildViewAt(i)->GetID() == MenuItemView::kMenuItemViewID &&
count++ == index) {
return static_cast<MenuItemView*>(GetChildViewAt(i));
}
}
NOTREACHED();
return NULL;
}
void SubmenuView::Layout() {
// We're in a ScrollView, and need to set our width/height ourselves.
View* parent = GetParent();
if (!parent)
return;
CSize pref;
GetPreferredSize(&pref);
SetBounds(GetX(), GetY(), parent->GetWidth(), pref.cy);
gfx::Insets insets = GetInsets();
int x = insets.left();
int y = insets.top();
int menu_item_width = GetWidth() - insets.width();
for (int i = 0; i < GetChildViewCount(); ++i) {
View* child = GetChildViewAt(i);
CSize child_pref_size;
child->GetPreferredSize(&child_pref_size);
child->SetBounds(x, y, menu_item_width, child_pref_size.cy);
y += child_pref_size.cy;
}
}
void SubmenuView::GetPreferredSize(CSize* out) {
if (GetChildViewCount() == 0) {
out->SetSize(0, 0);
return;
}
int max_width = 0;
int height = 0;
for (int i = 0; i < GetChildViewCount(); ++i) {
View* child = GetChildViewAt(i);
CSize child_pref_size;
child->GetPreferredSize(&child_pref_size);
max_width = std::max(max_width, static_cast<int>(child_pref_size.cx));
height += child_pref_size.cy;
}
gfx::Insets insets = GetInsets();
out->SetSize(max_width + insets.width(), height + insets.height());
}
void SubmenuView::DidChangeBounds(const CRect& previous, const CRect& current) {
SchedulePaint();
}
void SubmenuView::PaintChildren(ChromeCanvas* canvas) {
View::PaintChildren(canvas);
if (drop_item_ && drop_position_ != MenuDelegate::DROP_ON)
PaintDropIndicator(canvas, drop_item_, drop_position_);
}
bool SubmenuView::CanDrop(const OSExchangeData& data) {
DCHECK(GetMenuItem()->GetMenuController());
return GetMenuItem()->GetMenuController()->CanDrop(this, data);
}
void SubmenuView::OnDragEntered(const DropTargetEvent& event) {
DCHECK(GetMenuItem()->GetMenuController());
GetMenuItem()->GetMenuController()->OnDragEntered(this, event);
}
int SubmenuView::OnDragUpdated(const DropTargetEvent& event) {
DCHECK(GetMenuItem()->GetMenuController());
return GetMenuItem()->GetMenuController()->OnDragUpdated(this, event);
}
void SubmenuView::OnDragExited() {
DCHECK(GetMenuItem()->GetMenuController());
GetMenuItem()->GetMenuController()->OnDragExited(this);
}
int SubmenuView::OnPerformDrop(const DropTargetEvent& event) {
DCHECK(GetMenuItem()->GetMenuController());
return GetMenuItem()->GetMenuController()->OnPerformDrop(this, event);
}
bool SubmenuView::OnMouseWheel(const MouseWheelEvent& e) {
gfx::Rect vis_bounds = GetVisibleBounds();
int menu_item_count = GetMenuItemCount();
if (vis_bounds.height() == GetHeight() || !menu_item_count) {
// All menu items are visible, nothing to scroll.
return true;
}
// Find the index of the first menu item whose y-coordinate is >= visible
// y-coordinate.
int first_vis_index = -1;
for (int i = 0; i < menu_item_count; ++i) {
MenuItemView* menu_item = GetMenuItemAt(i);
if (menu_item->GetY() == vis_bounds.y()) {
first_vis_index = i;
break;
} else if (menu_item->GetY() > vis_bounds.y()) {
first_vis_index = std::max(0, i - 1);
break;
}
}
if (first_vis_index == -1)
return true;
// If the first item isn't entirely visible, make it visible, otherwise make
// the next/previous one entirely visible.
int delta = abs(e.GetOffset() / WHEEL_DELTA);
bool scroll_up = (e.GetOffset() > 0);
while (delta-- > 0) {
int scroll_amount = 0;
if (scroll_up) {
if (GetMenuItemAt(first_vis_index)->GetY() == vis_bounds.y()) {
if (first_vis_index != 0) {
scroll_amount = GetMenuItemAt(first_vis_index - 1)->GetY() -
vis_bounds.y();
first_vis_index--;
} else {
break;
}
} else {
scroll_amount = GetMenuItemAt(first_vis_index)->GetY() - vis_bounds.y();
}
} else {
if (first_vis_index + 1 == GetMenuItemCount())
break;
scroll_amount = GetMenuItemAt(first_vis_index + 1)->GetY() -
vis_bounds.y();
if (GetMenuItemAt(first_vis_index)->GetY() == vis_bounds.y())
first_vis_index++;
}
ScrollRectToVisible(0, vis_bounds.y() + scroll_amount, vis_bounds.width(),
vis_bounds.height());
vis_bounds = GetVisibleBounds();
}
return true;
}
void SubmenuView::ShowAt(HWND parent,
const gfx::Rect& bounds,
bool do_capture) {
DCHECK(!host_);
host_ = new MenuHost(this);
// Force construction of the scroll view container.
GetScrollViewContainer();
// Make sure the first row is visible.
ScrollRectToVisible(0, 0, 1, 1);
host_->Init(parent, bounds, scroll_view_container_, do_capture);
}
void SubmenuView::Close(bool destroy_host) {
if (host_) {
if (destroy_host) {
host_->Close();
host_ = NULL;
} else {
host_->Hide();
}
}
}
void SubmenuView::ReleaseCapture() {
host_->ReleaseCapture();
}
void SubmenuView::SetDropMenuItem(MenuItemView* item,
MenuDelegate::DropPosition position) {
if (drop_item_ == item && drop_position_ == position)
return;
SchedulePaintForDropIndicator(drop_item_, drop_position_);
drop_item_ = item;
drop_position_ = position;
SchedulePaintForDropIndicator(drop_item_, drop_position_);
}
bool SubmenuView::GetShowSelection(MenuItemView* item) {
if (drop_item_ == NULL)
return true;
// Something is being dropped on one of this menus items. Show the
// selection if the drop is on the passed in item and the drop position is
// ON.
return (drop_item_ == item && drop_position_ == MenuDelegate::DROP_ON);
}
MenuScrollViewContainer* SubmenuView::GetScrollViewContainer() {
if (!scroll_view_container_) {
scroll_view_container_ = new MenuScrollViewContainer(this);
// Otherwise MenuHost would delete us.
scroll_view_container_->SetParentOwned(false);
}
return scroll_view_container_;
}
void SubmenuView::PaintDropIndicator(ChromeCanvas* canvas,
MenuItemView* item,
MenuDelegate::DropPosition position) {
if (position == MenuDelegate::DROP_NONE)
return;
gfx::Rect bounds = CalculateDropIndicatorBounds(item, position);
canvas->FillRectInt(kDropIndicatorColor, bounds.x(), bounds.y(),
bounds.width(), bounds.height());
}
void SubmenuView::SchedulePaintForDropIndicator(
MenuItemView* item,
MenuDelegate::DropPosition position) {
if (item == NULL)
return;
if (position == MenuDelegate::DROP_ON) {
item->SchedulePaint();
} else if (position != MenuDelegate::DROP_NONE) {
gfx::Rect bounds = CalculateDropIndicatorBounds(item, position);
SchedulePaint(bounds.x(), bounds.y(), bounds.width(), bounds.height());
}
}
gfx::Rect SubmenuView::CalculateDropIndicatorBounds(
MenuItemView* item,
MenuDelegate::DropPosition position) {
DCHECK(position != MenuDelegate::DROP_NONE);
CRect item_bounds_c;
item->GetBounds(&item_bounds_c);
gfx::Rect item_bounds(item_bounds_c);
switch (position) {
case MenuDelegate::DROP_BEFORE:
item_bounds.Offset(0, -kDropIndicatorHeight / 2);
item_bounds.set_height(kDropIndicatorHeight);
return item_bounds;
case MenuDelegate::DROP_AFTER:
item_bounds.Offset(0, item_bounds.height() - kDropIndicatorHeight / 2);
item_bounds.set_height(kDropIndicatorHeight);
return item_bounds;
default:
// Don't render anything for on.
return gfx::Rect();
}
}
// MenuItemView ---------------------------------------------------------------
// static
const int MenuItemView::kMenuItemViewID = 1001;
// static
const int MenuItemView::kDropBetweenPixels = 5;
MenuItemView::MenuItemView(MenuDelegate* delegate) {
DCHECK(delegate_);
Init(NULL, 0, SUBMENU, delegate);
}
MenuItemView::~MenuItemView() {
if (controller_) {
// We're currently showing.
// We can't delete ourselves while we're blocking.
DCHECK(!controller_->IsBlockingRun());
// Invoking Cancel is going to call us back and notify the delegate.
// Notifying the delegate from the destructor can be problematic. To avoid
// this the delegate is set to NULL.
delegate_ = NULL;
controller_->Cancel(true);
}
delete submenu_;
}
void MenuItemView::RunMenuAt(HWND parent,
const gfx::Rect& bounds,
AnchorPosition anchor,
bool show_mnemonics) {
PrepareForRun(show_mnemonics);
int mouse_event_flags;
MenuController* controller = MenuController::GetActiveInstance();
bool owns_controller = false;
if (!controller) {
// No menus are showing, show one.
controller = new MenuController(true);
MenuController::SetActiveInstance(controller);
owns_controller = true;
} else {
// A menu is already showing, use the same controller.
// Don't support blocking from within non-blocking.
DCHECK(controller->IsBlockingRun());
}
controller_ = controller;
// Run the loop.
MenuItemView* result =
controller->Run(parent, this, bounds, anchor, &mouse_event_flags);
RemoveEmptyMenus();
controller_ = NULL;
if (owns_controller) {
// We created the controller and need to delete it.
if (MenuController::GetActiveInstance() == controller)
MenuController::SetActiveInstance(NULL);
delete controller;
}
// Make sure all the windows we created to show the menus have been
// destroyed.
DestroyAllMenuHosts();
if (result && delegate_)
delegate_->ExecuteCommand(result->GetCommand(), mouse_event_flags);
}
void MenuItemView::RunMenuForDropAt(HWND parent,
const gfx::Rect& bounds,
AnchorPosition anchor) {
PrepareForRun(false);
// If there is a menu, hide it so that only one menu is shown during dnd.
MenuController* current_controller = MenuController::GetActiveInstance();
if (current_controller) {
current_controller->Cancel(true);
}
// Always create a new controller for non-blocking.
controller_ = new MenuController(false);
// Set the instance, that way it can be canceled by another menu.
MenuController::SetActiveInstance(controller_);
controller_->Run(parent, this, bounds, anchor, NULL);
}
void MenuItemView::Cancel() {
if (controller_ && !canceled_) {
canceled_ = true;
controller_->Cancel(true);
}
}
SubmenuView* MenuItemView::CreateSubmenu() {
if (!submenu_)
submenu_ = new SubmenuView(this);
return submenu_;
}
void MenuItemView::SetSelected(bool selected) {
selected_ = selected;
SchedulePaint();
}
void MenuItemView::SetIcon(const SkBitmap& icon, int item_id) {
MenuItemView* item = GetDescendantByID(item_id);
DCHECK(item);
item->SetIcon(icon);
}
void MenuItemView::SetIcon(const SkBitmap& icon) {
icon_ = icon;
SchedulePaint();
}
void MenuItemView::Paint(ChromeCanvas* canvas) {
Paint(canvas, false);
}
void MenuItemView::GetPreferredSize(CSize* out) {
out->cx = font_.GetStringWidth(title_) + label_start + item_right_margin;
out->cy = font_.height() + kItemBottomMargin + kItemTopMargin;
}
MenuController* MenuItemView::GetMenuController() {
return GetRootMenuItem()->controller_;
}
MenuDelegate* MenuItemView::GetDelegate() {
return GetRootMenuItem()->delegate_;
}
MenuItemView* MenuItemView::GetRootMenuItem() {
MenuItemView* item = this;
while (item) {
MenuItemView* parent = item->GetParentMenuItem();
if (!parent)
return item;
item = parent;
}
NOTREACHED();
return NULL;
}
wchar_t MenuItemView::GetMnemonic() {
const std::wstring& title = GetTitle();
size_t index = 0;
do {
index = title.find('&', index);
if (index != std::wstring::npos) {
if (index + 1 != title.size() && title[index + 1] != '&')
return title[index + 1];
index++;
}
} while (index != std::wstring::npos);
return 0;
}
MenuItemView::MenuItemView(MenuItemView* parent,
int command,
MenuItemView::Type type) {
Init(parent, command, type, NULL);
}
void MenuItemView::Init(MenuItemView* parent,
int command,
MenuItemView::Type type,
MenuDelegate* delegate) {
delegate_ = delegate;
controller_ = NULL;
canceled_ = false;
parent_menu_item_ = parent;
type_ = type;
selected_ = false;
command_ = command;
submenu_ = NULL;
// Assign our ID, this allows SubmenuItemView to find MenuItemViews.
SetID(kMenuItemViewID);
MenuDelegate* root_delegate = GetDelegate();
if (root_delegate)
SetEnabled(root_delegate->IsCommandEnabled(command));
}
MenuItemView* MenuItemView::AppendMenuItemInternal(int item_id,
const std::wstring& label,
const SkBitmap& icon,
Type type) {
if (!submenu_)
CreateSubmenu();
if (type == SEPARATOR) {
submenu_->AddChildView(new MenuSeparator());
return NULL;
}
MenuItemView* item = new MenuItemView(this, item_id, type);
if (label.empty() && GetDelegate())
item->SetTitle(GetDelegate()->GetLabel(item_id));
else
item->SetTitle(label);
item->SetIcon(icon);
if (type == SUBMENU)
item->CreateSubmenu();
submenu_->AddChildView(item);
return item;
}
MenuItemView* MenuItemView::GetDescendantByID(int id) {
if (GetCommand() == id)
return this;
if (!HasSubmenu())
return NULL;
for (int i = 0; i < GetSubmenu()->GetChildViewCount(); ++i) {
View* child = GetSubmenu()->GetChildViewAt(i);
if (child->GetID() == MenuItemView::kMenuItemViewID) {
MenuItemView* result = static_cast<MenuItemView*>(child)->
GetDescendantByID(id);
if (result)
return result;
}
}
return NULL;
}
void MenuItemView::DropMenuClosed(bool notify_delegate) {
DCHECK(controller_);
DCHECK(!controller_->IsBlockingRun());
if (MenuController::GetActiveInstance() == controller_)
MenuController::SetActiveInstance(NULL);
delete controller_;
controller_ = NULL;
RemoveEmptyMenus();
if (notify_delegate && delegate_) {
// Our delegate is null when invoked from the destructor.
delegate_->DropMenuClosed(this);
}
// WARNING: its possible the delegate deleted us at this point.
}
void MenuItemView::PrepareForRun(bool show_mnemonics) {
// Currently we only support showing the root.
DCHECK(!parent_menu_item_);
// Don't invoke run from within run on the same menu.
DCHECK(!controller_);
// Force us to have a submenu.
CreateSubmenu();
canceled_ = false;
show_mnemonics_ = show_mnemonics;
AddEmptyMenus();
UpdateMenuPartSizes();
}
int MenuItemView::GetDrawStringFlags() {
int flags = 0;
if (UILayoutIsRightToLeft())
flags |= ChromeCanvas::TEXT_ALIGN_RIGHT;
else
flags |= ChromeCanvas::TEXT_ALIGN_LEFT;
return flags |
(show_mnemonics_ ? ChromeCanvas::SHOW_PREFIX : ChromeCanvas::HIDE_PREFIX);
}
void MenuItemView::AddEmptyMenus() {
DCHECK(HasSubmenu());
if (submenu_->GetChildViewCount() == 0) {
submenu_->AddChildView(0, new EmptyMenuMenuItem(this));
} else {
for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count;
++i) {
MenuItemView* child = submenu_->GetMenuItemAt(i);
if (child->HasSubmenu())
child->AddEmptyMenus();
}
}
}
void MenuItemView::RemoveEmptyMenus() {
DCHECK(HasSubmenu());
// Iterate backwards as we may end up removing views, which alters the child
// view count.
for (int i = submenu_->GetChildViewCount() - 1; i >= 0; --i) {
View* child = submenu_->GetChildViewAt(i);
if (child->GetID() == MenuItemView::kMenuItemViewID) {
MenuItemView* menu_item = static_cast<MenuItemView*>(child);
if (menu_item->HasSubmenu())
menu_item->RemoveEmptyMenus();
} else if (child->GetID() == EmptyMenuMenuItem::kEmptyMenuItemViewID) {
submenu_->RemoveChildView(child);
}
}
}
void MenuItemView::AdjustBoundsForRTLUI(RECT* rect) const {
gfx::Rect mirrored_rect(*rect);
mirrored_rect.set_x(MirroredLeftPointForRect(mirrored_rect));
*rect = mirrored_rect.ToRECT();
}
void MenuItemView::Paint(ChromeCanvas* canvas, bool for_drag) {
bool render_selection =
(!for_drag && IsSelected() &&
parent_menu_item_->GetSubmenu()->GetShowSelection(this));
int state = render_selection ? MPI_HOT :
(IsEnabled() ? MPI_NORMAL : MPI_DISABLED);
HDC dc = canvas->beginPlatformPaint();
// The gutter is rendered before the background.
if (render_gutter && !for_drag) {
RECT gutter_bounds = { label_start - kGutterToLabel - gutter_width, 0, 0,
GetHeight() };
gutter_bounds.right = gutter_bounds.left + gutter_width;
AdjustBoundsForRTLUI(&gutter_bounds);
NativeTheme::instance()->PaintMenuGutter(dc, MENU_POPUPGUTTER, MPI_NORMAL, &gutter_bounds);
}
// Render the background.
if (!for_drag) {
RECT item_bounds = { 0, 0, GetWidth(), GetHeight() };
AdjustBoundsForRTLUI(&item_bounds);
NativeTheme::instance()->PaintMenuItemBackground(
NativeTheme::MENU, dc, MENU_POPUPITEM, state, render_selection,
&item_bounds);
}
int icon_x = kItemLeftMargin;
int icon_y = kItemTopMargin + (GetHeight() - kItemTopMargin -
kItemBottomMargin - check_height) / 2;
int icon_height = check_height;
int icon_width = check_width;
if (type_ == CHECKBOX && GetDelegate()->IsItemChecked(GetCommand())) {
// Draw the check background.
RECT check_bg_bounds = { 0, 0, icon_x + icon_width, GetHeight() };
const int bg_state = IsEnabled() ? MCB_NORMAL : MCB_DISABLED;
AdjustBoundsForRTLUI(&check_bg_bounds);
NativeTheme::instance()->PaintMenuCheckBackground(
NativeTheme::MENU, dc, MENU_POPUPCHECKBACKGROUND, bg_state,
&check_bg_bounds);
// And the check.
RECT check_bounds = { icon_x, icon_y, icon_x + icon_width,
icon_y + icon_height };
const int check_state = IsEnabled() ? MC_CHECKMARKNORMAL :
MC_CHECKMARKDISABLED;
AdjustBoundsForRTLUI(&check_bounds);
NativeTheme::instance()->PaintMenuCheck(
NativeTheme::MENU, dc, MENU_POPUPCHECK, check_state, &check_bounds,
render_selection);
}
// Render the foreground.
// Menu color is specific to Vista, fallback to classic colors if can't
// get color.
int default_sys_color = render_selection ? COLOR_HIGHLIGHTTEXT :
(IsEnabled() ? COLOR_MENUTEXT : COLOR_GRAYTEXT);
SkColor fg_color = NativeTheme::instance()->GetThemeColorWithDefault(
NativeTheme::MENU, MENU_POPUPITEM, state, TMT_TEXTCOLOR, default_sys_color);
int width = GetWidth() - item_right_margin - label_start;
gfx::Rect text_bounds(label_start, kItemTopMargin, width, font_.height());
text_bounds.set_x(MirroredLeftPointForRect(text_bounds));
canvas->DrawStringInt(GetTitle(), font_, fg_color, text_bounds.x(),
text_bounds.y(), text_bounds.width(),
text_bounds.height(),
GetRootMenuItem()->GetDrawStringFlags());
if (icon_.width() > 0) {
gfx::Rect icon_bounds(kItemLeftMargin,
kItemTopMargin + (GetHeight() - kItemTopMargin -
kItemBottomMargin - icon_.height()) / 2,
icon_.width(),
icon_.height());
icon_bounds.set_x(MirroredLeftPointForRect(icon_bounds));
canvas->DrawBitmapInt(icon_, icon_bounds.x(), icon_bounds.y());
}
if (HasSubmenu()) {
int state_id = IsEnabled() ? MSM_NORMAL : MSM_DISABLED;
RECT arrow_bounds = { GetWidth() - item_right_margin + kLabelToArrowPadding,
0, 0, GetHeight() };
arrow_bounds.right = arrow_bounds.left + arrow_width;
AdjustBoundsForRTLUI(&arrow_bounds);
// If our sub menus open from right to left (which is the case when the
// locale is RTL) then we should make sure the menu arrow points to the
// right direction.
NativeTheme::MenuArrowDirection arrow_direction;
if (UILayoutIsRightToLeft())
arrow_direction = NativeTheme::LEFT_POINTING_ARROW;
else
arrow_direction = NativeTheme::RIGHT_POINTING_ARROW;
NativeTheme::instance()->PaintMenuArrow(
NativeTheme::MENU, dc, MENU_POPUPSUBMENU, state_id, &arrow_bounds,
arrow_direction, render_selection);
}
canvas->endPlatformPaint();
}
void MenuItemView::DestroyAllMenuHosts() {
if (!HasSubmenu())
return;
submenu_->Close(true);
for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count;
++i) {
submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts();
}
}
// MenuController ------------------------------------------------------------
// static
MenuController* MenuController::active_instance_ = NULL;
// static
MenuController* MenuController::GetActiveInstance() {
return active_instance_;
}
#ifdef DEBUG_MENU
static int instance_count = 0;
static int nested_depth = 0;
#endif
MenuItemView* MenuController::Run(HWND parent,
MenuItemView* root,
const gfx::Rect& bounds,
MenuItemView::AnchorPosition position,
int* result_mouse_event_flags) {
exit_all_ = false;
possible_drag_ = false;
bool nested_menu = showing_;
if (showing_) {
// Only support nesting of blocking_run menus, nesting of
// blocking/non-blocking shouldn't be needed.
DCHECK(blocking_run_);
// We're already showing, push the current state.
menu_stack_.push_back(state_);
// The context menu should be owned by the same parent.
DCHECK(owner_ == parent);
} else {
showing_ = true;
}
// Reset current state.
pending_state_ = State();
state_ = State();
pending_state_.initial_bounds = bounds;
if (bounds.height() > 1) {
// Inset the bounds slightly, otherwise drag coordinates don't line up
// nicely and menus close prematurely.
pending_state_.initial_bounds.Inset(0, 1);
}
pending_state_.anchor = position;
owner_ = parent;
// Calculate the bounds of the monitor we'll show menus on. Do this once to
// avoid repeated system queries for the info.
POINT initial_loc = { bounds.x(), bounds.y() };
HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST);
if (monitor) {
MONITORINFO mi = {0};
mi.cbSize = sizeof(mi);
GetMonitorInfo(monitor, &mi);
// Menus appear over the taskbar.
pending_state_.monitor_bounds = gfx::Rect(mi.rcMonitor);
}
did_capture_ = false;
any_menu_contains_mouse_ = false;
// Set the selection, which opens the initial menu.
SetSelection(root, true, true);
if (!blocking_run_) {
// Start the timer to hide the menu. This is needed as we get no
// notification when the drag has finished.
StartCancelAllTimer();
return NULL;
}
#ifdef DEBUG_MENU
nested_depth++;
DLOG(INFO) << " entering nested loop, depth=" << nested_depth;
#endif
MessageLoop::current()->Run(this);
#ifdef DEBUG_MENU
nested_depth--;
DLOG(INFO) << " exiting nested loop, depth=" << nested_depth;
#endif
// Close any open menus.
SetSelection(NULL, false, true);
if (nested_menu) {
DCHECK(!menu_stack_.empty());
// We're running from within a menu, restore the previous state.
// The menus are already showing, so we don't have to show them.
state_ = menu_stack_.back();
pending_state_ = menu_stack_.back();
menu_stack_.pop_back();
} else {
showing_ = false;
}
MenuItemView* result = result_;
// In case we're nested, reset result_.
result_ = NULL;
if (result_mouse_event_flags)
*result_mouse_event_flags = result_mouse_event_flags_;
if (nested_menu && result) {
// We're nested and about to return a value. The caller might enter another
// blocking loop. We need to make sure all menus are hidden before that
// happens otherwise the menus will stay on screen.
CloseAllNestedMenus();
// Set exit_all_ to true, which makes sure all nested loops exit
// immediately.
exit_all_ = true;
}
return result;
}
void MenuController::SetSelection(MenuItemView* menu_item,
bool open_submenu,
bool update_immediately) {
size_t paths_differ_at = 0;
std::vector<MenuItemView*> current_path;
std::vector<MenuItemView*> new_path;
BuildPathsAndCalculateDiff(pending_state_.item, menu_item, ¤t_path,
&new_path, &paths_differ_at);
size_t current_size = current_path.size();
size_t new_size = new_path.size();
// Notify the old path it isn't selected.
for (size_t i = paths_differ_at; i < current_size; ++i)
current_path[i]->SetSelected(false);
// Notify the new path it is selected.
for (size_t i = paths_differ_at; i < new_size; ++i)
new_path[i]->SetSelected(true);
if (menu_item && menu_item->GetDelegate())
menu_item->GetDelegate()->SelectionChanged(menu_item);
pending_state_.item = menu_item;
pending_state_.submenu_open = open_submenu;
// Stop timers.
StopShowTimer();
StopCancelAllTimer();
if (update_immediately)
CommitPendingSelection();
else
StartShowTimer();
}
void MenuController::Cancel(bool all) {
if (!showing_) {
// This occurs if we're in the process of notifying the delegate for a drop
// and the delegate cancels us.
return;
}
MenuItemView* selected = state_.item;
exit_all_ = all;
// Hide windows immediately.
SetSelection(NULL, false, true);
if (!blocking_run_) {
// If we didn't block the caller we need to notify the menu, which
// triggers deleting us.
DCHECK(selected);
showing_ = false;
selected->GetRootMenuItem()->DropMenuClosed(true);
// WARNING: the call to MenuClosed deletes us.
return;
}
}
void MenuController::OnMousePressed(SubmenuView* source,
const MouseEvent& event) {
#ifdef DEBUG_MENU
DLOG(INFO) << "OnMousePressed source=" << source;
#endif
if (!blocking_run_)
return;
MenuPart part =
GetMenuPartByScreenCoordinate(source, event.GetX(), event.GetY());
if (part.is_scroll())
return; // Ignore presses on scroll buttons.
if (part.type == MenuPart::NONE ||
(part.type == MenuPart::MENU_ITEM && part.menu &&
part.menu->GetRootMenuItem() != state_.item->GetRootMenuItem())) {
// Mouse wasn't pressed over any menu, or the active menu, cancel.
// We're going to close and we own the mouse capture. We need to repost the
// mouse down, otherwise the window the user clicked on won't get the
// event.
RepostEvent(source, event);
// And close.
Cancel(true);
return;
}
any_menu_contains_mouse_ = true;
bool open_submenu = false;
if (!part.menu) {
part.menu = source->GetMenuItem();
open_submenu = true;
} else {
if (part.menu->GetDelegate()->CanDrag(part.menu)) {
possible_drag_ = true;
press_x_ = event.GetX();
press_y_ = event.GetY();
}
if (part.menu->HasSubmenu())
open_submenu = true;
}
// On a press we immediately commit the selection, that way a submenu
// pops up immediately rather than after a delay.
SetSelection(part.menu, open_submenu, true);
}
void MenuController::OnMouseDragged(SubmenuView* source,
const MouseEvent& event) {
#ifdef DEBUG_MENU
DLOG(INFO) << "OnMouseDragged source=" << source;
#endif
MenuPart part =
GetMenuPartByScreenCoordinate(source, event.GetX(), event.GetY());
UpdateScrolling(part);
if (!blocking_run_)
return;
if (possible_drag_) {
if (ChromeViews::View::ExceededDragThreshold(event.GetX() - press_x_,
event.GetY() - press_y_)) {
MenuItemView* item = state_.item;
DCHECK(item);
// Points are in the coordinates of the submenu, need to map to that of
// the selected item. Additionally source may not be the parent of
// the selected item, so need to map to screen first then to item.
CPoint press_loc(press_x_, press_y_);
View::ConvertPointToScreen(source->GetScrollViewContainer(), &press_loc);
View::ConvertPointToView(NULL, item, &press_loc);
CPoint drag_loc(event.GetX(), event.GetY());
View::ConvertPointToScreen(source->GetScrollViewContainer(), &drag_loc);
View::ConvertPointToView(NULL, item, &drag_loc);
in_drag_ = true;
ChromeCanvas canvas(item->GetWidth(), item->GetHeight(), false);
item->Paint(&canvas, true);
scoped_refptr<OSExchangeData> data(new OSExchangeData);
item->GetDelegate()->WriteDragData(item, data.get());
drag_utils::SetDragImageOnDataObject(canvas, item->GetWidth(),
item->GetHeight(), press_loc.x,
press_loc.y, data);
scoped_refptr<BaseDragSource> drag_source(new BaseDragSource);
int drag_ops = item->GetDelegate()->GetDragOperations(item);
DWORD effects;
StopScrolling();
DoDragDrop(data, drag_source,
DragDropTypes::DragOperationToDropEffect(drag_ops),
&effects);
if (GetActiveInstance() == this) {
if (showing_ ) {
// We're still showing, close all menus.
CloseAllNestedMenus();
Cancel(true);
} // else case, drop was on us.
} // else case, someone canceled us, don't do anything
}
return;
}
if (part.type == MenuPart::MENU_ITEM) {
if (!part.menu)
part.menu = source->GetMenuItem();
SetSelection(part.menu ? part.menu : state_.item, true, false);
}
any_menu_contains_mouse_ = (part.type == MenuPart::MENU_ITEM);
}
void MenuController::OnMouseReleased(SubmenuView* source,
const MouseEvent& event) {
#ifdef DEBUG_MENU
DLOG(INFO) << "OnMouseReleased source=" << source;
#endif
if (!blocking_run_)
return;
DCHECK(state_.item);
possible_drag_ = false;
DCHECK(blocking_run_);
MenuPart part =
GetMenuPartByScreenCoordinate(source, event.GetX(), event.GetY());
any_menu_contains_mouse_ = (part.type == MenuPart::MENU_ITEM);
if (event.IsRightMouseButton() && (part.type == MenuPart::MENU_ITEM &&
part.menu)) {
// Set the selection immediately, making sure the submenu is only open
// if it already was.
bool open_submenu = (state_.item == pending_state_.item &&
state_.submenu_open);
SetSelection(pending_state_.item, open_submenu, true);
CPoint loc(event.GetX(), event.GetY());
View::ConvertPointToScreen(source->GetScrollViewContainer(), &loc);
part.menu->GetDelegate()->ShowContextMenu(
part.menu, part.menu->GetCommand(), loc.x, loc.y, true);
} else if (!part.is_scroll() && part.menu && !part.menu->HasSubmenu()) {
if (part.menu->GetDelegate()->IsTriggerableEvent(event)) {
Accept(part.menu, event.GetFlags());
return;
}
} else if (part.type == MenuPart::MENU_ITEM) {
// User either clicked on empty space, or a menu that has children.
SetSelection(part.menu ? part.menu : state_.item, true, true);
}
}
void MenuController::OnMouseMoved(SubmenuView* source,
const MouseEvent& event) {
#ifdef DEBUG_MENU
DLOG(INFO) << "OnMouseMoved source=" << source;
#endif
if (showing_submenu_)
return;
MenuPart part =
GetMenuPartByScreenCoordinate(source, event.GetX(), event.GetY());
UpdateScrolling(part);
if (!blocking_run_)
return;
any_menu_contains_mouse_ = (part.type == MenuPart::MENU_ITEM);
if (part.type == MenuPart::MENU_ITEM && part.menu) {
SetSelection(part.menu, true, false);
} else if (!part.is_scroll() && any_menu_contains_mouse_ &&
pending_state_.item &&
(!pending_state_.item->HasSubmenu() ||
!pending_state_.item->GetSubmenu()->IsShowing())) {
// On exit if the user hasn't selected an item with a submenu, move the
// selection back to the parent menu item.
SetSelection(pending_state_.item->GetParentMenuItem(), true, false);
any_menu_contains_mouse_ = false;
}
}
void MenuController::OnMouseEntered(SubmenuView* source,
const MouseEvent& event) {
// MouseEntered is always followed by a mouse moved, so don't need to
// do anything here.
}
bool MenuController::CanDrop(SubmenuView* source, const OSExchangeData& data) {
return source->GetMenuItem()->GetDelegate()->CanDrop(source->GetMenuItem(),
data);
}
void MenuController::OnDragEntered(SubmenuView* source,
const DropTargetEvent& event) {
valid_drop_coordinates_ = false;
}
int MenuController::OnDragUpdated(SubmenuView* source,
const DropTargetEvent& event) {
StopCancelAllTimer();
CPoint screen_loc(event.GetX(), event.GetY());
View::ConvertPointToScreen(source, &screen_loc);
if (valid_drop_coordinates_ && screen_loc.x == drop_x_ &&
screen_loc.y == drop_y_) {
return last_drop_operation_;
}
drop_x_ = screen_loc.x;
drop_y_ = screen_loc.y;
valid_drop_coordinates_ = true;
MenuItemView* menu_item = GetMenuItemAt(source, event.GetX(), event.GetY());
bool over_empty_menu = false;
if (!menu_item) {
// See if we're over an empty menu.
menu_item = GetEmptyMenuItemAt(source, event.GetX(), event.GetY());
if (menu_item)
over_empty_menu = true;
}
MenuDelegate::DropPosition drop_position = MenuDelegate::DROP_NONE;
int drop_operation = DragDropTypes::DRAG_NONE;
if (menu_item) {
CPoint menu_item_loc(event.GetX(), event.GetY());
View::ConvertPointToView(source, menu_item, &menu_item_loc);
MenuItemView* query_menu_item;
if (!over_empty_menu) {
int menu_item_height = menu_item->GetHeight();
if (menu_item->HasSubmenu() &&
(menu_item_loc.y > MenuItemView::kDropBetweenPixels &&
menu_item_loc.y < (menu_item_height -
MenuItemView::kDropBetweenPixels))) {
drop_position = MenuDelegate::DROP_ON;
} else if (menu_item_loc.y < menu_item_height / 2) {
drop_position = MenuDelegate::DROP_BEFORE;
} else {
drop_position = MenuDelegate::DROP_AFTER;
}
query_menu_item = menu_item;
} else {
query_menu_item = menu_item->GetParentMenuItem();
drop_position = MenuDelegate::DROP_ON;
}
drop_operation = menu_item->GetDelegate()->GetDropOperation(
query_menu_item, event, &drop_position);
if (menu_item->HasSubmenu()) {
// The menu has a submenu, schedule the submenu to open.
SetSelection(menu_item, true, false);
} else {
SetSelection(menu_item, false, false);
}
if (drop_position == MenuDelegate::DROP_NONE ||
drop_operation == DragDropTypes::DRAG_NONE) {
menu_item = NULL;
}
} else {
SetSelection(source->GetMenuItem(), true, false);
}
SetDropMenuItem(menu_item, drop_position);
last_drop_operation_ = drop_operation;
return drop_operation;
}
void MenuController::OnDragExited(SubmenuView* source) {
StartCancelAllTimer();
if (drop_target_) {
StopShowTimer();
SetDropMenuItem(NULL, MenuDelegate::DROP_NONE);
}
}
int MenuController::OnPerformDrop(SubmenuView* source,
const DropTargetEvent& event) {
DCHECK(drop_target_);
// NOTE: the delegate may delete us after invoking OnPerformDrop, as such
// we don't call cancel here.
MenuItemView* item = state_.item;
DCHECK(item);
MenuItemView* drop_target = drop_target_;
MenuDelegate::DropPosition drop_position = drop_position_;
// Close all menus, including any nested menus.
SetSelection(NULL, false, true);
CloseAllNestedMenus();
// Set state such that we exit.
showing_ = false;
exit_all_ = true;
if (!IsBlockingRun())
item->GetRootMenuItem()->DropMenuClosed(false);
// WARNING: the call to MenuClosed deletes us.
// If over an empty menu item, drop occurs on the parent.
if (drop_target->GetID() == EmptyMenuMenuItem::kEmptyMenuItemViewID)
drop_target = drop_target->GetParentMenuItem();
return drop_target->GetDelegate()->OnPerformDrop(
drop_target, drop_position, event);
}
void MenuController::OnDragEnteredScrollButton(SubmenuView* source,
bool is_up) {
MenuPart part;
part.type = is_up ? MenuPart::SCROLL_UP : MenuPart::SCROLL_DOWN;
part.submenu = source;
UpdateScrolling(part);
// Do this to force the selection to hide.
SetDropMenuItem(source->GetMenuItemAt(0), MenuDelegate::DROP_NONE);
StopCancelAllTimer();
}
void MenuController::OnDragExitedScrollButton(SubmenuView* source) {
StartCancelAllTimer();
SetDropMenuItem(NULL, MenuDelegate::DROP_NONE);
StopScrolling();
}
// static
void MenuController::SetActiveInstance(MenuController* controller) {
active_instance_ = controller;
}
bool MenuController::Dispatch(const MSG& msg) {
DCHECK(blocking_run_);
if (exit_all_)
return false;
// NOTE: we don't get WM_ACTIVATE or anything else interesting in here.
switch (msg.message) {
// NOTE: focus wasn't changed when the menu was shown. As such, don't
// dispatch key events otherwise the focused window will get the events.
case WM_KEYDOWN:
return OnKeyDown(msg);
case WM_CHAR:
return OnChar(msg);
case WM_KEYUP:
return true;
case WM_CANCELMODE:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
// Exit immediately on system keys.
Cancel(true);
return false;
default:
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
return !exit_all_;
}
bool MenuController::OnKeyDown(const MSG& msg) {
DCHECK(blocking_run_);
switch (msg.wParam) {
case VK_UP:
IncrementSelection(-1);
break;
case VK_DOWN:
IncrementSelection(1);
break;
// Handling of VK_RIGHT and VK_LEFT is different depending on the UI
// layout.
case VK_RIGHT:
if (l10n_util::TextDirection() == l10n_util::RIGHT_TO_LEFT)
CloseSubmenu();
else
OpenSubmenuChangeSelectionIfCan();
break;
case VK_LEFT:
if (l10n_util::TextDirection() == l10n_util::RIGHT_TO_LEFT)
OpenSubmenuChangeSelectionIfCan();
else
CloseSubmenu();
break;
case VK_RETURN:
if (pending_state_.item) {
if (pending_state_.item->HasSubmenu()) {
OpenSubmenuChangeSelectionIfCan();
} else if (pending_state_.item->IsEnabled()) {
Accept(pending_state_.item, 0);
return false;
}
}
break;
case VK_ESCAPE:
if (!state_.item->GetParentMenuItem() ||
(!state_.item->GetParentMenuItem()->GetParentMenuItem() &&
(!state_.item->HasSubmenu() ||
!state_.item->GetSubmenu()->IsShowing()))) {
// User pressed escape and only one menu is shown, cancel it.
Cancel(false);
return false;
} else {
CloseSubmenu();
}
break;
default:
TranslateMessage(&msg);
break;
}
return true;
}
bool MenuController::OnChar(const MSG& msg) {
DCHECK(blocking_run_);
return !SelectByChar(static_cast<wchar_t>(msg.wParam));
}
MenuController::MenuController(bool blocking)
: blocking_run_(blocking),
showing_(false),
exit_all_(false),
did_capture_(false),
#pragma warning(suppress: 4355) // Okay to pass "this" here.
show_task_(this),
result_(NULL),
show_timer_(NULL),
#pragma warning(suppress: 4355) // Okay to pass "this" here.
cancel_all_task_(this),
cancel_all_timer_(NULL),
drop_target_(NULL),
owner_(NULL),
possible_drag_(false),
valid_drop_coordinates_(false),
in_drag_(false),
any_menu_contains_mouse_(false),
showing_submenu_(false),
result_mouse_event_flags_(0) {
#ifdef DEBUG_MENU
instance_count++;
DLOG(INFO) << "created MC, count=" << instance_count;
#endif
}
MenuController::~MenuController() {
DCHECK(!showing_);
StopShowTimer();
StopCancelAllTimer();
#ifdef DEBUG_MENU
instance_count--;
DLOG(INFO) << "destroyed MC, count=" << instance_count;
#endif
}
void MenuController::Accept(MenuItemView* item, int mouse_event_flags) {
DCHECK(IsBlockingRun());
result_ = item;
exit_all_ = true;
result_mouse_event_flags_ = mouse_event_flags;
}
void MenuController::CloseAllNestedMenus() {
for (std::list<State>::iterator i = menu_stack_.begin();
i != menu_stack_.end(); ++i) {
MenuItemView* item = i->item;
MenuItemView* last_item = item;
while (item) {
CloseMenu(item);
last_item = item;
item = item->GetParentMenuItem();
}
i->submenu_open = false;
i->item = last_item;
}
}
MenuItemView* MenuController::GetMenuItemAt(View* source, int x, int y) {
View* child_under_mouse = source->GetViewForPoint(CPoint(x, y));
if (child_under_mouse && child_under_mouse->IsEnabled() &&
child_under_mouse->GetID() == MenuItemView::kMenuItemViewID) {
return static_cast<MenuItemView*>(child_under_mouse);
}
return NULL;
}
MenuItemView* MenuController::GetEmptyMenuItemAt(View* source, int x, int y) {
View* child_under_mouse = source->GetViewForPoint(CPoint(x, y));
if (child_under_mouse &&
child_under_mouse->GetID() == EmptyMenuMenuItem::kEmptyMenuItemViewID) {
return static_cast<MenuItemView*>(child_under_mouse);
}
return NULL;
}
bool MenuController::IsScrollButtonAt(SubmenuView* source,
int x,
int y,
MenuPart::Type* part) {
MenuScrollViewContainer* scroll_view = source->GetScrollViewContainer();
View* child_under_mouse = scroll_view->GetViewForPoint(CPoint(x, y));
if (child_under_mouse && child_under_mouse->IsEnabled()) {
if (child_under_mouse == scroll_view->scroll_up_button()) {
*part = MenuPart::SCROLL_UP;
return true;
}
if (child_under_mouse == scroll_view->scroll_down_button()) {
*part = MenuPart::SCROLL_DOWN;
return true;
}
}
return false;
}
MenuController::MenuPart MenuController::GetMenuPartByScreenCoordinate(
SubmenuView* source,
int source_x,
int source_y) {
MenuPart part;
CPoint screen_loc(source_x, source_y);
View::ConvertPointToScreen(source->GetScrollViewContainer(), &screen_loc);
MenuItemView* item = state_.item;
while (item) {
if (item->HasSubmenu() && item->GetSubmenu()->IsShowing() &&
GetMenuPartByScreenCoordinateImpl(item->GetSubmenu(), screen_loc,
&part)) {
return part;
}
item = item->GetParentMenuItem();
}
return part;
}
bool MenuController::GetMenuPartByScreenCoordinateImpl(
SubmenuView* menu,
const CPoint& screen_loc,
MenuPart* part) {
// Is the mouse over the scroll buttons?
CPoint scroll_view_loc = screen_loc;
View* scroll_view_container = menu->GetScrollViewContainer();
View::ConvertPointToView(NULL, scroll_view_container, &scroll_view_loc);
if (scroll_view_loc.x < 0 ||
scroll_view_loc.x >= scroll_view_container->GetWidth() ||
scroll_view_loc.y < 0 ||
scroll_view_loc.y >= scroll_view_container->GetHeight()) {
// Point isn't contained in menu.
return false;
}
if (IsScrollButtonAt(menu, scroll_view_loc.x, scroll_view_loc.y,
&(part->type))) {
part->submenu = menu;
return true;
}
// Not over the scroll button. Check the actual menu.
if (DoesSubmenuContainLocation(menu, screen_loc)) {
CPoint menu_loc = screen_loc;
View::ConvertPointToView(NULL, menu, &menu_loc);
part->menu = GetMenuItemAt(menu, menu_loc.x, menu_loc.y);
part->type = MenuPart::MENU_ITEM;
return true;
}
// While the mouse isn't over a menu item or the scroll buttons of menu, it
// is contained by menu and so we return true. If we didn't return true other
// menus would be searched, even though they are likely obscured by us.
return true;
}
bool MenuController::DoesSubmenuContainLocation(SubmenuView* submenu,
const CPoint& screen_loc) {
CPoint view_loc = screen_loc;
View::ConvertPointToView(NULL, submenu, &view_loc);
gfx::Rect vis_rect = submenu->GetVisibleBounds();
return vis_rect.Contains(view_loc.x, view_loc.y);
}
void MenuController::CommitPendingSelection() {
StopShowTimer();
size_t paths_differ_at = 0;
std::vector<MenuItemView*> current_path;
std::vector<MenuItemView*> new_path;
BuildPathsAndCalculateDiff(state_.item, pending_state_.item, ¤t_path,
&new_path, &paths_differ_at);
// Hide the old menu.
for (size_t i = paths_differ_at; i < current_path.size(); ++i) {
if (current_path[i]->HasSubmenu()) {
// See description of in_drag_ as to why close is conditionalized like
// this.
current_path[i]->GetSubmenu()->Close(!in_drag_);
}
}
// Copy pending to state_, making sure to preserve the direction menus were
// opened.
std::list<bool> pending_open_direction;
state_.open_leading.swap(pending_open_direction);
state_ = pending_state_;
state_.open_leading.swap(pending_open_direction);
int menu_depth = MenuDepth(state_.item);
if (menu_depth == 0) {
state_.open_leading.clear();
} else {
int cached_size = static_cast<int>(state_.open_leading.size());
DCHECK(menu_depth >= 0);
while (cached_size-- >= menu_depth)
state_.open_leading.pop_back();
}
if (!state_.item) {
// Nothing to select.
StopScrolling();
return;
}
// Open all the submenus preceeding the last menu item (last menu item is
// handled next).
if (new_path.size() > 1) {
for (std::vector<MenuItemView*>::iterator i = new_path.begin();
i != new_path.end() - 1; ++i) {
OpenMenu(*i);
}
}
if (state_.submenu_open) {
// The submenu should be open, open the submenu if the item has a submenu.
if (state_.item->HasSubmenu()) {
OpenMenu(state_.item);
} else {
state_.submenu_open = false;
}
} else if (state_.item->HasSubmenu() &&
state_.item->GetSubmenu()->IsShowing()) {
// The submenu is showing, but it shouldn't be, close it.
// See description of in_drag_ as to why close is conditionalized like
// this.
state_.item->GetSubmenu()->Close(!in_drag_);
}
if (scroll_task_.get() && scroll_task_->submenu()) {
// Stop the scrolling if none of the elements of the selection contain
// the menu being scrolled.
bool found = false;
MenuItemView* item = state_.item;
while (item && !found) {
found = (item->HasSubmenu() && item->GetSubmenu()->IsShowing() &&
item->GetSubmenu() == scroll_task_->submenu());
item = item->GetParentMenuItem();
}
if (!found)
StopScrolling();
}
}
void MenuController::CloseMenu(MenuItemView* item) {
DCHECK(item);
if (!item->HasSubmenu())
return;
// See description of in_drag_ as to why close is conditionalized like this.
item->GetSubmenu()->Close(!in_drag_);
}
void MenuController::OpenMenu(MenuItemView* item) {
DCHECK(item);
if (item->GetSubmenu()->IsShowing()) {
return;
}
bool prefer_leading =
state_.open_leading.empty() ? true : state_.open_leading.back();
bool resulting_direction;
gfx::Rect bounds =
CalculateMenuBounds(item, prefer_leading, &resulting_direction);
state_.open_leading.push_back(resulting_direction);
bool do_capture = (!did_capture_ && blocking_run_);
showing_submenu_ = true;
item->GetSubmenu()->ShowAt(owner_, bounds, do_capture);
showing_submenu_ = false;
did_capture_ = true;
}
void MenuController::BuildPathsAndCalculateDiff(
MenuItemView* old_item,
MenuItemView* new_item,
std::vector<MenuItemView*>* old_path,
std::vector<MenuItemView*>* new_path,
size_t* first_diff_at) {
DCHECK(old_path && new_path && first_diff_at);
BuildMenuItemPath(old_item, old_path);
BuildMenuItemPath(new_item, new_path);
size_t common_size = std::min(old_path->size(), new_path->size());
// Find the first difference between the two paths, when the loop
// returns, diff_i is the first index where the two paths differ.
for (size_t i = 0; i < common_size; ++i) {
if ((*old_path)[i] != (*new_path)[i]) {
*first_diff_at = i;
return;
}
}
*first_diff_at = common_size;
}
void MenuController::BuildMenuItemPath(MenuItemView* item,
std::vector<MenuItemView*>* path) {
if (!item)
return;
BuildMenuItemPath(item->GetParentMenuItem(), path);
path->push_back(item);
}
void MenuController::StartShowTimer() {
StopShowTimer();
show_timer_ = MessageLoop::current()->timer_manager()->
StartTimer(kShowDelay, &show_task_, false);
}
void MenuController::StopShowTimer() {
if (show_timer_) {
MessageLoop::current()->timer_manager()->StopTimer(show_timer_);
delete show_timer_;
show_timer_ = NULL;
}
}
void MenuController::StartCancelAllTimer() {
StopCancelAllTimer();
cancel_all_timer_ = MessageLoop::current()->timer_manager()->
StartTimer(kCloseOnExitTime, &cancel_all_task_, false);
}
void MenuController::StopCancelAllTimer() {
if (cancel_all_timer_) {
MessageLoop::current()->timer_manager()->StopTimer(cancel_all_timer_);
delete cancel_all_timer_;
cancel_all_timer_ = NULL;
}
}
gfx::Rect MenuController::CalculateMenuBounds(MenuItemView* item,
bool prefer_leading,
bool* is_leading) {
DCHECK(item);
SubmenuView* submenu = item->GetSubmenu();
DCHECK(submenu);
CSize pref;
submenu->GetScrollViewContainer()->GetPreferredSize(&pref);
// Don't let the menu go to wide. This is some where between what IE and FF
// do.
pref.cx = std::min(pref.cx, kMaxMenuWidth);
if (!state_.monitor_bounds.IsEmpty())
pref.cx = std::min(pref.cx,
static_cast<LONG>(state_.monitor_bounds.width()));
// Assume we can honor prefer_leading.
*is_leading = prefer_leading;
int x, y;
if (!item->GetParentMenuItem()) {
// First item, position relative to initial location.
x = state_.initial_bounds.x();
y = state_.initial_bounds.bottom();
if (state_.anchor == MenuItemView::TOPRIGHT)
x = x + state_.initial_bounds.width() - pref.cx;
if (!state_.monitor_bounds.IsEmpty() &&
y + pref.cy > state_.monitor_bounds.bottom()) {
// The menu doesn't fit on screen. If the first location is above the
// half way point, show from the mouse location to bottom of screen.
// Otherwise show from the top of the screen to the location of the mouse.
// While odd, this behavior matches IE.
if (y < (state_.monitor_bounds.y() +
state_.monitor_bounds.height() / 2)) {
pref.cy = std::min(pref.cy,
static_cast<LONG>(state_.monitor_bounds.bottom() - y));
} else {
pref.cy = std::min(pref.cy, static_cast<LONG>(
state_.initial_bounds.y() - state_.monitor_bounds.y()));
y = state_.initial_bounds.y() - pref.cy;
}
}
} else {
// Not the first menu; position it relative to the bounds of the menu
// item.
CPoint item_loc(0, 0);
View::ConvertPointToScreen(item, &item_loc);
// We must make sure we take into account the UI layout. If the layout is
// RTL, then a 'leading' menu is positioned to the left of the parent menu
// item and not to the right.
bool layout_is_rtl = item->UILayoutIsRightToLeft();
bool create_on_the_right = (prefer_leading && !layout_is_rtl) ||
(!prefer_leading && layout_is_rtl);
if (create_on_the_right) {
x = item_loc.x + item->GetWidth() - kSubmenuHorizontalInset;
if (state_.monitor_bounds.width() != 0 &&
x + pref.cx > state_.monitor_bounds.right()) {
if (layout_is_rtl)
*is_leading = true;
else
*is_leading = false;
x = item_loc.x - pref.cx + kSubmenuHorizontalInset;
}
} else {
x = item_loc.x - pref.cx + kSubmenuHorizontalInset;
if (state_.monitor_bounds.width() != 0 && x < state_.monitor_bounds.x()) {
if (layout_is_rtl)
*is_leading = false;
else
*is_leading = true;
x = item_loc.x + item->GetWidth() - kSubmenuHorizontalInset;
}
}
y = item_loc.y - kSubmenuBorderSize;
if (state_.monitor_bounds.width() != 0) {
pref.cy = std::min(pref.cy,
static_cast<LONG>(state_.monitor_bounds.height()));
if (y + pref.cy > state_.monitor_bounds.bottom())
y = state_.monitor_bounds.bottom() - pref.cy;
if (y < state_.monitor_bounds.y())
y = state_.monitor_bounds.y();
}
}
if (state_.monitor_bounds.width() != 0) {
if (x + pref.cx > state_.monitor_bounds.right())
x = state_.monitor_bounds.right() - pref.cx;
if (x < state_.monitor_bounds.x())
x = state_.monitor_bounds.x();
}
return gfx::Rect(x, y, pref.cx, pref.cy);
}
// static
int MenuController::MenuDepth(MenuItemView* item) {
if (!item)
return 0;
return MenuDepth(item->GetParentMenuItem()) + 1;
}
void MenuController::IncrementSelection(int delta) {
MenuItemView* item = pending_state_.item;
DCHECK(item);
if (pending_state_.submenu_open && item->HasSubmenu() &&
item->GetSubmenu()->IsShowing()) {
// A menu is selected and open, but none of its children are selected,
// select the first menu item.
if (item->GetSubmenu()->GetMenuItemCount()) {
SetSelection(item->GetSubmenu()->GetMenuItemAt(0), false, false);
ScrollToVisible(item->GetSubmenu()->GetMenuItemAt(0));
return; // return so else case can fall through.
}
}
if (item->GetParentMenuItem()) {
MenuItemView* parent = item->GetParentMenuItem();
int parent_count = parent->GetSubmenu()->GetMenuItemCount();
if (parent_count > 1) {
for (int i = 0; i < parent_count; ++i) {
if (parent->GetSubmenu()->GetMenuItemAt(i) == item) {
int next_index = (i + delta + parent_count) % parent_count;
ScrollToVisible(parent->GetSubmenu()->GetMenuItemAt(next_index));
SetSelection(parent->GetSubmenu()->GetMenuItemAt(next_index), false,
false);
break;
}
}
}
}
}
void MenuController::OpenSubmenuChangeSelectionIfCan() {
MenuItemView* item = pending_state_.item;
if (item->HasSubmenu()) {
if (item->GetSubmenu()->GetMenuItemCount() > 0) {
SetSelection(item->GetSubmenu()->GetMenuItemAt(0), false, true);
} else {
// No menu items, just show the sub-menu.
SetSelection(item, true, true);
}
}
}
void MenuController::CloseSubmenu() {
MenuItemView* item = state_.item;
DCHECK(item);
if (!item->GetParentMenuItem())
return;
if (item->HasSubmenu() && item->GetSubmenu()->IsShowing()) {
SetSelection(item, false, true);
} else if (item->GetParentMenuItem()->GetParentMenuItem()) {
SetSelection(item->GetParentMenuItem(), false, true);
}
}
bool MenuController::IsMenuWindow(MenuItemView* item, HWND window) {
if (!item)
return false;
return ((item->HasSubmenu() && item->GetSubmenu()->IsShowing() &&
item->GetSubmenu()->GetViewContainer()->GetHWND() == window) ||
IsMenuWindow(item->GetParentMenuItem(), window));
}
bool MenuController::SelectByChar(wchar_t character) {
wchar_t char_array[1] = { character };
wchar_t key = l10n_util::ToLower(char_array)[0];
MenuItemView* item = pending_state_.item;
if (!item->HasSubmenu() || !item->GetSubmenu()->IsShowing())
item = item->GetParentMenuItem();
DCHECK(item);
DCHECK(item->HasSubmenu());
SubmenuView* submenu = item->GetSubmenu();
DCHECK(submenu);
int menu_item_count = submenu->GetMenuItemCount();
if (!menu_item_count)
return false;
for (int i = 0; i < menu_item_count; ++i) {
MenuItemView* child = submenu->GetMenuItemAt(i);
if (child->GetMnemonic() == key && child->IsEnabled()) {
Accept(child, 0);
return true;
}
}
// No matching mnemonic, search through items that don't have mnemonic
// based on first character of the title.
int first_match = -1;
bool has_multiple = false;
int next_match = -1;
int index_of_item = -1;
for (int i = 0; i < menu_item_count; ++i) {
MenuItemView* child = submenu->GetMenuItemAt(i);
if (!child->GetMnemonic() && child->IsEnabled()) {
std::wstring lower_title = l10n_util::ToLower(child->GetTitle());
if (child == pending_state_.item)
index_of_item = i;
if (lower_title.length() && lower_title[0] == key) {
if (first_match == -1)
first_match = i;
else
has_multiple = true;
if (next_match == -1 && index_of_item != -1 && i > index_of_item)
next_match = i;
}
}
}
if (first_match != -1) {
if (!has_multiple) {
if (submenu->GetMenuItemAt(first_match)->HasSubmenu()) {
SetSelection(submenu->GetMenuItemAt(first_match), true, false);
} else {
Accept(submenu->GetMenuItemAt(first_match), 0);
return true;
}
} else if (index_of_item == -1 || next_match == -1) {
SetSelection(submenu->GetMenuItemAt(first_match), false, false);
} else {
SetSelection(submenu->GetMenuItemAt(next_match), false, false);
}
}
return false;
}
void MenuController::RepostEvent(SubmenuView* source,
const MouseEvent& event) {
CPoint screen_loc(event.GetX(), event.GetY());
View::ConvertPointToScreen(source->GetScrollViewContainer(), &screen_loc);
HWND window = WindowFromPoint(screen_loc);
if (window) {
#ifdef DEBUG_MENU
DLOG(INFO) << "RepostEvent on press";
#endif
// Release the capture.
SubmenuView* submenu = state_.item->GetRootMenuItem()->GetSubmenu();
submenu->ReleaseCapture();
if (submenu->host() && submenu->host()->GetHWND() &&
GetWindowThreadProcessId(submenu->host()->GetHWND(), NULL) !=
GetWindowThreadProcessId(window, NULL)) {
// Even though we have mouse capture, windows generates a mouse event
// if the other window is in a separate thread. Don't generate an event in
// this case else the target window can get double events leading to bad
// behavior.
return;
}
// Convert the coordinates to the target window.
RECT window_bounds;
GetWindowRect(window, &window_bounds);
int window_x = screen_loc.x - window_bounds.left;
int window_y = screen_loc.y - window_bounds.top;
// Determine whether the click was in the client area or not.
// NOTE: WM_NCHITTEST coordinates are relative to the screen.
LRESULT nc_hit_result = SendMessage(window, WM_NCHITTEST, 0,
MAKELPARAM(screen_loc.x, screen_loc.y));
const bool in_client_area = (nc_hit_result == HTCLIENT);
// TODO(sky): this isn't right. The event to generate should correspond
// with the event we just got. MouseEvent only tells us what is down,
// which may differ. Need to add ability to get changed button from
// MouseEvent.
int event_type;
if (event.IsLeftMouseButton())
event_type = in_client_area ? WM_LBUTTONDOWN : WM_NCLBUTTONDOWN;
else if (event.IsMiddleMouseButton())
event_type = in_client_area ? WM_MBUTTONDOWN : WM_NCMBUTTONDOWN;
else if (event.IsRightMouseButton())
event_type = in_client_area ? WM_RBUTTONDOWN : WM_NCRBUTTONDOWN;
else
event_type = 0; // Unknown mouse press.
if (event_type) {
if (in_client_area) {
PostMessage(window, event_type, event.GetWindowsFlags(),
MAKELPARAM(window_x, window_y));
} else {
PostMessage(window, WM_NCLBUTTONDOWN, nc_hit_result,
MAKELPARAM(window_x, window_y));
}
}
}
}
void MenuController::SetDropMenuItem(
MenuItemView* new_target,
MenuDelegate::DropPosition new_position) {
if (new_target == drop_target_ && new_position == drop_position_)
return;
if (drop_target_) {
drop_target_->GetParentMenuItem()->GetSubmenu()->SetDropMenuItem(
NULL, MenuDelegate::DROP_NONE);
}
drop_target_ = new_target;
drop_position_ = new_position;
if (drop_target_) {
drop_target_->GetParentMenuItem()->GetSubmenu()->SetDropMenuItem(
drop_target_, drop_position_);
}
}
void MenuController::UpdateScrolling(const MenuPart& part) {
if (!part.is_scroll() && !scroll_task_.get())
return;
if (!scroll_task_.get())
scroll_task_.reset(new MenuScrollTask());
scroll_task_->Update(part);
}
void MenuController::StopScrolling() {
scroll_task_.reset(NULL);
}
} // namespace ChromeViews
|