summaryrefslogtreecommitdiff
path: root/frontends/gtk/toolbar.c
blob: 16e5f1f6d9f2253fa85d8bf6d6c77a5384422d26 (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
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
/*
 * Copyright 2019 Vincent Sanders <vince@netsurf-browser.org>
 *
 * 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
 * implementation of toolbar to control browsing context
 */

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <gtk/gtk.h>

#include "utils/log.h"
#include "utils/messages.h"
#include "utils/nsoption.h"
#include "utils/file.h"
#include "utils/nsurl.h"
#include "utils/corestrings.h"
#include "desktop/browser_history.h"
#include "desktop/searchweb.h"
#include "desktop/search.h"
#include "desktop/save_complete.h"
#include "desktop/save_text.h"
#include "desktop/print.h"
#include "desktop/hotlist.h"
#include "netsurf/content.h"
#include "netsurf/browser_window.h"
#include "netsurf/keypress.h"

#include "gtk/toolbar_items.h"
#include "gtk/completion.h"
#include "gtk/gui.h"
#include "gtk/warn.h"
#include "gtk/search.h"
#include "gtk/throbber.h"
#include "gtk/scaffolding.h"
#include "gtk/window.h"
#include "gtk/compat.h"
#include "gtk/resources.h"
#include "gtk/schedule.h"
#include "gtk/local_history.h"
#include "gtk/global_history.h"
#include "gtk/viewsource.h"
#include "gtk/download.h"
#include "gtk/viewdata.h"
#include "gtk/tabs.h"
#include "gtk/print.h"
#include "gtk/layout_pango.h"
#include "gtk/preferences.h"
#include "gtk/hotlist.h"
#include "gtk/cookies.h"
#include "gtk/about.h"
#include "gtk/gdk.h"
#include "gtk/bitmap.h"
#include "gtk/page_info.h"
#include "gtk/toolbar.h"

/**
 * button location indicating button is not to be shown
 */
#define INACTIVE_LOCATION (-1)

/**
 * time (in ms) between throbber animation frame updates
 */
#define THROBBER_FRAME_TIME (100)

/**
 * the minimum number of columns in the tool store
 */
#define NSGTK_MIN_STORE_COLUMNS 4

/**
 * the 'standard' width of a button that makes sufficient of its label visible
 */
#define NSGTK_BUTTON_WIDTH 120

/**
 * the 'standard' height of a button that fits as many toolbars as
 *   possible into the store
 */
#define NSGTK_BUTTON_HEIGHT 70

/**
 * the 'normal' width of the websearch bar
 */
#define NSGTK_WEBSEARCH_WIDTH 150

/**
 * toolbar item context
 */
struct nsgtk_toolbar_item {

	/**
	 * GTK widget in the toolbar
	 */
	GtkToolItem *button;

	/**
	 * location index in toolbar
	 */
	int location;

	/**
	 * if the item is currently sensitive in the toolbar
	 */
	bool sensitivity;

	/**
	 * textural name used in serialising items
	 */
	const char *name;

	/**
	 * button clicked on toolbar handler
	 */
	gboolean (*clicked)(GtkWidget *widget, gpointer data);

	/**
	 * handler when dragging from customisation toolbox to toolbar
	 */
	void *dataplus;

	/**
	 * handler when dragging from toolbar to customisation toolbox
	 */
	void *dataminus;
};

/**
 * Location focus state machine
 *
 * 1. If we don't care, we're in LFS_IDLE
 * 2. When we create a new toolbar, we can put it into
 *    LFS_WANT which means that we want the url bar to focus
 * 3. When we start throbbing if we're in LFS_WANT we move to LFS_THROB
 * 4. When we stop throbbing, if we're in LFS_THROB we move to LFS_LAST
 *
 * While not in LFS_IDLE, if the url bar is updated and we previously had it
 * fully selected then we reselect it all.  If we're in LFS_LAST we move to
 * LFS_IDLE at that point.
 */
typedef enum {
	LFS_IDLE, /**< Nothing to do */
	LFS_WANT, /**< Want focus, will apply */
	LFS_THROB, /**< Want focus, we have started throbbing */
	LFS_LAST, /**< Last chance for a focus update */
} nsgtk_toolbar_location_focus_state;

/**
 * control toolbar context
 */
struct nsgtk_toolbar {
	/** gtk toolbar widget */
	GtkToolbar *widget;

	/* toolbar size allocation context */
	int offset;
	int toolbarmem;
	int toolbarbase;
	int historybase;

	/**
	 * Toolbar item contexts
	 */
	struct nsgtk_toolbar_item items[PLACEHOLDER_BUTTON];

	/**
	 * Current frame of throbber animation
	 */
	int throb_frame;

	/**
	 * Web search widget
	 */
	GtkWidget *webSearchEntry;

	/**
	 * callback to obtain a browser window for navigation
	 */
	struct browser_window *(*get_bw)(void *ctx);

	/**
	 * context passed to get_bw function
	 */
	void *get_ctx;

	/**
	 * Location focus state machine, current state
	 */
	nsgtk_toolbar_location_focus_state loc_focus;
};


/**
 * toolbar cusomisation context
 */
struct nsgtk_toolbar_customisation {
	/**
	 * first entry is a toolbar widget so a customisation widget
	 *   can be cast to toolbar and back.
	 */
	struct nsgtk_toolbar toolbar;

	/**
	 * The top level container (tabBox)
	 */
	GtkWidget *container;

	/**
	 * The vertical box into which the available tools are shown
	 */
	GtkBox *toolbox;

	/**
	 * widget handles for items in the customisation toolbox area
	 */
	GtkToolItem *items[PLACEHOLDER_BUTTON];

	/**
	 * which item is being dragged
	 */
	int dragitem; /* currentbutton */
	/**
	 * true if item being dragged onto toolbar, false if from toolbar
	 */
	bool dragfrom; /*fromstore */

};


/* forward declaration */
static nserror toolbar_item_create(nsgtk_toolbar_button id,
				   struct nsgtk_toolbar_item *item_out);


/**
 * returns a string without its underscores
 *
 * \param s The string to change.
 * \param replacespace true to insert a space where there was an underscore
 * \return The altered string
 */
static char *remove_underscores(const char *s, bool replacespace)
{
	size_t i, ii, len;
	char *ret;
	len = strlen(s);
	ret = malloc(len + 1);
	if (ret == NULL) {
		return NULL;
	}
	for (i = 0, ii = 0; i < len; i++) {
		if (s[i] != '_') {
			ret[ii++] = s[i];
		} else if (replacespace) {
			ret[ii++] = ' ';
		}
	}
	ret[ii] = '\0';
	return ret;
}


/**
 * create throbber toolbar item widget
 *
 * create a gtk entry widget with a completion attached
 */
static GtkToolItem *
make_toolbar_item_throbber(bool sensitivity, bool edit)
{
	nserror res;
	GtkToolItem *item;
	GdkPixbuf *pixbuf;
	GtkWidget *image;

	res = nsgtk_throbber_get_frame(0, &pixbuf);
	if (res != NSERROR_OK) {
		return NULL;
	}

	if (edit) {
		const char *msg;
		msg = messages_get("ToolThrob");
		item = gtk_tool_button_new(
				GTK_WIDGET(gtk_image_new_from_pixbuf(pixbuf)),
				msg);
	} else {
		item = gtk_tool_item_new();

		image = gtk_image_new_from_pixbuf(pixbuf);
		if (image != NULL) {
			nsgtk_widget_set_alignment(image,
						   GTK_ALIGN_CENTER,
						   GTK_ALIGN_CENTER);
			nsgtk_widget_set_margins(image, 3, 0);

			gtk_container_add(GTK_CONTAINER(item), image);
		}
	}
	gtk_widget_set_sensitive(GTK_WIDGET(item), sensitivity);

	return item;
}


/**
 * create url bar toolbar item widget
 *
 * create a gtk entry widget with a completion attached
 *
 * \param sensitivity if the entry should be created sensitive to input
 * \param edit if the entry should be editable
 */
static GtkToolItem *
make_toolbar_item_url_bar(bool sensitivity, bool edit)
{
	GtkToolItem *item;
	GtkWidget *entry;
	GtkEntryCompletion *completion;

	entry = nsgtk_entry_new();

	if (entry == NULL) {
		return NULL;
	}
	nsgtk_entry_set_icon_from_icon_name(entry,
					    GTK_ENTRY_ICON_PRIMARY,
					    "page-info-internal");

	if (edit) {
		gtk_entry_set_width_chars(GTK_ENTRY(entry), 9);

		item = gtk_tool_button_new(NULL, "URL");
		gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(item), entry);
	} else {
		completion = gtk_entry_completion_new();
		if (completion != NULL) {
			gtk_entry_set_completion(GTK_ENTRY(entry), completion);
		}

		item = gtk_tool_item_new();
		if (item == NULL) {
			return NULL;
		}

		gtk_container_add(GTK_CONTAINER(item), entry);
		gtk_tool_item_set_expand(item, TRUE);

	}
	gtk_widget_set_sensitive(GTK_WIDGET(item), TRUE);
	gtk_widget_set_sensitive(GTK_WIDGET(entry), sensitivity);

	return item;
}


/**
 * create web search toolbar item widget
 */
static GtkToolItem *
make_toolbar_item_websearch(bool sensitivity, bool edit)
{
	GtkToolItem *item;
	nserror res;
	GtkWidget *entry;
	struct bitmap *bitmap;
	GdkPixbuf *pixbuf = NULL;

	res = search_web_get_provider_bitmap(&bitmap);
	if ((res == NSERROR_OK) && (bitmap != NULL)) {
		pixbuf = nsgdk_pixbuf_get_from_surface(bitmap->surface, 32, 32);
	}

	entry = nsgtk_entry_new();

	if (entry == NULL) {
		return NULL;
	}

	if (pixbuf != NULL) {
		nsgtk_entry_set_icon_from_pixbuf(entry,
						 GTK_ENTRY_ICON_PRIMARY,
						 pixbuf);
		g_object_unref(pixbuf);
	} else {
		nsgtk_entry_set_icon_from_icon_name(entry,
						    GTK_ENTRY_ICON_PRIMARY,
						    NSGTK_STOCK_INFO);
	}

	if (edit) {
		gtk_entry_set_width_chars(GTK_ENTRY(entry), 9);

		item = gtk_tool_button_new(NULL, "Web Search");
		gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(item),
						entry);
	} else {
		gtk_widget_set_size_request(entry, NSGTK_WEBSEARCH_WIDTH, -1);

		item = gtk_tool_item_new();
		if (item == NULL) {
			return NULL;
		}

		gtk_container_add(GTK_CONTAINER(item), entry);
	}
	gtk_widget_set_sensitive(GTK_WIDGET(item), TRUE);
	gtk_widget_set_sensitive(GTK_WIDGET(entry), sensitivity);

	return item;
}


/**
 * create local history toolbar item widget
 */
static GtkToolItem *
make_toolbar_item_history(bool sensitivity, bool edit)
{
	GtkToolItem *item;
	const char *msg = "H";
	char *label = NULL;

	if (edit) {
		msg = messages_get("gtkLocalHistory");
	}
	label = remove_underscores(msg, false);
	item = gtk_tool_button_new(NULL, label);
	if (label != NULL) {
		free(label);
	}
	gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), "local-history");

	/* set history widget minimum width */
	gtk_widget_set_size_request(GTK_WIDGET(item), 20, -1);
	gtk_widget_set_sensitive(GTK_WIDGET(item), sensitivity);

	return item;
}


