File: blxwindow.cpp

package info (click to toggle)
seqtools 4.44.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,492 kB
  • sloc: cpp: 53,636; sh: 12,232; makefile: 386
file content (6863 lines) | stat: -rwxr-xr-x 265,716 bytes parent folder | download | duplicates (4)
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
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
/*  File: blxWindow.c
 *  Author: Gemma Barson, 2009-11-24
 *  Copyright (c) 2009 - 2012 Genome Research Ltd
 * ---------------------------------------------------------------------------
 * SeqTools 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; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
 * ---------------------------------------------------------------------------
 * This file is part of the SeqTools sequence analysis package, 
 * written by
 *      Gemma Barson      (Sanger Institute, UK)  <gb10@sanger.ac.uk>
 * 
 * based on original code by
 *      Erik Sonnhammer   (SBC, Sweden)           <Erik.Sonnhammer@sbc.su.se>
 * 
 * and utilizing code taken from the AceDB and ZMap packages, written by
 *      Richard Durbin    (Sanger Institute, UK)  <rd@sanger.ac.uk>
 *      Jean Thierry-Mieg (CRBM du CNRS, France)  <mieg@kaa.crbm.cnrs-mop.fr>
 *      Ed Griffiths      (Sanger Institute, UK)  <edgrif@sanger.ac.uk>
 *      Roy Storey        (Sanger Institute, UK)  <rds@sanger.ac.uk>
 *      Malcolm Hinsley   (Sanger Institute, UK)  <mh17@sanger.ac.uk>
 *
 * Description: See blxWindow.h
 *----------------------------------------------------------------------------
 */

#include <blixemApp/blxwindow.hpp>
#include <blixemApp/blxcontext.hpp>
#include <blixemApp/detailview.hpp>
#include <blixemApp/detailviewtree.hpp>
#include <blixemApp/bigpicture.hpp>
#include <blixemApp/blxdotter.hpp>
#include <blixemApp/exonview.hpp>
#include <blixemApp/coverageview.hpp>
#include <blixemApp/blxpanel.hpp>
#include <seqtoolsUtils/utilities.hpp>
#include <seqtoolsUtils/blxGff3Parser.hpp>
#include <seqtoolsUtils/blxmsp.hpp>
#include <gbtools/gbtools.hpp>
#include <gdk/gdkkeysyms.h>
#include <string.h>
#include <ctype.h>
#include <algorithm>
#include <sstream>

using namespace std;


#define DEFAULT_WINDOW_BORDER_WIDTH      1    /* used to change the default border width around the blixem window */
#define DEFAULT_COVERAGE_VIEW_BORDER     12   /* size of border to allow around the coverage view */
#define DEFAULT_FONT_SIZE_ADJUSTMENT     -2   /* used to start with a smaller font than the default widget font */
#define DEFAULT_SCROLL_STEP_INCREMENT    5    /* how many bases the scrollbar scrolls by for each increment */
#define DEFAULT_WINDOW_WIDTH_FRACTION    0.9  /* what fraction of the screen size the blixem window width defaults to */
#define DEFAULT_WINDOW_HEIGHT_FRACTION   0.6  /* what fraction of the screen size the blixem window height defaults to */
#define LOAD_DATA_TEXT                   "Load optional\ndata"
#define DEFAULT_TABLE_XPAD               2    /* default x-padding to use in tables */
#define DEFAULT_TABLE_YPAD               2    /* default y-padding to use in tables */
#define MAX_RECOMMENDED_COPY_LENGTH      100000 /* warn if about to copy text longer than this to the clipboard */


typedef enum {SORT_TYPE_COL, SORT_TEXT_COL, N_SORT_COLUMNS} SortColumns;


/* Utility struct used when comparing sequences to a search string */
typedef struct _CompareSeqData
  {
    const char *searchStr;    /* the string to search for */
    BlxColumnId searchCol;    /* the column ID, which defines what data to search e.g. Name or Tissue Type */
    BlxContext *bc;       /* the main context */
    GList *matchList;         /* resulting list of all BlxSequences that match */
    GError *error;
  } SeqSearchData;


/* Properties specific to the blixem window */
class BlxWindowProperties
{
public:
  GtkWidget *widget;
  GtkWidget *bigPicture;          /* The top section of the view, showing a "big picture" overview of the alignments */
  GtkWidget *detailView;          /* The bottom section of the view, showing a detailed list of the alignments */
  GtkWidget *mainmenu;            /* The main menu */
  GtkWidget *seqHeaderMenu;      /* The context menu for tree headers */
  GtkActionGroup *actionGroup;    /* The action-group for the menus */

  BlxContext *blxContext;       /* The blixem view context */

  GtkPageSetup *pageSetup;          /* Page setup for printing */
  GtkPrintSettings *printSettings;  /* Used so that we can re-use the same print settings as a previous print */
};


/* Local function declarations */
static BlxWindowProperties*       blxWindowGetProperties(GtkWidget *widget);

static void                       onHelpMenu(GtkAction *action, gpointer data);
static void                       onAboutMenu(GtkAction *action, gpointer data);
static void                       onQuit(GtkAction *action, gpointer data);
static void                       onPrintMenu(GtkAction *action, gpointer data);
static void                       onPageSetupMenu(GtkAction *action, gpointer data);
static void                       onSettingsMenu(GtkAction *action, gpointer data);
static void                       onLoadMenu(GtkAction *action, gpointer data);
static void                       onCopySeqsMenu(GtkAction *action, gpointer data);
static void                       onCopySeqDataMenu(GtkAction *action, gpointer data);
static void                       onCopySeqDataMarkMenu(GtkAction *action, gpointer data);
static void                       onCopyRefSeqDnaMenu(GtkAction *action, gpointer data);
static void                       onCopyRefSeqDisplayMenu(GtkAction *action, gpointer data);
static void                       onSortMenu(GtkAction *action, gpointer data);
static void                       onZoomInMenu(GtkAction *action, gpointer data);
static void                       onZoomOutMenu(GtkAction *action, gpointer data);
static void                       onFindMenu(GtkAction *action, gpointer data);
static void                       onGoToMenu(GtkAction *action, gpointer data);
static void                       onPrevMatchMenu(GtkAction *action, gpointer data);
static void                       onNextMatchMenu(GtkAction *action, gpointer data);
static void                       onFirstMatchMenu(GtkAction *action, gpointer data);
static void                       onLastMatchMenu(GtkAction *action, gpointer data);
static void                       onPageLeftMenu(GtkAction *action, gpointer data);
static void                       onPageRightMenu(GtkAction *action, gpointer data);
static void                       onScrollLeft1Menu(GtkAction *action, gpointer data);
static void                       onScrollRight1Menu(GtkAction *action, gpointer data);
static void                       onSquashMatchesMenu(GtkAction *action, gpointer data);
static void                       onToggleStrandMenu(GtkAction *action, gpointer data);
static void                       onViewMenu(GtkAction *action, gpointer data);
static void                       onCreateGroupMenu(GtkAction *action, gpointer data);
static void                       onEditGroupsMenu(GtkAction *action, gpointer data);
static void                       onCreateQuickGroup(GtkAction *action, gpointer data);
static void                       onCreateQuickFilter(GtkAction *action, gpointer data);
static void                       onClearGroups(GtkAction *action, gpointer data);
static void                       onHideSources(GtkAction *action, gpointer data);
static void                       onDotterMenu(GtkAction *action, gpointer data);
static void                       onCloseAllDottersMenu(GtkAction *action, gpointer data);
static void                       onSelectFeaturesMenu(GtkAction *action, gpointer data);
static void                       onDeselectAllRows(GtkAction *action, gpointer data);
static void                       onStatisticsMenu(GtkAction *action, gpointer data);

static gboolean                   onKeyPressBlxWindow(GtkWidget *window, GdkEventKey *event, gpointer data);
static void                       onUpdateBackgroundColor(GtkWidget *blxWindow);

static void                       onDestroyBlxWindow(GtkWidget *widget);

static BlxStrand                  blxWindowGetInactiveStrand(GtkWidget *blxWindow);

static GtkComboBox*               widgetGetComboBox(GtkWidget *widget);
static BlxColumnId                getColumnFromComboBox(GtkComboBox *combo);

static void                       onButtonClickedDeleteGroup(GtkWidget *button, gpointer data);
static void                       blxWindowGroupsChanged(GtkWidget *blxWindow);
static void                       getSequencesThatMatch(gpointer listDataItem, gpointer data);
static GList*                     getSeqStructsFromText(GtkWidget *blxWindow, const char *inputText, const BlxColumnId searchCol, GError **error);

static void                       createSortBox(GtkBox *parent, GtkWidget *detailView, const BlxColumnId initSortColumn, GList *columnList, const char *labelText, const gboolean searchableOnly);
static GtkWidget*                 createCheckButton(GtkBox *box, const char *mnemonic, const char *tooltip, const gboolean isActive, GCallback callback, gpointer data);
static void                       blxWindowSetUsePrintColors(GtkWidget *blxWindow, const gboolean usePrintColors);
static gboolean                   blxWindowGetUsePrintColors(GtkWidget *blxWindow);

static void                       blxWindowFindDnaString(GtkWidget *blxWindow, const char *inputSearchStr, const int startCoord, const gboolean searchLeft, const gboolean findAgain, GError **error);
static GList*                     findSeqsFromList(GtkWidget *blxWindow, const char *inputText, const BlxColumnId inputCol, const gboolean rememberSearch, const gboolean findAgain, GError **error);
static int                        getSearchStartCoord(GtkWidget *blxWindow, const gboolean startBeginning, const gboolean searchLeft);
static GList*                     findSeqsFromColumn(GtkWidget *blxWindow, const char *inputText, const BlxColumnId searchCol, const gboolean rememberSearch, const gboolean findAgain, GError **error);
static GtkWidget*                 dialogChildGetBlxWindow(GtkWidget *child);
static gdouble                    calculateMspData(MSP *mspList, BlxContext *bc);

static gboolean                   setFlagFromButton(GtkWidget *button, gpointer data);
static void                       copySelectedSeqDataToClipboard(GtkWidget *blxWindow);
static void                       copySelectedSeqRangeToClipboard(GtkWidget *blxWindow, const int fromIdx, const int toIdx);
static void                       copyRefSeqToClipboard(GtkWidget *blxWindow, const int fromIdx_in, const int toIdx_in);
static void                       copyRefSeqTranslationToClipboard(GtkWidget *blxWindow, const int fromIdx_in, const int toIdx_in);

static void                       saveBlixemSettings(GtkWidget *blxWindow);


/* MENU BUILDERS */

/* Standard menu entries */
static const GtkActionEntry mainMenuEntries[] = {
  { "CopyMenuAction",   NULL, "Copy"},
  { "GroupMenuAction",  NULL, "Group/Filter"},

  { "Quit",             GTK_STOCK_QUIT,           "_Quit",                    "<control>Q",         "Quit  Ctrl+Q",                         G_CALLBACK(onQuit)},
  { "Help",             GTK_STOCK_HELP,           "_Help",                    "<control>H",         "Display help  Ctrl+H",                 G_CALLBACK(onHelpMenu)},
  { "About",            GTK_STOCK_ABOUT,          "About",                    NULL,                 "Program information",                  G_CALLBACK(onAboutMenu)},
  { "Print",            GTK_STOCK_PRINT,          "_Print...",                "<control>P",         "Print  Ctrl+P",                        G_CALLBACK(onPrintMenu)},
  { "PageSetup",        GTK_STOCK_PAGE_SETUP,     "Page set_up...",           NULL,                 "Page setup",                           G_CALLBACK(onPageSetupMenu)},
  { "Settings",         GTK_STOCK_PREFERENCES,    "_Settings...",             "<control>S",         "Settings  Ctrl+S",                     G_CALLBACK(onSettingsMenu)},
  { "Load",             GTK_STOCK_OPEN,           "_Open features file...",    NULL,                "Load additional features from file  Ctrl+O", G_CALLBACK(onLoadMenu)},

  { "CopySeqNames",     NULL,                     "Copy match name(s)",       "<control>C",         "Copy selected match sequence's name(s)  Ctrl+C", G_CALLBACK(onCopySeqsMenu)},
  { "CopySeqData",      NULL,                     "Copy match sequence (entire sequence)",  NULL,   "Copy whole sequence for selected match",         G_CALLBACK(onCopySeqDataMenu)},
  { "CopySeqDataMark",  NULL,                     "Copy match sequence (selected section)","<shift><control>C","Copy selected match sequence segment  Shift+Ctrl+C", G_CALLBACK(onCopySeqDataMarkMenu)},
  { "CopyRefSeqDna",    NULL,                     "Copy reference DNA",       "<alt>C",             "Copy selected reference sequence DNA  Alt+C", G_CALLBACK(onCopyRefSeqDnaMenu)},
  { "CopyRefSeqDisplay",NULL,                     "Copy reference translation (current frame)","<shift><alt>C","Copy selected reference sequence translation  Shift+Alt+C", G_CALLBACK(onCopyRefSeqDisplayMenu)},

  { "Sort",             GTK_STOCK_SORT_ASCENDING, "Sort...",                  NULL,                 "Sort sequences",                       G_CALLBACK(onSortMenu)},
  { "ZoomIn",           GTK_STOCK_ZOOM_IN,        "Zoom in",                  "equal",              "Zoom in  =",                           G_CALLBACK(onZoomInMenu)},
  { "ZoomOut",          GTK_STOCK_ZOOM_OUT,       "Zoom out",                 "minus",              "Zoom out  -",                          G_CALLBACK(onZoomOutMenu)},
  { "GoTo",             GTK_STOCK_JUMP_TO,        "Go to position...",        "P",                  "Go to position  P",                    G_CALLBACK(onGoToMenu)},
  { "FirstMatch",       GTK_STOCK_GOTO_FIRST,     "First match",              "<control>Home",      "Go to first match in selection (or all, if none selected)  Ctrl+Home",    G_CALLBACK(onFirstMatchMenu)},
  { "PrevMatch",        GTK_STOCK_GO_BACK,        "Previous match",           "<control>Left",      "Go to previous match in selection (or all, if none selected)  Ctrl+Left", G_CALLBACK(onPrevMatchMenu)},
  { "NextMatch",        GTK_STOCK_GO_FORWARD,     "Next match",               "<control>Right",     "Go to next match in selection (or all, if none selected)  Ctrl+Right",    G_CALLBACK(onNextMatchMenu)},
  { "LastMatch",        GTK_STOCK_GOTO_LAST,      "Last match",               "<control>End",       "Go to last match in selection (or all, if none selected)  Ctrl+End",      G_CALLBACK(onLastMatchMenu)},
  { "BackPage",         NULL,                     "<<",                       "<control>comma",     "Scroll left one page  Ctrl+,",         G_CALLBACK(onPageLeftMenu)},
  { "BackOne",          NULL,                     "<",                        "comma",              "Scroll left one index  ,",             G_CALLBACK(onScrollLeft1Menu)},
  { "FwdOne",           NULL,                     ">",                        "period",             "Scroll right one index  .",            G_CALLBACK(onScrollRight1Menu)},
  { "FwdPage",          NULL,                     ">>",                       "<control>period",    "Scroll right one page  Ctrl+.",        G_CALLBACK(onPageRightMenu)},
  { "Find",             GTK_STOCK_FIND,           "Find...",                  "<control>F",         "Find sequences  Ctrl+F",               G_CALLBACK(onFindMenu)},
  { "ToggleStrand",     GTK_STOCK_REFRESH,        "Toggle strand",            "T",                  "Toggle the active strand  T",          G_CALLBACK(onToggleStrandMenu)},

  { "View",             GTK_STOCK_FULLSCREEN,     "_View...",                 "V",                  "Edit view settings  V",                G_CALLBACK(onViewMenu)},
  { "CreateGroup",      NULL,                     "Create Custom Group...",   "<shift><control>G",  "Create group  Shift+Ctrl+G",           G_CALLBACK(onCreateGroupMenu)},
  { "EditGroups",       GTK_STOCK_EDIT,           "Edit _Groups...",          "<control>G",         "Edit groups  Ctrl+G",                  G_CALLBACK(onEditGroupsMenu)},
  { "CreateQuickGroup", NULL,                     "Create group from clipboard", "G",               "Create a group based on features on the clipboard (clears existing group; hold shift to add to it)  G",  G_CALLBACK(onCreateQuickGroup)},
  { "CreateQuickFilter",NULL,                     "Create filter from clipboard","F",               "Create a filter based on features on the clipboard (clears existing filter; hold shift to add to it)  F",  G_CALLBACK(onCreateQuickFilter)},
  { "HideSources",      NULL,                     "Hide source(s)",           "H",                  "Hide the selected source(s)  H",        G_CALLBACK(onHideSources)},
  { "ClearGroups",      NULL,                     "Clear groups/filters",     "C",                  "Disable all groups/filters (you can re-enable them from the Groups dialog)  C",        G_CALLBACK(onClearGroups)},
  { "DeselectAllRows",  NULL,                     "Deselect _all",            "<shift><control>A",  "Deselect all  Shift+Ctrl+A",           G_CALLBACK(onDeselectAllRows)},

  { "Dotter",           NULL,                     "_Dotter...",               "<control>D",         "Start Dotter  Ctrl+D",                 G_CALLBACK(onDotterMenu)},
  { "CloseAllDotters",  GTK_STOCK_CLOSE,          "Close all Dotters",        NULL,                 "Close all Dotters",                    G_CALLBACK(onCloseAllDottersMenu)},
  { "SelectFeatures",   GTK_STOCK_SELECT_ALL,     "Feature series selection tool...",  NULL,           "Feature series selection tool",        G_CALLBACK(onSelectFeaturesMenu)},

  { "Statistics",       NULL,                     "Statistics",               NULL,                 "Show memory statistics",               G_CALLBACK(onStatisticsMenu)}
};


/* Menu entries for toggle-able actions */
static GtkToggleActionEntry toggleMenuEntries[] = {
  { "SquashMatches",    GTK_STOCK_DND_MULTIPLE,  "Squash matches",           NULL,                 "Squash matches", G_CALLBACK(onSquashMatchesMenu), FALSE} /* must be item 0 in list */
};


/* This defines the layout of the menu for a standard user */
static const char standardMenuDescription[] =
"<ui>"
"  <popup name='ContextMenu' accelerators='true'>"
"      <menuitem action='Quit'/>"
"      <menuitem action='Help'/>"
"      <menuitem action='Print'/>"
//"      <menuitem action='PageSetup'/>"
"      <menuitem action='Settings'/>"
"      <menuitem action='Load'/>"
"      <separator/>"
"      <menu action='CopyMenuAction'>"
"        <menuitem action='CopySeqNames'/>"
"        <menuitem action='CopySeqData'/>"
"        <menuitem action='CopySeqDataMark'/>"
"        <menuitem action='CopyRefSeqDna'/>"
"        <menuitem action='CopyRefSeqDisplay'/>"
"      </menu>"
"      <menuitem action='View'/>"
"      <menu action='GroupMenuAction'>"
"        <menuitem action='CreateQuickGroup'/>"
"        <menuitem action='CreateQuickFilter'/>"
"        <menuitem action='HideSources'/>"
"        <menuitem action='ClearGroups'/>"
"        <menuitem action='CreateGroup'/>"
"        <menuitem action='EditGroups'/>"
"      </menu>"
"      <menuitem action='DeselectAllRows'/>"
"      <separator/>"
"      <menuitem action='Dotter'/>"
"      <menuitem action='CloseAllDotters'/>"
"  </popup>"
"  <popup name='SeqHeaderContextMenu' accelerators='true'>"
"      <menuitem action='CopyRefSeqDna'/>"
"      <menuitem action='CopyRefSeqDisplay'/>"
"  </popup>"
"  <toolbar name='Toolbar'>"
"    <toolitem action='Help'/>"
"    <toolitem action='About'/>"
"    <toolitem action='Settings'/>"
"    <separator/>"
"    <toolitem action='Sort'/>"
"    <toolitem action='SquashMatches'/>"
"    <toolitem action='ZoomIn'/>"
"    <toolitem action='ZoomOut'/>"
"    <separator/>"
"    <toolitem action='GoTo'/>"
"    <toolitem action='FirstMatch'/>"
"    <toolitem action='PrevMatch'/>"
"    <toolitem action='NextMatch'/>"
"    <toolitem action='LastMatch'/>"
"    <toolitem action='BackPage'/>"
"    <toolitem action='BackOne'/>"
"    <toolitem action='FwdOne'/>"
"    <toolitem action='FwdPage'/>"
"    <separator/>"
"    <toolitem action='Find'/>"
"    <toolitem action='ToggleStrand'/>"
"  </toolbar>"
"</ui>";


/* This defines the additional menu components for a developer user */
static const char developerMenuDescription[] =
"<ui>"
"  <popup name='ContextMenu' accelerators='true'>"
"      <menuitem action='SelectFeatures'/>"
"      <separator/>"
"      <menuitem action='Statistics'/>"
"  </popup>"
"</ui>";



/***********************************************************
 *                         Utilities                       *
 ***********************************************************/

/* Return true if the current user is in our list of developers. */
static gboolean userIsDeveloper()
{
  const gchar* developers[] = {"edgrif", "gb10"};

  gboolean result = FALSE;
  const gchar *user = g_get_user_name();
  int numDevelopers = sizeof(developers) / sizeof(gchar*);
  
  int i = 0;
  for (i = 0; i < numDevelopers; ++i)
    {
      if (strcmp(user, developers[i]) == 0)
        {
          result = TRUE;
          break;
        }
    }

  return result;
}


/* Returns the order number of the group that this sequence belongs to, or
 * UNSET_INT if it does not belong to a group. */
int sequenceGetGroupOrder(GtkWidget *blxWindow, const BlxSequence *seq)
{
  SequenceGroup *group = blxWindowGetSequenceGroup(blxWindow, seq);
  return group ? group->order : UNSET_INT;
}


/* Scroll the detail view left/right by 1 base (or by 1 page, if the modifier
 * is pressed) */
static void scrollDetailView(GtkWidget *window, const gboolean moveLeft, const gboolean modifier)
{
  GtkWidget *detailView = blxWindowGetDetailView(window);
  
  if (moveLeft && modifier)
    scrollDetailViewLeftPage(detailView);
  else if (moveLeft)
    scrollDetailViewLeft1(detailView);
  else if (modifier)
    scrollDetailViewRightPage(detailView);
  else
    scrollDetailViewRight1(detailView);
}


/* Move the current row selection up/down */
static gboolean moveRowSelection(GtkWidget *blxWindow, const gboolean moveUp, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  const int activeFrame = detailViewGetActiveFrame(detailView);
  const BlxStrand activeStrand = detailViewGetSelectedStrand(detailView);
  
  GtkWidget *tree = detailViewGetTree(detailView, activeStrand, activeFrame);
  return treeMoveRowSelection(tree, moveUp, shiftModifier);
}


/* Move the selected base index 1 base to the left/right. Moves by individual
 * DNA bases (i.e. you have to move 3 bases in order to scroll a full peptide
 * if viewing protein matches). Scrolls the detail view if necessary to keep 
 * the new base in view. */
static void moveSelectedBaseIdxBy1(GtkWidget *window, const gboolean moveLeft, const gboolean extend)
{
  GtkWidget *detailView = blxWindowGetDetailView(window);
  DetailViewProperties *properties = detailViewGetProperties(detailView);

  const gboolean displayRev = detailViewGetDisplayRev(detailView);
  const int direction = (moveLeft == displayRev ? 1 : -1);
  
  int newDnaIdx = UNSET_INT;
  gboolean ok = FALSE;

  if (detailViewGetSelectedIdxSet(detailView))
    {
      newDnaIdx = properties->selectedIndex->dnaIdx + direction;
      ok = TRUE;
    }
  
  if (ok)
    {
      detailViewSetSelectedDnaBaseIdx(detailView, 
                                      newDnaIdx, 
                                      detailViewGetActiveFrame(detailView),
                                      TRUE, 
                                      TRUE,
                                      extend);
    }
}


/* Called when user pressed Home/End. If the modifier is pressed, scroll to the
*  start/end of all matches in the current selection (or all matches, if no 
* selection), or to the start/end of the entire display if the modifier is not pressed. */
static void scrollToExtremity(GtkWidget *blxWindow, const gboolean moveLeft, const gboolean modifier, const gboolean extend)
{
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  
  if (modifier)
    {
      GList *selectedSeqs = blxWindowGetSelectedSeqs(blxWindow);

      if (moveLeft)
        firstMatch(detailView, selectedSeqs, extend);
      else
        lastMatch(detailView, selectedSeqs, extend);
    }
  else
    {
      const BlxSeqType seqType = blxWindowGetSeqType(blxWindow);
      const IntRange* const fullRange = blxWindowGetFullRange(blxWindow);

      if (moveLeft)
        setDetailViewStartIdx(detailView, fullRange->min(), seqType);
      else
        setDetailViewEndIdx(detailView, fullRange->max(), seqType);
    }
}


/* Jump left or right to the next/prev nearest match. Only include matches in the
 * current selection, if any rows are selected. */
static void goToMatch(GtkWidget *blxWindow, const gboolean moveLeft, const gboolean extend)
{
  GList *selectedSeqs = blxWindowGetSelectedSeqs(blxWindow);
  
  if (moveLeft)
    {
      prevMatch(blxWindowGetDetailView(blxWindow), selectedSeqs, extend);
    }
  else
    {
      nextMatch(blxWindowGetDetailView(blxWindow), selectedSeqs, extend);  
    }
}


/* Move the selected display index 1 value to the left/right. Moves by full peptides
 * if viewing protein matches. Scrolls the detail view if necessary to keep the new 
 * index in view. If extend is true we extend the current selection range, otherwise
 * just move the current selection index */
static void moveSelectedDisplayIdxBy1(GtkWidget *window, const gboolean moveLeft, const gboolean extend)
{
  DEBUG_ENTER("moveSelectedDisplayIdxBy1()");

  GtkWidget *detailView = blxWindowGetDetailView(window);
  DetailViewProperties *detailViewProperties = detailViewGetProperties(detailView);

  int newSelectedBaseIdx = UNSET_INT;
  gboolean ok = FALSE;

  if (detailViewGetSelectedIdxSet(detailView))
    {
      /* Decrement the index if moving left or increment if moving right */
      newSelectedBaseIdx = detailViewProperties->selectedIndex->displayIdx;
      
      if (moveLeft)
        --newSelectedBaseIdx;
      else
        ++newSelectedBaseIdx;

      DEBUG_OUT("Moving selected display index to %d\n", newSelectedBaseIdx);

      ok = TRUE;
    }

  if (ok)
    {
      detailViewSetSelectedDisplayIdx(detailView,
                                      newSelectedBaseIdx,
                                      detailViewProperties->selectedIndex->frame,
                                      detailViewProperties->selectedIndex->baseNum,
                                      TRUE,
                                      TRUE,
                                      extend);

      detailViewRedrawAll(detailView);
    }

  DEBUG_EXIT("moveSelectedDisplayIdxBy1 returning ");
}


/* Zooms the display in/out. The modifiers control which section is zoomed */
static void zoomBlxWindow(GtkWidget *window, const gboolean zoomIn, const gboolean ctrl, const gboolean shift)
{
  if (ctrl)
    {
      if (shift)
        {
          zoomWholeBigPicture(blxWindowGetBigPicture(window));
        }
      else
        {
          zoomBigPicture(blxWindowGetBigPicture(window), zoomIn);
        }
    }
  else
    {
      zoomDetailView(blxWindowGetDetailView(window), zoomIn);
    }
}


/* Force a redraw of all widgets. Clears cached bitmaps etc. first */
void blxWindowRedrawAll(GtkWidget *blxWindow)
{
  GtkWidget *bigPicture = blxWindowGetBigPicture(blxWindow);
  bigPictureRedrawAll(bigPicture);

  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  detailViewRefreshAllHeaders(detailView);
  
  callFuncOnAllDetailViewTrees(detailView, widgetClearCachedDrawable, NULL);
  
  gtk_widget_queue_draw(blxWindow);
}


/* Utility to create a vbox with the given border and pack it into the given box.
 * Also put a frame around it with the given label if includeFrame is true */
static GtkWidget* createVBoxWithBorder(GtkWidget *parent, 
                                       const int borderWidth,
                                       const gboolean includeFrame,
                                       const char *frameTitle)
{
  GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_set_border_width(GTK_CONTAINER(vbox), borderWidth);

  if (includeFrame)
    {
      GtkWidget *frame = gtk_frame_new(frameTitle);
      gtk_box_pack_start(GTK_BOX(parent), frame, FALSE, FALSE, 0);
      gtk_container_add(GTK_CONTAINER(frame), vbox);
    }
  else
    {
      gtk_box_pack_start(GTK_BOX(parent), vbox, FALSE, FALSE, 0);
    }
  
  return vbox;
}

/* Utility to create an hbox with the given border and pack it into the given container */
static GtkWidget* createHBoxWithBorder(GtkWidget *parent, const int borderWidth, const gboolean includeFrame, const char *frameTitle)
{
  GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
  gtk_container_set_border_width(GTK_CONTAINER(hbox), borderWidth);
  
  if (includeFrame)
    {
      GtkWidget *frame = gtk_frame_new(frameTitle);
      gtk_container_add(GTK_CONTAINER(parent), frame);
      gtk_container_add(GTK_CONTAINER(frame), hbox);
    }
  else
    {
      gtk_container_add(GTK_CONTAINER(parent), hbox);
    }
  
  return hbox;
}


/* Utility to return true if any groups exist. Ignores the 'match set' group
 * if it doesn't have any sequences. */
static gboolean blxWindowGroupsExist(GtkWidget *blxWindow)
{
  gboolean result = FALSE;
  
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  GList *groupList = blxContext->sequenceGroups;
  
  if (g_list_length(groupList) >= 1)
    {
      result = TRUE;
    }
  
  return result;
}


/* Utility to create a text entry widget displaying the given double value. The
 * given callback will be called when the user OK's the dialog that this widget 
 * is a child of. */
static GtkWidget* createTextEntryString(const char *value)
{
  GtkWidget *entry = gtk_entry_new();
  
  gtk_entry_set_text(GTK_ENTRY(entry), value);
  gtk_entry_set_width_chars(GTK_ENTRY(entry), strlen(value) + 2);
  gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
  
  return entry;
}


/* Utility to create a text entry widget displaying the given double value. The
 * given callback will be called when the user OK's the dialog that this widget 
 * is a child of. */
