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
/* Cafe panel: panel widget
 * (C) 1997,1998,1999,2000 the Free Software Foundation
 * (C) 2000 Eazel, Inc.
 *
 * Authors:  George Lebl
 */
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <ctk/ctk.h>
#include <cdk/cdkkeysyms.h>

#ifdef HAVE_X11
#include <ctk/ctkx.h> /* for CTK_IS_SOCKET */
#endif

#include <libpanel-util/panel-list.h>

#include "applet.h"
#include "panel-widget.h"
#include "button-widget.h"
#include "panel.h"
#include "panel-util.h"
#include "panel-marshal.h"
#include "panel-typebuiltins.h"
#include "panel-applet-frame.h"
#include "panel-globals.h"
#include "panel-profile.h"
#include "panel-lockdown.h"

#define MOVE_INCREMENT 1

typedef enum {
	PANEL_SWITCH_MOVE = 0,
	PANEL_FREE_MOVE,
	PANEL_PUSH_MOVE
} PanelMovementType;

G_DEFINE_TYPE (PanelWidget, panel_widget, CTK_TYPE_FIXED);

enum {
	SIZE_CHANGE_SIGNAL,
	BACK_CHANGE_SIGNAL,
	APPLET_MOVE_SIGNAL,
	APPLET_ADDED_SIGNAL,
	APPLET_REMOVED_SIGNAL,
	PUSH_MOVE_SIGNAL,
	SWITCH_MOVE_SIGNAL,
	FREE_MOVE_SIGNAL,
	TAB_MOVE_SIGNAL,
	END_MOVE_SIGNAL,
	POPUP_PANEL_MENU_SIGNAL,
	LAST_SIGNAL
};

static guint panel_widget_signals [LAST_SIGNAL] = {0};

/*define for some debug output*/
#undef PANEL_WIDGET_DEBUG

static gboolean cafe_panel_applet_in_drag = FALSE;
static CtkWidget *saved_focus_widget = NULL;

static void panel_widget_get_preferred_size (CtkWidget	  *widget,
					     CtkRequisition *minimum_size,
					     CtkRequisition *natural_size);
static void panel_widget_get_preferred_width (CtkWidget *widget,
					      gint *minimum_width,
					      gint *natural_width);
static void panel_widget_get_preferred_height (CtkWidget *widget,
					       gint *minimum_height,
					       gint *natural_height);
static void panel_widget_size_allocate  (CtkWidget        *widget,
					 CtkAllocation    *allocation);
static void panel_widget_cadd           (CtkContainer     *container,
					 CtkWidget        *widget);
static void panel_widget_cremove        (CtkContainer     *container,
					 CtkWidget        *widget);
static void panel_widget_dispose        (GObject *obj);
static void panel_widget_finalize       (GObject          *obj);

static void panel_widget_push_move_applet   (PanelWidget      *panel,
                                             CtkDirectionType  dir);
static void panel_widget_switch_move_applet (PanelWidget      *panel,
                                             CtkDirectionType  dir);
static void panel_widget_free_move_applet   (PanelWidget      *panel,
                                             CtkDirectionType  dir);
static void panel_widget_tab_move           (PanelWidget      *panel,
                                             gboolean          next);
static void panel_widget_end_move           (PanelWidget      *panel);
static gboolean panel_widget_real_focus     (CtkWidget        *widget,
                                             CtkDirectionType  direction);

static gboolean panel_widget_push_applet_right (PanelWidget *panel,
						GList       *list,
						int          push);
static gboolean panel_widget_push_applet_left  (PanelWidget *panel,
						GList       *list,
						int          push);

/************************
 convenience functions
 ************************/
static int
applet_data_compare (AppletData *ad1, AppletData *ad2)<--- Parameter 'ad1' can be declared as pointer to const<--- Parameter 'ad2' can be declared as pointer to const
{
	return ad1->pos - ad2->pos;
}

static void
emit_applet_moved (PanelWidget *panel_widget,
		   AppletData  *applet)
{
	/* we always want to queue a draw after moving, so do it here instead
	* of after the signal emission in all callers */
	ctk_widget_queue_draw (applet->applet);
	g_signal_emit (panel_widget,
		       panel_widget_signals [APPLET_MOVE_SIGNAL], 0,
		       applet->applet);
}

/************************
 widget core
 ************************/

static void
add_tab_bindings (CtkBindingSet    *binding_set,
   	          CdkModifierType   modifiers,
		  gboolean          next)
{
	ctk_binding_entry_add_signal (binding_set, CDK_KEY_Tab, modifiers,
				      "tab_move", 1,
				      G_TYPE_BOOLEAN, next);
	ctk_binding_entry_add_signal (binding_set, CDK_KEY_KP_Tab, modifiers,
				      "tab_move", 1,
				      G_TYPE_BOOLEAN, next);
}

static void
add_move_bindings (CtkBindingSet    *binding_set,
		   CdkModifierType   modifiers,
		   const gchar      *name)
{
	ctk_binding_entry_add_signal (binding_set, CDK_KEY_Up, modifiers,
                                      name, 1,
                                      CTK_TYPE_DIRECTION_TYPE, CTK_DIR_UP);
	ctk_binding_entry_add_signal (binding_set, CDK_KEY_Down, modifiers,
                                      name, 1,
                                      CTK_TYPE_DIRECTION_TYPE, CTK_DIR_DOWN);
	ctk_binding_entry_add_signal (binding_set, CDK_KEY_Left, modifiers,
                                      name, 1,
                                      CTK_TYPE_DIRECTION_TYPE, CTK_DIR_LEFT);
	ctk_binding_entry_add_signal (binding_set, CDK_KEY_Right, modifiers,
                                      name, 1,
                                      CTK_TYPE_DIRECTION_TYPE, CTK_DIR_RIGHT);
}

static void
add_all_move_bindings (PanelWidget *panel)
{
	CtkWidgetClass *class;
	CtkBindingSet *binding_set;

	class = CTK_WIDGET_GET_CLASS (panel);

	binding_set = ctk_binding_set_by_class (class);

	add_move_bindings (binding_set, CDK_SHIFT_MASK, "push_move");
	add_move_bindings (binding_set, CDK_CONTROL_MASK, "switch_move");
	add_move_bindings (binding_set, CDK_MOD1_MASK, "free_move");
	add_move_bindings (binding_set, 0, "free_move");

	add_tab_bindings (binding_set, 0, TRUE);
	add_tab_bindings (binding_set, CDK_SHIFT_MASK, FALSE);

	ctk_binding_entry_add_signal (binding_set,
                                      CDK_KEY_Escape, 0,
                                      "end_move", 0);
	ctk_binding_entry_add_signal (binding_set,
                                      CDK_KEY_KP_Enter, 0,
                                      "end_move", 0);
	ctk_binding_entry_add_signal (binding_set,
                                      CDK_KEY_Return, 0,
                                      "end_move", 0);
	ctk_binding_entry_add_signal (binding_set,
                                      CDK_KEY_KP_Space, 0,
                                      "end_move", 0);
	ctk_binding_entry_add_signal (binding_set,
                                      CDK_KEY_space, 0,
                                      "end_move", 0);

#ifdef HAVE_X11
	CtkWidget *focus_widget;

	focus_widget = ctk_window_get_focus (CTK_WINDOW (panel->toplevel));
	// will always be false when not on X
	if (CTK_IS_SOCKET (focus_widget)) {
		/*
		 * If the focus widget is a CtkSocket, i.e. the
		 * focus is in an applet in another process then
		 * key bindings do not work. We get around this by
		 * by setting the focus to the PanelWidget for the
		 * duration of the move.
		 */
		ctk_widget_set_can_focus (CTK_WIDGET (panel), TRUE);
		ctk_widget_grab_focus (CTK_WIDGET (panel));
		saved_focus_widget = focus_widget;
	}
#endif // HAVE_X11
}

static void
panel_widget_force_grab_focus (CtkWidget *widget)
{
	gboolean can_focus = ctk_widget_get_can_focus (widget);
	/*
	 * This follows what ctk_socket_claim_focus() does
	 */
	if (!can_focus)
		ctk_widget_set_can_focus (widget, TRUE);
	ctk_widget_grab_focus (widget);
	if (!can_focus)
		ctk_widget_set_can_focus (widget, FALSE);
}

static void
panel_widget_reset_saved_focus (PanelWidget *panel)
{
	if (saved_focus_widget) {
		ctk_widget_set_can_focus (CTK_WIDGET (panel), FALSE);
		panel_widget_force_grab_focus (saved_focus_widget);
		saved_focus_widget = NULL;
	}
}

static void
remove_tab_bindings (CtkBindingSet    *binding_set,
		     CdkModifierType   modifiers,
		     gboolean          next)
{
	ctk_binding_entry_remove (binding_set, CDK_KEY_Tab, modifiers);
	ctk_binding_entry_remove (binding_set, CDK_KEY_KP_Tab, modifiers);
}

static void
remove_move_bindings (CtkBindingSet    *binding_set,
		      CdkModifierType   modifiers)
{
	ctk_binding_entry_remove (binding_set, CDK_KEY_Up, modifiers);
	ctk_binding_entry_remove (binding_set, CDK_KEY_Down, modifiers);
	ctk_binding_entry_remove (binding_set, CDK_KEY_Left, modifiers);
	ctk_binding_entry_remove (binding_set, CDK_KEY_Right, modifiers);
}

static void
remove_all_move_bindings (PanelWidget *panel)
{
	CtkWidgetClass *class;
	CtkBindingSet *binding_set;

	class = CTK_WIDGET_GET_CLASS (panel);

	binding_set = ctk_binding_set_by_class (class);

	panel_widget_reset_saved_focus (panel);

	remove_move_bindings (binding_set, CDK_SHIFT_MASK);
	remove_move_bindings (binding_set, CDK_CONTROL_MASK);
	remove_move_bindings (binding_set, CDK_MOD1_MASK);
	remove_move_bindings (binding_set, 0);
	remove_tab_bindings (binding_set, 0, TRUE);
	remove_tab_bindings (binding_set, CDK_SHIFT_MASK, FALSE);

	ctk_binding_entry_remove (binding_set, CDK_KEY_Escape, 0);
	ctk_binding_entry_remove (binding_set, CDK_KEY_KP_Enter, 0);
	ctk_binding_entry_remove (binding_set, CDK_KEY_Return, 0);
	ctk_binding_entry_remove (binding_set, CDK_KEY_KP_Space, 0);
	ctk_binding_entry_remove (binding_set, CDK_KEY_space, 0);
}