/**
 * create generic button toolbar item widget
 */
static GtkToolItem *
make_toolbar_item_button(const char *labelmsg,
			 const char *iconname,
			 bool sensitivity,
			 bool edit)
{
	GtkToolItem *item;
	char *label = NULL;

	label = remove_underscores(messages_get(labelmsg), false);

	item = gtk_tool_button_new(NULL, label);
	if (label != NULL) {
		free(label);
	}

	if (item != NULL) {
		gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), iconname);

		gtk_widget_set_sensitive(GTK_WIDGET(item), sensitivity);
		if (edit) {
			nsgtk_widget_set_margins(GTK_WIDGET(item), 0, 0);
		}
	}

	return item;
}


/**
 * widget factory for creation of toolbar item widgets
 *
 * \param i the id of the widget
 * \param theme the theme to make the widgets from
 * \return gtk widget
 */
static GtkToolItem *
make_toolbar_item(nsgtk_toolbar_button itemid, bool sensitivity)
{
	GtkToolItem *toolitem = NULL;

	switch(itemid) {
#define TOOLBAR_ITEM_y(identifier, label, iconame)
#define TOOLBAR_ITEM_n(identifier, label, iconame)
#define TOOLBAR_ITEM_t(identifier, label, iconame)		\
	case identifier:					\
		toolitem = make_toolbar_item_button(#label, iconame, sensitivity, false); \
		break;
#define TOOLBAR_ITEM_b(identifier, label, iconame)		\
	case identifier:					\
		toolitem = make_toolbar_item_button(#label, iconame, sensitivity, false); \
		break;
#define TOOLBAR_ITEM(identifier, name, snstvty, clicked, activate, label, iconame) \
		TOOLBAR_ITEM_ ## clicked(identifier, label, iconame)

#include "gtk/toolbar_items.h"

#undef TOOLBAR_ITEM_t
#undef TOOLBAR_ITEM_b
#undef TOOLBAR_ITEM_n
#undef TOOLBAR_ITEM_y
#undef TOOLBAR_ITEM

	case HISTORY_BUTTON:
		toolitem = make_toolbar_item_history(sensitivity, false);
		break;

	case URL_BAR_ITEM:
		toolitem = make_toolbar_item_url_bar(sensitivity, false);
		break;

	case THROBBER_ITEM:
		toolitem = make_toolbar_item_throbber(sensitivity, false);
		break;

	case WEBSEARCH_ITEM:
		toolitem = make_toolbar_item_websearch(sensitivity, false);
		break;

	default:
		break;

	}
	return toolitem;
}


/**
 * widget factory for creation of toolbar item widgets for the toolbox
 *
 * \param itemid the id of the widget
 * \return gtk tool item widget
 */
static GtkToolItem *
make_toolbox_item(nsgtk_toolbar_button itemid, bool bar)
{
	GtkToolItem *toolitem = NULL;

	switch(itemid) {
#define TOOLBAR_ITEM_y(identifier, label, iconame)
#define TOOLBAR_ITEM_n(identifier, label, iconame)
#define TOOLBAR_ITEM_t(identifier, label, iconame)		\
	case identifier:					\
		if (bar) {						\
			toolitem = make_toolbar_item_button(#label, iconame, true, true); \
		}							\
		break;
#define TOOLBAR_ITEM_b(identifier, label, iconame)		\
	case identifier:					\
		toolitem = make_toolbar_item_button(#label, iconame, true, true); \
		break;
#define TOOLBAR_ITEM(identifier, name, snstvty, clicked, activate, label, iconame) \
		TOOLBAR_ITEM_ ## clicked(identifier, label, iconame)

#include "gtk/toolbar_items.h"

#undef TOOLBAR_ITEM_t
#undef TOOLBAR_ITEM_b
#undef TOOLBAR_ITEM_n
#undef TOOLBAR_ITEM_y
#undef TOOLBAR_ITEM

	case HISTORY_BUTTON:
		toolitem = make_toolbar_item_history(true, true);
		break;

	case URL_BAR_ITEM:
		toolitem = make_toolbar_item_url_bar(false, true);
		break;

	case THROBBER_ITEM:
		toolitem = make_toolbar_item_throbber(true, true);
		break;

	case WEBSEARCH_ITEM:
		toolitem = make_toolbar_item_websearch(false, true);
		break;

	default:
		break;

	}
	return toolitem;
}


/**
 * target entry for drag source
 */
static GtkTargetEntry target_entry = {
	 (char *)"nsgtk_button_data",
	 GTK_TARGET_SAME_APP,
	 0
};


/**
 * find the toolbar item with a given location.
 *
 * \param tb the toolbar instance
 * \param locaction the location to search for
 * \return the item id for a location
 */
static nsgtk_toolbar_button
itemid_from_location(struct nsgtk_toolbar *tb, int location)
{
	int iidx;
	for (iidx = BACK_BUTTON; iidx < PLACEHOLDER_BUTTON; iidx++) {
		if (tb->items[iidx].location == location) {
			break;
		}
	}
	return iidx;
}


/**
 * save toolbar settings to file
 */
static nserror
nsgtk_toolbar_customisation_save(struct nsgtk_toolbar *tb)
{
	int iidx; /* item index */
	char *order; /* item ordering */
	char *start; /* start of next item name to be output */
	int orderlen = 0; /* length of item ordering */
	nsgtk_toolbar_button itemid;
	int location;
	char *choices = NULL;

	for (iidx = BACK_BUTTON; iidx < PLACEHOLDER_BUTTON; iidx++) {
		if (tb->items[iidx].location != INACTIVE_LOCATION) {
			orderlen += strlen(tb->items[iidx].name);
			orderlen++; /* allow for separator */
		}
	}

	/* ensure there are some items to store */
	if (orderlen == 0) {
		return NSERROR_INVALID;
	}

	order = malloc(orderlen);
	if (order == NULL) {
		return NSERROR_NOMEM;
	}

	start = order;

	for (location = BACK_BUTTON;
	     location < PLACEHOLDER_BUTTON;
	     location++) {
		itemid = itemid_from_location(tb, location);
		if (itemid == PLACEHOLDER_BUTTON) {
			/* no more filled locations */
			break;
		}
		start += snprintf(start,
				orderlen - (start - order),
				"%s/",
				tb->items[itemid].name);

		if ((start - order) >= orderlen) {
			break;
		}
	}

	order[orderlen - 1] = 0;

	nsoption_set_charp(toolbar_items, order);

	/* ensure choices are saved */
	netsurf_mkpath(&choices, NULL, 2, nsgtk_config_home, "Choices");
	if (choices != NULL) {
		nsoption_write(choices, NULL, NULL);
		free(choices);
	}

	return NSERROR_OK;
}


/**
 * connect signals to a toolbar item in a customisation toolbar
 *
 * \param tb The toolbar
 * \param itemid The item id within to toolbar to connect
 * \param NSERROR_OK on success
 */
static nserror
toolbar_item_connect_signals(struct nsgtk_toolbar *tb, int itemid)
{
	/* set toolbar items to be a drag source */
	gtk_tool_item_set_use_drag_window(tb->items[itemid].button, TRUE);
	gtk_drag_source_set(GTK_WIDGET(tb->items[itemid].button),
			    GDK_BUTTON1_MASK,
			    &target_entry,
			    1,
			    GDK_ACTION_COPY);
	g_signal_connect(tb->items[itemid].button,
			 "drag-data-get",
			 G_CALLBACK(tb->items[itemid].dataminus),
			 tb);
	return NSERROR_OK;
}


/**
 * customisation container handler for drag drop signal
 *
 * called when a widget is dropped onto the store window
 */
static gboolean
customisation_container_drag_drop_cb(GtkWidget *widget,
				     GdkDragContext *gdc,
				     gint x, gint y,
				     guint time,
				     gpointer data)
{
	struct nsgtk_toolbar_customisation *tbc;
	tbc = (struct nsgtk_toolbar_customisation *)data;
	int location;
	int itemid;

	if ((tbc->dragfrom) || (tbc->dragitem == -1)) {
		tbc->dragitem = -1;
		return FALSE;
	}

	if (tbc->toolbar.items[tbc->dragitem].location == INACTIVE_LOCATION) {
		tbc->dragitem = -1;
		gtk_drag_finish(gdc, TRUE, TRUE, time);
		return FALSE;

	}

	/* update the locations for all the subsequent toolbar items */
	for (location = tbc->toolbar.items[tbc->dragitem].location;
	     location < PLACEHOLDER_BUTTON;
	     location++) {
		itemid = itemid_from_location(&tbc->toolbar, location);
		if (itemid == PLACEHOLDER_BUTTON) {
			break;
		}
		tbc->toolbar.items[itemid].location--;
	}

	/* remove existing item */
	tbc->toolbar.items[tbc->dragitem].location = -1;
	gtk_container_remove(GTK_CONTAINER(tbc->toolbar.widget),
			     GTK_WIDGET(tbc->toolbar.items[tbc->dragitem].button));

	tbc->dragitem = -1;
	gtk_drag_finish(gdc, TRUE, TRUE, time);
	return FALSE;
}


/**
 * customisation container handler for drag motion signal
 *
 * called when hovering above the store
 */
static gboolean
customisation_container_drag_motion_cb(GtkWidget *widget,
				       GdkDragContext *gdc,
				       gint x, gint y,
				       guint time,
				       gpointer data)
{
	return FALSE;
}


/**
 * customisation toolbar handler for drag drop signal
 *
 * called when a widget is dropped onto the toolbar
 */
static gboolean
customisation_toolbar_drag_drop_cb(GtkWidget *widget,
				   GdkDragContext *gdc,
				   gint x,
				   gint y,
				   guint time,
				   gpointer data)
{
	struct nsgtk_toolbar_customisation *tbc;
	tbc = (struct nsgtk_toolbar_customisation *)data;
	gint position; /* drop position in toolbar */
	int location;
	int itemid;
	struct nsgtk_toolbar_item *dragitem; /* toolbar item being dragged */

	position = gtk_toolbar_get_drop_index(tbc->toolbar.widget, x, y);
	if (tbc->dragitem == -1) {
		return TRUE;
	}

	/* pure conveiance variable */
	dragitem = &tbc->toolbar.items[tbc->dragitem];

	/* deal with replacing existing item in toolbar */
	if (dragitem->location != INACTIVE_LOCATION) {
		if (dragitem->location < position) {
			position--;
		}

		/* update the locations for all the subsequent toolbar items */
		for (location = dragitem->location;
		     location < PLACEHOLDER_BUTTON;
		     location++) {
			itemid = itemid_from_location(&tbc->toolbar, location);
			if (itemid == PLACEHOLDER_BUTTON) {
				break;
			}
			tbc->toolbar.items[itemid].location--;
		}

		/* remove existing item */
		dragitem->location = INACTIVE_LOCATION;
		gtk_container_remove(GTK_CONTAINER(tbc->toolbar.widget),
				     GTK_WIDGET(dragitem->button));
	}


	dragitem->button = make_toolbox_item(tbc->dragitem, true);

	if (dragitem->button == NULL) {
		nsgtk_warning("NoMemory", 0);
		return TRUE;
	}

	/* update locations */
	for (location = PLACEHOLDER_BUTTON; location >= position; location--) {
		itemid = itemid_from_location(&tbc->toolbar, location);
		if (itemid != PLACEHOLDER_BUTTON) {
			tbc->toolbar.items[itemid].location++;
		}
	}
	dragitem->location = position;

	gtk_toolbar_insert(tbc->toolbar.widget,
			   dragitem->button,
			   dragitem->location);

	toolbar_item_connect_signals(&tbc->toolbar, tbc->dragitem);
	gtk_widget_show_all(GTK_WIDGET(dragitem->button));
	tbc->dragitem = -1;
	return TRUE;
}


/**
 * customisation toolbar handler for drag data received signal
 *
 * connected to toolbutton drop; perhaps one day it'll work properly
 * so it may replace the global current_button
 */
static gboolean
customisation_toolbar_drag_data_received_cb(GtkWidget *widget,
			    GdkDragContext *gdc,
			    gint x,
			    gint y,
			    GtkSelectionData *selection,
			    guint info,
			    guint time,
			    gpointer data)
{
	return FALSE;
}


/**
 * customisation toolbar handler for drag motion signal
 *
 * called when hovering an item above the toolbar
 */
static gboolean
customisation_toolbar_drag_motion_cb(GtkWidget *widget,
				     GdkDragContext *gdc,
				     gint x,
				     gint y,
				     guint time,
				     gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	GtkToolItem *item;
	gint position; /* position in toolbar */

	item = gtk_tool_button_new(NULL, NULL);
	position = gtk_toolbar_get_drop_index(tb->widget, x, y);

	gtk_toolbar_set_drop_highlight_item(tb->widget, item, position);

	return FALSE; /* drag not in drop zone */
}


/**
 * customisation toolbar handler for drag leave signal
 *
 * called when hovering stops
 */
static void
customisation_toolbar_drag_leave_cb(GtkWidget *widget,
				    GdkDragContext *gdc,
				    guint time,
				    gpointer data)
{
	gtk_toolbar_set_drop_highlight_item(GTK_TOOLBAR(widget), NULL, 0);
}


/**
 * create a new browser window
 *
 * creates a browser window with default url depending on user choices.
 *
 * \param bw The browser window to pass for existing window/
 * \param intab true if the new window should be in a tab else false
 *                for new window.
 * \return NSERROR_OK on success else error code.
 */
static nserror
nsgtk_browser_window_create(struct browser_window *bw, bool intab)
{
	nserror res = NSERROR_OK;
	nsurl *url = NULL;
	int flags = BW_CREATE_HISTORY | BW_CREATE_FOREGROUND | BW_CREATE_FOCUS_LOCATION;

	if (intab) {
		flags |= BW_CREATE_TAB;
	}

	if (!nsoption_bool(new_blank)) {
		const char *addr;
		if (nsoption_charp(homepage_url) != NULL) {
			addr = nsoption_charp(homepage_url);
		} else {
			addr = NETSURF_HOMEPAGE;
		}
		res = nsurl_create(addr, &url);
	}

	if (res == NSERROR_OK) {
		res = browser_window_create(flags, url, NULL, bw, NULL);
	}

	if (url != NULL) {
		nsurl_unref(url);
	}

	return res;
}


/**
 * Apply the user toolbar button settings from configuration
 *
 * GTK specific user option string is a set of fields arranged as
 * [itemreference];[itemlocation]|[itemreference];[itemlocation]| etc
 *
 * \param tb The toolbar to apply customisation to
 * \param NSERROR_OK on success else error code.
 */
static nserror
apply_user_button_customisation(struct nsgtk_toolbar *tb)
{
	const char *tbitems; /* item order user config */
	const char *start;
	const char *end;
	int iidx; /* item index */
	int location = 0; /* location index */

	/* set all button locations to inactive */
	for (iidx = BACK_BUTTON; iidx < PLACEHOLDER_BUTTON; iidx++) {
		tb->items[iidx].location = INACTIVE_LOCATION;
	}

	tbitems = nsoption_charp(toolbar_items);
	if (tbitems == NULL) {
		tbitems = "";
	}

	end = tbitems;
	while (*end != 0) {
		start = end;
		while ((*end != 0) && (*end !='/')) {
			end++;
		}

		for (iidx = BACK_BUTTON; iidx < PLACEHOLDER_BUTTON; iidx++) {
			if (((ssize_t)strlen(tb->items[iidx].name) == (end - start)) &&
			    (strncmp(tb->items[iidx].name, start, end - start) == 0)) {
				tb->items[iidx].location = location++;
				break;
			}
		}

		if (*end == '/') {
			end++;
		}
	}

	if (location == 0) {
		/* completely failed to create any buttons so use defaults */
		tb->items[BACK_BUTTON].location = location++;
		tb->items[HISTORY_BUTTON].location = location++;
		tb->items[FORWARD_BUTTON].location = location++;
		tb->items[RELOADSTOP_BUTTON].location = location++;
		tb->items[URL_BAR_ITEM].location = location++;
		tb->items[WEBSEARCH_ITEM].location = location++;
		tb->items[OPENMENU_BUTTON].location = location++;
		tb->items[THROBBER_ITEM].location = location++;
	}


	return NSERROR_OK;
}


/**
 * callback function to remove a widget from a container
 */
static void container_remove_widget(GtkWidget *widget, gpointer data)
{
	GtkContainer *container = GTK_CONTAINER(data);
	gtk_container_remove(container, widget);
}


/**
 * populates a toolbar with widgets in correct order
 *
 * \param tb toolbar
 * \return NSERROR_OK on success else error code.
 */
static nserror populate_gtk_toolbar_widget(struct nsgtk_toolbar *tb)
{
	int location; /* location index */
	int itemid;

	/* clear the toolbar container of all widgets */
	gtk_container_foreach(GTK_CONTAINER(tb->widget),
			      container_remove_widget,
			      tb->widget);

	/* add widgets to toolbar */
	for (location = 0; location < PLACEHOLDER_BUTTON; location++) {
		itemid = itemid_from_location(tb, location);
		if (itemid == PLACEHOLDER_BUTTON) {
			break;
		}
		tb->items[itemid].button =
			make_toolbar_item(itemid,
					  tb->items[itemid].sensitivity);

		gtk_toolbar_insert(tb->widget,
				   tb->items[itemid].button,
				   location);
	}

	gtk_widget_show_all(GTK_WIDGET(tb->widget));

	return NSERROR_OK;
}


/**
 * populates the customization toolbar with widgets in correct order
 *
 * \param tb toolbar
 * \return NSERROR_OK on success else error code.
 */
static nserror customisation_toolbar_populate(struct nsgtk_toolbar *tb)
{
	int location; /* location index */
	int itemid;

	/* clear the toolbar container of all widgets */
	gtk_container_foreach(GTK_CONTAINER(tb->widget),
			      container_remove_widget,
			      tb->widget);

	/* add widgets to toolbar */
	for (location = 0; location < PLACEHOLDER_BUTTON; location++) {
		itemid = itemid_from_location(tb, location);
		if (itemid == PLACEHOLDER_BUTTON) {
			break;
		}
		tb->items[itemid].button = make_toolbox_item(itemid, true);

		gtk_toolbar_insert(tb->widget,
				   tb->items[itemid].button,
				   location);
	}

	gtk_widget_show_all(GTK_WIDGET(tb->widget));

	return NSERROR_OK;
}


/**
 * find the toolbar item with a given gtk widget.
 *
 * \param tb the toolbar instance
 * \param toolitem the tool item widget to search for
 * \return the item id matching the widget
 */
static nsgtk_toolbar_button
itemid_from_gtktoolitem(struct nsgtk_toolbar *tb, GtkToolItem *toolitem)
{
	int iidx;
	for (iidx = BACK_BUTTON; iidx < PLACEHOLDER_BUTTON; iidx++) {
		if ((tb->items[iidx].location != INACTIVE_LOCATION) &&
		    (tb->items[iidx].button == toolitem)) {
			break;
		}
	}
	return iidx;
}


/**
 * set a toolbar items sensitivity
 *
 * note this does not set menu items sensitivity
 */
static nserror
set_item_sensitivity(struct nsgtk_toolbar_item *item, bool sensitivity)
{
	if (item->sensitivity != sensitivity) {
		/* item requires sensitivity changing */
		item->sensitivity = sensitivity;

		if ((item->location != -1) && (item->button != NULL)) {
			gtk_widget_set_sensitive(GTK_WIDGET(item->button),
						 item->sensitivity);
		}
	}

	return NSERROR_OK;
}


/**
 * set an item to its alternative action
 *
 * this is currently only used for the stop/reload button where we
 *   also reuse the item sensitivity for the state indicator.
 *
 * \param tb the toolbar instance
 */
static nserror set_item_action(struct nsgtk_toolbar *tb, int itemid, bool alt)
{
	const char *iconname;
	char *label = NULL;

	if (itemid != RELOADSTOP_BUTTON) {
		return NSERROR_INVALID;
	}
	if (tb->items[itemid].location == -1) {
		return NSERROR_OK;
	}
	tb->items[itemid].sensitivity = alt;

	if (tb->items[itemid].button == NULL) {
		return NSERROR_INVALID;
	}

	if (tb->items[itemid].sensitivity) {
		iconname = NSGTK_STOCK_REFRESH;
		label = remove_underscores(messages_get("Reload"), false);

	} else {
		iconname = NSGTK_STOCK_STOP;
		label = remove_underscores(messages_get("gtkStop"), false);

	}
	gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb->items[itemid].button),
				  label);

	gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb->items[itemid].button),
				      iconname);

	gtk_widget_set_sensitive(GTK_WIDGET(tb->items[itemid].button), TRUE);

	if (label != NULL) {
		free(label);
	}

	return NSERROR_OK;
}


