summaryrefslogtreecommitdiff
path: root/desktop/tree.c
blob: af64be83b38f4dd5bdceeb8e2c50aa54399cb921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
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
/*
 * Copyright 2004 Richard Wilson <not_ginger_matt@users.sourceforge.net>
 * Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * Generic tree handling (implementation).
 */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "content/content.h"
#include "content/hlcache.h"
#include "css/utils.h"
#include "desktop/browser.h"
#include "desktop/knockout.h"
#include "desktop/textarea.h"
#include "desktop/textinput.h"
#include "desktop/tree.h"
#include "desktop/options.h"
#include "desktop/plotters.h"
#include "render/font.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"
#include "utils/url.h"

#undef TREE_NOISY_DEBUG

#define MAXIMUM_URL_LENGTH 1024

#define TREE_TEXT_SIZE_PT 11
#define TREE_ICON_SIZE 17
#define NODE_INSTEP 20

static int tree_text_size_px;
static int TREE_LINE_HEIGHT;

static char *tree_icons_dir = NULL;

static plot_font_style_t plot_fstyle = {
	.family = PLOT_FONT_FAMILY_SANS_SERIF,
	.size = TREE_TEXT_SIZE_PT * FONT_SIZE_SCALE,
	.weight = 400,
	.flags = FONTF_NONE
};

static plot_font_style_t plot_fstyle_def_folder = {
	.family = PLOT_FONT_FAMILY_SANS_SERIF,
	.size = TREE_TEXT_SIZE_PT * FONT_SIZE_SCALE,
	.weight = 700,
	.flags = FONTF_NONE
};

static plot_font_style_t plot_fstyle_selected = {
	.family = PLOT_FONT_FAMILY_SANS_SERIF,
	.size = TREE_TEXT_SIZE_PT * FONT_SIZE_SCALE,
	.weight = 400,
	.flags = FONTF_NONE
};

static plot_font_style_t plot_fstyle_selected_def_folder = {
	.family = PLOT_FONT_FAMILY_SANS_SERIF,
	.size = TREE_TEXT_SIZE_PT * FONT_SIZE_SCALE,
	.weight = 700,
	.flags = FONTF_NONE
};

/** plot style for treeview backgrounds. */
static plot_style_t plot_style_fill_tree_background = {
	.fill_type = PLOT_OP_TYPE_SOLID
};

/** plot style for treeview backgrounds. */
static plot_style_t plot_style_fill_tree_selected = {
	.fill_type = PLOT_OP_TYPE_SOLID
};

/** plot style for treeview furniture lines. */
static plot_style_t plot_style_stroke_tree_furniture = {
	.stroke_type = PLOT_OP_TYPE_SOLID
};

/** plot style for treeview furniture fills. */
static plot_style_t plot_style_fill_tree_furniture = {
	.fill_type = PLOT_OP_TYPE_SOLID
};

struct node;
struct tree;

struct node_element_box {
	int x;				/**< X offset from origin */
	int y;				/**< Y offset from origin */
	int width;			/**< Element width */
	int height;			/**< Element height */
};

struct node_element {
	struct node *parent;		/**< Parent node */
	node_element_type type;		/**< Element type */
	struct node_element_box box;	/**< Element bounding box */
	const char *text;		/**< Text for the element */
	void *bitmap;			/**< Bitmap for the element */
	struct node_element *next;	/**< Next node element */
	unsigned int flag;		/**< Client specified flag for data
					   being represented */
	bool editable;			/**< Whether the node text can be
					 * modified, editable text is deleted
					 * without noticing the tree user
					 */
};

struct node {
	bool selected;			/**< Whether the node is selected */
	bool expanded;			/**< Whether the node is expanded */
	bool folder;			/**< Whether the node is a folder */
	bool def_folder;		/**< Whether the node is the default folder */
	bool retain_in_memory;		/**< Whether the node remains
					   in memory after deletion */
	bool deleted;			/**< Whether the node is currently
					   deleted */
	bool processing;		/**< Internal flag used when moving */
	struct node_element_box box;	/**< Bounding box of all elements */
	struct node_element data;	/**< Data to display */
	struct node *parent;		/**< Parent entry (NULL for root) */
	struct node *child;		/**< First child */
	struct node *last_child;	/**< Last child */
	struct node *previous;		/**< Previous child of the parent */
	struct node *next;		/**< Next child of the parent */

	/** Sorting function for	the node (for folder nodes only) */
	int (*sort) (struct node *, struct node *);
	/** Gets called for each deleted node_element and on node launch */
	tree_node_user_callback user_callback;
	/** User data to be passed to delete_callback */
	void *callback_data;
};

struct tree {
	struct node *root;		/* Tree root element */
	int width;			/* Tree width */
	int height;			/* Tree height */
	unsigned int flags;		/* Tree flags */
	struct textarea *textarea;	/* Handle for UTF-8 textarea */
	int ta_height;			/* Textarea height */
	struct node_element *editing;	/* Node element being edited */

	bool redraw;			/* Flag indicating whether the tree
					   should be redrawn on layout
					   changes */
	tree_drag_type drag;
	const struct treeview_table *callbacks;
	void *client_data;		/* User assigned data for the
					   callbacks */
	struct node *def_folder;	/* Node to be used for additions by default */
};

void tree_set_icon_dir(char *icon_dir)
{
	LOG(("Tree icon directory set to %s", icon_dir));
	tree_icons_dir = icon_dir;
}

/**
 * Set up colours for plot styles used in tree redraw.
 */
void tree_setup_colours(void)
{
	/* Background colour */
	plot_style_fill_tree_background.fill_colour =
			gui_system_colour_char("Window");

	/* Selection background colour */
	plot_style_fill_tree_selected.fill_colour =
			gui_system_colour_char("Highlight");

	/* Furniture line colour */
	plot_style_stroke_tree_furniture.stroke_colour = blend_colour(
			gui_system_colour_char("Window"), 
			gui_system_colour_char("WindowText"));

	/* Furniture fill colour */
	plot_style_fill_tree_furniture.fill_colour =
			gui_system_colour_char("Window");

	/* Text colour */
	plot_fstyle.foreground = gui_system_colour_char("WindowText");
	plot_fstyle.background = gui_system_colour_char("Window");
	plot_fstyle_def_folder.foreground =
			gui_system_colour_char("InfoText");
	plot_fstyle_def_folder.background =
			gui_system_colour_char("Window");

	/* Selected text colour */
	plot_fstyle_selected.foreground =
			gui_system_colour_char("HighlightText");
	plot_fstyle_selected.background =
			gui_system_colour_char("Highlight");
	plot_fstyle_selected_def_folder.foreground =
			gui_system_colour_char("HighlightText");
	plot_fstyle_selected_def_folder.background =
			gui_system_colour_char("Highlight");
}


/**
 * Creates and initialises a new tree.
 *
 * \param flags		Flag word for flags to create the new tree with
 * \param callbacks	Callback functions to support the tree in the frontend.
 * \param client_data	Data to be passed to start_redraw and end_redraw
 * \return		The newly created tree, or NULL on memory exhaustion
 */
struct tree *tree_create(unsigned int flags,
		const struct treeview_table *callbacks, void *client_data)
{
	struct tree *tree;
	char *title;

	tree = calloc(sizeof(struct tree), 1);
	if (tree == NULL) {
		LOG(("calloc failed"));
		warn_user("NoMemory", 0);
		return NULL;
	}

	title = strdup("Root");
	if (title == NULL) {
		LOG(("malloc failed"));
		warn_user("NoMemory", 0);
		free(tree);
		return NULL;
	}
	tree->root = tree_create_folder_node(NULL, NULL, title,
					     false, false, false);
	if (tree->root == NULL) {
		free(title);
		free(tree);
		return NULL;
	}
	tree->root->expanded = true;

	tree->width = 0;
	tree->height = 0;
	tree->flags = flags;
	tree->textarea = NULL;
	tree->editing = NULL;
	tree->redraw = false;
	tree->drag = TREE_NO_DRAG;
	tree->callbacks = callbacks;
	tree->client_data = client_data;

	/* Set text height in pixels */
	tree_text_size_px =
			(TREE_TEXT_SIZE_PT * FIXTOINT(nscss_screen_dpi) + 36) /
			72;
	/* Set line height appropriate for this text height in pixels
	 * Using 4/3 text height */
	TREE_LINE_HEIGHT = (tree_text_size_px * 8 + 3) / 6;

	/* But if that's too small for the icons, base the line height on
	 * the icon height. */
	if (TREE_LINE_HEIGHT < TREE_ICON_SIZE + 2)
		TREE_LINE_HEIGHT = TREE_ICON_SIZE + 2;

	tree_setup_colours();

	return tree;
}


/**
 * Recalculates the dimensions of a node element.
 *
 * \param tree	   the tree to which the element belongs, may be NULL
 * \param element  the element to recalculate
 */
static void tree_recalculate_node_element(struct tree *tree,
		struct node_element *element)
{
	struct bitmap *bitmap = NULL;
	int width, height;
	static char *cache_text = NULL;
	static int cache_size = 0;
	plot_font_style_t *fstyle;
	static plot_font_style_t *cache_fstyle = NULL;

	assert(element != NULL);

	if (element->parent->def_folder)
		fstyle = &plot_fstyle_def_folder;
	else
		fstyle = &plot_fstyle;

	switch (element->type) {
	case NODE_ELEMENT_TEXT_PLUS_ICON:
	case NODE_ELEMENT_TEXT:
		if(element->text == NULL)
			break;

		if (tree != NULL && element == tree->editing) {
			textarea_get_dimensions(tree->textarea,
						&element->box.width, NULL);
		} else {
			if ((cache_text != NULL) &&
				(strcmp(cache_text, element->text) == 0) &&
				(cache_fstyle == fstyle)) {
				element->box.width = cache_size;
				#ifdef TREE_NOISY_DEBUG
					LOG(("Tree font width cache hit"));
				#endif
			} else {
				if(cache_text != NULL) free(cache_text);
				nsfont.font_width(fstyle,
						  element->text,
						  strlen(element->text),
						  &cache_size);
				element->box.width = cache_size;
				cache_text = strdup(element->text);
				cache_fstyle = fstyle;
			}
		}

		element->box.width += 8;
		element->box.height = TREE_LINE_HEIGHT;

		if (element->type == NODE_ELEMENT_TEXT_PLUS_ICON)
			element->box.width += NODE_INSTEP;

		break;

	case NODE_ELEMENT_BITMAP:
		bitmap = element->bitmap;
		if (bitmap != NULL) {
			width = bitmap_get_width(bitmap);
			height = bitmap_get_height(bitmap);
			element->box.width = width + 1;
			element->box.height = height + 2;
		} else {
			element->box.width = 0;
			element->box.height = 0;
		}
		break;
	}
}