static void
panel_widget_class_init (PanelWidgetClass *class)
{
	GObjectClass *object_class = (GObjectClass*) class;
	CtkWidgetClass *widget_class = (CtkWidgetClass*) class;
	CtkContainerClass *container_class = (CtkContainerClass*) class;

	panel_widget_signals[SIZE_CHANGE_SIGNAL] =
                g_signal_new ("size_change",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (PanelWidgetClass, size_change),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE,
                              0);

	panel_widget_signals[BACK_CHANGE_SIGNAL] =
                g_signal_new ("back_change",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (PanelWidgetClass, back_change),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE,
                              0);

	panel_widget_signals[APPLET_MOVE_SIGNAL] =
                g_signal_new ("applet_move",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (PanelWidgetClass, applet_move),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__POINTER,
                              G_TYPE_NONE,
                              1,
                              G_TYPE_POINTER);

	panel_widget_signals[APPLET_ADDED_SIGNAL] =
                g_signal_new ("applet_added",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (PanelWidgetClass, applet_added),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__POINTER,
                              G_TYPE_NONE,
                              1,
                              G_TYPE_POINTER);

	panel_widget_signals[APPLET_REMOVED_SIGNAL] =
                g_signal_new ("applet_removed",
                              G_TYPE_FROM_CLASS (object_class),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (PanelWidgetClass, applet_removed),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__POINTER,
                              G_TYPE_NONE,
                              1,
                              G_TYPE_POINTER);

	panel_widget_signals[PUSH_MOVE_SIGNAL] =
		g_signal_new ("push_move",
                              G_TYPE_FROM_CLASS (class),
                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                              G_STRUCT_OFFSET (PanelWidgetClass, push_move),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__ENUM,
                              G_TYPE_NONE,
                              1,
                              CTK_TYPE_DIRECTION_TYPE);

	panel_widget_signals[SWITCH_MOVE_SIGNAL] =
                g_signal_new ("switch_move",
                              G_TYPE_FROM_CLASS (class),
                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                              G_STRUCT_OFFSET (PanelWidgetClass, switch_move),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__ENUM,
                              G_TYPE_NONE,
                              1,
                              CTK_TYPE_DIRECTION_TYPE);

	panel_widget_signals[FREE_MOVE_SIGNAL] =
                g_signal_new ("free_move",
                              G_TYPE_FROM_CLASS (class),
                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                              G_STRUCT_OFFSET (PanelWidgetClass, free_move),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__ENUM,
                              G_TYPE_NONE,
                              1,
                              CTK_TYPE_DIRECTION_TYPE);

	panel_widget_signals[TAB_MOVE_SIGNAL] =
                g_signal_new ("tab_move",
                              G_TYPE_FROM_CLASS (class),
                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
			      G_STRUCT_OFFSET (PanelWidgetClass, tab_move),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__BOOLEAN,
                              G_TYPE_NONE,
                              1,
                              G_TYPE_BOOLEAN);

	panel_widget_signals[END_MOVE_SIGNAL] =
		g_signal_new ("end_move",
                              G_TYPE_FROM_CLASS (class),
                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                              G_STRUCT_OFFSET (PanelWidgetClass, end_move),
                              NULL,
                              NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE,
                              0);

	class->size_change = NULL;
	class->back_change = NULL;
	class->applet_move = NULL;
	class->applet_added = NULL;
	class->applet_removed = NULL;
	class->push_move = panel_widget_push_move_applet;
	class->switch_move = panel_widget_switch_move_applet;
	class->free_move = panel_widget_free_move_applet;
	class->tab_move = panel_widget_tab_move;
	class->end_move = panel_widget_end_move;

	object_class->dispose = panel_widget_dispose;
	object_class->finalize = panel_widget_finalize;

	widget_class->get_preferred_width = panel_widget_get_preferred_width;
	widget_class->get_preferred_height = panel_widget_get_preferred_height;
	widget_class->size_allocate = panel_widget_size_allocate;

	ctk_widget_class_set_css_name (widget_class, "PanelWidget");

	widget_class->focus = panel_widget_real_focus;
	container_class->add = panel_widget_cadd;
	container_class->remove = panel_widget_cremove;
}

static void
remove_panel_from_forbidden(PanelWidget *panel, PanelWidget *r)
{
	GSList *list;
	CtkWidget *parent_panel;

	g_return_if_fail(PANEL_IS_WIDGET(panel));
	g_return_if_fail(PANEL_IS_WIDGET(r));

	if(!panel->master_widget)
		return;

	list = g_object_get_data (G_OBJECT(panel->master_widget),
				  CAFE_PANEL_APPLET_FORBIDDEN_PANELS);
	if(list) {
		list = g_slist_remove(list,r);
		g_object_set_data (G_OBJECT(panel->master_widget),
				   CAFE_PANEL_APPLET_FORBIDDEN_PANELS,
				   list);
	}
	parent_panel = ctk_widget_get_parent (panel->master_widget);
	if (parent_panel)
		remove_panel_from_forbidden(PANEL_WIDGET(parent_panel), r);
}

static void
add_panel_to_forbidden(PanelWidget *panel, PanelWidget *r)
{
	GSList *list;
	CtkWidget *parent_panel;

	g_return_if_fail(PANEL_IS_WIDGET(panel));
	g_return_if_fail(PANEL_IS_WIDGET(r));

	if(!panel->master_widget)
		return;

	list = g_object_get_data (G_OBJECT(panel->master_widget),
				  CAFE_PANEL_APPLET_FORBIDDEN_PANELS);
	if(g_slist_find(list,r)==NULL) {
		list = g_slist_prepend(list,r);

		g_object_set_data (G_OBJECT(panel->master_widget),
				   CAFE_PANEL_APPLET_FORBIDDEN_PANELS,
				   list);
	}
	parent_panel = ctk_widget_get_parent (panel->master_widget);
	if (parent_panel)
		add_panel_to_forbidden(PANEL_WIDGET(parent_panel), r);
}

static void
run_up_forbidden(PanelWidget *panel,
		 void (*runfunc)(PanelWidget *,PanelWidget *))
{
	GList *list;

	g_return_if_fail(PANEL_IS_WIDGET(panel));

	for(list = panel->applet_list;list!=NULL;list = g_list_next(list)) {
		AppletData *ad = list->data;
		PanelWidget *p =
			g_object_get_data (G_OBJECT(ad->applet),
					   CAFE_PANEL_APPLET_ASSOC_PANEL_KEY);
		if(p)
			run_up_forbidden(p,runfunc);
	}
	(*runfunc)(panel,panel);
}

static void
panel_widget_reset_focus (CtkContainer *container,
                          CtkWidget    *widget)<--- Parameter 'widget' can be declared as pointer to const
{
	PanelWidget *panel = PANEL_WIDGET (container);

	if (ctk_container_get_focus_child (container) == widget) {
		GList *children;

		children = ctk_container_get_children (container);

		/* More than one element on the list */
		if (children && children->next) {
			GList *l;

			/* There are still object on the panel */
			for (l = children; l; l = l->next) {
				CtkWidget *child_widget;<--- Variable 'child_widget' can be declared as pointer to const

				child_widget = l->data;
				if (child_widget == widget)
					break;
			}
			if (l) {
				CtkWidget *next_widget;

				if (l->next)
					next_widget = l->next->data;
				else
					next_widget = l->prev->data;

				ctk_widget_child_focus (next_widget,
						        CTK_DIR_TAB_FORWARD);
			}
		} else
			panel_widget_focus (panel);

		g_list_free (children);
	}
}

static void
panel_widget_cadd (CtkContainer *container,
		   CtkWidget    *widget)
{
	PanelWidget *p;

	g_return_if_fail (PANEL_IS_WIDGET (container));
	g_return_if_fail (CTK_IS_WIDGET (widget));

	panel_widget_add (PANEL_WIDGET (container), widget, FALSE, 0, FALSE);

	p = g_object_get_data (G_OBJECT(widget),
			       CAFE_PANEL_APPLET_ASSOC_PANEL_KEY);
	if (p) {
		panel_toplevel_attach_to_widget (p->toplevel,
						 PANEL_WIDGET (container)->toplevel,
						 widget);
		run_up_forbidden (p, add_panel_to_forbidden);
	}
}

static void
panel_widget_cremove (CtkContainer *container, CtkWidget *widget)
{
	AppletData *ad;
	PanelWidget *p;
	PanelWidget *panel;

	g_return_if_fail (PANEL_IS_WIDGET (container));
	g_return_if_fail (CTK_IS_WIDGET (widget));

	panel = PANEL_WIDGET (container);

	ad = g_object_get_data (G_OBJECT (widget), CAFE_PANEL_APPLET_DATA);
	p = g_object_get_data (G_OBJECT (widget),
				 CAFE_PANEL_APPLET_ASSOC_PANEL_KEY);

	if (p != NULL) {
		panel_toplevel_detach (p->toplevel);
		run_up_forbidden (p, remove_panel_from_forbidden);
	}

	panel_widget_reset_focus (container, widget);

	if(panel->currently_dragged_applet == ad)
		panel_widget_applet_drag_end(panel);

	g_object_ref (widget);
	if (CTK_CONTAINER_CLASS (panel_widget_parent_class)->remove)
		(* CTK_CONTAINER_CLASS (panel_widget_parent_class)->remove) (container,
								widget);
	if (ad)
		panel->applet_list = g_list_remove (panel->applet_list, ad);

	g_signal_emit (G_OBJECT (container),
		       panel_widget_signals[APPLET_REMOVED_SIGNAL],
		       0, widget);
	g_object_unref (widget);
}


/*get the list item of the data on the position pos*/
static GList *
get_applet_list_pos (PanelWidget *panel,
		     int          pos)
{
	GList *l;

	g_return_val_if_fail (PANEL_IS_WIDGET (panel), NULL);

	for (l = panel->applet_list; l; l = l->next) {
		AppletData *ad = l->data;<--- Variable 'ad' can be declared as pointer to const

		if (ad->pos <= pos) {
		       if (ad->pos + ad->cells > pos)
			       return l;
		} else
			return NULL;
	}

	return NULL;
}

/*tells us if an applet is "stuck" on the right side*/
int
panel_widget_is_applet_stuck (PanelWidget *panel_widget,
			      CtkWidget   *widget)
{
	AppletData *applet;

	g_return_val_if_fail (PANEL_IS_WIDGET (panel_widget), FALSE);
	g_return_val_if_fail (CTK_IS_WIDGET (widget), FALSE);

	applet = g_object_get_data (G_OBJECT (widget), CAFE_PANEL_APPLET_DATA);
	if (applet) {
		GList *applet_list, *l;
		int    end_pos = -1;

		applet_list = g_list_find (panel_widget->applet_list, applet);

		for (l = applet_list; l; l = l->next) {
			applet = l->data;

			if (end_pos != -1 && applet->pos != end_pos)
				break;

			end_pos = applet->pos + applet->cells;
			if (end_pos >= panel_widget->size)
				return TRUE;
		}
	}

	return FALSE;
}

static int
get_size_from_hints (AppletData *ad, int cells)
{
	int i;

	for (i = 0; i < ad->size_hints_len; i += 2) {
		if (cells > ad->size_hints[i]) {
			/* Clip to top */
			cells = ad->size_hints[i];
			break;
		}
		if (cells <= ad->size_hints[i] &&
		    cells >= ad->size_hints[i+1]) {
			/* Keep cell size */
			break;
		}
	}

	return MAX (cells, ad->min_cells);
}