/**
 * cause the toolbar browsing context to navigate to a new url.
 *
 * \param tb the toolbar context.
 * \param urltxt The url string.
 * \return NSERROR_OK on success else appropriate error code.
 */
static nserror
toolbar_navigate_to_url(struct nsgtk_toolbar *tb, const char *urltxt)
{
	struct browser_window *bw;
	nsurl *url;
	nserror res;

	res = nsurl_create(urltxt, &url);
	if (res != NSERROR_OK) {
		return res;
	}

	bw = tb->get_bw(tb->get_ctx);

	res = browser_window_navigate(bw,
				      url,
				      NULL,
				      BW_NAVIGATE_HISTORY,
				      NULL,
				      NULL,
				      NULL);
	nsurl_unref(url);

	return res;
}


/**
 * run a gtk file chooser as a save dialog to obtain a path
 */
static nserror
nsgtk_saveas_dialog(struct browser_window *bw,
		    const char *title,
		    GtkWindow *parent,
		    bool folder,
		    gchar **path_out)
{
	nserror res;
	GtkWidget *fc; /* file chooser widget */
	GtkFileChooserAction action;
	char *path; /* proposed path */

	if (!browser_window_has_content(bw)) {
		/* cannot save a page with no content */
		return NSERROR_INVALID;
	}

	if (folder) {
		action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
	} else {
		action = GTK_FILE_CHOOSER_ACTION_SAVE;
	}

	fc = gtk_file_chooser_dialog_new(title,
					 parent,
					 action,
					 NSGTK_STOCK_CANCEL,
					 GTK_RESPONSE_CANCEL,
					 NSGTK_STOCK_SAVE,
					 GTK_RESPONSE_ACCEPT,
					 NULL);

	/* set a default file name */
	res = nsurl_nice(browser_window_access_url(bw), &path, false);
	if (res != NSERROR_OK) {
		path = strdup(messages_get("SaveText"));
		if (path == NULL) {
			gtk_widget_destroy(fc);
			return NSERROR_NOMEM;
		}
	}

	if ((!folder) || (access(path, F_OK) != 0)) {
		gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fc), path);
	}
	free(path);

	/* confirm overwriting */
	gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(fc), TRUE);

	/* run the dialog to let user select path */
	if (gtk_dialog_run(GTK_DIALOG(fc)) != GTK_RESPONSE_ACCEPT) {
		gtk_widget_destroy(fc);
		return NSERROR_NOT_FOUND;
	}

	*path_out = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));

	gtk_widget_destroy(fc);

	return NSERROR_OK;
}