/**
 * Calculates the height of a node including any children
 *
 * \param node	the node to calculate the height of
 * \return the total height of the node and children
 */
static int tree_get_node_height(struct node *node)
{
	int y1;

	assert(node != NULL);

	if ((node->child == NULL) || (node->expanded == false)) {
		return node->box.height;
	}

	y1 = node->box.y;
	if (y1 < 0) {
		y1 = 0;
	}
	node = node->child;

	while ((node->next != NULL) ||
	       ((node->child != NULL) && (node->expanded))) {
		for (; node->next != NULL; node = node->next);

		if ((node->child != NULL) && (node->expanded)) {
			node = node->child;
		}
	}
	return node->box.y + node->box.height - y1;
}


/**
 * Calculates the width of a node including any children
 *
 * \param node	the node to calculate the height of
 * \return the total width of the node and children
 */
static int tree_get_node_width(struct node *node)
{
	int width = 0;
	int child_width;

	assert(node != NULL);

	for (; node != NULL; node = node->next) {
		if (width < (node->box.x + node->box.width)) {
			width = node->box.x + node->box.width;
		}

		if ((node->child != NULL) && (node->expanded)) {
			child_width = tree_get_node_width(node->child);
			if (width < child_width) {
				width = child_width;
			}
		}
	}
	return width;
}


/**
 * Recalculates the position of a node, its siblings and children.
 *
 * \param tree	the tree to which 'root' belongs
 * \param root	the root node to update from
 */
static void tree_recalculate_node_positions(struct tree *tree,
		struct node *root)
{
	struct node *parent;
	struct node *node;
	struct node *child;
	struct node_element *element;
	int y;
	bool has_icon;

	for (node = root; node != NULL; node = node->next) {

		parent = node->parent;

		if (node->previous != NULL) {
			node->box.x = node->previous->box.x;
			node->box.y = node->previous->box.y +
				tree_get_node_height(node->previous);
		} else if (parent != NULL) {
			node->box.x = parent->box.x + NODE_INSTEP;
			node->box.y = parent->box.y +
				parent->box.height;
			for (child = parent->child; child != node;
			     child = child->next)
				node->box.y += child->box.height;
		} else {
			node->box.x = tree->flags & TREE_NO_FURNITURE
				? -NODE_INSTEP + 4 : 0;
			node->box.y = -TREE_LINE_HEIGHT;
		}

		if (!node->expanded) {
			node->data.box.x = node->box.x;
			node->data.box.y = node->box.y;
			continue;
		}

		if (node->folder) {
			node->data.box.x = node->box.x;
			node->data.box.y = node->box.y;
			tree_recalculate_node_positions(tree, node->child);
		} else {
			y = node->box.y;
			has_icon = false;
			for (element = &node->data; element != NULL;
			     element = element->next)
				if (element->type ==
				    NODE_ELEMENT_TEXT_PLUS_ICON) {
					has_icon = true;
					break;
				}

			for (element = &node->data; element != NULL;
			     element = element->next) {
				element->box.x = node->box.x;
				if (element->type !=
				    NODE_ELEMENT_TEXT_PLUS_ICON &&
				    has_icon)
					element->box.x += NODE_INSTEP;
				element->box.y = y;
				y += element->box.height;
			}
		}

	}
}


/**
 * Recalculates the size of a node.
 *
 * \param tree			the tree to which node belongs, may be NULL
 * \param node			the node to update
 * \param recalculate_sizes	whether the node elements have changed
 */
static void tree_recalculate_node_sizes(struct tree *tree, struct node *node,
		bool recalculate_sizes)
{
	struct node_element *element;
	int height;

	assert(node != NULL);

	height = node->box.height;
	node->box.width = 0;
	node->box.height = 0;
	if (node->expanded) {
		for (element = &node->data; element != NULL;
		     element = element->next) {
			if (recalculate_sizes) {
				#ifdef TREE_NOISY_DEBUG
					if(element->text) LOG(("%s", element->text));
				#endif
				tree_recalculate_node_element(tree, element);
			}
			node->box.width = (node->box.width > element->box.x +
					   element->box.width - node->box.x) ?
				node->box.width :
				element->box.width + element->box.x -
				node->box.x;
			node->box.height += element->box.height;
		}
	} else {
		if (recalculate_sizes)
			for (element = &node->data; element != NULL;
			     element = element->next) {
				#ifdef TREE_NOISY_DEBUG
					if(element->text) LOG(("%s", element->text));
				#endif
				tree_recalculate_node_element(tree, element);
			}

		node->box.width = node->data.box.width;
		node->box.height = node->data.box.height;
	}

	if (tree != NULL && height != node->box.height)
		tree_recalculate_node_positions(tree, tree->root);
}


/**
 * Creates a folder node with the specified title, and optionally links it into
 * the tree.
 *
 * \param tree		    the owner tree of 'parent', may be NULL
 * \param parent	    the parent node, or NULL not to link
 * \param title		    the node title (not copied, used directly)
 * \param editable	    if true, the node title will be editable
 * \param retain_in_memory  if true, the node will stay in memory after deletion
 * \param deleted	    if true, the node is created with the deleted flag
 * \return		    the newly created node.
 */
struct node *tree_create_folder_node(struct tree *tree, struct node *parent,
		const char *title, bool editable, bool retain_in_memory,
		bool deleted)
{
	struct node *node;

	assert(title != NULL);

	node = calloc(sizeof(struct node), 1);
	if (node == NULL) {
		LOG(("calloc failed"));
		warn_user("NoMemory", 0);
		return NULL;
	}
	node->folder = true;
	node->retain_in_memory = retain_in_memory;
	node->deleted = deleted;
	node->data.parent = node;
	node->data.type = NODE_ELEMENT_TEXT;
	node->data.text = title;
	node->data.flag = TREE_ELEMENT_TITLE;
	node->data.editable = editable;
	node->sort = NULL;
	node->user_callback = NULL;
	node->previous = NULL;

	tree_recalculate_node_sizes(tree, node, true);
	if (parent != NULL)
		tree_link_node(tree, parent, node, false);

	return node;
}


/**
 * Creates a leaf node with the specified title, and optionally links it into
 * the tree.
 *
 * \param tree		    the owner tree of 'parent', may be NULL
 * \param parent	    the parent node, or NULL not to link
 * \param title		    the node title (not copied, used directly)
 * \param editable	    if true, the node title will be editable
 * \param retain_in_memory  if true, the node will stay in memory after deletion
 * \param deleted	    if true, the node is created with the deleted flag
 * \return		    the newly created node.
 */
struct node *tree_create_leaf_node(struct tree *tree, struct node *parent,
		const char *title, bool editable, bool retain_in_memory,
		bool deleted)
{
	struct node *node;

	assert(title != NULL);

	node = calloc(sizeof(struct node), 1);
	if (node == NULL) {
		LOG(("calloc failed"));
		warn_user("NoMemory", 0);
		return NULL;
	}

	node->folder = false;
	node->retain_in_memory = retain_in_memory;
	node->deleted = deleted;
	node->data.parent = node;
	node->data.type = NODE_ELEMENT_TEXT;
	node->data.text = title;
	node->data.flag = TREE_ELEMENT_TITLE;
	node->data.editable = editable;
	node->sort = NULL;
	node->user_callback = NULL;
	node->previous = NULL;

	tree_recalculate_node_sizes(tree, node, true);
	if (parent != NULL)
		tree_link_node(tree, parent, node, false);

	return node;
}


/**
 * Creates an empty text node element and links it to a node.
 *
 * \param parent  the parent node
 * \param type	  the required element type
 * \param flag	  user assigned flag used for searches
 * \return	  the newly created element.
 */
struct node_element *tree_create_node_element(struct node *parent,
		node_element_type type, unsigned int flag, bool editable)
{
	struct node_element *element;

	element = calloc(sizeof(struct node_element), 1);
	if (element == NULL)
		return NULL;

	element->parent = parent;
	element->flag = flag;
	element->type = type;
	element->editable = editable;
	element->next = parent->data.next;
	parent->data.next = element;

	return element;
}


/**
 * Inserts a node into the correct place according to the parent's sort function
 *
 * \param parent  the node whose child node 'node' becomes
 * \param node	  the node to be inserted
 */
static void tree_sort_insert(struct node *parent, struct node *node)
{
	struct node *after;

	assert(node != NULL);
	assert(parent != NULL);
	assert(parent->sort != NULL);

	after = parent->last_child;
	while ((after != NULL) &&
	       (parent->sort(node, after) == -1))
		after = after->previous;

	if (after != NULL) {
		if (after->next != NULL)
			after->next->previous = node;
		node->next = after->next;
		node->previous = after;
		after->next = node;
	} else {
		node->previous = NULL;
		node->next = parent->child;
		if (parent->child != NULL) {
			parent->child->previous = node;
		}
		parent->child = node;
	}

	if (node->next == NULL)
		parent->last_child = node;

	node->parent = parent;
}


/**
 * Recalculates the size of a tree.
 *
 * \param tree	the tree to recalculate
 */
static void tree_recalculate_size(struct tree *tree)
{
	int width, height;

	assert(tree != NULL);

	width = tree->width;
	height = tree->height;

	tree->width = tree_get_node_width(tree->root);
	tree->height = tree_get_node_height(tree->root);

	if ((width != tree->width) || (height != tree->height))
		tree->callbacks->resized(tree, tree->width, tree->height,
					 tree->client_data);
}

/**
 * Recalculate the node data and redraw the relevant section of the tree.
 *
 * \param tree		     the tree to redraw, may be NULL
 * \param node		     the node to update
 * \param recalculate_sizes  whether the elements have changed
 * \param expansion	     the request is the result of a node expansion
 */
static void tree_handle_node_changed(struct tree *tree, struct node *node,
		bool recalculate_sizes, bool expansion)
{
	int node_width, node_height, tree_width, tree_height;

	assert(node != NULL);
	assert(tree != NULL);

	node_width = node->box.width;
	node_height = node->box.height;
	tree_width = tree->width;
	tree_height = tree->height;

	if ((recalculate_sizes) || (expansion)) {
		tree_recalculate_node_sizes(tree, node, true);
	}

	if (tree != NULL) {
		if ((node->box.height != node_height) || (expansion)) {
			tree_recalculate_node_positions(tree, tree->root);
			tree_recalculate_size(tree);
			if (tree->width > tree_width)
				tree_width = tree->width;
			if (tree->height > tree_height)
				tree_height = tree->height;
			if (tree->redraw) {
				tree->callbacks->redraw_request(0, node->box.y,
						tree_width,
						tree_height - node->box.y,
						tree->client_data);
			}
		} else {
			if (node->box.width > node_width)
				node_width = node->box.width;
			if (tree->redraw)
				tree->callbacks->redraw_request(node->box.x,
						node->box.y,
						node_width, node->box.height,
						tree->client_data);
			if (recalculate_sizes) {
				tree_recalculate_size(tree);
			}
		}
	}
}