static GtkWidget* createTextEntryInt(const int value)
{
  GtkWidget *entry = gtk_entry_new();
  
  char *displayText = convertIntToString(value);
  gtk_entry_set_text(GTK_ENTRY(entry), displayText);
  
  gtk_entry_set_width_chars(GTK_ENTRY(entry), strlen(displayText) + 2);
  gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
  
  g_free(displayText);

  return entry;
}


/* This dialog is shown when the user attempts to load a file that
 * is not in a natively-supported format. It asks the user what the
 * source should be, and allows the user to edit the coordinate range
 * to fetch data for. If the user enters valid values and hits OK then
 * the return values are populated and we return TRUE; else return FALSE. */
static gboolean showNonNativeFileDialog(GtkWidget *window, 
                                        const char *filename,
                                        GString **source_out,
                                        int *start_out,
                                        int *end_out)
{
  BlxContext *bc = blxWindowGetContext(window);
  
  char *title = g_strdup_printf("%sLoad Non-Native File", blxGetTitlePrefix(bc));

  GtkWidget *dialog = gtk_dialog_new_with_buttons(title, 
                                                  GTK_WINDOW(window),
                                                  (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
                                                  GTK_STOCK_CANCEL,
                                                  GTK_RESPONSE_REJECT,
                                                  GTK_STOCK_OK,
                                                  GTK_RESPONSE_ACCEPT,
                                                  NULL);

  g_free(title);
  
  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);

  GtkContainer *contentArea = GTK_CONTAINER(GTK_DIALOG(dialog)->vbox);

  char *labelStr = g_strdup_printf("\nFile '%s' is not a natively-supported file format.\n\nSpecify the Source to fetch data from this file using an external command\n(a fetch method for the Source must be specified in the config file)\n", filename);  
  GtkWidget *label = gtk_label_new(labelStr);
  g_free(labelStr);
  
  GtkWidget *sourceEntry = createTextEntryString("");
  GtkWidget *label2 = gtk_label_new("\n\nRegion to fetch data for:");
  GtkWidget *startEntry = createTextEntryInt(bc->refSeqRange.min());
  GtkWidget *endEntry = createTextEntryInt(bc->refSeqRange.max());
  
  GtkTable *table = GTK_TABLE(gtk_table_new(5, 2, FALSE));
  gtk_container_add(contentArea, GTK_WIDGET(table));

  gtk_table_attach(table, label, 0, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, DEFAULT_TABLE_XPAD, DEFAULT_TABLE_YPAD);
  gtk_table_attach(table, gtk_label_new("Source"), 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, DEFAULT_TABLE_XPAD, DEFAULT_TABLE_YPAD);
  gtk_table_attach(table, sourceEntry, 1, 2, 1, 2, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), GTK_SHRINK, DEFAULT_TABLE_XPAD, DEFAULT_TABLE_YPAD);
  gtk_table_attach(table, label2, 0, 2, 2, 3, GTK_SHRINK, GTK_SHRINK, DEFAULT_TABLE_XPAD, DEFAULT_TABLE_YPAD);
  gtk_table_attach(table, gtk_label_new("Start"), 0, 1, 3, 4, GTK_SHRINK, GTK_SHRINK, DEFAULT_TABLE_XPAD, DEFAULT_TABLE_YPAD);
  gtk_table_attach(table, startEntry, 1, 2, 3, 4, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), GTK_SHRINK, DEFAULT_TABLE_XPAD, DEFAULT_TABLE_YPAD);
  gtk_table_attach(table, gtk_label_new("End"), 0, 1, 4, 5, GTK_SHRINK, GTK_SHRINK, DEFAULT_TABLE_XPAD, DEFAULT_TABLE_YPAD);
  gtk_table_attach(table, endEntry, 1, 2, 4, 5, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), GTK_SHRINK, DEFAULT_TABLE_XPAD, DEFAULT_TABLE_YPAD);

  gtk_widget_show_all(dialog);
  gint response = gtk_dialog_run(GTK_DIALOG(dialog));
  gboolean result = FALSE;
  
  if (response == GTK_RESPONSE_ACCEPT)
    {
      const gchar *source = gtk_entry_get_text(GTK_ENTRY(sourceEntry));
      
      /* source is mandatory */
      if (source && *source)
        {
          result = TRUE;
          *source_out = g_string_new(source);

          /* to do: start and end */
        }
    }

  gtk_widget_destroy(dialog);

  return result;
}


/* This function loads the contents of a non-natively supported features-
 * file into blixem, using an external script to convert the file into
 * a supported file format such as GFF. A fetch method stanza must exist in the
 * config to define the script and its parameters.
 * This function asks the user what Source the file relates to so that it can 
 * look up the fetch method that should be used. It optionally also allows the
 * user to specify a coordinate range to limit the fetch to.. */
static void loadNonNativeFile(const char *filename,
                              GtkWidget *blxWindow,
                              MSP **newMsps,
                              GList **newSeqs,
                              GHashTable *lookupTable,
                              const int refSeqOffset,
                              const IntRange* const refSeqRange,
                              GError **error)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  GKeyFile *keyFile = blxGetConfig();

  GString *source = NULL;
  int start = bc->refSeqRange.min(), end = bc->refSeqRange.max();
  
  if (!showNonNativeFileDialog(blxWindow, filename, &source, &start, &end))
    return;

  GError *tmp_error = NULL;
  BlxDataType *dataType = NULL;
  const BlxFetchMethod *fetchMethod = NULL;

  if (!source || !source->str)
    {
      g_set_error(&tmp_error, BLX_ERROR, 1, "No Source specified; cannot look up fetch method.\n");
    }

  if (!tmp_error)
    {
      dataType = getBlxDataType(0, source->str, keyFile, &tmp_error);

      if (!dataType && !tmp_error)
        g_set_error(&tmp_error, BLX_ERROR, 1, "No data-type found for source '%s'\n", source->str);
    }

  if (!tmp_error)
    {
      if (dataType->bulkFetch)
        {
          GQuark fetchMethodQuark = g_array_index(dataType->bulkFetch, GQuark, 0);
          fetchMethod = getFetchMethodDetails(fetchMethodQuark, bc->fetchMethods);
        }

      if (!fetchMethod)
        {
          g_set_error(&tmp_error, BLX_ERROR, 1, "No fetch method specified for data-type '%s'\n", g_quark_to_string(dataType->name));
        }

      /* The output of the fetch must be a natively supported file format (i.e. GFF) */
      if (!tmp_error && fetchMethod->outputType != BLXFETCH_OUTPUT_GFF)
        {
          g_set_error(&tmp_error, BLX_ERROR, 1, "Expected fetch method output type to be '%s' but got '%s'\n", outputTypeStr(BLXFETCH_OUTPUT_GFF), outputTypeStr(fetchMethod->outputType));
        }
    }
     
  if (!tmp_error)
    {
      MatchSequenceData match_data = {NULL, bc->refSeqName, start, end, bc->dataset, source->str, filename};
      GString *command = doGetFetchCommand(fetchMethod, &match_data, &tmp_error);

      if (!tmp_error && command && command->str)
        {
          const char *fetchName = g_quark_to_string(fetchMethod->name);
          GSList *styles = blxReadStylesFile(NULL, NULL);

          sendFetchOutputToFile(command, keyFile, &bc->blastMode, 
                                bc->featureLists, bc->supportedTypes, styles,
                                &bc->matchSeqs, &bc->mspList, 
                                fetchName, bc->saveTempFiles, newMsps, newSeqs,
                                bc->columnList, lookupTable, refSeqOffset, refSeqRange, &tmp_error);
        }
    }          

  if (tmp_error)
    g_propagate_error(error, tmp_error);
}


/* Utility to call the updateDepth functions for any coverage views we have */
static void updateCoverageDepth(GtkWidget *blxWindow)
{
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);

  if (properties)
    {
      BigPictureProperties *bpProperties = bigPictureGetProperties(properties->bigPicture);

      if (bpProperties && bpProperties->coverageViewProperties())
        bpProperties->coverageViewProperties()->updateDepth();

      DetailViewProperties *dvProperties = detailViewGetProperties(properties->detailView);

      if (dvProperties && dvProperties->coverageViewProperties())
        dvProperties->coverageViewProperties()->updateDepth();
    }
}


/* Utility to hide any coverage views we have */
static void coverageSetHidden(GtkWidget *blxWindow, const bool hide)
{
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);

  if (properties)
    {
      BigPictureProperties *bpProperties = bigPictureGetProperties(properties->bigPicture);
      DetailViewProperties *dvProperties = detailViewGetProperties(properties->detailView);

      if (bpProperties && bpProperties->coverageViewProperties())
        widgetSetHidden(bpProperties->coverageViewProperties()->widget(), hide);

      if (dvProperties && dvProperties->coverageViewProperties())
        widgetSetHidden(dvProperties->coverageViewProperties()->widget(), hide);
    }
}


/* Dynamically load in additional features from a file. (should be called after
 * blixem's GUI has already started up, rather than during start-up where normal
 * feature-loading happens) */
static void dynamicLoadFeaturesFile(GtkWidget *blxWindow, const char *filename, const char *buffer, GError **error)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);

  /* Must be passed either a filename or buffer */
  g_return_if_fail(filename || buffer);
  g_return_if_fail(bc);

  GKeyFile *keyFile = blxGetConfig();
  
  /* We'll load the features from the file into some temporary lists */
  MSP *newMsps = NULL;
  GList *newSeqs = NULL;
  GError *tmp_error = NULL;
  int numAdded = 0;

  /* Create a temporary lookup table for BlxSequences so we can link them on GFF ID */
  GHashTable *lookupTable = g_hash_table_new(g_direct_hash, g_direct_equal);

  /* Assume it's a natively-supported file and attempt to parse it. The first thing this
   * does is check that it's a native file and if not it sets the error */
  loadNativeFile(filename, buffer, keyFile, &bc->blastMode, bc->featureLists, bc->supportedTypes, bc->styles, &newMsps, &newSeqs, bc->columnList, lookupTable, bc->refSeqOffset, &bc->refSeqRange, &tmp_error);

  if (tmp_error && filename)
    {
      /* Input file is not natively supported. We can still load it if
       * there is a fetch method associated with it: ask the user what
       * the Source is so that we can find the fetch method. Probably 
       * should only get here if the input is an actual file so don't
       * support this for buffers for now. */
      g_error_free(tmp_error);
      tmp_error = NULL;
      
      loadNonNativeFile(filename, blxWindow, &newMsps, &newSeqs, lookupTable, bc->refSeqOffset, &bc->refSeqRange, &tmp_error);
    }

  if (!tmp_error)
    {
      /* Count how many features were added. (Need to do this before blxMergeFeatures because
       * once this list gets merged the count will no longer be correct.) */
      numAdded = g_list_length(newSeqs);

      /* Fetch any missing sequence data and finalise the new sequences */
      BulkFetch bulk_fetch(FALSE, bc->saveTempFiles, bc->seqType, &newSeqs, bc->columnList,
                           bc->bulkFetchDefault, bc->fetchMethods, &newMsps, &bc->blastMode,
                           bc->featureLists, bc->supportedTypes, NULL, bc->refSeqOffset,
                           &bc->refSeqRange, bc->dataset, FALSE, lookupTable,
#ifdef PFETCH_HTML
                           bc->ipresolve,
                           bc->cainfo,
#endif
                           bc->fetch_debug);

      bulk_fetch.performFetch();
    }

  if (newMsps)
    {
      finaliseFetch(newSeqs, bc->columnList);

      finaliseBlxSequences(bc->featureLists, &newMsps, &newSeqs, bc->columnList, bc->refSeqOffset, bc->seqType, 
                           bc->numFrames, &bc->refSeqRange, TRUE, lookupTable);

      double lowestId = calculateMspData(newMsps, bc);
      bigPictureSetMinPercentId(blxWindowGetBigPicture(blxWindow), lowestId);

      /* Add the msps/sequences to the tree data models (must be done after finalise because
       * finalise populates the child msp lists for parent features) */
      detailViewAddMspData(blxWindowGetDetailView(blxWindow), newMsps, newSeqs);

      /* Merge the temporary lists into the main lists (takes ownership of the temp lists) */
      blxMergeFeatures(newMsps, newSeqs, &bc->mspList, &bc->matchSeqs);

      /* Cache the new msp display ranges and sort and filter the trees. */
      GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
      const int numUnalignedBases = detailViewGetNumUnalignedBases(detailView);
      cacheMspDisplayRanges(bc, numUnalignedBases);
      detailViewResortTrees(detailView);
      callFuncOnAllDetailViewTrees(detailView, refilterTree, NULL);

      /* Recalculate the coverage */
      bc->calculateDepth(numUnalignedBases);
      updateCoverageDepth(blxWindow);
  
      /* Re-calculate the height of the exon views */
      GtkWidget *bigPicture = blxWindowGetBigPicture(blxWindow);
      calculateExonViewHeight(bigPictureGetFwdExonView(bigPicture));
      calculateExonViewHeight(bigPictureGetRevExonView(bigPicture));
      forceResize(bigPicture);
  
      blxWindowRedrawAll(blxWindow);
      
      if (numAdded == 0)
        g_warning("No features loaded\n");
      else if (numAdded == 1)
        g_message("Loaded %d new feature\n", numAdded);
      else
        g_message("Loaded %d new features\n", numAdded);
    }

  g_hash_table_unref(lookupTable);

  if (tmp_error)
    g_propagate_error(error, tmp_error);
}


/***********************************************************
 *                         View panes menu                 *
 ***********************************************************/

/* Toggle visibility the n'th tree. This is the active strand's frame n if displaying
 * protein matches (where we only display one strand), or the forward or reverse
 * strand tree if displaying DNA matches (where both strands are displayed). */
static void toggleTreeVisibility(GtkWidget *blxWindow, const int number)
{
  const gboolean toggled = blxWindowGetDisplayRev(blxWindow);
  const BlxStrand activeStrand = toggled ? BLXSTRAND_REVERSE : BLXSTRAND_FORWARD;
  
  /* For protein matches, trees are always displayed in frame order (i.e. 1, 2, 3), 
   * so just use the number pressed for the frame, and the active strand for the
   * strand. */
  int frame = number;
  BlxStrand strand = activeStrand;
  
  /* For DNA matches, the frame is always 1, but the strand depends on which number
   * was pressed: use 1 to toggle active strand, 2 for other strand */
  if (blxWindowGetSeqType(blxWindow) == BLXSEQ_DNA)
    {
      frame = 1;
      
      if (number == 1)
        {
          strand = activeStrand;
        }
      else if (number == 2)
        {
          strand = toggled ? BLXSTRAND_FORWARD : BLXSTRAND_REVERSE;
        }
    }
  
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  GtkWidget *tree = detailViewGetTreeContainer(detailView, strand, frame);
  
  if (tree && gtk_widget_get_parent(tree))
    {
      widgetSetHidden(tree, !widgetGetHidden(tree));
    }
}


/* Toggle visibility of the active (1) or other (2) strand grid depending on the number pressed */
static void toggleGridVisibility(GtkWidget *blxWindow, const int number)
{
  if (number == 1 || number == 2)
    {
      GtkWidget *bigPicture = blxWindowGetBigPicture(blxWindow);
      const gboolean useFwdGrid = (number == 1) != blxWindowGetDisplayRev(blxWindow);
      
      GtkWidget *grid = useFwdGrid ? bigPictureGetFwdGrid(bigPicture) : bigPictureGetRevGrid(bigPicture);
      widgetSetHidden(grid, !widgetGetHidden(grid));

      /* We need to force a resize of the big picture because the size-allocate
       * signal doesn't get emitted by default when its contents shrink */
      forceResize(bigPicture);
    }
}


/* Toggle visibility of the active (1) or other (2) strand exon view depending on the number pressed */
static void toggleExonViewVisibility(GtkWidget *blxWindow, const int number)
{
  if (number == 1 || number == 2)
    {
      GtkWidget *bigPicture = blxWindowGetBigPicture(blxWindow);
      const gboolean useFwdExonView = (number == 1) != blxWindowGetDisplayRev(blxWindow);
      
      GtkWidget *exonView = useFwdExonView ? bigPictureGetFwdExonView(bigPicture) : bigPictureGetRevExonView(bigPicture);
      widgetSetHidden(exonView, !widgetGetHidden(exonView));

      forceResize(bigPicture);
    }
}


/* Toggle the visibility of tree/grid panes following a number key press */
static void togglePaneVisibility(GtkWidget *blxWindow, const int number, const gboolean modifier1, const gboolean modifier2)
{
  /* Affects big picture if modifier1 was pressed, the detail view otherwise */
  if (modifier1)
    {
      /* If modifier 2 was also pressed, affects the exon views; otherwise the grids */
      if (modifier2)
        {
          toggleExonViewVisibility(blxWindow, number);
        }
      else
        {
          toggleGridVisibility(blxWindow, number);
        }
    }
  else
    {
      toggleTreeVisibility(blxWindow, number);
    }
}


/* Repeat the last find operation. Searches for the next (rightwards) match unless the given
 * modifier is pressed, in which case it searches for the previous (leftwards) match */
static void findAgain(GtkWidget *blxWindow, const gboolean modifier)
{
  GError *error = NULL;

  const int startCoord = getSearchStartCoord(blxWindow, FALSE, modifier);

  /* Try the DNA search. Does nothing if last search was not a DNA search. */
  blxWindowFindDnaString(blxWindow, NULL, startCoord, modifier, TRUE, &error);

  if (error)
    {
      /* DNA search was attempted but not found. Try looping round to the beginning */
      g_error_free(error);
      error = NULL;
      const int newStart = getSearchStartCoord(blxWindow, TRUE, modifier);
      blxWindowFindDnaString(blxWindow, NULL, newStart, modifier, TRUE, &error);
    }
  
  if (!error)
    {
      /* Try the search-from-list search. Returns NULL if last search was not a list search */
      GList *seqList = findSeqsFromList(blxWindow, NULL, BLXCOL_NONE, FALSE, TRUE, &error);
      
      if (!seqList && !error)
        {
          /* Try the search-by-name search. Returns NULL if last search was not a name search. */
          seqList = findSeqsFromColumn(blxWindow, NULL, BLXCOL_NONE, FALSE, TRUE, &error);
        }
      
      /* If either the list or name search succeeded, select the prev/next MSP from the 
       * found sequence(s) depending on which direction we're searching. */
      if (seqList)
        {
          blxWindowSetSelectedSeqList(blxWindow, seqList);
          
          if (modifier)
            {
              prevMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
            }
          else
            {
              nextMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
            }
        }
    }
  
  if (error)
    {
      prefixError(error, "Find %s failed. ", (modifier ? "previous" : "next"));
      reportAndClearIfError(&error, G_LOG_LEVEL_MESSAGE);
    }
}


/* Called when the state of a check button is toggled */
static void onVisibilityButtonToggled(GtkWidget *button, gpointer data)
{
  GtkWidget *widgetToToggle = GTK_WIDGET(data);
  gboolean visible = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
  widgetSetHidden(widgetToToggle, !visible);
}


/* Create a check button to control visibility of the given widget */
static void createVisibilityButton(GtkWidget *widgetToToggle, const char *mnemonic, GtkWidget *container)
{
  GtkWidget *button = gtk_check_button_new_with_mnemonic(mnemonic);
  gtk_container_add(GTK_CONTAINER(container), button);

  /* Set the state depending on the widget's current visibility */
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), GTK_WIDGET_VISIBLE(widgetToToggle));
  
  g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(onVisibilityButtonToggled), widgetToToggle);
}


/* Create a check button to control visibility of the given tree. */
static void createTreeVisibilityButton(GtkWidget *detailView, const BlxStrand strand, const int frame, GtkWidget *container)
{
  /* Some trees may have been removed from the blixem window if they are not on the active 
   * strand, so only show check boxes for those that are in the window (i.e. have a parent). 
   * Note that we get the tree container here, which consists of the tree itself plus any headers etc. */
  GtkWidget *tree = detailViewGetTreeContainer(detailView, strand, frame);
  
  if (gtk_widget_get_parent(tree))
    {
      const gboolean toggled = detailViewGetDisplayRev(detailView);
      gboolean isActiveStrand = ((strand == BLXSTRAND_FORWARD) != toggled);

      if (detailViewGetSeqType(detailView) == BLXSEQ_DNA)
        {
          /* We only have 1 frame, but trees are from both strands, so distinguish between strands.
           * Put each strand in its own frame. */
          char text1[] = "Show _active strand";
          char text2[] = "Show othe_r strand";

          GtkWidget *frame = gtk_frame_new(isActiveStrand ? "Active strand" : "Other strand");
          gtk_container_add(GTK_CONTAINER(container), frame);
          createVisibilityButton(tree, isActiveStrand ? text1 : text2, frame);
        }
      else
        {
          /* All the visible trees should be in the same strand, so just distinguish by frame number. */
          char formatStr[] = "Show frame _%d";
          char displayText[strlen(formatStr) + numDigitsInInt(frame) + 1];
          sprintf(displayText, formatStr, frame);

          createVisibilityButton(tree, displayText, container);
        }
    }
}


/* Callback called when the user clicks the 'bump exon view' button */
static void onBumpExonView(GtkWidget *button, gpointer data)
{
  GtkWidget *exonView = GTK_WIDGET(data);
  const gboolean expanded = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
  exonViewSetExpanded(exonView, expanded);
}


/* Create the set of settings buttons to control display of an exon-view widget. */
static void createExonButtons(GtkWidget *exonView, const char *visLabel, const char *bumpLabel, GtkWidget *parent)
{
  /* Pack everything in an hbox */
  GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(parent), hbox);
  
  /* Create a check button to control visibility of the exon view */
  createVisibilityButton(exonView, visLabel, hbox);

  /* Create a check button to control whether the exon view is expanded or compressed */
  const gboolean isBumped = exonViewGetExpanded(exonView);
  createCheckButton(GTK_BOX(hbox),
                    bumpLabel,
                    NULL,
                    isBumped,
                    G_CALLBACK(onBumpExonView),
                    exonView);
}


/* Shows the "View panes" dialog. This dialog allows the user to show/hide certain portions of the window. */
void showViewPanesDialog(GtkWidget *blxWindow, const gboolean bringToFront)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  const BlxDialogId dialogId = BLXDIALOG_VIEW;
  GtkWidget *dialog = getPersistentDialog(bc->dialogList, dialogId);
  
  if (!dialog)
    {
      char *title = g_strdup_printf("%sView panes", blxGetTitlePrefix(bc));

      dialog = gtk_dialog_new_with_buttons(title, 
                                           GTK_WINDOW(blxWindow), 
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_STOCK_OK,
                                           GTK_RESPONSE_ACCEPT,
                                           NULL);

      g_free(title);

      /* These calls are required to make the dialog persistent... */
      addPersistentDialog(bc->dialogList, dialogId, dialog);
      g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
      
      g_signal_connect(dialog, "response", G_CALLBACK(onResponseDialog), GINT_TO_POINTER(TRUE));
    }
  else
    {
      /* Clear contents and re-create */
      dialogClearContentArea(GTK_DIALOG(dialog));
    }
  
  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
  GtkWidget *contentArea = GTK_DIALOG(dialog)->vbox;

  int borderWidth = 12;
  
  /* Big picture */
  GtkWidget *bp = blxWindowGetBigPicture(blxWindow);
  GtkWidget *bpVbox = createVBoxWithBorder(contentArea, borderWidth, TRUE, "Big picture");
  
  createVisibilityButton(bp, "Show _big picture", bpVbox);
  GtkWidget *bpSubBox = createVBoxWithBorder(bpVbox, borderWidth, FALSE, NULL);
  
  GtkWidget *bpActiveStrand = createVBoxWithBorder(bpSubBox, 0, TRUE, "Active strand");
  createVisibilityButton(bigPictureGetActiveGrid(bp), "Show _grid", bpActiveStrand);
  createExonButtons(bigPictureGetActiveExonView(bp), "Show _exons    ", "_Bump exons    ", bpActiveStrand);
  
  GtkWidget *bpOtherStrand = createVBoxWithBorder(bpSubBox, 0, TRUE, "Other strand");
  createVisibilityButton(bigPictureGetInactiveGrid(bp), "Show gr_id", bpOtherStrand);
  createExonButtons(bigPictureGetInactiveExonView(bp), "Show e_xons    ", "Bum_p exons    ", bpOtherStrand);
  
  /* Detail view */
  GtkWidget *dvVbox = createVBoxWithBorder(contentArea, borderWidth, TRUE, "Alignment lists");
  GtkWidget *dv = blxWindowGetDetailView(blxWindow);
  createVisibilityButton(dv, "Show alignment _lists", dvVbox);
  
  GtkWidget *dvSubBox = createVBoxWithBorder(dvVbox, borderWidth, FALSE, NULL);
  const int numFrames = blxWindowGetNumFrames(blxWindow);
  int frame = 1;
  for ( ; frame <= numFrames; ++frame)
    {
      createTreeVisibilityButton(dv, blxWindowGetActiveStrand(blxWindow), frame, dvSubBox);
      createTreeVisibilityButton(dv, blxWindowGetInactiveStrand(blxWindow), frame, dvSubBox);
    }
  
  /* Coverage views */
  GtkWidget *bpCoverageView = blxWindowGetBigPictureCoverageView(blxWindow);
  GtkWidget *dvCoverageView = blxWindowGetDetailViewCoverageView(blxWindow);
  GtkWidget *coverageVbox = createVBoxWithBorder(contentArea, borderWidth, TRUE, "Coverage view");
  createVisibilityButton(bpCoverageView, "Show _coverage view (big picture)", coverageVbox);
  createVisibilityButton(dvCoverageView, "Show _coverage view (detail view)", coverageVbox);

  
  gtk_widget_show_all(dialog);
  
  if (bringToFront)
    {
      gtk_window_present(GTK_WINDOW(dialog));
    }
}



/***********************************************************
 *                          Find menu                      *
 ***********************************************************/

static GList* findSeqsFromColumn(GtkWidget *blxWindow, const char *inputText, const BlxColumnId inputCol, const gboolean rememberSearch, const gboolean findAgain, GError **error)
{
  /* Previous values (if applicable) */
  static char *prevSearchStr = NULL;
  static BlxColumnId prevSearchCol = BLXCOL_NONE;

  /* Current values */
  char *searchStr = NULL;
  BlxColumnId searchCol = BLXCOL_NONE;
  
  /* If it's a find-again, use the existing values; otherwise, use the input values */
  if (findAgain)
    {
      searchStr = prevSearchStr;
      searchCol = prevSearchCol;
    }
  else
    {
      g_free(searchStr);
      searchStr = g_strdup(inputText);
      searchCol = inputCol;
    
      if (rememberSearch)
        {
          prevSearchStr = searchStr;
          prevSearchCol = searchCol;
        }
    }
  
  if (!searchStr || searchCol == BLXCOL_NONE)
    {
      /* We will get here if we do a find-again when there wasn't a previous find */
      return NULL;
    }
  
  /* Loop through all the sequences and see if the sequence data for this column
   * matches the search string */
  GList *seqList = blxWindowGetAllMatchSeqs(blxWindow);
  BlxContext *bc = blxWindowGetContext(blxWindow);
  SeqSearchData searchData = {searchStr, searchCol, bc, NULL, NULL};
  
  g_list_foreach(seqList, getSequencesThatMatch, &searchData);
  
  if (g_list_length(searchData.matchList) < 1)
    {
      GList *columnList = blxWindowGetColumnList(blxWindow);
      const char *columnName = getColumnTitle(columnList, searchCol);

      if (searchData.error)
        g_propagate_error(error, searchData.error);
      else
        g_set_error(error, BLX_ERROR, BLX_ERROR_STRING_NOT_FOUND, "No sequences found where column '%s' matches text '%s'.\n", columnName, searchStr);
    }
  
  return searchData.matchList;
}


/* Utility to extract the contents of a GtkTextView and return it as a string. The result is
 * owned by the GtkTextView and should not be free'd. */
static const char* getStringFromTextView(GtkTextView *textView)
{
  if (!textView || !GTK_WIDGET_SENSITIVE(GTK_WIDGET(textView)))
    {
      g_critical("Could not set search string: invalid text entry box\n");
      return NULL;
    }
  
  /* Get the input text from the text buffer and create the group */
  GtkTextBuffer *textBuffer = gtk_text_view_get_buffer(textView);
  
  GtkTextIter start, end;
  gtk_text_buffer_get_bounds(textBuffer, &start, &end);
  
  return gtk_text_buffer_get_text(textBuffer, &start, &end, TRUE);
}


/* Finds all the valid sequences blixem knows about whose column data matches
 * an item from the given input text. Returns the results as a GList of BlxSequences.
 * The input text may be a multi-line list (one search item per line). */
static GList* findSeqsFromList(GtkWidget *blxWindow,
                               const char *inputText, 
                               const BlxColumnId inputCol, 
                               const gboolean rememberSearch, 
                               const gboolean findAgain, 
                               GError **error)
{
  /* Previous values (if applicable) */
  static char *prevSearchStr = NULL;
  static BlxColumnId prevSearchCol = BLXCOL_NONE;
  
  /* Current values */
  char *searchStr = NULL;
  BlxColumnId searchCol = BLXCOL_NONE;
  
  /* If we-re doing a find-again, use the values from last time; otherwise use the input values */
  if (findAgain)
    {
      searchStr = prevSearchStr;
      searchCol = prevSearchCol;
    }
  else
    {
      g_free(searchStr);
      searchStr = g_strdup(inputText);
      searchCol = inputCol;
    
      if (rememberSearch)
        {
          prevSearchStr = searchStr;
          prevSearchCol = searchCol;
        }
    }
      
  if (!searchStr || searchCol == BLXCOL_NONE)
    {
      /* We may get here if we did a find-again when there was no previous find */
      return NULL;
    }

  
  GError *tmpError = NULL;
  GList *seqList = getSeqStructsFromText(blxWindow, searchStr, searchCol, &tmpError);

  if (g_list_length(seqList) < 1)
    {
      GList *columnList = blxWindowGetColumnList(blxWindow);
      const char *columnName = getColumnTitle(columnList, searchCol);
      
      if (tmpError)
        g_propagate_error(error, tmpError);
      else
        g_set_error(error, BLX_ERROR, BLX_ERROR_STRING_NOT_FOUND, "No sequences found where column '%s' matches text '%s'.\n", columnName, searchStr);
    }
  
  return seqList;
}


/* Callback called when requested to find sequences from a sequence name. Selects
 * the sequences and scrolls to the start of the first match in the selection */