/**
 * connect all signals to widgets in a customisation
 */
static nserror
toolbar_customisation_connect_signals(struct nsgtk_toolbar *tb)
{
	int iidx;

	for (iidx = BACK_BUTTON; iidx < PLACEHOLDER_BUTTON; iidx++) {
		/* skip inactive items in toolbar */
		if (tb->items[iidx].location != INACTIVE_LOCATION) {
			toolbar_item_connect_signals(tb, iidx);
		}
	}

	/* add move button listeners */
	g_signal_connect(tb->widget,
			"drag-drop",
			 G_CALLBACK(customisation_toolbar_drag_drop_cb),
			 tb);
	g_signal_connect(tb->widget,
			"drag-data-received",
			 G_CALLBACK(customisation_toolbar_drag_data_received_cb),
			 tb);
	g_signal_connect(tb->widget,
			"drag-motion",
			 G_CALLBACK(customisation_toolbar_drag_motion_cb),
			 tb);
	g_signal_connect(tb->widget,
			"drag-leave",
			 G_CALLBACK(customisation_toolbar_drag_leave_cb),
			 tb);

	/* set data types */
	gtk_drag_dest_set(GTK_WIDGET(tb->widget),
			  GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
			  &target_entry,
			  1,
			  GDK_ACTION_COPY);

	return NSERROR_OK;
}


static void
item_size_allocate_cb(GtkWidget *widget,
		      GdkRectangle *alloc,
		      gpointer user_data)
{
	if (alloc->width > NSGTK_BUTTON_WIDTH) {
		alloc->width = NSGTK_BUTTON_WIDTH;
	}
	if (alloc->height > NSGTK_BUTTON_HEIGHT) {
		alloc->height = NSGTK_BUTTON_HEIGHT;
	}
	gtk_widget_set_allocation(widget, alloc);
}


/**
 * add a row to a toolbar customisation toolbox
 *
 * \param tbc The toolbar customisation context
 * \param startitem The item index of the beginning of the row
 * \param enditem The item index of the beginning of the next row
 * \return NSERROR_OK on successs else error
 */
static nserror
add_toolbox_row(struct nsgtk_toolbar_customisation *tbc,
		int startitem,
		int enditem)
{
	GtkToolbar *rowbar;
	int iidx;

	rowbar = GTK_TOOLBAR(gtk_toolbar_new());
	if (rowbar == NULL) {
		return NSERROR_NOMEM;
	}

	gtk_toolbar_set_style(rowbar, GTK_TOOLBAR_BOTH);
	gtk_toolbar_set_icon_size(rowbar, GTK_ICON_SIZE_LARGE_TOOLBAR);
	gtk_box_pack_start(tbc->toolbox, GTK_WIDGET(rowbar), FALSE, FALSE, 0);

	for (iidx = startitem; iidx < enditem; iidx++) {
		if (tbc->items[iidx] == NULL) {
			/* skip any widgets that failed to initialise */
			continue;
		}
		gtk_widget_set_size_request(GTK_WIDGET(tbc->items[iidx]),
					    NSGTK_BUTTON_WIDTH,
					    NSGTK_BUTTON_HEIGHT);
		gtk_tool_item_set_use_drag_window(tbc->items[iidx], TRUE);
		gtk_drag_source_set(GTK_WIDGET(tbc->items[iidx]),
				    GDK_BUTTON1_MASK,
				    &target_entry,
				    1,
				    GDK_ACTION_COPY);
		g_signal_connect(tbc->items[iidx],
				 "drag-data-get",
				 G_CALLBACK(tbc->toolbar.items[iidx].dataplus),
				 &tbc->toolbar);
		g_signal_connect(tbc->items[iidx],
				 "size-allocate",
				 G_CALLBACK(item_size_allocate_cb),
				 NULL);
		gtk_toolbar_insert(rowbar, tbc->items[iidx], -1);
	}
	return NSERROR_OK;
}


/**
 * creates widgets in customisation toolbox
 *
 * \param tbc The toolbar customisation context
 * \param width The width to layout the toolbox to
 * \return NSERROR_OK on success else error code.
 */
static nserror
toolbar_customisation_create_toolbox(struct nsgtk_toolbar_customisation *tbc,
				     int width)
{
	int columns; /* number of items in a single row */
	int curcol; /* current column in creation */
	int iidx; /* item index */
	int startidx; /* index of item at start of row */

	/* ensure there are a minimum number of items per row */
	columns = width / NSGTK_BUTTON_WIDTH;
	if (columns < NSGTK_MIN_STORE_COLUMNS) {
		columns = NSGTK_MIN_STORE_COLUMNS;
	}

	curcol = 0;
	for (iidx = startidx = BACK_BUTTON; iidx < PLACEHOLDER_BUTTON; iidx++) {
		if (curcol >= columns) {
			add_toolbox_row(tbc, startidx, iidx);
			curcol = 0;
			startidx = iidx;
		}
		tbc->items[iidx] = make_toolbox_item(iidx, false);
		if (tbc->items[iidx] != NULL) {
			curcol++;
		}
	}
	if (curcol > 0) {
		add_toolbox_row(tbc, startidx, iidx);
	}

	return NSERROR_OK;
}


/**
 * update toolbar in customisation to user settings
 */
static nserror
customisation_toolbar_update(struct nsgtk_toolbar_customisation *tbc)
{
	nserror res;

	res = apply_user_button_customisation(&tbc->toolbar);
	if (res != NSERROR_OK) {
		return res;
	}

	/* populate toolbar widget */
	res = customisation_toolbar_populate(&tbc->toolbar);
	if (res != NSERROR_OK) {
		return res;
	}

	/* ensure icon sizes and text labels on toolbar are set */
	res = nsgtk_toolbar_restyle(&tbc->toolbar);
	if (res != NSERROR_OK) {
		return res;
	}

	/* attach handlers to toolbar widgets */
	res = toolbar_customisation_connect_signals(&tbc->toolbar);
	if (res != NSERROR_OK) {
		return res;
	}

	return NSERROR_OK;
}


/**
 * customisation apply handler for clicked signal
 *
 * when 'save settings' button is clicked
 */
static gboolean
customisation_apply_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar_customisation *tbc;
	tbc = (struct nsgtk_toolbar_customisation *)data;

	/* save state to file, update toolbars for all windows */
	nsgtk_toolbar_customisation_save(&tbc->toolbar);
	nsgtk_window_toolbar_update();
	gtk_widget_destroy(tbc->container);

	return TRUE;
}


/**
 * customisation reset handler for clicked signal
 *
 * when 'reload defaults' button is clicked
 */
static gboolean
customisation_reset_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar_customisation *tbc;
	tbc = (struct nsgtk_toolbar_customisation *)data;

	customisation_toolbar_update(tbc);

	return TRUE;
}


/**
 * customisation container destroy handler
 */
static void customisation_container_destroy_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar_customisation *tbc;
	tbc = (struct nsgtk_toolbar_customisation *)data;

	free(tbc);
}

/*
 * Toolbar button clicked handlers
 */

/**
 * create a toolbar customisation tab
 *
 * this is completely different approach to previous implementation. it
 *  is not modal and the toolbar configuration is performed completely
 *  within the tab. once the user is happy they can apply the change or
 *  cancel as they see fit while continuing to use the browser as usual.
 */
static gboolean cutomize_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar_customisation *tbc;
	nserror res;
	GtkBuilder *builder;
	GtkNotebook *notebook; /* notebook containing widget */
	GtkAllocation notebook_alloc; /* notebook size allocation */
	int iidx; /* item index */

	/* obtain the notebook being added to */
	notebook = GTK_NOTEBOOK(gtk_widget_get_ancestor(widget,
							GTK_TYPE_NOTEBOOK));
	if (notebook == NULL) {
		return TRUE;
	}

	/* create builder */
	res = nsgtk_builder_new_from_resname("toolbar", &builder);
	if (res != NSERROR_OK) {
		NSLOG(netsurf, INFO, "Toolbar UI builder init failed");
		return TRUE;
	}
	gtk_builder_connect_signals(builder, NULL);

	/* create nsgtk_toolbar_customisation which has nsgtk_toolbar
	 * at the front so we can reuse functions that take
	 * nsgtk_toolbar
	 */
	tbc = calloc(1, sizeof(struct nsgtk_toolbar_customisation));
	if (tbc == NULL) {
		g_object_unref(builder);
		return TRUE;
	}

	/* get container box widget which forms a page of the tabs */
	tbc->container = GTK_WIDGET(gtk_builder_get_object(builder, "customisation"));
	if (tbc->container == NULL) {
		goto cutomize_button_clicked_cb_error;
	}

	/* vertical box for the toolbox to drag items into and out of */
	tbc->toolbox = GTK_BOX(gtk_builder_get_object(builder, "toolbox"));
	if (tbc->toolbox == NULL) {
		goto cutomize_button_clicked_cb_error;
	}

	/* customisation toolbar container */
	tbc->toolbar.widget = GTK_TOOLBAR(gtk_builder_get_object(builder, "toolbar"));
	if (tbc->toolbar.widget == NULL) {
		goto cutomize_button_clicked_cb_error;
	}

	/* build customisation toolbar */
	gtk_toolbar_set_show_arrow(tbc->toolbar.widget, TRUE);

	for (iidx = BACK_BUTTON; iidx < PLACEHOLDER_BUTTON; iidx++) {
		res = toolbar_item_create(iidx, &tbc->toolbar.items[iidx]);
		if (res != NSERROR_OK) {
			goto cutomize_button_clicked_cb_error;
		}
	}

	res = customisation_toolbar_update(tbc);
	if (res != NSERROR_OK) {
		goto cutomize_button_clicked_cb_error;
	}

	/* use toolbox for widgets to drag to/from */
	gtk_widget_get_allocation(GTK_WIDGET(notebook), &notebook_alloc);

	res = toolbar_customisation_create_toolbox(tbc, notebook_alloc.width);
	if (res != NSERROR_OK) {
		goto cutomize_button_clicked_cb_error;
	}

	/* configure the container */
	gtk_drag_dest_set(GTK_WIDGET(tbc->container),
			  GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
			  &target_entry,
			  1,
			  GDK_ACTION_COPY);

	/* discard button calls destroy */
	g_signal_connect_swapped(GTK_WIDGET(gtk_builder_get_object(builder,
								   "discard")),
				 "clicked",
				 G_CALLBACK(gtk_widget_destroy),
				 tbc->container);

	/* save and update on apply button  */
	g_signal_connect(GTK_WIDGET(gtk_builder_get_object(builder, "apply")),
			 "clicked",
			 G_CALLBACK(customisation_apply_clicked_cb),
			 tbc);

	g_signal_connect(GTK_WIDGET(gtk_builder_get_object(builder, "reset")),
			 "clicked",
			 G_CALLBACK(customisation_reset_clicked_cb),
			 tbc);

	/* close and cleanup on delete signal */
	g_signal_connect(tbc->container,
			 "destroy",
			 G_CALLBACK(customisation_container_destroy_cb),
			 tbc);


	g_signal_connect(tbc->container,
			 "drag-drop",
			 G_CALLBACK(customisation_container_drag_drop_cb),
			 tbc);

	g_signal_connect(tbc->container,
			 "drag-motion",
			 G_CALLBACK(customisation_container_drag_motion_cb),
			 tbc);


	nsgtk_tab_add_page(notebook,
			   tbc->container,
			   false,
			   messages_get("gtkCustomizeToolbarTitle"),
			   favicon_pixbuf);


	/* safe to drop the reference to the builder as the container is
	 * referenced by the notebook now.
	 */
	g_object_unref(builder);

	return TRUE;

 cutomize_button_clicked_cb_error:
	free(tbc);
	g_object_unref(builder);
	return TRUE;

}