/**
 * Links a node to another node.
 *
 * \param tree	  the tree in which the link takes place, may be NULL
 * \param link	  the node to link before/as a child (folders)
 *		  or before/after (link)
 * \param node	  the node to link
 * \param before  whether to link siblings before or after the supplied node
 */
void tree_link_node(struct tree *tree, struct node *link, struct node *node,
		bool before)
{

	struct node *parent;
	bool sort = false;

	assert(link != NULL);
	assert(node != NULL);

	if ((link->folder == 0) || (before)) {
		parent = node->parent = link->parent;
		if (parent->sort) {
			sort = true;
		} else {
			if (before) {
				node->next = link;
				node->previous = link->previous;
				if (link->previous != NULL)
					link->previous->next = node;
				link->previous = node;
				if ((parent != NULL) && (parent->child == link))
					parent->child = node;
			} else {
				node->previous = link;
				node->next = link->next;
				if (link->next != NULL)
					link->next->previous = node;
				link->next = node;
				if ((parent != NULL) &&
				    (parent->last_child == link))
					parent->last_child = node;
			}
		}
	} else {
		parent = node->parent = link;
		if (parent->sort != NULL) {
			sort = true;
		} else {
			node->next = NULL;
			if (link->child == NULL) {
				link->child = link->last_child = node;
				node->previous = NULL;
			} else {
				link->last_child->next = node;
				node->previous = link->last_child;
				link->last_child = node;
			}
		}

	}

	if (sort) {
		tree_sort_insert(parent, node);
	}

	tree_handle_node_changed(tree, link, false, true);

	node->deleted = false;
}


/**
 * Recalculate the node element and redraw the relevant section of the tree.
 * The tree size is not updated.
 *
 * \param tree	   the tree to redraw, may be NULL
 * \param element  the node element to update
 */
static void tree_handle_node_element_changed(struct tree *tree,
		struct node_element *element, bool text_changed)
{
	int width, height;

	assert(element != NULL);

	width = element->box.width;
	height = element->box.height;

	if(text_changed == true) {
		#ifdef TREE_NOISY_DEBUG
			if(element->text) LOG(("%s", element->text));
		#endif
		tree_recalculate_node_element(tree, element);
	}

	if (element->box.height != height) {
		tree_recalculate_node_sizes(tree, element->parent, false);
		if ((tree != NULL) && (tree->redraw)) {
			tree->callbacks->redraw_request(0, element->box.y,
					tree->width + element->box.width -
					width,
					tree->height - element->box.y +
					element->box.height - height,
					tree->client_data);
		}
	} else {
		if (element->box.width != width) {
			tree_recalculate_node_sizes(tree, element->parent,
						    false);
		}

		if (tree != NULL) {
			width = (width > element->box.width) ? width :
				element->box.width;
			if (tree->redraw) {
				tree->callbacks->redraw_request(element->box.x,
						element->box.y,
						width,
						element->box.height,
						tree->client_data);
			}
		}
	}
}


/**
 * Stops editing a node_element
 *
 * \param tree		The tree to stop editing for
 * \param keep_changes	If true the changes made to the text will be kept,
 *			if false they will be dropped
 */
static void tree_stop_edit(struct tree *tree, bool keep_changes)
{
	int text_len;
	char *text = NULL;
	struct node_element *element;
	struct node_msg_data msg_data;
	node_callback_resp response;

	assert(tree != NULL);

	if (tree->editing == NULL || tree->textarea == NULL)
		return;

	element = tree->editing;

	if (keep_changes) {
		text_len = textarea_get_text(tree->textarea, NULL, 0);
		text = malloc(text_len * sizeof(char));
		if (text == NULL) {
			LOG(("malloc failed"));
			warn_user("NoMemory", 0);
			textarea_destroy(tree->textarea);
			tree->textarea = NULL;
			return;
		}
		textarea_get_text(tree->textarea, text, text_len);
	}


	if (keep_changes && element->parent->user_callback != NULL) {
		msg_data.msg = NODE_ELEMENT_EDIT_FINISHING;
		msg_data.flag = element->flag;
		msg_data.node = element->parent;
		msg_data.data.text = text;
		response = element->parent->user_callback(
			element->parent->callback_data,
			&msg_data);

		switch (response) {
		case NODE_CALLBACK_REJECT:
			free(text);
			text = NULL;
			break;
		case NODE_CALLBACK_CONTINUE:
			free(text);
			text = NULL;
			return;
		case NODE_CALLBACK_HANDLED:
		case NODE_CALLBACK_NOT_HANDLED:
			text = msg_data.data.text;
			break;
		}
	}

	textarea_destroy(tree->textarea);
	tree->textarea = NULL;
	tree->editing = NULL;

	if (text != NULL)
		tree_update_node_element(tree, element, text, NULL);
	else
		tree_handle_node_element_changed(tree, element, true);


	tree_recalculate_size(tree);
	if (element->parent->user_callback != NULL) {
		msg_data.msg = keep_changes ? NODE_ELEMENT_EDIT_FINISHED :
				NODE_ELEMENT_EDIT_CANCELLED;
		msg_data.flag = element->flag;
		msg_data.node = element->parent;
		element->parent->user_callback(element->parent->callback_data,
					       &msg_data);
	}
}


/**
 * Delinks a node from the tree structures.
 *
 * \param tree	the tree in which the delink takes place, may be NULL
 * \param node	the node to delink
 */
void tree_delink_node(struct tree *tree, struct node *node)
{
	struct node *parent;

	assert(node != NULL);

	/* do not remove the root */
	if (tree != NULL && node == tree->root)
		return;
	if ((tree != NULL) && (tree->editing != NULL)) {
		parent = tree->editing->parent;
		while (parent != NULL) {
			if (node == parent) {
				tree_stop_edit(tree, false);
				break;
			}
			parent = parent->parent;
		}
	}

	if (node->parent->child == node)
		node->parent->child = node->next;
	if (node->parent->last_child == node)
		node->parent->last_child = node->previous;
	parent = node->parent;
	node->parent = NULL;

	if (node->previous != NULL)
		node->previous->next = node->next;
	if (node->next != NULL)
		node->next->previous = node->previous;
	node->previous = NULL;
	node->next = NULL;

	tree_handle_node_changed(tree, parent, false, true);
}


/**
 * Deletes a node from the tree.
 *
 * \param tree	    the tree to delete from, may be NULL
 * \param node	    the node to delete
 * \param siblings  whether to delete all siblings
 */
static void tree_delete_node_internal(struct tree *tree, struct node *node,
		bool siblings)
{
	struct node *next, *child, *parent;
	struct node_element *e, *f;
	node_callback_resp response;
	struct node_msg_data msg_data;

	assert(node != NULL);

	if (tree != NULL && tree->root == node)
		return;

	next = node->next;
	parent = node->parent;
	if (tree != NULL && parent == tree->root)
		parent = NULL;
	if ((tree != NULL) && (tree->def_folder == node))
		tree->def_folder = NULL;
	tree_delink_node(tree, node);
	child = node->child;
	node->child = NULL;

	node->deleted = true;
	if (child != NULL)
		tree_delete_node_internal(tree, child, true);

	if (!node->retain_in_memory) {
		node->retain_in_memory = true;
		for (e = &node->data; e != NULL; e = f) {
			if (e->text != NULL) {
				response = NODE_CALLBACK_NOT_HANDLED;
				if (!e->editable &&
				    node->user_callback != NULL) {
					msg_data.msg = NODE_DELETE_ELEMENT_TXT;
					msg_data.flag = e->flag;
					msg_data.node = node;
					msg_data.data.text = (void *)e->text;
					response = node->user_callback(
						node->callback_data,
						&msg_data);
				}
				if (response != NODE_CALLBACK_HANDLED)
					free((void *)e->text);
				e->text = NULL;
			}
			if (e->bitmap != NULL) {
				response = NODE_CALLBACK_NOT_HANDLED;
				if (node->user_callback != NULL) {
					msg_data.msg = NODE_DELETE_ELEMENT_IMG;
					msg_data.flag = e->flag;
					msg_data.node = node;
					msg_data.data.bitmap =
						(void *)e->bitmap;
					response = node->user_callback(
						node->callback_data,
						&msg_data);
				}
				/* TODO the type of this field is platform
				   dependent */
				if (response != NODE_CALLBACK_HANDLED)
					free(e->bitmap);
				e->bitmap = NULL;
			}
			f = e->next;
			if (e != &node->data)
				free(e);
		}
		free(node);
	}

	if (siblings && next)
		tree_delete_node_internal(tree, next, true);
	if ((tree->flags & TREE_DELETE_EMPTY_DIRS) && parent != NULL &&
	    parent->child == NULL && !parent->deleted)
		tree_delete_node_internal(tree, parent, false);
}


/**
 * Deletes all nodes of a tree and the tree itself.
 *
 * \param tree the tree to be deleted
 */
void tree_delete(struct tree *tree)
{
	tree->redraw = false;

	if (tree->root->child != NULL)
		tree_delete_node_internal(tree, tree->root->child, true);

	free((void *)tree->root->data.text);
	free(tree->root);
	free(tree);
}


/**
 * Gets the redraw property of the given tree.
 *
 * \param tree	the tree for which to retrieve the property
 * \return	the redraw property of the tree
 */
bool tree_get_redraw(struct tree *tree)
{
	return tree->redraw;
}


/**
 * Deletes a node from the tree.
 *
 * \param tree	    the tree to delete from, may be NULL
 * \param node	    the node to delete
 * \param siblings  whether to delete all siblings
 */
void tree_delete_node(struct tree *tree, struct node *node, bool siblings)
{
	int y = node->box.y;
	int height = tree->height;
	int width = tree->width;
	bool redraw_setting = tree->redraw;

	tree->redraw = false;

	tree_delete_node_internal(tree, node, siblings);
	tree_recalculate_node_positions(tree, tree->root);

	tree->redraw = redraw_setting;

	if (tree->redraw)
		tree->callbacks->redraw_request(0, y,
				width, height, tree->client_data);
	tree_recalculate_size(tree);
}


/**
 * Sets an icon for a node
 *
 * \param tree		The tree to which node belongs, may be NULL
 * \param node		The node for which the icon is set
 * \param icon		the image to use
 */
void tree_set_node_icon(struct tree *tree, struct node *node,
		hlcache_handle *icon)
{
	node->data.type = NODE_ELEMENT_TEXT_PLUS_ICON;
	tree_update_node_element(tree, &(node->data), NULL, icon);
}


