This file is indexed.

/usr/include/OGRE/SdkTrays.h is in libogre-dev 1.7.4-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
/*
 -----------------------------------------------------------------------------
 This source file is part of OGRE
 (Object-oriented Graphics Rendering Engine)
 For the latest info, see http://www.ogre3d.org/
 
 Copyright (c) 2000-2011 Torus Knot Software Ltd
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 -----------------------------------------------------------------------------
 */
#ifndef __SdkTrays_H__
#define __SdkTrays_H__

#include "Ogre.h"
#include "OgreFontManager.h"
#include "OgreBorderPanelOverlayElement.h"
#include "OgreTextAreaOverlayElement.h"
#include <math.h>

#if OGRE_COMPILER == OGRE_COMPILER_MSVC
// TODO - remove this
#   pragma warning (disable : 4244)
#endif

namespace OgreBites
{
	enum TrayLocation   // enumerator values for widget tray anchoring locations
	{
		TL_TOPLEFT,
		TL_TOP,
		TL_TOPRIGHT,
		TL_LEFT,
		TL_CENTER,
		TL_RIGHT,
		TL_BOTTOMLEFT,
		TL_BOTTOM,
		TL_BOTTOMRIGHT,
		TL_NONE
	};

	enum ButtonState   // enumerator values for button states
	{
		BS_UP,
		BS_OVER,
		BS_DOWN
	};

	// forward widget class declarations
	class Widget;
	class Button;
	class SelectMenu;
	class Label;
	class Slider;
	class CheckBox;

	/*=============================================================================
	| Listener class for responding to tray events.
	=============================================================================*/
	class SdkTrayListener
    {
    public:

		virtual ~SdkTrayListener() {}
		virtual void buttonHit(Button* button) {}
		virtual void itemSelected(SelectMenu* menu) {}
		virtual void labelHit(Label* label) {}
		virtual void sliderMoved(Slider* slider) {}
		virtual void checkBoxToggled(CheckBox* box) {}
		virtual void okDialogClosed(const Ogre::DisplayString& message) {}
		virtual void yesNoDialogClosed(const Ogre::DisplayString& question, bool yesHit) {}
    };

	/*=============================================================================
	| Abstract base class for all widgets.
	=============================================================================*/
	class Widget
	{
	public:
			
		Widget()
		{
			mTrayLoc = TL_NONE;
			mElement = 0;
			mListener = 0;
		}

		virtual ~Widget() {}

		void cleanup()
		{
			if (mElement) nukeOverlayElement(mElement);
			mElement = 0;
		}

		/*-----------------------------------------------------------------------------
		| Static utility method to recursively delete an overlay element plus
		| all of its children from the system.
		-----------------------------------------------------------------------------*/
		static void nukeOverlayElement(Ogre::OverlayElement* element)
		{
			Ogre::OverlayContainer* container = dynamic_cast<Ogre::OverlayContainer*>(element);
			if (container)
			{
				std::vector<Ogre::OverlayElement*> toDelete;

				Ogre::OverlayContainer::ChildIterator children = container->getChildIterator();
				while (children.hasMoreElements())
				{
					toDelete.push_back(children.getNext());
				}

				for (unsigned int i = 0; i < toDelete.size(); i++)
				{
					nukeOverlayElement(toDelete[i]);
				}
			}
			if (element)
			{
				Ogre::OverlayContainer* parent = element->getParent();
				if (parent) parent->removeChild(element->getName());
				Ogre::OverlayManager::getSingleton().destroyOverlayElement(element);
			}
		}

		/*-----------------------------------------------------------------------------
		| Static utility method to check if the cursor is over an overlay element.
		-----------------------------------------------------------------------------*/
		static bool isCursorOver(Ogre::OverlayElement* element, const Ogre::Vector2& cursorPos, Ogre::Real voidBorder = 0)
		{
			Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton();
            Ogre::Real l = element->_getDerivedLeft() * om.getViewportWidth();
            Ogre::Real t = element->_getDerivedTop() * om.getViewportHeight();
            Ogre::Real r = l + element->getWidth();
            Ogre::Real b = t + element->getHeight();

			return (cursorPos.x >= l + voidBorder && cursorPos.x <= r - voidBorder &&
				cursorPos.y >= t + voidBorder && cursorPos.y <= b - voidBorder);
		}

		/*-----------------------------------------------------------------------------
		| Static utility method used to get the cursor's offset from the center
		| of an overlay element in pixels.
		-----------------------------------------------------------------------------*/
		static Ogre::Vector2 cursorOffset(Ogre::OverlayElement* element, const Ogre::Vector2& cursorPos)
		{
			Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton();
			return Ogre::Vector2(cursorPos.x - (element->_getDerivedLeft() * om.getViewportWidth() + element->getWidth() / 2),
				cursorPos.y - (element->_getDerivedTop() * om.getViewportHeight() + element->getHeight() / 2));
		}

		/*-----------------------------------------------------------------------------
		| Static utility method used to get the width of a caption in a text area.
		-----------------------------------------------------------------------------*/
		static Ogre::Real getCaptionWidth(const Ogre::DisplayString& caption, Ogre::TextAreaOverlayElement* area)
		{
			Ogre::Font* font = (Ogre::Font*)Ogre::FontManager::getSingleton().getByName(area->getFontName()).getPointer();
			Ogre::String current = caption.asUTF8();
			Ogre::Real lineWidth = 0;

			for (unsigned int i = 0; i < current.length(); i++)
			{
				// be sure to provide a line width in the text area
				if (current[i] == ' ')
				{
					if (area->getSpaceWidth() != 0) lineWidth += area->getSpaceWidth();
					else lineWidth += font->getGlyphAspectRatio(' ') * area->getCharHeight();
				}
				else if (current[i] == '\n') break;
				// use glyph information to calculate line width
				else lineWidth += font->getGlyphAspectRatio(current[i]) * area->getCharHeight();
			}

			return (unsigned int)lineWidth;
		}

		/*-----------------------------------------------------------------------------
		| Static utility method to cut off a string to fit in a text area.
		-----------------------------------------------------------------------------*/
		static void fitCaptionToArea(const Ogre::DisplayString& caption, Ogre::TextAreaOverlayElement* area, Ogre::Real maxWidth)
		{
			Ogre::Font* f = (Ogre::Font*)Ogre::FontManager::getSingleton().getByName(area->getFontName()).getPointer();
			Ogre::String s = caption.asUTF8();

			int nl = s.find('\n');
			if (nl != -1) s = s.substr(0, nl);

			Ogre::Real width = 0;

			for (unsigned int i = 0; i < s.length(); i++)
			{
				if (s[i] == ' ' && area->getSpaceWidth() != 0) width += area->getSpaceWidth();
				else width += f->getGlyphAspectRatio(s[i]) * area->getCharHeight();
				if (width > maxWidth)
				{
					s = s.substr(0, i);
					break;
				}
			}

			area->setCaption(s);
		}

		Ogre::OverlayElement* getOverlayElement()
		{
			return mElement;
		}

		const Ogre::String& getName()
		{
			return mElement->getName();
		}

		TrayLocation getTrayLocation()
		{
			return mTrayLoc;
		}

		void hide()
		{
			mElement->hide();
		}

		void show()
		{
			mElement->show();
		}

		bool isVisible()
		{
			return mElement->isVisible();
		}

		// callbacks

		virtual void _cursorPressed(const Ogre::Vector2& cursorPos) {}
		virtual void _cursorReleased(const Ogre::Vector2& cursorPos) {}
		virtual void _cursorMoved(const Ogre::Vector2& cursorPos) {}
		virtual void _focusLost() {}

		// internal methods used by SdkTrayManager. do not call directly.

		void _assignToTray(TrayLocation trayLoc) { mTrayLoc = trayLoc; }
		void _assignListener(SdkTrayListener* listener) { mListener = listener; }

	protected:

		Ogre::OverlayElement* mElement;
		TrayLocation mTrayLoc;
		SdkTrayListener* mListener;
	};

	typedef std::vector<Widget*> WidgetList;
	typedef Ogre::VectorIterator<WidgetList> WidgetIterator;

	/*=============================================================================
	| Basic button class.
	=============================================================================*/
	class Button : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		Button(const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width)
		{
			mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate("SdkTrays/Button", "BorderPanel", name);
			mBP = (Ogre::BorderPanelOverlayElement*)mElement;
			mTextArea = (Ogre::TextAreaOverlayElement*)mBP->getChild(mBP->getName() + "/ButtonCaption");
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
            mTextArea->setCharHeight(mTextArea->getCharHeight() - 3);
#endif
			mTextArea->setTop(-(mTextArea->getCharHeight() / 2));

			if (width > 0)
			{
				mElement->setWidth(width);
				mFitToContents = false;
			}
			else mFitToContents = true;

			setCaption(caption);
			mState = BS_UP;
		}

		virtual ~Button() {}

		const Ogre::DisplayString& getCaption()
		{
			return mTextArea->getCaption();
		}

		void setCaption(const Ogre::DisplayString& caption)
		{
			mTextArea->setCaption(caption);
			if (mFitToContents) mElement->setWidth(getCaptionWidth(caption, mTextArea) + mElement->getHeight() - 12);
		}

		const ButtonState& getState() { return mState; }

		void _cursorPressed(const Ogre::Vector2& cursorPos)
		{
			if (isCursorOver(mElement, cursorPos, 4)) setState(BS_DOWN);
		}

		void _cursorReleased(const Ogre::Vector2& cursorPos)
		{
			if (mState == BS_DOWN)
			{
				setState(BS_OVER);
				if (mListener) mListener->buttonHit(this);
			}
		}

		void _cursorMoved(const Ogre::Vector2& cursorPos)
		{
			if (isCursorOver(mElement, cursorPos, 4))
			{
				if (mState == BS_UP) setState(BS_OVER);
			}
			else
			{
				if (mState != BS_UP) setState(BS_UP);
			}
		}

		void _focusLost()
		{
			setState(BS_UP);   // reset button if cursor was lost
		}

	protected:

		void setState(const ButtonState& bs)
		{
			if (bs == BS_OVER)
			{
				mBP->setBorderMaterialName("SdkTrays/Button/Over");
				mBP->setMaterialName("SdkTrays/Button/Over");
			}
			else if (bs == BS_UP)
			{
				mBP->setBorderMaterialName("SdkTrays/Button/Up");
				mBP->setMaterialName("SdkTrays/Button/Up");
			}
			else
			{
				mBP->setBorderMaterialName("SdkTrays/Button/Down");
				mBP->setMaterialName("SdkTrays/Button/Down");
			}

			mState = bs;
		}