/**
 * callback for all toolbar items widget size allocation
 *
 * handler connected to all toolbar items for the size-allocate signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param alloc The size allocation being set.
 * \param data The toolbar context passed when the signal was connected
 */
static void
toolbar_item_size_allocate_cb(GtkWidget *widget,
			      GtkAllocation *alloc,
			      gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	nsgtk_toolbar_button itemid;

	itemid = itemid_from_gtktoolitem(tb, GTK_TOOL_ITEM(widget));

	if ((tb->toolbarmem == alloc->x) ||
	    (tb->items[itemid].location < tb->items[HISTORY_BUTTON].location)) {
		/*
		 * no reallocation after first adjustment,
		 * no reallocation for buttons left of history button
		 */
		return;
	}

	if (itemid == HISTORY_BUTTON) {
		if (alloc->width == 20) {
			return;
		}

		tb->toolbarbase = alloc->y + alloc->height;
		tb->historybase = alloc->x + 20;
		if (tb->offset == 0) {
			tb->offset = alloc->width - 20;
		}
		alloc->width = 20;
	} else if (tb->items[itemid].location <= tb->items[URL_BAR_ITEM].location) {
		alloc->x -= tb->offset;
		if (itemid == URL_BAR_ITEM) {
			alloc->width += tb->offset;
		}
	}
	tb->toolbarmem = alloc->x;

	gtk_widget_size_allocate(widget, alloc);
}


/**
 * handler for back tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
back_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);

	if ((bw != NULL) && browser_window_history_back_available(bw)) {
		/* clear potential search effects */
		browser_window_search_clear(bw);

		browser_window_history_back(bw, false);

		set_item_sensitivity(&tb->items[BACK_BUTTON],
				browser_window_history_back_available(bw));
		set_item_sensitivity(&tb->items[FORWARD_BUTTON],
				browser_window_history_forward_available(bw));

		nsgtk_local_history_hide();
	}
	return TRUE;
}


/**
 * handler for forward tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
forward_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);

	if ((bw != NULL) && browser_window_history_forward_available(bw)) {
		/* clear potential search effects */
		browser_window_search_clear(bw);

		browser_window_history_forward(bw, false);

		set_item_sensitivity(&tb->items[BACK_BUTTON],
				browser_window_history_back_available(bw));
		set_item_sensitivity(&tb->items[FORWARD_BUTTON],
				browser_window_history_forward_available(bw));
		nsgtk_local_history_hide();
	}
	return TRUE;
}


/**
 * handler for stop tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
stop_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;

	browser_window_stop(tb->get_bw(tb->get_ctx));

	return TRUE;
}


/**
 * handler for reload tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
reload_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);

	/* clear potential search effects */
	browser_window_search_clear(bw);

	browser_window_reload(bw, true);

	return TRUE;
}


/**
 * handler for reload/stop tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
reloadstop_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);

	/* clear potential search effects */
	browser_window_search_clear(bw);

	if (tb->items[RELOADSTOP_BUTTON].sensitivity) {
		browser_window_reload(bw, true);
	} else {
		browser_window_stop(tb->get_bw(tb->get_ctx));
	}

	return TRUE;
}


/**
 * handler for home tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
home_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	nserror res;
	const char *addr;

	if (nsoption_charp(homepage_url) != NULL) {
		addr = nsoption_charp(homepage_url);
	} else {
		addr = NETSURF_HOMEPAGE;
	}

	res = toolbar_navigate_to_url(tb, addr);
	if (res != NSERROR_OK) {
		nsgtk_warning(messages_get_errorcode(res), 0);
	}

	return TRUE;
}


/**
 * callback for url entry widget activation
 *
 * handler connected to url entry widget for the activate signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE to allow activation.
 */
static gboolean url_entry_activate_cb(GtkWidget *widget, gpointer data)
{
	nserror res;
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	nsurl *url;

	res = search_web_omni(gtk_entry_get_text(GTK_ENTRY(widget)),
			      SEARCH_WEB_OMNI_NONE,
			      &url);
	if (res == NSERROR_OK) {
		bw = tb->get_bw(tb->get_ctx);
		res = browser_window_navigate(
			bw, url, NULL, BW_NAVIGATE_HISTORY, NULL, NULL, NULL);
		nsurl_unref(url);
	}
	if (res != NSERROR_OK) {
		nsgtk_warning(messages_get_errorcode(res), 0);
	}

	return TRUE;
}


/**
 * callback for url entry widget changing
 *
 * handler connected to url entry widget for the change signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param event The key change event that changed the entry.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE to allow activation.
 */
static gboolean
url_entry_changed_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
	return nsgtk_completion_update(GTK_ENTRY(widget));
}


/**
 * callback for url entry widget icon button release
 *
 * handler connected to url entry widget for the icon release signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param event The key change event that changed the entry.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE to allow activation.
 */
static void
url_entry_icon_release_cb(GtkEntry *entry,
			   GtkEntryIconPosition icon_pos,
			   GdkEvent *event,
			   gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);

	nsgtk_page_info(bw);
}


/**
 * handler for web search tool bar entry item activate signal
 *
 * handler connected to web search entry widget for the activate signal
 *
 * \todo make this user selectable to switch between opening in new
 *   and navigating current window. Possibly improve core search_web interfaces
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean websearch_entry_activate_cb(GtkWidget *widget, gpointer data)
{
	nserror res;
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	nsurl *url;

	res = search_web_omni(gtk_entry_get_text(GTK_ENTRY(widget)),
			      SEARCH_WEB_OMNI_SEARCHONLY,
			      &url);
	if (res == NSERROR_OK) {
		bw = tb->get_bw(tb->get_ctx);

		res = browser_window_create(
			BW_CREATE_HISTORY | BW_CREATE_TAB | BW_CREATE_FOREGROUND,
			url,
			NULL,
			bw,
			NULL);
		nsurl_unref(url);
	}
	if (res != NSERROR_OK) {
		nsgtk_warning(messages_get_errorcode(res), 0);
	}

	return TRUE;
}

/**
 * handler for web search tool bar item button press signal
 *
 * allows a click in the websearch entry field to clear the name of the
 * provider.
 *
 * \todo this does not work well, different behaviour wanted perhaps?
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
websearch_entry_button_press_cb(GtkWidget *widget,
			     GdkEventFocus *f,
			     gpointer data)
{
	gtk_editable_select_region(GTK_EDITABLE(widget), 0, -1);
	gtk_widget_grab_focus(GTK_WIDGET(widget));

	return TRUE;
}


/**
 * handler for new window tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
newwindow_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	nserror res;
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;

	res = nsgtk_browser_window_create(tb->get_bw(tb->get_ctx), false);
	if (res != NSERROR_OK) {
		nsgtk_warning(messages_get_errorcode(res), 0);
	}

	return TRUE;
}


/**
 * handler for new tab tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
newtab_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	nserror res;
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;

	res = nsgtk_browser_window_create(tb->get_bw(tb->get_ctx), true);
	if (res != NSERROR_OK) {
		nsgtk_warning(messages_get_errorcode(res), 0);
	}
	return TRUE;
}


/**
 * handler for open file tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
openfile_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	GtkWidget *dlgOpen;
	gint response;
	GtkWidget *toplevel;
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	dlgOpen = gtk_file_chooser_dialog_new("Open File",
					      GTK_WINDOW(toplevel),
			GTK_FILE_CHOOSER_ACTION_OPEN,
			NSGTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			NSGTK_STOCK_OPEN, GTK_RESPONSE_OK,
			NULL, NULL);

	response = gtk_dialog_run(GTK_DIALOG(dlgOpen));
	if (response == GTK_RESPONSE_OK) {
		char *urltxt;
		gchar *filename;
		nserror res;
		nsurl *url;

		filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlgOpen));

		urltxt = malloc(strlen(filename) + FILE_SCHEME_PREFIX_LEN + 1);
		if (urltxt != NULL) {
			sprintf(urltxt, FILE_SCHEME_PREFIX"%s", filename);

			res = nsurl_create(urltxt, &url);
			if (res == NSERROR_OK) {
				bw = tb->get_bw(tb->get_ctx);
				res = browser_window_navigate(bw,
							url,
							NULL,
							BW_NAVIGATE_HISTORY,
							NULL,
							NULL,
							NULL);
				nsurl_unref(url);
			}
			if (res != NSERROR_OK) {
				nsgtk_warning(messages_get_errorcode(res), 0);
			}
			free(urltxt);
		}


		g_free(filename);
	}

	gtk_widget_destroy(dlgOpen);

	return TRUE;
}


/**
 * handler for close window tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
closewindow_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	GtkWidget *toplevel;
	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);
	gtk_widget_destroy(toplevel);
	return TRUE;
}


/**
 * handler for full save export tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
savepage_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	DIR *d;
	gchar *path;
	nserror res;
	GtkWidget *toplevel;

	bw = tb->get_bw(tb->get_ctx);
	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	res = nsgtk_saveas_dialog(bw,
				  messages_get("gtkcompleteSave"),
				  GTK_WINDOW(toplevel),
				  true,
				  &path);
	if (res != NSERROR_OK) {
		return FALSE;
	}

	d = opendir(path);
	if (d == NULL) {
		NSLOG(netsurf, INFO,
		      "Unable to open directory %s for complete save: %s",
		      path,
		      strerror(errno));
		if (errno == ENOTDIR) {
			nsgtk_warning("NoDirError", path);
		} else {
			nsgtk_warning("gtkFileError", path);
		}
		g_free(path);
		return TRUE;
	}
	closedir(d);

	save_complete(browser_window_get_content(bw), path, NULL);
	g_free(path);

	return TRUE;
}


/**
 * handler for pdf export tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
pdf_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWidget *toplevel;
	gchar *filename;
	nserror res;

	bw = tb->get_bw(tb->get_ctx);

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	res = nsgtk_saveas_dialog(bw,
				  "Export to PDF",
				  GTK_WINDOW(toplevel),
				  false,
				  &filename);
	if (res != NSERROR_OK) {
		return FALSE;
	}

#ifdef WITH_PDF_EXPORT
	struct print_settings *settings;

	/* this way the scale used by PDF functions is synchronised with that
	 * used by the all-purpose print interface
	 */
	haru_nsfont_set_scale((float)option_export_scale / 100);

	settings = print_make_settings(PRINT_OPTIONS,
				       (const char *) filename,
				       &haru_nsfont);
	g_free(filename);
	if (settings == NULL) {
		return TRUE;
	}
	/* This will clean up the print_settings object for us */
	print_basic_run(browser_window_get_content(bw),	&pdf_printer, settings);