static gboolean onFindSeqsFromName(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  const char *inputText = NULL;
  
  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
    {
      inputText = getStringFromTextEntry(GTK_ENTRY(data));
    }
  
  GtkWidget *dialog = gtk_widget_get_toplevel(button);
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(GTK_WINDOW(dialog)));
  
  /* Find the combo box on the dialog (there should only be one), which tells
   * us which column to search. */
  GtkComboBox *combo = widgetGetComboBox(dialog);
  BlxColumnId searchCol = getColumnFromComboBox(combo);

  /* Find all sequences that match */
  GError *error = NULL;
  GList *seqList = findSeqsFromColumn(blxWindow, inputText, searchCol, TRUE, FALSE, &error);
  
  if (error)
    {
      reportAndClearIfError(&error, G_LOG_LEVEL_CRITICAL);
      result = FALSE;
    }
  
  if (seqList)
    {
      GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(button));
      GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));

      blxWindowSetSelectedSeqList(blxWindow, seqList);
      
      if (responseId == BLX_RESPONSE_FORWARD)
        {
          nextMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
        }
      else if (responseId == BLX_RESPONSE_BACK)
        {
          prevMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
        }
      else
        {
          firstMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
        }
    }
    
  return result;
}


/* Callback called when requested to find sequences from a given list. Selects
 * the sequences ands scrolls to the start of the first match in the selection. */
static gboolean onFindSeqsFromList(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  const char *inputText = NULL;
  
  /* Nothing to do if this button is not active */
  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
    {
      inputText = getStringFromTextView(GTK_TEXT_VIEW(data));
    }
  
  /* Get the dialog and main window */
  GtkWidget *dialog = gtk_widget_get_toplevel(button);
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(GTK_WINDOW(dialog)));

  /* Find the combo box on the dialog (there should only be one), which tells
   * us which column to search. */
  GtkComboBox *combo = widgetGetComboBox(dialog);
  BlxColumnId searchCol = getColumnFromComboBox(combo);
  
  GError *error = NULL;
  GList *seqList = findSeqsFromList(blxWindow, inputText, searchCol, TRUE, FALSE, &error);

  if (error)
    {
      reportAndClearIfError(&error, G_LOG_LEVEL_CRITICAL);
      result = FALSE;
    }
  
  if (seqList)
    {
      blxWindowSetSelectedSeqList(blxWindow, seqList);
      
      if (responseId == BLX_RESPONSE_FORWARD)
        {
          nextMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
        }
      else if (responseId == BLX_RESPONSE_BACK)
        {
          prevMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
        }
      else
        {
          firstMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
        }
    }
  
  return result;
}


/* Search for the given DNA string in the reference sequence. Searches for the next (rightwards)
 * value from the given start coord, unless searchLeft is true in which case it searches leftwards.
 * If findAgain is true it repeats the last DNA search. */
static void blxWindowFindDnaString(GtkWidget *blxWindow, 
                                   const char *inputSearchStr, 
                                   const int refSeqStart,
                                   const gboolean searchLeft, 
                                   const gboolean findAgain, 
                                   GError **error)
{
  /* Remember the last input string for use with findAgain */
  static char *searchStr = NULL;
  
  if (!findAgain)
    {
      /* We must copy the input string because it may not exist if/when we come to do a 'find again' */
      g_free(searchStr);
      searchStr = g_strdup(inputSearchStr);
    }
  
  const int searchStrMax = searchStr ? strlen(searchStr) - 1 : -1;
  
  if (searchStrMax < 0)
    {
      return;
    }

  const int searchStart = searchLeft ? searchStrMax : 0;
  const int searchEnd = searchLeft ? 0 : searchStrMax;
  const int searchStrIncrement = searchLeft ? -1 : 1;
  
  /* Values increase left-to-right in normal display or right-to-left in reversed display */
  BlxContext *bc = blxWindowGetContext(blxWindow);
  const gboolean searchForward = (searchLeft == bc->displayRev);
  const int refSeqIncrement = searchForward ? 1 : -1;
  
  /* We'll need to complement ref seq bases if the active strand is the reverse strand */
  const gboolean complement = (blxWindowGetActiveStrand(blxWindow) == BLXSTRAND_REVERSE);
  
  int refSeqIdx = refSeqStart;
  int searchStrIdx = searchStart;
  int matchStart = UNSET_INT;
  
  while (refSeqIdx >= bc->refSeqRange.min() && refSeqIdx <= bc->refSeqRange.max() && searchStrIdx >= 0 && searchStrIdx <= searchStrMax)
    {
      const char refSeqBase = getSequenceIndex(bc->refSeq, refSeqIdx, complement, &bc->refSeqRange, BLXSEQ_DNA);
      char searchStrBase = convertBaseToCorrectCase(searchStr[searchStrIdx], BLXSEQ_DNA);      
      
      if (refSeqBase == searchStrBase)
        {
          /* The base matches. If it's the first matching base, set the match-start coord (or if we're 
           * searching leftwards, then always set the match-start coord, because the start is actually 
           * the last coord that will be found). Then proceed to the next position in the search string */
          if (matchStart == UNSET_INT)
            {
              matchStart = refSeqIdx;
            }
          
          searchStrIdx += searchStrIncrement;
          refSeqIdx += refSeqIncrement;
        }
      else if (matchStart != UNSET_INT)
        {
          /* We were in a match but this base doesn't match. Reset to the start of the 
           * search string, and start looking again from one base after the place where the last
           * match started. (We need to re-check all bases from there because we're comparing
           * against a different section of the search string now.) */
          searchStrIdx = searchStart;
          refSeqIdx = matchStart + refSeqIncrement;
          matchStart = UNSET_INT;
        }
      else
        {
          refSeqIdx += refSeqIncrement;
        }
    }
  
  /* Undo the last increment, so that we have the final coords of the matching section (if found) */
  refSeqIdx -= refSeqIncrement;
  searchStrIdx -= searchStrIncrement;
  
  /* If we reached the end of the search string, then we matched the whole lot. */
  const gboolean finished = searchStrIdx == searchEnd;
  
  if (matchStart != UNSET_INT && finished)
    {
      GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
      const int frame = 1;
      
      int resultStart = searchLeft ? refSeqIdx : matchStart;
      int resultEnd = searchLeft ? matchStart : refSeqIdx;
      
      /* Select the start index in the result */
      detailViewSetSelectedDnaBaseIdx(detailView, resultStart, frame, TRUE, FALSE, FALSE);

      /* Extend the selection to the end index */
      detailViewSetSelectedDnaBaseIdx(detailView, resultEnd, frame, FALSE, TRUE, TRUE);
      
      detailViewRedrawAll(detailView);
    }
  else
    {
      g_set_error(error, BLX_ERROR, BLX_ERROR_STRING_NOT_FOUND, "The string '%s' was not found in the reference sequence searching to the %s from coord %d.\n", searchStr, (searchLeft ? "left" : "right"), refSeqStart);
    }
}


/* Get the start coord for a search. If startBeginning is false, this gets the currently-selected display
 * index (shifted by one base so that we don't start searching at the same position as a previous
 * find result) or, if no base index is selected, returns the start coord of the current display range. If
 * startBeginning is true, just start from the beginning of the reference sequence. The result is 
 * nucleotide coord on the ref sequence. */
static int getSearchStartCoord(GtkWidget *blxWindow, const gboolean startBeginning, const gboolean searchLeft)
{
  int result = UNSET_INT;
  
  const BlxContext *bc = blxWindowGetContext(blxWindow);
  
  if (startBeginning)
    {
      result = (searchLeft == bc->displayRev) ? bc->refSeqRange.min() : bc->refSeqRange.max();
    }
  else  
    {
      GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
      result = detailViewGetSelectedDisplayIdx(detailView);
      
      if (result != UNSET_INT)
        {
          /* Increment by one to make sure we don't re-find a previously-found match
           * (or decrement if searching leftwards) */
           if (searchLeft)
             {
                --result;
              }
            else
              {
                ++result;
              }
        }
      else
        {
          /* The start display coord is the min coord if we're searching left and the max if searching right. */
          const IntRange* const displayRange = detailViewGetDisplayRange(detailView);
          result = searchLeft ? displayRange->max() : displayRange->min();
        }

      /* Convert the display coord to a nucleotide coord */
      result = convertDisplayIdxToDnaIdx(result, bc->seqType, 1, 1, bc->numFrames, bc->displayRev, &bc->refSeqRange);
    }
  
  return result;
}


/* Callback called when requested to search for a DNA string. If found, sets the currently-
 * selected base index to the coord where the matching string starts. The text entry for the
 * search string is passed as the callback data. */
static gboolean onFindDnaString(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  /* Get the search string from the text entry. If the toggle button is not active, call
   * blxWindowFindDnaString with a NULL search string to "cancel" any previous searches
   * so that "findAgain" will not attempt to perform a DNA search). */
  const char *searchStr = NULL;

  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
    {
      searchStr = getStringFromTextEntry(GTK_ENTRY(data));

      if (!searchStr || strlen(searchStr) < 1)
        {
          g_critical("DNA search failed. The search string was empty.\n");
          result = FALSE;
        }
    }

  /* Search left wrt the screen if the user hit 'back' search right if 'forward' */
  const gboolean searchLeft = (responseId == BLX_RESPONSE_BACK);

  GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(button));
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));
  
  const gboolean startBeginning = (responseId != BLX_RESPONSE_FORWARD && responseId != BLX_RESPONSE_BACK);
  int startCoord = getSearchStartCoord(blxWindow, startBeginning, searchLeft);
  
  GError *error = NULL;
  blxWindowFindDnaString(blxWindow, searchStr, startCoord, searchLeft, FALSE, &error);
  
  if (error)
    {
      if (!startBeginning)
        {
          /* Try looping round to the beginning */
          postfixError(error, " Trying again from the %s of the range.\n", (searchLeft ? "end" : "start"));
          reportAndClearIfError(&error, G_LOG_LEVEL_WARNING);
          
          startCoord = getSearchStartCoord(blxWindow, TRUE, searchLeft);
          blxWindowFindDnaString(blxWindow, searchStr, startCoord, searchLeft, FALSE, &error);
        }
    }
  
  if (error)
    {
      result = FALSE;
      prefixError(error, "DNA search failed. ");
      reportAndClearIfError(&error, G_LOG_LEVEL_CRITICAL);
    }
  
  return result;
}


/* Utility to create a drop-down combo box for selecting the column to search by */
static void createSearchColumnCombo(GtkTable *table, const int col, const int row, GtkWidget *blxWindow)
{
  GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
  gtk_table_attach(table, hbox, col, col + 1, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), GTK_SHRINK, DEFAULT_TABLE_XPAD, DEFAULT_TABLE_YPAD);
  
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  DetailViewProperties *dvProperties = detailViewGetProperties(detailView);
  GList *columnList = blxWindowGetColumnList(dvProperties->blxWindow());

  createSortBox(GTK_BOX(hbox), detailView, BLXCOL_SEQNAME, columnList, "Search column: ", TRUE);  
}


static void onClearFindDialog(GtkWidget *button, gpointer data)
{
  GSList *entryList = (GSList*)data;
  
  for ( ; entryList; entryList = entryList->next)
    {
      if (GTK_IS_ENTRY(entryList->data))
        {
          GtkEntry *entry = GTK_ENTRY(entryList->data);
          gtk_entry_set_text(entry, "");
        }
      else if (GTK_IS_TEXT_VIEW(entryList->data))
        {
          GtkTextView *textView = GTK_TEXT_VIEW(entryList->data);
          gtk_text_buffer_set_text(gtk_text_view_get_buffer(textView), "", -1);
        }
      else
        {
          g_warning("onClearFindDialog: Unexpected widget type: expected GtkEntry or GtkTextView\n");
        }
    }
}


/* Clear up data created for the find dialog when it is destroyed
 * (here for completeness but not actually called because the dialog
 * is persistent). */
static void onDestroyFindDialog(GtkWidget *widget, gpointer data)
{
  GSList *entryList = (GSList*)data;

  if (entryList)
    {
      g_slist_free(entryList);
    }
}


/* Show the 'Find' dialog */
void showFindDialog(GtkWidget *blxWindow, const gboolean bringToFront)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  const BlxDialogId dialogId = BLXDIALOG_FIND;
  GtkWidget *dialog = getPersistentDialog(bc->dialogList, dialogId);
  
  if (!dialog)
    {
      char *title = g_strdup_printf("%sFind sequences", blxGetTitlePrefix(bc));

      /* Note that we add some buttons here but some more at the end because
       * we want to create a custom Clear button in the middle somewhere */
      dialog = gtk_dialog_new_with_buttons(title, 
                                           GTK_WINDOW(blxWindow), 
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_STOCK_GO_BACK,     /* previous match */
                                           BLX_RESPONSE_BACK,
                                           GTK_STOCK_GO_FORWARD,  /* next match */
                                           BLX_RESPONSE_FORWARD,
                                           NULL);
      
      g_free(title);
      
      /* These calls are required to make the dialog persistent... */
      addPersistentDialog(bc->dialogList, dialogId, dialog);
      g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
      
      GtkBox *contentArea = GTK_BOX(GTK_DIALOG(dialog)->vbox);
      GtkBox *actionArea = GTK_BOX(GTK_DIALOG(dialog)->action_area);
      const int numRows = 3;
      const int numCols = 2;
      GtkTable *table = GTK_TABLE(gtk_table_new(numRows, numCols, FALSE));
      gtk_box_pack_start(contentArea, GTK_WIDGET(table), TRUE, TRUE, 0);

      /* This list will be populated with the text entry widgets. */
      GSList *entryList = NULL;

      /* Column 1: match-seq search options */
      GtkRadioButton *button1 = createRadioButton(table, 1, 1, NULL, "_Text search (wildcards * and ?)", TRUE, TRUE, FALSE, onFindSeqsFromName, blxWindow, &entryList);
      createRadioButton(table, 1, 2, button1, "_List search", FALSE, TRUE, TRUE, onFindSeqsFromList, blxWindow, &entryList);
      createSearchColumnCombo(table, 1, 3, blxWindow);
      
      /* Column 2: ref-seq search options */
      createRadioButton(table, 2, 1, button1, "_DNA search", FALSE, TRUE, FALSE, onFindDnaString, blxWindow, &entryList);
      
      /* Add a button to clear the text entry fields. It's easier to do this
       * here than in the response callback because we want to send different data */
      GtkWidget *clearButton = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
      g_signal_connect(G_OBJECT(clearButton), "clicked", G_CALLBACK(onClearFindDialog), entryList);
      gtk_box_pack_end(actionArea, clearButton, FALSE, FALSE, 0);

      /* Add remaining buttons after the Clear button */
      gtk_dialog_add_buttons(GTK_DIALOG(dialog),
                             GTK_STOCK_CLOSE,       /* close / cancel */
                             GTK_RESPONSE_REJECT,
                             GTK_STOCK_OK,          /* ok, do the search */
                             GTK_RESPONSE_ACCEPT,
                             NULL);

      gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(blxWindow));
      g_signal_connect(dialog, "response", G_CALLBACK(onResponseDialog), GINT_TO_POINTER(TRUE));
      g_signal_connect(dialog, "destroy", G_CALLBACK(onDestroyFindDialog), entryList);
    }

  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
  
  gtk_widget_show_all(dialog);
  
  if (bringToFront)
    {
      gtk_window_present(GTK_WINDOW(dialog));
    }
}


/* Show the 'Info' dialog, which displays info about the currently-selected sequence(s) */
void showInfoDialog(GtkWidget *blxWindow)
{
  GtkWidget *dialog = gtk_dialog_new_with_buttons("Blixem - Sequence info", 
						  NULL, 
						  GTK_DIALOG_DESTROY_WITH_PARENT,
						  GTK_STOCK_CLOSE,
						  GTK_RESPONSE_REJECT,
						  NULL);
  
  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_REJECT);

  int width = blxWindow->allocation.width * 0.7;
  int height = blxWindow->allocation.height * 0.9;
  
  BlxContext *bc = blxWindowGetContext(blxWindow);

  /* Compile the message text from the selected sequence(s) */
  GString *resultStr = g_string_new("");
  GList *seqItem = bc->selectedSeqs;
  
  for ( ; seqItem; seqItem = seqItem->next)
    {
      BlxSequence *blxSeq = (BlxSequence*)(seqItem->data);
      char *seqText = blxSequenceGetInfo(blxSeq, TRUE, bc->columnList);
      g_string_append_printf(resultStr, "%s\n\n", seqText);
      g_free(seqText);
    }
  
  /* We'll use the same fixed-width font as the detail-view */
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  PangoFontDescription *fontDesc = detailViewGetFontDesc(detailView);
  
  GtkWidget *child = createScrollableTextView(resultStr->str, TRUE, fontDesc, TRUE, NULL, &height, NULL);
                             
  gtk_window_set_default_size(GTK_WINDOW(dialog), width, height);
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), child, TRUE, TRUE, 0);
  
  g_signal_connect(dialog, "response", G_CALLBACK(onResponseDialog), NULL);
  gtk_widget_show_all(dialog);
  
  g_string_free(resultStr, TRUE);
}


/* Toggle the bump state. Currently only the exon view can be bumped, so this just
 * affects that. */
static void toggleBumpState(GtkWidget *blxWindow)
{
  GtkWidget *bigPicture = blxWindowGetBigPicture(blxWindow);
  
  exonViewToggleExpanded(bigPictureGetFwdExonView(bigPicture));
  exonViewToggleExpanded(bigPictureGetRevExonView(bigPicture));
}

/***********************************************************
 *                    Group sequences menu                 *
 ***********************************************************/

/* Delete a single group */
static void blxWindowDeleteSequenceGroup(GtkWidget *blxWindow, SequenceGroup *group)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  
  if (blxContext->sequenceGroups)
    {
      blxContext->destroySequenceGroup(&group);
      blxWindowGroupsChanged(blxWindow);
    }
}


static void blxWindowDeleteAllSequenceGroups(GtkWidget *blxWindow)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  bc->deleteAllSequenceGroups();
  blxWindowGroupsChanged(blxWindow);
}


/* Update function to be called whenever groups have been added or deleted,
 * or sequences have been added to or removed from a group */
static void blxWindowGroupsChanged(GtkWidget *blxWindow)
{
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  GtkWidget *bigPicture = blxWindowGetBigPicture(blxWindow);
  
  /* Re-sort all trees, because grouping affects sort order */
  detailViewResortTrees(detailView);
  
  /* Refilter the trees (because groups affect whether sequences are visible) */
  callFuncOnAllDetailViewTrees(detailView, refilterTree, NULL);

  /* Resize exon view because transcripts may have been hidden/unhidden */
  calculateExonViewHeight(bigPictureGetFwdExonView(bigPicture));
  calculateExonViewHeight(bigPictureGetRevExonView(bigPicture));
  forceResize(bigPicture);

  /* Redraw all (because highlighting affects both big picture and detail view) */
  blxWindowRedrawAll(blxWindow);
}


/* Create a new sequence group from the given list of sequence names, with a
 * unique ID and name, and add it to the blxWindow's list of groups. The group 
 * should be destroyed with destroySequenceGroup. If ownSeqNames is true, the group
 * will take ownership of the sequence names and free them when it is destroyed. 
 * Caller can optionally provide the group name; if not provided, a default name
 * will be allocated. */
static SequenceGroup* createSequenceGroup(GtkWidget *blxWindow, 
                                          GList *seqList, 
                                          const gboolean ownSeqNames, 
                                          const char *groupName,
                                          const bool isQuickGroup = false,
                                          const bool isFilter = false,
                                          const bool highlight = true,
                                          const bool hide = false)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  
  /* Create the new group */
  SequenceGroup *group = new SequenceGroup;
  
  group->seqList = seqList;
  group->ownsSeqNames = ownSeqNames;
  group->hidden = hide;
  group->highlighted = highlight;
  group->isQuickGroup = isQuickGroup;
  group->isFilter = isFilter;
  
  /* Find a unique ID */
  GList *lastItem = g_list_last(bc->sequenceGroups);
  
  if (lastItem)
    {
      SequenceGroup *lastGroup = (SequenceGroup*)(lastItem->data);
      group->groupId = lastGroup->groupId + 1;
    }
  else
    {
      group->groupId = 1;
    }

  if (groupName)
    {
      group->groupName = g_strdup_printf("%s%d", groupName, group->groupId);
    }
  else
    {
      /* Create a default name based on the unique ID */
      std::stringstream ss;
      if (isFilter)
        ss << "Filter";
      else
        ss << "Group";
          
      ss << group->groupId;
      
      group->groupName = g_strdup(ss.str().c_str());
    }
  
  /* Set the order number. For simplicity, set the default order to be the same
   * as the ID number, so groups are sorted in the order they were added */
  group->order = group->groupId;

  /* Set the default highlight color */
  BlxColorId colorId = BLXCOLOR_GROUP;
  GdkColor *color = getGdkColor(colorId, bc->defaultColors, FALSE, bc->usePrintColors);
  group->highlightColor = *color;

  /* Add it to the list, and update */
  bc->sequenceGroups = g_list_append(bc->sequenceGroups, group);
  blxWindowGroupsChanged(blxWindow);
  
  return group;
}


/* This function sets the sequence-group-name text based on the given text entry widget */
static gboolean onGroupNameChanged(GtkWidget *widget, const gint responseId, gpointer data)
{
  gboolean result = TRUE;

  GtkEntry *entry = GTK_ENTRY(widget);
  SequenceGroup *group = (SequenceGroup*)data;
  
  const gchar *newName = gtk_entry_get_text(entry);
  
  if (!newName || strlen(newName) < 1)
    {
      g_critical("Invalid group name '%s' entered; reverting to previous group name '%s'.", newName, group->groupName);
      gtk_entry_set_text(entry, group->groupName);
      result = FALSE;
    }
  else
    {
      if (group->groupName) 
        g_free(group->groupName);
      
      group->groupName = g_strdup(newName);
    }
  
  return result;
}


/* This function is called when the sequence-group-order text entry widget's
 * value has changed. It sets the new order number in the group. */
static gboolean onGroupOrderChanged(GtkWidget *widget, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  GtkEntry *entry = GTK_ENTRY(widget);
  SequenceGroup *group = (SequenceGroup*)data;
  
  const gchar *newOrder = gtk_entry_get_text(entry);
  
  if (!newOrder || strlen(newOrder) < 1)
    {
      g_critical("Invalid order number '%s' entered; reverting to previous order number '%d'.", newOrder, group->order);
      char *orderStr = convertIntToString(group->order);
      gtk_entry_set_text(entry, orderStr);
      g_free(orderStr);
      result = FALSE;
    }
  else
    {
      group->order = convertStringToInt(newOrder);
      
      GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(widget));
      GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));

      blxWindowGroupsChanged(blxWindow);
    }
  
  return result;
}


/* This callback is called when the dialog settings are applied. It sets the filter
 * status of the passed group based on the toggle button's state */
static gboolean onGroupFilterToggled(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  SequenceGroup *group = (SequenceGroup*)data;
  group->isFilter = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));

  /* Refilter trees and redraw all immediately show the new status */
  GtkWidget *dialog = gtk_widget_get_toplevel(button);
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(GTK_WINDOW(dialog)));

  blxWindowGroupsChanged(blxWindow);
  
  return result;
}


/* This callback is called when the dialog settings are applied. It sets the hidden
 * status of the passed groupo based on the toggle button's state */
static gboolean onGroupHiddenToggled(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  SequenceGroup *group = (SequenceGroup*)data;
  group->hidden = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));

  /* Refilter trees and redraw all immediately show the new status */
  GtkWidget *dialog = gtk_widget_get_toplevel(button);
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(GTK_WINDOW(dialog)));

  blxWindowGroupsChanged(blxWindow);
  
  return result;
}


/* This callback is called when the toggle button for a group's "highlighted" flag is toggled.
 * It updates the group's highlighted flag according to the button's new status. */
static gboolean onGroupHighlightedToggled(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  SequenceGroup *group = (SequenceGroup*)data;
  group->highlighted = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
  
  /* Redraw the blixem window to immediately show the new status. The toplevel
   * parent of the button is the dialog, and the blixem window is the transient
   * parent of the dialog. */
  GtkWidget *dialog = gtk_widget_get_toplevel(button);
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(GTK_WINDOW(dialog)));
  
  blxWindowRedrawAll(blxWindow);
  
  return result;
}


/* Called when the user has changed the color of a group in the 'edit groups' dialog */
static gboolean onGroupColorChanged(GtkWidget *button, const gint responseId, gpointer data)
{  
  gboolean result = TRUE;
  
  SequenceGroup *group = (SequenceGroup*)data;
  gtk_color_button_get_color(GTK_COLOR_BUTTON(button), &group->highlightColor);
  gdk_colormap_alloc_color(gdk_colormap_get_system(), &group->highlightColor, TRUE, TRUE);
  
  /* Redraw everything in the new colors */
  GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button)));
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));
  blxWindowRedrawAll(blxWindow);
  
  return result;
}


/* This function creates a widget that allows the user to edit the group
 * pointed to by the given list item, and adds it to the table container
 * widget at the given row. */
static void createEditGroupWidget(GtkWidget *blxWindow, SequenceGroup *group, GtkTable *table, const int row, const int xpad, const int ypad)
{
  /* Show the group's name in a text box that the user can edit */
  GtkWidget *nameWidget = gtk_entry_new();
  gtk_entry_set_text(GTK_ENTRY(nameWidget), group->groupName);
  gtk_entry_set_activates_default(GTK_ENTRY(nameWidget), TRUE);
  widgetSetCallbackData(nameWidget, onGroupNameChanged, group);
      
  /* Add a check box for the 'isFilter' flag */
  GtkWidget *isFilterWidget = gtk_check_button_new();
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(isFilterWidget), group->isFilter);
  gtk_widget_set_tooltip_text(isFilterWidget, "If any Filter groups are set, Blixem will filter out features of the same type that are not in a Filter group");
  widgetSetCallbackData(isFilterWidget, onGroupFilterToggled, group);
      
  /* Add a check box for the 'hidden' flag */
  GtkWidget *isHiddenWidget = gtk_check_button_new();
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(isHiddenWidget), group->hidden);
  gtk_widget_set_tooltip_text(isHiddenWidget, "Always hide features in this group");
  widgetSetCallbackData(isHiddenWidget, onGroupHiddenToggled, group);

  /* Add a check box for the 'highlighted' flag */
  GtkWidget *isHighlightedWidget = gtk_check_button_new();
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(isHighlightedWidget), group->highlighted);
  gtk_widget_set_tooltip_text(isHighlightedWidget, "Highlight features that are in this group (use the colour selector to set the highlight color)");
  widgetSetCallbackData(isHighlightedWidget, onGroupHighlightedToggled, group);
      
  /* Show the group's order number in an editable text box */
  GtkWidget *orderWidget = gtk_entry_new();

  char *orderStr = convertIntToString(group->order);
  gtk_entry_set_text(GTK_ENTRY(orderWidget), orderStr);
  g_free(orderStr);
      
  gtk_entry_set_activates_default(GTK_ENTRY(orderWidget), TRUE);
  gtk_widget_set_size_request(orderWidget, 30, -1);
  widgetSetCallbackData(orderWidget, onGroupOrderChanged, group);

  /* Show the group's highlight color in a button that will also launch a color-picker */
  GtkWidget *colorButton = gtk_color_button_new_with_color(&group->highlightColor);
  widgetSetCallbackData(colorButton, onGroupColorChanged, group);
      
  /* Create a button that will delete this group */
  GtkWidget *deleteButton = gtk_button_new_from_stock(GTK_STOCK_DELETE);
  g_signal_connect(G_OBJECT(deleteButton), "clicked", G_CALLBACK(onButtonClickedDeleteGroup), group);
      
  /* Put everything in the table */
  gtk_table_attach(table, nameWidget,               1, 2, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, isFilterWidget,           2, 3, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, isHiddenWidget,           3, 4, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, isHighlightedWidget,      4, 5, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, orderWidget,              5, 6, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, colorButton,              6, 7, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, deleteButton,             7, 8, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
}


/* Like blxSequenceGetColumn but also supports the group column (which
 * needs the context for its data) */
static const char* blxSequenceGetColumnData(const BlxSequence* const blxSeq, 
                                            const BlxColumnId columnId,
                                            const BlxContext *bc)
{
  const char *result = NULL;

  if (columnId == BLXCOL_GROUP)
    {
      const SequenceGroup *group = bc->getFirstSequenceGroup(blxSeq);

      if (group)
        result = group->groupName;
    }
  else
    {
      result = blxSequenceGetColumn(blxSeq, columnId);
    }
  
  return result;
}
          

/* Called for an entry from a list of BlxSequences. Compares the relevant data
 * from the BlxSequence (as indicated by the searchCol member of the SeqSearchData
 * struct) to the search string (also specified in the SeqSearchData). If it
 * matches, it appends the BlxSequence to the result list in the SeqSearchData. */
static void getSequencesThatMatch(gpointer listDataItem, gpointer data)
{
  /* Get the BlxSequence for this list item */
  BlxSequence *seq = (BlxSequence*)listDataItem;
  
  /* Get the search data */
  SeqSearchData *searchData = (SeqSearchData*)data;
  
  if (searchData->error)
    return; /* already hit an error so don't try any more */

  /* Get the relevant data for the search column */
  const char *dataToCompare = blxSequenceGetColumnData(seq, searchData->searchCol, searchData->bc);
  
  if (!dataToCompare)
    {
      if (searchData->error)
        {
          /* Default error message is not that useful, so replace it */
          reportAndClearIfError(&searchData->error, G_LOG_LEVEL_DEBUG);
          g_set_error(&searchData->error, BLX_ERROR, BLX_ERROR_INVALID_COLUMN, "Invalid search column.\n");
        }
      
      return;
    }
  
  /* Do the search */
  gboolean found = wildcardSearch(dataToCompare, searchData->searchStr);

  if (searchData->searchCol == BLXCOL_SEQNAME)
    {
      /* Sequence names have a variant number postfix; if not found, try
       * to match the text without this postfix. */
      if (!found && dataToCompare)
        {
          char *seqName = g_strdup(dataToCompare);
          char *cutPoint = strchr(seqName, '.');
          
          if (cutPoint)
            {
              *cutPoint = '\0';
              found = wildcardSearch(seqName, searchData->searchStr);
            }
          
          g_free(seqName);
        }
    }
  
  if (found)
    {
      /* Add this BlxSequence onto the result list. */
      searchData->matchList = g_list_prepend(searchData->matchList, seq);
    }  
}


/* If the given radio button is enabled, add a group based on the curently-
 * selected sequences. */
static gboolean onAddGroupFromSelection(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
    {  
      GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(button));
      GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));

      BlxContext *blxContext = blxWindowGetContext(blxWindow);
      
      if (g_list_length(blxContext->selectedSeqs) > 0)
        {
          GList *list = g_list_copy(blxContext->selectedSeqs); /* group takes ownership of this */
          createSequenceGroup(blxWindow, list, FALSE, NULL);
        }
      else
        {
          result = FALSE;
          g_critical("Warning: cannot create group; no sequences are currently selected");
        }
    }
  
  return result;
}