static void
panel_widget_jump_applet_right (PanelWidget *panel,
				GList       *list,
				GList       *next,
				int          pos)
{
	AppletData *ad;
	AppletData *nad = NULL;<--- Variable 'nad' can be declared as pointer to const

	ad = list->data;
	if (next)
		nad = next->data;

	if (pos >= panel->size)
		return;

	if (!nad || nad->constrained >= pos + ad->min_cells)
		goto jump_right;

	if (!panel_widget_push_applet_right (panel, next, pos + ad->min_cells - nad->constrained)) {
		panel_widget_jump_applet_right (panel,
						list,
						next->next,
						nad->constrained + nad->min_cells);
		return;
	}

 jump_right:
	ad->pos = ad->constrained = pos;
	panel->applet_list = g_list_remove_link (panel->applet_list, list);
	panel->applet_list = panel_g_list_insert_before (panel->applet_list, next, list);
	ctk_widget_queue_resize (CTK_WIDGET (panel));
	emit_applet_moved (panel, ad);
}

static void
panel_widget_switch_applet_right (PanelWidget *panel,
				  GList       *list)
{
	AppletData *ad;
	AppletData *nad = NULL;

	g_assert (list != NULL);

	ad = list->data;
	if (ad->constrained + ad->min_cells >= panel->size)
		return;

	if (list->next)
		nad = list->next->data;

	if (!nad || nad->constrained >= ad->constrained + ad->min_cells + MOVE_INCREMENT) {
		ad->pos = ad->constrained += MOVE_INCREMENT;
		ctk_widget_queue_resize (CTK_WIDGET (panel));
		emit_applet_moved (panel, ad);
		return;
	}

	if (nad->locked) {
		panel_widget_jump_applet_right (panel,
						list,
						list->next->next,
						nad->constrained + nad->min_cells);
		return;
	}

	nad->constrained = nad->pos = ad->constrained;
	ad->constrained = ad->pos = ad->constrained + nad->min_cells;
	panel->applet_list = panel_g_list_swap_next (panel->applet_list, list);

	ctk_widget_queue_resize (CTK_WIDGET (panel));

	emit_applet_moved (panel, ad);
	emit_applet_moved (panel, nad);
}

static void
panel_widget_jump_applet_left (PanelWidget *panel,
			       GList       *list,
			       GList       *prev,
			       int          pos)
{
	AppletData *ad;
	AppletData *pad = NULL;<--- Variable 'pad' can be declared as pointer to const

	ad = list->data;
	if (prev)
		pad = prev->data;

	if (pos < 0)
		return;

	if (!pad || pad->constrained + pad->min_cells <= pos)
		goto jump_left;

	if (!panel_widget_push_applet_left (panel, prev, pad->constrained + pad->min_cells - pos)) {
		panel_widget_jump_applet_left (panel,
					       list,
					       prev->prev,
					       pad->constrained - ad->min_cells);
		return;
	}

 jump_left:
	ad->pos = ad->constrained = pos;
	panel->applet_list = g_list_remove_link (panel->applet_list, list);
	panel->applet_list = panel_g_list_insert_after (panel->applet_list, prev, list);
	ctk_widget_queue_resize (CTK_WIDGET (panel));
	emit_applet_moved (panel, ad);
}

static void
panel_widget_switch_applet_left (PanelWidget *panel,
				 GList       *list)
{
	AppletData *ad;
	AppletData *pad = NULL;

	ad = list->data;
	if (ad->constrained <= 0)
		return;

	if (list->prev)
		pad = list->prev->data;

	if (!pad || pad->constrained + pad->min_cells <= ad->constrained - MOVE_INCREMENT) {
		ad->pos = ad->constrained -= MOVE_INCREMENT;
		ctk_widget_queue_resize (CTK_WIDGET (panel));
		emit_applet_moved (panel, ad);
		return;
	}

	if (pad->locked) {
		panel_widget_jump_applet_left (panel,
					       list,
					       list->prev->prev,
					       pad->constrained - ad->min_cells);
		return;
	}

	ad->constrained = ad->pos = pad->constrained;
	pad->constrained = pad->pos = ad->constrained + ad->min_cells;
	panel->applet_list = panel_g_list_swap_prev (panel->applet_list, list);

	ctk_widget_queue_resize (CTK_WIDGET (panel));

	emit_applet_moved (panel, ad);
	emit_applet_moved (panel, pad);
}

static gboolean
panel_widget_try_push_right (PanelWidget *panel,
			     GList       *list,
			     int          push)
{
	AppletData *ad;<--- Variable 'ad' can be declared as pointer to const
	AppletData *nad = NULL;<--- Variable 'nad' can be declared as pointer to const

	g_assert (list != NULL);

	ad = list->data;
	if (list->next)
		nad = list->next->data;

	if (ad->locked)
		return FALSE;

	if (ad->constrained + ad->min_cells + push >= panel->size)
		return FALSE;

	if (!nad || nad->constrained >= ad->constrained + ad->min_cells + push)
		return TRUE;

	return panel_widget_try_push_right (panel, list->next, push);
}

static int
panel_widget_get_right_jump_pos (PanelWidget *panel,
				 AppletData  *ad,
				 GList       *next,
				 int          pos)
{
	AppletData *nad = NULL;<--- Variable 'nad' can be declared as pointer to const

	if (next)
		nad = next->data;

	if (!nad || nad->constrained >= pos + ad->min_cells)
		return pos;

	if (panel_widget_try_push_right (panel, next, pos + ad->min_cells - nad->constrained))
		return pos;

	return panel_widget_get_right_jump_pos (panel,
						ad,
						next->next,
						nad->constrained + nad->min_cells);
}

static int
panel_widget_get_right_switch_pos (PanelWidget *panel,
				   GList       *list)
{
	AppletData *ad;
	AppletData *nad = NULL;<--- Variable 'nad' can be declared as pointer to const

	g_assert (list != NULL);

	ad = list->data;
	if (list->next)
		nad = list->next->data;

	if (!nad || nad->constrained >= ad->constrained + ad->min_cells + MOVE_INCREMENT)
		return ad->constrained + MOVE_INCREMENT;

	if (nad->locked)
		return panel_widget_get_right_jump_pos (panel,
							ad,
							list->next->next,
							nad->constrained + nad->min_cells);

	return nad->constrained + nad->min_cells - ad->cells;
}

static gboolean
panel_widget_try_push_left (PanelWidget *panel,
			    GList       *list,
			    int          push)
{
	AppletData *ad;<--- Variable 'ad' can be declared as pointer to const
	AppletData *pad = NULL;<--- Variable 'pad' can be declared as pointer to const

	g_assert (list != NULL);

	ad = list->data;
	if (list->prev)
		pad = list->prev->data;

	if (ad->locked)
		return FALSE;

	if (ad->constrained - push < 0)
		return FALSE;

	if (!pad || pad->constrained + pad->min_cells <= ad->constrained - push)
		return TRUE;

	return panel_widget_try_push_left (panel, list->prev, push);
}

static int
panel_widget_get_left_jump_pos (PanelWidget *panel,
				AppletData  *ad,
				GList       *prev,
				int          pos)
{
	AppletData *pad = NULL;<--- Variable 'pad' can be declared as pointer to const

	if (prev)
		pad = prev->data;

	if (!pad || pad->constrained + pad->min_cells <= pos)
		return pos;

	if (panel_widget_try_push_left (panel, prev, pad->constrained + pad->min_cells - pos))
		return pos;

	return panel_widget_get_left_jump_pos (panel,
					       ad,
					       prev->prev,
					       pad->constrained - ad->min_cells);
}

static int
panel_widget_get_left_switch_pos (PanelWidget *panel,
				  GList       *list)
{
	AppletData *ad;
	AppletData *pad = NULL;<--- Variable 'pad' can be declared as pointer to const

	g_assert (list != NULL);

	ad = list->data;
	if (list->prev)
		pad = list->prev->data;

	if (!pad || pad->constrained + pad->min_cells <= ad->constrained - MOVE_INCREMENT)
		return ad->constrained - MOVE_INCREMENT;

	if (pad->locked)
		return panel_widget_get_left_jump_pos (panel,
						       ad,
						       list->prev->prev,
						       pad->constrained - ad->min_cells);

	return pad->constrained;
}

static void
panel_widget_switch_move (PanelWidget *panel,
			  AppletData  *ad,
			  int          moveby)
{
	GList *list;
	int    finalpos;
	int    pos;

	g_return_if_fail (ad != NULL);
	g_return_if_fail (PANEL_IS_WIDGET (panel));

	if (moveby == 0)
		return;

	list = g_list_find (panel->applet_list, ad);
	g_return_if_fail (list != NULL);

	finalpos = ad->constrained + moveby;

	if (ad->constrained < finalpos) {<--- Assuming that condition 'ad->constrained
		AppletData *pad;<--- Variable 'pad' can be declared as pointer to const

		if (list->prev) {
			pad = list->prev->data;
			if (pad->expand_major)
				ctk_widget_queue_resize (CTK_WIDGET (panel));
		}

		while (ad->constrained < finalpos) {<--- Condition 'ad->constrained
			pos = panel_widget_get_right_switch_pos (panel, list);

			if (abs (pos - finalpos) >= abs (ad->constrained - finalpos) ||
			    pos + ad->min_cells > panel->size)
				break;

			panel_widget_switch_applet_right (panel, list);
		}

		if (list->prev) {
			pad = list->prev->data;
			if (pad->expand_major)
				ctk_widget_queue_resize (CTK_WIDGET (panel));
		}
	} else {
		AppletData *nad;<--- Variable 'nad' can be declared as pointer to const

		if (list->next) {
			nad = list->next->data;
			if (nad->expand_major)
				ctk_widget_queue_resize (CTK_WIDGET (panel));
		}

		while (ad->constrained > finalpos) {
			pos = panel_widget_get_left_switch_pos (panel, list);

			if (abs (pos - finalpos) >= abs (ad->constrained - finalpos) || pos < 0)
				break;

			panel_widget_switch_applet_left (panel, list);
		}

	}
}

static int
panel_widget_push_applet_right (PanelWidget *panel,
				GList       *list,
				int          push)
{
	AppletData *ad;
	AppletData *nad = NULL;<--- Variable 'nad' can be declared as pointer to const

	g_assert (list != NULL);

	ad = list->data;
	if (ad->constrained + ad->min_cells + push >= panel->size)
		return FALSE;

	if (ad->locked)
		return FALSE;

	if (list->next)
		nad = list->next->data;

	if (!nad || nad->constrained >= ad->constrained + ad->min_cells + push) {
		ad->pos = ad->constrained += push;
		ctk_widget_queue_resize (CTK_WIDGET (panel));
		emit_applet_moved (panel, ad);
		return TRUE;
	}

	g_assert (list->next != NULL);

	if (!panel_widget_push_applet_right (panel, list->next, push))
		return FALSE;

	ad->pos = ad->constrained += push;;
	ctk_widget_queue_resize (CTK_WIDGET (panel));
	emit_applet_moved (panel, ad);

	return TRUE;
}