#endif
	return TRUE;

}


/**
 * handler for plain text export tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
plaintext_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWidget *toplevel;
	gchar *filename;
	nserror res;

	bw = tb->get_bw(tb->get_ctx);

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	res = nsgtk_saveas_dialog(bw,
				  messages_get("gtkplainSave"),
				  GTK_WINDOW(toplevel),
				  false,
				  &filename);
	if (res != NSERROR_OK) {
		return FALSE;
	}


	save_as_text(browser_window_get_content(bw), filename);
	g_free(filename);

	return TRUE;
}


/**
 * handler for print tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
print_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkPrintOperation *print_op;
	GtkPageSetup *page_setup;
	GtkPrintSettings *print_settings;
	GtkPrintOperationResult res = GTK_PRINT_OPERATION_RESULT_ERROR;
	struct print_settings *nssettings;
	char *settings_fname = NULL;
	GtkWidget *toplevel;

	bw = tb->get_bw(tb->get_ctx);

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	print_op = gtk_print_operation_new();
	if (print_op == NULL) {
		nsgtk_warning(messages_get("NoMemory"), 0);
		return TRUE;
	}

	/* use previously saved settings if any */
	netsurf_mkpath(&settings_fname, NULL, 2, nsgtk_config_home, "Print");
	if (settings_fname != NULL) {
		print_settings = gtk_print_settings_new_from_file(settings_fname, NULL);
		if (print_settings != NULL) {
			gtk_print_operation_set_print_settings(print_op,
							       print_settings);

			/* We're not interested in the settings any more */
			g_object_unref(print_settings);
		}
	}

	content_to_print = browser_window_get_content(bw);

	page_setup = gtk_print_run_page_setup_dialog(GTK_WINDOW(toplevel),
						     NULL,
						     NULL);
	if (page_setup == NULL) {
		nsgtk_warning(messages_get("NoMemory"), 0);
		free(settings_fname);
		g_object_unref(print_op);
		return TRUE;
	}
	gtk_print_operation_set_default_page_setup(print_op, page_setup);

	nssettings = print_make_settings(PRINT_DEFAULT,
					 NULL,
					 nsgtk_layout_table);

	g_signal_connect(print_op,
			 "begin_print",
			 G_CALLBACK(gtk_print_signal_begin_print),
			 nssettings);
	g_signal_connect(print_op,
			 "draw_page",
			 G_CALLBACK(gtk_print_signal_draw_page),
			 NULL);
	g_signal_connect(print_op,
			 "end_print",
			 G_CALLBACK(gtk_print_signal_end_print),
			 nssettings);

	if (content_get_type(browser_window_get_content(bw)) != CONTENT_TEXTPLAIN) {
		res = gtk_print_operation_run(print_op,
					      GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
					      GTK_WINDOW(toplevel),
					      NULL);
	}

	/* if the settings were used save them for future use */
	if (settings_fname != NULL) {
		if (res == GTK_PRINT_OPERATION_RESULT_APPLY) {
			/* Do not increment the settings reference */
			print_settings = gtk_print_operation_get_print_settings(print_op);

			gtk_print_settings_to_file(print_settings,
						   settings_fname,
						   NULL);
		}
		free(settings_fname);
	}

	/* Our print_settings object is destroyed by the end print handler */
	g_object_unref(page_setup);
	g_object_unref(print_op);

	return TRUE;
}

/**
 * handler for quit tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
quit_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	nsgtk_scaffolding_destroy_all();
	return TRUE;
}


/**
 * handler for cut tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
cut_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWidget *focused;
	GtkWidget *toplevel;

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	focused = gtk_window_get_focus(GTK_WINDOW(toplevel));

	/* let gtk handle it if focused widget is an editable */
	if (GTK_IS_EDITABLE(focused)) {
		gtk_editable_cut_clipboard(GTK_EDITABLE(focused));
	} else {
		bw = tb->get_bw(tb->get_ctx);
		browser_window_key_press(bw, NS_KEY_CUT_SELECTION);
	}

	return TRUE;
}


/**
 * handler for copy tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
copy_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWidget *focused;
	GtkWidget *toplevel;

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	focused = gtk_window_get_focus(GTK_WINDOW(toplevel));

	/* let gtk handle it if focused widget is an editable */
	if (GTK_IS_EDITABLE(focused)) {
		gtk_editable_copy_clipboard(GTK_EDITABLE(focused));
	} else {
		bw = tb->get_bw(tb->get_ctx);
		browser_window_key_press(bw, NS_KEY_COPY_SELECTION);
	}

	return TRUE;
}


/**
 * handler for paste tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
paste_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWidget *focused;
	GtkWidget *toplevel;

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	focused = gtk_window_get_focus(GTK_WINDOW(toplevel));

	/* let gtk handle it if focused widget is an editable */
	if (GTK_IS_EDITABLE(focused)) {
		gtk_editable_paste_clipboard(GTK_EDITABLE(focused));
	} else {
		bw = tb->get_bw(tb->get_ctx);
		browser_window_key_press(bw, NS_KEY_PASTE);
	}

	return TRUE;
}


/**
 * handler for delete tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
delete_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWidget *focused;
	GtkWidget *toplevel;

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	focused = gtk_window_get_focus(GTK_WINDOW(toplevel));

	/* let gtk handle it if focused widget is an editable */
	if (GTK_IS_EDITABLE(focused)) {
		gtk_editable_delete_selection(GTK_EDITABLE(focused));
	} else {
		bw = tb->get_bw(tb->get_ctx);
		browser_window_key_press(bw, NS_KEY_CLEAR_SELECTION);
	}

	return TRUE;
}


/**
 * handler for select all tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
selectall_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWidget *focused;
	GtkWidget *toplevel;

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	focused = gtk_window_get_focus(GTK_WINDOW(toplevel));

	/* let gtk handle it if focused widget is an editable */
	if (GTK_IS_EDITABLE(focused)) {
		gtk_editable_select_region(GTK_EDITABLE(focused), 0, -1);
	} else {
		bw = tb->get_bw(tb->get_ctx);
		browser_window_key_press(bw, NS_KEY_SELECT_ALL);
	}

	return TRUE;
}


/**
 * handler for preferences tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
preferences_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWidget *toplevel;
	GtkWidget *wndpreferences;

	bw = tb->get_bw(tb->get_ctx);

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);

	wndpreferences = nsgtk_preferences(bw, GTK_WINDOW(toplevel));
	if (wndpreferences != NULL) {
		gtk_widget_show(wndpreferences);
	}

	return TRUE;
}


/**
 * handler for zoom plus tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
zoomplus_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);

	browser_window_set_scale(bw, 0.05, false);

	return TRUE;
}


/**
 * handler for zoom minus tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
zoomminus_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);

	browser_window_set_scale(bw, -0.05, false);

	return TRUE;

}


/**
 * handler for zoom normal tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
zoomnormal_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);

	browser_window_set_scale(bw, 1.0, true);

	return TRUE;
}


/**
 * handler for full screen tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
fullscreen_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	GtkWindow *gtkwindow; /* gtk window widget is in */
	GdkWindow *gdkwindow;
	GdkWindowState state;

	gtkwindow = GTK_WINDOW(gtk_widget_get_ancestor(widget,GTK_TYPE_WINDOW));
	gdkwindow = gtk_widget_get_window(GTK_WIDGET(gtkwindow));
	state = gdk_window_get_state(gdkwindow);

	if (state & GDK_WINDOW_STATE_FULLSCREEN) {
		gtk_window_unfullscreen(gtkwindow);
	} else {
		gtk_window_fullscreen(gtkwindow);
	}
	return TRUE;
}


/**
 * handler for view source tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
viewsource_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	nserror res;
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWindow *gtkwindow; /* gtk window widget is in */

	bw = tb->get_bw(tb->get_ctx);

	gtkwindow = GTK_WINDOW(gtk_widget_get_ancestor(widget,GTK_TYPE_WINDOW));

	res = nsgtk_viewsource(gtkwindow, bw);
	if (res != NSERROR_OK) {
		nsgtk_warning(messages_get_errorcode(res), 0);
	}

	return TRUE;
}


/**
 * handler for show downloads tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
downloads_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	GtkWindow *gtkwindow; /* gtk window widget is in */
	gtkwindow = GTK_WINDOW(gtk_widget_get_ancestor(widget,GTK_TYPE_WINDOW));
	nsgtk_download_show(gtkwindow);
	return TRUE;
}


/**
 * handler for show downloads tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
savewindowsize_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	GtkWindow *gtkwindow; /* gtk window widget is in */
	int x,y,w,h;
	char *choices = NULL;

	gtkwindow = GTK_WINDOW(gtk_widget_get_ancestor(widget,GTK_TYPE_WINDOW));

	gtk_window_get_position(gtkwindow, &x, &y);
	gtk_window_get_size(gtkwindow, &w, &h);

	nsoption_set_int(window_width, w);
	nsoption_set_int(window_height, h);
	nsoption_set_int(window_x, x);
	nsoption_set_int(window_y, y);

	netsurf_mkpath(&choices, NULL, 2, nsgtk_config_home, "Choices");
	if (choices != NULL) {
		nsoption_write(choices, NULL, NULL);
		free(choices);
	}

	return TRUE;
}


/**
 * handler for show downloads tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
toggledebugging_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);

	browser_window_debug(bw, CONTENT_DEBUG_REDRAW);

	nsgtk_window_update_all();

	return TRUE;
}


/**
 * handler for debug box tree tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
debugboxtree_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	gchar *fname;
	gint handle;
	FILE *f;

	handle = g_file_open_tmp("nsgtkboxtreeXXXXXX", &fname, NULL);
	if ((handle == -1) || (fname == NULL)) {
		return TRUE;
	}
	close(handle); /* in case it was binary mode */

	/* save data to temporary file */
	f = fopen(fname, "w");
	if (f == NULL) {
		nsgtk_warning("Error saving box tree dump.",
			      "Unable to open file for writing.");
		unlink(fname);
		return TRUE;
	}

	bw = tb->get_bw(tb->get_ctx);

	browser_window_debug_dump(bw, f, CONTENT_DEBUG_RENDER);

	fclose(f);

	nsgtk_viewfile("Box Tree Debug", "boxtree", fname);

	g_free(fname);

	return TRUE;
}


/**
 * handler for debug dom tree tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
debugdomtree_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	gchar *fname;
	gint handle;
	FILE *f;

	handle = g_file_open_tmp("nsgtkdomtreeXXXXXX", &fname, NULL);
	if ((handle == -1) || (fname == NULL)) {
		return TRUE;
	}
	close(handle); /* in case it was binary mode */

	/* save data to temporary file */
	f = fopen(fname, "w");
	if (f == NULL) {
		nsgtk_warning("Error saving box tree dump.",
			      "Unable to open file for writing.");
		unlink(fname);
		return TRUE;
	}

	bw = tb->get_bw(tb->get_ctx);

	browser_window_debug_dump(bw, f, CONTENT_DEBUG_DOM);

	fclose(f);

	nsgtk_viewfile("DOM Tree Debug", "domtree", fname);

	g_free(fname);

	return TRUE;

}