/* If the given radio button is enabled, add a group based on the search text
 * in the given text entry. This function finds the combo box on the dialog that
 * specifies which column to search by, and searches the relevant column for
 * the given search text. */
static gboolean onAddGroupFromText(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  /* Nothing to do if this radio button is not active */
  if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
    {
      return result;
    }
  
  /* Get the dialog and main window */
  GtkWidget *dialog = gtk_widget_get_toplevel(button);
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(GTK_WINDOW(dialog)));

  /* Get the search text from the text entry widget (which is passed as user data) */
  const char *inputText = getStringFromTextEntry(GTK_ENTRY(data));

  /* Find the combo box on the dialog (there should only be one), which tells
   * us which column to search. */
  GtkComboBox *combo = widgetGetComboBox(dialog);
  BlxColumnId searchCol = getColumnFromComboBox(combo);
  
  GError *error = NULL;
  GList *seqList = findSeqsFromColumn(blxWindow, inputText, searchCol, FALSE, FALSE, &error);

  if (error)
    {
      reportAndClearIfError(&error, G_LOG_LEVEL_CRITICAL);
      result = FALSE;
    }

  if (seqList)
    {
      GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(button));
      GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));

      createSequenceGroup(blxWindow, seqList, FALSE, inputText);
    }
  
  return result;
}


/* Utility function to take a list of search strings (newline separated), and
 * return a list of BlxSequences whose column data matches one of those search
 * strings. */
static GList* getSeqStructsFromSearchStringList(GList *searchStringList, 
                                                GList *seqList, 
                                                BlxContext *bc, 
                                                const BlxColumnId searchCol,
                                                GError **error)
{
  SeqSearchData searchData = {NULL, searchCol, bc, NULL, NULL};
  
  /* Loop through all the names in the input list */
  GList *nameItem = searchStringList;
  for ( ; nameItem; nameItem = nameItem->next)
    {
      /* Compare this name to all names in the sequence list. If it matches,
       * add it to the result list. */
      searchData.searchStr = (const char*)(nameItem->data);
      g_list_foreach(seqList, getSequencesThatMatch, &searchData);
      
      if (searchData.error)
        break;
    }

  if (searchData.error)
    g_propagate_error(error, searchData.error);
  
  return searchData.matchList;
}


/* Utility to create a GList of BlxSequences from a textual list of search strings.
 * Returns only valid sequences that blixem knows about. Looks for sequences
 * whose relevent data for the given column matches an item in the input text
 * (which may be a multi-line list; one search string per line). */
static GList* getSeqStructsFromText(GtkWidget *blxWindow, const char *inputText, const BlxColumnId searchCol, GError **error)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);

  GError *tmpError = NULL;

  GList *searchStringList = parseMatchList(inputText);

  /* Extract the entries from the list that are sequences that blixem knows about */
  GList *matchSeqs = bc->matchSeqs;
  GList *seqList = getSeqStructsFromSearchStringList(searchStringList, matchSeqs, bc, searchCol, &tmpError);

  if (tmpError)
    g_propagate_error(error, tmpError);
  
  /* Must free the original name list and all its data. */
  freeStringList(&searchStringList, TRUE);
  
  if (g_list_length(seqList) < 1)
    {
      g_list_free(seqList);
      seqList = NULL;
    }
  
  return seqList;
}


/* Create a group/filter from features on the clipboard. Adds to an existing quick group/filter
 * if exists, otherwise creates one. */
static void createGroupOrFilterFromClipboard(GtkClipboard *clipboard, 
                                             const char *clipboardText, 
                                             const bool isFilter,
                                             gpointer data)
{
  /* Get the list of sequences to include */
  GtkWidget *blxWindow = GTK_WIDGET(data);
  GList *seqList = getSeqStructsFromText(blxWindow, clipboardText, BLXCOL_SEQNAME, NULL);
  
  if (seqList)
    {
      /* See if there's already a quick group/filter */
      BlxContext *blxContext = blxWindowGetContext(blxWindow);
      SequenceGroup *group = blxContext->getQuickGroup(isFilter);

      if (group)
        {
          group->seqList = g_list_concat(group->seqList, seqList);
          blxWindowGroupsChanged(blxWindow);
        }
      else
        {
          createSequenceGroup(blxWindow, seqList, FALSE, NULL, true, isFilter, !isFilter, false);
        }

      refreshDialog(BLXDIALOG_GROUPS, blxWindow);
    }
}


/* Callback function to be used when requesting text from the clipboard to be used
 * to create a group from the paste text */
static void createGroupFromClipboard(GtkClipboard *clipboard, const char *clipboardText, gpointer data)
{
  createGroupOrFilterFromClipboard(clipboard, clipboardText, false, data) ;
}

/* Callback function to be used when requesting text from the clipboard to be used
 * to create a filter from the paste text */
static void createFilterFromClipboard(GtkClipboard *clipboard, const char *clipboardText, gpointer data)
{
  createGroupOrFilterFromClipboard(clipboard, clipboardText, true, data) ;
}



/* Callback function to be used when requesting text from the clipboard to be used
 * to find sequences based on the paste text. Scrolls the first selected
 * match into view. */
void findSeqsFromClipboard(GtkClipboard *clipboard, const char *clipboardText, gpointer data)
{
  /* Get the list of sequences to include */
  GtkWidget *blxWindow = GTK_WIDGET(data);
  GList *seqList = getSeqStructsFromText(blxWindow, clipboardText, BLXCOL_SEQNAME, NULL);
  
  if (seqList)
    {
      blxWindowSetSelectedSeqList(blxWindow, seqList);
    }
}


/* Callback function to be used when requesting text from the clipboard to be used
 * to find and select sequences based on the paste text. Scrolls the first selected
 * match into view. */
void findAndSelectSeqsFromClipboard(GtkClipboard *clipboard, const char *clipboardText, gpointer data)
{
  /* Get the list of sequences to include */
  GtkWidget *blxWindow = GTK_WIDGET(data);
  GList *seqList = getSeqStructsFromText(blxWindow, clipboardText, BLXCOL_SEQNAME, NULL);
  
  if (seqList)
    {
      blxWindowSetSelectedSeqList(blxWindow, seqList);
      firstMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
    }
}


static void hideSelectedSources(GtkWidget *blxWindow, const bool refresh = true)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  g_return_if_fail(blxContext);

  std::set<GQuark> sourceList = blxContext->getSelectedSources();
  GList *seqList = blxContext->getFeaturesInSourceList(sourceList);

  if (seqList)
    {
      createSequenceGroup(blxWindow, seqList, FALSE, "HideSource", false, false, false, true);
  
      if (refresh)
        {
          blxWindowGroupsChanged(blxWindow);
          refreshDialog(BLXDIALOG_GROUPS, blxWindow);
        }
    }
}


/* Disable all groups/filters */
static void clearGroups(GtkWidget *blxWindow, const bool refresh = true)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);

  blxContext->disableAllGroups();
  
  if (refresh)
    {
      blxWindowGroupsChanged(blxWindow);
      refreshDialog(BLXDIALOG_GROUPS, blxWindow);
    }
}


/* This function creates a group (or filter, if filter=true) from features 
 * on the clipboard text (which should contain valid sequence name(s)). */
static void createQuickGroup(GtkWidget *blxWindow, const bool isFilter, const bool clearPrevious)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);

  if (clearPrevious)
    blxContext->disableAllQuickGroups();
  
  if (isFilter)
    requestPrimaryClipboardText(createFilterFromClipboard, blxWindow);
  else
    requestPrimaryClipboardText(createGroupFromClipboard, blxWindow);
}


/* If the given radio button is enabled, add a group based on the list of sequences
 * in the given text entry. */
static gboolean onAddGroupFromList(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
    {
      return result;
    }
  
  /* Get the dialog and main window */
  GtkWidget *dialog = gtk_widget_get_toplevel(button);
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(GTK_WINDOW(dialog)));
  
  /* Find the combo box on the dialog (there should only be one), which tells
   * us which column to search. */
  GtkComboBox *combo = widgetGetComboBox(dialog);
  BlxColumnId searchCol = getColumnFromComboBox(combo);
  
  /* The text entry box was passed as the user data. We should have a (multi-line) text view */
  const char *inputText = getStringFromTextView(GTK_TEXT_VIEW(data));

  GError *error = NULL;
  GList *seqList = findSeqsFromList(blxWindow, inputText, searchCol, FALSE, FALSE, &error);
  
  if (error)
    {
      reportAndClearIfError(&error, G_LOG_LEVEL_CRITICAL);
      result = FALSE;
    }
  
  if (seqList)
    {
      GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(button));
      GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));

      createSequenceGroup(blxWindow, seqList, FALSE, NULL);
    }
  
  return result;
}


/* Called when the user has clicked the "delete all groups" button on the "group sequences" dialog. */
static void onButtonClickedDeleteAllGroups(GtkWidget *button, gpointer data)
{
  GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(button));
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));

  /* Ask the user if they're sure */
  BlxContext *bc = blxWindowGetContext(blxWindow);
  char *title = g_strdup_printf("%sDelete groups", blxGetTitlePrefix(bc));
  gint response = runConfirmationBox(blxWindow, title, "This will delete ALL groups. Are you sure?");
  g_free(title);

  if (response == GTK_RESPONSE_ACCEPT)
    {
      blxWindowDeleteAllSequenceGroups(blxWindow);
      
      /* Close the dialog, because there are no groups left to display. */
      gtk_widget_hide_all(GTK_WIDGET(dialogWindow));
    }
}


/* Called when the user has clicked the "delete group" button on the "group sequences" dialog. */
static void onButtonClickedDeleteGroup(GtkWidget *button, gpointer data)
{
  SequenceGroup *group = (SequenceGroup*)data;
  
  GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(button));
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));
  
  /* Ask the user if they're sure */
  char formatStr[] = "Are you sure you wish to delete group '%s'?";
  char messageText[strlen(formatStr) + strlen(group->groupName)];
  sprintf(messageText, formatStr, group->groupName);
  
  BlxContext *bc = blxWindowGetContext(blxWindow);
  char *title = g_strdup_printf("%sDelete group", blxGetTitlePrefix(bc));
  gint response = runConfirmationBox(blxWindow, title, messageText);
  g_free(title);
  
  if (response == GTK_RESPONSE_ACCEPT)
    {
      blxWindowDeleteSequenceGroup(blxWindow, group);
      refreshDialog(BLXDIALOG_GROUPS, blxWindow);
    }
}


/* Called when the user chooses a different tabe on the groups dialog */
static gboolean onSwitchPageGroupsDialog(GtkNotebook *notebook, GtkNotebookPage *page, guint pageNum, gpointer data)
{
  GtkDialog *dialog = GTK_DIALOG(data);

  if (pageNum == 0)
    {
      /* For the create-groups page, set the default response to be 'accept' */
      gtk_dialog_set_default_response(dialog, GTK_RESPONSE_ACCEPT);
    }
  else
    {
      /* For other pages, set default response to be 'apply' */
      gtk_dialog_set_default_response(dialog, GTK_RESPONSE_APPLY);
    }
    
  return FALSE;
}


/* Utility to find and return a notebook child of the given widget. Assumes there is only
 * one - if there are more it will just return the first found. Returns NULL if not found. */
static GtkNotebook* containerGetChildNotebook(GtkContainer *container)
{
  GtkNotebook *result = NULL;
  
  GList *children = gtk_container_get_children(container);
  GList *child = children;
  
  for ( ; child; child = child->next)
    {
      GtkWidget *childWidget = GTK_WIDGET(child->data);
      
      if (GTK_IS_NOTEBOOK(childWidget))
        {
          result = GTK_NOTEBOOK(childWidget);
          break;
        }
      else if (GTK_IS_CONTAINER(childWidget))
       {
         /* recurse */
         containerGetChildNotebook(GTK_CONTAINER(childWidget));
       }
    }

  g_list_free(children);
 
  return result;
}


/* Callback called when user responds to groups dialog */
void onResponseGroupsDialog(GtkDialog *dialog, gint responseId, gpointer data)
{
  gboolean destroy = FALSE;
  gboolean refresh = FALSE;
  
  /* If a notebook was passed, only call callbacks for widgets in the active tab */
  GtkNotebook *notebook = containerGetChildNotebook(GTK_CONTAINER(dialog->vbox));
               
  if (!notebook)
    {
      g_warning("Expected Groups dialog to contain a notebook widget. Dialog may not refresh properly.\n");
    }

  guint pageNo = notebook ? gtk_notebook_get_current_page(notebook) : 0;
  GtkWidget *page = notebook ? gtk_notebook_get_nth_page(notebook, pageNo) : NULL;
  
  switch (responseId)
  {
    case GTK_RESPONSE_ACCEPT:
      destroy = widgetCallAllCallbacks(page, GINT_TO_POINTER(responseId));
      refresh = FALSE;
      break;

    case GTK_RESPONSE_APPLY:
      widgetCallAllCallbacks(page, GINT_TO_POINTER(responseId));
      destroy = FALSE;
      refresh = (pageNo == 0); /* if created a new group, Edit Groups section must be refreshed */
      break;
      
    case GTK_RESPONSE_CANCEL:
    case GTK_RESPONSE_REJECT:
      destroy = TRUE;
      refresh = FALSE;
      break;
      
    default:
      break;
  };
  
  if (destroy)
    {
      /* Groups dialog is persistent, so hide it rather than destroying it */
      gtk_widget_hide_all(GTK_WIDGET(dialog));
    }
  else if (refresh)
    {
      GtkWidget *blxWindow = GTK_WIDGET(data);
      refreshDialog(BLXDIALOG_GROUPS, blxWindow);
    }
}


/* Create the 'create group' tab of the groups dialog. Appends it to the notebook. */
static void createCreateGroupTab(GtkNotebook *notebook, BlxContext *bc, GtkWidget *blxWindow)
{
  const int numRows = 3;
  const int numCols = 2;
  const gboolean seqsSelected = g_list_length(bc->selectedSeqs) > 0;
  
  /* Put everything in a table */
  GtkTable *table = GTK_TABLE(gtk_table_new(numRows, numCols, FALSE));

  /* Append the table as a new tab to the notebook */
  gtk_notebook_append_page(notebook, GTK_WIDGET(table), gtk_label_new("Create group"));
  
  /* Create the left-hand-side column */
  GtkRadioButton *button1 = createRadioButton(table, 1, 1, NULL, "_Text search (wildcards * and ?)", !seqsSelected, TRUE, FALSE, onAddGroupFromText, blxWindow, NULL);
  createRadioButton(table, 1, 2, button1, "_List search", FALSE, TRUE, TRUE, onAddGroupFromList, blxWindow, NULL);
  createSearchColumnCombo(table, 1, 3, blxWindow);
  
  /* Create the right-hand-side column */
  createRadioButton(table, 2, 1, button1, "Use current _selection", seqsSelected, FALSE, FALSE, onAddGroupFromSelection, blxWindow, NULL);
}


/* Create the 'edit groups' tab of the groups dialog. Appends it to the given notebook. */
static void createEditGroupsTab(GtkNotebook *notebook, BlxContext *bc, GtkWidget *blxWindow)
{
  const int numRows = g_list_length(bc->sequenceGroups) + 4; /* +4 for: header; delete-all button;
                                                                hide-all-seqs; hide-all-features */
  const int numCols = 7;
  const int xpad = DEFAULT_TABLE_XPAD;
  const int ypad = DEFAULT_TABLE_YPAD;
  int row = 1;
  
  /* Put everything in a table inside a scrolled window */
  GtkTable *table = GTK_TABLE(gtk_table_new(numRows, numCols, FALSE));
  GtkWidget *scrollWin = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollWin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollWin), GTK_WIDGET(table));
  
  /* Append the table as a new tab to the notebook */
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), GTK_WIDGET(scrollWin), gtk_label_new("Edit groups"));
  
  /* Add labels for each column in the table */
  gtk_table_attach(table, gtk_label_new("Group name"),    1, 2, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, gtk_label_new("Filter"),        2, 3, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, gtk_label_new("Hide"),          3, 4, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, gtk_label_new("Highlight"),     4, 5, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, gtk_label_new("Order"),         5, 6, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  ++row;
  
  /* Add a set of widgets for each group */
  GList *groupItem = blxWindowGetSequenceGroups(blxWindow);
  for ( ; groupItem; groupItem = groupItem->next)
    {
      SequenceGroup *group = (SequenceGroup*)(groupItem->data);
      createEditGroupWidget(blxWindow, group, table, row, xpad, ypad);
      ++row;
    }
  
  /* Add a button to delete all groups */
  GtkWidget *deleteGroupsButton = gtk_button_new_with_label("Delete all groups");
  gtk_button_set_image(GTK_BUTTON(deleteGroupsButton), gtk_image_new_from_stock(GTK_STOCK_DELETE, GTK_ICON_SIZE_BUTTON));
  gtk_widget_set_size_request(deleteGroupsButton, -1, 30);
  g_signal_connect(G_OBJECT(deleteGroupsButton), "clicked", G_CALLBACK(onButtonClickedDeleteAllGroups), NULL);
  gtk_table_attach(table, deleteGroupsButton, numCols - 1, numCols + 1, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), GTK_SHRINK, xpad, ypad);
}


/* Shows the "Group sequences" dialog. This dialog allows the user to group sequences together.
 * This tabbed dialog shows both the 'create group' and 'edit groups' dialogs in one. If the
 * 'editGroups' argument is true and groups exist, the 'Edit Groups' tab is displayed by default;
 * otherwise the 'Create Groups' tab is shown. */
void showGroupsDialog(GtkWidget *blxWindow, const gboolean editGroups, const gboolean bringToFront)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  const BlxDialogId dialogId = BLXDIALOG_GROUPS;
  GtkWidget *dialog = getPersistentDialog(bc->dialogList, dialogId);
  
  if (!dialog)
    {
      char *title = g_strdup_printf("%sGroups", blxGetTitlePrefix(bc));

      dialog = gtk_dialog_new_with_buttons(title,
                                           GTK_WINDOW(blxWindow), 
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_STOCK_CANCEL,
                                           GTK_RESPONSE_REJECT,
                                           GTK_STOCK_APPLY,
                                           GTK_RESPONSE_APPLY,
                                           GTK_STOCK_OK,
                                           GTK_RESPONSE_ACCEPT,
                                           NULL);

      g_free(title);
      
      /* These calls are required to make the dialog persistent... */
      addPersistentDialog(bc->dialogList, dialogId, dialog);
      g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
      
      /* Make sure we only connect the response event once */
      g_signal_connect(dialog, "response", G_CALLBACK(onResponseGroupsDialog), blxWindow);
    }
  else
    {
      /* Refresh by deleting the dialog contents and re-creating them. */
      dialogClearContentArea(GTK_DIALOG(dialog));
    }
  
  /* Create tabbed pages */
  GtkWidget *notebook = gtk_notebook_new();
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), notebook, TRUE, TRUE, 0);

  createCreateGroupTab(GTK_NOTEBOOK(notebook), bc, blxWindow);
  createEditGroupsTab(GTK_NOTEBOOK(notebook), bc, blxWindow);

  
  /* Connect signals and show */
  gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(blxWindow));
  g_signal_connect(notebook, "switch-page", G_CALLBACK(onSwitchPageGroupsDialog), dialog);
  
  int width = 450, height = 300;
  int maxwidth = width, maxheight = height;
  gbtools::GUIGetTrueMonitorSizeFraction(dialog, 0.33, 0.33, &maxwidth, &maxheight);
  gtk_window_set_default_size(GTK_WINDOW(dialog), std::min(width, maxwidth), std::min(height, maxheight));

  gtk_widget_show_all(dialog);

  if (editGroups && notebook && blxWindowGroupsExist(blxWindow))
    {
      gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 1); /* 'edit' page is the 2nd page */
    }
  else
    {
      gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 0); /* 'create' page is the 1st page */
    }
  
  if (bringToFront)
    {
      /* If user has asked to edit groups (and some groups exist), make the second tab
       * the default and the 'close' button the default action. (Must do this after showing
       * the child widgets due to a GTK legacy whereby the notebook won't change tabs otherwise.) */
      gtk_window_present(GTK_WINDOW(dialog));
    }
}


/***********************************************************
 *                         Settings menu                   *
 ***********************************************************/

/* This function should be called on the child widget of a dialog box that is a transient
 * child of the main blixem window. It finds the parent dialog of the child and then finds
 * the blxWindow from the dialog. */
static GtkWidget* dialogChildGetBlxWindow(GtkWidget *child)
{
  GtkWidget *result = NULL;
  
  GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(child));
  
  if (dialogWindow)
    {
      result = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));
    }
  
  return result;
}


/* Updates the given flag from the given button. The passed in widget is the toggle button and
 * the data is an enum indicating which flag was toggled. Returns the new value that was set. */
static gboolean setFlagFromButton(GtkWidget *button, gpointer data)
{
  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  BlxContext *bc = blxWindowGetContext(blxWindow);
  
  BlxFlag flag = (BlxFlag)GPOINTER_TO_INT(data);
  const gboolean newValue = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));

  if (flag > BLXFLAG_MIN && flag < BLXFLAG_NUM_FLAGS)
    bc->flags[flag] = newValue;
  
  return newValue;
}


/* This callback is called when one of the boolean flags is toggled on the settings dialog.
 * This generic function sets the flag and redraws everything; if different 
 * updates are required a custom callback function can be used instead. */
static void onToggleFlag(GtkWidget *button, gpointer data)
{
  setFlagFromButton(button, data);
  
  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  blxWindowRedrawAll(blxWindow);
}


/* Callback function called when the 'squash matches' button is toggled */
static void onSquashMatches(GtkWidget *button, gpointer data)
{
  const gboolean squash = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));

  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);
  
  setToggleMenuStatus(properties->actionGroup, "SquashMatches", squash);
}


/* Callback function called when the 'Show Variation track' button is toggled */
static void onShowVariationTrackToggled(GtkWidget *button, gpointer data)
{
  const gboolean showSnpTrack = setFlagFromButton(button, data);
  
  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  
  detailViewUpdateShowSnpTrack(detailView, showSnpTrack);
}


/* Utility to create a check button with certain given properties, and to pack it into the parent.
 * Returns the new button. */
static GtkWidget* createCheckButton(GtkBox *box, 
                                    const char *mnemonic, 
                                    const char *tooltip,
                                    const gboolean isActive, 
                                    GCallback callback, 
                                    gpointer data)
{
  GtkWidget *button = gtk_check_button_new_with_mnemonic(mnemonic);
  gtk_box_pack_start(box, button, FALSE, FALSE, 0);
  
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), isActive);
  
  g_signal_connect(G_OBJECT(button), "toggled", callback, data);

  if (tooltip)
    gtk_widget_set_tooltip_text(button, tooltip);
  
  return button;
}


/* Callback to be called when the user has entered a new column size */
static gboolean onColumnSizeChanged(GtkWidget *widget, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  GtkEntry *entry = GTK_ENTRY(widget);
  BlxColumnInfo *columnInfo = (BlxColumnInfo*)data;
  
  const gchar *newSizeText = gtk_entry_get_text(entry);
  const int newWidth = convertStringToInt(newSizeText);
  
  if (newWidth != columnInfo->width)
    {
      /* Check it's a sensible value. We could do with a better check really but
       * for now just check that it's less than the screen width. This at least
       * catches excessively large values, which can cause Blixem to crash. Slightly
       * too-large values may make things look odd but should be recoverable. */
      GtkWidget *blxWindow = dialogChildGetBlxWindow(widget);
      
      int maxWidth = 300;
      gbtools::GUIGetTrueMonitorSize(blxWindow, &maxWidth, NULL);

      if (newWidth > maxWidth)
        {
          g_critical("Column width '%d' too large; not changed.\n", newWidth);
        }
      else
        {
          columnInfo->width = newWidth;

          GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
          updateDynamicColumnWidths(detailView);
        }
    }
  
  return result;
}


/* Callback to be called when the user has toggled the visibility of a column */
static gboolean onColumnVisibilityChanged(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  BlxColumnInfo *columnInfo = (BlxColumnInfo*)data;
  columnInfo->showColumn = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
  
  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);

  updateDynamicColumnWidths(detailView);
  
  return result;
}


/* Callback to be called when the user has toggled which columns are included in summary info */
static gboolean onSummaryColumnsChanged(GtkWidget *button, const gint responseId, gpointer data)
{
  gboolean result = TRUE;
  
  BlxColumnInfo *columnInfo = (BlxColumnInfo*)data;
  columnInfo->showSummary = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
  
  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);

  /* Just clear the moused-over feedback area to make sure it's not showing invalid data. The
   * user can mouse-over again to see the new data. */
  clearFeedbackArea(detailView);
  
  return result;
}


/* Just calls gtk_widget_set_sensitive, but so that we can use it as a callback */
static void widgetSetSensitive(GtkWidget *widget, gpointer data)
{
  gboolean sensitive = GPOINTER_TO_INT(data);
  gtk_widget_set_sensitive(widget, sensitive);
}


/* Callback when the user hits the 'load optional data' button on the settings dialog */
static void onButtonClickedLoadOptional(GtkWidget *button, gpointer data)
{
  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  BlxContext *bc = blxWindowGetContext(blxWindow);

  /* Create a temporary lookup table for BlxSequences so we can link them on GFF ID */
  GHashTable *lookupTable = g_hash_table_new(g_direct_hash, g_direct_equal);
  
  GError *error = NULL;
  BulkFetch bulk_fetch(bc->external, bc->flags[BLXFLAG_SAVE_TEMP_FILES],
                       bc->seqType, &bc->matchSeqs, bc->columnList, bc->optionalFetchDefault, bc->fetchMethods, &bc->mspList,
                       &bc->blastMode, bc->featureLists, bc->supportedTypes, NULL, bc->refSeqOffset,
                       &bc->refSeqRange, bc->dataset, TRUE, lookupTable,
#ifdef PFETCH_HTML
                       bc->ipresolve,
                       bc->cainfo,
#endif
                       bc->fetch_debug);
  
  gboolean success = bulk_fetch.performFetch();

  finaliseFetch(bc->matchSeqs, bc->columnList);

  if (error)
    {
      prefixError(error, "Error loading optional data. ");
      reportAndClearIfError(&error, G_LOG_LEVEL_CRITICAL);
    }
  
  if (success)
    {
      /* Set the flag to say that the data has now been loaded */
      bc->flags[BLXFLAG_OPTIONAL_COLUMNS] = TRUE;
      
      /* Disable the button so user can't try to load data again. */
      gtk_widget_set_sensitive(button, FALSE);
      
      /* Enable the text entry boxes for all columns. They are all in the container passed
       * as the user data. */
      GtkContainer *container = GTK_CONTAINER(data);
      gtk_container_foreach(container, widgetSetSensitive, GINT_TO_POINTER(TRUE));
      
      /* Update the flag in all columns to indicate that the data is now loaded */
      GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
      GList *listItem = detailViewGetColumnList(detailView);
      
      for ( ; listItem; listItem = listItem->next)
        {
          BlxColumnInfo *columnInfo = (BlxColumnInfo*)(listItem->data);
          columnInfo->dataLoaded = TRUE;
        }   
      
      /* Re-sort the trees, because the new data may affect the sort order. Also
       * resize them, because whether data is loaded affects whether columns are shown. */
      detailViewResortTrees(detailView);
      updateDynamicColumnWidths(detailView);

      /* Force a of resize the tree columns (updateDynamicColumnWidths won't resize them
       * because the widths and visibility-flags haven't actually changed, but visibility
       * IS affected because we've now loaded the data) */
      callFuncOnAllDetailViewTrees(detailView, resizeTreeColumns, NULL);
      resizeDetailViewHeaders(detailView);
      updateDetailViewRange(detailView);
      
      detailViewRedrawAll(detailView);
    }

  g_hash_table_unref(lookupTable);
}


/* Create a button to allow the user to load the data for optional columns, if not already loaded */
static GtkWidget* createColumnLoadDataButton(GtkTable *table, 
                                             GtkWidget *detailView,
                                             int *row,
                                             const int cols,
                                             int xpad,
                                             int ypad)
{
  BlxContext *bc = blxWindowGetContext(detailViewGetBlxWindow(detailView));
  const gboolean dataLoaded = bc->flags[BLXFLAG_OPTIONAL_COLUMNS];

  /* Create the button */
  GtkWidget *button = gtk_button_new_with_label(LOAD_DATA_TEXT);
  gtk_widget_set_sensitive(button, !dataLoaded); /* only enable if data not yet loaded */

  /* Add the button to the table, spanning all of the remaining columns */
  gtk_table_attach(table, button, 0, 1, *row, *row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  
  /* Create an explanatory label spanning the rest of the columns */
  GtkWidget *label = gtk_label_new("Fetches additional information e.g. from an\nEMBL file (as determined by the config).");
  gtk_table_attach(table, label, 1, cols, *row, *row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);

  *row += 1;

  return button;
}


/* Create the settings buttons for a single column */
static void createColumnButton(BlxColumnInfo *columnInfo, GtkTable *table, int *row)
{
  /* Create a label showing the column name */
  GtkWidget *label = gtk_label_new(columnInfo->title);
  gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
  
  /* Tick-box controlling whether column is displayed */
  GtkWidget *showColButton = gtk_check_button_new();
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(showColButton), columnInfo->showColumn);
  widgetSetCallbackData(showColButton, onColumnVisibilityChanged, (gpointer)columnInfo);
  gtk_widget_set_tooltip_text(showColButton, "Whether to display this column");

  /* Tick-box controlling whether column is included in summary info */
  GtkWidget *showSummaryButton = gtk_check_button_new();
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(showSummaryButton), columnInfo->showSummary);
  widgetSetCallbackData(showSummaryButton, onSummaryColumnsChanged, (gpointer)columnInfo);
  gtk_widget_set_tooltip_text(showSummaryButton, "Whether to include this data in the moused-over-item feedback area");
  
  GtkWidget *entry = gtk_entry_new();
  gtk_widget_set_tooltip_text(label, "The column width, in pixels");
  
  if (columnInfo->columnId == BLXCOL_SEQUENCE)
    {
      /* The sequence column updates dynamically, so don't allow the user to edit it */
      char displayText[] = "<dynamic>";
      gtk_entry_set_text(GTK_ENTRY(entry), displayText);
      gtk_widget_set_sensitive(entry, FALSE);
      gtk_widget_set_sensitive(showSummaryButton, FALSE);
      gtk_widget_set_sensitive(showSummaryButton, FALSE);
      gtk_entry_set_width_chars(GTK_ENTRY(entry), strlen(displayText) + 2); /* fudge up width a bit in case user enters longer text */
    }
  else
    {
      /* Grey out all the options if the column hasn't been loaded */
      if (!columnInfo->dataLoaded)
        {
          gtk_widget_set_sensitive(showColButton, FALSE);
          gtk_widget_set_sensitive(showSummaryButton, FALSE);
          gtk_widget_set_sensitive(entry, FALSE);
        }
      else if (!columnInfo->canShowSummary)
        {
          gtk_widget_set_sensitive(showSummaryButton, FALSE);          
        }

      char *displayText = convertIntToString(columnInfo->width);
      gtk_entry_set_text(GTK_ENTRY(entry), displayText);
      
      gtk_entry_set_width_chars(GTK_ENTRY(entry), strlen(displayText) + 2);
      gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
      
      widgetSetCallbackData(entry, onColumnSizeChanged, (gpointer)columnInfo);
      
      g_free(displayText);
    }
  
  gtk_table_attach(table, label,             0, 1, *row, *row + 1, GTK_FILL, GTK_SHRINK, 4, 4);
  gtk_table_attach(table, showColButton,     1, 2, *row, *row + 1, GTK_FILL, GTK_SHRINK, 4, 4);
  gtk_table_attach(table, showSummaryButton, 2, 3, *row, *row + 1, GTK_FILL, GTK_SHRINK, 4, 4);
  gtk_table_attach(table, entry,             3, 4, *row, *row + 1, GTK_FILL, GTK_SHRINK, 4, 4);
  *row += 1;
}