static int
panel_widget_push_applet_left (PanelWidget *panel,
			       GList       *list,
			       int          push)
{
	AppletData *ad;
	AppletData *pad = NULL;<--- Variable 'pad' can be declared as pointer to const

	g_assert (list != NULL);

	ad = list->data;
	if (ad->constrained - push < 0)
		return FALSE;

	if (ad->locked)
		return FALSE;

	if (list->prev)
		pad = list->prev->data;

	if (!pad || pad->constrained + pad->min_cells <= ad->constrained - push) {
		ad->pos = ad->constrained -= push;
		ctk_widget_queue_resize (CTK_WIDGET (panel));
		emit_applet_moved (panel, ad);
		return TRUE;
	}

	g_assert (list->prev != NULL);

	if (!panel_widget_push_applet_left (panel, list->prev, push))
		return FALSE;

	ad->pos = ad->constrained -= push;
	ctk_widget_queue_resize (CTK_WIDGET (panel));
	emit_applet_moved (panel, ad);

	return TRUE;
}

static void
panel_widget_push_move (PanelWidget *panel,
			AppletData  *ad,
			int          moveby)
{
	AppletData *pad;<--- Variable 'pad' can be declared as pointer to const
	int finalpos;
	GList *list;

	g_return_if_fail (ad != NULL);
	g_return_if_fail (PANEL_IS_WIDGET (panel));

	if (moveby == 0)
		return;

	list = g_list_find (panel->applet_list, ad);
	g_return_if_fail (list != NULL);

	finalpos = ad->constrained + moveby;

	if (ad->constrained < finalpos) {<--- Assuming that condition 'ad->constrained
		while (ad->constrained < finalpos)<--- Condition 'ad->constrained
			if (!panel_widget_push_applet_right (panel, list, 1))
				break;

                if (list->prev) {
			pad = list->prev->data;
			if (pad->expand_major)
				ctk_widget_queue_resize (CTK_WIDGET (panel));
		}
	} else {
                while (ad->constrained > finalpos)
			if (!panel_widget_push_applet_left (panel, list, 1))
				break;
	}
}


/*this is a special function and may fail if called improperly, it works
only under special circumstance when we know there is nothing from
old_size to panel->size*/
static void
panel_widget_right_stick(PanelWidget *panel,int old_size)
{
	int i,pos;
	GList *list,*prev;
	AppletData *ad;

	g_return_if_fail(PANEL_IS_WIDGET(panel));
	g_return_if_fail(old_size>=0);

	if(old_size>=panel->size ||
	   panel->packed)
	   	return;

	list = get_applet_list_pos(panel,old_size-1);

	if(!list)
		return;

	pos = panel->size-1;

	ad = list->data;
	do {
		i = ad->pos;
		ad->pos = ad->constrained = pos--;
		ad->cells = 1;
		prev = list;
		list = g_list_previous(list);
		if(!list)
			break;
		ad = list->data;
	} while(ad->pos + ad->cells == i);

	for (list = prev; list; list = list->next)
		emit_applet_moved (panel, list->data);
}

static void
panel_widget_get_preferred_size(CtkWidget	     *widget,
				CtkRequisition *minimum_size,
				CtkRequisition *natural_size)
{
	PanelWidget *panel;
	GList *list;
	GList *ad_with_hints;
	gboolean dont_fill;
	gint scale;

	g_return_if_fail(PANEL_IS_WIDGET(widget));
	g_return_if_fail(minimum_size != NULL);

	panel = PANEL_WIDGET(widget);

	if (panel->orient == CTK_ORIENTATION_HORIZONTAL) {
		minimum_size->width = 0;
		minimum_size->height = panel->sz;
	} else {
		minimum_size->height = 0;
		minimum_size->width = panel->sz;
	}
	natural_size->width = minimum_size->width;
	natural_size->height = minimum_size->height;

	ad_with_hints = NULL;
	scale = ctk_widget_get_scale_factor(widget);

	for (list = panel->applet_list; list!=NULL; list = g_list_next(list)) {
		AppletData *ad = list->data;
		CtkRequisition child_min_size;
		CtkRequisition child_natural_size;
		ctk_widget_get_preferred_size(ad->applet,
		                              &child_min_size,
		                              &child_natural_size);

		if (panel->orient == CTK_ORIENTATION_HORIZONTAL) {
			if (minimum_size->height < child_min_size.height &&
			    !ad->size_constrained)
				minimum_size->height = child_min_size.height;
			if (natural_size->height < child_natural_size.height &&
			    !ad->size_constrained)
				natural_size->height = child_natural_size.height;

			if (panel->packed && ad->expand_major && ad->size_hints)
				ad_with_hints = g_list_prepend (ad_with_hints,
								ad);

			else if (panel->packed)
			{
				/* Just because everything is bigger when scaled up doesn't mean
				 * that the applets need any less room when the panel is packed. */
				minimum_size->width += child_min_size.width * scale;
				natural_size->width += child_natural_size.width * scale;
			}
		} else {
			if (minimum_size->width < child_min_size.width &&
			    !ad->size_constrained)
				minimum_size->width = child_min_size.width;
			if (natural_size->width < child_min_size.width &&
			    !ad->size_constrained)
				natural_size->width = child_min_size.width;

			if (panel->packed && ad->expand_major && ad->size_hints)
				ad_with_hints = g_list_prepend (ad_with_hints,
								ad);

			else if (panel->packed)
			{
				/* Just because everything is bigger when scaled up doesn't mean
				 * that the applets need any less room when the panel is packed. */
				minimum_size->height += child_min_size.height * scale;
				natural_size->height += child_natural_size.height * scale;
			}
		}
	}

	panel->nb_applets_size_hints = 0;
	if (panel->applets_hints != NULL)
		g_free (panel->applets_hints);
	panel->applets_hints = NULL;
	if (panel->applets_using_hint != NULL)
		g_free (panel->applets_using_hint);
	panel->applets_using_hint = NULL;

	if (panel->packed) {
		/* put the list in the correct order: this is important
		 * since we'll use this order in the size_allocate() */
		ad_with_hints = g_list_reverse (ad_with_hints);

		panel->nb_applets_size_hints = g_list_length (ad_with_hints);
		if (panel->nb_applets_size_hints > 0) {
			int i;
			panel->applets_hints = g_new0 (AppletSizeHints, panel->nb_applets_size_hints);

			i = 0;
			for (list = ad_with_hints;
			     list != NULL;
			     list = g_list_next (list)) {
				AppletData *ad = list->data;

				panel->applets_hints[i].hints = ad->size_hints;
				panel->applets_hints[i].len = ad->size_hints_len;
				i++;
			}

			panel->applets_using_hint = g_new0 (AppletSizeHintsAlloc, panel->nb_applets_size_hints);
		}
	}

	dont_fill = panel->packed && panel->nb_applets_size_hints != 0;

	if (panel->orient == CTK_ORIENTATION_HORIZONTAL) {
		if (minimum_size->width < 12 && !dont_fill)
			minimum_size->width = 12;
		if (minimum_size->height < 12)
			minimum_size->height = 12;
		if (natural_size->width < 12 && !dont_fill)
			natural_size->width = 12;
		if (natural_size->height < 12)
			natural_size->height = 12;
	} else {
		if (minimum_size->width < 12)
			minimum_size->width = 12;
		if (minimum_size->height < 12 && !dont_fill)
			minimum_size->height = 12;
		if (natural_size->width < 12)
			natural_size->width = 12;
		if (natural_size->height < 12 && !dont_fill)
			natural_size->height = 12;
	}
}

static void
panel_widget_get_preferred_width(CtkWidget *widget,
				 gint	   *minimum_width,
				 gint	   *natural_width)
{
	CtkRequisition req_min, req_natural;
	panel_widget_get_preferred_size(widget, &req_min, &req_natural);
	*minimum_width = req_min.width;
	*natural_width = req_natural.width;
}

static void
panel_widget_get_preferred_height(CtkWidget *widget,
				  gint	    *minimum_height,
				  gint	    *natural_height)
{
	CtkRequisition req_min, req_natural;
	panel_widget_get_preferred_size(widget, &req_min, &req_natural);
	*minimum_height = req_min.height;
	*natural_height = req_natural.height;
}

static void
queue_resize_on_all_applets(PanelWidget *panel)
{
	GList *li;
	for(li = panel->applet_list; li != NULL;
	    li = g_list_next(li)) {
		AppletData *ad = li->data;
		ctk_widget_queue_resize (ad->applet);
	}
}