/**
 * handler for local history tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
localhistory_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	nserror res;
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;
	GtkWidget *toplevel;

	toplevel = gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW);
	if (toplevel != NULL) {
		bw = tb->get_bw(tb->get_ctx);

		res = nsgtk_local_history_present(GTK_WINDOW(toplevel), bw);
		if (res != NSERROR_OK) {
			NSLOG(netsurf, INFO,
			      "Unable to present local history window.");
		}
	}
	return TRUE;
}

/**
 * handler for history tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
history_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	return localhistory_button_clicked_cb(widget, data);
}


/**
 * handler for global history tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
globalhistory_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	nserror res;
	res = nsgtk_global_history_present();
	if (res != NSERROR_OK) {
		NSLOG(netsurf, INFO,
		      "Unable to initialise global history window.");
	}
	return TRUE;
}


/**
 * handler for add bookmark tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
addbookmarks_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct browser_window *bw;

	bw = tb->get_bw(tb->get_ctx);
	if (browser_window_has_content(bw)) {
		hotlist_add_url(browser_window_access_url(bw));
	}
	return TRUE;
}


/**
 * handler for show bookmark tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
showbookmarks_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	nserror res;
	res = nsgtk_hotlist_present();
	if (res != NSERROR_OK) {
		NSLOG(netsurf, INFO, "Unable to initialise bookmark window.");
	}
	return TRUE;
}


/**
 * handler for show cookies tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
showcookies_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	nserror res;
	res = nsgtk_cookies_present();
	if (res != NSERROR_OK) {
		NSLOG(netsurf, INFO, "Unable to initialise cookies window.");
	}
	return TRUE;
}


/**
 * handler for open location tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
openlocation_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	GtkToolItem *urltitem;

	urltitem = tb->items[URL_BAR_ITEM].button;
	if (urltitem != NULL) {
		GtkEntry *entry;
		entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(urltitem)));
		gtk_widget_grab_focus(GTK_WIDGET(entry));
	}
	return TRUE;
}


/**
 * handler for contents tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
contents_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	nserror res;

	res = toolbar_navigate_to_url(tb, "http://www.netsurf-browser.org/documentation/");
	if (res != NSERROR_OK) {
		nsgtk_warning(messages_get_errorcode(res), 0);
	}

	return TRUE;
}

/**
 * handler for contents tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
guide_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	nserror res;

	res = toolbar_navigate_to_url(tb, "http://www.netsurf-browser.org/documentation/guide");
	if (res != NSERROR_OK) {
		nsgtk_warning(messages_get_errorcode(res), 0);
	}

	return TRUE;
}


/**
 * handler for contents tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean
info_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	nserror res;

	res = toolbar_navigate_to_url(tb, "http://www.netsurf-browser.org/documentation/info");
	if (res != NSERROR_OK) {
		nsgtk_warning(messages_get_errorcode(res), 0);
	}

	return TRUE;
}


/**
 * handler for contents tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE
 */
static gboolean about_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	GtkWindow *parent; /* gtk window widget is in */

	parent = GTK_WINDOW(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW));

	nsgtk_about_dialog_init(parent);
	return TRUE;
}

/**
 * handler for openmenu tool bar item clicked signal
 *
 * \param widget The widget the signal is being delivered to.
 * \param data The toolbar context passed when the signal was connected
 * \return TRUE to indicate signal handled.
 */
static gboolean openmenu_button_clicked_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct gui_window *gw;
	struct nsgtk_scaffolding *gs;

	gw = tb->get_ctx; /** \todo stop assuming the context is a gui window */

	gs = nsgtk_get_scaffold(gw);

	nsgtk_scaffolding_burger_menu(gs);

	return TRUE;
}


/* define data plus and data minus handlers */
#define TOOLBAR_ITEM(identifier, name, snstvty, clicked, activate, label, iconame) \
static gboolean								\
nsgtk_toolbar_##name##_data_plus(GtkWidget *widget,			\
				 GdkDragContext *cont,			\
				 GtkSelectionData *selection,		\
				 guint info,				\
				 guint time,				\
				 gpointer data)				\
{									\
	struct nsgtk_toolbar_customisation *tbc;			\
	tbc = (struct nsgtk_toolbar_customisation *)data;		\
	tbc->dragitem = identifier;					\
	tbc->dragfrom = true;						\
	return TRUE;							\
}									\
static gboolean								\
nsgtk_toolbar_##name##_data_minus(GtkWidget *widget,			\
				  GdkDragContext *cont,			\
				  GtkSelectionData *selection,		\
				  guint info,				\
				  guint time,				\
				  gpointer data)			\
{									\
	struct nsgtk_toolbar_customisation *tbc;			\
	tbc = (struct nsgtk_toolbar_customisation *)data;		\
	tbc->dragitem = identifier;					\
	tbc->dragfrom = false;						\
	return TRUE;							\
}

#include "gtk/toolbar_items.h"

#undef TOOLBAR_ITEM


/**
 * create a toolbar item
 *
 * create a toolbar item and set up its default handlers
 */
static nserror
toolbar_item_create(nsgtk_toolbar_button id, struct nsgtk_toolbar_item *item)
{
	item->location = INACTIVE_LOCATION;

	/* set item defaults from macro */
	switch (id) {
#define TOOLBAR_ITEM_t(name)						\
		item->clicked = name##_button_clicked_cb;
#define TOOLBAR_ITEM_b(name)						\
		item->clicked = name##_button_clicked_cb;
#define TOOLBAR_ITEM_y(name)						\
		item->clicked = name##_button_clicked_cb;
#define TOOLBAR_ITEM_n(name)						\
		item->clicked = NULL;
#define TOOLBAR_ITEM(identifier, iname, snstvty, clicked, activate, label, iconame) \
	case identifier:						\
		item->name = #iname;					\
		item->sensitivity = snstvty;				\
		item->dataplus = nsgtk_toolbar_##iname##_data_plus;	\
		item->dataminus = nsgtk_toolbar_##iname##_data_minus;	\
		TOOLBAR_ITEM_ ## clicked(iname)				\
		break;

#include "gtk/toolbar_items.h"

#undef TOOLBAR_ITEM_t
#undef TOOLBAR_ITEM_y
#undef TOOLBAR_ITEM_n
#undef TOOLBAR_ITEM

	case PLACEHOLDER_BUTTON:
		return NSERROR_INVALID;
	}

	return NSERROR_OK;
}


/**
 * set a toolbar item to a throbber frame number
 *
 * \param toolbar_item The toolbar item to update
 * \param frame The animation frame number to update to
 * \return NSERROR_OK on success,
 *         NSERROR_INVALID if the toolbar item does not contain an image,
 *         NSERROR_BAD_SIZE if the frame is out of range.
 */
static nserror set_throbber_frame(GtkToolItem *toolbar_item, int frame)
{
	nserror res;
	GdkPixbuf *pixbuf;
	GtkImage *throbber;

	if (toolbar_item == NULL) {
		/* no toolbar item */
		return NSERROR_INVALID;
	}

	res = nsgtk_throbber_get_frame(frame, &pixbuf);
	if (res != NSERROR_OK) {
		return res;
	}

	throbber = GTK_IMAGE(gtk_bin_get_child(GTK_BIN(toolbar_item)));

	gtk_image_set_from_pixbuf(throbber, pixbuf);

	return NSERROR_OK;
}


/**
 * Make the throbber run.
 *
 * scheduled callback to update the throbber
 *
 * \param p The context passed when scheduled.
 */
static void next_throbber_frame(void *p)
{
	struct nsgtk_toolbar *tb = p;
	nserror res;

	tb->throb_frame++; /* advance to next frame */

	res = set_throbber_frame(tb->items[THROBBER_ITEM].button,
				 tb->throb_frame);
	if (res == NSERROR_BAD_SIZE) {
		tb->throb_frame = 1;
		res = set_throbber_frame(tb->items[THROBBER_ITEM].button,
					 tb->throb_frame);
	}

	/* only schedule next frame if there are no errors */
	if (res == NSERROR_OK) {
		nsgtk_schedule(THROBBER_FRAME_TIME, next_throbber_frame, p);
	}
}


/**
 * connect signal handlers to a gtk toolbar item
 */
static nserror
toolbar_connect_signal(struct nsgtk_toolbar *tb, nsgtk_toolbar_button itemid)
{
	struct nsgtk_toolbar_item *item;
	GtkEntry *entry;

	item = &tb->items[itemid];

	if (item->button != NULL) {
		g_signal_connect(item->button,
				 "size-allocate",
				 G_CALLBACK(toolbar_item_size_allocate_cb),
				 tb);
	}

	switch (itemid) {
	case URL_BAR_ITEM:
		entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(item->button)));

		g_signal_connect(GTK_WIDGET(entry),
				 "activate",
				 G_CALLBACK(url_entry_activate_cb),
				 tb);
		g_signal_connect(GTK_WIDGET(entry),
				 "changed",
				 G_CALLBACK(url_entry_changed_cb),
				 tb);
		g_signal_connect(GTK_WIDGET(entry),
				 "icon-release",
				 G_CALLBACK(url_entry_icon_release_cb),
				 tb);

		nsgtk_completion_connect_signals(entry,
						 tb->get_bw,
						 tb->get_ctx);
		break;


	case WEBSEARCH_ITEM:
		entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(item->button)));

		g_signal_connect(GTK_WIDGET(entry),
				 "activate",
				 G_CALLBACK(websearch_entry_activate_cb),
				 tb);
		g_signal_connect(GTK_WIDGET(entry),
				 "button-press-event",
				 G_CALLBACK(websearch_entry_button_press_cb),
				 tb);
		break;

	default:
		if ((item->clicked != NULL) && (item->button != NULL)) {
			g_signal_connect(item->button,
					 "clicked",
					 G_CALLBACK(item->clicked),
					 tb);
		}
		break;

	}

	return NSERROR_OK;
}

/**
 * connect all signals to widgets in a toolbar
 */
static nserror toolbar_connect_signals(struct nsgtk_toolbar *tb)
{
	int location; /* location index */
	nsgtk_toolbar_button itemid; /* item id */

	for (location = BACK_BUTTON; location < PLACEHOLDER_BUTTON; location++) {
		itemid = itemid_from_location(tb, location);
		if (itemid == PLACEHOLDER_BUTTON) {
			/* no more filled locations */
			break;
		}
		toolbar_connect_signal(tb, itemid);
	}

	return NSERROR_OK;
}


/**
 * signal handler for toolbar context menu
 *
 * \param toolbar The toolbar event is being delivered to
 * \param x The x coordinate where the click happened
 * \param y The x coordinate where the click happened
 * \param button the buttons being pressed
 * \param data The context pointer passed when the connection was made.
 * \return TRUE to indicate signal handled.
 */
static gboolean
toolbar_popup_context_menu_cb(GtkToolbar *toolbar,
			      gint x,
			      gint y,
			      gint button,
			      gpointer data)
{
	struct nsgtk_toolbar *tb = (struct nsgtk_toolbar *)data;
	struct gui_window *gw;
	struct nsgtk_scaffolding *gs;

	gw = tb->get_ctx; /** \todo stop assuming the context is a gui window */

	gs = nsgtk_get_scaffold(gw);

	nsgtk_scaffolding_toolbar_context_menu(gs);

	return TRUE;
}


/**
 * toolbar delete signal handler
 */