/**
 * Updates all siblings and descendants of a node to an expansion state.
 * No update is performed for the tree changes.
 *
 * \param tree		the tree to which 'node' belongs
 * \param node		the node to set all siblings and descendants of
 * \param expanded	the expansion state to set
 */
static void tree_set_node_expanded_all(struct tree *tree, struct node *node,
		bool expanded)
{
	for (; node != NULL; node = node->next) {
		if (node->expanded != expanded) {
			node->expanded = expanded;
			tree_recalculate_node_sizes(tree, node, false);
		}
		if ((node->child != NULL) && (node->expanded))
			tree_set_node_expanded_all(tree, node->child, expanded);
	}
}


/**
 * Updates [all siblings and descendants of] a node to an expansion state.
 *
 * \param tree	    the tree to update
 * \param node	    the node to set [all siblings and descendants of]
 * \param expanded  the expansion state to set
 * \param folder    whether to update folders, if this together with leaf
 *		    will be false only 'node' will be updated
 * \param leaf	    whether to update leaves (check also description for folder)
 * \return	    whether any changes were made
 */
static bool tree_set_node_expanded_internal(struct tree *tree,
		struct node *node, bool expanded, bool folder, bool leaf)
{
	bool redraw = false;
	struct node *end = (folder == false && leaf == false) ?
		node->next : NULL;

	if (tree->editing != NULL && node == tree->editing->parent)
		tree_stop_edit(tree, false);

	for (; node != end; node = node->next) {
		if ((node->expanded != expanded) && (node != tree->root) &&
		    ((folder && (node->folder)) ||
		     (leaf && (!node->folder)) ||
		     (!folder && !leaf))) {
			node->expanded = expanded;
			if (node->child != NULL)
				tree_set_node_expanded_all(tree,
							   node->child, false);
			if ((node->data.next != NULL) &&
			    (node->data.next->box.height == 0))
				tree_recalculate_node_sizes(tree, node, true);
			else
				tree_recalculate_node_sizes(tree, node, false);
			redraw = true;
		}
		if ((folder || leaf) && (node->child != NULL) &&
		    (node->expanded))
			redraw |= tree_set_node_expanded_internal(tree,
								  node->child, expanded, folder, leaf);
	}
	return redraw;
}


/**
 * Updates [all siblings and descendants of] a node to an expansion state.
 *
 * \param tree	    the tree to update
 * \param node	    the node to set [all siblings and descendants of]
 * \param expanded  the expansion state to set
 * \param folder    whether to update folders, if this together with leaf
 *		    will be false only 'node' will be updated
 * \param leaf	    whether to update leaves (check also description for folder)
 */
void tree_set_node_expanded(struct tree *tree, struct node *node, bool expanded,
		bool folder, bool leaf)
{
	if (tree_set_node_expanded_internal(tree, node, expanded, folder, leaf))
		tree_handle_node_changed(tree, node, false, true);
}


/**
 * Updates a node to an selected state. The required areas of the tree are
 * redrawn.
 *
 * \param tree	    the tree to update nodes for, may be NULL
 * \param node	    the node to set all siblings and descendants of
 * \param all	    if true update node together with its siblings and
 *		    descendants
 * \param selected  the selection state to set
 */
void tree_set_node_selected(struct tree *tree, struct node *node, bool all,
		bool selected)
{
	struct node *end;

	if (tree != NULL && node == tree->root)
		node = tree->root->child;
	if (node == NULL)
		return;

	end = all ? NULL : node->next;

	for (; node != end; node = node->next) {
		if (node->selected != selected) {
			node->selected = selected;
			if (tree != NULL && tree->redraw)
				tree->callbacks->redraw_request(
						node->data.box.x,
						node->data.box.y,
						node->data.box.width,
						node->data.box.height,
						tree->client_data);
		}
		if (all && (node->child != NULL) && (node->expanded))
			tree_set_node_selected(tree, node->child, all,
					       selected);
	}
}


/**
 * Sets the sort function for a node
 *
 * \param tree	the tree to which 'node' belongs, may be NULL
 * \param node	the node to be inserted
 * \param sort	pointer to the sorting function
 */
void tree_set_node_sort_function(struct tree *tree, struct node *node,
		int (*sort) (struct node *, struct node *))
{
	struct node *child;

	node->sort = sort;

	if (tree != NULL && tree->editing != NULL)
		tree_stop_edit(tree, false);

	/* the node had already some children so they must get sorted */
	if (node->child != NULL) {

		child = node->child;
		node->child = NULL;

		while (child != NULL) {
			tree_sort_insert(node, child);
			child = child->next;
		}

	}

	if (tree != NULL)
		tree_recalculate_node_positions(tree, node->child);
}


/**
 * Sets the delete callback for a node.
 *
 * \param node	    the node for which the callback is set
 * \param callback  the callback functions to be set
 * \param data	    user data to be passed to callback
 */
void tree_set_node_user_callback(struct node *node,
		tree_node_user_callback callback, void *data)
{
	node->user_callback = callback;
	node->callback_data = data;
}


/**
 * Sets the redraw property to the given value. If redraw is true, the tree will
 * be redrawn on layout/appearance changes.
 *
 * \param tree	  the tree for which the property is set
 * \param redraw  the value to set
 */
void tree_set_redraw(struct tree *tree, bool redraw)
{
	/* the tree might have no graphical representation, do not set the
	   redraw flag in such case */
	if (tree->callbacks == NULL)
		return;
	tree->redraw = redraw;
}


/**
 * Checks whether a node, its siblings or any children are selected.
 *
 * \param node	the root node to check from
 * \return	whether 'node', its siblings or any children are selected.
 */
bool tree_node_has_selection(struct node *node)
{
	for (; node != NULL; node = node->next) {
		if (node->selected)
			return true;
		if ((node->child != NULL) && (node->expanded) &&
		    (tree_node_has_selection(node->child)))
			return true;
	}
	return false;
}


/**
 * Returns the current value of the nodes deleted property.
 *
 * \param node	the node to be checked
 * \return	the current value of the nodes deleted property
 */
bool tree_node_is_deleted(struct node *node)
{
	return node->deleted;
}


/**
 * Returns true if the node is a folder
 *
 * \param node	the node to be checked
 * \return	true if the node is a folder, false otherwise
 */
bool tree_node_is_folder(struct node *node)
{
	return node->folder;
}


/**
 * Returns true if the node is the default folder for a tree
 *
 * \param node	the node to be checked
 * \return	true if the node is a default folder, false otherwise
 */
bool tree_node_is_default(struct node *node)
{
	return node->def_folder;
}


/**
 * Update the text of a node element if it has changed.
 *
 * \param element The node element to update.
 * \param text The text to update the element with. The ownership of
 *	       this string is taken by this function and must not be
 *	       referred to after the function exits.
 */
bool tree_update_element_text(struct tree *tree,
		struct node_element *element, char *text)
{
	const char *node_text; /* existing node text */

	if (text == NULL)
		return false;

	if (element == NULL) {
		free(text);
		return false;
	}

	node_text = tree_node_element_get_text(element);

	if ((node_text == NULL) || (strcmp(node_text, text) != 0)) {
		tree_update_node_element(tree, element, text, NULL);
	} else {
		/* text does not need changing, free it */
		free(text);
	}
	return true;
}


/**
 * Updates the content of a node_element.
 *
 * \param tree	    the tree owning element, may be NULL
 * \param element   the element to be updated
 * \param text	    new text to be set, may be NULL
 * \param bitmap    new bitmap to be set, may be NULL
 */
void tree_update_node_element(struct tree *tree, struct node_element *element,
		const char *text, void *bitmap)
{
	node_callback_resp response;
	struct node_msg_data msg_data;
	bool text_changed = false;

	assert(element != NULL);

	if (tree != NULL && element == tree->editing)
		tree_stop_edit(tree, false);

	if (text != NULL && (element->type == NODE_ELEMENT_TEXT ||
			     element->type == NODE_ELEMENT_TEXT_PLUS_ICON)) {
		if (element->text != NULL) {
			if(strcmp(element->text, text) == 0) text_changed = true;

			response = NODE_CALLBACK_NOT_HANDLED;
			if (!element->editable &&
			    element->parent->user_callback !=
			    NULL) {
				msg_data.msg = NODE_DELETE_ELEMENT_TXT;
				msg_data.flag = element->flag;
				msg_data.node = element->parent;
				msg_data.data.text = (void *)element->text;
				response = element->parent->user_callback(
					element->parent->callback_data,
					&msg_data);
			}
			if (response != NODE_CALLBACK_HANDLED)
				free((void *)element->text);
		}
		element->text = text;
	}

	if (bitmap != NULL && (element->type == NODE_ELEMENT_BITMAP ||
			       element->type == NODE_ELEMENT_TEXT_PLUS_ICON)) {
		if (element->bitmap != NULL) {
			response = NODE_CALLBACK_NOT_HANDLED;
			if (element->parent->user_callback != NULL) {
				msg_data.msg = NODE_DELETE_ELEMENT_IMG;
				msg_data.flag = element->flag;
				msg_data.node = element->parent;
				msg_data.data.bitmap = (void *)element->bitmap;
				response = element->parent->user_callback(
					element->parent->callback_data,
					&msg_data);
			}
			if (response != NODE_CALLBACK_HANDLED)
				free(element->bitmap);
		}
		else {
			/* Increase the box width to accomodate the new icon */
			element->box.width += NODE_INSTEP;
		}

		element->bitmap = bitmap;
	}

	tree_handle_node_element_changed(tree, element, text_changed);
}


/**
 * Returns the node element's text
 *
 * \return  the node element's text
 */
const char *tree_node_element_get_text(struct node_element *element)
{
	return element->text;
}


/**
 * Get the root node of a tree
 *
 * \param tree	the tree to get the root of
 * \return	the root of the tree
 */
struct node *tree_get_root(struct tree *tree)
{
	return tree->root;
}


/**
 * Returns whether the current tree is being edited at this time
 *
 * \param tree	the tree to be checked
 * \return	true if the tree is currently being edited
 */
bool tree_is_edited(struct tree *tree)
{
	return tree->editing == NULL ? false : true;
}


/**
 * Get the drag state of a tree
 *
 * \param tree	the tree to get the state of
 * \return	drag type (defined in desktop/tree.h)
 */
tree_drag_type tree_drag_status(struct tree *tree)
{
	return tree->drag;
}


/**
 * Get the default node of a tree for additions
 *
 * \param tree	the tree to get the default node of
 * \return	the default node
 */
struct node *tree_get_default_folder_node(struct tree *tree)
{
	if (tree->def_folder != NULL) {
		return tree->def_folder;
	} else {
		return tree_get_root(tree);
	}
}