/* Create labels for the column properties widgets created by createColumnButton */
static void createColumnButtonHeaders(GtkTable *table, int *row)
{
  GtkWidget *label = gtk_label_new("Show\ncolumn");
  gtk_misc_set_alignment(GTK_MISC(label), 0.0, 1.0);
  gtk_table_attach(table, label, 1, 2, *row, *row + 1, GTK_FILL, GTK_SHRINK, 4, 4);
  gtk_widget_set_tooltip_text(label, "Whether to display this column");

  label = gtk_label_new("Show mouse-\nover details");
  gtk_misc_set_alignment(GTK_MISC(label), 0.0, 1.0);
  gtk_table_attach(table, label, 2, 3, *row, *row + 1, GTK_FILL, GTK_SHRINK, 4, 4);
  gtk_widget_set_tooltip_text(label, "Whether to include this data in the moused-over-item feedback area");

  label = gtk_label_new("Column width");
  gtk_misc_set_alignment(GTK_MISC(label), 0.0, 1.0);
  gtk_table_attach(table, label, 3, 4, *row, *row + 1, GTK_FILL, GTK_SHRINK, 4, 4);
  gtk_widget_set_tooltip_text(label, "The column width, in pixels");

  *row += 1;
}

/* Create a set of widgets that allow columns settings to be adjusted */
static void createColumnButtons(GtkWidget *parent, GtkWidget *detailView, const int border)
{
  /* put all the column settings in a table. Put the table in a
   * scrolled window, because there are likely to be many rows */
  GtkWidget *scrollWin = gtk_scrolled_window_new(NULL, NULL);
  gtk_container_add(GTK_CONTAINER(parent), scrollWin);
  
  GList *columnList = detailViewGetColumnList(detailView);
  const int rows = g_list_length(columnList) + 1;
  const int cols = 4;
  int row = 1;

  GtkTable *table = GTK_TABLE(gtk_table_new(rows, cols, FALSE));  

  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollWin), GTK_WIDGET(table));
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollWin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  /* Create a button to allow the user to load the full EMBL data, if not already loaded */
  GtkWidget *button = createColumnLoadDataButton(table, detailView, &row, cols, border, border);
  
  /* Loop through each column and create widgets to control the properties */
  createColumnButtonHeaders(table, &row);

  GList *listItem = columnList;
  for ( ; listItem; listItem = listItem->next)
    {
      BlxColumnInfo *columnInfo = (BlxColumnInfo*)(listItem->data);
      createColumnButton(columnInfo, table, &row);
    }
  
  g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(onButtonClickedLoadOptional), table);
}


/* Callback to be called when the user has entered a new percent-ID per cell */
static gboolean onIdPerCellChanged(GtkWidget *widget, const gint responseId, gpointer data)
{
  GtkWidget *bigPicture = GTK_WIDGET(data);  
  const char *text = gtk_entry_get_text(GTK_ENTRY(widget));
  const gdouble newValue = g_strtod(text, NULL);
  return bigPictureSetIdPerCell(bigPicture, newValue);
}

 
/* Callback to be called when the user has entered a new maximum percent-ID to display */
static gboolean onMaxPercentIdChanged(GtkWidget *widget, const gint responseId, gpointer data)
{
  GtkWidget *bigPicture = GTK_WIDGET(data);  
  const char *text = gtk_entry_get_text(GTK_ENTRY(widget));
  const gdouble newValue = g_strtod(text, NULL);
  return bigPictureSetMaxPercentId(bigPicture, newValue);
}

/* Callback to be called when the user has entered a new minimum percent-ID to display */
static gboolean onMinPercentIdChanged(GtkWidget *widget, const gint responseId, gpointer data)
{
  GtkWidget *bigPicture = GTK_WIDGET(data);  
  const char *text = gtk_entry_get_text(GTK_ENTRY(widget));
  const gdouble newValue = g_strtod(text, NULL);
  return bigPictureSetMinPercentId(bigPicture, newValue);
}


/* Callback to be called when the user has changed the depth-per-cell on the coverage view */
static gboolean onDepthPerCellChanged(GtkWidget *widget, const gint responseId, gpointer data)
{
  CoverageViewProperties *coverageViewP = (CoverageViewProperties*)data;  
  const char *text = gtk_entry_get_text(GTK_ENTRY(widget));
  const gdouble newValue = g_strtod(text, NULL);
  return coverageViewP->setDepthPerCell(newValue);
}


///* Utility to create a text entry widget displaying the given integer value. The
// * given callback will be called when the user OK's the dialog that this widget 
// * is a child of. */
//static void createTextEntryFromInt(GtkWidget *parent, 
//                                 const char *title, 
//                                 const int value, 
//                                 BlxResponseCallback callbackFunc, 
//                                 gpointer callbackData)
//{
//  /* Pack label and text entry into a vbox */
//  GtkWidget *vbox = createVBoxWithBorder(parent, 4, FALSE, NULL);
//
//  GtkWidget *label = gtk_label_new(title);
//  gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
//
//  GtkWidget *entry = gtk_entry_new();
//  gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
//
//  char *displayText = convertIntToString(value);
//  gtk_entry_set_text(GTK_ENTRY(entry), displayText);
//
//  gtk_entry_set_width_chars(GTK_ENTRY(entry), strlen(displayText) + 2);
//  gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
//
//  widgetSetCallbackData(entry, callbackFunc, callbackData);
//  
//  g_free(displayText);
//}


/* Utility to create a text entry widget displaying the given double value. The
 * given callback will be called when the user OK's the dialog that this widget 
 * is a child of. */
static void createTextEntry(GtkWidget *parent, 
                            const char *title, 
                            const gdouble value, 
                            BlxResponseCallback callbackFunc, 
                            gpointer callbackData)
{
  /* Pack label and text entry into a vbox */
  GtkWidget *vbox = createVBoxWithBorder(parent, 4, FALSE, NULL);
  
  GtkWidget *label = gtk_label_new(title);
  gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
  
  GtkWidget *entry = gtk_entry_new();
  gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
  
  char *displayText = convertDoubleToString(value, 1);
  gtk_entry_set_text(GTK_ENTRY(entry), displayText);
  
  gtk_entry_set_width_chars(GTK_ENTRY(entry), strlen(displayText) + 2);
  gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
  
  widgetSetCallbackData(entry, callbackFunc, callbackData);
  
  g_free(displayText);
}


/* Create a set of widgets to allow the user to edit grid properties */
static void createGridSettingsButtons(GtkWidget *parent, GtkWidget *bigPicture)
{
  /* Group these buttons in a frame */
  GtkWidget *frame = gtk_frame_new("Overview section");
  gtk_box_pack_start(GTK_BOX(parent), frame, FALSE, FALSE, 0);

  /* Arrange the widgets horizontally */
  GtkWidget *hbox = createHBoxWithBorder(frame, 12, FALSE, NULL);
  const DoubleRange* const percentIdRange = bigPictureGetPercentIdRange(bigPicture);
  
  createTextEntry(hbox, "%ID per cell", bigPictureGetIdPerCell(bigPicture), onIdPerCellChanged, bigPicture);
  createTextEntry(hbox, "Max %ID", percentIdRange->max, onMaxPercentIdChanged, bigPicture);
  createTextEntry(hbox, "Min %ID", percentIdRange->min, onMinPercentIdChanged, bigPicture);
}


/* Create a set of widgets to allow the user to edit coverage-view properties */
static void createCoverageSettingsButtons(GtkWidget *parent, GtkWidget *bigPicture, GtkWidget *detailView)
{
  BigPictureProperties *bpProperties = bigPictureGetProperties(bigPicture);
  DetailViewProperties *dvProperties = detailViewGetProperties(detailView);
  
  /* Group these buttons in a frame */
  GtkWidget *frame = gtk_frame_new("Coverage section");
  gtk_box_pack_start(GTK_BOX(parent), frame, FALSE, FALSE, 0);
  
  /* Arrange the widgets horizontally */
  GtkWidget *hbox = createHBoxWithBorder(frame, 12, FALSE, NULL);
  
  createTextEntry(hbox, "Depth per cell (big picture)", 
                  bpProperties->coverageViewProperties()->depthPerCell(),
                  onDepthPerCellChanged, bpProperties->coverageViewProperties());

  createTextEntry(hbox, "Depth per cell (detail view)", 
                  dvProperties->coverageViewProperties()->depthPerCell(),
                  onDepthPerCellChanged, dvProperties->coverageViewProperties());
}


/* Callback called when user has changed a blixem color */
static gboolean onChangeBlxColor(GtkWidget *button, const gint responseId, gpointer data)
{
  GdkColor *color = (GdkColor*)data;
  
  /* update the color */
  gtk_color_button_get_color(GTK_COLOR_BUTTON(button), color);
  gdk_colormap_alloc_color(gdk_colormap_get_system(), color, TRUE, TRUE);
  
  /* Redraw */
  GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(button));
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));
  blxWindowRedrawAll(blxWindow);
  
  return TRUE;
}


/* Callback called when user has changed the blixem background color */
static gboolean onChangeBackgroundColor(GtkWidget *button, const gint responseId, gpointer data)
{
  GdkColor *color = (GdkColor*)data;
  
  /* update the color */
  gtk_color_button_get_color(GTK_COLOR_BUTTON(button), color);
  gdk_colormap_alloc_color(gdk_colormap_get_system(), color, TRUE, TRUE);
  
  /* Update */
  GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(button));
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));
  onUpdateBackgroundColor(blxWindow);
  
  return TRUE;
}


/* Create a button to allow user to change the color of the given setting */
static void createColorButton(GtkTable *table, GdkColor *color, BlxResponseCallback callbackFunc, gpointer callbackData,
                              const int row, const int column, const int xpad, const int ypad)
{
  GtkWidget *colorButton = gtk_color_button_new_with_color(color);
  widgetSetCallbackData(colorButton, callbackFunc, callbackData);

  gtk_table_attach(table, colorButton, column, column + 1, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
}


/* Create buttons for the user to be able to change the blixem colour settings */
static void createColorButtons(GtkWidget *parent, GtkWidget *blxWindow, const int borderWidth)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  
  /* put all the colors in a table. Put the table in a scrolled window, because
   * there are likely to be many rows */
  GtkWidget *scrollWin = gtk_scrolled_window_new(NULL, NULL);
  gtk_container_add(GTK_CONTAINER(parent), scrollWin);
  
  const int numCols = 5;
  const int numRows = BLXCOL_NUM_COLORS + 1; /* add one for header row */
  const int xpad = 2;
  const int ypad = 2;

  GtkTable *table = GTK_TABLE(gtk_table_new(numRows, numCols, FALSE));
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollWin), GTK_WIDGET(table));
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollWin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  /* Add a header row */
  int row = 1;
  gtk_table_attach(table, gtk_label_new("Normal   "), 2, 3, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, gtk_label_new("(selected)   "), 3, 4, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, gtk_label_new("Print   "), 4, 5, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  gtk_table_attach(table, gtk_label_new("(selected)   "), 5, 6, row, row + 1, GTK_SHRINK, GTK_SHRINK, xpad, ypad);
  
  /* loop through all defined blixem colours */
  int colorId = BLXCOLOR_MIN + 1;
  
  for ( ; colorId < BLXCOL_NUM_COLORS; ++colorId)
    {
      ++row;
    
      BlxColor *blxCol = getBlxColor(bc->defaultColors, colorId);
      GtkWidget *label = gtk_label_new(blxCol->name);
      gtk_table_attach(table, label, 1, 2, row, row + 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), GTK_SHRINK, xpad, ypad);

      /* Special callback for the background color */
      BlxResponseCallback callbackFunc = (colorId == BLXCOLOR_BACKGROUND) ? onChangeBackgroundColor : onChangeBlxColor;
      
      createColorButton(table, &blxCol->normal, callbackFunc, &blxCol->normal, row, 2, xpad, ypad);
      createColorButton(table, &blxCol->print, callbackFunc, &blxCol->print, row, 4, xpad, ypad);
      createColorButton(table, &blxCol->selected, callbackFunc, &blxCol->selected, row, 3, xpad, ypad);
      createColorButton(table, &blxCol->printSelected, callbackFunc, &blxCol->printSelected, row, 5, xpad, ypad);
    }
}


/* Called when the user toggles whether print colors should be used or not */
static void onTogglePrintColors(GtkWidget *button, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  const gboolean usePrintColors = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
  blxWindowSetUsePrintColors(blxWindow, usePrintColors);
}


/* Callback function called when the parent button of a set of sub-buttons is toggled. Enables/disables
 * the child buttons depending on whether the parent is now active or not. The container widget of the
 * child buttons is passed as the user data. */
static void onParentBtnToggled(GtkWidget *button, gpointer data)
{
  const gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
  GtkWidget *subComponents = GTK_WIDGET(data);

  gtk_widget_set_sensitive(subComponents, active); 
}


/* Callback function called when the 'Show unaligned bases' or 'Show polyA tails'
 * buttons are toggled. These are parent buttons so will cause child buttons 
 * to be enabled/disabled. */
static void onShowAdditionalSeqToggled(GtkWidget *button, gpointer data)
{
  onParentBtnToggled(button, data);
  
  /* Perform any required updates */
  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  detailViewUpdateMspLengths(detailView, detailViewGetNumUnalignedBases(detailView));
}


/* Callback function called when the 'Limit unaligned bases' button is toggled */
static void onLimitUnalignedBasesToggled(GtkWidget *button, gpointer data)
{
  /* Get the new value */
  const gboolean limitUnalignedBases = setFlagFromButton(button, GINT_TO_POINTER(BLXFLAG_LIMIT_UNALIGNED_BASES));

  /* Enable/disable the sub-options. Their widgets are all in the container passed as the data. */
  GtkWidget *subComponents = GTK_WIDGET(data);
  gtk_widget_set_sensitive(subComponents, limitUnalignedBases); 
  
  /* Get the detail view from the main window */
  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  
  /* Perform any required updates */
  detailViewUpdateMspLengths(detailView, detailViewGetNumUnalignedBases(detailView));
}


/* Callback called when the user has changed the number of additional bases to show when the
 * 'show unaligned bases' option is enabled. */
static gboolean onSetNumUnalignedBases(GtkWidget *entry, const gint responseId, gpointer data)
{
  const char *numStr = gtk_entry_get_text(GTK_ENTRY(entry));
  int numBases = convertStringToInt(numStr);
  
  GtkWidget *detailView = GTK_WIDGET(data);
  detailViewSetNumUnalignedBases(detailView, numBases);

  /* Perform any required updates */
  detailViewUpdateMspLengths(detailView, numBases);

  return TRUE;
}


/* Callback called when the 'selected seqs only' option of the 'show unaligned bases'
 * option is toggled. */
static void onToggleShowUnalignedSelected(GtkWidget *button, gpointer data)
{
  /* Get the new value */
  setFlagFromButton(button, GINT_TO_POINTER(BLXFLAG_SHOW_UNALIGNED_SELECTED));
 
  /* Perform any required updates */
  GtkWidget *detailView = GTK_WIDGET(data);
//  detailViewUpdateMspLengths(detailView, detailViewGetNumUnalignedBases(detailView));
  refilterDetailView(detailView, NULL);
}



/* Create the check button for the 'limit number of unaligned bases' option on the settings dialog
 * and pack it into the given container. */
static void createLimitUnalignedBasesButton(GtkContainer *parent, GtkWidget *detailView, BlxContext *bc)
{
  /* Create an hbox for the "limit to so-many bases" option, which has a check button, text
   * entry and some labels. Pack the hbox into the given parent. */
  GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
  gtk_container_add(parent, hbox);
  
  /* Create a text entry box so the user can enter the number of bases */
  GtkWidget *entry = gtk_entry_new();
  gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
  widgetSetCallbackData(entry, onSetNumUnalignedBases, detailView);
  
  DetailViewProperties *properties = detailViewGetProperties(detailView);
  char *numStr = convertIntToString(properties->numUnalignedBases);
  
  gtk_entry_set_text(GTK_ENTRY(entry), numStr);
  gtk_entry_set_width_chars(GTK_ENTRY(entry), strlen(numStr) + 2); /* fudge up width a bit in case user enters longer text */
  g_free(numStr);
  
  /* Check button to enable/disable setting the limit */
  GtkWidget *button = gtk_check_button_new_with_mnemonic("Li_mit to ");
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), bc->flags[BLXFLAG_LIMIT_UNALIGNED_BASES]);
  g_signal_connect(G_OBJECT(button), "toggled", G_CALLBACK(onLimitUnalignedBasesToggled), entry);
  
  GtkWidget *label = gtk_label_new(" additional bases");

  /* Pack it all in the hbox */
  gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(hbox), entry, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);

  const char *tooltip = "Specifies the maximum number of additional bases to display from unaligned sections of sequence.";
  gtk_widget_set_tooltip_text(button, tooltip);
  gtk_widget_set_tooltip_text(entry, tooltip);
  gtk_widget_set_tooltip_text(label, tooltip);
}


/* Create a "parent" option button that has a vbox container for "sub-components", i.e. more 
 * option buttons (or other dialog widgets) that will be enabled only when the parent option is
 * active. Returns the container for the sub-options, which should be packed with the sub-option widgets. */
static GtkContainer* createParentCheckButton(GtkWidget *parent, 
                                             GtkWidget *detailView,
                                             BlxContext *bc,
                                             const char *label,
                                             const char *tooltip,
                                             const BlxFlag flag,
                                             GtkWidget **buttonOut,
                                             GCallback callbackFunc)
{
  /* We'll the main button and any sub-components into a vbox */
  GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
  gtk_box_pack_start(GTK_BOX(parent), vbox, FALSE, FALSE, 0);

  /* Create a vbox for the sub-components. Create the vbox now so we can pass it to the main toggle
   * button callback, but don't pack it in the container till we've added the main check button. The sub
   * components are only active if the main check button is active. */
  const gboolean active = bc->flags[flag];
  GtkWidget *subContainer = gtk_vbox_new(FALSE, 0);
  gtk_widget_set_sensitive(subContainer, active); 
  
  /* Main check button to enable/disable the option. This call puts it in the vbox. Set two callbacks:
   * one to update the flag, and one to enable/disable the child buttons. */
  GtkWidget *btn = gtk_check_button_new_with_mnemonic(label);
  gtk_box_pack_start(GTK_BOX(vbox), btn, FALSE, FALSE, 0);
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(btn), active);

  if (tooltip)
    gtk_widget_set_tooltip_text(btn, tooltip);

  /* Connect the toggleFlag callback first so that the flag is set correctly before the callbackFunc is called */
  g_signal_connect(G_OBJECT(btn), "toggled", G_CALLBACK(onToggleFlag), GINT_TO_POINTER(flag));
  
  if (callbackFunc)
    g_signal_connect(G_OBJECT(btn), "toggled", callbackFunc, subContainer);

  if (buttonOut)
    *buttonOut = btn;

  /* Now add the subcomponent container to the vbox. Bit of a hack - put it inside an hbox with 
   * a blank label preceeding it, so that the sub-components appear offset to the right slightly
   * from the main button. */
  GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
  gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new("   "), FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(hbox), subContainer, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
  
  return GTK_CONTAINER(subContainer);
}


/* Refresh the given dialog, if it is open */
void refreshDialog(const BlxDialogId dialogId, GtkWidget *blxWindow)
{
  /* This is a bit crude but does the job: if the dialog is visible, just call its
   * 'show' function to re-create its contents. Only need to do anything for persistent
   * dialogs. Note that we don't want to bring the dialog to the front, just refresh it in
   * case the user looks at it again. */
  if (blxWindow)
    {
      BlxContext *bc = blxWindowGetContext(blxWindow);
      GtkWidget *dialog = getPersistentDialog(bc->dialogList, dialogId);
      
      if (dialog && GTK_WIDGET_VISIBLE(dialog))
        {
           switch (dialogId)
             {
               case BLXDIALOG_SETTINGS:
                 showSettingsDialog(blxWindow, FALSE);
                 break;

               case BLXDIALOG_SORT:
                 showSortDialog(blxWindow, FALSE);
                 break;
                 
               case BLXDIALOG_HELP:
                 showHelpDialog(blxWindow, FALSE);
                 break;

               case BLXDIALOG_FIND:
                 showFindDialog(blxWindow, FALSE);
                 break;

               case BLXDIALOG_VIEW:
                 showViewPanesDialog(blxWindow, FALSE);
                 break;

               case BLXDIALOG_DOTTER:
                 showDotterDialog(blxWindow, FALSE);
                 break;
                 
               case BLXDIALOG_GROUPS:
                 showGroupsDialog(blxWindow, TRUE, FALSE); /* show the 'edit' pane because we've got here by adding/deleting a group */
                 break;
                 
               default:
                 break;
             };
        }
    }
  else
    {
      g_warning("Could not refresh dialog [ID=%d]; parent window not found.\n", dialogId);
    }
}


/* Callback called when the user has responded to the font selection dialog */
static void onResponseFontSelectionDialog(GtkDialog *dialog, gint responseId, gpointer data)
{
  if (responseId == GTK_RESPONSE_ACCEPT || responseId == GTK_RESPONSE_OK || responseId == GTK_RESPONSE_APPLY)
    {
      GtkWidget *blxWindow = GTK_WIDGET(data);

      /* Check that the user selected a monospace font (unfortunately there's no easy way to get the
       * font family in older GTK versions so don't bother checking) */
      gboolean ok = TRUE;
    
#if CHECK_GTK_VERSION(2, 6)  
      GtkFontSelection *fontSeln = GTK_FONT_SELECTION(gtk_buildable_get_internal_child(GTK_BUILDABLE(dialog), gtk_builder_new(), "font_selection"));
      PangoFontFamily *family = gtk_font_selection_get_family(fontSeln);
      ok = pango_font_family_is_monospace(family);
#endif
      
      /* Get the selected font name */
      gchar *fontName = gtk_font_selection_dialog_get_font_name(GTK_FONT_SELECTION_DIALOG(dialog));

      if (!ok)
        {
          BlxContext *bc = blxWindowGetContext(blxWindow);

          char *msg = g_strdup_printf("Selected font '%s' is not a fixed-width font. Matches may not appear correctly aligned. Are you sure you want to continue?", fontName);
          char *title = g_strdup_printf("%sWarning", blxGetTitlePrefix(bc));

          gint response = runConfirmationBox(GTK_WIDGET(dialog), "Blixem - Warning", msg);

          g_free(title);
          g_free(msg);

          ok = (response == GTK_RESPONSE_ACCEPT);
        }
      
      if (ok)
        {
          GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
          DetailViewProperties *properties = detailViewGetProperties(detailView);
          
          pango_font_description_free(properties->fontDesc);
          properties->fontDesc = pango_font_description_from_string(fontName);
          
          updateDetailViewFontDesc(detailView);

          g_debug("Set font family to '%s'\n", fontName);
          blxWindowRedrawAll(blxWindow);
          
          if (responseId != GTK_RESPONSE_APPLY)
            {
              gtk_widget_destroy(GTK_WIDGET(dialog));
            }
        }
    }
  else
    {
      /* Cancelled */
      gtk_widget_destroy(GTK_WIDGET(dialog));
    }
}


/* Callback for when the font selection button is pressed. Opens the font selection dialog */
static void onFontSelectionButtonPressed(GtkWidget *button, gpointer data)
{
  GtkWidget *dialog = gtk_font_selection_dialog_new("Select fixed-width font");
  g_signal_connect(G_OBJECT(dialog), "response", G_CALLBACK(onResponseFontSelectionDialog), data);
  gtk_widget_show_all(dialog);
}


/* Create a button on the settings dialog to open a font-selection dialog */
static void createFontSelectionButton(GtkBox *parent, GtkWidget *blxWindow)
{
  GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
  gtk_box_pack_start(parent, hbox, FALSE, FALSE, 0);

  gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new("Change fixed-width font:   "), FALSE, FALSE, 0);
  
  GtkWidget *button = gtk_button_new_from_stock(GTK_STOCK_SELECT_FONT);
  g_signal_connect(G_OBJECT(button), "pressed", G_CALLBACK(onFontSelectionButtonPressed), blxWindow);
  gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
}


/* This function restores all settings to defaults */
static void resetSettings(GtkWidget *blxWindow)
{
  gint responseId = runConfirmationBox(blxWindow, "Reset all settings", 
    "This will reset all settings to their default values: are you sure you want to continue?");
  
  if (responseId == GTK_RESPONSE_ACCEPT)
    {
      resetColumnWidths(blxWindowGetColumnList(blxWindow));
      showSettingsDialog(blxWindow, FALSE);
    }
}


void onResponseSettingsDialog(GtkDialog *dialog, gint responseId, gpointer data)
{
  if (responseId == BLX_RESPONSE_RESET)
    resetSettings(dialogChildGetBlxWindow(GTK_WIDGET(dialog)));
  else
    onResponseDialog(dialog, responseId, data); /* default handler */

  /* If the save button was pressed, also save these settings to the config file */
  if (responseId == GTK_RESPONSE_ACCEPT)
    {
      GtkWidget *blxWindow = dialogChildGetBlxWindow(GTK_WIDGET(dialog));
      saveBlixemSettings(blxWindow);
    }
}