static void
panel_widget_size_allocate(CtkWidget *widget, CtkAllocation *allocation)
{
	PanelWidget *panel;
	GList *list;
	int i;
	int old_size;
	gboolean ltr;

	g_return_if_fail(PANEL_IS_WIDGET(widget));
	g_return_if_fail(allocation!=NULL);

	panel = PANEL_WIDGET(widget);

	old_size = panel->size;
	ltr = ctk_widget_get_direction (widget) == CTK_TEXT_DIR_LTR;

	ctk_widget_set_allocation (widget, allocation);
	if (ctk_widget_get_realized (widget))
		cdk_window_move_resize (ctk_widget_get_window (widget),
					allocation->x,
					allocation->y,
					allocation->width,
					allocation->height);

	if(panel->orient == CTK_ORIENTATION_HORIZONTAL)
		panel->size = allocation->width;
	else
		panel->size = allocation->height;
	if(old_size<panel->size)
		panel_widget_right_stick(panel,old_size);

	if (panel->packed) {
		/* we're assuming the order is the same as the one that was
		 * in size_request() */
		int applet_using_hint_index = 0;

		i = 0;
		for(list = panel->applet_list;
		    list!=NULL;
		    list = g_list_next(list)) {
			AppletData *ad = list->data;
			CtkAllocation challoc;
			CtkRequisition chreq;
			ctk_widget_get_preferred_size (ad->applet, &chreq, NULL);

			ad->constrained = i;

			challoc.width = chreq.width;
			challoc.height = chreq.height;
			if(panel->orient == CTK_ORIENTATION_HORIZONTAL) {
				if (ad->expand_minor)
					challoc.height = allocation->height;

				if (ad->expand_major && ad->size_hints) {
					int width = panel->applets_using_hint[applet_using_hint_index].size;
					applet_using_hint_index++;
					challoc.width = MIN (width, allocation->width - i);
				}

				ad->cells = challoc.width;
				challoc.x = ltr ? ad->constrained : panel->size - ad->constrained - challoc.width;
				challoc.y = allocation->height / 2 - challoc.height / 2;
			} else {
				if (ad->expand_minor)
					challoc.width = allocation->width;

				if (ad->expand_major && ad->size_hints) {
					int height = panel->applets_using_hint[applet_using_hint_index].size;
					applet_using_hint_index++;
					challoc.height = MIN (height, allocation->height - i);
				}

				ad->cells = challoc.height;
				challoc.x = allocation->width / 2 - challoc.width / 2;
				challoc.y = ad->constrained;
			}
			ad->min_cells  = ad->cells;
			ctk_widget_size_allocate(ad->applet,&challoc);
			i += ad->cells;
		}

		/* EEEEK, there might be not enough room and we don't handle
		 * it: all the applets at the right well be unusable */

	} else { /*not packed*/

		/* First make sure there's enough room on the left */
		i = 0;
		for (list = panel->applet_list;
		     list != NULL;
		     list = g_list_next (list)) {
			AppletData *ad = list->data;
			CtkRequisition chreq;

			ctk_widget_get_preferred_size (ad->applet, &chreq, NULL);

			if (!ad->expand_major || !ad->size_hints) {
				if(panel->orient == CTK_ORIENTATION_HORIZONTAL)
					ad->cells = chreq.width;
				else
					ad->cells = chreq.height;

				ad->min_cells = ad->cells;
			} else {
				ad->cells = ad->size_hints [ad->size_hints_len - 1];
				ad->min_cells = ad->size_hints [ad->size_hints_len - 1];
			}

			ad->constrained = ad->pos;

			if (ad->constrained < i)
				ad->constrained = i;

			i = ad->constrained + ad->cells;
		}

		/* Now expand from the right */
		i = panel->size;
		for(list = g_list_last(panel->applet_list);
		    list!=NULL;
		    list = g_list_previous(list)) {
			AppletData *ad = list->data;
			int cells;

			if (ad->constrained + ad->min_cells > i)
				ad->constrained = MAX (i - ad->min_cells, 0);

			if (ad->expand_major) {
				cells = (i - ad->constrained) - 1;

				if (ad->size_hints)
					cells = get_size_from_hints (ad, cells);
				cells = MAX (cells, ad->min_cells);
				cells = MIN (cells, panel->size);

				ad->cells = cells;
			}

			i = ad->constrained;
		}

		/* EEEEK, there's not enough room, so shift applets even
		 * at the expense of perhaps running out of room on the
		 * right if there is no free space in the middle */
		if(i < 0) {
			i = 0;
			for(list = panel->applet_list;
			    list!=NULL;
			    list = g_list_next(list)) {
				AppletData *ad = list->data;

				if (ad->constrained < i)
					ad->constrained = i;

				i = ad->constrained + ad->cells;
			}
		}

		for(list = panel->applet_list;
		    list!=NULL;
		    list = g_list_next(list)) {
			AppletData *ad = list->data;
			CtkAllocation challoc;
			CtkRequisition chreq;
			ctk_widget_get_preferred_size (ad->applet, &chreq, NULL);

			challoc.width = chreq.width;
			challoc.height = chreq.height;
			if(panel->orient == CTK_ORIENTATION_HORIZONTAL) {
				challoc.width = ad->cells;
				if (ad->expand_minor) {
					challoc.height = allocation->height;
				}
				challoc.x = ltr ? ad->constrained : panel->size - ad->constrained - challoc.width;
				challoc.y = allocation->height / 2 - challoc.height / 2;
			} else {
				challoc.height = ad->cells;
				if (ad->expand_minor) {
					challoc.width = allocation->width;
				}
				challoc.x = allocation->width / 2 - challoc.width / 2;
				challoc.y = ad->constrained;
			}

			challoc.width = MAX(challoc.width, 1);
			challoc.height = MAX(challoc.height, 1);

			ctk_widget_size_allocate(ad->applet,&challoc);
		}
	}

	ctk_widget_queue_resize(widget);
}

gboolean
panel_widget_is_cursor(PanelWidget *panel, int overlap)
{
	CtkWidget     *widget;
	CtkAllocation allocation;
	CdkDevice      *device;
	int           x,y;
	int           w,h;

	g_return_val_if_fail(PANEL_IS_WIDGET(panel),FALSE);

	widget = panel->drop_widget;

	if(!widget ||
	   !CTK_IS_WIDGET(widget) ||
	   !ctk_widget_get_visible(widget))
		return FALSE;

	device = cdk_seat_get_pointer (cdk_display_get_default_seat (ctk_widget_get_display (widget)));
	cdk_window_get_device_position(ctk_widget_get_window (widget), device, &x, &y, NULL);

	ctk_widget_get_allocation (widget, &allocation);
	w = allocation.width;
	h = allocation.height;

	if((x+overlap)>=0 &&
	   (x-overlap)<=w &&
	   (y+overlap)>=0 &&
	   (y-overlap)<=h)
		return TRUE;
	return FALSE;
}

static void
panel_widget_finalize (GObject *obj)
{
	PanelWidget *panel;

	g_return_if_fail (PANEL_IS_WIDGET (obj));

	panel = PANEL_WIDGET (obj);

	if (panel->applets_hints != NULL)
		g_free (panel->applets_hints);
	panel->applets_hints = NULL;
	if (panel->applets_using_hint != NULL)
		g_free (panel->applets_using_hint);
	panel->applets_using_hint = NULL;


	G_OBJECT_CLASS (panel_widget_parent_class)->finalize (obj);
}

static void
panel_widget_open_dialog_destroyed (PanelWidget *panel_widget,
				    CtkWidget   *dialog)
{
	g_return_if_fail (panel_widget->open_dialogs != NULL);

	panel_widget->open_dialogs = g_slist_remove (panel_widget->open_dialogs, dialog);
}

static void
panel_widget_destroy_open_dialogs (PanelWidget *panel_widget)
{
	GSList *l, *list;

	list = panel_widget->open_dialogs;
	panel_widget->open_dialogs = NULL;

	for (l = list; l; l = l->next) {
		g_signal_handlers_disconnect_by_func (G_OBJECT (l->data),
				G_CALLBACK (panel_widget_open_dialog_destroyed),
				panel_widget);
		ctk_widget_destroy (l->data);
	}
	g_slist_free (list);

}

static void
panel_widget_dispose (GObject *obj)
{
	PanelWidget *panel;

	g_return_if_fail (PANEL_IS_WIDGET (obj));

	panel = PANEL_WIDGET (obj);

	panels = g_slist_remove (panels, panel);

	panel_widget_destroy_open_dialogs (panel);

	if (panel->master_widget != NULL) {
		g_object_set_data (G_OBJECT (panel->master_widget),
				   CAFE_PANEL_APPLET_ASSOC_PANEL_KEY,
				   NULL);
		g_object_remove_weak_pointer (G_OBJECT (panel->master_widget),
					      (gpointer *) &panel->master_widget);
		panel->master_widget = NULL;
	}

	G_OBJECT_CLASS (panel_widget_parent_class)->dispose (obj);
}

static void
panel_widget_init (PanelWidget *panel)
{
	CtkWidget *widget = (CtkWidget *) panel;

	ctk_widget_set_events (
		widget,
		ctk_widget_get_events (widget) | CDK_BUTTON_RELEASE_MASK);

	panel->packed        = FALSE;
	panel->orient        = CTK_ORIENTATION_HORIZONTAL;
	panel->size          = 0;
	panel->applet_list   = NULL;
	panel->master_widget = NULL;
	panel->drop_widget   = widget;
	panel->open_dialogs  = NULL;

	panel->nb_applets_size_hints = 0;
	panel->applets_hints = NULL;
	panel->applets_using_hint = NULL;

	panels = g_slist_append (panels, panel);
}

CtkWidget *
panel_widget_new (PanelToplevel  *toplevel,
		  gboolean        packed,
		  CtkOrientation  orient,
		  int             sz)
{
	PanelWidget *panel;

	panel = g_object_new (PANEL_TYPE_WIDGET, NULL);

	ctk_widget_set_has_window (CTK_WIDGET (panel), TRUE);
	ctk_widget_set_can_focus (CTK_WIDGET (panel), TRUE);

	panel->orient = orient;
	panel->sz = sz;

	panel->packed = packed;
	panel->size = 0;

	panel->toplevel    = toplevel;
	panel->drop_widget = CTK_WIDGET (toplevel);

	return CTK_WIDGET (panel);
}

static guint moving_timeout = 0;
static gboolean been_moved = FALSE;
static gboolean repeat_if_outside = FALSE;

static gboolean
panel_widget_applet_drag_start_no_grab (PanelWidget *panel,
					CtkWidget *applet,
					int drag_off)
{
	AppletData *ad;
	AppletInfo *info;

	g_return_val_if_fail (PANEL_IS_WIDGET (panel), FALSE);
	g_return_val_if_fail (CTK_IS_WIDGET (panel), FALSE);

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);
	g_return_val_if_fail (ad != NULL, FALSE);

	if (ad->locked)
		return FALSE;

	/* Check if we can actually move this object in the
	   configuration */
	info = g_object_get_data (G_OBJECT (applet), "applet_info");
	if (info != NULL &&
	    ! cafe_panel_applet_can_freely_move (info))
		return FALSE;

	if (moving_timeout != 0) {
		g_source_remove (moving_timeout);
		moving_timeout = 0;
		been_moved = FALSE;
	}

#ifdef PANEL_WIDGET_DEBUG
	g_message("Starting drag on a %s at %p\n",
		  g_type_name(G_TYPE_FROM_INSTANCE (applet)), applet);
#endif
	panel->currently_dragged_applet = ad;
	if (drag_off == PW_DRAG_OFF_CURSOR)
		ad->drag_off = panel_widget_get_cursorloc (panel) - ad->constrained;
	else if (drag_off == PW_DRAG_OFF_CENTER)
		ad->drag_off = ad->cells / 2;
	else
		ad->drag_off = drag_off;

	add_all_move_bindings (panel);

	cafe_panel_applet_in_drag = TRUE;

	return TRUE;
}


static void
panel_widget_applet_drag_end_no_grab (PanelWidget *panel)
{
	g_return_if_fail (panel != NULL);
	g_return_if_fail (PANEL_IS_WIDGET (panel));

#ifdef PANEL_WIDGET_DEBUG
	g_message("Ending drag\n");
#endif
	panel->currently_dragged_applet = NULL;
	cafe_panel_applet_in_drag = FALSE;

	remove_all_move_bindings (panel);
	if (moving_timeout != 0) {
		g_source_remove (moving_timeout);
		moving_timeout = 0;
		been_moved = FALSE;
	}
}