/**
 * Set the default node of a tree to the selected node
 *
 * \param tree	the tree to set the default node of
 * \param node  the node to set as default (NULL for selected node)
 * \return	success
 */
bool tree_set_default_folder_node(struct tree *tree, struct node *node)
{
	struct node *sel_node;

	if (node == NULL) {
		sel_node = tree_get_selected_node(tree->root);
	} else {
		sel_node = node;
	}

	if((sel_node == NULL) ||
		(tree_node_is_folder(sel_node) == false)) {
		return false;
	}

	tree_clear_default_folder_node(tree);

	tree->def_folder = sel_node;
	sel_node->def_folder = true;
	tree_handle_node_changed(tree, sel_node, true, false);

	return true;
}


/**
 * Clear the default node of a tree
 *
 * \param tree	the tree to clear the default node of
 */
void tree_clear_default_folder_node(struct tree *tree)
{
	struct node *def_node = NULL;
	def_node = tree_get_default_folder_node(tree);

	if (def_node != NULL) {
		tree->def_folder = NULL;
		def_node->def_folder = false;
		tree_handle_node_changed(tree, def_node, true, false);
	}
}


/**
 * Returns the parent of a node
 *
 * \param node	the node to get the parent of
 * \return	the node's parent
 */
struct node *tree_node_get_parent(struct node *node)
{
	return node->parent;
}


/**
 * Returns the first child of a node
 *
 * \param node	the node to get the child of
 * \return	the nodes first child
 */
struct node *tree_node_get_child(struct node *node)
{
	return node->child;
}


/**
 * Returns the closest sibling a node
 *
 * \param node	the node to get the sibling of
 * \return	the nodes sibling
 */
struct node *tree_node_get_next(struct node *node)
{
	return node->next;
}


/**
 * Draws an element's expansion icon
 *
 * \param tree		the tree to draw the expansion for
 * \param element	the element to draw the expansion for
 * \param tree_x	X coordinate of the tree
 * \param tree_y	Y coordinate of the tree
 * \param ctx		current redraw context
 */
static void tree_draw_node_expansion_toggle(struct tree *tree,
		struct node *node, int tree_x, int tree_y,
		const struct redraw_context *ctx)
{
	const struct plotter_table *plot = ctx->plot;
	int x, y;

	assert(tree != NULL);
	assert(node != NULL);

	if ((node->child != NULL) || (node->data.next != NULL)) {
		x = tree_x + node->box.x - (NODE_INSTEP / 2) - 4;
		y = tree_y + node->box.y + (TREE_LINE_HEIGHT - 9) / 2;
		plot->rectangle(x, y, x + 9, y + 9,
				&plot_style_fill_tree_furniture);
		plot->rectangle(x , y, x + 8, y + 8,
				&plot_style_stroke_tree_furniture);
		plot->line(x + 2, y + 4, x + 7, y + 4,
				&plot_style_stroke_tree_furniture);
		if (!node->expanded)
			plot->line(x + 4, y + 2, x + 4, y + 7,
					&plot_style_stroke_tree_furniture);

	}

}


/**
 * Draws an element, including any expansion icons
 *
 * \param tree		the tree to draw an element for
 * \param element	the element to draw
 * \param tree_x	X coordinate to draw the tree at (wrt plot origin)
 * \param tree_y	Y coordinate to draw the tree at (wrt plot origin)
 * \param clip		clipping rectangle (wrt plot origin)
 * \param ctx		current redraw context
 */
static void tree_draw_node_element(struct tree *tree,
		struct node_element *element, int tree_x, int tree_y,
		const struct rect *clip, const struct redraw_context *ctx)
{
	const struct plotter_table *plot = ctx->plot;
	struct bitmap *bitmap = NULL;
	int x, y, width;
	bool selected = false;
	bool def_folder = false;
	hlcache_handle *icon;
	plot_font_style_t *fstyle;
	const int icon_inset = (TREE_LINE_HEIGHT - TREE_ICON_SIZE) / 2;

	assert(tree != NULL);
	assert(element != NULL);
	assert(element->parent != NULL);

	x = tree_x + element->box.x;
	y = tree_y + element->box.y;
	width = element->box.width;
	if (&element->parent->data == element) {
		if (element->parent->selected)
			selected = true;
		if (element->parent->def_folder)
			def_folder = true;
	}

	switch (element->type) {
	case NODE_ELEMENT_TEXT_PLUS_ICON:
		icon = element->bitmap;
		if (icon != NULL && (content_get_status(icon) ==
				CONTENT_STATUS_READY ||
				content_get_status(icon) ==
				CONTENT_STATUS_DONE) &&
				x + TREE_ICON_SIZE > clip->x0 &&
				x < clip->x1) {
			struct rect c;
			/* Clip to image area */
			c.x0 = x;
			c.y0 = y + icon_inset;
			c.x1 = x + TREE_ICON_SIZE;
			c.y1 = y + icon_inset + TREE_ICON_SIZE;
			if (c.x0 < clip->x0) c.x0 = clip->x0;
			if (c.y0 < clip->y0) c.y0 = clip->y0;
			if (c.x1 > clip->x1) c.x1 = clip->x1;
			if (c.y1 > clip->y1) c.y1 = clip->y1;

			if (c.x1 > c.x0 && c.y1 > c.y0) {
				/* Valid clip rectangles only */
				struct content_redraw_data data;

				plot->clip(&c);

				data.x = x;
				data.y = y + icon_inset;
				data.width = TREE_ICON_SIZE;
				data.height = TREE_ICON_SIZE;

				data.background_colour = 0xFFFFFF;
				data.scale = 1;
				data.repeat_x = false;
				data.repeat_y = false;

				content_redraw(icon, &data, &c, ctx);

				/* Restore previous clipping area */
				plot->clip(clip);
			}
		}

		x += NODE_INSTEP;
		width -= NODE_INSTEP;

		/* fall through */
	case NODE_ELEMENT_TEXT:
		if (element->text == NULL || clip->x1 < x)
			break;

		if (element == tree->editing)
			return;

		if (selected) {
			if (def_folder == true)
				fstyle = &plot_fstyle_selected_def_folder;
			else
				fstyle = &plot_fstyle_selected;

			plot->rectangle(x, y, x + width,
				       y + element->box.height,
				       &plot_style_fill_tree_selected);
		} else {
		if (def_folder == true)
			fstyle = &plot_fstyle_def_folder;
		else
			fstyle = &plot_fstyle;
		}

		plot->text(x + 4, y + (TREE_LINE_HEIGHT * 3 + 2) / 4,
				element->text, strlen(element->text),
				fstyle);
		break;
	case NODE_ELEMENT_BITMAP:
		bitmap = element->bitmap;
		if (bitmap == NULL)
			break;
		plot->bitmap(x, y, element->box.width - 1,
			    element->box.height - 2,
			    bitmap, 0xFFFFFF, BITMAPF_NONE);
		if (!(tree->flags & TREE_NO_FURNITURE))
			plot->rectangle(x, y, x + element->box.width - 1,
				       y + element->box.height - 3,
				       &plot_style_stroke_tree_furniture);

		break;
	}

}


/**
 * Redraws a node.
 *
 * \param tree		the tree to draw
 * \param node		the node to draw children and siblings of
 * \param tree_x	X coordinate to draw the tree at (wrt plot origin)
 * \param tree_y	Y coordinate to draw the tree at (wrt plot origin)
 * \param clip		clipping rectangle (wrt plot origin)
 * \param ctx		current redraw context
 */
static void tree_draw_node(struct tree *tree, struct node *node,
		int tree_x, int tree_y, struct rect clip,
		const struct redraw_context *ctx)
{
	const struct plotter_table *plot = ctx->plot;
	struct node_element *element;
	struct node *parent;
	int x0, y0, x1, y1;
	struct rect node_extents;

	assert(tree != NULL);
	assert(node != NULL);

	/* Find node's extents, including children's area */
	node_extents.x0 = tree_x + node->box.x - NODE_INSTEP;
	node_extents.y0 = tree_y + node->box.y;
	node_extents.x1 = tree_x + node->box.x + node->box.width + NODE_INSTEP;
	if (node->next != NULL)
		node_extents.y1 = tree_y + node->next->box.y;
	else
		node_extents.y1 = tree_y + node->box.y + node->box.height;

	/* Nothing to draw, if node is outside clip region */
	if ((node_extents.x1 < clip.x0) && (node_extents.y1 < clip.y0) &&
			(node_extents.x0 > clip.x1) &&
			(node_extents.y0 > clip.y1)) {
		return;
	}

	/* Intersect clip region with node's extents */
	if (clip.x0 < node_extents.x0) clip.x0 = node_extents.x0;
	if (clip.y0 < node_extents.y0) clip.y0 = node_extents.y0;
	if (clip.x1 > node_extents.x1) clip.x1 = node_extents.x1;
	if (clip.y1 > node_extents.y1) clip.y1 = node_extents.y1;

	if (clip.x0 >= clip.x1 || clip.y0 >= clip.y1) {
		/* Invalid clip rectangle */
		return;
	}

	/* Set up the clipping area */
	plot->clip(&clip);

	/* Draw node's furniture */
	if (!(tree->flags & TREE_NO_FURNITURE)) {
		/* Display furniture */
		if (node->previous != NULL) {
			/* There is a node above this
			 * Display furniture; line connecting up to previous */
			x0 = x1 = tree_x + node->box.x - (NODE_INSTEP / 2);
			y0 = tree_y + node->previous->box.y;
			y1 = tree_y + node->box.y + (TREE_LINE_HEIGHT / 2);
			plot->line(x0, y0, x1, y1,
					&plot_style_stroke_tree_furniture);
		}
		if (node->next != NULL) {
			/* There is a node below this
			 * Display furniture; line connecting down to next */
			x0 = x1 = tree_x + node->box.x - (NODE_INSTEP / 2);
			y0 = tree_y + node->box.y + (TREE_LINE_HEIGHT / 2);
			y1 = tree_y + node->next->box.y;
			plot->line(x0, y0, x1, y1,
					&plot_style_stroke_tree_furniture);
		}

		parent = node->parent;
		if ((parent != NULL) && (parent != tree->root) &&
				(parent->child == node)) {
			/* Node is first child */
			x0 = x1 = tree_x + parent->box.x + (NODE_INSTEP / 2);
			y0 = tree_y + parent->data.box.y +
					parent->data.box.height;
			y1 = y0 + (TREE_LINE_HEIGHT / 2);
			plot->line(x0, y0, x1, y1,
					&plot_style_stroke_tree_furniture);
		}
		/* Line from expansion toggle to icon */
		x0 = tree_x + node->box.x - (NODE_INSTEP / 2);
		x1 = x0 + (NODE_INSTEP / 2) - 2;
		y0 = y1 = tree_y + node->data.box.y + node->data.box.height -
				(TREE_LINE_HEIGHT / 2);
		plot->line(x0, y0, x1, y1, &plot_style_stroke_tree_furniture);

		tree_draw_node_expansion_toggle(tree, node,
				tree_x, tree_y, ctx);
	}

	/* Draw node's element(s)
	 * NOTE: node's children are handled later in tree_draw_tree() */
	if (node->expanded) {
		for (element = &node->data; element != NULL;
				element = element->next) {
			/* Draw each element of expanded node */
			tree_draw_node_element(tree, element, tree_x, tree_y,
					&clip, ctx);
		}
	} else {
		/* Draw main title element of node */
		tree_draw_node_element(tree, &node->data, tree_x, tree_y,
				&clip, ctx);
	}
}