/* Show/refresh the "Settings" dialog. */
void showSettingsDialog(GtkWidget *blxWindow, const gboolean bringToFront)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  const BlxDialogId dialogId = BLXDIALOG_SETTINGS;
  GtkWidget *dialog = getPersistentDialog(bc->dialogList, dialogId);
  
  if (!dialog)
    {
      /* note: reset-to-defaults option commented out because it is incomplete:
       * for now, the help page tells the user to delete the ~/.blixemrc file
       * to reset to defaults */
      char *title = g_strdup_printf("%sSettings", blxGetTitlePrefix(bc));

      dialog = gtk_dialog_new_with_buttons(title,
                                           GTK_WINDOW(blxWindow), 
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
//                                           "Reset to defaults",
//                                           BLX_RESPONSE_RESET,
                                           GTK_STOCK_CLOSE,
                                           GTK_RESPONSE_REJECT,
                                           GTK_STOCK_APPLY,
                                           GTK_RESPONSE_APPLY,
                                           GTK_STOCK_SAVE,
                                           GTK_RESPONSE_ACCEPT,
                                           NULL);

      g_free(title);
      
      int width = 300, height = 200;
      gbtools::GUIGetTrueMonitorSizeFraction(dialog, 0.33, 0.33, &width, &height);
      gtk_window_set_default_size(GTK_WINDOW(dialog), width, height);
      
      /* These calls are required to make the dialog persistent... */
      addPersistentDialog(bc->dialogList, dialogId, dialog);
      g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);

      g_signal_connect(dialog, "response", G_CALLBACK(onResponseSettingsDialog), GINT_TO_POINTER(TRUE));
    }
  else
    {
      /* Need to refresh the dialog contents, so clear and re-create content area */
      dialogClearContentArea(GTK_DIALOG(dialog));
    }

  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_APPLY);

  int borderWidth = 12;
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  GtkWidget *bigPicture = blxWindowGetBigPicture(blxWindow);
  
  /* We'll put everything into a tabbed notebook */
  GtkWidget *notebook = gtk_notebook_new();
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), notebook, TRUE, TRUE, 0);


  /* OPTIONS PAGE */
  GtkWidget *optionsPage = gtk_vbox_new(FALSE, 0);
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), GTK_WIDGET(optionsPage), gtk_label_new_with_mnemonic("Opt_ions"));

  GtkWidget *scrollWin = gtk_scrolled_window_new(NULL, NULL);
  gtk_container_add(GTK_CONTAINER(optionsPage), scrollWin);

  GtkWidget *optionsBox = gtk_vbox_new(FALSE, 0);
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollWin), GTK_WIDGET(optionsBox));
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollWin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  GtkContainer *variationContainer = createParentCheckButton(optionsBox,
                                                             detailView,
                                                             bc,
                                                             "Highlight _variations in reference sequence",
                                                             "Any known variations will be highlighted in the reference sequence. Hover over them to see details or double-click to open the URL.",
                                                             BLXFLAG_HIGHLIGHT_VARIATIONS,
                                                             NULL,
                                                             G_CALLBACK(onParentBtnToggled));
  createCheckButton(GTK_BOX(variationContainer), 
                    "Show variations trac_k",
                    "Show a track above the reference sequence which shows the details of any variations that are highlighted in the reference sequence",
                    bc->flags[BLXFLAG_SHOW_VARIATION_TRACK],
                    G_CALLBACK(onShowVariationTrackToggled),
                    GINT_TO_POINTER(BLXFLAG_SHOW_VARIATION_TRACK));


  /* show-polyA-tails option and its sub-options. Connect onToggleFlag twice to the 'when selected' button to also toggle the 'show signals when selected' button in unison. */
  GtkWidget *polyAParentBtn = NULL;
  GtkContainer *polyAContainer = createParentCheckButton(optionsBox,
                                                         detailView,
                                                         bc,
                                                         "Show polyA _tails",
                                                         "Show polyA tails; polyA signals are also highlighted in the reference sequence.",
                                                         BLXFLAG_SHOW_POLYA_SITE,
                                                         &polyAParentBtn,
                                                         G_CALLBACK(onShowAdditionalSeqToggled));
  GtkWidget *polyABtn = createCheckButton(GTK_BOX(polyAContainer),
                                          "Selected sequences only",
                                          "Only show polyA tails for the currently-selected sequence(s)",
                                          bc->flags[BLXFLAG_SHOW_POLYA_SITE_SELECTED],
                                          G_CALLBACK(onToggleFlag),
                                          GINT_TO_POINTER(BLXFLAG_SHOW_POLYA_SITE_SELECTED));

  g_signal_connect(G_OBJECT(polyAParentBtn), "toggled", G_CALLBACK(onToggleFlag), GINT_TO_POINTER(BLXFLAG_SHOW_POLYA_SIG));
  g_signal_connect(G_OBJECT(polyABtn), "toggled", G_CALLBACK(onToggleFlag), GINT_TO_POINTER(BLXFLAG_SHOW_POLYA_SIG_SELECTED));

  const gboolean squashMatches = (bc->modelId == BLXMODEL_SQUASHED);
  

  /* show-unaligned-sequence option and its sub-options */
  GtkContainer *unalignContainer = createParentCheckButton(optionsBox,
                                                           detailView,
                                                           bc,
                                                           "Show _unaligned sequence",
                                                           "Show unaligned sections of match sequences", 
                                                           BLXFLAG_SHOW_UNALIGNED,
                                                           NULL,
                                                           G_CALLBACK(onShowAdditionalSeqToggled));
  createLimitUnalignedBasesButton(unalignContainer, detailView, bc);
  createCheckButton(GTK_BOX(unalignContainer),
                    "Selected sequences only",
                    "Only show unaligned sections of sequence for the currently-selected sequence(s)",
                    bc->flags[BLXFLAG_SHOW_UNALIGNED_SELECTED],
                    G_CALLBACK(onToggleShowUnalignedSelected),
                    detailView);


  /* show-colinearity-lines option and its sub-options */
  GtkContainer *colinearityContainer = createParentCheckButton(optionsBox,
                                                               detailView,
                                                               bc,
                                                               "Show _colinearity lines",
                                                               "Show \"traffic-light\" colinearity lines between alignment blocks: green for perfectly colinear, orange for imperfectly colinear, red for not colinear",
                                                               BLXFLAG_SHOW_COLINEARITY,
                                                               NULL,
                                                               G_CALLBACK(onParentBtnToggled));
  createCheckButton(GTK_BOX(colinearityContainer),
                    "Selected sequences only",
                    "Only show colinearity lines in the Detail section for the currently-selected sequence(s). (Note that in the Overview section they are only ever displayed for the selected sequence.)", 
                    bc->flags[BLXFLAG_SHOW_COLINEARITY_SELECTED],
                    G_CALLBACK(onToggleFlag),
                    GINT_TO_POINTER(BLXFLAG_SHOW_COLINEARITY_SELECTED));


  /* Show splice-sites option and its sub-options */
  GtkContainer *spliceSitesContainer = createParentCheckButton(optionsBox,
                                                               detailView,
                                                               bc,
                                                               "Show Sp_lice Sites for selected seqs",
                                                               "Highlights splice sites in the reference sequence for the currently selected feature(s). Green means canonical and red non-canonical.", 
                                                               BLXFLAG_SHOW_SPLICE_SITES,
                                                               NULL,
                                                               G_CALLBACK(onParentBtnToggled));
  createCheckButton(GTK_BOX(spliceSitesContainer),
                    "Highlight \"maybe canonical\" splice sites",
                    "With this option enabled, splice sites that would be canonical if they were on the other strand are highlighted in orange, rather than in red for non-canonical. This option can help find problems with strand representation in the input data.", 
                    bc->flags[BLXFLAG_SHOW_MAYBE_CANONICAL],
                    G_CALLBACK(onToggleFlag),
                    GINT_TO_POINTER(BLXFLAG_SHOW_MAYBE_CANONICAL));

  createCheckButton(GTK_BOX(optionsBox),
                    "_Highlight differences",
                    "Only display bases in a match sequence that are different to the reference sequence.",
                    bc->flags[BLXFLAG_HIGHLIGHT_DIFFS],
                    G_CALLBACK(onToggleFlag),
                    GINT_TO_POINTER(BLXFLAG_HIGHLIGHT_DIFFS));
  createCheckButton(GTK_BOX(optionsBox),
                    "_Squash matches",
                    "Compress matches to save space. Depending on how the input sources are configured, this may place matches from the same alignment onto the same line, and/or may compress duplicate reads into the same space.",
                    squashMatches,
                    G_CALLBACK(onSquashMatches),
                    NULL);



  /* DISPLAY PAGE */
  GtkWidget *displayPage = gtk_vbox_new(FALSE, 0);
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), GTK_WIDGET(displayPage), gtk_label_new_with_mnemonic("_Display"));

  GtkWidget *displayScrollWin = gtk_scrolled_window_new(NULL, NULL);
  gtk_container_add(GTK_CONTAINER(displayPage), displayScrollWin);

  GtkWidget *displayBox = gtk_vbox_new(FALSE, 0);
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(displayScrollWin), GTK_WIDGET(displayBox));
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(displayScrollWin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  GtkWidget *settingsBox = createVBoxWithBorder(displayBox, borderWidth, TRUE, "General");
  const gboolean usePrintColours = blxWindowGetUsePrintColors(blxWindow);
  createCheckButton(GTK_BOX(settingsBox),
                    "Use _print colours",
                    "Use black-and-white colours, suitable for printing",
                    usePrintColours,
                    G_CALLBACK(onTogglePrintColors),
                    blxWindow);
  createFontSelectionButton(GTK_BOX(settingsBox), blxWindow);

  createGridSettingsButtons(displayBox, bigPicture);

  createCoverageSettingsButtons(displayBox, bigPicture, detailView);


  /* COLUMNS PAGE */
  GtkWidget *columnsPage = gtk_vbox_new(FALSE, 0);
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), GTK_WIDGET(columnsPage), gtk_label_new_with_mnemonic("Colum_ns"));

  createColumnButtons(columnsPage, detailView, borderWidth);


  /* COLOURS PAGE */
  GtkWidget *appearancePage = gtk_vbox_new(FALSE, borderWidth);
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), GTK_WIDGET(appearancePage), gtk_label_new_with_mnemonic("Colou_rs"));

  createColorButtons(appearancePage, blxWindow, borderWidth);

  
  gtk_widget_show_all(dialog);
  
  if (bringToFront)
    {
      gtk_window_present(GTK_WINDOW(dialog));
    }
}


/***********************************************************
 *                       Sort menu                         *
 ***********************************************************/

/* See if this widget is a combo box, or if it has a child combo box */
static GtkComboBox* widgetGetComboBox(GtkWidget *widget)
{
  GtkComboBox *result = NULL;
  
  if (GTK_IS_COMBO_BOX(widget))
    {
      result = GTK_COMBO_BOX(widget);
    }
  else if (GTK_IS_CONTAINER(widget))
    {
      GList *children = gtk_container_get_children(GTK_CONTAINER(widget));
      GList *childItem = children;
      
      for ( ; childItem; childItem = childItem->next)
        {
          GtkWidget *childWidget = GTK_WIDGET(childItem->data);
          result = widgetGetComboBox(childWidget);
          
          if (result)
            break;
        }

      g_list_free(children);
    }
  
  return result;
}


/* For a drop-down box that contains columns, find which column is currently
 * selected */
static BlxColumnId getColumnFromComboBox(GtkComboBox *combo)
{
  BlxColumnId result = BLXCOL_NONE;
  
  if (combo)
    {
      /* Get the combo box value */
      GtkTreeIter iter;
      
      if (gtk_combo_box_get_active_iter(combo, &iter))
        {
          GtkTreeModel *model = gtk_combo_box_get_model(combo);
          
          GValue val = {0};
          gtk_tree_model_get_value(model, &iter, SORT_TYPE_COL, &val);
          
          result = (BlxColumnId)g_value_get_int(&val);
        }
    }
  
  return result;
}


/* Callback function called when the 'invert sort order' button is toggled */
static gboolean onInvertSortChanged(GtkWidget *button, const gint responseId, gpointer data)
{
  const gboolean invert = setFlagFromButton(button, data);
  
  GtkWidget *blxWindow = dialogChildGetBlxWindow(button);
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  
  detailViewUpdateSortInverted(detailView, invert);
  
  return TRUE;
}


/* Callback called when the sort order has been changed in the drop-down box */
static gboolean onSortOrderChanged(GtkWidget *widget, const gint responseId, gpointer data)
{
  GtkWidget *detailView = GTK_WIDGET(data);
  DetailViewProperties *dvProperties = detailViewGetProperties(detailView);
  GList *columnList = blxWindowGetColumnList(dvProperties->blxWindow());

  if (GTK_WIDGET_REALIZED(detailView) && GTK_IS_CONTAINER(widget))
    {
      /* Loop through each child of the given widget (assumes that each child is or
       * contains one combo box) */
      GList *children = gtk_container_get_children(GTK_CONTAINER(widget));
      GList *childItem = children;
      const int numColumns = g_list_length(columnList);
      int priority = 0;
      
      for ( ; childItem; childItem = childItem->next, ++priority)
        {
          if (priority >= numColumns)
            {
              g_critical("Exceeded max number of sort columns (%d).\n", numColumns);
              break;
            }
          
          /* See if this is a (or has a child) combo box */
          GtkWidget *childWidget = GTK_WIDGET(childItem->data);
          GtkComboBox *combo = widgetGetComboBox(childWidget);
          
          if (combo)
            {
              dvProperties->sortColumns[priority] = getColumnFromComboBox(combo);
            }
        }
      
      g_list_free(children);

      /* Re-sort trees */
      detailViewResortTrees(detailView);
    }
  
  return TRUE;
}


/* Add an option for the sorting drop-down box */
static GtkTreeIter* addSortBoxItem(GtkTreeStore *store, 
                                   GtkTreeIter *parent, 
                                   BlxColumnId sortColumn, 
                                   const char *sortName,
                                   BlxColumnId initSortColumn,
                                   GtkComboBox *combo)
{
  GtkTreeIter iter;
  gtk_tree_store_append(store, &iter, parent);
  
  gtk_tree_store_set(store, &iter, SORT_TYPE_COL, sortColumn, SORT_TEXT_COL, sortName, -1);
  
  if (sortColumn == initSortColumn)
    {
      gtk_combo_box_set_active_iter(combo, &iter);
    }
  
  return NULL;
}


/* Create the combo box used for selecting sort criteria */
static void createSortBox(GtkBox *parent, 
                          GtkWidget *detailView, 
                          const BlxColumnId initSortColumn, 
                          GList *columnList, 
                          const char *labelText,
                          const gboolean searchableOnly)
{
  /* Put the label and drop-down in a box */
  GtkWidget *box = gtk_hbox_new(FALSE, 0);
  gtk_box_pack_start(parent, box, FALSE, FALSE, 0);
  
  /* Add a label, to make it obvious what the combo box is for */
  GtkWidget *label = gtk_label_new(labelText);
  gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
  gtk_container_add(GTK_CONTAINER(box), label);
  
  /* Create the data for the drop-down box. Use a tree so that we can sort by
   * multiple criteria. */
  GtkTreeStore *store = gtk_tree_store_new(N_SORT_COLUMNS, G_TYPE_INT, G_TYPE_STRING);
  GtkComboBox *combo = GTK_COMBO_BOX(gtk_combo_box_new_with_model(GTK_TREE_MODEL(store)));
  g_object_unref(store);
  gtk_container_add(GTK_CONTAINER(box), GTK_WIDGET(combo));

  /* Create a cell renderer to display the sort text. */
  GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
  gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, FALSE);
  gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, "text", SORT_TEXT_COL, NULL);
  
  GtkTreeIter *iter = NULL;
  
  /* Add a blank row for the case where nothing is selected (unless we only
   * want searchable columns, because we can't search the NONE column) */
  if (!searchableOnly)
    iter = addSortBoxItem(store, iter , BLXCOL_NONE, "<select column>", initSortColumn, combo);

  /* Add a row for each column that has the 'sortName' property set. */
  GList *columnItem = columnList;

  for ( ; columnItem; columnItem = columnItem->next)
    {
      BlxColumnInfo *columnInfo = (BlxColumnInfo*)(columnItem->data);

      /* Only include columns that have a sort name and, if searchableOnly is
       * true, only include columns that are searchable. */
      if (columnInfo->sortName && (columnInfo->searchable || !searchableOnly))
        {
          iter = addSortBoxItem(store, iter, columnInfo->columnId, columnInfo->sortName, initSortColumn, combo);
        }
    }
}


/* Called when the user clicks the 'add new sort-by box' button on the sort dialog */
static void onAddNewSortByBox(GtkButton *button, gpointer data)
{
  /* The user-data is the container box that contains the sort-by boxes */
  GtkBox *box = GTK_BOX(data);

  GtkWindow *dialogWindow = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button)));
  GtkWidget *blxWindow = GTK_WIDGET(gtk_window_get_transient_for(dialogWindow));
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  DetailViewProperties *dvProperties = detailViewGetProperties(detailView);
  GList *columnList = blxWindowGetColumnList(dvProperties->blxWindow());

  /* Add another sort-by box to the container */
  createSortBox(box, detailView, BLXCOL_NONE, columnList, "then by", FALSE);
  
  gtk_widget_show_all(GTK_WIDGET(box));
}


/* Show/refresh the "Sort" dialog. */
void showSortDialog(GtkWidget *blxWindow, const gboolean bringToFront)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  const BlxDialogId dialogId = BLXDIALOG_SORT;
  GtkWidget *dialog = getPersistentDialog(bc->dialogList, dialogId);
  
  if (!dialog)
    {
      char *title = g_strdup_printf("%sSort", blxGetTitlePrefix(bc));

      dialog = gtk_dialog_new_with_buttons(title,
                                           GTK_WINDOW(blxWindow), 
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_STOCK_CANCEL,
                                           GTK_RESPONSE_REJECT,
                                           GTK_STOCK_APPLY,
                                           GTK_RESPONSE_APPLY,
                                           GTK_STOCK_OK,
                                           GTK_RESPONSE_ACCEPT,
                                           NULL);

      g_free(title);
      
      /* These calls are required to make the dialog persistent... */
      addPersistentDialog(bc->dialogList, dialogId, dialog);
      g_signal_connect(dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
      
      g_signal_connect(dialog, "response", G_CALLBACK(onResponseDialog), GINT_TO_POINTER(TRUE));
    }
  else
    {
      /* Need to refresh the dialog contents, so clear and re-create content area */
      dialogClearContentArea(GTK_DIALOG(dialog));
    }
  
  gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_APPLY);
  
  const int borderWidth = 12;
  GtkWidget *contentArea = GTK_DIALOG(dialog)->vbox;

  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  DetailViewProperties *dvProperties = detailViewGetProperties(detailView);
  GList *columnList = blxWindowGetColumnList(dvProperties->blxWindow());
  const int numColumns = g_list_length(columnList);

  /* Add a drop-down for each sort column that is currently specified (or just
   * the default number if none specified). */
  GtkWidget *vbox = gtk_vbox_new(FALSE, borderWidth);
  gtk_container_add(GTK_CONTAINER(contentArea), vbox);
  int sortPriority = 0;
  const int minBoxes = 3;
  
  for ( ; sortPriority < numColumns; ++sortPriority)
    {
      const BlxColumnId columnId = dvProperties->sortColumns[sortPriority];
      
      if (columnId != BLXCOL_NONE || sortPriority < minBoxes)
        {
          if (sortPriority == 0)
            createSortBox(GTK_BOX(vbox), detailView, columnId, columnList, "Sort by", FALSE);
          else
            createSortBox(GTK_BOX(vbox), detailView, columnId, columnList, "then by", FALSE);
        }
      else
        {
          break;
        }
    }
  
  widgetSetCallbackData(vbox, onSortOrderChanged, detailView);

  
  /* Add a button to add a new sort-by box */
  GtkWidget *button = gtk_button_new_from_stock(GTK_STOCK_ADD);
  gtk_box_pack_end(GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(onAddNewSortByBox), vbox);
  
  
  /* Add a toggle button for the 'invert sort order' option */
  GtkWidget *toggle = gtk_check_button_new_with_mnemonic("_Invert sort order");
  gtk_box_pack_end(GTK_BOX(vbox), toggle, FALSE, FALSE, 0);
  
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), bc->flags[BLXFLAG_INVERT_SORT]);
  widgetSetCallbackData(toggle, onInvertSortChanged, GINT_TO_POINTER(BLXFLAG_INVERT_SORT));
  

  /* Shot the dialog */
  gtk_widget_show_all(dialog);
  
  if (bringToFront)
    {
      gtk_window_present(GTK_WINDOW(dialog));
    }
}


/***********************************************************
 *                  Statistics menu                        *
 ***********************************************************/

static void getStats(GtkWidget *blxWindow, GString *result, MSP *MSPlist)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);

  /* Compile data for sequences */
  int totalNumSeqs = 0;             /* total number of sequences */
  int numValidSeqs = 0;             /* how many sequences have sequence data filled in */

  gint32 seqDataSize = 0;           /* total memory used by the sequence data */
  gint32 seqStructSize = 0;         /* total memory used by the sequence structs */

  GList *seqItem = bc->matchSeqs;
  for ( ; seqItem; seqItem = seqItem->next)
    {
      ++totalNumSeqs;
      seqStructSize += sizeof(BlxSequence);
      BlxSequence *blxSeq = (BlxSequence*)(seqItem->data);
      const char *sequence = blxSequenceGetSequence(blxSeq);

      if (sequence)
        {
          ++numValidSeqs;
          seqDataSize += strlen(sequence) * sizeof(char);
        }
    }
  

  /* Compile data for MSPs */
  int numMSPs = 0;                /* total number of MSPs */
  gint32 mspStructSize = 0;       /* total memory used by the MSP structs */

  MSP *msp = NULL;
  for (msp = MSPlist; msp ; msp = msp->next)
    {
      ++numMSPs;
      mspStructSize += sizeof(MSP);
    }

  
  /* Other data */
  int refSeqLen = strlen(blxWindowGetRefSeq(blxWindow));

  
  /* Create the text based on the results */
  g_string_printf(result, "%s%d%s %s%d%s %s%d%s %s%d%s %s%d%s %s%d%s %s%d%s %s%d%s %s%d%s",
                  "Length of reference sequence\t\t\t\t\t\t\t= ", refSeqLen, " characters\n\n",
                  "Total number of match sequences\t\t\t\t\t\t= ", totalNumSeqs, "\n",
                  "Number of match sequences containing sequence data\t= ", numValidSeqs, "\n",
                  "Total memory used by sequence data\t\t\t\t\t= ", seqDataSize, " bytes\n\n",
                  "Size of each sequence struct\t\t\t\t\t\t\t= ", (int)sizeof(BlxSequence), " bytes\n",
                  "Total memory used by sequence structs\t\t\t\t\t= ", seqStructSize, " bytes\n\n",
                  "Number of MSPs\t\t\t\t\t\t\t\t\t\t= ", numMSPs, "\n",
                  "Size of each MSP\t\t\t\t\t\t\t\t\t\t= ", (int)sizeof(MSP), " bytes\n",
                  "Total memory used by MSP structs\t\t\t\t\t\t= ", (int)sizeof(MSP) * numMSPs, " bytes");
}


static void showStatsDialog(GtkWidget *blxWindow, MSP *MSPlist)
{
  /* Create a dialog widget with an OK button */
  BlxContext *bc = blxWindowGetContext(blxWindow);
  char *title = g_strdup_printf("%sStatistics", blxGetTitlePrefix(bc));

  GtkWidget *dialog = gtk_dialog_new_with_buttons(title, 
                                                  GTK_WINDOW(blxWindow),
                                                  GTK_DIALOG_DESTROY_WITH_PARENT,
                                                  GTK_STOCK_OK,
                                                  GTK_RESPONSE_ACCEPT,
                                                  NULL);

  g_free(title);
  
  /* Ensure that the dialog box (along with any children) is destroyed when the user responds. */
  g_signal_connect (dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL);
  
  /* Create a text buffer containing the required text*/
  GString *displayText = g_string_sized_new(200); /* will be extended if we need more space */
  getStats(blxWindow, displayText, MSPlist);
  GtkTextBuffer *textBuffer = gtk_text_buffer_new(gtk_text_tag_table_new());
  
  gtk_text_buffer_set_text(GTK_TEXT_BUFFER(textBuffer), displayText->str, -1);
  
  g_string_free(displayText, TRUE);
  
  /* Create a text view widget and put it in the vbox area of the dialog */
  GtkWidget *textView = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(textBuffer));
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), textView, TRUE, TRUE, 0);
  
  /* Show the dialog */
  gtk_widget_show(textView);
  gtk_widget_show(dialog);
}


/***********************************************************
 *                      About dialog                       *
 ***********************************************************/

/* A GtkAboutDialogActivateLinkFunc() called when user clicks on website link in "About" window. */
static void aboutDialogOpenLinkCB(GtkAboutDialog *about, const gchar *link, gpointer data)
{
  GError *error = NULL ;
    
  if (!seqtoolsLaunchWebBrowser(link, &error))
    g_critical("Cannot show link in web browser: \"%s\"", link) ;    
}


/* Shows the 'About' dialog */
void showAboutDialog(GtkWidget *parent)
{
#if CHECK_GTK_VERSION(2, 6)
  const gchar *authors[] = {AUTHOR_LIST, NULL} ;

  gtk_about_dialog_set_url_hook(aboutDialogOpenLinkCB, NULL, NULL) ;
  
  char *comments_string = blxGetCommentsString();

  gtk_show_about_dialog(GTK_WINDOW(parent),
                        "authors", authors,
                        "comments", blxGetCommentsString(), 
                        "copyright", blxGetCopyrightString(),
                        "license", blxGetLicenseString(),
                        "name", blxGetAppName(),
                        "version", blxGetVersionString(),
                        "website", blxGetWebSiteString(),
                        NULL) ;

  g_free(comments_string);
#endif
}


/***********************************************************
 *                      Help menu                          *
 ***********************************************************/

void onResponseHelpDialog(GtkDialog *dialog, gint responseId, gpointer data)
{
  gboolean destroy = TRUE;
  
  switch (responseId)
  {
    case GTK_RESPONSE_ACCEPT:
      destroy = TRUE;
      break;
      
    case GTK_RESPONSE_HELP:
      showAboutDialog(NULL);
      destroy = FALSE;
      break;
      
    case GTK_RESPONSE_CANCEL:
    case GTK_RESPONSE_REJECT:
      destroy = TRUE;
      break;
      
    default:
      break;
  };
  
  if (destroy)
    {
      /* If it's a persistent dialog, just hide it, otherwise destroy it */
      const gboolean isPersistent = GPOINTER_TO_INT(data);
      
      if (isPersistent)
        {
          gtk_widget_hide_all(GTK_WIDGET(dialog));
        }
      else
        {
          gtk_widget_destroy(GTK_WIDGET(dialog));
        }
    }
}


void showHelpDialog(GtkWidget *blxWindow, const gboolean bringToFront)
{
  GError *error = NULL;

  /* The docs should live in /share/doc/seqtools/, in the same parent
   * directory that our executable's 'bin' directory is in. Open the 'quick
   * start' page. */
  char rel_path[100] = "../share/doc/seqtools/blixem_quick_start.html";

  /* Find the executable's path */
  char *exe = g_find_program_in_path(g_get_prgname());
  gboolean ok = (exe != NULL);

  if (ok)
    {
      /* Get the executable's directory */
      char *dir = g_path_get_dirname(exe);
      
      ok = dir != NULL;
      
      if (ok)
        {
          /* Get the path to the html page */
          char *path = g_strdup_printf("%s/%s", dir, rel_path);
          
          ok = path != NULL;
          
          if (ok)
            {
              g_message("Opening help page '%s'\n", path);
              seqtoolsLaunchWebBrowser(path, &error);
              g_free(path);
            }

          g_free(dir);
        }

      g_free(exe);
    }
  
  if (!ok)
    {
      if (error)
        reportAndClearIfError(&error, G_LOG_LEVEL_CRITICAL);
      else
        g_critical("Could not find help documentation: %s\n", rel_path);
    }
}

/***********************************************************
 *                        Menu actions                     *
 ***********************************************************/

/* Called when the user selects the quit menu option, or hits the Quit shortcut key.
 * Pops up a dialog showing user help information */
static void onQuit(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  gtk_widget_destroy(blxWindow);
}

static void onHelpMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  showHelpDialog(blxWindow, TRUE);
}

static void onAboutMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  showAboutDialog(blxWindow);
}

static void onFindMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  showFindDialog(blxWindow, TRUE);
}

static void onGoToMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  
  /* We currently only accept input in terms of DNA coords on the ref seq */
  const BlxSeqType seqType = BLXSEQ_DNA;
  
  goToDetailViewCoord(blxWindowGetDetailView(blxWindow), seqType);
}

static void onPrevMatchMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  GList *seqList = blxWindowGetSelectedSeqs(blxWindow);
  prevMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
}

static void onNextMatchMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  GList *seqList = blxWindowGetSelectedSeqs(blxWindow);
  nextMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
}

static void onFirstMatchMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  GList *seqList = blxWindowGetSelectedSeqs(blxWindow);
  firstMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
}

static void onLastMatchMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  GList *seqList = blxWindowGetSelectedSeqs(blxWindow);
  lastMatch(blxWindowGetDetailView(blxWindow), seqList, FALSE);
}

static void onPageLeftMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  scrollDetailViewLeftPage(blxWindowGetDetailView(blxWindow));
}

static void onPageRightMenu(GtkAction *action, gpointer data)
{  
  GtkWidget *blxWindow = GTK_WIDGET(data);
  scrollDetailViewRightPage(blxWindowGetDetailView(blxWindow));
}

static void onScrollLeft1Menu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  scrollDetailViewLeft1(blxWindowGetDetailView(blxWindow));
}

static void onScrollRight1Menu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  scrollDetailViewRight1(blxWindowGetDetailView(blxWindow));
}

static void onSquashMatchesMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  
  const gboolean squash = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
  
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  detailViewUpdateSquashMatches(detailView, squash);
  
  /* Refresh the settings dialog, if it is open */
  refreshDialog(BLXDIALOG_SETTINGS, blxWindow);
}

static void onToggleStrandMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  toggleStrand(blxWindowGetDetailView(blxWindow));
}

/* Called when the user selects the View menu option, or hits the Settings shortcut key */
static void onViewMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  showViewPanesDialog(blxWindow, TRUE);
}


/* Called when the user selects the 'Group Sequences' menu option, or hits the relevant shortcut key */
static void onCreateGroupMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  showGroupsDialog(blxWindow, FALSE, TRUE);
}


/* Called when the user selects the 'Groups' menu option, or hits the relevant shortcut key */
static void onEditGroupsMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  showGroupsDialog(blxWindow, TRUE, TRUE);
}

/* Called when the user selects the 'Create group from clipboard' option */
static void onCreateQuickGroup(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  createQuickGroup(blxWindow, false, true);
}

/* Called when the user selects the 'Create filter from clipboard' option */
static void onCreateQuickFilter(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  createQuickGroup(blxWindow, true, true);
}

/* Called when the user selects the 'Hide source(s)' option */
static void onHideSources(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);

  hideSelectedSources(blxWindow);
}

/* Called when the user selects the 'Clear groups/filters' option */
static void onClearGroups(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);

  clearGroups(blxWindow);
}

/* Called when the user selects the Settings menu option, or hits the Settings shortcut key */
static void onSettingsMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  showSettingsDialog(blxWindow, TRUE);
}


static void onLoadMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  GError *tmp_error = NULL;

  char *filename = getLoadFileName(blxWindow, NULL, "Load file");
  dynamicLoadFeaturesFile(blxWindow, filename, NULL, &tmp_error);
  
  g_free(filename);

  reportAndClearIfError(&tmp_error, G_LOG_LEVEL_CRITICAL);
}

static void onCopySeqsMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  copySelectionToClipboard(blxWindow);
}

static void onCopySeqDataMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  copySelectedSeqDataToClipboard(blxWindow);
}

static void onCopySeqDataMarkMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);

  /* Copy the portion of the match seq from the selected index
   * to the clicked index */
  IntRange *range = detailViewGetSelectedDnaIdxRange(detailView);

  if (range)
    {
      const int fromIdx = range->min();
      const int toIdx = range->max();

      copySelectedSeqRangeToClipboard(blxWindow, fromIdx, toIdx);

      delete range;
    }
  else
    {
      g_critical("Please middle-click on a coordinate first to set the mark\n");
    }
}

/* Copy the ref seq for the currently-selected range of coords to the clipboard */
static void onCopyRefSeqDnaMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);

  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);

  /* Copy the portion of the ref seq from the selected index
   * to the clicked index */
  IntRange *range = detailViewGetSelectedDnaIdxRange(detailView);

  if (range)
    {
      const int fromIdx = range->min();
      const int toIdx = range->max();

      copyRefSeqToClipboard(blxWindow, fromIdx, toIdx);

      delete range;
    }
  else
    {
      g_critical("Please middle-click on a coordinate first to set the mark\n");
    }
}

/* Copy the translation of the ref seq for the currently-selected range of coords 
 * to the clipboard. Uses the currently-active reading frame. */
static void onCopyRefSeqDisplayMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);

  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);

  /* Copy the portion of the ref seq from the selected index
   * to the clicked index */
  IntRange *range = detailViewGetSelectedDnaIdxRange(detailView);

  if (range)
    {
      const int fromIdx = range->min();
      const int toIdx = range->max();

      copyRefSeqTranslationToClipboard(blxWindow, fromIdx, toIdx);

      delete range;
    }
  else
    {
      g_critical("Please middle-click on a coordinate first to set the mark\n");
    }
}

static void onSortMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  showSortDialog(blxWindow, TRUE);
}

static void onZoomInMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  zoomDetailView(blxWindowGetDetailView(blxWindow), TRUE);
}

static void onZoomOutMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  zoomDetailView(blxWindowGetDetailView(blxWindow), FALSE);
}

/* Called when the user selects the Dotter menu option, or hits the Dotter shortcut key */
static void onDotterMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  showDotterDialog(blxWindow, TRUE);
}

/* Called when the user selects the 'Close all Dotters' menu option */
static void onCloseAllDottersMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  BlxContext *bc = blxWindowGetContext(blxWindow);

  /* Check if there are actually any spawned processes */
  if (g_slist_length(bc->spawnedProcesses) > 0)
    {
      gint responseId = runConfirmationBox(blxWindow, "Close all Dotters", 
        "Are you sure you want to close all Dotters started from this Blixem?");

      if (responseId == GTK_RESPONSE_ACCEPT)
        {
          bc->killAllSpawned();
        }
    }
  else
    {
      g_message("No Dotters to close.\n");
    }
}