		ButtonState mState;
		Ogre::BorderPanelOverlayElement* mBP;
		Ogre::TextAreaOverlayElement* mTextArea;
		bool mFitToContents;
	};  

	/*=============================================================================
	| Scrollable text box widget.
	=============================================================================*/
	class TextBox : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		TextBox(const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width, Ogre::Real height)
		{
			mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate("SdkTrays/TextBox", "BorderPanel", name);
			mElement->setWidth(width);
			mElement->setHeight(height);
			Ogre::OverlayContainer* container = (Ogre::OverlayContainer*)mElement;
			mTextArea = (Ogre::TextAreaOverlayElement*)container->getChild(getName() + "/TextBoxText");
			mCaptionBar = (Ogre::BorderPanelOverlayElement*)container->getChild(getName() + "/TextBoxCaptionBar");
			mCaptionBar->setWidth(width - 4);
			mCaptionTextArea = (Ogre::TextAreaOverlayElement*)mCaptionBar->getChild(mCaptionBar->getName() + "/TextBoxCaption");
			setCaption(caption);
			mScrollTrack = (Ogre::BorderPanelOverlayElement*)container->getChild(getName() + "/TextBoxScrollTrack");
			mScrollHandle = (Ogre::PanelOverlayElement*)mScrollTrack->getChild(mScrollTrack->getName() + "/TextBoxScrollHandle");
			mScrollHandle->hide();
			mDragging = false;
			mScrollPercentage = 0;
			mStartingLine = 0;
			mPadding = 15;
			mText = "";
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
            mTextArea->setCharHeight(mTextArea->getCharHeight() - 3);
            mCaptionTextArea->setCharHeight(mCaptionTextArea->getCharHeight() - 3);
#endif
			refitContents();
		}

		void setPadding(Ogre::Real padding)
		{
			mPadding = padding;
			refitContents();
		}

		Ogre::Real getPadding()
		{
			return mPadding;
		}

		const Ogre::DisplayString& getCaption()
		{
			return mCaptionTextArea->getCaption();
		}

		void setCaption(const Ogre::DisplayString& caption)
		{
			mCaptionTextArea->setCaption(caption);
		}

		const Ogre::DisplayString& getText()
		{
			return mText;
		}

		/*-----------------------------------------------------------------------------
		| Sets text box content. Most of this method is for wordwrap.
		-----------------------------------------------------------------------------*/
		void setText(const Ogre::DisplayString& text)
		{
			mText = text;
			mLines.clear();

			Ogre::Font* font = (Ogre::Font*)Ogre::FontManager::getSingleton().getByName(mTextArea->getFontName()).getPointer();
            
			Ogre::String current = text.asUTF8();
			bool firstWord = true;
			unsigned int lastSpace = 0;
			unsigned int lineBegin = 0;
			Ogre::Real lineWidth = 0;
			Ogre::Real rightBoundary = mElement->getWidth() - 2 * mPadding + mScrollTrack->getLeft() + 10;

			for (unsigned int i = 0; i < current.length(); i++)
			{
				if (current[i] == ' ')
				{
					if (mTextArea->getSpaceWidth() != 0) lineWidth += mTextArea->getSpaceWidth();
					else lineWidth += font->getGlyphAspectRatio(' ') * mTextArea->getCharHeight();
					firstWord = false;
					lastSpace = i;
				}
				else if (current[i] == '\n')
				{
					firstWord = true;
					lineWidth = 0;
					mLines.push_back(current.substr(lineBegin, i - lineBegin));
					lineBegin = i + 1;
				}
				else
				{
					// use glyph information to calculate line width
					lineWidth += font->getGlyphAspectRatio(current[i]) * mTextArea->getCharHeight();
					if (lineWidth > rightBoundary)
					{
						if (firstWord)
						{
							current.insert(i, "\n");
							i = i - 1;
						}
						else
						{
							current[lastSpace] = '\n';
							i = lastSpace - 1;
						}
					}
				}
			}

			mLines.push_back(current.substr(lineBegin));

			unsigned int maxLines = getHeightInLines();

			if (mLines.size() > maxLines)     // if too much text, filter based on scroll percentage
			{
				mScrollHandle->show();
				filterLines();
			}
			else       // otherwise just show all the text
			{
				mTextArea->setCaption(current);
				mScrollHandle->hide();
				mScrollPercentage = 0;
				mScrollHandle->setTop(0);
			}
		}

		/*-----------------------------------------------------------------------------
		| Sets text box content horizontal alignment.
		-----------------------------------------------------------------------------*/
		void setTextAlignment(Ogre::TextAreaOverlayElement::Alignment ta)
		{
			if (ta == Ogre::TextAreaOverlayElement::Left) mTextArea->setHorizontalAlignment(Ogre::GHA_LEFT);
			else if (ta == Ogre::TextAreaOverlayElement::Center) mTextArea->setHorizontalAlignment(Ogre::GHA_CENTER);
			else mTextArea->setHorizontalAlignment(Ogre::GHA_RIGHT);
			refitContents();
		}

		void clearText()
		{
			setText("");
		}

		void appendText(const Ogre::DisplayString& text)
		{
			setText(getText() + text);
		}

		/*-----------------------------------------------------------------------------
		| Makes adjustments based on new padding, size, or alignment info.
		-----------------------------------------------------------------------------*/
		void refitContents()
		{
			mScrollTrack->setHeight(mElement->getHeight() - mCaptionBar->getHeight() - 20);
			mScrollTrack->setTop(mCaptionBar->getHeight() + 10);

			mTextArea->setTop(mCaptionBar->getHeight() + mPadding - 5);
			if (mTextArea->getHorizontalAlignment() == Ogre::GHA_RIGHT) mTextArea->setLeft(-mPadding + mScrollTrack->getLeft());
			else if (mTextArea->getHorizontalAlignment() == Ogre::GHA_LEFT) mTextArea->setLeft(mPadding);
			else mTextArea->setLeft(mScrollTrack->getLeft() / 2);

			setText(getText());
		}

		/*-----------------------------------------------------------------------------
		| Sets how far scrolled down the text is as a percentage.
		-----------------------------------------------------------------------------*/
		void setScrollPercentage(Ogre::Real percentage)
		{
			mScrollPercentage = Ogre::Math::Clamp<Ogre::Real>(percentage, 0, 1);
			mScrollHandle->setTop((int)(percentage * (mScrollTrack->getHeight() - mScrollHandle->getHeight())));
			filterLines();
		}

		/*-----------------------------------------------------------------------------
		| Gets how far scrolled down the text is as a percentage.
		-----------------------------------------------------------------------------*/
		Ogre::Real getScrollPercentage()
		{
			return mScrollPercentage;
		}

		/*-----------------------------------------------------------------------------
		| Gets how many lines of text can fit in this window.
		-----------------------------------------------------------------------------*/
		unsigned int getHeightInLines()
		{
			return (unsigned int) ((mElement->getHeight() - 2 * mPadding - mCaptionBar->getHeight() + 5) / mTextArea->getCharHeight());
		}

		void _cursorPressed(const Ogre::Vector2& cursorPos)
		{
			if (!mScrollHandle->isVisible()) return;   // don't care about clicks if text not scrollable

			Ogre::Vector2 co = Widget::cursorOffset(mScrollHandle, cursorPos);

			if (co.squaredLength() <= 81)
			{
				mDragging = true;
				mDragOffset = co.y;
			}
			else if (Widget::isCursorOver(mScrollTrack, cursorPos))
			{
				Ogre::Real newTop = mScrollHandle->getTop() + co.y;
				Ogre::Real lowerBoundary = mScrollTrack->getHeight() - mScrollHandle->getHeight();
				mScrollHandle->setTop(Ogre::Math::Clamp<int>((int)newTop, 0, (int)lowerBoundary));

				// update text area contents based on new scroll percentage
				mScrollPercentage = Ogre::Math::Clamp<Ogre::Real>(newTop / lowerBoundary, 0, 1);
				filterLines();
			}
		}

		void _cursorReleased(const Ogre::Vector2& cursorPos)
		{
			mDragging = false;
		}

		void _cursorMoved(const Ogre::Vector2& cursorPos)
		{
			if (mDragging)
			{
				Ogre::Vector2 co = Widget::cursorOffset(mScrollHandle, cursorPos);
				Ogre::Real newTop = mScrollHandle->getTop() + co.y - mDragOffset;
				Ogre::Real lowerBoundary = mScrollTrack->getHeight() - mScrollHandle->getHeight();
				mScrollHandle->setTop(Ogre::Math::Clamp<int>((int)newTop, 0, (int)lowerBoundary));

				// update text area contents based on new scroll percentage
				mScrollPercentage = Ogre::Math::Clamp<Ogre::Real>(newTop / lowerBoundary, 0, 1);
				filterLines();
			}
		}

		void _focusLost()
		{
			mDragging = false;  // stop dragging if cursor was lost
		}

	protected:

		/*-----------------------------------------------------------------------------
		| Decides which lines to show.
		-----------------------------------------------------------------------------*/
		void filterLines()
		{
			Ogre::String shown = "";
			unsigned int maxLines = getHeightInLines();
			unsigned int newStart = (unsigned int) (mScrollPercentage * (mLines.size() - maxLines) + 0.5);

			mStartingLine = newStart;

			for (unsigned int i = 0; i < maxLines; i++)
			{
				shown += mLines[mStartingLine + i] + "\n";
			}

			mTextArea->setCaption(shown);    // show just the filtered lines
		}

		Ogre::TextAreaOverlayElement* mTextArea;
		Ogre::BorderPanelOverlayElement* mCaptionBar;
		Ogre::TextAreaOverlayElement* mCaptionTextArea;
		Ogre::BorderPanelOverlayElement* mScrollTrack;
		Ogre::PanelOverlayElement* mScrollHandle;
		Ogre::DisplayString mText;
		Ogre::StringVector mLines;
		Ogre::Real mPadding;
		bool mDragging;
		Ogre::Real mScrollPercentage;
		Ogre::Real mDragOffset;
		unsigned int mStartingLine;
	};

	/*=============================================================================
	| Basic selection menu widget.
	=============================================================================*/
	class SelectMenu : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		SelectMenu(const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width,
			Ogre::Real boxWidth, unsigned int maxItemsShown)
			: mHighlightIndex(0)
			, mDisplayIndex(0)
			, mDragOffset(0.0f)
		{
			mSelectionIndex = -1;
			mFitToContents = false;
			mCursorOver = false;
			mExpanded = false;
			mDragging = false;
			mMaxItemsShown = maxItemsShown;
			mItemsShown = 0;
			mElement = (Ogre::BorderPanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
				("SdkTrays/SelectMenu", "BorderPanel", name);
			mTextArea = (Ogre::TextAreaOverlayElement*)((Ogre::OverlayContainer*)mElement)->getChild(name + "/MenuCaption");
			mSmallBox = (Ogre::BorderPanelOverlayElement*)((Ogre::OverlayContainer*)mElement)->getChild(name + "/MenuSmallBox");
			mSmallBox->setWidth(width - 10);
			mSmallTextArea = (Ogre::TextAreaOverlayElement*)mSmallBox->getChild(name + "/MenuSmallBox/MenuSmallText");
			mElement->setWidth(width);
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
            mTextArea->setCharHeight(mTextArea->getCharHeight() - 3);
            mSmallTextArea->setCharHeight(mSmallTextArea->getCharHeight() - 3);
#endif
            
			if (boxWidth > 0)  // long style
			{
				if (width <= 0) mFitToContents = true;
				mSmallBox->setWidth(boxWidth);
				mSmallBox->setTop(2);
				mSmallBox->setLeft(width - boxWidth - 5);
				mElement->setHeight(mSmallBox->getHeight() + 4);
				mTextArea->setHorizontalAlignment(Ogre::GHA_LEFT);
				mTextArea->setAlignment(Ogre::TextAreaOverlayElement::Left);
				mTextArea->setLeft(12);
				mTextArea->setTop(10);
			}
						
			mExpandedBox = (Ogre::BorderPanelOverlayElement*)((Ogre::OverlayContainer*)mElement)->getChild(name + "/MenuExpandedBox");
			mExpandedBox->setWidth(mSmallBox->getWidth() + 10);
			mExpandedBox->hide();
			mScrollTrack = (Ogre::BorderPanelOverlayElement*)mExpandedBox->getChild(mExpandedBox->getName() + "/MenuScrollTrack");
			mScrollHandle = (Ogre::PanelOverlayElement*)mScrollTrack->getChild(mScrollTrack->getName() + "/MenuScrollHandle");

			setCaption(caption);
		}

		bool isExpanded()
		{
			return mExpanded;
		}

		const Ogre::DisplayString& getCaption()
		{
			return mTextArea->getCaption();
		}

		void setCaption(const Ogre::DisplayString& caption)
		{
			mTextArea->setCaption(caption);
			if (mFitToContents)
			{
				mElement->setWidth(getCaptionWidth(caption, mTextArea) + mSmallBox->getWidth() + 23);
				mSmallBox->setLeft(mElement->getWidth() - mSmallBox->getWidth() - 5);
			}
		}

		const Ogre::StringVector& getItems()
		{
			return mItems;
		}

		unsigned int getNumItems()
		{
			return mItems.size();
		}

		void setItems(const Ogre::StringVector& items)
		{
			mItems = items;
			mSelectionIndex = -1;

			for (unsigned int i = 0; i < mItemElements.size(); i++)   // destroy all the item elements
			{
				nukeOverlayElement(mItemElements[i]);
			}
			mItemElements.clear();

			mItemsShown = std::max<int>(2, std::min<int>(mMaxItemsShown, mItems.size()));

			for (unsigned int i = 0; i < mItemsShown; i++)   // create all the item elements
			{
				Ogre::BorderPanelOverlayElement* e =
					(Ogre::BorderPanelOverlayElement*)Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
					("SdkTrays/SelectMenuItem", "BorderPanel",
					mExpandedBox->getName() + "/Item" + Ogre::StringConverter::toString(i + 1));

				e->setTop(6 + i * (mSmallBox->getHeight() - 8));
				e->setWidth(mExpandedBox->getWidth() - 32);

				mExpandedBox->addChild(e);
				mItemElements.push_back(e);
			}

			if (!items.empty()) selectItem(0, false);
			else mSmallTextArea->setCaption("");
		}

		void addItem(const Ogre::DisplayString& item)
		{
			mItems.push_back(item);
			setItems(mItems);
		}

		void removeItem(const Ogre::DisplayString& item)
		{
			Ogre::StringVector::iterator it;

			for (it = mItems.begin(); it != mItems.end(); it++)
			{
				if (item == *it) break;
			}

			if (it != mItems.end())
			{
				mItems.erase(it);
				if (mItems.size() < mItemsShown)
				{
					mItemsShown = mItems.size();
					nukeOverlayElement(mItemElements.back());
					mItemElements.pop_back();
				}
			}
			else 
			{
				Ogre::String desc = "Menu \"" + getName() + "\" contains no item \"" + item + "\".";
				OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "SelectMenu::removeItem");
			}
		}

		void removeItem(unsigned int index)
		{
			Ogre::StringVector::iterator it;
			unsigned int i = 0;

			for (it = mItems.begin(); it != mItems.end(); it++)
			{
				if (i == index) break;
				i++;
			}

			if (it != mItems.end())
			{
				mItems.erase(it);
				if (mItems.size() < mItemsShown)
				{
					mItemsShown = mItems.size();
					nukeOverlayElement(mItemElements.back());
					mItemElements.pop_back();
				}
			}
			else 
			{
				Ogre::String desc = "Menu \"" + getName() + "\" contains no item at position " +
					Ogre::StringConverter::toString(index) + ".";
				OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "SelectMenu::removeItem");
			}
		}

		void clearItems()
		{
			mItems.clear();
			mSelectionIndex = -1;
			mSmallTextArea->setCaption("");
		}

		void selectItem(unsigned int index, bool notifyListener = true)
		{
			if (index >= mItems.size())
			{
				Ogre::String desc = "Menu \"" + getName() + "\" contains no item at position " +
					Ogre::StringConverter::toString(index) + ".";
				OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "SelectMenu::selectItem");
			}

			mSelectionIndex = index;
			fitCaptionToArea(mItems[index], mSmallTextArea, mSmallBox->getWidth() - mSmallTextArea->getLeft() * 2);

			if (mListener && notifyListener) mListener->itemSelected(this);
		}

		void selectItem(const Ogre::DisplayString& item, bool notifyListener = true)
		{
			for (unsigned int i = 0; i < mItems.size(); i++)
			{
				if (item == mItems[i])
				{
					selectItem(i, notifyListener);
					return;
				}
			}

			Ogre::String desc = "Menu \"" + getName() + "\" contains no item \"" + item + "\".";
			OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "SelectMenu::selectItem");
		}

		Ogre::DisplayString getSelectedItem()
		{
			if (mSelectionIndex == -1)
			{
				Ogre::String desc = "Menu \"" + getName() + "\" has no item selected.";
				OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "SelectMenu::getSelectedItem");
				return "";
			}
			else return mItems[mSelectionIndex];
		}

		int getSelectionIndex()
		{
			return mSelectionIndex;
		}

		void _cursorPressed(const Ogre::Vector2& cursorPos)
		{
			Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton();

			if (mExpanded)
			{
				if (mScrollHandle->isVisible())   // check for scrolling
				{
					Ogre::Vector2 co = Widget::cursorOffset(mScrollHandle, cursorPos);

					if (co.squaredLength() <= 81)
					{
						mDragging = true;
						mDragOffset = co.y;
						return;
					}
					else if (Widget::isCursorOver(mScrollTrack, cursorPos))
					{
						Ogre::Real newTop = mScrollHandle->getTop() + co.y;
						Ogre::Real lowerBoundary = mScrollTrack->getHeight() - mScrollHandle->getHeight();
						mScrollHandle->setTop(Ogre::Math::Clamp<int>((int)newTop, 0, (int)lowerBoundary));

						Ogre::Real scrollPercentage = Ogre::Math::Clamp<Ogre::Real>(newTop / lowerBoundary, 0, 1);
						setDisplayIndex((unsigned int)(scrollPercentage * (mItems.size() - mItemElements.size()) + 0.5));
						return;
					}
				}

				if (!isCursorOver(mExpandedBox, cursorPos, 3)) retract();
				else
				{
					Ogre::Real l = mItemElements.front()->_getDerivedLeft() * om.getViewportWidth() + 5;
					Ogre::Real t = mItemElements.front()->_getDerivedTop() * om.getViewportHeight() + 5;
					Ogre::Real r = l + mItemElements.back()->getWidth() - 10;
					Ogre::Real b = mItemElements.back()->_getDerivedTop() * om.getViewportHeight() +
						mItemElements.back()->getHeight() - 5;

					if (cursorPos.x >= l && cursorPos.x <= r && cursorPos.y >= t && cursorPos.y <= b)
					{
						if (mHighlightIndex != mSelectionIndex) selectItem(mHighlightIndex);
						retract();
					}
				}
			}
			else
			{
				if (mItems.size() < 2) return;   // don't waste time showing a menu if there's no choice

				if (isCursorOver(mSmallBox, cursorPos, 4))
				{
					mExpandedBox->show();
					mSmallBox->hide();

					// calculate how much vertical space we need
					Ogre::Real idealHeight = mItemsShown * (mSmallBox->getHeight() - 8) + 20;
					mExpandedBox->setHeight(idealHeight);
					mScrollTrack->setHeight(mExpandedBox->getHeight() - 20);

					mExpandedBox->setLeft(mSmallBox->getLeft() - 4);

					// if the expanded menu goes down off the screen, make it go up instead
					if (mSmallBox->_getDerivedTop() * om.getViewportHeight() + idealHeight > om.getViewportHeight())
					{
						mExpandedBox->setTop(mSmallBox->getTop() + mSmallBox->getHeight() - idealHeight + 3);
						// if we're in thick style, hide the caption because it will interfere with the expanded menu
						if (mTextArea->getHorizontalAlignment() == Ogre::GHA_CENTER) mTextArea->hide();
					}
					else mExpandedBox->setTop(mSmallBox->getTop() + 3);

					mExpanded = true;
					mHighlightIndex = mSelectionIndex;
					setDisplayIndex(mHighlightIndex);

					if (mItemsShown < mItems.size())  // update scrollbar position
					{
						mScrollHandle->show();
						Ogre::Real lowerBoundary = mScrollTrack->getHeight() - mScrollHandle->getHeight();
						mScrollHandle->setTop((int)(mDisplayIndex * lowerBoundary / (mItems.size() - mItemElements.size())));
					}
					else mScrollHandle->hide();
				}
			}
		}

		void _cursorReleased(const Ogre::Vector2& cursorPos)
		{
			mDragging = false;
		}

		void _cursorMoved(const Ogre::Vector2& cursorPos)
		{
			Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton();

			if (mExpanded)
			{
				if (mDragging)
				{
					Ogre::Vector2 co = Widget::cursorOffset(mScrollHandle, cursorPos);
					Ogre::Real newTop = mScrollHandle->getTop() + co.y - mDragOffset;
					Ogre::Real lowerBoundary = mScrollTrack->getHeight() - mScrollHandle->getHeight();
					mScrollHandle->setTop(Ogre::Math::Clamp<int>((int)newTop, 0, (int)lowerBoundary));

					Ogre::Real scrollPercentage = Ogre::Math::Clamp<Ogre::Real>(newTop / lowerBoundary, 0, 1);
					int newIndex = (int) (scrollPercentage * (mItems.size() - mItemElements.size()) + 0.5);
					if (newIndex != mDisplayIndex) setDisplayIndex(newIndex);
					return;
				}

				Ogre::Real l = mItemElements.front()->_getDerivedLeft() * om.getViewportWidth() + 5;
				Ogre::Real t = mItemElements.front()->_getDerivedTop() * om.getViewportHeight() + 5;
				Ogre::Real r = l + mItemElements.back()->getWidth() - 10;
				Ogre::Real b = mItemElements.back()->_getDerivedTop() * om.getViewportHeight() +
					mItemElements.back()->getHeight() - 5;

				if (cursorPos.x >= l && cursorPos.x <= r && cursorPos.y >= t && cursorPos.y <= b)
				{
					int newIndex = (int)(mDisplayIndex + (cursorPos.y - t) / (b - t) * mItemElements.size());
					if (mHighlightIndex != newIndex)
					{
						mHighlightIndex = newIndex;
						setDisplayIndex(mDisplayIndex);
					}
				}
			}
			else
			{
				if (isCursorOver(mSmallBox, cursorPos, 4))
				{
					mSmallBox->setMaterialName("SdkTrays/MiniTextBox/Over");
					mSmallBox->setBorderMaterialName("SdkTrays/MiniTextBox/Over");
					mCursorOver = true;
				}
				else
				{
					if (mCursorOver)
					{
						mSmallBox->setMaterialName("SdkTrays/MiniTextBox");
						mSmallBox->setBorderMaterialName("SdkTrays/MiniTextBox");
						mCursorOver = false;
					}
				}
			}
		}

		void _focusLost()
		{
			if (mExpandedBox->isVisible()) retract();
		}

	protected:

		/*-----------------------------------------------------------------------------
		| Internal method - sets which item goes at the top of the expanded menu.
		-----------------------------------------------------------------------------*/
		void setDisplayIndex(unsigned int index)
		{
			index = std::min<int>(index, mItems.size() - mItemElements.size());
			mDisplayIndex = index;
			Ogre::BorderPanelOverlayElement* ie;
			Ogre::TextAreaOverlayElement* ta;

			for (int i = 0; i < (int)mItemElements.size(); i++)
			{
				ie = mItemElements[i];
				ta = (Ogre::TextAreaOverlayElement*)ie->getChild(ie->getName() + "/MenuItemText");

				fitCaptionToArea(mItems[mDisplayIndex + i], ta, ie->getWidth() - 2 * ta->getLeft());

				if ((mDisplayIndex + i) == mHighlightIndex)
				{
					ie->setMaterialName("SdkTrays/MiniTextBox/Over");
					ie->setBorderMaterialName("SdkTrays/MiniTextBox/Over");
				}
				else
				{
					ie->setMaterialName("SdkTrays/MiniTextBox");
					ie->setBorderMaterialName("SdkTrays/MiniTextBox");
				}
			}
		}

		/*-----------------------------------------------------------------------------
		| Internal method - cleans up an expanded menu.
		-----------------------------------------------------------------------------*/
		void retract()
		{
			mDragging = false;
			mExpanded = false;
			mExpandedBox->hide();
			mTextArea->show();
			mSmallBox->show();
			mSmallBox->setMaterialName("SdkTrays/MiniTextBox");
			mSmallBox->setBorderMaterialName("SdkTrays/MiniTextBox");
		}

		Ogre::BorderPanelOverlayElement* mSmallBox;
		Ogre::BorderPanelOverlayElement* mExpandedBox;
		Ogre::TextAreaOverlayElement* mTextArea;
		Ogre::TextAreaOverlayElement* mSmallTextArea;
		Ogre::BorderPanelOverlayElement* mScrollTrack;
		Ogre::PanelOverlayElement* mScrollHandle;
		std::vector<Ogre::BorderPanelOverlayElement*> mItemElements;
		unsigned int mMaxItemsShown;
		unsigned int mItemsShown;
		bool mCursorOver;
		bool mExpanded;
		bool mFitToContents;
		bool mDragging;
		Ogre::StringVector mItems;
		int mSelectionIndex;
		int mHighlightIndex;
		int mDisplayIndex;
		Ogre::Real mDragOffset;
	};

	/*=============================================================================
	| Basic label widget.
	=============================================================================*/
	class Label : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		Label(const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width)
		{
			mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate("SdkTrays/Label", "BorderPanel", name);
			mTextArea = (Ogre::TextAreaOverlayElement*)((Ogre::OverlayContainer*)mElement)->getChild(getName() + "/LabelCaption");
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
            mTextArea->setCharHeight(mTextArea->getCharHeight() - 3);
#endif
			setCaption(caption);
			if (width <= 0) mFitToTray = true;
			else
			{
				mFitToTray = false;
				mElement->setWidth(width);
			}
		}

		const Ogre::DisplayString& getCaption()
		{
			return mTextArea->getCaption();
		}

		void setCaption(const Ogre::DisplayString& caption)
		{
			mTextArea->setCaption(caption);
		}

		void _cursorPressed(const Ogre::Vector2& cursorPos)
		{
			if (mListener && isCursorOver(mElement, cursorPos, 3)) mListener->labelHit(this);
		}

		bool _isFitToTray()
		{
			return mFitToTray;
		}

	protected:

		Ogre::TextAreaOverlayElement* mTextArea;
		bool mFitToTray;
	};

	/*=============================================================================
	| Basic separator widget.
	=============================================================================*/
	class Separator : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		Separator(const Ogre::String& name, Ogre::Real width)
		{
			mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate("SdkTrays/Separator", "Panel", name);
			if (width <= 0) mFitToTray = true;
			else
			{
				mFitToTray = false;
				mElement->setWidth(width);
			}
		}

		bool _isFitToTray()
		{
			return mFitToTray;
		}

	protected:

		bool mFitToTray;
	};

	/*=============================================================================
	| Basic slider widget.
	=============================================================================*/
	class Slider : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		Slider(const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width, Ogre::Real trackWidth,
			Ogre::Real valueBoxWidth, Ogre::Real minValue, Ogre::Real maxValue, unsigned int snaps)
			: mDragOffset(0.0f)
			, mValue(0.0f)
			, mMinValue(0.0f)
			, mMaxValue(0.0f)
			, mInterval(0.0f)
		{
			mDragging = false;
			mFitToContents = false;
			mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
				("SdkTrays/Slider", "BorderPanel", name);
			mElement->setWidth(width);
			Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)mElement;
			mTextArea = (Ogre::TextAreaOverlayElement*)c->getChild(getName() + "/SliderCaption");
			Ogre::OverlayContainer* valueBox = (Ogre::OverlayContainer*)c->getChild(getName() + "/SliderValueBox");
			valueBox->setWidth(valueBoxWidth);
			valueBox->setLeft(-(valueBoxWidth + 5));
			mValueTextArea = (Ogre::TextAreaOverlayElement*)valueBox->getChild(valueBox->getName() + "/SliderValueText");
			mTrack = (Ogre::BorderPanelOverlayElement*)c->getChild(getName() + "/SliderTrack");
			mHandle = (Ogre::PanelOverlayElement*)mTrack->getChild(mTrack->getName() + "/SliderHandle");
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
            mTextArea->setCharHeight(mTextArea->getCharHeight() - 3);
            mValueTextArea->setCharHeight(mValueTextArea->getCharHeight() - 3);