/**
 * Redraws a node's descendants.
 *
 * \param tree		the tree to draw
 * \param node		the node to draw children and siblings of
 * \param tree_x	X coordinate to draw the tree at (wrt plot origin)
 * \param tree_y	Y coordinate to draw the tree at (wrt plot origin)
 * \param clip		clipping rectangle (wrt plot origin)
 * \param ctx		current redraw context
 */
static void tree_draw_tree(struct tree *tree, struct node *node,
		int tree_x, int tree_y, struct rect clip,
		const struct redraw_context *ctx)
{
	struct node *child;

	assert(tree != NULL);
	assert(node != NULL);

	for (child = node->child; child != NULL; child = child->next) {
		/* Draw children that are inside the clip region */

		if (child->next != NULL &&
				(child->next->box.y + tree_y < clip.y0))
			/* Child is above clip region */
			continue;
		if (child->box.y + tree_y > clip.y1)
			/* Child is below clip region
			 * further siblings will be too */
			return;

		/* Draw current child */
		tree_draw_node(tree, child, tree_x, tree_y, clip, ctx);
		/* And its children */
		if ((child->child != NULL) && (child->expanded)) {
			/* Child has children and they are visible */
			tree_draw_tree(tree, child, tree_x, tree_y, clip, ctx);
		}
	}
}


/**
 * Redraws a tree.
 *
 * \param tree		the tree to draw
 * \param x		X coordinate to draw the tree at (wrt plot origin)
 * \param y		Y coordinate to draw the tree at (wrt plot origin)
 * \param clip_x	minimum x of the clipping rectangle (wrt tree origin)
 * \param clip_y	minimum y of the clipping rectangle (wrt tree origin)
 * \param clip_width	width of the clipping rectangle
 * \param clip_height	height of the clipping rectangle
 * \param ctx		current redraw context
 */
void tree_draw(struct tree *tree, int x, int y,
		int clip_x, int clip_y, int clip_width, int clip_height,
		const struct redraw_context *ctx)
{
	struct redraw_context new_ctx = *ctx;
	struct rect clip;

	assert(tree != NULL);
	assert(tree->root != NULL);

	/* Start knockout rendering if it's available for this plotter */
	if (ctx->plot->option_knockout)
		knockout_plot_start(ctx, &new_ctx);

	/* Set up clip rectangle */
	clip.x0 = x + clip_x;
	clip.y0 = y + clip_y;
	clip.x1 = clip.x0 + clip_width;
	clip.y1 = clip.y0 + clip_height;
	new_ctx.plot->clip(&clip);

	/* Flat fill extents of clipping area */
	new_ctx.plot->rectangle(clip.x0, clip.y0, clip.x1, clip.y1,
		       &plot_style_fill_tree_background);

	/* don't draw empty trees or trees with redraw flag set to false */
	if (tree->root->child != NULL && tree->redraw) {

		/* Draw the tree */
		tree_draw_tree(tree, tree->root, x, y, clip, &new_ctx);

		/* Draw textarea, if present */
		if (tree->editing != NULL) {
			x = x + tree->editing->box.x;
			y = y + tree->editing->box.y;
			if (tree->editing->type == NODE_ELEMENT_TEXT_PLUS_ICON)
				x += NODE_INSTEP;
			textarea_redraw(tree->textarea, x, y,
					plot_style_fill_tree_background.
							fill_colour,
					1.0, &clip, &new_ctx);
		}
	}

	/* Rendering complete */
	if (ctx->plot->option_knockout)
		knockout_plot_end();
}


/**
 * Finds a node element from a node with a specific user_type
 *
 * \param node	the node to examine
 * \param flag	user assinged flag used is searches
 * \param after if this is not NULL the search will start after the given
 *		node_element
 * \return	the corresponding element
 */
struct node_element *tree_node_find_element(struct node *node,
		unsigned int flag, struct node_element *after)
{
	struct node_element *element;

	if (after == NULL)
		element = &node->data;
	else {
		assert(after->parent == node);
		element = after->next;
	}

	for (; element != NULL; element = element->next)
		if (element->flag == flag) return element;

	return NULL;
}


/**
 * Deletes all selected nodes from the tree.
 *
 * \param tree	the tree to delete from
 * \param node	the node to delete
 */
void tree_delete_selected_nodes(struct tree *tree, struct node *node)
{
	struct node *next;
	int y = node->box.y;
	int height = tree->height;
	int width = tree->width;
	bool redraw_setting = tree->redraw;

	tree->redraw = false;

	if (node == tree->root) {
		if (node->child != NULL)
			tree_delete_selected_nodes(tree, node->child);

		tree->redraw = redraw_setting;

		if (tree->redraw)
			tree->callbacks->redraw_request(0, y,
					width, height,
					tree->client_data);
		return;
	}

	while (node != NULL) {
		next = node->next;
		if (node->selected)
			tree_delete_node(tree, node, false);
		else if (node->child != NULL)
			tree_delete_selected_nodes(tree, node->child);
		node = next;
	}

	tree->redraw = redraw_setting;

	if (tree->redraw)
		tree->callbacks->redraw_request(0, y,
				width, height,
				tree->client_data);
}


/**
 * Returns the selected node, or NULL if multiple nodes are selected.
 *
 * \param node	the node to search sibling and children
 * \return	the selected node, or NULL if multiple nodes are selected
 */
struct node *tree_get_selected_node(struct node *node)
{
	struct node *result = NULL;
	struct node *temp;

	for (; node != NULL; node = node->next) {
		if (node->selected) {
			if (result != NULL)
				return NULL;
			result = node;
		}
		if ((node->child != NULL) && (node->expanded)) {
			temp = tree_get_selected_node(node->child);
			if (temp != NULL) {
				if (result != NULL)
					return NULL;
				else
					result = temp;
			}
		}
	}
	return result;
}


/**
 * Finds a node element at a specific location.
 *
 * \param node	     the root node to check from
 * \param x	     the x co-ordinate
 * \param y	     the y co-ordinate
 * \param expansion_toggle  whether the coordinate was in an expansion toggle
 * \return the node at the specified position, or NULL for none
 */
static struct node_element *tree_get_node_element_at(struct node *node,
		int x, int y, bool *expansion_toggle)
{
	struct node_element *element;
	int x0, x1, y0, y1;

	*expansion_toggle = false;
	for (; node != NULL; node = node->next) {
		if (node->box.y > y) return NULL;
		if ((node->box.x - NODE_INSTEP < x) && (node->box.y < y) &&
		    (node->box.x + node->box.width >= x) &&
		    (node->box.y + node->box.height >= y)) {
			if (node->expanded) {
				for (element = &node->data; element != NULL;
				     element = element->next) {
					x0 = element->box.x;
					y0 = element->box.y;
					x1 = element->box.x +
						element->box.width;
					y1 = element->box.y +
						element->box.height;
					if ((x0 < x) && (y0 < y) && (x1 >= x)
					    && (y1 >= y))
						return element;
				}
			} else {
				x0 = node->data.box.x;
				y0 = node->data.box.y;
				x1 = node->data.box.x +	node->data.box.width;
				y1 = node->data.box.y +	node->data.box.height;
				if ((x0 < x) && (y0 < y) && (x1 >= x) &&
				    (y1>= y))
					return &node->data;
			}
			if (((node->child != NULL) ||
			     (node->data.next != NULL)) &&
			    (node->data.box.x - NODE_INSTEP + 4 < x)
			    && (node->data.box.y + 4 < y) &&
			    (node->data.box.x > x) &&
			    (node->data.box.y + TREE_LINE_HEIGHT > y)) {
				/* Node either has node children, or node
				 * has more than one element.
				 * Coordinate is over node expansion toggle area
				 */
				*expansion_toggle = true;
				return &node->data;
			}
		}

		element = tree_get_node_element_at(node->child, x, y,
				expansion_toggle);
		if ((node->child != NULL) && (node->expanded) &&
		    (element != NULL))
			return element;
	}
	return NULL;
}


/**
 * Finds a node at a specific location.
 *
 * \param root	     the root node to check from
 * \param x	     the x co-ordinate
 * \param y	     the y co-ordinate
 * \param expansion_toggle  whether the coordinate was in an expansion toggle
 * \return the node at the specified position, or NULL for none
 */
static struct node *tree_get_node_at(struct node *root, int x, int y,
		bool *expansion_toggle)
{
	struct node_element *result;

	if ((result = tree_get_node_element_at(root, x, y, expansion_toggle)))
		return result->parent;
	return NULL;
}


/**
 * Gets link characteristics to insert a node at a specified position.
 *
 * \param tree	  the tree to find link information for
 * \param x	  the x co-ordinate
 * \param y	  the y co-ordinate
 * \param before  set to whether the node should be linked before on exit
 * \return	  the node to link with
 */
struct node *tree_get_link_details(struct tree *tree, int x, int y,
		bool *before)
{
	struct node *node = NULL;
	bool expansion_toggle;

	assert(tree != NULL);
	assert(tree->root != NULL);

	*before = false;
	if (tree->root->child != NULL)
		node = tree_get_node_at(tree->root->child, x, y,
				&expansion_toggle);
	if ((node == NULL) || (expansion_toggle))
		return tree->root;

	if (y < (node->box.y + (node->box.height / 2))) {
		*before = true;
	} else if ((node->folder) && (node->expanded) &&
		   (node->child != NULL)) {
		node = node->child;
		*before = true;
	}
	return node;
}


/**
 * Launches all the selected nodes of the tree
 *
 * \param tree	the tree for which all nodes will be launched
 * \param node	the node which will be checked together with its children
 * \param tabs	launch node in a new tab instead of a new window
 */