/* Called when the user selects the 'Select Features' menu option, or hits the relevant shortcut key */
static void onSelectFeaturesMenu(GtkAction *action, gpointer data)
{
//  selectFeatures();
}


/* Called when the user selects the 'Deselect all' menu option, or hits the relevant shortcut key */
static void onDeselectAllRows(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  blxWindowDeselectAllSeqs(blxWindow);
}


/* Called when the user selects the Statistics menu option, or hits the Statistics shortcut key.
 * Pops up a dialog showing memory usage statistics. */
static void onStatisticsMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  MSP *mspList = blxWindowGetMspList(blxWindow);
  showStatsDialog(blxWindow, mspList);
}


/* Called when the user selects the Print menu option, or hits the Print shortcut key */
static void onPrintMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);

  /* We need to do some work to prepare the big picture for printing */
  bigPicturePrepareForPrinting(properties->bigPicture);
  
  blxPrintWidget(blxWindow, NULL, GTK_WINDOW(blxWindow), &properties->printSettings, &properties->pageSetup, NULL, TRUE, PRINT_FIT_BOTH);
  
  bigPictureRedrawAll(properties->bigPicture);
}


/* Called when the user selects the Page Setup menu option, or hits the Print shortcut key */
static void onPageSetupMenu(GtkAction *action, gpointer data)
{
  GtkWidget *blxWindow = GTK_WIDGET(data);
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);

  if (!properties->pageSetup)
    properties->pageSetup = gtk_page_setup_new();
  
  if (!properties->printSettings)
    properties->printSettings = gtk_print_settings_new();
  
  properties->pageSetup = gtk_print_run_page_setup_dialog(GTK_WINDOW(blxWindow), 
                                                          properties->pageSetup, 
                                                          properties->printSettings);
}  

/***********************************************************
 *                         Events                          *
 ***********************************************************/

/* Mouse button handler */
static gboolean onButtonPressBlxWindow(GtkWidget *window, GdkEventButton *event, gpointer data)
{
  if (event->type == GDK_BUTTON_PRESS && event->button == 3)
    {
      gtk_menu_popup (GTK_MENU (data), NULL, NULL, NULL, NULL, event->button, event->time);
      return TRUE;
  }
  
  return TRUE;
}


/* Mouse button handler for the paned window containing the big picture and detail view */
static gboolean onButtonPressPanedWin(GtkWidget *panedWin, GdkEventButton *event, gpointer data)
{
  gboolean handled = FALSE;
  
  switch (event->button)
    {
    case 1: /* left button */
      {
        if (event->type == GDK_2BUTTON_PRESS) /* double-click */
          {
            /* When the user double-clicks the paned window separator, reset the splitter position
             * (i.e. so that gets automatically positioned based on the child widgets' size)
             * to do: This makes the splitter jump temporarily to the desired position but then it
             * immediately jumps back, so I'm leaving it out for now */
            /* gtk_paned_set_position(GTK_PANED(panedWin), 100); */
            handled = TRUE;
          }

        break;
      }
      
    default:
      break;
    };
    
  return handled;
}


/* Handlers for specific key presses */
static gboolean onKeyPressEscape(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  /* Reset the selected base index. Leave the selected frame as it is, though. */
  GtkWidget *detailView = blxWindowGetDetailView(window);
  detailViewUnsetSelectedBaseIdx(detailView);
  return TRUE;
}

static gboolean onKeyPressLeftRight(GtkWidget *window, 
                                    const gboolean left, 
                                    const gboolean ctrlModifier, 
                                    const gboolean shiftModifier,
                                    const gboolean metaModifier)
{
  if (ctrlModifier)
    {
      goToMatch(window, left, shiftModifier);
    }
  else if (metaModifier)
    {
      moveSelectedBaseIdxBy1(window, left, shiftModifier);
    }
  else
    {
      moveSelectedDisplayIdxBy1(window, left, shiftModifier);
    }
  
  return TRUE;
}

static gboolean onKeyPressUpDown(GtkWidget *window, const gboolean up, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  gboolean result = moveRowSelection(window, up, ctrlModifier, shiftModifier);
  return result;
}

static gboolean onKeyPressHomeEnd(GtkWidget *window, const gboolean home, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  scrollToExtremity(window, home, ctrlModifier, shiftModifier);
  return TRUE;
}

static gboolean onKeyPressComma(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  scrollDetailView(window, TRUE, ctrlModifier);
  return TRUE;
}

static gboolean onKeyPressPeriod(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  scrollDetailView(window, FALSE, ctrlModifier);
  return TRUE;
}

static gboolean onKeyPressPlusMinus(GtkWidget *window, const gboolean zoomIn, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  zoomBlxWindow(window, zoomIn, ctrlModifier, shiftModifier);
  return TRUE;
}

static gboolean onKeyPressV(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  gboolean result = FALSE;
  
  if (ctrlModifier)
    {
      /* Paste from the default clipboard */
      requestDefaultClipboardText(findAndSelectSeqsFromClipboard, window);
      result = TRUE;
    }
  
  return result;
}

static gboolean onKeyPressF(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  if (ctrlModifier)
    {
      showFindDialog(window, TRUE);
    }
  else
    {
      createQuickGroup(window, true, !shiftModifier);
    }

  return TRUE;
}

static gboolean onKeyPressP(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  gboolean result = FALSE;
  
  if (!ctrlModifier)
    {
      goToDetailViewCoord(blxWindowGetDetailView(window), BLXSEQ_DNA); /* for now, only accept input in terms of DNA seq coords */
      result = TRUE;
    }
  
  return result;
}

static gboolean onKeyPressG(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  gboolean result = FALSE;
  
  if (!ctrlModifier)
    {       
      createQuickGroup(window, false, !shiftModifier);
      result = TRUE;
    }
  
  return result;
}

static gboolean onKeyPressB(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  toggleBumpState(window);
  
  /* Refresh the view dialog, if it is open */
  refreshDialog(BLXDIALOG_VIEW, window);
  
  return TRUE;
}

static gboolean onKeyPressT(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  toggleStrand(blxWindowGetDetailView(window));
  return TRUE;
}

static gboolean onKeyPressI(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  showInfoDialog(window);
  return TRUE;
}

static gboolean onKeyPressNumber(GtkWidget *window, const int number, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  togglePaneVisibility(window, number, ctrlModifier, shiftModifier);
  
  /* Refresh the view dialog, if it is open */
  refreshDialog(BLXDIALOG_VIEW, window);
  
  return TRUE;
}

static gboolean onKeyPressF3(GtkWidget *window, const gboolean ctrlModifier, const gboolean shiftModifier)
{
  findAgain(window, shiftModifier);
  return TRUE;
}

/* Key press handler */
static gboolean onKeyPressBlxWindow(GtkWidget *window, GdkEventKey *event, gpointer data)
{
  gboolean result = FALSE;
  
  const gboolean ctrlModifier = (event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK;
  const gboolean shiftModifier = (event->state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK;
  const gboolean metaModifier = (event->state & GDK_META_MASK) == GDK_META_MASK;
  
  switch (event->keyval)
    {
      case GDK_Escape:      result = onKeyPressEscape(window, ctrlModifier, shiftModifier);           break;
      
      case GDK_Left:        result = onKeyPressLeftRight(window, TRUE, ctrlModifier, shiftModifier, metaModifier);  break;
      case GDK_Right:       result = onKeyPressLeftRight(window, FALSE, ctrlModifier, shiftModifier, metaModifier); break;
      
      case GDK_Up:          result = onKeyPressUpDown(window, TRUE, ctrlModifier, shiftModifier);     break;
      case GDK_Down:        result = onKeyPressUpDown(window, FALSE, ctrlModifier, shiftModifier);    break;
        
      case GDK_Home:        result = onKeyPressHomeEnd(window, TRUE, ctrlModifier, shiftModifier);    break;
      case GDK_End:         result = onKeyPressHomeEnd(window, FALSE, ctrlModifier, shiftModifier);   break;

      case GDK_comma:       result = onKeyPressComma(window, ctrlModifier, shiftModifier);            break;
      case GDK_period:      result = onKeyPressPeriod(window, ctrlModifier, shiftModifier);           break;

      case GDK_equal:       /* fall through */
      case GDK_plus:        result = onKeyPressPlusMinus(window, TRUE, ctrlModifier, shiftModifier);  break;
      
      case GDK_minus:       /* fall through */
      case GDK_underscore:  result = onKeyPressPlusMinus(window, FALSE, ctrlModifier, shiftModifier); break;
      
      case GDK_F3:          result = onKeyPressF3(window, ctrlModifier, shiftModifier);               break;

      case GDK_v:           /* fall through */
      case GDK_V:           result = onKeyPressV(window, ctrlModifier, shiftModifier);                break;
        
      case GDK_f:           /* fall through */
      case GDK_F:           result = onKeyPressF(window, ctrlModifier, shiftModifier);                break;

      case GDK_g:           /* fall through */
      case GDK_G:           result = onKeyPressG(window, ctrlModifier, shiftModifier);                break;

      case GDK_p:           /* fall through */
      case GDK_P:           result = onKeyPressP(window, ctrlModifier, shiftModifier);                break;
                
      case GDK_b:           /* fall through */
      case GDK_B:           result = onKeyPressB(window, ctrlModifier, shiftModifier);                break;
        
      case GDK_t:           /* fall through */
      case GDK_T:           result = onKeyPressT(window, ctrlModifier, shiftModifier);                break;

      case GDK_i:           /* fall through */
      case GDK_I:           result = onKeyPressI(window, ctrlModifier, shiftModifier);                break;

      case GDK_1:           /* fall through */
      case GDK_exclam:      result = onKeyPressNumber(window, 1, ctrlModifier, shiftModifier);        break;

      case GDK_2:           /* fall through */
      case GDK_quotedbl:    /* fall through */
      case GDK_at:          result = onKeyPressNumber(window, 2, ctrlModifier, shiftModifier);        break;

      case GDK_3:           /* fall through */
      case GDK_currency:    result = onKeyPressNumber(window, 3, ctrlModifier, shiftModifier);        break;
    };
  
  return result;
}

/***********************************************************
 *                         Properties                      *
 ***********************************************************/

static BlxWindowProperties* blxWindowGetProperties(GtkWidget *widget)
{
  /* optimisation: cache result, because we know there is only ever one main window */
  static BlxWindowProperties *properties = NULL;
  
  if (!properties && widget)
    properties = (BlxWindowProperties*)(g_object_get_data(G_OBJECT(widget), "BlxWindowProperties"));
  
  return properties;
}

BlxContext* blxWindowGetContext(GtkWidget *blxWindow)
{
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);
  return properties ? properties->blxContext : NULL;
}

GList* blxWindowGetColumnList(GtkWidget *blxWindow)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  return (bc ? bc->columnList : NULL);
}

/* This function saves all of blixem's customisable settings to
 * a config file. */
static void saveBlixemSettings(GtkWidget *blxWindow)
{
  g_return_if_fail(blxWindow);

  char *filename = g_strdup_printf("%s/%s", g_get_home_dir(), BLIXEM_SETTINGS_FILE);
  BlxContext *bc = blxWindowGetContext(blxWindow);

  GKeyFile *key_file = g_key_file_new();
  GKeyFileFlags flags = G_KEY_FILE_NONE;
  GError *error = NULL;
  
  /* Load existing contents so they can be merged, if the file already exists */
  g_key_file_load_from_file(key_file, filename, flags, &error);
  g_message("Saving Blixem settings to '%s'.\n", filename);

  /* Write the settings */
  bc->saveSettingsFlags(key_file);
  g_key_file_set_integer(key_file, SETTINGS_GROUP, SETTING_NAME_SQUASH_MATCHES, (bc->modelId == BLXMODEL_SQUASHED));
  detailViewSaveProperties(blxWindowGetDetailView(blxWindow), key_file);

  /* Output to the config file */
  gchar *file_content = g_key_file_to_data(key_file, NULL, NULL);
      
  if (!g_file_set_contents(filename, file_content, -1, NULL))
    g_warning("Error saving settings to '%s'.\n", filename);

  g_free(file_content);
  g_key_file_free(key_file);
}


static void onDestroyBlxWindow(GtkWidget *widget)
{
  BlxWindowProperties *properties = blxWindowGetProperties(widget);
  
  if (properties)
    {
      delete properties->blxContext;
      properties->blxContext = NULL;

      if (properties->mainmenu)
        {
          gtk_widget_destroy(properties->mainmenu);
          properties->mainmenu = NULL;
        }

      if (properties->seqHeaderMenu)
        {
          gtk_widget_destroy(properties->seqHeaderMenu);
          properties->seqHeaderMenu = NULL;
        }
      
      /* Destroy the print settings */
      if (properties->printSettings)
        {
          g_object_unref(properties->printSettings);
          properties->printSettings = NULL;
        }
      
      /* Free the properties struct itself */
      delete properties;
      properties = NULL;
      g_object_set_data(G_OBJECT(widget), "BlxWindowProperties", NULL);
    }

  /* Reset any globals */
  blviewResetGlobals();
  
  gtk_main_quit();
}


/* Create the properties struct and initialise all values. */
static void blxWindowCreateProperties(CommandLineOptions *options,
                                      BlxContext *blxContext,
                                      GtkWidget *widget, 
                                      GtkWidget *bigPicture, 
                                      GtkWidget *detailView,
                                      GtkWidget *mainmenu,
                                      GtkWidget *seqHeaderMenu,
                                      GtkActionGroup *actionGroup,
                                      const IntRange* const refSeqRange,
                                      const IntRange* const fullDisplayRange,
                                      const char *paddingSeq)
{
  if (widget)
    {
      BlxWindowProperties *properties = new BlxWindowProperties;
      
      properties->widget = widget;
      properties->blxContext = blxContext;

      properties->bigPicture = bigPicture;
      properties->detailView = detailView;
      properties->mainmenu = mainmenu;
      properties->seqHeaderMenu = seqHeaderMenu;
      properties->actionGroup = actionGroup;

      properties->pageSetup = gtk_page_setup_new();
      gtk_page_setup_set_orientation(properties->pageSetup, GTK_PAGE_ORIENTATION_LANDSCAPE);
      
      properties->printSettings = gtk_print_settings_new();
      gtk_print_settings_set_orientation(properties->printSettings, GTK_PAGE_ORIENTATION_LANDSCAPE);
      gtk_print_settings_set_quality(properties->printSettings, GTK_PRINT_QUALITY_HIGH);
      gtk_print_settings_set_resolution(properties->printSettings, DEFAULT_PRINT_RESOLUTION);
    
      g_object_set_data(G_OBJECT(widget), "BlxWindowProperties", properties);
      g_signal_connect(G_OBJECT(widget), "destroy", G_CALLBACK (onDestroyBlxWindow), NULL);
    }
}

gboolean blxWindowGetDisplayRev(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->displayRev : FALSE;
}

GtkWidget* blxWindowGetBigPicture(GtkWidget *blxWindow)
{
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);
  return properties ? properties->bigPicture : NULL;
}

GtkWidget* blxWindowGetDetailView(GtkWidget *blxWindow)
{
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);
  return properties ? properties->detailView : NULL;
}

GtkWidget* blxWindowGetBigPictureCoverageView(GtkWidget *blxWindow)
{
  GtkWidget *coverageView = NULL;
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);

  if (properties && properties->bigPicture)
    {
      BigPictureProperties *bpProperties = bigPictureGetProperties(properties->bigPicture);
      
      if (bpProperties)
        coverageView = bpProperties->coverageView();
    }

  return coverageView;
}

GtkWidget* blxWindowGetDetailViewCoverageView(GtkWidget *blxWindow)
{
  GtkWidget *coverageView = NULL;
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);

  if (properties && properties->detailView)
    {
      DetailViewProperties *dvProperties = detailViewGetProperties(properties->detailView);
      
      if (dvProperties)
        coverageView = dvProperties->coverageView();
    }

  return coverageView;
}

GtkWidget* blxWindowGetMainMenu(GtkWidget *blxWindow)
{
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);
  return properties ? properties->mainmenu : NULL;
}

GtkWidget* blxWindowGetSeqHeaderMenu(GtkWidget *blxWindow)
{
  BlxWindowProperties *properties = blxWindowGetProperties(blxWindow);
  return properties ? properties->seqHeaderMenu : NULL;
}

BlxBlastMode blxWindowGetBlastMode(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->blastMode : (BlxBlastMode)0;
}

char * blxWindowGetRefSeq(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->refSeq : NULL;
}

const char * blxWindowGetRefSeqName(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->refSeqName : NULL;
}

char** blxWindowGetGeneticCode(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->geneticCode : NULL;
}

MSP* blxWindowGetMspList(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->mspList : NULL;
}

GList* blxWindowGetAllMatchSeqs(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->matchSeqs : NULL;
}

BlxSeqType blxWindowGetSeqType(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->seqType : BLXSEQ_NONE;
}

IntRange* blxWindowGetFullRange(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? &blxContext->fullDisplayRange : NULL;
}

IntRange* blxWindowGetRefSeqRange(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? &blxContext->refSeqRange : NULL;
}

int blxWindowGetNumFrames(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->numFrames : UNSET_INT;
}

int blxWindowGetDotterStart(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->dotterStart : UNSET_INT;
}

int blxWindowGetDotterEnd(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->dotterEnd : UNSET_INT;
}

int blxWindowGetDotterZoom(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->dotterZoom : UNSET_INT;
}

const char* blxWindowGetPaddingSeq(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->paddingSeq : NULL;
}

/* Return the active strand - forward strand by default, reverse strand if display toggled */
BlxStrand blxWindowGetActiveStrand(GtkWidget *blxWindow)
{
  return blxWindowGetDisplayRev(blxWindow) ? BLXSTRAND_REVERSE : BLXSTRAND_FORWARD;
}

/* Return the inactive strand - reverse strand by default, forward strand if display toggled */
static BlxStrand blxWindowGetInactiveStrand(GtkWidget *blxWindow)
{
  return blxWindowGetDisplayRev(blxWindow) ? BLXSTRAND_FORWARD : BLXSTRAND_REVERSE;
}

/* returns true if display coords should be negated */
gboolean blxWindowGetNegateCoords(GtkWidget *blxWindow)
{
  /* We negate coords (literally just stick a '-' on the front) for display purposes if the
   * display is reversed and the negate-coords option is enabled. This gives the effect that coords
   * always increase left-to-right, whereas when the display is reversed they really decrease. */
  BlxContext *bc = blxWindowGetContext(blxWindow);
  return (bc->displayRev && bc->flags[BLXFLAG_NEGATE_COORDS]);
}

/* Get the column info for a particular column */
BlxColumnInfo *getColumnInfo(const GList *columnList, const BlxColumnId columnId)
{
  BlxColumnInfo *result = NULL;
  
  const GList *listItem = columnList;
  
  for ( ; listItem; listItem = listItem->next)
  {
    BlxColumnInfo *columnInfo = (BlxColumnInfo*)(listItem->data);
    if (columnInfo && columnInfo->columnId == columnId)
      {
        result = columnInfo;
        break;
      }
  }
  
  return result;
}


/* Returns the list of all sequence groups */
GList *blxWindowGetSequenceGroups(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext->sequenceGroups;
}


/* Returns the group that the given sequence belongs to, if any (assumes the sequence
 * is only in one group; otherwise it just returns the first group it finds). */
SequenceGroup *blxWindowGetSequenceGroup(GtkWidget *blxWindow, const BlxSequence *seqToFind)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  return bc->getFirstSequenceGroup(seqToFind);
}


static gboolean blxWindowGetUsePrintColors(GtkWidget *blxWindow)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  return bc->usePrintColors;
}


/* This should be called whenever the background color has changed */
static void onUpdateBackgroundColor(GtkWidget *blxWindow)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);

  GdkColor *defaultBgColor = getGdkColor(BLXCOLOR_BACKGROUND, bc->defaultColors, FALSE, bc->usePrintColors);
  setWidgetBackgroundColor(blxWindow, defaultBgColor);
  
  blxWindowRedrawAll(blxWindow);
}


/* This sets the 'use print colors' flag and then updates the display */
static void blxWindowSetUsePrintColors(GtkWidget *blxWindow, const gboolean usePrintColors)
{
  BlxContext *bc = blxWindowGetContext(blxWindow);
  bc->usePrintColors = usePrintColors;
  onUpdateBackgroundColor(blxWindow);
}


/***********************************************************
 *                        Selections                       *
 ***********************************************************/

/* Return all the selected sequences */
GList* blxWindowGetSelectedSeqs(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext ? blxContext->selectedSeqs : NULL;
}


/* Return a list of all selected features of the given type. Result should be free'd by caller
 * using g_list_free */
GList *blxWindowGetSelectedSeqsByType(GtkWidget *blxWindow, const BlxSequenceType type)
{
  GList *result = NULL;

  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  result = blxContext->getSelectedSeqsByType(type);

  return result;
}

/* If there is one (and only one) selected transcript then return it; otherwise return null */
BlxSequence* blxWindowGetSelectedTranscript(GtkWidget *blxWindow)
{
  BlxSequence *result = NULL;

  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  result = blxContext->getSelectedTranscript(NULL);

  return result;
}


/* Get the selected sequences as a list of sequence names. The returned list
 * is formatted as newline-separated values. */
static GString* blxWindowGetSelectedSeqNames(GtkWidget *blxWindow)
{
  GList *listItem = blxWindowGetSelectedSeqs(blxWindow);
  GString *result = g_string_new_len(NULL, 50);
  gboolean first = TRUE;
  
  for ( ; listItem; listItem = listItem->next)
    {
      /* Add a separator before the name, unless it's the first one */
      if (!first)
        {
          g_string_append(result, "\n");
        }
      else
        {
          first = FALSE;
        }

      const BlxSequence *seq = (const BlxSequence*)(listItem->data);
      g_string_append(result, blxSequenceGetName(seq));
    }

  return result;
}


/* Get the selected sequence data. Only works for a single selection.
 * Returns null if fails. */
static const char* blxWindowGetSelectedSeqData(GtkWidget *blxWindow)
{
  GList *listItem = blxWindowGetSelectedSeqs(blxWindow);
  const char *result = NULL;

  if (g_list_length(listItem) < 1)
    g_critical("Please select a sequence.\n");
  else if (g_list_length(listItem) > 1)
    g_critical("Please select a single sequence.\n");
  else
    {
      const BlxSequence *seq = (const BlxSequence*)(listItem->data);
      result = blxSequenceGetSequence(seq);
    }

  return result;
}


/* This function copies the currently-selected sequences' names to the default
 * clipboard. */
void copySelectionToClipboard(GtkWidget *blxWindow)
{
  if (g_list_length(blxWindowGetSelectedSeqs(blxWindow)) < 1)
    {
      g_critical("Please select a sequence");
    }
  else
    {
      GString *displayText = blxWindowGetSelectedSeqNames(blxWindow);

      if (displayText)
        {
          setDefaultClipboardText(displayText->str);
          g_string_free(displayText, TRUE);
          g_message("Copied selected sequence name(s) to clipboard\n");
        }
    }
}


/* This function copies the currently-selected sequence's data to the default
 * clipboard. */
static void copySelectedSeqDataToClipboard(GtkWidget *blxWindow)
{
  const char *displayText = blxWindowGetSelectedSeqData(blxWindow);

  if (displayText)
    {
      const int len = strlen(displayText);
      
      /* Warn user if they're about to copy a large sequence */
      if (len <= MAX_RECOMMENDED_COPY_LENGTH || 
          runConfirmationBox(blxWindow, "Copy sequence", "You are about to copy a large amount of text to the clipboard\n\nAre you sure you want to continue?") == GTK_RESPONSE_ACCEPT)
        {
          setDefaultClipboardText(displayText);
          g_message("Copied selected sequence data to clipboard\n");
        }
    }
}


/* This function copies the currently-selected sequence's data to the default
 * clipboard. Only copies the range between the given coords */
static void copySelectedSeqRangeToClipboard(GtkWidget *blxWindow, const int fromIdx_in, const int toIdx_in)
{
  GList *listItem = blxWindowGetSelectedSeqs(blxWindow);
  const char *sequence = NULL;

  if (g_list_length(listItem) < 1)
    g_critical("Please select a sequence.\n");
  else if (g_list_length(listItem) > 1)
    g_critical("Please select a single sequence.\n");
  else
    {
      const BlxSequence *seq = (const BlxSequence*)(listItem->data);
      sequence = blxSequenceGetSequence(seq);

      if (sequence)
        {
          BlxContext *bc = blxWindowGetContext(blxWindow);
          const gboolean reverse = (bc->displayRev != (seq->strand == BLXSTRAND_REVERSE));
          int fromIdx = fromIdx_in;
          int toIdx = toIdx_in;

          /* Coords can be passed in any order. Swap them if from > to */
          if (!reverse && fromIdx > toIdx)
            {
              int tmp = fromIdx;
              fromIdx = toIdx;
              toIdx = tmp;
            }
          else if (reverse && fromIdx < toIdx)
            {
              int tmp = fromIdx;
              fromIdx = toIdx;
              toIdx = tmp;
            }
          
          /* Find the match-sequence coord at these ref-seq coords */
          GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
          const int numUnalignedBases = detailViewGetNumUnalignedBases(detailView);

          /* Loop through all msps and look for one which contains both the start and end coord. */
          int matchStart = 0, matchEnd = 0;
          GList *mspItem = seq->mspList;
          const MSP *msp = NULL;

          for ( ; mspItem && !msp; mspItem = mspItem->next)
            {
              const MSP *check_msp = (const MSP*)(mspItem->data);

              if (mspGetMatchCoord(check_msp, fromIdx, TRUE, numUnalignedBases, bc, &matchStart) &&
                  mspGetMatchCoord(check_msp, toIdx, TRUE, numUnalignedBases, bc, &matchEnd))
                {
                  msp = check_msp;
                  break;
                }
            }

          if (msp)
            {
              /* Match coords are 1-based: convert to 0-based */
              --matchStart;
              --matchEnd;

              /* Match coords may be opposite direction to ref coords so make sure start < end */
              if (matchStart > matchEnd)
                {
                  int tmp = matchStart;
                  matchStart = matchEnd;
                  matchEnd = tmp;
                }

              const int sourceLen = strlen(sequence);
              const int len = matchEnd - matchStart + 1;
              DEBUG_OUT("Copying %s (%d, %d) (len=%d) for ref seq (%d, %d)\n", 
                        blxSequenceGetName(seq), matchStart, matchEnd, sourceLen, fromIdx, toIdx);

              if (matchStart >= sourceLen || matchEnd >= sourceLen)
                {
                  g_critical("Coordinates (%d, %d) are not within match sequence length %d", matchStart, matchEnd, sourceLen);
                }
              else if (len > sourceLen)
                {
                  g_critical("Match sequence range (%d, %d) is longer than match sequence length (%d)", matchStart, matchEnd, sourceLen);
                }
              else if (len < 0)
                {
                  g_critical("Error: range length is negative (ref = %d, %d, match = %d, %d, len=%d)", 
                             fromIdx, toIdx, matchStart, matchEnd, len);
                }
              else if (len == 0)
                {
                  g_critical("Error: range length is 0");
                }
              else if (matchStart > matchEnd)
                {
                  g_critical("Error copying sequence: match sequence start coord is less than the end coord");
                }
              else
                {
                  /* Ok, now do the copy */

                  /* Warn user if they're about to copy a large sequence and ask to confirm */
                  if (len <= MAX_RECOMMENDED_COPY_LENGTH || 
                      runConfirmationBox(blxWindow, "Copy sequence", "You are about to copy a large amount of text to the clipboard\n\nAre you sure you want to continue?") == GTK_RESPONSE_ACCEPT)
                    {
                      char *displayText = g_strndup(sequence + matchStart, len);

                      /* If the msp is in the opposite strand to the forward-direction strand (as
                       * determined by displayRev) then we need to reverse the result */
                      if (bc->displayRev != (msp->qStrand == BLXSTRAND_REVERSE))
                        displayText = g_strreverse(displayText);

                      setDefaultClipboardText(displayText);
                      g_message("Copied sequence data for %s (%d, %d) to clipboard (%d, %d)\n", 
                                blxSequenceGetName(seq), matchStart + 1, matchEnd + 1, fromIdx, toIdx);

                      g_free(displayText);
                    }
                }
            }
          else
            {
              g_critical("Failed to find a match from sequence '%s' which contains the range (%d, %d)", 
                         blxSequenceGetName(seq), fromIdx, toIdx);
            }
        }
    }
}


/* This gets the ref seq segment for the given range of coords. Returns a newly allocated string
 * which should be free'd by the caller with g_free */
static char* getRefSeqSegment(GtkWidget *blxWindow, const int fromIdx_in, const int toIdx_in)
{
  DEBUG_ENTER("getRefSeqSegment()");

  char *result = NULL;

  const char *refSeq = blxWindowGetRefSeq(blxWindow);
  BlxContext *bc = blxWindowGetContext(blxWindow);

  if (refSeq)
    {
      /* Need to get 0-based indices */
      const IntRange* const refSeqRange = blxWindowGetRefSeqRange(blxWindow);
      
      const int fromIdx = min(fromIdx_in, toIdx_in) - refSeqRange->min();
      const int toIdx = max(fromIdx_in, toIdx_in) - refSeqRange->min();
      const int len = toIdx - fromIdx + 1;

      /* Warn user if they're about to copy a large sequence */
      if (len <= MAX_RECOMMENDED_COPY_LENGTH || 
          runConfirmationBox(blxWindow, "Copy sequence", "You are about to copy a large amount of text to the clipboard\n\nAre you sure you want to continue?") == GTK_RESPONSE_ACCEPT)
        {
          result = g_strndup(refSeq + fromIdx, toIdx - fromIdx + 1);
      
          if (result)
            {
              /*! \todo Currently in dna mode the active strand pane is ignored (the strand is
               * forward if the display is forward and vice versa). We could find the strand here
               * that the user last clicked on and return the sequence for that. Needs a bit of
               * thought to get all the directionality right. */
              //GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
              //BlxStrand strand = detailViewGetSelectedStrand(detailView);

              if (bc->displayRev)
                {
                  char *tmp = (char*)g_malloc(strlen(result) + 1);
                  revComplement(tmp, result);
                  g_free(result);
                  result = tmp;
                }
            }
        }
    }
  else
    {
      DEBUG_OUT("No reference sequence!\n");
    }

  DEBUG_EXIT("getRefSeqSegment returning %s", result);

  return result;
}