#endif
            
			if (trackWidth <= 0)  // tall style
			{
				mTrack->setWidth(width - 16);
			}
			else  // long style
			{
				if (width <= 0) mFitToContents = true;
				mElement->setHeight(34);
				mTextArea->setTop(10);
				valueBox->setTop(2);
				mTrack->setTop(-23);
				mTrack->setWidth(trackWidth);
				mTrack->setHorizontalAlignment(Ogre::GHA_RIGHT);
				mTrack->setLeft(-(trackWidth + valueBoxWidth + 5));
			}

			setCaption(caption);
			setRange(minValue, maxValue, snaps, false);
		}

		/*-----------------------------------------------------------------------------
		| Sets the minimum value, maximum value, and the number of snapping points.
		-----------------------------------------------------------------------------*/
		void setRange(Ogre::Real minValue, Ogre::Real maxValue, unsigned int snaps, bool notifyListener = true)
		{
			mMinValue = minValue;
			mMaxValue = maxValue;

			if (snaps <= 1 || mMinValue >= mMaxValue)
			{
				mInterval = 0;
				mHandle->hide();
				mValue = minValue;
				if (snaps == 1) mValueTextArea->setCaption(Ogre::StringConverter::toString(mMinValue));
				else mValueTextArea->setCaption("");
			}
			else
			{
				mHandle->show();
				mInterval = (maxValue - minValue) / (snaps - 1);
				setValue(minValue, notifyListener);
			}
		}

		const Ogre::DisplayString& getValueCaption()
		{
			return mValueTextArea->getCaption();
		}
		
		/*-----------------------------------------------------------------------------
		| You can use this method to manually format how the value is displayed.
		-----------------------------------------------------------------------------*/
		void setValueCaption(const Ogre::DisplayString& caption)
		{
			mValueTextArea->setCaption(caption);
		}

		void setValue(Ogre::Real value, bool notifyListener = true)
		{
			if (mInterval == 0) return;

			mValue = Ogre::Math::Clamp<Ogre::Real>(value, mMinValue, mMaxValue);

			setValueCaption(Ogre::StringConverter::toString(mValue));

			if (mListener && notifyListener) mListener->sliderMoved(this);

			if (!mDragging) mHandle->setLeft((int)((mValue - mMinValue) / (mMaxValue - mMinValue) *
				(mTrack->getWidth() - mHandle->getWidth())));
		}

		Ogre::Real getValue()
		{
			return mValue;
		}

		const Ogre::DisplayString& getCaption()
		{
			return mTextArea->getCaption();
		}

		void setCaption(const Ogre::DisplayString& caption)
		{
			mTextArea->setCaption(caption);

			if (mFitToContents) mElement->setWidth(getCaptionWidth(caption, mTextArea) +
				mValueTextArea->getParent()->getWidth() + mTrack->getWidth() + 26);
		}

		void _cursorPressed(const Ogre::Vector2& cursorPos)
		{
			if (!mHandle->isVisible()) return;

			Ogre::Vector2 co = Widget::cursorOffset(mHandle, cursorPos);

			if (co.squaredLength() <= 81)
			{
				mDragging = true;
				mDragOffset = co.x;
			}
			else if (Widget::isCursorOver(mTrack, cursorPos))
			{
				Ogre::Real newLeft = mHandle->getLeft() + co.x;
				Ogre::Real rightBoundary = mTrack->getWidth() - mHandle->getWidth();

				mHandle->setLeft(Ogre::Math::Clamp<int>((int)newLeft, 0, (int)rightBoundary));
				setValue(getSnappedValue(newLeft / rightBoundary));
			}
		}

		void _cursorReleased(const Ogre::Vector2& cursorPos)
		{
			if (mDragging)
			{
				mDragging = false;
				mHandle->setLeft((int)((mValue - mMinValue) / (mMaxValue - mMinValue) *
					(mTrack->getWidth() - mHandle->getWidth())));
			}
		}

		void _cursorMoved(const Ogre::Vector2& cursorPos)
		{
			if (mDragging)
			{
				Ogre::Vector2 co = Widget::cursorOffset(mHandle, cursorPos);
				Ogre::Real newLeft = mHandle->getLeft() + co.x - mDragOffset;
				Ogre::Real rightBoundary = mTrack->getWidth() - mHandle->getWidth();

				mHandle->setLeft(Ogre::Math::Clamp<int>((int)newLeft, 0, (int)rightBoundary));
				setValue(getSnappedValue(newLeft / rightBoundary));
			}
		}

		void _focusLost()
		{
			mDragging = false;
		}

	protected:

		/*-----------------------------------------------------------------------------
		| Internal method - given a percentage (from left to right), gets the
		| value of the nearest marker.
		-----------------------------------------------------------------------------*/
		Ogre::Real getSnappedValue(Ogre::Real percentage)
		{
			percentage = Ogre::Math::Clamp<Ogre::Real>(percentage, 0, 1);
			unsigned int whichMarker = (unsigned int) (percentage * (mMaxValue - mMinValue) / mInterval + 0.5);
			return whichMarker * mInterval + mMinValue;
		}

		Ogre::TextAreaOverlayElement* mTextArea;
		Ogre::TextAreaOverlayElement* mValueTextArea;
		Ogre::BorderPanelOverlayElement* mTrack;
		Ogre::PanelOverlayElement* mHandle;
		bool mDragging;
		bool mFitToContents;
		Ogre::Real mDragOffset;
		Ogre::Real mValue;
		Ogre::Real mMinValue;
		Ogre::Real mMaxValue;
		Ogre::Real mInterval;
	};

	/*=============================================================================
	| Basic parameters panel widget.
	=============================================================================*/
	class ParamsPanel : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		ParamsPanel(const Ogre::String& name, Ogre::Real width, unsigned int lines)
		{
			mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
				("SdkTrays/ParamsPanel", "BorderPanel", name);
			Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)mElement;
			mNamesArea = (Ogre::TextAreaOverlayElement*)c->getChild(getName() + "/ParamsPanelNames");
			mValuesArea = (Ogre::TextAreaOverlayElement*)c->getChild(getName() + "/ParamsPanelValues");
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
            mNamesArea->setCharHeight(mNamesArea->getCharHeight() - 3);
            mValuesArea->setCharHeight(mValuesArea->getCharHeight() - 3);