static void tree_launch_selected_internal(struct tree *tree, struct node *node,
		bool tabs)
{
	struct node_msg_data msg_data;

	msg_data.data.bw = NULL;

	for (; node != NULL; node = node->next) {
		if (node->selected && node->user_callback != NULL) {
			msg_data.msg = NODE_LAUNCH;
			if (tabs == true) {
				msg_data.flag = TREE_ELEMENT_LAUNCH_IN_TABS;
			} else {
				msg_data.flag = TREE_ELEMENT_TITLE;
			}

			msg_data.node = node;
			node->user_callback(node->callback_data, &msg_data);
		}
		if (node->child != NULL)
			tree_launch_selected_internal(tree, node->child, tabs);
	}
}


/**
 * Launches all the selected nodes of the tree
 *
 * \param tree	the tree for which all nodes will be launched
 * \param tabs	launch nodes in new tabs instead of new windows
 */
void tree_launch_selected(struct tree *tree, bool tabs)
{
	if (tree->root->child != NULL)
		tree_launch_selected_internal(tree, tree->root->child, tabs);
}


/**
 * Updates the node at position x,y to a selected state.
 * The required areas of the tree are redrawn.
 *
 * \param tree	    the tree to update nodes for, may be NULL
 * \param x	        x position in tree
 * \param y	        y position in tree
 * \param selected  the selection state to set
 */
void tree_set_node_selected_at(struct tree *tree, int x, int y, bool selected)
{
	bool expansion_toggle;
	struct node *node;

	node = tree_get_node_at(tree->root, x, y, &expansion_toggle);

	if ((node == NULL) || (expansion_toggle == true))
		return;

	tree_set_node_selected(tree, node, false, selected);
}


/**
 * Handles a mouse action for a tree
 *
 * \param tree	 the tree to handle a click for
 * \param mouse	 the mouse state
 * \param x	 X coordinate of mouse action
 * \param y	 Y coordinate of mouse action
 * \return	 whether the click was handled
 */
bool tree_mouse_action(struct tree *tree, browser_mouse_state mouse, int x,
		int y)
{
	bool expansion_toggle;
	struct node *node;
	struct node *last;
	struct node_element *element;
	struct node_msg_data msg_data;

	bool double_click_1 = mouse & BROWSER_MOUSE_DOUBLE_CLICK &&
			mouse & BROWSER_MOUSE_CLICK_1;
	bool double_click_2 = mouse & BROWSER_MOUSE_DOUBLE_CLICK &&
			mouse & BROWSER_MOUSE_CLICK_2;

	assert(tree != NULL);
	assert(tree->root != NULL);

	if (tree->root->child == NULL)
		return true;

	element = tree_get_node_element_at(tree->root->child, x, y,
			&expansion_toggle);

	/* pass in-textarea mouse action and drags which started in it
	   to the textarea */
	if (tree->editing != NULL) {
		int x0, x1, y0, y1;
		x0 = tree->editing->box.x;
		if (tree->editing->type == NODE_ELEMENT_TEXT_PLUS_ICON)
			x0 += NODE_INSTEP;
		x1 = tree->editing->box.x + tree->editing->box.width;
		y0 = tree->editing->box.y;
		y1 = tree->editing->box.y + tree->ta_height;

		if (tree->drag == TREE_TEXTAREA_DRAG &&
				(mouse & (BROWSER_MOUSE_HOLDING_1 |
				BROWSER_MOUSE_HOLDING_2))) {
			/* Track the drag path */
			textarea_mouse_action(tree->textarea, mouse,
					      x - x0, y - y0);
			return true;
		}

		if ((x >= x0) && (x < x1) && (y >= y0) && (y < y1)) {
			textarea_mouse_action(tree->textarea, mouse,
					      x - x0, y - y0);
			return true;

		}
	}

	/* we are not interested in the drag path, return */
	if (mouse & (BROWSER_MOUSE_HOLDING_1 | BROWSER_MOUSE_HOLDING_2))
		return true;

	/* cancel edit */
	if (tree->editing != NULL)
		tree_stop_edit(tree, false);

	/* no item either means cancel selection on (select) click or a drag */
	if (element == NULL) {
		if (tree->flags & TREE_SINGLE_SELECT) {
			tree_set_node_selected(tree, tree->root->child, true,
					       false);
			return true;
		}
		if (mouse & (BROWSER_MOUSE_CLICK_1 | BROWSER_MOUSE_DRAG_1))
			tree_set_node_selected(tree, tree->root->child, true,
					       false);
		if (mouse & (BROWSER_MOUSE_DRAG_1 | BROWSER_MOUSE_DRAG_2)) {

			/** @todo the tree window has to scroll the tree when
			 * mouse reaches border while dragging this isn't
			 * solved for the browser window too.
			 */
			tree->drag = TREE_SELECT_DRAG;
		}
		return true;
	}

	node = element->parent;

	/* A click on expansion toggle or double click on folder toggles node
	 * expansion */
	if (((expansion_toggle) && (mouse & (BROWSER_MOUSE_CLICK_1 |
			BROWSER_MOUSE_CLICK_2))) ||
			(((!expansion_toggle) && (node->child != NULL)) &&
			(double_click_1 || double_click_2))) {

		/* clear any selection */
		tree_set_node_selected(tree, tree->root->child, true, false);

		/* expand / contract node and redraw */
		tree_set_node_expanded(tree, node, !node->expanded,
				       false, false);

		/* find the last child node if expanded */
		last = node;
		if ((last->child != NULL) && (last->expanded)) {
			last = last->child;
			while ((last->next != NULL) ||
			       ((last->child != NULL) &&
				(last->expanded))) {
				if (last->next != NULL)
					last = last->next;
				else
					last = last->child;
			}
		}
		/* scroll to the bottom element then back to the top */
		element = &last->data;
		if (last->expanded)
			for (; element->next != NULL; element = element->next);
		tree->callbacks->scroll_visible(element->box.y,
						element->box.height,
						tree->client_data);
		tree->callbacks->scroll_visible(node->data.box.y,
						node->data.box.height,
						tree->client_data);
		return true;
	}

	/* no use for any other expansion toggle click */
	if (expansion_toggle)
		return true;

	/* single/double ctrl+click or alt+click starts editing */
	if ((element->editable) && (!tree->editing) &&
	    ((element->type == NODE_ELEMENT_TEXT) ||
	     (element->type == NODE_ELEMENT_TEXT_PLUS_ICON)) &&
	    (mouse & BROWSER_MOUSE_CLICK_1 || double_click_1) &&
	    (mouse & BROWSER_MOUSE_MOD_2 ||
	     mouse & BROWSER_MOUSE_MOD_3)) {
		tree_set_node_selected(tree, tree->root->child, true, false);
		tree_start_edit(tree, element);
		return true;
	}

	/* double click launches the leaf */
	if (double_click_1 || double_click_2) {
		if (node->user_callback == NULL)
			return false;
		msg_data.msg = NODE_LAUNCH;
		msg_data.flag = TREE_ELEMENT_TITLE;
		msg_data.node = node;
		if (node->user_callback(node->callback_data, &msg_data) !=
		    NODE_CALLBACK_HANDLED)
			return false;

		return true;
	}

	/* single click (select) cancels current selection and selects item */
	if (mouse & BROWSER_MOUSE_CLICK_1 || (mouse & BROWSER_MOUSE_CLICK_2 &&
					      tree->flags & TREE_SINGLE_SELECT)) {
		if (tree->flags & TREE_NO_SELECT)
			return true;
		if (!node->selected) {
			tree_set_node_selected(tree, tree->root->child, true,
					       false);
			node->selected = true;
			tree_handle_node_element_changed(tree, &node->data, false);
		}
		return true;
	}

	/* single click (adjust) toggles item selection */
	if (mouse & BROWSER_MOUSE_CLICK_2) {
		if (tree->flags & TREE_NO_SELECT)
			return true;
		node->selected = !node->selected;
		tree_handle_node_element_changed(tree, &node->data, false);
		return true;
	}

	/* drag starts a drag operation */
	if ((!tree->editing) && (mouse & (BROWSER_MOUSE_DRAG_1 |
					  BROWSER_MOUSE_DRAG_2))) {
		if (tree->flags & TREE_NO_DRAGS)
			return true;

		if (!node->selected) {
			tree_set_node_selected(tree, tree->root->child, true,
					       false);
			node->selected = true;
			tree_handle_node_element_changed(tree, &node->data, false);
		}

		if (tree->flags & TREE_MOVABLE)
			tree->drag = TREE_MOVE_DRAG;
		else tree->drag = TREE_UNKNOWN_DRAG;

		return true;
	}


	return false;
}


/**
 * Updates the selected state for a region of nodes.
 *
 * \param tree	  the tree to update
 * \param node	  the node to update children and siblings of
 * \param y	  the minimum y of the selection rectangle
 * \param height  the height of the selection rectangle
 * \param invert  whether to invert the selected state
 */
static void tree_handle_selection_area_node(struct tree *tree,
		struct node *node, int y, int height, bool invert)
{
	struct node_element *element;
	struct node *update;
	int y_max;
	int y0, y1;

	assert(tree != NULL);
	assert(node != NULL);

	y_max = y + height;

	for (; node != NULL; node = node->next) {
		if (node->box.y > y_max) return;
		y0 = node->box.y;
		y1 = node->box.y + node->box.height;
		if ((y0 < y_max) && (y1 >= y)) {
			update = NULL;
			if (node->expanded) {
				for (element = &node->data; element != NULL;
				     element = element->next) {
					y0 = element->box.y;
					y1 = element->box.y +
						element->box.height;
					if ((y0 < y_max) && (y1 >= y)) {
						update = element->parent;
						break;
					}
				}
			} else {
				y0 = node->data.box.y;
				y1 = node->data.box.y +	node->data.box.height;
				if ((y0 < y_max) && (y1 >= y))
					update = node->data.parent;
			}
			if ((update) && (node != tree->root)) {
				if (invert) {
					node->selected = !node->selected;
					tree_handle_node_element_changed(tree,
							&node->data, false);
				} else if (!node->selected) {
					node->selected = true;
					tree_handle_node_element_changed(tree,
							&node->data, false);
				}
			}
		}
		if ((node->child != NULL) && (node->expanded))
			tree_handle_selection_area_node(tree, node->child, y,
							height, invert);
	}
}


/**
 * Updates the selected state for a region of nodes.
 *
 * \param tree	  the tree to update
 * \param y	  the minimum y of the selection rectangle
 * \param height  the height of the selection rectangle
 * \param invert  whether to invert the selected state
 */
static void tree_handle_selection_area(struct tree *tree, int y, int height,
		bool invert)
{
	assert(tree != NULL);
	assert(tree->root != NULL);

	if (tree->root->child == NULL)
		return;

	if (height < 0) {
		y += height;
		height = -height;
	}
	tree_handle_selection_area_node(tree, tree->root->child, y, height,
			invert);
}


/**
 * Clears the processing flag.
 *
 * \param node	  the node to process siblings and children of
 */