/* This function copies the reference sequence, from the 
 * clicked position to the marked position, onto the clipboard. */
static void copyRefSeqToClipboard(GtkWidget *blxWindow, const int fromIdx, const int toIdx)
{
  char *dnaSeq = getRefSeqSegment(blxWindow, fromIdx, toIdx);

  if (dnaSeq)
    {
      setDefaultClipboardText(dnaSeq);
      g_message("Copied reference sequence from %d to %d\n", fromIdx, toIdx);

      g_free(dnaSeq);
    }
  else
    {
      g_critical("Error getting DNA sequence for %d to %d\n", fromIdx, toIdx);
    }
}


/* This function copies the reference sequence, from the 
 * clicked position to the marked position, onto the clipboard. */
static void copyRefSeqTranslationToClipboard(GtkWidget *blxWindow, const int fromIdx, const int toIdx)
{
  DEBUG_ENTER("copyRefSeqTranslationToClipboard()");

  BlxContext *bc = blxWindowGetContext(blxWindow);
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);

  if (bc && detailView)
    {
      char *dnaSeq = getRefSeqSegment(blxWindow, fromIdx, toIdx);

      if (dnaSeq)
        {
          int requiredFrame = detailViewGetActiveFrame(detailView);
          int offset = 0;

          if (bc->seqType == BLXSEQ_PEPTIDE)
            {
              /* Get the offset from the start frame of the dna seq to the required reading frame */
              int curFrame = fromIdx % 3;

              /* If the display is reversed, use the end coord to calculate frame */
              if (bc->displayRev)
                curFrame = toIdx % 3;

              if (curFrame < 1)
                curFrame += 3;

              int offset = requiredFrame - curFrame;
              if (offset < 0)
                offset += 3;
            }

          char *pepSeq = blxTranslate(dnaSeq + offset, bc->geneticCode);

          if (pepSeq)
            {
              setDefaultClipboardText(pepSeq);
              
              g_message("Copied reference sequence translation from %d to %d for frame %d\n", fromIdx, toIdx, requiredFrame);
              g_free(pepSeq);
            }
          else
            {
              g_critical("Error getting translation of DNA sequence from %d to %d for frame %d\n", fromIdx, toIdx, requiredFrame);
            }

          g_free(dnaSeq);
        }
      else
        {
          g_critical("Error getting DNA sequence for %d to %d\n", fromIdx, toIdx);
        }
    }

  DEBUG_EXIT("copyRefSeqTranslationToClipboard returning ");
}


/* Update function to be called whenever the MSP selection has changed */
void blxWindowSelectionChanged(GtkWidget *blxWindow)
{
  GtkWidget *detailView = blxWindowGetDetailView(blxWindow);
  BlxContext *bc = blxWindowGetContext(blxWindow);

  if ((bc->flags[BLXFLAG_SHOW_UNALIGNED] && bc->flags[BLXFLAG_SHOW_UNALIGNED_SELECTED]) ||
      (bc->flags[BLXFLAG_SHOW_POLYA_SITE] && bc->flags[BLXFLAG_SHOW_POLYA_SITE_SELECTED]))
    {
      /* When these options are enabled, the length of an MSP can depend on
       * whether it is selected or not; changing the selection can therefore
       * affect which MPSs are visible, so we need to re-filter the trees */
      refilterDetailView(detailView, NULL);
    }
  
  /* Redraw */
  updateFeedbackBox(detailView);
  blxWindowRedrawAll(blxWindow);
  
  /* Copy the selected sequence names to the PRIMARY clipboard */
  GString *displayText = blxWindowGetSelectedSeqNames(blxWindow);
  setPrimaryClipboardText(displayText->str);
  g_string_free(displayText, TRUE);
  
  /* Refresh the dotter dialog, if it happens to be open */
  refreshDialog(BLXDIALOG_DOTTER, blxWindow);
}


/* Call this function to select the given match sequence */
void blxWindowSelectSeq(GtkWidget *blxWindow, BlxSequence *seq)
{
  if (!blxWindowIsSeqSelected(blxWindow, seq))
    {
      BlxContext *blxContext = blxWindowGetContext(blxWindow);
      blxContext->selectedSeqs = g_list_append(blxContext->selectedSeqs, seq);
      blxWindowSelectionChanged(blxWindow);
    }
}


/* Utility function to set the list of selected sequences */
void blxWindowSetSelectedSeqList(GtkWidget *blxWindow, GList *seqList)
{
  blxWindowDeselectAllSeqs(blxWindow);

  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  blxContext->selectedSeqs = seqList;

  blxWindowSelectionChanged(blxWindow);
}


/* Call this function to deselect the given sequence */
void blxWindowDeselectSeq(GtkWidget *blxWindow, BlxSequence *seq)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);

  /* See if it's in the list and, if so, get a pointer to the list element */
  GList *foundSeq = g_list_find(blxContext->selectedSeqs, seq);

  if (foundSeq)
    {
      blxContext->selectedSeqs = g_list_remove(blxContext->selectedSeqs, foundSeq->data);
      blxWindowSelectionChanged(blxWindow);
    }
}


/* Call this function to deselect all sequences */
void blxWindowDeselectAllSeqs(GtkWidget *blxWindow)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);

  if (g_list_length(blxContext->selectedSeqs) > 0)
    {
      g_list_free(blxContext->selectedSeqs);
      blxContext->selectedSeqs = NULL;
      blxWindowSelectionChanged(blxWindow);
    }
}

/* Returns true if the given sequence is selected */
gboolean blxWindowIsSeqSelected(GtkWidget *blxWindow, const BlxSequence *seq)
{
  BlxContext *blxContext = blxWindowGetContext(blxWindow);
  return blxContext->isSeqSelected(seq);
}


/* Set the given sequence as selected or unselected, depending on the given argument */
void blxWindowSetSeqSelected(GtkWidget *blxWindow, BlxSequence *seq, const gboolean selected)
{
  if (selected)
    {
      blxWindowSelectSeq(blxWindow, seq);
    }
  else
    {
      blxWindowDeselectSeq(blxWindow, seq);
    }
}


BlxSequence* blxWindowGetLastSelectedSeq(GtkWidget *blxWindow)
{
  BlxSequence *result = NULL;
  
  GList *selectedSeqs = blxWindowGetSelectedSeqs(blxWindow);
  
  if (g_list_length(selectedSeqs) > 0)
    {
      /* Get the last-selected sequence */
      GList *lastItem = g_list_last(selectedSeqs);
      result = (BlxSequence*)(lastItem->data);
    }
  
  return result;
}


/***********************************************************
 *                      Initialisation                     *
 ***********************************************************/

static void onDragDataReceived(GtkWidget *widget, 
                               GdkDragContext *context, 
                               int x, 
                               int y,
                               GtkSelectionData *selectionData, 
                               guint info, 
                               guint time,
                               gpointer userdata)
{
  DEBUG_ENTER("onDragDataReceived()");

  g_return_if_fail(selectionData);

  if ((info == TARGET_STRING || info == TARGET_URL) && selectionData->data)
    {
      DEBUG_OUT("Received drag and drop text:\n%s\n", selectionData->data);
      GError *tmp_error = NULL;
      
      /* For now just assume the text contains supported file contents. The file parsing
       * will fail if it's not a supported format. */
      char *text = (char*)(gtk_selection_data_get_text(selectionData));
      dynamicLoadFeaturesFile(widget, NULL, text, &tmp_error);

      if (tmp_error)
        {
          prefixError(tmp_error, "Error processing text from drag-and-drop: ");
          reportAndClearIfError(&tmp_error, G_LOG_LEVEL_CRITICAL);
        }
    }

  DEBUG_EXIT("onDragDataReceived returning ");
}


/* Called when the user moves the cursor over the window during a drag */
static gboolean onDragMotion(GtkWidget *widget, GdkDragContext *event, gint x, gint y, guint time, gpointer data)
{
  /* Bring the blixem window to the front so the user can see where they're going to drop */
  gtk_window_present(GTK_WINDOW(widget));

  /* Return true to indicate that the whole window is a drop zone */
  return TRUE;
}


static void setDragDropProperties(GtkWidget *widget)
{
  DEBUG_ENTER("setDragDropProperties()");

  static GtkTargetEntry targetentries[] =
    {
      { (gchar*)"STRING",        0, TARGET_STRING },
      { (gchar*)"text/plain",    0, TARGET_STRING },
      { (gchar*)"text/uri-list", 0, TARGET_URL },
    };
  
  gtk_drag_dest_set(widget, GTK_DEST_DEFAULT_ALL, targetentries, 3,
                    (GdkDragAction)(GDK_ACTION_COPY|GDK_ACTION_MOVE|GDK_ACTION_LINK));
 
  g_signal_connect(widget, "drag-data-received", G_CALLBACK(onDragDataReceived), NULL);
  g_signal_connect(widget, "drag-motion", G_CALLBACK(onDragMotion),	NULL);
  
  DEBUG_EXIT("setDragDropProperties returning ")
}

/* Set various properties for the blixem window */
static void setStyleProperties(GtkWidget *widget, char *windowColor)
{
  DEBUG_ENTER("setStyleProperties()");

  /* Set the initial window size based on some fraction of the screen size */
  int width = 300, height = 200;
  gbtools::GUIGetTrueMonitorSizeFraction(widget, DEFAULT_WINDOW_WIDTH_FRACTION, DEFAULT_WINDOW_HEIGHT_FRACTION,
                                  &width, &height);
  
  gtk_window_set_default_size(GTK_WINDOW(widget), width, height);
  
  gtk_container_set_border_width (GTK_CONTAINER(widget), DEFAULT_WINDOW_BORDER_WIDTH); 
  gtk_window_set_mnemonic_modifier(GTK_WINDOW(widget), GDK_MOD1_MASK); /* MOD1 is ALT on most systems */
  
  /* Set the default font size to be a bit smaller than usual */
  int origSize = pango_font_description_get_size(widget->style->font_desc) / PANGO_SCALE;
  const char *origFamily = pango_font_description_get_family(widget->style->font_desc);

  char parseString[500];
  sprintf(parseString, "gtk-font-name = \"%s %d\"", origFamily, origSize + DEFAULT_FONT_SIZE_ADJUSTMENT);
  gtk_rc_parse_string(parseString);

  DEBUG_EXIT("setStyleProperties returning ");
}


/* Create the main menu */
static void createMainMenu(GtkWidget *window,
                           BlxContext *bc,
                           GtkWidget **mainmenu,
                           GtkWidget **seqHeaderMenu,
                           GtkWidget **toolbar,
                           GtkActionGroup **actionGroupOut)
{
  GtkActionGroup *action_group = gtk_action_group_new ("MenuActions");
  
  if (actionGroupOut)
    *actionGroupOut = action_group;

  /* Set the squash-matches toggle button depending on the initial state */
  toggleMenuEntries[0].is_active = (bc->modelId == BLXMODEL_SQUASHED);  

  gtk_action_group_add_actions (action_group, mainMenuEntries, G_N_ELEMENTS (mainMenuEntries), window);
  gtk_action_group_add_toggle_actions(action_group, toggleMenuEntries, G_N_ELEMENTS (toggleMenuEntries), window);

  GtkUIManager *ui_manager = gtk_ui_manager_new ();
  gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
  
  GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (ui_manager);
  gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
  
  GError *error = NULL;
  gboolean ok = gtk_ui_manager_add_ui_from_string (ui_manager, standardMenuDescription, -1, &error);
  
  if (ok && userIsDeveloper())
    ok = gtk_ui_manager_add_ui_from_string (ui_manager, developerMenuDescription, -1, &error);
    
  if (!ok)
    {
      prefixError(error, "Building menus failed: ");
      reportAndClearIfError(&error, G_LOG_LEVEL_ERROR);
    }
  
  *mainmenu = gtk_ui_manager_get_widget (ui_manager, "/ContextMenu");
  *seqHeaderMenu = gtk_ui_manager_get_widget (ui_manager, "/SeqHeaderContextMenu");
  *toolbar = gtk_ui_manager_get_widget (ui_manager, "/Toolbar");
}


/* calcID: caculated percent identity of an MSP
 * 
 * There seems to be a general problem with this routine for protein
 * alignments, the existing code certainly does not do the right thing.
 * I have fixed this routine for gapped sequence alignments but not for
 * protein stuff at all.
 * 
 * To be honest I think this routine is a _waste_ of time, the alignment
 * programs that feed data to blixem produce an identity anyway so why
 * not use that...why reinvent the wheel......
 * 
 * */
static void calcID(MSP *msp, BlxContext *bc)
{
  const gboolean sForward = (mspGetMatchStrand(msp) == BLXSTRAND_FORWARD);
  const gboolean qForward = (mspGetRefStrand(msp) == BLXSTRAND_FORWARD);
  
  if (mspIsBlastMatch(msp) && msp->id < 0) /* Only calculate if ID is not already set */
    {
      msp->id = 0.0;

      /* If there is no sequence data, leave the ID as zero */
      const char *matchSeq = mspGetMatchSeq(msp);

      if (matchSeq)
        {
          /* Note that this will reverse complement the ref seq if it is the reverse 
           * strand. This means that where there is no gaps array the comparison is trivial
           * as coordinates can be ignored and the two sequences just whipped through. */
          GError *error = NULL;
          IntRange qRange(msp->qRange); /* make a copy because it will be updated */
          
          char *refSeqSegment = getSequenceSegment(bc->refSeq,
                                                   &qRange,
                                                   mspGetRefStrand(msp), 
                                                   BLXSEQ_DNA,        /* msp q coords are always nucleotide coords */
                                                   bc->seqType,       /* required seq type is the display seq type */
                                                   mspGetRefFrame(msp, bc->seqType),
                                                   bc->numFrames,
                                                   &bc->refSeqRange,
                                                   bc->blastMode,
                                                   bc->geneticCode,
                                                   bc->displayRev,
                                                   !qForward,
                                                   TRUE,
                                                   &error);
          
          if (!refSeqSegment)
            {
              prefixError(error, "Failed to calculate ID for sequence '%s' (match coords = %d - %d). ", mspGetSName(msp), msp->sRange.min(), msp->sRange.max());
              reportAndClearIfError(&error, G_LOG_LEVEL_CRITICAL);
              return;
            }
          else
            {
              /* If there's an error but the sequence was still returned it's 
               * a non-critical warning. Only issue one warning because we can
               * get many thousands and it can fill up the terminal if we output
               * them all. */
              if (error)
                {
                  static gboolean done = FALSE;
                  
                  if (!done)
                    {
                      g_warning("There were errors calculating the percent ID for some sequences because the match extends out of the reference sequence range; some IDs may be incorrect.\n");
                      done = TRUE;
                    }
                  
                  g_error_free(error);
                  error = NULL;
                }
            }
          
          /* We need to find the number of characters that match out of the total number */
          int numMatchingChars = 0;
          int totalNumChars = 0;
          const int numGaps = msp->gaps ? g_slist_length(msp->gaps) : 0;
          
          if (numGaps == 0)
            {
              /* Ungapped alignments. */
              totalNumChars = qRange.length() / bc->numFrames;

              if (bc->blastMode == BLXMODE_TBLASTN || bc->blastMode == BLXMODE_TBLASTX)
                {
                  int i = 0;
                  for ( ; i < totalNumChars; i++)
                    {
                      if (toupper(matchSeq[i]) == toupper(refSeqSegment[i]))
                        {
                          numMatchingChars++;
                        }
                    }
                }
              else                                                  /* blastn, blastp & blastx */
                {
                  int i = 0;
                  for ( ; i < totalNumChars; i++)
                    {
                      int sIndex = sForward ? msp->sRange.min() + i - 1 : msp->sRange.max() - i - 1;
                      if (toupper(matchSeq[sIndex]) == toupper(refSeqSegment[i]))
                        {
                          numMatchingChars++;
                        }
                    }
                }
            }
          else
            {
              /* Gapped alignments. */

              /* To do tblastn and tblastx is not imposssible but would like to work from
               * examples to get it right.... */
              if (bc->blastMode == BLXMODE_TBLASTN)
                {
                  g_message("not implemented yet\n") ;
                }
              else if (bc->blastMode == BLXMODE_TBLASTX)
                {
                  g_message("not implemented yet\n") ;
                }
              else
                {
                  /* blastn and blastp remain simple but blastx is more complex since the query
                   * coords are nucleic not protein. */
                  GSList *rangeItem = msp->gaps;
                  
                  for ( ; rangeItem; rangeItem = rangeItem->next)
                    {
                      CoordRange *range = (CoordRange*)(rangeItem->data);
                      
                      int qRangeMin = 0, qRangeMax = 0, sRangeMin = 0, sRangeMax = 0;
                      getCoordRangeExtents(range, &qRangeMin, &qRangeMax, &sRangeMin, &sRangeMax);
                      
                      totalNumChars += sRangeMax - sRangeMin + 1;
                      
                      /* Note that refSeqSegment is just the section of the ref seq relating to this msp.
                       * We need to translate the first coord in the range (which is in terms of the full
                       * reference sequence) into coords in the cut-down ref sequence. */
                      int q_start = qForward ? (qRangeMin - qRange.min()) / bc->numFrames : (qRange.max() - qRangeMax) / bc->numFrames;
                      const int qLen = strlen(refSeqSegment);
                      
                      /* We can index sseq directly (but we need to adjust by 1 for zero-indexing). We'll loop forwards
                       * through sseq if we have the forward strand or backwards if we have the reverse strand,
                       * so start from the lower or upper end accordingly. */
                      int s_start = sForward ? sRangeMin - 1 : sRangeMax - 1 ;

                      int sIdx = s_start, qIdx = q_start ;
                      while (((sForward && sIdx < sRangeMax) || (!sForward && sIdx >= sRangeMin - 1)) && qIdx < qLen)
                        {
                          /* Check that qIdx is not less that 0, which could happen if we have somehow got duff data. */
                          if (qIdx >= 0 && toupper(matchSeq[sIdx]) == toupper(refSeqSegment[qIdx]))
                            {
                              numMatchingChars++ ;
                            }

                          /* Move to the next base. The refSeqSegment is always forward, but we might have to
                           * traverse the s sequence in reverse. */
                          ++qIdx ;
                          if (sForward) ++sIdx ;
                          else --sIdx ;
                        }
                    }
                }
            }
          
          msp->id = (100.0 * numMatchingChars / totalNumChars);
          
          g_free(refSeqSegment);
        }
    }
  
  return ;
}


/* Calculate the ID and q frame for the given MSP and store 
 * them in the MSP struct. Returns the calculated ID (or UNSET_INT if this msp
 * is not a blast match). */
static void calcMspData(MSP *msp, BlxContext *bc)
{  
  /* Calculate the ID */
  if (mspIsBlastMatch(msp))
    {
      calcID(msp, bc);
    }
}


static gdouble calculateMspData(MSP *mspList, BlxContext *bc)
{
  MSP *msp = mspList;
  gdouble lowestId = -1.0;
  
  for ( ; msp; msp = msp->next)
    {
      calcMspData(msp, bc);
      
      if (mspIsBlastMatch(msp) && (lowestId == -1.0 || msp->id < lowestId))
        {
          lowestId = msp->id;
        }
    }
  
  return lowestId;
}


/* Calculate the reference sequence range from the range and offset given in 
 * the option. Also translate this to display coords. */
static void calculateRefSeqRange(CommandLineOptions *options,
                                 IntRange &refSeqRange,
                                 IntRange &fullDisplayRange)
{
  
  /* Offset the reference sequence range, if an offset was specified. */ 
  refSeqRange.set(options->refSeqRange);
  refSeqRange.set(refSeqRange.min() + options->refSeqOffset,
                  refSeqRange.max() + options->refSeqOffset);
  
  fullDisplayRange.set(refSeqRange);
  
  if (options->seqType == BLXSEQ_PEPTIDE)
    {
      /* Adjust the reference sequence reading frame so that it always starts at
        * base 1 in frame 1. This makes the display easier because we can always just
        * start drawing from the 1st base in the reference sequence. */
      int base = UNSET_INT;
      convertDnaIdxToDisplayIdx(refSeqRange.min(), options->seqType, 1, options->numFrames, FALSE, &refSeqRange, &base);
      
      int offset = (options->numFrames - base + 1);
      
      if (offset >= options->numFrames)
          offset -= options->numFrames;
      
      refSeqRange.setMin(refSeqRange.min() + offset);
      options->refSeq = options->refSeq + offset;
      
      /* Now do the same for when the ref seq is reversed */
      convertDnaIdxToDisplayIdx(refSeqRange.max(), options->seqType, 1, options->numFrames, TRUE, &refSeqRange, &base);
      offset = (options->numFrames - base + 1);
      
      if (offset >= options->numFrames) 
          offset -= options->numFrames;
      
      refSeqRange.setMax(refSeqRange.max() - offset);
      options->refSeq[refSeqRange.length()] = '\0';
      
      /* Now calculate the full display range in display coords */
      fullDisplayRange.setMin(convertDnaIdxToDisplayIdx(refSeqRange.min(), options->seqType, 1, options->numFrames, FALSE, &refSeqRange, NULL));
      fullDisplayRange.setMax(convertDnaIdxToDisplayIdx(refSeqRange.max(), options->seqType, 1, options->numFrames, FALSE, &refSeqRange, NULL));
    }  
}


/* Create the main blixem window */
GtkWidget* createBlxWindow(CommandLineOptions *options, 
                           const char *paddingSeq, 
                           GArray* featureLists[],
                           GList *seqList, 
                           GSList *supportedTypes,
                           const gboolean External,
                           GSList *styles)
{
  IntRange refSeqRange;
  IntRange fullDisplayRange;
  
  calculateRefSeqRange(options, refSeqRange, fullDisplayRange);
  
  g_message("Reference sequence [%d - %d], display range [%d - %d]\n", 
            refSeqRange.min(), refSeqRange.max(), fullDisplayRange.min(), fullDisplayRange.max());
  
  /* Offset the start coords, if applicable, and convert it to display coords */
  int startCoord = options->startCoord + options->refSeqOffset;
  if (options->seqType == BLXSEQ_PEPTIDE)
    startCoord = convertDnaIdxToDisplayIdx(startCoord, options->seqType, 1, options->numFrames, FALSE, &refSeqRange, NULL);

  if (options->bigPictRange.isSet())
    {
      /* Apply any offset */
      options->bigPictRange.set(options->bigPictRange.min() + options->refSeqOffset,
                                options->bigPictRange.max() + options->refSeqOffset);

      /* Make sure the big picture range is not outside our ref seq range */
      options->bigPictRange.boundsLimit(&refSeqRange, FALSE);

      if (options->seqType == BLXSEQ_PEPTIDE)
        {
          /* Convert to peptide coords */
          options->bigPictRange.setMin(convertDnaIdxToDisplayIdx(options->bigPictRange.min(), options->seqType, 1, options->numFrames, FALSE, &refSeqRange, NULL));
          options->bigPictRange.setMax(convertDnaIdxToDisplayIdx(options->bigPictRange.max(), options->seqType, 1, options->numFrames, FALSE, &refSeqRange, NULL));
        }
    }
  
  
  
  /* Create the main blixem window */
  GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  setStyleProperties(window, options->windowColor);
  setDragDropProperties(window);

  /* Create a status bar */
  GtkWidget *statusBar = gtk_statusbar_new();
  gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(statusBar), TRUE);
  setStatusBarShadowStyle(statusBar, "GTK_SHADOW_NONE");
  
  /* Set the window and statusbar in the message handler data, now that we know them */
  options->msgData.parent = GTK_WINDOW(window);
  options->msgData.statusBar = GTK_STATUSBAR(statusBar);
  
  /* Create a vertical box to pack everything in */
  GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), vbox);
  
  /* Create the widgets. We need a single adjustment for the entire detail view, which will also be referenced
   * by the big picture view, so create it first. */
  GtkAdjustment *detailAdjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, /* initial value = 0 */
                                                                      fullDisplayRange.min(), /* lower value */
                                                                      fullDisplayRange.max(), /* upper value */
                                                                      DEFAULT_SCROLL_STEP_INCREMENT, /* step increment used for mouse wheel scrolling */
                                                                      0,   /* page increment dynamically set based on display range */
                                                                      0)); /* page size dunamically set based on display range */
  
  BlxContext *blxContext = new BlxContext(options, 
                                          &refSeqRange, 
                                          &fullDisplayRange, 
                                          paddingSeq, 
                                          featureLists,
                                          seqList, 
                                          supportedTypes,
                                          window, 
                                          statusBar,
                                          External,
                                          styles);

  /* Create the main menu */
  GtkWidget *mainmenu = NULL;
  GtkWidget *seqHeaderMenu = NULL;
  GtkWidget *toolbar = NULL;
  GtkActionGroup *actionGroup = NULL;
  createMainMenu(window, blxContext, &mainmenu, &seqHeaderMenu, &toolbar, &actionGroup);
  
  const gdouble lowestId = calculateMspData(options->mspList, blxContext);
  
  GtkWidget *fwdStrandGrid = NULL, *revStrandGrid = NULL;

  /* Create the two main sections - the big picture and detail view - in a paned window */
  GtkWidget *panedWin = gtk_vpaned_new();
  gtk_box_pack_start(GTK_BOX(vbox), panedWin, TRUE, TRUE, 0);
  
  GtkWidget *bigPicture = createBigPicture(window,
                                           blxContext,
                                           GTK_CONTAINER(panedWin),
                                           &fwdStrandGrid, 
                                           &revStrandGrid,
                                           &options->bigPictRange,
                                           &refSeqRange,
                                           options->bigPictZoom,
                                           lowestId);

  GtkWidget *detailView = createDetailView(window,
                                           blxContext,
                                           GTK_CONTAINER(panedWin),
                                           toolbar,
					   detailAdjustment, 
					   fwdStrandGrid, 
					   revStrandGrid,
					   options->mspList,
                                           options->columnList,
					   options->blastMode,
					   options->seqType,
					   options->numFrames,
					   options->refSeqName,
					   startCoord,
					   options->sortInverted,
					   options->initSortColumn,
                                           options->optionalColumns,
                                           options->windowColor);

  
  /* Create a custom scrollbar for scrolling the sequence column and put it at the bottom of the window */
  GtkWidget *scrollBar = createDetailViewScrollBar(detailAdjustment, detailView);
  gtk_box_pack_start(GTK_BOX(vbox), scrollBar, FALSE, TRUE, 0);

  
  /* Put the statusbar at the bottom */
  gtk_box_pack_start(GTK_BOX(vbox), statusBar, FALSE, TRUE, 0);

  
  /* Set required data for the blixem window */
  blxWindowCreateProperties(options,
                            blxContext,
                            window, 
                            bigPicture, 
                            detailView, 
                            mainmenu,
                            seqHeaderMenu,
                            actionGroup,
                            &refSeqRange, 
                            &fullDisplayRange,
                            paddingSeq);
  
  /* Connect signals */
  g_signal_connect(G_OBJECT(panedWin), "button-press-event", G_CALLBACK(onButtonPressPanedWin), window);
  g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(onButtonPressBlxWindow), mainmenu);
  g_signal_connect(G_OBJECT(window), "key-press-event", G_CALLBACK(onKeyPressBlxWindow), NULL);
  
  
  const int numUnalignedBases = detailViewGetNumUnalignedBases(detailView);
  cacheMspDisplayRanges(blxContext, numUnalignedBases);

  /* Add the MSP's to the trees and sort them by the initial sort mode. This must
   * be done after all widgets have been created, because it accesses their properties.*/
  detailViewAddMspData(detailView, options->mspList, seqList);
  detailViewUpdateMspLengths(detailView, numUnalignedBases);

  /* Updated the cached display range and full extents of the MSPs */
  if (blxContext)
    blxContext->calculateDepth(numUnalignedBases);

  updateCoverageDepth(window);
  
  /* Set the detail view font (again, this accesses the widgets' properties). */
  updateDetailViewFontDesc(detailView);

  /* Calculate the number of vertical cells in the grids (again, requires properties) */
  calculateNumVCells(bigPicture);


  /* Realise the widgets */
  g_message_info("Starting %s\n", g_get_prgname());
  gtk_widget_show_all(window);


  /* If the options say to make the reverse strand the active strand, toggle the display now */
  if (options->activeStrand == BLXSTRAND_REVERSE)
    toggleStrand(detailView);

  /* Hide the coverage view by default (unless told to display it) */
  if (!options->coverageOn)
    coverageSetHidden(window, TRUE);
  
  /* The trees use the normal model by default, so if we're starting in 
   * 'squash matches' mode we need to change the model */
  if (blxContext->modelId == BLXMODEL_SQUASHED)
    {
      callFuncOnAllDetailViewTrees(detailView, treeUpdateSquashMatches, NULL);
      detailViewRedrawAll(detailView);
    }
  
  /* If the options say to hide the inactive strand, hide it now. (This must be done
   * after showing the widgets, or it will get shown again in show_all.). To do: we just
   * hide the grid at the moment; hide the detail-view pane as well?  */
  if (options->hideInactive && options->activeStrand == BLXSTRAND_FORWARD)
    widgetSetHidden(revStrandGrid, TRUE);
  else if (options->hideInactive && options->activeStrand == BLXSTRAND_REVERSE)
    widgetSetHidden(fwdStrandGrid, TRUE);

  if (!options->bigPictON)
    widgetSetHidden(bigPicture, TRUE);

  if (options->sortInverted)
    {
      blxContext->flags[BLXFLAG_INVERT_SORT] = TRUE;
      detailViewUpdateSortInverted(detailView, options->sortInverted);
    }
  
  /* Set the initial column widths. (This must be called after the widgets are 
   * realised because it causes the scroll range to be updated, which in turn causes
   * the big picture range to be set. The widgets must be realised before this because
   * the initial big picture range depends on the detail view range, which is calculated
   * from its window's width, and this will be incorrect if it has not been realised.) */
  updateDynamicColumnWidths(detailView);
  
  /* Just once, at the start, update the visibility of all tree rows. (After this,
   * filter updates will be done on affected rows only.) */
  /* gb10: already done by updatemsplengths but still required at this point or matches
   * do not display - look into whether we can remove the previous calls because refilter
   * and resort are slow when there are many thousands of reads */
  callFuncOnAllDetailViewTrees(detailView, refilterTree, NULL);
  detailViewResortTrees(detailView);
  
  /* Calculate initial size of the exon views (depends on big picture range) */
  calculateExonViewHeight(bigPictureGetFwdExonView(bigPicture));
  calculateExonViewHeight(bigPictureGetRevExonView(bigPicture));
  forceResize(bigPicture);

  /* If the primary clipboard contains features, select them on startup */
  requestPrimaryClipboardText(findSeqsFromClipboard, window);
  
  return window;
}