#endif
			mElement->setWidth(width);
			mElement->setHeight(mNamesArea->getTop() * 2 + lines * mNamesArea->getCharHeight());
		}

		void setAllParamNames(const Ogre::StringVector& paramNames)
		{
			mNames = paramNames;
			mValues.clear();
			mValues.resize(mNames.size(), "");
			mElement->setHeight(mNamesArea->getTop() * 2 + mNames.size() * mNamesArea->getCharHeight());
			updateText();
		}

		const Ogre::StringVector& getAllParamNames()
		{
			return mNames;
		}

		void setAllParamValues(const Ogre::StringVector& paramValues)
		{
			mValues = paramValues;
			mValues.resize(mNames.size(), "");
			updateText();
		}

		void setParamValue(const Ogre::DisplayString& paramName, const Ogre::DisplayString& paramValue)
		{
			for (unsigned int i = 0; i < mNames.size(); i++)
			{
				if (mNames[i] == paramName.asUTF8())
				{
					mValues[i] = paramValue.asUTF8();
					updateText();
					return;
				}
			}

			Ogre::String desc = "ParamsPanel \"" + getName() + "\" has no parameter \"" + paramName.asUTF8() + "\".";
			OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "ParamsPanel::setParamValue");
		}

		void setParamValue(unsigned int index, const Ogre::DisplayString& paramValue)
		{
			if (index >= mNames.size())
			{
				Ogre::String desc = "ParamsPanel \"" + getName() + "\" has no parameter at position " +
					Ogre::StringConverter::toString(index) + ".";
				OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "ParamsPanel::setParamValue");
			}

			mValues[index] = paramValue.asUTF8();
			updateText();
		}

		Ogre::DisplayString getParamValue(const Ogre::DisplayString& paramName)
		{
			for (unsigned int i = 0; i < mNames.size(); i++)
			{
				if (mNames[i] == paramName.asUTF8()) return mValues[i];
			}
			
			Ogre::String desc = "ParamsPanel \"" + getName() + "\" has no parameter \"" + paramName.asUTF8() + "\".";
			OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "ParamsPanel::getParamValue");
			return "";
		}

		Ogre::DisplayString getParamValue(unsigned int index)
		{
			if (index >= mNames.size())
			{
				Ogre::String desc = "ParamsPanel \"" + getName() + "\" has no parameter at position " +
					Ogre::StringConverter::toString(index) + ".";
				OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, desc, "ParamsPanel::getParamValue");
			}
			
			return mValues[index];
		}

		const Ogre::StringVector& getAllParamValues()
		{
			return mValues;
		}

	protected:

		/*-----------------------------------------------------------------------------
		| Internal method - updates text areas based on name and value lists.
		-----------------------------------------------------------------------------*/
		void updateText()
		{
			Ogre::DisplayString namesDS;
			Ogre::DisplayString valuesDS;

			for (unsigned int i = 0; i < mNames.size(); i++)
			{
				namesDS.append(mNames[i] + ":\n");
				valuesDS.append(mValues[i] + "\n");
			}

			mNamesArea->setCaption(namesDS);
			mValuesArea->setCaption(valuesDS);
		}

		Ogre::TextAreaOverlayElement* mNamesArea;
		Ogre::TextAreaOverlayElement* mValuesArea;
		Ogre::StringVector mNames;
		Ogre::StringVector mValues;
	};

	/*=============================================================================
	| Basic check box widget.
	=============================================================================*/
	class CheckBox : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		CheckBox(const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width)
		{
			mCursorOver = false;
			mFitToContents = width <= 0;
			mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
				("SdkTrays/CheckBox", "BorderPanel", name);
			Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)mElement;
			mTextArea = (Ogre::TextAreaOverlayElement*)c->getChild(getName() + "/CheckBoxCaption");
			mSquare = (Ogre::BorderPanelOverlayElement*)c->getChild(getName() + "/CheckBoxSquare");
			mX = mSquare->getChild(mSquare->getName() + "/CheckBoxX");
			mX->hide();
			mElement->setWidth(width);
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
            mTextArea->setCharHeight(mTextArea->getCharHeight() - 3);