static void tree_clear_processing(struct node *node)
{
	for (; node != NULL; node = node->next) {
		node->processing = false;
		if (node->child != NULL)
			tree_clear_processing(node->child);
	}
}


/**
 * Sets the processing flag to the selection state.
 *
 * \param node	  the node to process siblings and children of
 */
static void tree_selected_to_processing(struct node *node)
{
	for (; node != NULL; node = node->next) {
		node->processing = node->selected;
		if ((node->child != NULL) && (node->expanded))
			tree_selected_to_processing(node->child);
	}
}


/**
 * Moves the first node in a tree with the processing flag set.
 *
 * \param tree	  the tree in which the move takes place
 * \param node	  the node to move siblings/children of
 * \param link	  the node to link before/as a child (folders) or before/after
 *		  (link)
 * \param before  whether to link siblings before or after the supplied node
 * \param first	  whether to always link after the supplied node (ie not
 *		  inside of folders)
 * \return	  the node moved
 */
static struct node *tree_move_processing_node(struct tree *tree,
		struct node *node, struct node *link, bool before, bool first)
{
	struct node *result;

	bool folder = link->folder;
	for (; node != NULL; node = node->next) {
		if (node->processing) {
			node->processing = false;
			tree_delink_node(tree, node);
			if (!first)
				link->folder = false;
			tree_link_node(tree, link, node, before);
			if (!first)
				link->folder = folder;
			return node;
		}
		if (node->child != NULL) {
			result = tree_move_processing_node(tree, node->child,
							   link, before, first);
			if (result != NULL)
				return result;
		}
	}
	return NULL;
}


/**
 * Moves nodes within a tree.
 *
 * \param tree	       the tree to process
 * \param destination  the node to link before/as a child (folders)
 *		       or before/after (link)
 * \param before       whether to link siblings before or after the supplied
 *		       node
 */
static void tree_move_selected_nodes(struct tree *tree,
		struct node *destination, bool before)
{
	struct node *link;
	struct node *test;
	bool error;

	tree_clear_processing(tree->root);
	tree_selected_to_processing(tree->root);

	/* the destination node cannot be a child of any node with
	   the processing flag set */
	error = destination->processing;
	for (test = destination; test != NULL; test = test->parent)
		error |= test->processing;
	if (error) {
		tree_clear_processing(tree->root);
		return;
	}
	if ((destination->folder) && (!destination->expanded) && (!before)) {
		tree_set_node_expanded(tree, destination, true, false, false);
	}
	link = tree_move_processing_node(tree, tree->root, destination, before,
					 true);
	while (link != NULL)
		link = tree_move_processing_node(tree, tree->root, link, false,
						 false);

	tree_clear_processing(tree->root);
	tree_recalculate_node_positions(tree, tree->root);
	if (tree->redraw)
		tree->callbacks->redraw_request(0, 0, tree->width, tree->height,
						tree->client_data);
}


/**
 * Handle the end of a drag operation
 *
 * \param tree	the tree on which the drag was performed
 * \param mouse	mouse state during drag end
 * \param x0	x coordinate of drag start
 * \param y0	y coordinate of drag start
 * \param x1	x coordinate of drag end
 * \param y1	y coordinate of drag end
 */
void tree_drag_end(struct tree *tree, browser_mouse_state mouse, int x0, int y0,
		int x1, int y1)
{

	bool before;
	struct node *node;
	int x, y;

	switch (tree->drag) {
	case TREE_NO_DRAG:
	case TREE_UNKNOWN_DRAG:
		break;

	case TREE_TEXTAREA_DRAG:
		x = tree->editing->box.x;
		y = tree->editing->box.y;
		if (tree->editing->type == NODE_ELEMENT_TEXT_PLUS_ICON)
			x += NODE_INSTEP;
		textarea_mouse_action(tree->textarea, BROWSER_MOUSE_HOVER,
				x1 - x, y1 - y);
		break;

	case TREE_SELECT_DRAG:
		tree_handle_selection_area(tree, y0, y1 - y0,
					   (mouse | BROWSER_MOUSE_HOLDING_2));
		break;

	case TREE_MOVE_DRAG:
		if (!(tree->flags & TREE_MOVABLE))
			return;
		node = tree_get_link_details(tree, x1, y1, &before);
		tree_move_selected_nodes(tree, node, before);
		break;
	}

	tree->drag = TREE_NO_DRAG;
}


/**
 * Key press handling for a tree.
 *
 * \param tree	The tree which got the keypress
 * \param key	The ucs4 character codepoint
 * \return	true if the keypress is dealt with, false otherwise.
 */
bool tree_keypress(struct tree *tree, uint32_t key)
{
	if (tree->editing != NULL)
		switch (key) {
		case KEY_ESCAPE:
			tree_stop_edit(tree, false);
			return true;
		case KEY_NL:
		case KEY_CR:
			tree_stop_edit(tree, true);
			return true;
		default:
			return textarea_keypress(tree->textarea, key);
		}

	return false;
}


/**
 * Alphabetical comparison function for nodes
 *
 * \param n1  first node to compare
 * \param n2  first node to compare
 * \return    0 if equal, greater then zero if n1 > n2,
 *	      less then zero if n2 < n1
 */
int tree_alphabetical_sort(struct node *n1, struct node *n2)
{
	return strcmp(n1->data.text, n2->data.text);
}


/**
 * Redraw requests from the textarea are piped through this because we have to
 * check the redraw flag of the tree before requesting a redraw and change the
 * position to tree origin relative.
 */

static void tree_textarea_callback(void *data, struct textarea_msg *msg)
{
	struct tree *tree = data;
	int x, y;

	switch (msg->type) {
	case TEXTAREA_MSG_DRAG_REPORT:
		if (msg->data.drag == TEXTAREA_DRAG_NONE) {
			/* Textarea drag finished */
			tree->drag = TREE_NO_DRAG;
		} else {
			/* Textarea drag started */
			tree->drag = TREE_TEXTAREA_DRAG;
		}
		break;

	case TEXTAREA_MSG_REDRAW_REQUEST:
		x = msg->data.redraw.x0 + tree->editing->box.x;
		y = msg->data.redraw.y0 + tree->editing->box.y;

		if (tree->editing->type == NODE_ELEMENT_TEXT_PLUS_ICON)
			x += NODE_INSTEP;

		/* Redraw the textarea */
		if (tree->redraw)
			tree->callbacks->redraw_request(x, y,
					msg->data.redraw.x1 -
						msg->data.redraw.x0,
					msg->data.redraw.y1 -
						msg->data.redraw.y0,
					tree->client_data);
		break;

	default:
		break;
	}
}


/**
 * Starts editing a node_element
 *
 * \param tree		The tree to which element belongs
 * \param element	The element to start being edited
 */
void tree_start_edit(struct tree *tree, struct node_element *element)
{
	struct node *parent;
	int width, height;
	textarea_setup ta_setup;
	textarea_flags ta_flags;

	assert(tree != NULL);
	assert(element != NULL);

	if (tree->editing != NULL)
		tree_stop_edit(tree, true);

	parent = element->parent;
	if (&parent->data == element)
		parent = parent->parent;
	for (; parent != NULL; parent = parent->parent) {
		if (!parent->expanded) {
			tree_set_node_expanded(tree, parent, true,
					       false, false);
		}
	}

	tree->editing = element;
	tree->callbacks->get_window_dimensions(&width, NULL, tree->client_data);
	width -= element->box.x;
	height = element->box.height;
	if (element->type == NODE_ELEMENT_TEXT_PLUS_ICON)
		width -= NODE_INSTEP;

	tree->ta_height = height;

	ta_flags = TEXTAREA_INTERNAL_CARET;

	ta_setup.width = width;
	ta_setup.height = tree->ta_height;
	ta_setup.pad_top = 0;
	ta_setup.pad_right = 4;
	ta_setup.pad_bottom = 0;
	ta_setup.pad_left = 4;
	ta_setup.border_width = 1;
	ta_setup.border_col = 0x000000;
	ta_setup.selected_text = 0xffffff;
	ta_setup.selected_bg = 0x000000;
	ta_setup.text = plot_fstyle;
	ta_setup.text.foreground = 0x000000;
	ta_setup.text.background = 0xffffff;

	tree->textarea = textarea_create(ta_flags, &ta_setup,
			tree_textarea_callback, tree);
	if (tree->textarea == NULL) {
		tree_stop_edit(tree, false);
		return;
	}
	textarea_set_text(tree->textarea, element->text);

	tree_handle_node_element_changed(tree, element, true);
	tree_recalculate_size(tree);
	tree->callbacks->scroll_visible(element->box.y, element->box.height,
					tree->client_data);
}


/**
 * Callback for fetchcache(). Should be removed once bitmaps get loaded directly
 * from disc
 */
static nserror tree_icon_callback(hlcache_handle *handle,
		const hlcache_event *event, void *pw)
{
	return NSERROR_OK;
}


/**
 * Tree utility function. Placed here so that this code doesn't have to be
 * copied by each user.
 *
 * \param name	the name of the loaded icon, if it's not a full path the icon is
 *		looked for in the directory specified by tree_icons_dir
 * \return the icon in form of a content or NULL on failure
 */
hlcache_handle *tree_load_icon(const char *name)
{
	char *url = NULL;
	const char *icon_url = NULL;
	int len;
	hlcache_handle *c;
	nserror err;
	nsurl *icon_nsurl;

	/** @todo something like bitmap_from_disc is needed here */

	if (!strncmp(name, "file://", 7)) {
		icon_url = name;
	} else {
		char *native_path;

		if (tree_icons_dir == NULL)
			return NULL;

		/* path + separator + leafname + '\0' */
		len = strlen(tree_icons_dir) + 1 + strlen(name) + 1;
		native_path = malloc(len);
		if (native_path == NULL) {
			LOG(("malloc failed"));
			warn_user("NoMemory", 0);
			return NULL;
		}

		/* Build native path */
		memcpy(native_path, tree_icons_dir,
		       strlen(tree_icons_dir) + 1);
		path_add_part(native_path, len, name);

		/* Convert native path to URL */
		url = path_to_url(native_path);

		free(native_path);
		icon_url = url;
	}

	err = nsurl_create(icon_url, &icon_nsurl);
	if (err != NSERROR_OK) {
		if (url != NULL)
			free(url);
		return NULL;
	}

	/* Fetch the icon */
	err = hlcache_handle_retrieve(icon_nsurl, 0, 0, 0,
				      tree_icon_callback, 0, 0, 
				      CONTENT_IMAGE, &c);

	nsurl_unref(icon_nsurl);

	/* If we built the URL here, free it */
	if (url != NULL)
		free(url);

	if (err != NSERROR_OK) {
		return NULL;
	}

	return c;
}