void
panel_widget_applet_drag_start (PanelWidget *panel,
				CtkWidget   *applet,
				int          drag_off,
				guint32      time_)
{
	CdkWindow *window;

	g_return_if_fail (PANEL_IS_WIDGET (panel));
	g_return_if_fail (CTK_IS_WIDGET (applet));

#ifdef PANEL_WIDGET_DEBUG
	g_message("Starting drag [grabbed] on a %s at %p\n",
		  g_type_name(G_TYPE_FROM_INSTANCE(applet)), applet);
#endif

	if (!panel_widget_applet_drag_start_no_grab (panel, applet, drag_off))
		return;

	panel_toplevel_push_autohide_disabler (panel->toplevel);

	ctk_grab_add (applet);

	window = ctk_widget_get_window (applet);
	if (window) {
		CdkGrabStatus  status;
		CdkCursor     *fleur_cursor;
		CdkDisplay *display;
		CdkSeat *seat;

		fleur_cursor = cdk_cursor_new_for_display (cdk_display_get_default (),
		                                           CDK_FLEUR);

		display = cdk_window_get_display (window);
		seat = cdk_display_get_default_seat (display);

		status = cdk_seat_grab (seat, window, CDK_SEAT_CAPABILITY_POINTER,
		                        FALSE, fleur_cursor, NULL, NULL, NULL);

		g_object_unref (fleur_cursor);
		cdk_display_flush (display);

		if (status != CDK_GRAB_SUCCESS) {
			g_warning (G_STRLOC ": failed to grab pointer (errorcode: %d)",
				   status);
			panel_widget_applet_drag_end (panel);
		}
	}
}

void
panel_widget_applet_drag_end (PanelWidget *panel)
{
	CdkDisplay *display;
	CdkSeat *seat;

	g_return_if_fail (PANEL_IS_WIDGET (panel));

	if (panel->currently_dragged_applet == NULL)
		return;

	display = ctk_widget_get_display (CTK_WIDGET (panel));
	seat = cdk_display_get_default_seat (display);

	cdk_seat_ungrab (seat);

	ctk_grab_remove (panel->currently_dragged_applet->applet);
	panel_widget_applet_drag_end_no_grab (panel);
	panel_toplevel_pop_autohide_disabler (panel->toplevel);
	cdk_display_flush (display);
}

/*get pos of the cursor location in panel coordinates*/
int
panel_widget_get_cursorloc (PanelWidget *panel)
{
	CdkDevice      *device;
	int x, y;
	gboolean rtl;

	g_return_val_if_fail (PANEL_IS_WIDGET (panel), -1);

	device = cdk_seat_get_pointer (cdk_display_get_default_seat (ctk_widget_get_display (CTK_WIDGET(panel))));
	cdk_window_get_device_position(ctk_widget_get_window (CTK_WIDGET(panel)), device, &x, &y, NULL);
	rtl = ctk_widget_get_direction (CTK_WIDGET (panel)) == CTK_TEXT_DIR_RTL;

	if (panel->orient == CTK_ORIENTATION_HORIZONTAL)
		return (rtl ? panel->size - x : x);
	else
		return y;
}

/*calculates the value to move the applet by*/
static int
panel_widget_get_moveby (PanelWidget *panel, int pos, int offset)
{
	g_return_val_if_fail (PANEL_IS_WIDGET (panel), -1);

	return panel_widget_get_cursorloc (panel) - offset - pos;
}

static GList *
walk_up_to (int pos, GList *list)
{
	AppletData *ad;<--- Variable 'ad' can be declared as pointer to const

	g_return_val_if_fail (list != NULL, NULL);

	ad = list->data;

	if (ad->constrained <= pos &&
	    ad->constrained + ad->cells > pos)
		return list;
	while (list->next != NULL &&
	       ad->constrained + ad->cells <= pos) {
		list = list->next;
		ad = list->data;
	}
	while (list->prev != NULL &&
	       ad->constrained > pos) {
		list = list->prev;
		ad = list->data;
	}
	return list;
}

static CtkWidget *
is_in_applet (int pos, AppletData *ad)
{
	g_return_val_if_fail (ad != NULL, NULL);

	if (ad->constrained <= pos &&
	    ad->constrained + ad->cells > pos)
		return ad->applet;
	return NULL;
}

static int
panel_widget_get_free_spot (PanelWidget *panel,
			    AppletData  *ad,<--- Parameter 'ad' can be declared as pointer to const
			    int          place)
{
	int i, e;
	int start;
	int right = -1, left = -1;
	GList *list;

	g_return_val_if_fail (PANEL_IS_WIDGET (panel), -1);
	g_return_val_if_fail (ad != NULL, -1);

	if (ad->constrained >= panel->size)
		return -1;

	if (panel->applet_list == NULL) {
		if (place + ad->min_cells > panel->size)
			return panel->size-ad->min_cells;
		else
			return place;
	}

	list = panel->applet_list;

	start = place - ad->drag_off;
	if (start < 0)
		start = 0;
	for (e = 0, i = start; i < panel->size; i++) {
		CtkWidget *applet;<--- Variable 'applet' can be declared as pointer to const
		list = walk_up_to (i, list);
		applet = is_in_applet (i, list->data);
		if (applet == NULL ||
		    applet == ad->applet) {
			e++;
			if (e >= ad->min_cells) {
				right = i - e + 1;
				break;
			}
		} else {
			e = 0;
		}
	}

	start = place + ad->drag_off;
	if (start >= panel->size)
		start = panel->size - 1;
	for (e = 0, i = start; i >= 0; i--) {
		CtkWidget *applet;<--- Variable 'applet' can be declared as pointer to const
		list = walk_up_to (i, list);
		applet = is_in_applet (i, list->data);
		if (applet == NULL ||
		    applet == ad->applet) {
			e++;
			if (e >= ad->min_cells) {
				left = i;
				break;
			}
		} else {
			e=0;
		}
	}

	start = place - ad->drag_off;

	if (left == -1) {
		if (right == -1)
			return -1;
		else
			return right;
	} else {
		if (right == -1)
			return left;
		else
			return abs (left - start) > abs (right - start) ?
				right : left;
	}
}

static void
panel_widget_nice_move (PanelWidget *panel,
			AppletData  *ad,
			int          pos)
{
	g_return_if_fail (PANEL_IS_WIDGET (panel));
	g_return_if_fail (ad != NULL);

	pos = panel_widget_get_free_spot (panel, ad, pos);
	if (pos < 0 || pos == ad->pos)
		return;

	ad->pos = ad->constrained = pos;

	panel->applet_list =
		panel_g_list_resort_item (panel->applet_list, ad,
					  (GCompareFunc)applet_data_compare);

	ctk_widget_queue_resize (CTK_WIDGET (panel));

	emit_applet_moved (panel, ad);
}

/* schedule to run the below function */
static void schedule_try_move (PanelWidget *panel, gboolean repeater);

/*find the cursor position and move the applet to that position*/
static void
panel_widget_applet_move_to_cursor (PanelWidget *panel)
{
	int moveby;
	int pos;
	int movement;
	CtkWidget *applet;
	CdkDevice      *device;
	GSList *forb;
	CdkModifierType mods;
	AppletData *ad;

	g_return_if_fail(PANEL_IS_WIDGET(panel));

	if (panel->currently_dragged_applet == NULL)
		return;

	ad = panel->currently_dragged_applet;

	pos = ad->constrained;

	applet = ad->applet;
	g_assert(CTK_IS_WIDGET(applet));
	forb = g_object_get_data (G_OBJECT(applet),
				  CAFE_PANEL_APPLET_FORBIDDEN_PANELS);

	if(!panel_widget_is_cursor(panel,10)) {
		GSList *list;

		for(list=panels;
		    list!=NULL;
		    list=g_slist_next(list)) {
			PanelWidget *new_panel =
				PANEL_WIDGET(list->data);

			if (panel != new_panel &&
			    panel_widget_is_cursor (new_panel,10) &&
			    panel_screen_from_panel_widget (panel) ==
			    panel_screen_from_panel_widget (new_panel) &&
			    !g_slist_find (forb, new_panel) &&
			    !panel_lockdown_get_locked_down ()) {
				pos = panel_widget_get_moveby (new_panel, 0, ad->drag_off);

				if (pos < 0) pos = 0;

				panel_widget_applet_drag_end (panel);

				/*disable reentrancy into this function*/
				if (!panel_widget_reparent (panel, new_panel, applet, pos)) {
					panel_widget_applet_drag_start (
						panel, applet, ad->drag_off, CDK_CURRENT_TIME);
					continue;
				}

				panel_widget_applet_drag_start (
					new_panel, applet, ad->drag_off, CDK_CURRENT_TIME);
				schedule_try_move (new_panel, TRUE);

				return;
			}
		}
	}

	device = cdk_seat_get_pointer (cdk_display_get_default_seat (ctk_widget_get_display (CTK_WIDGET (panel))));
	cdk_window_get_device_position (ctk_widget_get_window (CTK_WIDGET(panel)), device, NULL, NULL, &mods);

	movement = PANEL_SWITCH_MOVE;

	if (panel->packed) {
		movement = PANEL_SWITCH_MOVE;
	} else {
		if (mods & CDK_CONTROL_MASK)
			movement = PANEL_SWITCH_MOVE;
		else if (mods & CDK_SHIFT_MASK)
			movement = PANEL_PUSH_MOVE;
		else if (mods & CDK_MOD1_MASK)
			movement = PANEL_FREE_MOVE;
	}

	switch (movement) {
	case PANEL_SWITCH_MOVE:
		moveby = panel_widget_get_moveby (panel, pos, ad->drag_off);
		panel_widget_switch_move (panel, ad, moveby);
		break;
	case PANEL_FREE_MOVE:
		panel_widget_nice_move (panel, ad, panel_widget_get_cursorloc (panel));
		break;
	case PANEL_PUSH_MOVE:
		moveby = panel_widget_get_moveby (panel, pos, ad->drag_off);
		panel_widget_push_move (panel, ad, moveby);
		break;
	}
}

static int
move_timeout_handler(gpointer data)
{
	PanelWidget   *panel = data;
	CdkDevice      *device;

	g_return_val_if_fail(PANEL_IS_WIDGET(data),FALSE);

	if(been_moved &&
	   panel->currently_dragged_applet) {
		panel_widget_applet_move_to_cursor(panel);
		been_moved = FALSE;
		return TRUE;
	}
	been_moved = FALSE;

	if(panel->currently_dragged_applet && repeat_if_outside) {
		CtkWidget     *widget;
		CtkAllocation  allocation;
		int            x,y;
		int            w,h;

		widget = panel->currently_dragged_applet->applet;

		device = cdk_seat_get_pointer (cdk_display_get_default_seat (ctk_widget_get_display (widget)));
		cdk_window_get_device_position (ctk_widget_get_window (widget), device, &x, &y, NULL);

		ctk_widget_get_allocation (widget, &allocation);
		w = allocation.width;
		h = allocation.height;

		/* if NOT inside return TRUE, this means we will be
		 * kept inside the timeout until we hit the damn widget
		 * or the drag ends */
		if(!(x>=0 && x<=w && y>=0 && y<=h))
			return TRUE;
	}

	moving_timeout = 0;

	return FALSE;
}

static void
schedule_try_move(PanelWidget *panel, gboolean repeater)
{
	if (!panel->currently_dragged_applet)
		return;
	repeat_if_outside = repeater;
	if(moving_timeout == 0) {
		been_moved = FALSE;
		panel_widget_applet_move_to_cursor(panel);
		moving_timeout =
			g_timeout_add (50, move_timeout_handler, panel);
	} else
		been_moved = TRUE;
}