#endif
			setCaption(caption);
		}

		const Ogre::DisplayString& getCaption()
		{
			return mTextArea->getCaption();
		}

		void setCaption(const Ogre::DisplayString& caption)
		{
			mTextArea->setCaption(caption);
			if (mFitToContents) mElement->setWidth(getCaptionWidth(caption, mTextArea) + mSquare->getWidth() + 23);
		}

		bool isChecked()
		{
			return mX->isVisible();
		}

		void setChecked(bool checked, bool notifyListener = true)
		{
			if (checked) mX->show();
			else mX->hide();
			if (mListener && notifyListener) mListener->checkBoxToggled(this);
		}

		void toggle(bool notifyListener = true)
		{
			setChecked(!isChecked(), notifyListener);
		}

		void _cursorPressed(const Ogre::Vector2& cursorPos)
		{
			if (mCursorOver && mListener) toggle();
		}

		void _cursorMoved(const Ogre::Vector2& cursorPos)
		{
			if (isCursorOver(mSquare, cursorPos, 5))
			{
				if (!mCursorOver)
				{
					mCursorOver = true;
					mSquare->setMaterialName("SdkTrays/MiniTextBox/Over");
					mSquare->setBorderMaterialName("SdkTrays/MiniTextBox/Over");
				}
			}
			else
			{
				if (mCursorOver)
				{
					mCursorOver = false;
					mSquare->setMaterialName("SdkTrays/MiniTextBox");
					mSquare->setBorderMaterialName("SdkTrays/MiniTextBox");
				}
			}
		}

		void _focusLost()
		{
			mSquare->setMaterialName("SdkTrays/MiniTextBox");
			mSquare->setBorderMaterialName("SdkTrays/MiniTextBox");
			mCursorOver = false;
		}

	protected:

		Ogre::TextAreaOverlayElement* mTextArea;
		Ogre::BorderPanelOverlayElement* mSquare;
		Ogre::OverlayElement* mX;
		bool mFitToContents;
		bool mCursorOver;
	};

	/*=============================================================================
	| Custom, decorative widget created from a template.
	=============================================================================*/
	class DecorWidget : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		DecorWidget(const Ogre::String& name, const Ogre::String& templateName)
		{
			mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate(templateName, "", name);
		}
	};

	/*=============================================================================
	| Basic progress bar widget.
	=============================================================================*/
	class ProgressBar : public Widget
	{
	public:

		// Do not instantiate any widgets directly. Use SdkTrayManager.
		ProgressBar(const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width, Ogre::Real commentBoxWidth)
			: mProgress(0.0f)
		{
			mElement = Ogre::OverlayManager::getSingleton().createOverlayElementFromTemplate
				("SdkTrays/ProgressBar", "BorderPanel", name);
			mElement->setWidth(width);
			Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)mElement;
			mTextArea = (Ogre::TextAreaOverlayElement*)c->getChild(getName() + "/ProgressCaption");
			Ogre::OverlayContainer* commentBox = (Ogre::OverlayContainer*)c->getChild(getName() + "/ProgressCommentBox");
			commentBox->setWidth(commentBoxWidth);
			commentBox->setLeft(-(commentBoxWidth + 5));
			mCommentTextArea = (Ogre::TextAreaOverlayElement*)commentBox->getChild(commentBox->getName() + "/ProgressCommentText");
			mMeter = c->getChild(getName() + "/ProgressMeter");
			mMeter->setWidth(width - 10);
			mFill = ((Ogre::OverlayContainer*)mMeter)->getChild(mMeter->getName() + "/ProgressFill");
			setCaption(caption);
		}

		/*-----------------------------------------------------------------------------
		| Sets the progress as a percentage.
		-----------------------------------------------------------------------------*/
		void setProgress(Ogre::Real progress)
		{
			mProgress = Ogre::Math::Clamp<Ogre::Real>(progress, 0, 1);
			mFill->setWidth(std::max<int>((int)mFill->getHeight(), (int)(mProgress * (mMeter->getWidth() - 2 * mFill->getLeft()))));
		}

		/*-----------------------------------------------------------------------------
		| Gets the progress as a percentage.
		-----------------------------------------------------------------------------*/
		Ogre::Real getProgress()
		{
			return mProgress;
		}

		const Ogre::DisplayString& getCaption()
		{
			return mTextArea->getCaption();
		}

		void setCaption(const Ogre::DisplayString& caption)
		{
			mTextArea->setCaption(caption);
		}

		const Ogre::DisplayString& getComment()
		{
			return mCommentTextArea->getCaption();
		}

		void setComment(const Ogre::DisplayString& comment)
		{
			mCommentTextArea->setCaption(comment);
		}


	protected:

		Ogre::TextAreaOverlayElement* mTextArea;
		Ogre::TextAreaOverlayElement* mCommentTextArea;
		Ogre::OverlayElement* mMeter;
		Ogre::OverlayElement* mFill;
		Ogre::Real mProgress;
	};

	/*=============================================================================
	| Main class to manage a cursor, backdrop, trays and widgets.
	=============================================================================*/
	class SdkTrayManager : public SdkTrayListener, public Ogre::ResourceGroupListener
    {
    public:

		/*-----------------------------------------------------------------------------
		| Creates backdrop, cursor, and trays.
		-----------------------------------------------------------------------------*/
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
		SdkTrayManager(const Ogre::String& name, Ogre::RenderWindow* window, OIS::MultiTouch* mouse, SdkTrayListener* listener = 0) :
#else
		SdkTrayManager(const Ogre::String& name, Ogre::RenderWindow* window, OIS::Mouse* mouse, SdkTrayListener* listener = 0) :
#endif
		  mName(name), mWindow(window), mMouse(mouse), mWidgetDeathRow(), mListener(listener), mWidgetPadding(8),
                mWidgetSpacing(2), mTrayPadding(0), mTrayDrag(false), mExpandedMenu(0), mDialog(0), mOk(0), mYes(0),
                mNo(0), mCursorWasVisible(false), mFpsLabel(0), mStatsPanel(0), mLogo(0), mLoadBar(0),
				mGroupInitProportion(0.0f), mGroupLoadProportion(0.0f), mLoadInc(0.0f)
		{
			Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton();

			Ogre::String nameBase = mName + "/";
			std::replace(nameBase.begin(), nameBase.end(), ' ', '_');

			// create overlay layers for everything

			mBackdropLayer = om.create(nameBase + "BackdropLayer");
			mTraysLayer = om.create(nameBase + "WidgetsLayer");
			mPriorityLayer = om.create(nameBase + "PriorityLayer");
			mCursorLayer = om.create(nameBase + "CursorLayer");
			mBackdropLayer->setZOrder(100);
			mTraysLayer->setZOrder(200);
			mPriorityLayer->setZOrder(300);
			mCursorLayer->setZOrder(400);

			// make backdrop and cursor overlay containers

			mCursor = (Ogre::OverlayContainer*)om.createOverlayElementFromTemplate("SdkTrays/Cursor", "Panel", nameBase + "Cursor");
			mCursorLayer->add2D(mCursor);
			mBackdrop = (Ogre::OverlayContainer*)om.createOverlayElement("Panel", nameBase + "Backdrop");
			mBackdropLayer->add2D(mBackdrop);
			mDialogShade = (Ogre::OverlayContainer*)om.createOverlayElement("Panel", nameBase + "DialogShade");
			mDialogShade->setMaterialName("SdkTrays/Shade");
			mDialogShade->hide();
			mPriorityLayer->add2D(mDialogShade);

			Ogre::String trayNames[] =
			{ "TopLeft", "Top", "TopRight", "Left", "Center", "Right", "BottomLeft", "Bottom", "BottomRight" };

			for (unsigned int i = 0; i < 9; i++)    // make the real trays
			{
				mTrays[i] = (Ogre::OverlayContainer*)om.createOverlayElementFromTemplate
					("SdkTrays/Tray", "BorderPanel", nameBase + trayNames[i] + "Tray");
				mTraysLayer->add2D(mTrays[i]);

				mTrayWidgetAlign[i] = Ogre::GHA_CENTER;

				// align trays based on location
				if (i == TL_TOP || i == TL_CENTER || i == TL_BOTTOM) mTrays[i]->setHorizontalAlignment(Ogre::GHA_CENTER);
				if (i == TL_LEFT || i == TL_CENTER || i == TL_RIGHT) mTrays[i]->setVerticalAlignment(Ogre::GVA_CENTER);
				if (i == TL_TOPRIGHT || i == TL_RIGHT || i == TL_BOTTOMRIGHT) mTrays[i]->setHorizontalAlignment(Ogre::GHA_RIGHT);
				if (i == TL_BOTTOMLEFT || i == TL_BOTTOM || i == TL_BOTTOMRIGHT) mTrays[i]->setVerticalAlignment(Ogre::GVA_BOTTOM);
			}

			// create the null tray for free-floating widgets
			mTrays[9] = (Ogre::OverlayContainer*)om.createOverlayElement("Panel", nameBase + "NullTray");
			mTrayWidgetAlign[9] = Ogre::GHA_LEFT;
			mTraysLayer->add2D(mTrays[9]);

			adjustTrays();
			
			showTrays();
			showCursor();
		}

		/*-----------------------------------------------------------------------------
		| Destroys background, cursor, widgets, and trays.
		-----------------------------------------------------------------------------*/
		virtual ~SdkTrayManager()
		{
			Ogre::OverlayManager& om = Ogre::OverlayManager::getSingleton();

			destroyAllWidgets();

			for (unsigned int i = 0; i < mWidgetDeathRow.size(); i++)   // delete widgets queued for destruction
			{
				delete mWidgetDeathRow[i];
			}
			mWidgetDeathRow.clear();

			om.destroy(mBackdropLayer);
			om.destroy(mTraysLayer);
			om.destroy(mPriorityLayer);
			om.destroy(mCursorLayer);

			closeDialog();
			hideLoadingBar();

			Widget::nukeOverlayElement(mBackdrop);
			Widget::nukeOverlayElement(mCursor);
			Widget::nukeOverlayElement(mDialogShade);

			for (unsigned int i = 0; i < 10; i++)
			{
				Widget::nukeOverlayElement(mTrays[i]);
			}
		}

		/*-----------------------------------------------------------------------------
		| Converts a 2D screen coordinate (in pixels) to a 3D ray into the scene.
		-----------------------------------------------------------------------------*/
		static Ogre::Ray screenToScene(Ogre::Camera* cam, const Ogre::Vector2& pt)
		{
			return cam->getCameraToViewportRay(pt.x, pt.y);
		}

		/*-----------------------------------------------------------------------------
		| Converts a 3D scene position to a 2D screen coordinate (in pixels).
		-----------------------------------------------------------------------------*/
		static Ogre::Vector2 sceneToScreen(Ogre::Camera* cam, const Ogre::Vector3& pt)
		{
			Ogre::Vector3 result = cam->getProjectionMatrix() * cam->getViewMatrix() * pt;
			return Ogre::Vector2((result.x + 1) / 2, -(result.y + 1) / 2);
		}

		// these methods get the underlying overlays and overlay elements

		Ogre::OverlayContainer* getTrayContainer(TrayLocation trayLoc) { return mTrays[trayLoc]; }
		Ogre::Overlay* getBackdropLayer() { return mBackdropLayer; }
		Ogre::Overlay* getTraysLayer() { return mTraysLayer; }
		Ogre::Overlay* getCursorLayer() { return mCursorLayer; }
		Ogre::OverlayContainer* getBackdropContainer() { return mBackdrop; }
		Ogre::OverlayContainer* getCursorContainer() { return mCursor; }
		Ogre::OverlayElement* getCursorImage() { return mCursor->getChild(mCursor->getName() + "/CursorImage"); }

		void setListener(SdkTrayListener* listener)
		{
			mListener = listener;
		}

		SdkTrayListener* getListener()
		{
			return mListener;
		}

		void showAll()
		{
			showBackdrop();
			showTrays();
			showCursor();
		}

		void hideAll()
		{
			hideBackdrop();
			hideTrays();
			hideCursor();
		}

		/*-----------------------------------------------------------------------------
		| Displays specified material on backdrop, or the last material used if
		| none specified. Good for pause menus like in the browser.
		-----------------------------------------------------------------------------*/
		void showBackdrop(const Ogre::String& materialName = Ogre::StringUtil::BLANK)
		{
			if (materialName != Ogre::StringUtil::BLANK) mBackdrop->setMaterialName(materialName);
			mBackdropLayer->show();
		}

		void hideBackdrop()
		{
			mBackdropLayer->hide();
		}

		/*-----------------------------------------------------------------------------
		| Displays specified material on cursor, or the last material used if
		| none specified. Used to change cursor type.
		-----------------------------------------------------------------------------*/
		void showCursor(const Ogre::String& materialName = Ogre::StringUtil::BLANK)
		{
			if (materialName != Ogre::StringUtil::BLANK) getCursorImage()->setMaterialName(materialName);

			if (!mCursorLayer->isVisible())
			{
				mCursorLayer->show();
				refreshCursor();
			}
		}

		void hideCursor()
		{
			mCursorLayer->hide();

			// give widgets a chance to reset in case they're in the middle of something
			for (unsigned int i = 0; i < 10; i++)
			{
				for (unsigned int j = 0; j < mWidgets[i].size(); j++)
				{
					mWidgets[i][j]->_focusLost();
				}
			}

			setExpandedMenu(0);
		}

		/*-----------------------------------------------------------------------------
		| Updates cursor position based on unbuffered mouse state. This is necessary
		| because if the tray manager has been cut off from mouse events for a time,
		| the cursor position will be out of date.
		-----------------------------------------------------------------------------*/
		void refreshCursor()
		{
#if OGRE_NO_VIEWPORT_ORIENTATIONMODE == 0
            // TODO:
            // the position should be based on the orientation, for now simply return
            return;
#endif
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
            std::vector<OIS::MultiTouchState> states = mMouse->getMultiTouchStates();
            if(states.size() > 0)
                mCursor->setPosition(states[0].X.abs, states[0].Y.abs);
#else
			mCursor->setPosition(mMouse->getMouseState().X.abs, mMouse->getMouseState().Y.abs);
#endif
		}

		void showTrays()
		{
			mTraysLayer->show();
			mPriorityLayer->show();
		}

		void hideTrays()
		{
			mTraysLayer->hide();
			mPriorityLayer->hide();

			// give widgets a chance to reset in case they're in the middle of something
			for (unsigned int i = 0; i < 10; i++)
			{
				for (unsigned int j = 0; j < mWidgets[i].size(); j++)
				{
					mWidgets[i][j]->_focusLost();
				}
			}

			setExpandedMenu(0);
		}

		bool isCursorVisible() { return mCursorLayer->isVisible(); }
		bool isBackdropVisible() { return mBackdropLayer->isVisible(); }
		bool areTraysVisible() { return mTraysLayer->isVisible(); }

		/*-----------------------------------------------------------------------------
		| Sets horizontal alignment of a tray's contents.
		-----------------------------------------------------------------------------*/
		void setTrayWidgetAlignment(TrayLocation trayLoc, Ogre::GuiHorizontalAlignment gha)
		{
			mTrayWidgetAlign[trayLoc] = gha;

			for (unsigned int i = 0; i < mWidgets[trayLoc].size(); i++)
			{
				mWidgets[trayLoc][i]->getOverlayElement()->setHorizontalAlignment(gha);
			}
		}

		// padding and spacing methods

		void setWidgetPadding(Ogre::Real padding)
		{
			mWidgetPadding = std::max<int>((int)padding, 0);
			adjustTrays();
		}

		void setWidgetSpacing(Ogre::Real spacing)
		{
			mWidgetSpacing = std::max<int>((int)spacing, 0);
			adjustTrays();
		}
		void setTrayPadding(Ogre::Real padding)
		{
			mTrayPadding = std::max<int>((int)padding, 0);
			adjustTrays();
		}

		virtual Ogre::Real getWidgetPadding() const { return mWidgetPadding; }
		virtual Ogre::Real getWidgetSpacing() const { return mWidgetSpacing; }
		virtual Ogre::Real getTrayPadding() const { return mTrayPadding; }

		/*-----------------------------------------------------------------------------
		| Fits trays to their contents and snaps them to their anchor locations.
		-----------------------------------------------------------------------------*/
		virtual void adjustTrays()
		{
			for (unsigned int i = 0; i < 9; i++)   // resizes and hides trays if necessary
			{
				Ogre::Real trayWidth = 0;
				Ogre::Real trayHeight = mWidgetPadding;
				std::vector<Ogre::OverlayElement*> labelsAndSeps;

				if (mWidgets[i].empty())   // hide tray if empty
				{
					mTrays[i]->hide();
					continue;
				}
				else mTrays[i]->show();

				// arrange widgets and calculate final tray size and position
				for (unsigned int j = 0; j < mWidgets[i].size(); j++)
				{
					Ogre::OverlayElement* e = mWidgets[i][j]->getOverlayElement();

					if (j != 0) trayHeight += mWidgetSpacing;   // don't space first widget

					e->setVerticalAlignment(Ogre::GVA_TOP);
					e->setTop(trayHeight);

					switch (e->getHorizontalAlignment())
					{
					case Ogre::GHA_LEFT:
						e->setLeft(mWidgetPadding);
						break;
					case Ogre::GHA_RIGHT:
						e->setLeft(-(e->getWidth() + mWidgetPadding));
						break;
					default:
						e->setLeft(-(e->getWidth() / 2));
					}

					// prevents some weird texture filtering problems (just some)
					e->setPosition((int)e->getLeft(), (int)e->getTop());
					e->setDimensions((int)e->getWidth(), (int)e->getHeight());

					trayHeight += e->getHeight();

					Label* l = dynamic_cast<Label*>(mWidgets[i][j]);
					if (l && l->_isFitToTray())
					{
						labelsAndSeps.push_back(e);
						continue;
					}
					Separator* s = dynamic_cast<Separator*>(mWidgets[i][j]);
					if (s && s->_isFitToTray()) 
					{
						labelsAndSeps.push_back(e);
						continue;
					}

					if (e->getWidth() > trayWidth) trayWidth = e->getWidth();
				}

				// add paddings and resize trays
				mTrays[i]->setWidth(trayWidth + 2 * mWidgetPadding);
				mTrays[i]->setHeight(trayHeight + mWidgetPadding);

				for (unsigned int j = 0; j < labelsAndSeps.size(); j++)
				{
					labelsAndSeps[j]->setWidth((int)trayWidth);
					labelsAndSeps[j]->setLeft(-(int)(trayWidth / 2));
				}
			}

			for (unsigned int i = 0; i < 9; i++)    // snap trays to anchors
			{
				if (i == TL_TOPLEFT || i == TL_LEFT || i == TL_BOTTOMLEFT)
					mTrays[i]->setLeft(mTrayPadding);
				if (i == TL_TOP || i == TL_CENTER || i == TL_BOTTOM)
					mTrays[i]->setLeft(-mTrays[i]->getWidth() / 2);
				if (i == TL_TOPRIGHT || i == TL_RIGHT || i == TL_BOTTOMRIGHT)
					mTrays[i]->setLeft(-(mTrays[i]->getWidth() + mTrayPadding));

				if (i == TL_TOPLEFT || i == TL_TOP || i == TL_TOPRIGHT)
					mTrays[i]->setTop(mTrayPadding);
				if (i == TL_LEFT || i == TL_CENTER || i == TL_RIGHT)
					mTrays[i]->setTop(-mTrays[i]->getHeight() / 2);
				if (i == TL_BOTTOMLEFT || i == TL_BOTTOM || i == TL_BOTTOMRIGHT)
					mTrays[i]->setTop(-mTrays[i]->getHeight() - mTrayPadding);

				// prevents some weird texture filtering problems (just some)
				mTrays[i]->setPosition((int)mTrays[i]->getLeft(), (int)mTrays[i]->getTop());
				mTrays[i]->setDimensions((int)mTrays[i]->getWidth(), (int)mTrays[i]->getHeight());
			}
		}

		/*-----------------------------------------------------------------------------
		| Returns a 3D ray into the scene that is directly underneath the cursor.
		-----------------------------------------------------------------------------*/
		Ogre::Ray getCursorRay(Ogre::Camera* cam)
		{
			return screenToScene(cam, Ogre::Vector2(mCursor->_getLeft(), mCursor->_getTop()));
		}

		Button* createButton(TrayLocation trayLoc, const Ogre::String& name, const Ogre::String& caption, Ogre::Real width = 0)
		{
			Button* b = new Button(name, caption, width);
			moveWidgetToTray(b, trayLoc);
			b->_assignListener(mListener);
			return b;
		}

		TextBox* createTextBox(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption,
			Ogre::Real width, Ogre::Real height)
		{
			TextBox* tb = new TextBox(name, caption, width, height);
			moveWidgetToTray(tb, trayLoc);
			tb->_assignListener(mListener);
			return tb;
		}

		SelectMenu* createThickSelectMenu(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption,
			Ogre::Real width, unsigned int maxItemsShown, const Ogre::StringVector& items = Ogre::StringVector())
		{
			SelectMenu* sm = new SelectMenu(name, caption, width, 0, maxItemsShown);
			moveWidgetToTray(sm, trayLoc);
			sm->_assignListener(mListener);
			if (!items.empty()) sm->setItems(items);
			return sm;
		}

		SelectMenu* createLongSelectMenu(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption,
			Ogre::Real width, Ogre::Real boxWidth, unsigned int maxItemsShown, const Ogre::StringVector& items = Ogre::StringVector())
		{
			SelectMenu* sm = new SelectMenu(name, caption, width, boxWidth, maxItemsShown);
			moveWidgetToTray(sm, trayLoc);
			sm->_assignListener(mListener);
			if (!items.empty()) sm->setItems(items);
			return sm;
		}

		SelectMenu* createLongSelectMenu(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption,
			Ogre::Real boxWidth, unsigned int maxItemsShown, const Ogre::StringVector& items = Ogre::StringVector())
		{
			return createLongSelectMenu(trayLoc, name, caption, 0, boxWidth, maxItemsShown, items);
		}

		Label* createLabel(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width = 0)
		{
			Label* l = new Label(name, caption, width);
			moveWidgetToTray(l, trayLoc);
			l->_assignListener(mListener);
			return l;
		}

		Separator* createSeparator(TrayLocation trayLoc, const Ogre::String& name, Ogre::Real width = 0)
		{
			Separator* s = new Separator(name, width);
			moveWidgetToTray(s, trayLoc);
			return s;
		}

		Slider* createThickSlider(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption,
			Ogre::Real width, Ogre::Real valueBoxWidth, Ogre::Real minValue, Ogre::Real maxValue, unsigned int snaps)
		{
			Slider* s = new Slider(name, caption, width, 0, valueBoxWidth, minValue, maxValue, snaps);
			moveWidgetToTray(s, trayLoc);
			s->_assignListener(mListener);
			return s;
		}

		Slider* createLongSlider(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption, Ogre::Real width,
			Ogre::Real trackWidth, Ogre::Real valueBoxWidth, Ogre::Real minValue, Ogre::Real maxValue, unsigned int snaps)
		{
			if (trackWidth <= 0) trackWidth = 1;
			Slider* s = new Slider(name, caption, width, trackWidth, valueBoxWidth, minValue, maxValue, snaps);
			moveWidgetToTray(s, trayLoc);
			s->_assignListener(mListener);
			return s;
		}

		Slider* createLongSlider(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption,
			Ogre::Real trackWidth, Ogre::Real valueBoxWidth, Ogre::Real minValue, Ogre::Real maxValue, unsigned int snaps)
		{
			return createLongSlider(trayLoc, name, caption, 0, trackWidth, valueBoxWidth, minValue, maxValue, snaps);
		}

		ParamsPanel* createParamsPanel(TrayLocation trayLoc, const Ogre::String& name, Ogre::Real width, unsigned int lines)
		{
			ParamsPanel* pp = new ParamsPanel(name, width, lines);
			moveWidgetToTray(pp, trayLoc);
			return pp;
		}

		ParamsPanel* createParamsPanel(TrayLocation trayLoc, const Ogre::String& name, Ogre::Real width,
			const Ogre::StringVector& paramNames)
		{
			ParamsPanel* pp = new ParamsPanel(name, width, paramNames.size());
			pp->setAllParamNames(paramNames);
			moveWidgetToTray(pp, trayLoc);
			return pp;
		}

		CheckBox* createCheckBox(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption,
			Ogre::Real width = 0)
		{
			CheckBox* cb = new CheckBox(name, caption, width);
			moveWidgetToTray(cb, trayLoc);
			cb->_assignListener(mListener);
			return cb;
		}

		DecorWidget* createDecorWidget(TrayLocation trayLoc, const Ogre::String& name, const Ogre::String& templateName)
		{
			DecorWidget* dw = new DecorWidget(name, templateName);
			moveWidgetToTray(dw, trayLoc);
			return dw;
		}

		ProgressBar* createProgressBar(TrayLocation trayLoc, const Ogre::String& name, const Ogre::DisplayString& caption,
			Ogre::Real width, Ogre::Real commentBoxWidth)
		{
			ProgressBar* pb = new ProgressBar(name, caption, width, commentBoxWidth);
			moveWidgetToTray(pb, trayLoc);
			return pb;
		}

		/*-----------------------------------------------------------------------------
		| Shows frame statistics widget set in the specified location.
		-----------------------------------------------------------------------------*/
		void showFrameStats(TrayLocation trayLoc, int place = -1)
		{
			if (!areFrameStatsVisible())
			{
				Ogre::StringVector stats;
				stats.push_back("Average FPS");
				stats.push_back("Best FPS");
				stats.push_back("Worst FPS");
				stats.push_back("Triangles");
				stats.push_back("Batches");

				mFpsLabel = createLabel(TL_NONE, mName + "/FpsLabel", "FPS:", 180);
				mFpsLabel->_assignListener(this);
				mStatsPanel = createParamsPanel(TL_NONE, mName + "/StatsPanel", 180, stats);
			}

			moveWidgetToTray(mFpsLabel, trayLoc, place);
			moveWidgetToTray(mStatsPanel, trayLoc, locateWidgetInTray(mFpsLabel) + 1);
		}

		/*-----------------------------------------------------------------------------
		| Hides frame statistics widget set.
		-----------------------------------------------------------------------------*/
		void hideFrameStats()
		{
			if (areFrameStatsVisible())
			{
				destroyWidget(mFpsLabel);
				destroyWidget(mStatsPanel);
				mFpsLabel = 0;
				mStatsPanel = 0;
			}
		}

		bool areFrameStatsVisible()
		{
			return mFpsLabel != 0;
		}

		/*-----------------------------------------------------------------------------
		| Toggles visibility of advanced statistics.
		-----------------------------------------------------------------------------*/
		void toggleAdvancedFrameStats()
		{
			if (mFpsLabel) labelHit(mFpsLabel);
		}

		/*-----------------------------------------------------------------------------
		| Shows logo in the specified location.
		-----------------------------------------------------------------------------*/
		void showLogo(TrayLocation trayLoc, int place = -1)
		{
			if (!isLogoVisible()) mLogo = createDecorWidget(TL_NONE, mName + "/Logo", "SdkTrays/Logo");
			moveWidgetToTray(mLogo, trayLoc, place);
		}

		void hideLogo()
		{
			if (isLogoVisible())
			{
				destroyWidget(mLogo);
				mLogo = 0;
			}
		}

		bool isLogoVisible()
		{
			return mLogo != 0;
		}

		/*-----------------------------------------------------------------------------
		| Shows loading bar. Also takes job settings: the number of resource groups
		| to be initialised, the number of resource groups to be loaded, and the
		| proportion of the job that will be taken up by initialisation. Usually,
		| script parsing takes up most time, so the default value is 0.7.
		-----------------------------------------------------------------------------*/
		void showLoadingBar(unsigned int numGroupsInit = 1, unsigned int numGroupsLoad = 1,
			Ogre::Real initProportion = 0.7)
		{
			if (mDialog) closeDialog();
			if (mLoadBar) hideLoadingBar();

			mLoadBar = new ProgressBar(mName + "/LoadingBar", "Loading...", 400, 308);
			Ogre::OverlayElement* e = mLoadBar->getOverlayElement();
			mDialogShade->addChild(e);
			e->setVerticalAlignment(Ogre::GVA_CENTER);
			e->setLeft(-(e->getWidth() / 2));
			e->setTop(-(e->getHeight() / 2));

			Ogre::ResourceGroupManager::getSingleton().addResourceGroupListener(this);
			mCursorWasVisible = isCursorVisible();
			hideCursor();
			mDialogShade->show();

			// calculate the proportion of job required to init/load one group

			if (numGroupsInit == 0 && numGroupsLoad != 0)
			{
				mGroupInitProportion = 0;
				mGroupLoadProportion = 1;
			}
			else if (numGroupsLoad == 0 && numGroupsInit != 0)
			{
				mGroupLoadProportion = 0;
				if (numGroupsInit != 0) mGroupInitProportion = 1;
			}
			else if (numGroupsInit == 0 && numGroupsLoad == 0)
			{
				mGroupInitProportion = 0;
				mGroupLoadProportion = 0;
			}
			else
			{
				mGroupInitProportion = initProportion / numGroupsInit;
				mGroupLoadProportion = (1 - initProportion) / numGroupsLoad;
			}
		}

		void hideLoadingBar()
		{
			if (mLoadBar)
			{
				mLoadBar->cleanup();
				delete mLoadBar;
				mLoadBar = 0;

				Ogre::ResourceGroupManager::getSingleton().removeResourceGroupListener(this);
				if (mCursorWasVisible) showCursor();
				mDialogShade->hide();
			}
		}

		bool isLoadingBarVisible()
		{
			return mLoadBar != 0;
		}

		/*-----------------------------------------------------------------------------
		| Pops up a message dialog with an OK button.
		-----------------------------------------------------------------------------*/
		void showOkDialog(const Ogre::DisplayString& caption, const Ogre::DisplayString& message)
		{
			if (mLoadBar) hideLoadingBar();

			Ogre::OverlayElement* e;

			if (mDialog)
			{
				mDialog->setCaption(caption);
				mDialog->setText(message);

				if (mOk) return;
				else
				{
					mYes->cleanup();
					mNo->cleanup();
					delete mYes;
					delete mNo;
					mYes = 0;
					mNo = 0;
				}
			}
			else
			{
				// give widgets a chance to reset in case they're in the middle of something
				for (unsigned int i = 0; i < 10; i++)
				{
					for (unsigned int j = 0; j < mWidgets[i].size(); j++)
					{
						mWidgets[i][j]->_focusLost();
					}
				}

				mDialogShade->show();

				mDialog = new TextBox(mName + "/DialogBox", caption, 300, 208);
				mDialog->setText(message);
				e = mDialog->getOverlayElement();
				mDialogShade->addChild(e);
				e->setVerticalAlignment(Ogre::GVA_CENTER);
				e->setLeft(-(e->getWidth() / 2));
				e->setTop(-(e->getHeight() / 2));

				mCursorWasVisible = isCursorVisible();
				showCursor();
			}

			mOk = new Button(mName + "/OkButton", "OK", 60);
			mOk->_assignListener(this);
			e = mOk->getOverlayElement();
			mDialogShade->addChild(e);
			e->setVerticalAlignment(Ogre::GVA_CENTER);
			e->setLeft(-(e->getWidth() / 2));
			e->setTop(mDialog->getOverlayElement()->getTop() + mDialog->getOverlayElement()->getHeight() + 5);
		}

		/*-----------------------------------------------------------------------------
		| Pops up a question dialog with Yes and No buttons.
		-----------------------------------------------------------------------------*/
		void showYesNoDialog(const Ogre::DisplayString& caption, const Ogre::DisplayString& question)
		{
			if (mLoadBar) hideLoadingBar();

			Ogre::OverlayElement* e;

			if (mDialog)
			{
				mDialog->setCaption(caption);
				mDialog->setText(question);

				if (mOk)
				{
					mOk->cleanup();
					delete mOk;
					mOk = 0;
				}
				else return;
			}
			else
			{
				// give widgets a chance to reset in case they're in the middle of something
				for (unsigned int i = 0; i < 10; i++)
				{
					for (unsigned int j = 0; j < mWidgets[i].size(); j++)
					{
						mWidgets[i][j]->_focusLost();
					}
				}

				mDialogShade->show();

				mDialog = new TextBox(mName + "/DialogBox", caption, 300, 208);
				mDialog->setText(question);
				e = mDialog->getOverlayElement();
				mDialogShade->addChild(e);
				e->setVerticalAlignment(Ogre::GVA_CENTER);
				e->setLeft(-(e->getWidth() / 2));
				e->setTop(-(e->getHeight() / 2));

				mCursorWasVisible = isCursorVisible();
				showCursor();
			}

			mYes = new Button(mName + "/YesButton", "Yes", 58);
			mYes->_assignListener(this);
			e = mYes->getOverlayElement();
			mDialogShade->addChild(e);
			e->setVerticalAlignment(Ogre::GVA_CENTER);
			e->setLeft(-(e->getWidth() + 2));
			e->setTop(mDialog->getOverlayElement()->getTop() + mDialog->getOverlayElement()->getHeight() + 5);

			mNo = new Button(mName + "/NoButton", "No", 50);
			mNo->_assignListener(this);
			e = mNo->getOverlayElement();
			mDialogShade->addChild(e);
			e->setVerticalAlignment(Ogre::GVA_CENTER);
			e->setLeft(3);
			e->setTop(mDialog->getOverlayElement()->getTop() + mDialog->getOverlayElement()->getHeight() + 5);
		}

		/*-----------------------------------------------------------------------------
		| Hides whatever dialog is currently showing.
		-----------------------------------------------------------------------------*/
		void closeDialog()
		{
			if (mDialog)
			{
				if (mOk)
				{
					mOk->cleanup();
					delete mOk;
					mOk = 0;
				}
				else
				{
					mYes->cleanup();
					mNo->cleanup();
					delete mYes;
					delete mNo;
					mYes = 0;
					mNo = 0;
				}

				mDialogShade->hide();
				mDialog->cleanup();
				delete mDialog;
				mDialog = 0;

				if (!mCursorWasVisible) hideCursor();
			}
		}

		/*-----------------------------------------------------------------------------
		| Determines if any dialog is currently visible.
		-----------------------------------------------------------------------------*/
		bool isDialogVisible()
		{
			return mDialog != 0;
		}

		/*-----------------------------------------------------------------------------
		| Gets a widget from a tray by place.
		-----------------------------------------------------------------------------*/
		Widget* getWidget(TrayLocation trayLoc, unsigned int place)
		{
			if (place < mWidgets[trayLoc].size()) return mWidgets[trayLoc][place];
			return 0;
		}

		/*-----------------------------------------------------------------------------
		| Gets a widget from a tray by name.
		-----------------------------------------------------------------------------*/
		Widget* getWidget(TrayLocation trayLoc, const Ogre::String& name)
		{
			for (unsigned int i = 0; i < mWidgets[trayLoc].size(); i++)
			{
				if (mWidgets[trayLoc][i]->getName() == name) return mWidgets[trayLoc][i];
			}
			return 0;
		}

		/*-----------------------------------------------------------------------------
		| Gets a widget by name.
		-----------------------------------------------------------------------------*/
		Widget* getWidget(const Ogre::String& name)
		{
			for (unsigned int i = 0; i < 10; i++)
			{
				for (unsigned int j = 0; j < mWidgets[i].size(); j++)
				{
					if (mWidgets[i][j]->getName() == name) return mWidgets[i][j];
				}
			}
			return 0;
		}

		/*-----------------------------------------------------------------------------
		| Gets the number of widgets in total.
		-----------------------------------------------------------------------------*/
		unsigned int getNumWidgets()
		{
			unsigned int total = 0;

			for (unsigned int i = 0; i < 10; i++)
			{
				total += mWidgets[i].size();
			}

			return total;
		}

		/*-----------------------------------------------------------------------------
		| Gets the number of widgets in a tray.
		-----------------------------------------------------------------------------*/
		unsigned int getNumWidgets(TrayLocation trayLoc)
		{
			return mWidgets[trayLoc].size();
		}

		/*-----------------------------------------------------------------------------
		| Gets all the widgets of a specific tray.
		-----------------------------------------------------------------------------*/
		WidgetIterator getWidgetIterator(TrayLocation trayLoc)
		{
			return WidgetIterator(mWidgets[trayLoc].begin(), mWidgets[trayLoc].end());
		}

		/*-----------------------------------------------------------------------------
		| Gets a widget's position in its tray.
		-----------------------------------------------------------------------------*/
		int locateWidgetInTray(Widget* widget)
		{
			for (unsigned int i = 0; i < mWidgets[widget->getTrayLocation()].size(); i++)
			{
				if (mWidgets[widget->getTrayLocation()][i] == widget) return i;
			}
			return -1;
		}

		/*-----------------------------------------------------------------------------
		| Destroys a widget.
		-----------------------------------------------------------------------------*/
		void destroyWidget(Widget* widget)
		{
			if (!widget) OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Widget does not exist.", "TrayManager::destroyWidget");

			// in case special widgets are destroyed manually, set them to 0
			if (widget == mLogo) mLogo = 0;
			else if (widget == mStatsPanel) mStatsPanel = 0;
			else if (widget == mFpsLabel) mFpsLabel = 0;

			mTrays[widget->getTrayLocation()]->removeChild(widget->getName());

			WidgetList& wList = mWidgets[widget->getTrayLocation()];
			wList.erase(std::find(wList.begin(), wList.end(), widget));
			if (widget == mExpandedMenu) setExpandedMenu(0);

			widget->cleanup();

			mWidgetDeathRow.push_back(widget);

			adjustTrays();
		}

		void destroyWidget(TrayLocation trayLoc, unsigned int place)
		{
			destroyWidget(getWidget(trayLoc, place));
		}

		void destroyWidget(TrayLocation trayLoc, const Ogre::String& name)
		{
			destroyWidget(getWidget(trayLoc, name));
		}

		void destroyWidget(const Ogre::String& name)
		{
			destroyWidget(getWidget(name));
		}

		/*-----------------------------------------------------------------------------
		| Destroys all widgets in a tray.
		-----------------------------------------------------------------------------*/
		void destroyAllWidgetsInTray(TrayLocation trayLoc)
		{
			while (!mWidgets[trayLoc].empty()) destroyWidget(mWidgets[trayLoc][0]);
		}

		/*-----------------------------------------------------------------------------
		| Destroys all widgets.
		-----------------------------------------------------------------------------*/
		void destroyAllWidgets()
		{
			for (unsigned int i = 0; i < 10; i++)  // destroy every widget in every tray (including null tray)
			{
				destroyAllWidgetsInTray((TrayLocation)i);
			}
		}

		/*-----------------------------------------------------------------------------
		| Adds a widget to a specified tray.
		-----------------------------------------------------------------------------*/
		void moveWidgetToTray(Widget* widget, TrayLocation trayLoc, int place = -1)
		{
			if (!widget) OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Widget does not exist.", "TrayManager::moveWidgetToTray");

			// remove widget from old tray
			WidgetList& wList = mWidgets[widget->getTrayLocation()];
			WidgetList::iterator it = std::find(wList.begin(), wList.end(), widget);
			if (it != wList.end())
			{
				wList.erase(it);
				mTrays[widget->getTrayLocation()]->removeChild(widget->getName());
			}

			// insert widget into new tray at given position, or at the end if unspecified or invalid
			if (place == -1 || place > (int)mWidgets[trayLoc].size()) place = mWidgets[trayLoc].size();
			mWidgets[trayLoc].insert(mWidgets[trayLoc].begin() + place, widget);
			mTrays[trayLoc]->addChild(widget->getOverlayElement());

			widget->getOverlayElement()->setHorizontalAlignment(mTrayWidgetAlign[trayLoc]);
			
			// adjust trays if necessary
			if (widget->getTrayLocation() != TL_NONE || trayLoc != TL_NONE) adjustTrays();

			widget->_assignToTray(trayLoc);
		}

		void moveWidgetToTray(const Ogre::String& name, TrayLocation trayLoc, unsigned int place = -1)
		{
			moveWidgetToTray(getWidget(name), trayLoc, place);
		}

		void moveWidgetToTray(TrayLocation currentTrayLoc, const Ogre::String& name, TrayLocation targetTrayLoc,
			int place = -1)
		{
			moveWidgetToTray(getWidget(currentTrayLoc, name), targetTrayLoc, place);
		}

		void moveWidgetToTray(TrayLocation currentTrayLoc, unsigned int currentPlace, TrayLocation targetTrayLoc,
			int targetPlace = -1)
		{
			moveWidgetToTray(getWidget(currentTrayLoc, currentPlace), targetTrayLoc, targetPlace);
		}

		/*-----------------------------------------------------------------------------
		| Removes a widget from its tray. Same as moving it to the null tray.
		-----------------------------------------------------------------------------*/
		void removeWidgetFromTray(Widget* widget)
		{
			moveWidgetToTray(widget, TL_NONE);
		}

		void removeWidgetFromTray(const Ogre::String& name)
		{
			removeWidgetFromTray(getWidget(name));
		}

		void removeWidgetFromTray(TrayLocation trayLoc, const Ogre::String& name)
		{
			removeWidgetFromTray(getWidget(trayLoc, name));
		}

		void removeWidgetFromTray(TrayLocation trayLoc, int place)
		{
			removeWidgetFromTray(getWidget(trayLoc, place));
		}

		/*-----------------------------------------------------------------------------
		| Removes all widgets from a widget tray.
		-----------------------------------------------------------------------------*/
		void clearTray(TrayLocation trayLoc)
		{
			if (trayLoc == TL_NONE) return;      // can't clear the null tray

			while (!mWidgets[trayLoc].empty())   // remove every widget from given tray
			{
				removeWidgetFromTray(mWidgets[trayLoc][0]);
			}
		}

		/*-----------------------------------------------------------------------------
		| Removes all widgets from all widget trays.
		-----------------------------------------------------------------------------*/
		void clearAllTrays()
		{
			for (unsigned int i = 0; i < 9; i++)
			{
				clearTray((TrayLocation)i);
			}
		}

		/*-----------------------------------------------------------------------------
		| Process frame events. Updates frame statistics widget set and deletes
		| all widgets queued for destruction.
		-----------------------------------------------------------------------------*/
		bool frameRenderingQueued(const Ogre::FrameEvent& evt)
		{
			for (unsigned int i = 0; i < mWidgetDeathRow.size(); i++)
			{
				delete mWidgetDeathRow[i];
			}
			mWidgetDeathRow.clear();

			Ogre::RenderTarget::FrameStats stats = mWindow->getStatistics();

			if (areFrameStatsVisible())
			{
				std::ostringstream oss;
				Ogre::String s;

				oss << "FPS: " << std::fixed << std::setprecision(1) << stats.lastFPS;
				s = oss.str();
				for (int i = s.length() - 5; i > 5; i -= 3) { s.insert(i, 1, ','); }
				mFpsLabel->setCaption(s);

				if (mStatsPanel->getOverlayElement()->isVisible())
				{
					Ogre::StringVector values;

					oss.str("");
					oss << std::fixed << std::setprecision(1) << stats.avgFPS;
					Ogre::String str = oss.str();
					for (int i = str.length() - 5; i > 0; i -= 3) { str.insert(i, 1, ','); }
					values.push_back(str);

					oss.str("");
					oss << std::fixed << std::setprecision(1) << stats.bestFPS;
					str = oss.str();
					for (int i = str.length() - 5; i > 0; i -= 3) { str.insert(i, 1, ','); }
					values.push_back(str);

					oss.str("");
					oss << std::fixed << std::setprecision(1) << stats.worstFPS;
					str = oss.str();
					for (int i = str.length() - 5; i > 0; i -= 3) { str.insert(i, 1, ','); }
					values.push_back(str);

					str = Ogre::StringConverter::toString(stats.triangleCount);
					for (int i = str.length() - 3; i > 0; i -= 3) { str.insert(i, 1, ','); }
					values.push_back(str);

					str = Ogre::StringConverter::toString(stats.batchCount);
					for (int i = str.length() - 3; i > 0; i -= 3) { str.insert(i, 1, ','); }
					values.push_back(str);

					mStatsPanel->setAllParamValues(values);
				}
			}

			return true;
		}

		void resourceGroupScriptingStarted(const Ogre::String& groupName, size_t scriptCount)
		{
			mLoadInc = mGroupInitProportion / scriptCount;
			mLoadBar->setCaption("Parsing...");
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			mWindow->update();
#endif
		}

		void scriptParseStarted(const Ogre::String& scriptName, bool& skipThisScript)
		{
			mLoadBar->setComment(scriptName);
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			mWindow->update();
#endif
		}

		void scriptParseEnded(const Ogre::String& scriptName, bool skipped)
		{
			mLoadBar->setProgress(mLoadBar->getProgress() + mLoadInc);
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			mWindow->update();
#endif
		}

		void resourceGroupScriptingEnded(const Ogre::String& groupName) {}

		void resourceGroupLoadStarted(const Ogre::String& groupName, size_t resourceCount)
		{
			mLoadInc = mGroupLoadProportion / resourceCount;
			mLoadBar->setCaption("Loading...");
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			mWindow->update();
#endif
		}

		void resourceLoadStarted(const Ogre::ResourcePtr& resource)
		{
			mLoadBar->setComment(resource->getName());
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			mWindow->update();
#endif
		}

		void resourceLoadEnded()
		{
			mLoadBar->setProgress(mLoadBar->getProgress() + mLoadInc);
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			mWindow->update();
#endif
		}

		void worldGeometryStageStarted(const Ogre::String& description)
		{
			mLoadBar->setComment(description);
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			mWindow->update();
#endif
		}

		void worldGeometryStageEnded()
		{
			mLoadBar->setProgress(mLoadBar->getProgress() + mLoadInc);
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			mWindow->update();
#endif
		}

		void resourceGroupLoadEnded(const Ogre::String& groupName) {}

		/*-----------------------------------------------------------------------------
		| Toggles visibility of advanced statistics.
		-----------------------------------------------------------------------------*/
		void labelHit(Label* label)
		{
			if (mStatsPanel->getOverlayElement()->isVisible())
			{
				mStatsPanel->getOverlayElement()->hide();
				mFpsLabel->getOverlayElement()->setWidth(150);
				removeWidgetFromTray(mStatsPanel);
			}
			else
			{
				mStatsPanel->getOverlayElement()->show();
				mFpsLabel->getOverlayElement()->setWidth(180);
				moveWidgetToTray(mStatsPanel, mFpsLabel->getTrayLocation(), locateWidgetInTray(mFpsLabel) + 1);
			}
		}

		/*-----------------------------------------------------------------------------
		| Destroys dialog widgets, notifies listener, and ends high priority session.
		-----------------------------------------------------------------------------*/
		void buttonHit(Button* button)
		{
			if (mListener)
			{
				if (button == mOk) mListener->okDialogClosed(mDialog->getText());
				else mListener->yesNoDialogClosed(mDialog->getText(), button == mYes);
			}
			closeDialog();
		}

		/*-----------------------------------------------------------------------------
		| Processes mouse button down events. Returns true if the event was
		| consumed and should not be passed on to other handlers.
		-----------------------------------------------------------------------------*/
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
		bool injectMouseDown(const OIS::MultiTouchEvent& evt)
#else
		bool injectMouseDown(const OIS::MouseEvent& evt, OIS::MouseButtonID id)
#endif
		{
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			// only process left button when stuff is visible
			if (!mCursorLayer->isVisible() || id != OIS::MB_Left) return false;
#else
            // only process touches when stuff is visible
			if (!mCursorLayer->isVisible()) return false;
#endif
			Ogre::Vector2 cursorPos(mCursor->getLeft(), mCursor->getTop());

			mTrayDrag = false;

			if (mExpandedMenu)   // only check top priority widget until it passes on
			{
				mExpandedMenu->_cursorPressed(cursorPos);
				if (!mExpandedMenu->isExpanded()) setExpandedMenu(0);
				return true;
			}

			if (mDialog)   // only check top priority widget until it passes on
			{
				mDialog->_cursorPressed(cursorPos);
				if (mOk) mOk->_cursorPressed(cursorPos);
				else
				{
					mYes->_cursorPressed(cursorPos);
					mNo->_cursorPressed(cursorPos);
				}
				return true;
			}

			for (unsigned int i = 0; i < 9; i++)   // check if mouse is over a non-null tray
			{
				if (mTrays[i]->isVisible() && Widget::isCursorOver(mTrays[i], cursorPos, 2))
				{
					mTrayDrag = true;   // initiate a drag that originates in a tray
					break;
				}
			}

			for (unsigned int i = 0; i < mWidgets[9].size(); i++)  // check if mouse is over a non-null tray's widgets
			{
				if (mWidgets[9][i]->getOverlayElement()->isVisible() &&
					Widget::isCursorOver(mWidgets[9][i]->getOverlayElement(), cursorPos))
				{
					mTrayDrag = true;   // initiate a drag that originates in a tray
					break;
				}
			}

			if (!mTrayDrag) return false;   // don't process if mouse press is not in tray

			for (unsigned int i = 0; i < 10; i++)
			{
				if (!mTrays[i]->isVisible()) continue;

				for (unsigned int j = 0; j < mWidgets[i].size(); j++)
				{
					Widget* w = mWidgets[i][j];
					if (!w->getOverlayElement()->isVisible()) continue;
					w->_cursorPressed(cursorPos);    // send event to widget

					SelectMenu* m = dynamic_cast<SelectMenu*>(w);
					if (m && m->isExpanded())       // a menu has begun a top priority session
					{
						setExpandedMenu(m);
						return true;
					}
				}
			}

			return true;   // a tray click is not to be handled by another party
		}

		/*-----------------------------------------------------------------------------
		| Processes mouse button up events. Returns true if the event was
		| consumed and should not be passed on to other handlers.
		-----------------------------------------------------------------------------*/
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
		bool injectMouseUp(const OIS::MultiTouchEvent& evt)
#else
		bool injectMouseUp(const OIS::MouseEvent& evt, OIS::MouseButtonID id)
#endif
		{
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
			// only process left button when stuff is visible
			if (!mCursorLayer->isVisible() || id != OIS::MB_Left) return false;
#else
            // only process touches when stuff is visible
			if (!mCursorLayer->isVisible()) return false;
#endif
			Ogre::Vector2 cursorPos(mCursor->getLeft(), mCursor->getTop());

			if (mExpandedMenu)   // only check top priority widget until it passes on
			{
				mExpandedMenu->_cursorReleased(cursorPos);
				return true;
			}

			if (mDialog)   // only check top priority widget until it passes on
			{
				mDialog->_cursorReleased(cursorPos);
				if (mOk) mOk->_cursorReleased(cursorPos);
				else
				{
					mYes->_cursorReleased(cursorPos);
					// very important to check if second button still exists, because first button could've closed the popup
					if (mNo) mNo->_cursorReleased(cursorPos); 
				}
				return true;
			}

			if (!mTrayDrag) return false;    // this click did not originate in a tray, so don't process

			Widget* w;

			for (unsigned int i = 0; i < 10; i++)
			{
				if (!mTrays[i]->isVisible()) continue;

				for (unsigned int j = 0; j < mWidgets[i].size(); j++)
				{
					w = mWidgets[i][j];
					if (!w->getOverlayElement()->isVisible()) continue;
					w->_cursorReleased(cursorPos);    // send event to widget
				}
			}

			mTrayDrag = false;   // stop this drag
			return true;         // this click did originate in this tray, so don't pass it on
		}

		/*-----------------------------------------------------------------------------
		| Updates cursor position. Returns true if the event was
		| consumed and should not be passed on to other handlers.
		-----------------------------------------------------------------------------*/
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
		bool injectMouseMove(const OIS::MultiTouchEvent& evt)
#else
		bool injectMouseMove(const OIS::MouseEvent& evt)
#endif
		{
			if (!mCursorLayer->isVisible()) return false;   // don't process if cursor layer is invisible

            Ogre::Vector2 cursorPos(evt.state.X.abs, evt.state.Y.abs);
			mCursor->setPosition(cursorPos.x, cursorPos.y);

			if (mExpandedMenu)   // only check top priority widget until it passes on
			{
				mExpandedMenu->_cursorMoved(cursorPos);
				return true;
			}

			if (mDialog)   // only check top priority widget until it passes on
			{
				mDialog->_cursorMoved(cursorPos);
				if (mOk) mOk->_cursorMoved(cursorPos);
				else
				{
					mYes->_cursorMoved(cursorPos);
					mNo->_cursorMoved(cursorPos);
				}
				return true;
			}

			Widget* w;

			for (unsigned int i = 0; i < 10; i++)
			{
				if (!mTrays[i]->isVisible()) continue;

				for (unsigned int j = 0; j < mWidgets[i].size(); j++)
				{
					w = mWidgets[i][j];
					if (!w->getOverlayElement()->isVisible()) continue;
					w->_cursorMoved(cursorPos);    // send event to widget
				}
			}

			if (mTrayDrag) return true;  // don't pass this event on if we're in the middle of a drag
			return false;
		}

    protected:

		/*-----------------------------------------------------------------------------
		| Internal method to prioritise / deprioritise expanded menus.
		-----------------------------------------------------------------------------*/
		void setExpandedMenu(SelectMenu* m)
		{
			if (!mExpandedMenu && m)
			{
				Ogre::OverlayContainer* c = (Ogre::OverlayContainer*)m->getOverlayElement();
				Ogre::OverlayContainer* eb = (Ogre::OverlayContainer*)c->getChild(m->getName() + "/MenuExpandedBox");
				eb->_update();
				eb->setPosition
					((unsigned int)(eb->_getDerivedLeft() * Ogre::OverlayManager::getSingleton().getViewportWidth()),
					(unsigned int)(eb->_getDerivedTop() * Ogre::OverlayManager::getSingleton().getViewportHeight()));
				c->removeChild(eb->getName());
				mPriorityLayer->add2D(eb);
			}
			else if(mExpandedMenu && !m)
			{
				Ogre::OverlayContainer* eb = mPriorityLayer->getChild(mExpandedMenu->getName() + "/MenuExpandedBox");
				mPriorityLayer->remove2D(eb);
				((Ogre::OverlayContainer*)mExpandedMenu->getOverlayElement())->addChild(eb);
			}

			mExpandedMenu = m;
		}

		Ogre::String mName;                   // name of this tray system
		Ogre::RenderWindow* mWindow;          // render window
#if OGRE_PLATFORM == OGRE_PLATFORM_IPHONE
		OIS::MultiTouch* mMouse;              // multitouch device
#else
		OIS::Mouse* mMouse;                   // mouse device
#endif
		Ogre::Overlay* mBackdropLayer;        // backdrop layer
		Ogre::Overlay* mTraysLayer;           // widget layer
		Ogre::Overlay* mPriorityLayer;        // top priority layer
		Ogre::Overlay* mCursorLayer;          // cursor layer
		Ogre::OverlayContainer* mBackdrop;    // backdrop
		Ogre::OverlayContainer* mTrays[10];   // widget trays
	    WidgetList mWidgets[10];              // widgets
		WidgetList mWidgetDeathRow;           // widget queue for deletion
		Ogre::OverlayContainer* mCursor;      // cursor
		SdkTrayListener* mListener;           // tray listener
		Ogre::Real mWidgetPadding;            // widget padding
		Ogre::Real mWidgetSpacing;            // widget spacing
		Ogre::Real mTrayPadding;              // tray padding
		bool mTrayDrag;                       // a mouse press was initiated on a tray
		SelectMenu* mExpandedMenu;            // top priority expanded menu widget
		TextBox* mDialog;                     // top priority dialog widget
		Ogre::OverlayContainer* mDialogShade; // top priority dialog shade
		Button* mOk;                          // top priority OK button
		Button* mYes;                         // top priority Yes button
		Button* mNo;                          // top priority No button
		bool mCursorWasVisible;               // cursor state before showing dialog
		Label* mFpsLabel;                     // FPS label
		ParamsPanel* mStatsPanel;             // frame stats panel
		DecorWidget* mLogo;                   // logo
		ProgressBar* mLoadBar;                // loading bar
		Ogre::Real mGroupInitProportion;      // proportion of load job assigned to initialising one resource group
		Ogre::Real mGroupLoadProportion;      // proportion of load job assigned to loading one resource group
		Ogre::Real mLoadInc;                  // loading increment
		Ogre::GuiHorizontalAlignment mTrayWidgetAlign[10];   // tray widget alignments
    };
}

#endif