static void toolbar_destroy_cb(GtkWidget *widget, gpointer data)
{
	struct nsgtk_toolbar *tb;
	tb = (struct nsgtk_toolbar *)data;

	/* ensure any throbber scheduled is stopped */
	nsgtk_schedule(-1, next_throbber_frame, tb);

	free(tb);
}


/* exported interface documented in toolbar.h */
nserror
nsgtk_toolbar_create(GtkBuilder *builder,
		     struct browser_window *(*get_bw)(void *ctx),
		     void *get_ctx,
		     bool want_location_focus,
		     struct nsgtk_toolbar **tb_out)
{
	nserror res;
	struct nsgtk_toolbar *tb;
	int bidx; /* button index */

	tb = calloc(1, sizeof(struct nsgtk_toolbar));
	if (tb == NULL) {
		return NSERROR_NOMEM;
	}

	tb->get_bw = get_bw;
	tb->get_ctx = get_ctx;
	/* set the throbber start frame. */
	tb->throb_frame = 0;
	if (want_location_focus) {
		tb->loc_focus = LFS_WANT;
	} else {
		tb->loc_focus = LFS_IDLE;
	}

	tb->widget = GTK_TOOLBAR(gtk_builder_get_object(builder, "toolbar"));
	gtk_toolbar_set_show_arrow(tb->widget, TRUE);

	g_signal_connect(tb->widget,
			 "popup-context-menu",
			 G_CALLBACK(toolbar_popup_context_menu_cb),
			 tb);

	/* close and cleanup on delete signal */
	g_signal_connect(tb->widget,
			 "destroy",
			 G_CALLBACK(toolbar_destroy_cb),
			 tb);

	/* allocate button contexts */
	for (bidx = BACK_BUTTON; bidx < PLACEHOLDER_BUTTON; bidx++) {
		res = toolbar_item_create(bidx, &tb->items[bidx]);
		if (res != NSERROR_OK) {
			return res;
		}
	}

	res = nsgtk_toolbar_update(tb);
	if (res != NSERROR_OK) {
		return res;
	}

	*tb_out = tb;
	return NSERROR_OK;
}


/* exported interface documented in toolbar.h */
nserror nsgtk_toolbar_restyle(struct nsgtk_toolbar *tb)
{
	/*
	 * reset toolbar size allocation so icon size change affects
	 *  allocated widths.
	 */
	tb->offset = 0;

	switch (nsoption_int(button_type)) {

	case 1: /* Small icons */
		gtk_toolbar_set_style(GTK_TOOLBAR(tb->widget),
				      GTK_TOOLBAR_ICONS);
		gtk_toolbar_set_icon_size(GTK_TOOLBAR(tb->widget),
					  GTK_ICON_SIZE_SMALL_TOOLBAR);
		break;

	case 2: /* Large icons */
		gtk_toolbar_set_style(GTK_TOOLBAR(tb->widget),
				      GTK_TOOLBAR_ICONS);
		gtk_toolbar_set_icon_size(GTK_TOOLBAR(tb->widget),
					  GTK_ICON_SIZE_LARGE_TOOLBAR);
		break;

	case 3: /* Large icons with text */
		gtk_toolbar_set_style(GTK_TOOLBAR(tb->widget),
				      GTK_TOOLBAR_BOTH);
		gtk_toolbar_set_icon_size(GTK_TOOLBAR(tb->widget),
					  GTK_ICON_SIZE_LARGE_TOOLBAR);
		break;

	case 4: /* Text icons only */
		gtk_toolbar_set_style(GTK_TOOLBAR(tb->widget),
				      GTK_TOOLBAR_TEXT);
		break;

	default:
		break;
	}

	return NSERROR_OK;
}


/* exported interface documented in toolbar.h */
nserror nsgtk_toolbar_throbber(struct nsgtk_toolbar *tb, bool active)
{
	nserror res;
	struct browser_window *bw;

	/* Manage the location focus state */
	switch (tb->loc_focus) {
	case LFS_IDLE:
		break;
	case LFS_WANT:
		if (active) {
			tb->loc_focus = LFS_THROB;
		}
		break;
	case LFS_THROB:
		if (!active) {
			tb->loc_focus = LFS_LAST;
		}
		break;
	case LFS_LAST:
		break;
	}

	/* when activating the throbber simply schedule the next frame update */
	if (active) {
		nsgtk_schedule(THROBBER_FRAME_TIME, next_throbber_frame, tb);

		set_item_sensitivity(&tb->items[STOP_BUTTON], true);
		set_item_sensitivity(&tb->items[RELOAD_BUTTON], false);
		set_item_action(tb, RELOADSTOP_BUTTON, false);

		return NSERROR_OK;
	}

	/* stopping the throbber */
	nsgtk_schedule(-1, next_throbber_frame, tb);
	tb->throb_frame = 0;
	res =  set_throbber_frame(tb->items[THROBBER_ITEM].button,
				  tb->throb_frame);

	bw = tb->get_bw(tb->get_ctx);

	/* adjust sensitivity of other items */
	set_item_sensitivity(&tb->items[STOP_BUTTON], false);
	set_item_sensitivity(&tb->items[RELOAD_BUTTON], true);
	set_item_action(tb, RELOADSTOP_BUTTON, true);
	set_item_sensitivity(&tb->items[BACK_BUTTON],
			     browser_window_history_back_available(bw));
	set_item_sensitivity(&tb->items[FORWARD_BUTTON],
			     browser_window_history_forward_available(bw));
	nsgtk_local_history_hide();

	/* update the url bar, for the final time */
	browser_window_refresh_url_bar(bw);

	return res;
}


/* exported interface documented in toolbar.h */
nserror nsgtk_toolbar_page_info_change(struct nsgtk_toolbar *tb)
{
	GtkEntry *url_entry;
	browser_window_page_info_state pistate;
	struct browser_window *bw;
	const char *icon_name;

	if (tb->items[URL_BAR_ITEM].button == NULL) {
		/* no toolbar item */
		return NSERROR_INVALID;
	}
	url_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(tb->items[URL_BAR_ITEM].button)));

	bw = tb->get_bw(tb->get_ctx);

	pistate = browser_window_get_page_info_state(bw);

	switch (pistate) {
	case PAGE_STATE_INTERNAL:
		icon_name = "page-info-internal";
		break;

	case PAGE_STATE_LOCAL:
		icon_name = "page-info-local";
		break;

	case PAGE_STATE_INSECURE:
		icon_name = "page-info-insecure";
		break;

	case PAGE_STATE_SECURE_OVERRIDE:
		icon_name = "page-info-warning";
		break;

	case PAGE_STATE_SECURE_ISSUES:
		icon_name = "page-info-warning";
		break;

	case PAGE_STATE_SECURE:
		icon_name = "page-info-secure";
		break;

	default:
		icon_name = "page-info-internal";
		break;
	}

	nsgtk_entry_set_icon_from_icon_name(GTK_WIDGET(url_entry),
					    GTK_ENTRY_ICON_PRIMARY,
					    icon_name);
	return NSERROR_OK;
}


/* exported interface documented in toolbar.h */
nserror nsgtk_toolbar_set_url(struct nsgtk_toolbar *tb, nsurl *url)
{
	size_t idn_url_l;
	char *idn_url_s = NULL;
	const char *url_text = NULL;
	GtkEntry *url_entry;

	if (tb->items[URL_BAR_ITEM].button == NULL) {
		/* no toolbar item */
		return NSERROR_INVALID;
	}
	url_entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(tb->items[URL_BAR_ITEM].button)));

	if (nsoption_bool(display_decoded_idn) == true) {
		if (nsurl_get_utf8(url, &idn_url_s, &idn_url_l) != NSERROR_OK) {
			idn_url_s = NULL;
		}
		url_text = idn_url_s;
	}
	if (url_text == NULL) {
		url_text = nsurl_access(url);
	}

	if (strcmp(url_text, gtk_entry_get_text(url_entry)) != 0) {
		/* The URL bar content has changed, we need to update it */
		gint startpos, endpos;
		bool was_selected;
		gtk_editable_get_selection_bounds(GTK_EDITABLE(url_entry),
						  &startpos, &endpos);
		was_selected = gtk_widget_is_focus(GTK_WIDGET(url_entry)) &&
			startpos == 0 &&
			endpos == gtk_entry_get_text_length(url_entry);
		gtk_entry_set_text(url_entry, url_text);
		if (was_selected && tb->loc_focus != LFS_IDLE) {
			gtk_widget_grab_focus(GTK_WIDGET(url_entry));
			if (tb->loc_focus == LFS_LAST) {
				tb->loc_focus = LFS_IDLE;
			}
		}
	}

	if (idn_url_s != NULL) {
		free(idn_url_s);
	}

	return NSERROR_OK;
}


/* exported interface documented in toolbar.h */
nserror
nsgtk_toolbar_set_websearch_image(struct nsgtk_toolbar *tb, GdkPixbuf *pixbuf)
{
	GtkWidget *entry;

	if (tb->items[WEBSEARCH_ITEM].button == NULL) {
		/* no toolbar item */
		return NSERROR_INVALID;
	}

	entry = gtk_bin_get_child(GTK_BIN(tb->items[WEBSEARCH_ITEM].button));

	if (pixbuf != NULL) {
		nsgtk_entry_set_icon_from_pixbuf(entry,
						 GTK_ENTRY_ICON_PRIMARY,
						 pixbuf);
	} else {
		nsgtk_entry_set_icon_from_icon_name(entry,
						    GTK_ENTRY_ICON_PRIMARY,
						    NSGTK_STOCK_INFO);
	}

	return NSERROR_OK;
}


/* exported interface documented in toolbar.h */
nserror
nsgtk_toolbar_item_activate(struct nsgtk_toolbar *tb,
			    nsgtk_toolbar_button itemid)
{
	GtkWidget *widget;

	/* ensure item id in range */
	if ((itemid < BACK_BUTTON) || (itemid >= PLACEHOLDER_BUTTON)) {
		return NSERROR_BAD_PARAMETER;
	}

	if (tb->items[itemid].clicked == NULL) {
		return NSERROR_INVALID;
	}

	/*
	 * if item has a widget in the current toolbar use that as the
	 *   signal source otherwise use the toolbar widget itself.
	 */
	if (tb->items[itemid].button != NULL) {
		widget = GTK_WIDGET(tb->items[itemid].button);
	} else {
		widget = GTK_WIDGET(tb->widget);
	}

	tb->items[itemid].clicked(widget, tb);

	return NSERROR_OK;
}


/* exported interface documented in toolbar.h */
nserror nsgtk_toolbar_show(struct nsgtk_toolbar *tb, bool show)
{
	if (show) {
		gtk_widget_show(GTK_WIDGET(tb->widget));
	} else {
		gtk_widget_hide(GTK_WIDGET(tb->widget));
	}
	return NSERROR_OK;
}


/* exported interface documented in toolbar.h */
nserror nsgtk_toolbar_update(struct nsgtk_toolbar *tb)
{
	nserror res;

	/* setup item locations based on user config */
	res = apply_user_button_customisation(tb);
	if (res != NSERROR_OK) {
		return res;
	}

	/* populate toolbar widget */
	res = populate_gtk_toolbar_widget(tb);
	if (res != NSERROR_OK) {
		return res;
	}

	/* ensure icon sizes and text labels on toolbar are set */
	res = nsgtk_toolbar_restyle(tb);
	if (res != NSERROR_OK) {
		return res;
	}

	res = toolbar_connect_signals(tb);

	return res;
}