static gboolean
panel_widget_applet_button_press_event (CtkWidget      *widget,
					CdkEventButton *event)<--- Parameter 'event' can be declared as pointer to const
{
	CtkWidget   *parent;
	PanelWidget *panel;
	guint32      event_time;

	parent = ctk_widget_get_parent (widget);

	g_return_val_if_fail (PANEL_IS_WIDGET (parent), FALSE);

	panel = PANEL_WIDGET (parent);

	/* don't propagate this event */
	if (panel->currently_dragged_applet) {
		g_signal_stop_emission (G_OBJECT (widget),
					g_signal_lookup ("button-press-event",
							 G_OBJECT_TYPE (widget)),
					0);
		return TRUE;
	}

	/* Begin drag if the middle mouse button is pressed, unless the panel
	 * is locked down or a grab is active (meaning a menu is open) */
	if (panel_lockdown_get_locked_down () || event->button != 2 ||
	    ctk_grab_get_current() != NULL)
		return FALSE;

	/* time on sent events seems to be bogus */
	event_time = event->time;
	if (event->send_event)
		event_time = CDK_CURRENT_TIME;

	panel_widget_applet_drag_start (panel, widget, PW_DRAG_OFF_CURSOR, event_time);

	return TRUE;
}

static gboolean
panel_widget_applet_button_release_event (CtkWidget      *widget,
					  CdkEventButton *event)
{
	CtkWidget   *parent;
	PanelWidget *panel;

	parent = ctk_widget_get_parent (widget);

	g_return_val_if_fail (PANEL_IS_WIDGET (parent), FALSE);

	panel = PANEL_WIDGET (parent);

	/* don't propagate this event */
	if (panel->currently_dragged_applet) {
		g_signal_stop_emission (G_OBJECT (widget),
					g_signal_lookup ("button-release-event",
							 G_OBJECT_TYPE (widget)),
					0);
		panel_widget_applet_drag_end (panel);
		return TRUE;
	}

	return FALSE;
}

static gboolean
panel_widget_applet_motion_notify_event (CtkWidget *widget,
					 CdkEvent  *event)
{
	CtkWidget   *parent;
	PanelWidget *panel;

	parent = ctk_widget_get_parent (widget);

	g_return_val_if_fail (PANEL_IS_WIDGET (parent), FALSE);

	if (cdk_event_get_screen (event) != ctk_widget_get_screen (widget))
		return FALSE;

	panel = PANEL_WIDGET (parent);

	schedule_try_move (panel, FALSE);

	return FALSE;
}

static gboolean
panel_widget_applet_key_press_event (CtkWidget   *widget,
				     CdkEventKey *event)
{
	CtkWidget   *parent;
	PanelWidget *panel;

	parent = ctk_widget_get_parent (widget);

	g_return_val_if_fail (PANEL_IS_WIDGET (parent), FALSE);

	panel = PANEL_WIDGET (parent);

	if (!cafe_panel_applet_in_drag)
		return FALSE;

	return ctk_bindings_activate (G_OBJECT (panel),
				      ((CdkEventKey *)event)->keyval,
				      ((CdkEventKey *)event)->state);
}

static int
panel_sub_event_handler(CtkWidget *widget, CdkEvent *event, gpointer data)
{
	g_return_val_if_fail(CTK_IS_WIDGET(widget),FALSE);
	g_return_val_if_fail(event!=NULL,FALSE);

	switch (event->type) {
		/*pass these to the parent!*/
		case CDK_BUTTON_PRESS:
		case CDK_BUTTON_RELEASE:
		case CDK_MOTION_NOTIFY: {
			CdkEventButton *bevent = (CdkEventButton *)event;<--- Variable 'bevent' can be declared as pointer to const

			if (bevent->button != 1 || cafe_panel_applet_in_drag)
				return ctk_widget_event (data, event);

			}
			break;
		case CDK_KEY_PRESS:
			if (cafe_panel_applet_in_drag)
				return ctk_widget_event(data, event);
			break;
		default:
			break;
	}

	return FALSE;
}


static void
bind_applet_events(CtkWidget *widget, gpointer data)
{
	g_return_if_fail(CTK_IS_WIDGET(widget));

	/* XXX: This is more or less a hack.  We need to be able to
	 * capture events over applets so that we can drag them with
	 * the mouse and such.  So we need to force the applet's
	 * widgets to recursively send the events back to their parent
	 * until the event gets to the applet wrapper (the
	 * CtkEventBox) for processing by us.
	 */

	if (ctk_widget_get_has_window (widget))
		g_signal_connect (G_OBJECT(widget), "event",
				  G_CALLBACK (panel_sub_event_handler),
				  data);

	if (CTK_IS_CONTAINER(widget))
		ctk_container_foreach (CTK_CONTAINER (widget),
				       bind_applet_events, data);
}

static void
panel_widget_applet_destroy (CtkWidget *applet, gpointer data)
{
	AppletData *ad;
	CtkWidget  *parent;

	g_return_if_fail (CTK_IS_WIDGET (applet));

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);
	g_object_set_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA, NULL);

	parent = ctk_widget_get_parent (applet);
	/*if it wasn't yet removed*/
	if(parent) {
		PanelWidget *panel = PANEL_WIDGET (parent);

		if (panel->currently_dragged_applet == ad)
			panel_widget_applet_drag_end (panel);

		panel->applet_list = g_list_remove (panel->applet_list,ad);
	}

	g_free (ad->size_hints);

	g_free (ad);
}

static void
bind_top_applet_events (CtkWidget *widget)
{
	g_return_if_fail(CTK_IS_WIDGET(widget));

	g_signal_connect (G_OBJECT(widget), "destroy",
			  G_CALLBACK (panel_widget_applet_destroy),
			  NULL);

	g_signal_connect (widget, "button-press-event",
			  G_CALLBACK (panel_widget_applet_button_press_event),<--- You might need to cast the function pointer here
			  NULL);

	g_signal_connect (widget, "button-release-event",
			  G_CALLBACK (panel_widget_applet_button_release_event),
			  NULL);
	g_signal_connect (widget, "motion-notify-event",
			  G_CALLBACK (panel_widget_applet_motion_notify_event),
			  NULL);
	g_signal_connect (widget, "key-press-event",
			  G_CALLBACK (panel_widget_applet_key_press_event),
			  NULL);

	/* XXX: This is more or less a hack.  We need to be able to
	 * capture events over applets so that we can drag them with
	 * the mouse and such.  So we need to force the applet's
	 * widgets to recursively send the events back to their parent
	 * until the event gets to the applet wrapper (the
	 * CtkEventBox) for processing by us.
	 */

	if (CTK_IS_CONTAINER(widget))
		ctk_container_foreach (CTK_CONTAINER (widget),
				       bind_applet_events, widget);
}

static int
panel_widget_find_empty_pos(PanelWidget *panel, int pos)
{
	int i;
	int right=-1,left=-1;
	GList *list;

	g_return_val_if_fail(PANEL_IS_WIDGET(panel),-1);

	if(pos>=panel->size)
		pos = panel->size-1;

	if (pos <= 0)
		pos = 0;

	if(!panel->applet_list)
		return pos;

	list = panel->applet_list;

	for (i = pos; i < panel->size; i++) {
		list = walk_up_to (i, list);
		if ( ! is_in_applet (i, list->data)) {
			right = i;
			break;
		}
	}

	for(i = pos; i >= 0; i--) {
		list = walk_up_to (i, list);
		if ( ! is_in_applet (i, list->data)) {
			left = i;
			break;
		}
	}

	if (left == -1) {
		if (right == -1)
			return -1;
		else
			return right;
	} else {
		if (right == -1)
			return left;
		else
			return abs (left - pos) > abs (right - pos) ?
				right : left;
	}
}

void
panel_widget_add_forbidden (PanelWidget *panel)
{
	g_return_if_fail (panel != NULL);
	g_return_if_fail (PANEL_IS_WIDGET (panel));

	add_panel_to_forbidden (panel, panel);
}

int
panel_widget_add (PanelWidget *panel,
		  CtkWidget   *applet,
		  gboolean     locked,
		  int          pos,
		  gboolean     insert_at_pos)
{
	AppletData *ad = NULL;

	g_return_val_if_fail (PANEL_IS_WIDGET (panel), -1);
	g_return_val_if_fail (CTK_IS_WIDGET (applet), -1);

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);

	if (ad != NULL)
		pos = ad->pos;

	if (!insert_at_pos || pos < 0) {
		if (panel->packed) {
			if (get_applet_list_pos (panel, pos))
				/*this is a slight hack so that this applet
				  is inserted AFTER an applet with this pos
				  number*/
				pos++;
		} else {
			int newpos = panel_widget_find_empty_pos (panel, pos);
			if (newpos >= 0)
				pos = newpos;
			else if (get_applet_list_pos (panel, pos))
				/*this is a slight hack so that this applet
				  is inserted AFTER an applet with this pos
				  number*/
				pos++;
		}
	}

	if(pos==-1) return -1;

	if (ad == NULL) {
		ad = g_new (AppletData, 1);
		ad->applet = applet;
		ad->cells = 1;
		ad->min_cells = 1;
		ad->pos = pos;
		ad->constrained = pos;
		ad->drag_off = 0;
		ad->size_constrained = FALSE;
		ad->expand_major = FALSE;
		ad->expand_minor = FALSE;
		ad->locked = locked;
		ad->size_hints = NULL;
		g_object_set_data (G_OBJECT (applet),
				   CAFE_PANEL_APPLET_DATA, ad);

		/*this is a completely new applet, which was not yet bound*/
		bind_top_applet_events (applet);
	}

	panel->applet_list =
		g_list_insert_sorted(panel->applet_list,ad,
				     (GCompareFunc)applet_data_compare);<--- You might need to cast the function pointer here<--- You might need to cast the function pointer here

	/*this will get done right on size allocate!*/
	if(panel->orient == CTK_ORIENTATION_HORIZONTAL)
		ctk_fixed_put(CTK_FIXED(panel),applet,
			      pos,0);
	else
		ctk_fixed_put(CTK_FIXED(panel),applet,
			      0,pos);


	ctk_widget_queue_resize(CTK_WIDGET(panel));

	g_signal_emit (G_OBJECT(panel),
		       panel_widget_signals[APPLET_ADDED_SIGNAL],
		       0, applet);

	/*NOTE: forbidden list is not updated on addition, use the
	function above for the panel*/

	return pos;
}

gboolean
panel_widget_reparent (PanelWidget *old_panel,
		       PanelWidget *new_panel,
		       CtkWidget *applet,
		       int pos)
{
	AppletData *ad;
	CtkWidget *focus_widget = NULL;
	AppletInfo* info;<--- Variable 'info' can be declared as pointer to const
	CdkDisplay *display;

	g_return_val_if_fail(PANEL_IS_WIDGET(old_panel), FALSE);
	g_return_val_if_fail(PANEL_IS_WIDGET(new_panel), FALSE);
	g_return_val_if_fail(CTK_IS_WIDGET(applet), FALSE);
	g_return_val_if_fail(pos>=0, FALSE);

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);
	g_return_val_if_fail(ad!=NULL, FALSE);

	/* Don't try and reparent to an explicitly hidden panel,
	 * very confusing for the user ...
	 */
	if (panel_toplevel_get_is_hidden (new_panel->toplevel))
		return FALSE;

	info = g_object_get_data (G_OBJECT (ad->applet), "applet_info");

	ad->pos = ad->constrained = panel_widget_get_free_spot (new_panel, ad, pos);
	if (ad->pos == -1)
		ad->pos = ad->constrained = 0;

	ctk_widget_queue_resize (CTK_WIDGET (new_panel));
	ctk_widget_queue_resize (CTK_WIDGET (old_panel));

	panel_widget_reset_saved_focus (old_panel);
	if (ctk_container_get_focus_child (CTK_CONTAINER (old_panel)) == applet)
		focus_widget = ctk_window_get_focus (CTK_WINDOW (old_panel->toplevel));

	ctk_widget_reparent (applet, CTK_WIDGET (new_panel));

	if (info && info->type == PANEL_OBJECT_APPLET)
		cafe_panel_applet_frame_set_panel (CAFE_PANEL_APPLET_FRAME (ad->applet), new_panel);

	if (ctk_widget_get_can_focus (CTK_WIDGET (new_panel)))
		ctk_widget_set_can_focus (CTK_WIDGET (new_panel), FALSE);
	if (focus_widget) {
		panel_widget_force_grab_focus (focus_widget);
	} else {
		gboolean return_val;

		g_signal_emit_by_name (applet, "focus",
				       CTK_DIR_TAB_FORWARD,
				       &return_val);
	}
 	ctk_window_present (CTK_WINDOW (new_panel->toplevel));

	display = ctk_widget_get_display (CTK_WIDGET (new_panel));
	cdk_display_flush (display);

	emit_applet_moved (new_panel, ad);

	return TRUE;
}

void
panel_widget_set_packed (PanelWidget *panel_widget,
			 gboolean     packed)
{
	panel_widget->packed = packed;

	ctk_widget_queue_resize (CTK_WIDGET (panel_widget));
}

void
panel_widget_set_orientation (PanelWidget    *panel_widget,
			      CtkOrientation  orientation)
{
	panel_widget->orient = orientation;

	ctk_widget_queue_resize (CTK_WIDGET (panel_widget));
}

void
panel_widget_set_size (PanelWidget *panel_widget,
		       int          size)
{
	g_return_if_fail (PANEL_IS_WIDGET (panel_widget));

	if (size == panel_widget->sz)
		return;

	panel_widget->sz = size;

	queue_resize_on_all_applets (panel_widget);

	g_signal_emit (panel_widget, panel_widget_signals [SIZE_CHANGE_SIGNAL], 0);

	ctk_widget_queue_resize (CTK_WIDGET (panel_widget));
}

void
panel_widget_emit_background_changed (PanelWidget *panel)
{
	g_signal_emit (panel, panel_widget_signals [BACK_CHANGE_SIGNAL], 0);
}

static void
panel_widget_push_move_applet (PanelWidget     *panel,
                               CtkDirectionType dir)
{
	AppletData *applet;
	int         increment = 0;

	applet = panel->currently_dragged_applet;
	g_return_if_fail (applet);

	switch (dir) {
	case CTK_DIR_LEFT:
	case CTK_DIR_UP:
		increment = -MOVE_INCREMENT;
		break;
	case CTK_DIR_RIGHT:
	case CTK_DIR_DOWN:
		increment = MOVE_INCREMENT;
		break;
	default:
		return;
	}

	panel_widget_push_move (panel, applet, increment);
}

static void
panel_widget_switch_move_applet (PanelWidget      *panel,
                                 CtkDirectionType  dir)
{
	AppletData *applet;
	GList      *list;

	applet = panel->currently_dragged_applet;
	g_return_if_fail (applet != NULL);

	list = g_list_find (panel->applet_list, applet);
	g_return_if_fail (list != NULL);

	switch (dir) {
	case CTK_DIR_LEFT:
	case CTK_DIR_UP:
		panel_widget_switch_applet_left (panel, list);
		break;
	case CTK_DIR_RIGHT:
	case CTK_DIR_DOWN:
		panel_widget_switch_applet_right (panel, list);
		break;
	default:
		return;
	}
}

static void
panel_widget_free_move_applet (PanelWidget      *panel,
                               CtkDirectionType  dir)
{
	AppletData *ad;
	gint        increment = MOVE_INCREMENT;

	ad = panel->currently_dragged_applet;

	g_return_if_fail (ad);

	switch (dir) {
	case CTK_DIR_LEFT:
	case CTK_DIR_UP:
		increment = -increment;
		break;
	case CTK_DIR_RIGHT:
	case CTK_DIR_DOWN:
		break;
	default:
		return;
	}

	panel_widget_nice_move (panel, ad, increment + ad->constrained + ad->drag_off);
}

static void
panel_widget_tab_move (PanelWidget *panel,
                       gboolean     next)
{
	PanelWidget *new_panel = NULL;
	PanelWidget *previous_panel = NULL;
	AppletData  *ad;
	GSList      *l;

	ad = panel->currently_dragged_applet;

	if (!ad)
		return;

	for (l = panels; l; l = l->next) {
		PanelWidget *panel_in_list = l->data;

		if (panel_in_list == panel) {
			if (next) {
				if (l->next)
					new_panel = l->next->data;
				else
					new_panel = ((GSList *)panels)->data;

			} else {
				if (previous_panel)
					new_panel = previous_panel;
				else
					continue;
			}
			break;
		} else {
			if (!next)
				previous_panel = panel_in_list;
		}
	}

	g_return_if_fail (l != NULL);

	if (!new_panel && previous_panel)
		new_panel = previous_panel;

	if (new_panel &&
	    (new_panel != panel) &&
	    !panel_lockdown_get_locked_down ())
		panel_widget_reparent (panel, new_panel, ad->applet, 0);
}

static void
panel_widget_end_move (PanelWidget *panel)
{
	panel_widget_applet_drag_end (panel);
}

static gboolean
panel_widget_real_focus (CtkWidget        *widget,
                         CtkDirectionType  direction)
{
	if (ctk_widget_get_can_focus (widget) && ctk_container_get_children (CTK_CONTAINER (widget))) {
		ctk_widget_set_can_focus (widget, FALSE);
	}
	return CTK_WIDGET_CLASS (panel_widget_parent_class)->focus (widget, direction);
}

void
panel_widget_focus (PanelWidget *panel_widget)
{
	if (panel_toplevel_get_is_attached (panel_widget->toplevel))
		return;

	/*
         * Set the focus back on the panel; we unset the focus child so that
	 * the next time focus is inside the panel we do not remember the
	 * previously focused child. We also need to set CTK_CAN_FOCUS flag
	 * on the panel as it is unset when this function is called.
	 */
	ctk_container_set_focus_child (CTK_CONTAINER (panel_widget), NULL);
	ctk_widget_set_can_focus (CTK_WIDGET (panel_widget), TRUE);
	ctk_widget_grab_focus (CTK_WIDGET (panel_widget));
}


PanelOrientation
panel_widget_get_applet_orientation (PanelWidget *panel)
{
	g_return_val_if_fail (PANEL_IS_WIDGET (panel), PANEL_ORIENTATION_TOP);
	g_return_val_if_fail (PANEL_IS_TOPLEVEL (panel->toplevel), PANEL_ORIENTATION_TOP);

	return panel_toplevel_get_orientation (panel->toplevel);
}

void
panel_widget_set_applet_size_constrained (PanelWidget *panel,
					  CtkWidget   *applet,
					  gboolean     size_constrained)
{
	AppletData *ad;

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);
	if (!ad)
		return;

	size_constrained = size_constrained != FALSE;

	if (ad->size_constrained == size_constrained)
		return;

	ad->size_constrained = size_constrained;

	ctk_widget_queue_resize (CTK_WIDGET (panel));
}

void
panel_widget_set_applet_expandable (PanelWidget *panel,
				    CtkWidget   *applet,
				    gboolean     major,
				    gboolean     minor)
{
	AppletData *ad;

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);
	if (!ad)
		return;

	major = major != FALSE;
	minor = minor != FALSE;

	if (ad->expand_major == major && ad->expand_minor == minor)
		return;

	ad->expand_major = major;
	ad->expand_minor = minor;

	ctk_widget_queue_resize (CTK_WIDGET (panel));
}

void
panel_widget_set_applet_size_hints (PanelWidget *panel,
				    CtkWidget   *applet,
				    int         *size_hints,
				    int          size_hints_len)
{
	AppletData *ad;

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);
	if (!ad)
		return;

	g_free (ad->size_hints);

	if (size_hints_len > 0 && (size_hints_len % 2 == 0)) {
		ad->size_hints     = size_hints;
		ad->size_hints_len = size_hints_len;
	} else {
		g_free (size_hints);
		ad->size_hints = NULL;
	}

	ctk_widget_queue_resize (CTK_WIDGET (panel));
}

void
panel_widget_set_applet_locked (PanelWidget *panel,
				CtkWidget   *applet,
				gboolean     locked)
{
	AppletData *ad;

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);
	if (!ad)
		return;

	ad->locked = locked;
}

gboolean
panel_widget_get_applet_locked (PanelWidget *panel,
				CtkWidget   *applet)
{
	AppletData *ad;<--- Variable 'ad' can be declared as pointer to const

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);
	if (!ad)
		return FALSE;

	return ad->locked;
}

gboolean
panel_widget_toggle_applet_locked (PanelWidget *panel,
				   CtkWidget   *applet)
{
	AppletData *ad;

	ad = g_object_get_data (G_OBJECT (applet), CAFE_PANEL_APPLET_DATA);
	if (!ad)
		return FALSE;

	return ad->locked = !ad->locked;
}

gboolean
cafe_panel_applet_is_in_drag (void)
{
	return cafe_panel_applet_in_drag;
}

void
panel_widget_register_open_dialog (PanelWidget *panel,
				   CtkWidget   *dialog)
{
	/* the window is for a panel, so it should be shown in the taskbar. See
	 * HIG: An alert should not appear in the panel window list unless it
	 * is, or may be, the only window shown by an application. */
	ctk_window_set_skip_taskbar_hint (CTK_WINDOW (dialog), FALSE);

	panel->open_dialogs = g_slist_append (panel->open_dialogs,
					      dialog);

	g_signal_connect_object (dialog, "destroy",
				 G_CALLBACK (panel_widget_open_dialog_destroyed),
				 panel,
				 G_CONNECT_SWAPPED);
}