File: belvuAlignment.cpp

package info (click to toggle)
seqtools 4.44.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,496 kB
  • sloc: cpp: 53,636; sh: 12,232; makefile: 386
file content (1861 lines) | stat: -rw-r--r-- 70,552 bytes parent folder | download | duplicates (5)
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
/*  File: belvuAlignment.c
 *  Author: Gemma Barson, 2011-04-12
 *  Copyright (c) 2011 - 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 belvuAlignment.h
 *----------------------------------------------------------------------------
 */

#include "belvuApp/belvuAlignment.hpp"
#include "belvuApp/belvuWindow.hpp"
#include <math.h>
#include <algorithm>

using namespace std;


#define DEFAULT_XPAD                            2
#define DEFAULT_YPAD                            4
#define DEFAULT_PADDING_CHARS                   1  /* number of char widths to use to pad between columns */
#define SEQ_AREA_PADDING                        2  /* padding around the sequence area, in pixels */
#define DEFAULT_NAME_COLUMN_PADDING_CHARS       2  /* number of char widths to use to pad after the name column */
#define WRAP_DISPLAY_PADDING_CHARS              4
#define TICKMARK_INTERVAL                       10 /* number of coords between each tick marker in the sequence area header */
#define MAJOR_TICKMARK_HEIGHT                   6  /* height of major tick marks in the sequence area header */
#define MINOR_TICKMARK_HEIGHT                   3  /* height of minor tick marks in the sequence area header */

/* Local function declarations */
static void               bg2fgColor(BelvuContext *bc, GdkColor *bgColor, GdkColor *result);


/* Properties specific to the belvu alignment */
class BelvuAlignmentProperties
{
public:
  GtkWidget *widget;
  BelvuContext *bc;                 /* The belvu context */
  
  GtkWidget *columnsArea;           /* Drawing widget for the columns (i.e. name and coord columns) */
  GtkWidget *columnsHeader;         /* Drawing widget for the header for the the columns area */
  GtkWidget *seqArea;               /* Drawing widget for the actual sequence */
  GtkWidget *seqHeader;             /* Drawing widget for the header for the sequence area */
  GdkRectangle columnsRect;         /* Drawing area for the columns (i.e. name and coord columns) */
  GdkRectangle seqRect;             /* Drawing area for the actual sequence */
  GdkRectangle seqHeaderRect;       /* Drawing area for the sequence header */
  GtkAdjustment *hAdjustment;       /* Controls horizontal scroll bar */
  GtkAdjustment *vAdjustment;       /* Controls vertical scroll bar */
  
  int columnPadding;                /* Padding in between columns, in pixels */
  int nameColumnPadding;            /* Padding after name column, in pixels */
  char *title;                /* title to display at the top of the alignments */
  int wrapWidth;                    /* Number of characters after which to wrap (or UNSET_INT for no wrapping) */
  
  gdouble charWidth;                /* The width of each character in the display */
  gdouble charHeight;               /* The height of each character in the display */
};


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

static BelvuAlignmentProperties* belvuAlignmentGetProperties(GtkWidget *widget)
{
  return widget ? (BelvuAlignmentProperties*)(g_object_get_data(G_OBJECT(widget), "BelvuAlignmentProperties")) : NULL;
}

static void onDestroyBelvuAlignment(GtkWidget *belvuAlignment)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);

  if (properties)
    {
      if (properties->title)
        {
          g_free(properties->title);
          properties->title = NULL;
        }
      
      /* Free the properties struct itself */
      delete properties;
      properties = NULL;
      g_object_set_data(G_OBJECT(belvuAlignment), "BelvuAlignmentProperties", NULL);
    }
}


/* Create the properties struct and initialise all values. */
static void belvuAlignmentCreateProperties(GtkWidget *belvuAlignment, 
                                           BelvuContext *bc,
                                           GtkWidget *columnsArea,
                                           GtkWidget *columnsHeader,
                                           GtkWidget *seqArea,
                                           GtkWidget *seqHeader,
                                           GtkAdjustment *hAdjustment,
                                           GtkAdjustment *vAdjustment,
                                           const char *title,
                                           const int wrapWidth)
{
  if (belvuAlignment)
    {
      BelvuAlignmentProperties *properties = new BelvuAlignmentProperties;
      
      properties->widget = belvuAlignment;
      properties->bc = bc;
      properties->columnsArea = columnsArea;
      properties->columnsHeader = columnsHeader;
      properties->seqArea = seqArea;
      properties->seqHeader = seqHeader;
      properties->columnPadding = 0; /* calculated in calculate-borders */
      properties->hAdjustment = hAdjustment;
      properties->vAdjustment = vAdjustment;
      properties->title = g_strdup(title);
      properties->wrapWidth = wrapWidth;
      
      properties->seqRect.x = 0;
      properties->seqRect.y = DEFAULT_YPAD;
      properties->seqHeaderRect.x = properties->seqRect.x;
      properties->seqHeaderRect.y = DEFAULT_YPAD;
      properties->columnsRect.x = DEFAULT_XPAD;
      properties->columnsRect.y = DEFAULT_YPAD;

      /* Find a fixed-width font */
      const char *fontFamily = findFixedWidthFont(properties->seqArea);
      PangoFontDescription *fontDesc = pango_font_description_from_string(fontFamily);
      gtk_widget_modify_font(seqArea, fontDesc);

      if (columnsArea)
        gtk_widget_modify_font(columnsArea, fontDesc);

      g_object_set_data(G_OBJECT(belvuAlignment), "BelvuAlignmentProperties", properties);
      g_signal_connect(G_OBJECT(belvuAlignment), "destroy", G_CALLBACK (onDestroyBelvuAlignment), NULL);
    }
}

/***********************************************************
 *                         Misc                         *
 ***********************************************************/

/* This should be called after the font has changed. It caches
 * the new font size in the properties and recalculates all sizes */
void onBelvuAlignmentFontSizeChanged(GtkWidget *belvuAlignment)
{
  if (!belvuAlignment)
    return;

  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (!properties || !properties->seqArea)
    return;
  
  getFontCharSize(properties->seqArea, properties->seqArea->style->font_desc, &properties->charWidth, &properties->charHeight, TRUE);

  /* Update the column padding, which is based on the character width. We use
   * slightly different padding if the view is wrapped because we don't draw 
   * column separators */
  if (properties->wrapWidth != UNSET_INT) /* wrapped */
    {
      properties->columnPadding = (DEFAULT_PADDING_CHARS * properties->charWidth);
      properties->nameColumnPadding = (DEFAULT_NAME_COLUMN_PADDING_CHARS * properties->charWidth);
    }
  else /* unwrapped */
    {
      properties->columnPadding = (DEFAULT_PADDING_CHARS * properties->charWidth) / 2;
      properties->nameColumnPadding = 0;
    }
  
  
  /* The font size affects the height of the header line */
  properties->seqHeaderRect.height = properties->charHeight + DEFAULT_YPAD;

  if (properties->seqHeader)
    gtk_widget_set_size_request(properties->seqHeader, -1, properties->seqHeaderRect.y + properties->seqHeaderRect.height);

  /* Set the size of the header columns widget */
  updateHeaderColumnsSize(belvuAlignment);

  /* Recalculate the size of the drawing areas */
  calculateDrawingSizes(belvuAlignment);

  belvuAlignmentRedrawAll(belvuAlignment);
}



/***********************************************************
 *                         Drawing                         *
 ***********************************************************/

/* Refresh all */
void belvuAlignmentRefreshAll(GtkWidget *belvuAlignment)
{
  gtk_widget_queue_draw(belvuAlignment);
}


/* Clear cached drawables and redraw all */
void belvuAlignmentRedrawAll(GtkWidget *belvuAlignment)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);

  if (properties->seqArea)
    widgetClearCachedDrawable(properties->seqArea, NULL);
  
  if (properties->seqHeader)
    widgetClearCachedDrawable(properties->seqHeader, NULL);
  
  if (properties->columnsArea)
    widgetClearCachedDrawable(properties->columnsArea, NULL);

  if (properties->columnsHeader)
    widgetClearCachedDrawable(properties->columnsHeader, NULL);
  
  gtk_widget_queue_draw(belvuAlignment);
}


/* Clear cached drawables for the sequence area only */
static void belvuAlignmentRedrawSequenceArea(GtkWidget *belvuAlignment)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (properties->seqArea)
    widgetClearCachedDrawable(properties->seqArea, NULL);
  
  if (properties->seqHeader)
    widgetClearCachedDrawable(properties->seqHeader, NULL);
  
  gtk_widget_queue_draw(belvuAlignment);
}


/* Find the background color of the given residue index i in the given alignment */
static void findResidueBGcolor(BelvuContext *bc, ALN* alnp, int i, const gboolean isSelected, GdkColor *result) 
{
  int colorNum = 0;
  char *alnpSeq = alnGetSeq(alnp);
  
  if (!alnpSeq || i >= alnGetSeqLen(alnp))
    colorNum = WHITE;
  else if (bc->lowercaseOn && alnpSeq[i] >= 'a' && alnpSeq[i] <= 'z') 
    colorNum = ORANGE;
  else if (alnp->nocolor)
    colorNum = BOXCOLOR;
  else if (alnp->markup)
    colorNum = getMarkupColor(alnpSeq[i]);
  else if (colorByConservation(bc) || colorByResId(bc))
    colorNum = getConservColor(bc, alnpSeq[i], i);
  else
    colorNum = getColor(alnpSeq[i]);

  convertColorNumToGdkColor(colorNum, isSelected, result);
}


/* Draw a single row in the columns area */
static void drawSingleColumn(GtkWidget *widget,
                             GdkDrawable *drawable, 
                             BelvuAlignmentProperties *properties,
                             ALN *alnp, 
                             const int lineNum)
{
  int x = 0;
  const int y = properties->columnsRect.y + (properties->charHeight * lineNum);
  
  const gboolean highlightAln = alignmentHighlighted(properties->bc, alnp);
  GdkGC *gc = gdk_gc_new(drawable);
  
  if (highlightAln || alnp->color != WHITE)
    {
      /* Highlight the background */
      GdkColor bgColor;
      convertColorNumToGdkColor(alnp->color, highlightAln, &bgColor);
      
      gdk_gc_set_foreground(gc, &bgColor);
      gdk_draw_rectangle(drawable, gc, TRUE, properties->columnsRect.x, y, properties->columnsRect.width, properties->charHeight);
    }
  
  GdkColor *textColor = getGdkColor(BELCOLOR_ALIGN_TEXT, properties->bc->defaultColors, highlightAln, FALSE);
  gdk_gc_set_foreground(gc, textColor);

  /* Draw the name */
  if (alnp->name)
    {
      x += properties->columnPadding;
      drawText(widget, drawable, gc, x, y, alnp->name, NULL, NULL);
      x += (properties->bc->maxNameLen * properties->charWidth) + properties->columnPadding;
    }
  
  /* Draw the coords (but only if this is not GC markup; note that we always
   * draw the column separator lines for the coords, though) */
  gdk_draw_line(drawable, gc, x, y, x, y + properties->charHeight);
  x += properties->columnPadding + properties->bc->maxStartLen * properties->charWidth; /* right-hand edge of column for right-aligned text */

  if (alnp->markup != GC)
    drawIntAsText(widget, drawable, gc, x, y, alnp->start);
  
  x += properties->columnPadding;
  gdk_draw_line(drawable, gc, x, y, x, y + properties->charHeight);
  
  x += properties->columnPadding + properties->bc->maxEndLen * properties->charWidth; /* right-aligned */

  if (alnp->markup != GC)
    drawIntAsText(widget, drawable, gc, x, y, alnp->end);

  x += properties->columnPadding;
  gdk_draw_line(drawable, gc, x, y, x, y + properties->charHeight);
  
  /* Draw the score, if displaying scores (and if not a markup row) */
  if (properties->bc->displayScores)
    {
      x += (properties->bc->maxScoreLen * properties->charWidth) + properties->columnPadding;
      
      if (!alnp->markup)
        drawDoubleAsText(widget, drawable, gc, x, y, alnp->score);
      
      x += properties->columnPadding;
      gdk_draw_line(drawable, gc, x, y, x, y + properties->charHeight);
    }
  
  g_object_unref(gc);
}


/* Utility to return true if the given alignment is selected */
static gboolean alignmentSelected(BelvuContext *bc, ALN *alnp)
{
  /* Return true if this alignment has the same name as the selected alignment */
  return (bc->selectedAln && bc->selectedAln == alnp);
}


/* Draw a single character in the given sequence. In standard mode, fgColorsOnly
 * should be false and the function will draw the background color for the 
 * base (and will not draw any text). This is because drawing each character
 * separately is slow, so all text in the default color is drawn at once
 * by the calling function.
 * However, if fgColorsOnly is true then this function checks if the required
 * foreground color is different to the default and, if so, draws the character
 * in that color. (It does not draw background colors in this mode.) */
static void drawSequenceChar(BelvuAlignmentProperties *properties, 
                             ALN *alnp,
                             const int colIdx,
                             const gboolean rowHighlighted,
                             GtkWidget *widget,
                             GdkDrawable *drawable,
                             GdkGC *gc,
                             GdkColor *defaultFgColor,
                             const gboolean fgColorsOnly,
                             const int x, 
                             const int y)
{
  if (!fgColorsOnly)
    {
      /* Draw the background */
      GdkColor bgColor;
      findResidueBGcolor(properties->bc, alnp, colIdx, rowHighlighted, &bgColor);
      gdk_gc_set_foreground(gc, &bgColor);
      
      gdk_draw_rectangle(drawable, gc, TRUE, x, y, properties->charWidth, properties->charHeight);
    }
  else if (colIdx < alnGetSeqLen(alnp) && colorByConservation(properties->bc))
    {
      /* Draw the text if it is in a different color to the default. This is only
       * possible in color by conservation mode. We get the text colour from the 
       * background color */
      GdkColor fgColor, bgColor;
      findResidueBGcolor(properties->bc, alnp, colIdx, rowHighlighted, &bgColor);
      bg2fgColor(properties->bc, &bgColor, &fgColor);
      
      if (!colorsEqual(&fgColor, defaultFgColor))
        {
          gdk_gc_set_foreground(gc, &fgColor);
          
          char displayText[2];
          displayText[0] = alnGetSeq(alnp)[colIdx];
          displayText[1] = '\0';
          drawText(widget, drawable, gc, x, y, displayText, NULL, NULL);
        }
    }
  
}


/* Draw a single line in the sequence area.
 * 
 * This is rather convoluted in an attempt to maximise speed.  Drawing of text in
 * GTK is particularly slow, and is best done a whole line at a time, rather than
 * drawing individual characters.  The characters may each have a different background
 * colour, so we draw the background colours first and then draw the text over the top
 * a whole line at a time.  A further problem, though, is that some characters may
 * have a different foreground colour to others on the same line (in colour-by-conservation
 * mode), so some characters do need to be drawn individually.  There are usually
 * relatively few characters that this affects, though, so we continue to draw the
 * whole line in the default colour first, and then we look to see if any individual
 * characters have a different foreground colour and just draw those over the top.
*/
static void drawSingleSequence(GtkWidget *widget,
                                GdkDrawable *drawable, 
                                BelvuAlignmentProperties *properties,
                                ALN *alnp, 
                                const int lineNum)
{
  if (!alnGetSeq(alnp))
    return;
  
  const int startX = properties->seqRect.x;
  int x = startX;
  const int y = properties->seqRect.y + (properties->charHeight * lineNum);
  
  GdkGC *gc = gdk_gc_new(drawable);
  GtkAdjustment *hAdjustment = properties->hAdjustment;
  
  GdkColor *defaultFgColor = getGdkColor(BELCOLOR_ALIGN_TEXT, properties->bc->defaultColors, FALSE, FALSE);
  
  /* Loop through each column in the current display range and color
   * the according to the relevant highlight color */
  int colIdx = hAdjustment->value;
  int iMax = min(properties->bc->maxLen, (int)hAdjustment->value + (int)hAdjustment->page_size);
  const gboolean rowHighlighted = alignmentSelected(properties->bc, alnp);
  
  if (properties->bc->displayColors)
    {
      for ( ; colIdx < iMax; ++colIdx)
        {
          drawSequenceChar(properties, alnp, colIdx, rowHighlighted, widget, drawable, gc, defaultFgColor, FALSE, x, y);
          x += properties->charWidth;
        }
    }  
  else if (rowHighlighted)
    {
      /* We're not displaying colors for individual chars, but we still need to
       * highlight the background if the row is selected. */
      GdkColor color;
      convertColorNumToGdkColor(WHITE, TRUE, &color);
      gdk_gc_set_foreground(gc, &color);
      
      gdk_draw_rectangle(drawable, gc, TRUE, startX, y, properties->seqRect.width, properties->charHeight);
    }
  
  
  /* Draw the sequence text (current display range only) */
  if (alnGetSeq(alnp))
    {
      GdkColor *textColor = getGdkColor(BELCOLOR_ALIGN_TEXT, properties->bc->defaultColors, FALSE, FALSE);
      gdk_gc_set_foreground(gc, textColor);

      /* Start at the number of characters into the string where the horizontal 
       * scrollbar indicates we are (making sure that's not out of the end of the
       * string) */
      if ((int)hAdjustment->value < alnGetSeqLen(alnp))
        {
          char *cp = alnGetSeq(alnp) + (int)hAdjustment->value;
          char *displayText = g_strndup(cp, iMax - properties->hAdjustment->value);
      
          drawText(widget, drawable, gc, startX, y, displayText, NULL, NULL);
      
          g_free(displayText);
        }
    }
  
  /* Loop again and draw any characters that are not in the default text color.
   * We don't draw every character individually because the pango layout calls
   * are expensive, but we can't really avoid it when some characters are in
   * difference colors.  (We could put markup in the pango layout instead, but
   * generally the number of characters in a non-default color is small, so 
   * this should be good enough for most cases.) */
  if (properties->bc->displayColors)
    {
      for (x = startX, colIdx = hAdjustment->value; colIdx < iMax; ++colIdx)
        {
          drawSequenceChar(properties, alnp, colIdx, rowHighlighted, widget, drawable, gc, defaultFgColor, TRUE, x, y);
          x += properties->charWidth;
        }
    } 
  
  g_object_unref(gc);
}


/* Return the foreground color selected for a given
   background color in color-by-conservation mode 
 */
static void bg2fgColor(BelvuContext *bc, GdkColor *bgColor, GdkColor *result)
{
  int fgColorNum = BLACK; /* default colour for uncoloured or markup */
  
  /* See if the given bgcolor matches the max-conservation background colour */
  int testColor = *getConsColor(bc, CONS_LEVEL_MAX, FALSE);
  convertColorNumToGdkColor(testColor, FALSE, result);
  
  if (colorsEqual(bgColor, result))
    {
      fgColorNum = *getConsColor(bc, CONS_LEVEL_MAX, TRUE);
    }
  else
    {
      /* Try the mid-conservation colour */
      int testColor = *getConsColor(bc, CONS_LEVEL_MID, FALSE);
      convertColorNumToGdkColor(testColor, FALSE, result);
      
      if (colorsEqual(bgColor, result))
        {
          fgColorNum = *getConsColor(bc, CONS_LEVEL_MID, TRUE);
        }
      else
        {
          /* Lastly, try the low-conservation colour */
          int testColor = *getConsColor(bc, CONS_LEVEL_LOW, FALSE);
          convertColorNumToGdkColor(testColor, FALSE, result);
          
          if (colorsEqual(bgColor, result))
            fgColorNum = *getConsColor(bc, CONS_LEVEL_LOW, TRUE);
        }
    }

  /* Convert the foreground colour number into the GdkColor result. */
  convertColorNumToGdkColor(fgColorNum, FALSE, result);
}


static void drawWrappedSequences(GtkWidget *widget, GdkDrawable *drawable, BelvuAlignmentProperties *properties)
{
  /*  Column makeup:
   space, maxNameLen, 2xspace, maxEndLen, space, maxLen, maxEndLen
   i.e. 4 spaces
   */
  
  int i, oldpos, collapseRes = 0, collapsePos = 0, collapseOn = 0;
  int numSpaces = WRAP_DISPLAY_PADDING_CHARS;
  char collapseStr[10],
  ch[2] = " ";
  static int *pos=0;                    /* Current residue position of sequence j */
  
  GdkColor bgColor;
  GdkColor *pBgColor = &bgColor;
  GdkGC *gcText = gdk_gc_new(drawable);
  GdkGC *gc = gdk_gc_new(drawable);
  
  BelvuContext *bc = properties->bc;
  
  
  /* Initialise the sequence and position array */
  char *seq = (char*)g_malloc(properties->wrapWidth + 1);
  
  if (!pos) 
    pos = (int *)g_malloc(bc->alignArr->len * sizeof(int *));
  
  int j = 0;
  for (j = 0; j < (int)bc->alignArr->len; ++j) 
    pos[j] = g_array_index(bc->alignArr, ALN*, j)->start;
  
  
  int paragraph = 0;
  int totCollapsed = 0;
  int line = 1; /* leave a blank line (line=0) at the top for spacing */
  
  if (properties->title)
    {
      const int x = properties->columnPadding;
      const int y = line * properties->charHeight;
      
      drawText(widget, drawable, gcText, x, y, properties->title, NULL, NULL);
      line += 2; /* increment, and also add another blank line for spacing */
    }
  
  gboolean quit = FALSE;
  while (!quit && paragraph * properties->wrapWidth + totCollapsed < bc->maxLen)
    {
      gboolean emptyPara = TRUE;
      
      for (j = 0; j < (int)bc->alignArr->len; ++j)
        {
          ALN *alnp = g_array_index(bc->alignArr, ALN*, j);
          char *alnpSeq = alnGetSeq(alnp);
          
          if (alnp->hide) 
            continue;
          
          int alnstart = paragraph*properties->wrapWidth +totCollapsed; 
          int alnlen = ( (paragraph+1)*properties->wrapWidth +totCollapsed < bc->maxLen ? properties->wrapWidth : bc->maxLen - alnstart );
          int alnend = min(alnstart + alnlen, alnGetSeqLen(alnp));
          
          gboolean emptyLine = TRUE;
          for (i = alnstart; i < alnend; ++i) 
            {
              if (alnpSeq && !isGap(alnpSeq[i]) && alnpSeq[i] != ' ') 
                {
                  emptyLine = FALSE;
                  emptyPara = FALSE;
                  break;
                }
            }
          
          if (!emptyLine) 
            {
              const int y = line * properties->charHeight;
              
              if (y > properties->seqRect.y + properties->seqRect.height)
                {
                  quit = TRUE;
                  break;
                }

              for (collapsePos = 0, oldpos = pos[j], i = alnstart; i < alnend; i++) 
                {       
                  const int xpos = bc->maxNameLen + bc->maxEndLen + numSpaces + i - alnstart - collapsePos;
                  const int x = xpos * properties->charWidth;
                  
                  if (alnpSeq[i] == '[') 
                    {
                      /* Experimental - collapse block: "[" -> Collapse start, "]" -> Collapse end, e.g.
                       1 S[..........]FKSJFE
                       2 L[RVLKLKYKNS]KDEJHF
                       3 P[RL......DK]FKEJFJ
                                 |
                                 V
                       1 S[  0]FKSJFE
                       2 L[ 10]KDEJHF
                       3 P[  4]FKEJFJ
                       
                       Minimum collapse = 5 chars. The system depends on absolute coherence in format; 
                       very strange results will be generated otherwise. Edges need to be avoided manually.
                       */
                      collapseOn = 1;
                      collapsePos += 1;
                      collapseRes = 0;
                      continue;
                    }
                  
                  if (alnpSeq[i] == ']') 
                    {
                      collapseOn = 0;
                      collapsePos -= 4;
                      alnend -= 3;
                      
                      sprintf(collapseStr, "[%3d]", collapseRes);
                      drawText(widget, drawable, gcText, x, y, collapseStr, NULL, NULL);

                      continue;
                    }
                  
                  if (collapseOn) 
                    {
                      collapsePos++;
                      if (!isGap(alnpSeq[i])) 
                        {
                          collapseRes++;
                          pos[j]++;
                        }
                      alnend++;
                      continue;
                    }
                  
                  
                  if (!isGap(alnpSeq[i]) && alnpSeq[i] != ' ') 
                    {
                      findResidueBGcolor(bc, alnp, i, FALSE, pBgColor);
                      gdk_gc_set_foreground(gc, pBgColor);
                      
                      gdk_draw_rectangle(drawable, gc, TRUE, x, y, properties->charWidth, properties->charHeight);
                      
                      pos[j]++;
                    }
                  
                  /* Foreground color */
                  GdkColor fgColor;
                  if (colorByConservation(bc))
                    bg2fgColor(bc, pBgColor, &fgColor);
                  else
                    convertColorNumToGdkColor(BLACK, FALSE, &fgColor);
                  
                  gdk_gc_set_foreground(gc, &fgColor);

                  *ch = alnpSeq[i];
                  drawText(widget, drawable, gc, x, y, ch, NULL, NULL);
                }
          
              drawText(widget, drawable, gcText, properties->charWidth, y, alnp->name, NULL, NULL);
              
              if (!alnp->markup) 
                {
                  char *tmpStr = g_strdup_printf("%*d", bc->maxEndLen, oldpos);
                  drawText(widget, drawable, gcText, (bc->maxNameLen + 3) * properties->charWidth, y, tmpStr, NULL, NULL);
                  g_free(tmpStr);
                  
                  if (alnend == bc->maxLen) 
                    {
                      char *tmpStr = g_strdup_printf("%-d", pos[j] - 1);
                      drawText(widget, drawable, gcText, (bc->maxNameLen + bc->maxEndLen + alnlen + 5) * properties->charWidth, y, tmpStr, NULL, NULL);
                      g_free(tmpStr);
                    }
                }
              
              line++;
            }
        }

      /* Move to next paragraph */
      paragraph++;
      
      /* Add a blank line (unless nothing was drawn for this paragraph) */
      if (!emptyPara)
        line++;
      
      totCollapsed += collapsePos;
    }
  
  g_free(seq);
  g_object_unref(gc);
  g_object_unref(gcText);

  /* Set the layout size now we know how big it is */
  properties->seqRect.height = line * properties->charHeight;
  
  gtk_layout_set_size(GTK_LAYOUT(properties->seqArea),
                      properties->seqRect.x + properties->seqRect.width,
                      properties->seqRect.y + properties->seqRect.height);
  
  if (properties->hAdjustment)
    gtk_adjustment_changed(properties->hAdjustment); /* signal that the scroll range has changed */
  
  if (properties->vAdjustment)
    gtk_adjustment_changed(properties->vAdjustment);
}


/* Draw the header line for the columns section */
static void drawBelvuColumnsHeader(GtkWidget *widget, GdkDrawable *drawable, BelvuAlignmentProperties *properties)
{
  BelvuContext *bc = properties->bc;
  GdkGC *gc = gdk_gc_new(drawable);

  /* Draw the summary line, which shows the number of sequences & alignment length */
  int x = properties->columnPadding;
  int y = DEFAULT_YPAD;
  
  char *tmpStr = g_strdup_printf("(%dx%d)", bc->alignArr->len, bc->maxLen);
  
  drawText(widget, drawable, gc, x, y, tmpStr, NULL, NULL);
  y += properties->charHeight + DEFAULT_YPAD - 1;
  
  g_free(tmpStr);
  
  /* Draw a horizontal separator line */
  x = 0;
  gdk_draw_line(drawable, gc, x, y, x + properties->columnsRect.width, y);
  
  g_object_unref(gc);
}


/* Draw the alignment view */
static void drawBelvuColumns(GtkWidget *widget, GdkDrawable *drawable, BelvuAlignmentProperties *properties)
{
  BelvuContext *bc = properties->bc;

  /* Draw each visible alignment */
  GtkAdjustment *vAdjustment = properties->vAdjustment;
  
  int i = vAdjustment->value;
  int lineNum = 0;
  const int iMax = min((int)bc->alignArr->len, (int)vAdjustment->value + (int)vAdjustment->page_size);
  
  for ( ; i < iMax; ++i)
    {
      ALN *alnp = g_array_index(bc->alignArr, ALN*, i);
      
      if (alnp && !alnp->hide)
        {
          drawSingleColumn(widget, drawable, properties, alnp, lineNum);
          ++lineNum;
        }
    }
}


/* Draw the header line in the sequence area */
static void drawBelvuSequenceHeader(GtkWidget *widget, GdkDrawable *drawable, BelvuAlignmentProperties *properties)
{
  /* The range starts at the current adjustment value (add one to make it a 1-based column number) */
  IntRange displayRange = {(int)(properties->hAdjustment->value + 1),
                           (int)min(properties->bc->maxLen, (int)properties->hAdjustment->value + (int)properties->hAdjustment->page_size)};
  
  drawHScale(widget, 
             drawable,
             &displayRange,
             &properties->seqHeaderRect,
             properties->charWidth, /* width between each minor tickmark */
             TICKMARK_INTERVAL / 2, /* major markers half way between labels */
             TICKMARK_INTERVAL,     /* interval between main tickmarks with labels */
             MINOR_TICKMARK_HEIGHT,
             MAJOR_TICKMARK_HEIGHT);
}


/* This draws some shading where the currently-selected column is in order to
 * highlight it.  If there is no column selected, does nothing. */
static void drawSelectedColumn(GtkWidget *widget, GdkDrawable *drawable, BelvuAlignmentProperties *properties)
{
  BelvuContext *bc = properties->bc;
  
  /* Quit if there is no column selected */
  if (bc->highlightedCol < 1)
    return;

  GtkAdjustment *hAdjustment = properties->hAdjustment;
  
  const int iMin = properties->hAdjustment->value + 1;
  const int iMax = min(bc->maxLen, (int)hAdjustment->value + (int)hAdjustment->page_size + 1);

  /* Quit if the selected column is not in the visible range */
  if (bc->highlightedCol < iMin || bc->highlightedCol > iMax)
    return;

  /* Get the column number as a zero-based index in the current range and 
   * convert this to a distance from the left edge, which will be our x coord.
   * Add half a char's width to get the x coord at the centre of the char. */
  const int colIdx = bc->highlightedCol - iMin;
  const int x = properties->seqRect.x + (colIdx * properties->charWidth) + (properties->charWidth / 2);
  

  /* Draw a vertical line at this index */
  GdkGC *gc = gdk_gc_new(drawable);

  GdkColor *color = getGdkColor(BELCOLOR_COLUMN_HIGHLIGHT, bc->defaultColors, FALSE, FALSE);
  gdk_gc_set_foreground(gc, color);
  gdk_gc_set_function(gc, GDK_INVERT);
  gdk_draw_line(drawable, gc, x, 0, x, properties->seqRect.height);
  
  g_object_unref(gc);
  
//  /* Draw a transparent box at this index */
//  cairo_t *cr = gdk_cairo_create(drawable);
//  gdk_cairo_set_source_color(cr, color);
//  cairo_rectangle(cr, x, 0, properties->charWidth, properties->seqRect.height);
//  cairo_clip(cr);
//  cairo_paint_with_alpha(cr, 0.2);
//  cairo_destroy(cr);
}


/* Draw the alignment view */
static void drawBelvuSequence(GtkWidget *widget, GdkDrawable *drawable, BelvuAlignmentProperties *properties)
{
  BelvuContext *bc = properties->bc;
  
  /* Draw each visible alignment */
  GtkAdjustment *vAdjustment = properties->vAdjustment;
  
  int i = vAdjustment->value;
  int lineNum = 0;
  const int iMax = min((int)bc->alignArr->len, (int)vAdjustment->value + (int)vAdjustment->page_size);
  
  for ( ; i < iMax; ++i)
    {
      ALN *alnp = g_array_index(bc->alignArr, ALN*, i);
      
      if (alnp && !alnp->hide)
        {
          drawSingleSequence(widget, drawable, properties, alnp, lineNum);
          ++lineNum;
        }
    }
}


/* Expose handler for the alignment section */
static gboolean onExposeBelvuColumns(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  GdkDrawable *window = GTK_LAYOUT(widget)->bin_window;

  if (window)
    {
      GdkDrawable *bitmap = widgetGetDrawable(widget);
      BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
      
      if (!bitmap)
        {
          /* There isn't a bitmap yet. Create it now. */
          bitmap = createBlankSizedPixmap(widget, window, properties->columnsRect.width, properties->columnsRect.height);
          drawBelvuColumns(widget, bitmap, properties);
        }
      
      if (bitmap)
        {
          /* Push the bitmap onto the window */
          GdkGC *gc = gdk_gc_new(window);
          gdk_draw_drawable(window, gc, bitmap, 0, 0, 0, 0, -1, -1);
          g_object_unref(gc);
        }
      else
        {
          g_warning("Failed to draw Belvu alignment [%p] - could not create bitmap.\n", widget);
        }
    }

  return TRUE;
}

/* Expose handler for the alignment section */
static gboolean onExposeBelvuSequence(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);

  GdkDrawable *window = GTK_LAYOUT(widget)->bin_window;
  
  if (window)
    {
      GdkDrawable *bitmap = widgetGetDrawable(widget);
      
      if (!bitmap)
        {
          /* There isn't a bitmap yet. Create it now. */
          bitmap = createBlankSizedPixmap(widget, window, properties->seqRect.width, properties->seqRect.height);
          
          if (properties->wrapWidth == UNSET_INT)
            drawBelvuSequence(widget, bitmap, properties);
          else
            drawWrappedSequences(widget, bitmap, properties);
        }
      
      if (bitmap)
        {
          /* Push the bitmap onto the window */
          GdkGC *gc = gdk_gc_new(window);
          gdk_draw_drawable(window, gc, bitmap, 0, 0, 0, 0, -1, -1);
          g_object_unref(gc);
          
          /* Draw the currently-selected column on top */
          drawSelectedColumn(widget, window, properties);
        }
      else
        {
          g_warning("Failed to draw Belvu alignment [%p] - could not create bitmap.\n", widget);
        }
    }
  
  return TRUE;
}


/* Expose handler for the header widget of the alignment section */
static gboolean onExposeBelvuSequenceHeader(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  GdkDrawable *window = widget->window;

  if (window)
    {
      GdkDrawable *bitmap = widgetGetDrawable(widget);
      
      if (!bitmap)
        {
          /* There isn't a bitmap yet. Create it now. */
          bitmap = createBlankSizedPixmap(widget, window, properties->seqRect.width, properties->seqRect.height);
          drawBelvuSequenceHeader(widget, bitmap, properties);
        }
      if (bitmap)
        {
          /* Push the bitmap onto the window */
          GdkGC *gc = gdk_gc_new(window);
          gdk_draw_drawable(window, gc, bitmap, 0, 0, 0, 0, -1, -1);
          g_object_unref(gc);
        }
      else
        {
          g_warning("Failed to draw Belvu alignment header [%p] - could not create bitmap.\n", widget);
        }
    }
  
  return TRUE;
}


/* Expose handler for the header widget of the columns section */
static gboolean onExposeBelvuColumnsHeader(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  GdkDrawable *window = widget->window;
  
  if (window)
    {
      GdkDrawable *bitmap = widgetGetDrawable(widget);
      
      if (!bitmap)
        {
          /* There isn't a bitmap yet. Create it now. */
          bitmap = createBlankSizedPixmap(widget, window, properties->seqRect.width, properties->seqRect.height);
          drawBelvuColumnsHeader(widget, bitmap, properties);
        }
      
      if (bitmap)
        {
          /* Push the bitmap onto the window */
          GdkGC *gc = gdk_gc_new(window);
          gdk_draw_drawable(window, gc, bitmap, 0, 0, 0, 0, -1, -1);
          g_object_unref(gc);
        }
      else
        {
          g_warning("Failed to draw Belvu columns header [%p] - could not create bitmap.\n", widget);
        }
    }
  
  return TRUE;
}

/***********************************************************
 *                         Scrolling                       *
 ***********************************************************/

/* Called when the horizontal scroll position has changed */
static void onHScrollPosChangedBelvuAlignment(GtkObject *object, gpointer data)
{
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (properties->wrapWidth == UNSET_INT)
    {
      /* We need to clear and re-draw the sequence area (unless we're displaying
       * wrapped sequences, in which case we've already drawn the full sequence
       * width in the drawing area */
      belvuAlignmentRedrawSequenceArea(belvuAlignment);
    }
}

/* Called when the vertical scroll position has changed */
static void onVScrollPosChangedBelvuAlignment(GtkObject *object, gpointer data)
{
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (properties->wrapWidth == UNSET_INT)
    {
      belvuAlignmentRedrawAll(belvuAlignment);
    }
}


/* Called when the horizontal scroll range has changed */
static void onHScrollRangeChangedBelvuAlignment(GtkObject *object, gpointer data)
{
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);

  /* Set the horizontal adjustment page size to be the number of characters
   * that will fit in the width */
  properties->hAdjustment->page_size = (int)(properties->seqRect.width / properties->charWidth);
  properties->hAdjustment->page_increment = properties->hAdjustment->page_size;

  /* Make sure the scroll position still lies within upper - page_size. */
  if (properties->hAdjustment->value > properties->hAdjustment->upper - properties->hAdjustment->page_size)
    properties->hAdjustment->value = properties->hAdjustment->upper - properties->hAdjustment->page_size;

  /* Make sure we're still within the lower limit */
  if (properties->hAdjustment->value < properties->hAdjustment->lower)
    properties->hAdjustment->value = properties->hAdjustment->lower;

  belvuAlignmentRedrawSequenceArea(belvuAlignment);
}

/* Called when the vertical scroll range has changed */
static void onVScrollRangeChangedBelvuAlignment(GtkObject *object, gpointer data)
{
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  /* Set the horizontal adjustment page size to be the number of characters
   * that will fit in the width */
  properties->vAdjustment->page_size = (int)(properties->seqArea->allocation.height / properties->charHeight);
  properties->vAdjustment->page_increment = properties->vAdjustment->page_size;
  
  /* Make sure the scroll position still lies within upper - page_size. */
  if (properties->vAdjustment->value > properties->vAdjustment->upper - properties->vAdjustment->page_size)
    properties->vAdjustment->value = properties->vAdjustment->upper - properties->vAdjustment->page_size;
  
  /* Make sure we're still within the lower limit */
  if (properties->vAdjustment->value < properties->vAdjustment->lower)
    properties->vAdjustment->value = properties->vAdjustment->lower;

  belvuAlignmentRedrawAll(belvuAlignment);
}


static void updateOnAlignmentSizeChanged(GtkWidget *belvuAlignment)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (properties->vAdjustment)
    {
      properties->vAdjustment->upper = properties->bc->alignArr->len + 1;
      gtk_adjustment_changed(properties->vAdjustment);
    }
  
  if (properties->hAdjustment)
    {
      properties->hAdjustment->upper = properties->bc->maxLen;
      gtk_adjustment_changed(properties->hAdjustment);
    }

  belvuAlignmentRedrawAll(belvuAlignment);
}


/* Called when the vertical scroll range upper value has changed (i.e. the length
 * of the alignments array has changed). */
void updateOnVScrollSizeChaged(GtkWidget *belvuAlignment)
{
  /* Recalculate the borders, because the max name length etc. may have changed */
  calculateDrawingSizes(belvuAlignment);
  
  /* Update the scroll ranges because the number of rows and columns may have changed */
  updateOnAlignmentSizeChanged(belvuAlignment);
}


/* This should be called when the alignment length has changed, i.e. when columns
 * have been deleted. */
void updateOnAlignmentLenChanged(GtkWidget *belvuAlignment)
{
  updateOnAlignmentSizeChanged(belvuAlignment);
}

/***********************************************************
 *                         Sizing                          *
 ***********************************************************/

int belvuAlignmentGetWidth(GtkWidget *belvuAlignment)
{
  if (!belvuAlignment)
    return 0;

  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  return properties ? properties->seqRect.width : 0;
}


/* Get the width of the drawing area that shows the sequences */
static int getAlignmentDisplayWidth(BelvuAlignmentProperties *properties)
{
  int result = 0;
  
  if (properties->wrapWidth == UNSET_INT)
    {
      /* Drawing can be very slow if we draw the full alignment, so we only
       * draw what's in the current display width. */
      result = properties->seqArea->allocation.width - SEQ_AREA_PADDING;
    }
  else
    {
      /* The sequences are wrapped, so we only have a short width to deal with
       * and can therefore draw the full width. (Although specify a size limit
       * in case the user specifies a very large width because this can cause
       * a badalloc crash if too big.) */
      result =
        properties->columnPadding + (properties->bc->maxNameLen * properties->charWidth) + 
        properties->nameColumnPadding + (properties->bc->maxEndLen * properties->charWidth) +
        properties->columnPadding + (properties->wrapWidth * properties->charWidth) + 
        properties->columnPadding + (properties->bc->maxEndLen * properties->charWidth) + 
        properties->columnPadding + (properties->bc->maxScoreLen * properties->charWidth);
      
      if (result > MAX_PIXMAP_WIDTH)
        {
          result = MAX_PIXMAP_WIDTH;
          g_warning("The alignment window is too large and will be clipped.\n");
        }
    }
  
  return result;
}


static int getAlignmentDisplayHeight(BelvuAlignmentProperties *properties)
{
  int result = 0;
  
  if (properties->wrapWidth == UNSET_INT)
    {
      result = properties->seqArea->allocation.height - DEFAULT_YPAD;
    }  
  else
    {
      /* Divide the full sequence length by the display width to give the number
       * of paragraphs we require */
      const int numParagraphs = ceil((double)properties->bc->maxLen / (double)properties->wrapWidth);
      const int paragraphHeight = (properties->bc->alignArr->len + 1) * properties->charHeight;
    
      result = paragraphHeight * numParagraphs;
      
      /* Add space for the title, if given (one line for the title and one as a spacer) */
      if (properties->title)
        result += properties->charHeight * 2;
      
      if (result > MAX_PIXMAP_HEIGHT)
        {
          result = MAX_PIXMAP_HEIGHT;
          g_warning("The alignment window is too large and will be clipped.\n");
        }
    }
  
  return result;
}


/* Update the size of the header-columns widget. Should be called after any
 * change that affects the length of the text displayed in the columns area. */
void updateHeaderColumnsSize(GtkWidget *belvuAlignment)
{
  if (!belvuAlignment)
    return;
  
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (properties->columnsArea)
    {
      /* There is a separate drawing area for the columns that contain the row
       * headers; calculate its size. */
      properties->columnsRect.width = (properties->bc->maxNameLen * properties->charWidth) + (2 * properties->columnPadding) +
                                      (properties->bc->maxStartLen * properties->charWidth) + (2 * properties->columnPadding) +
                                      (properties->bc->maxEndLen * properties->charWidth) + (2 * properties->columnPadding) +
                                      properties->columnPadding;
      
      if (properties->bc->displayScores)
        properties->columnsRect.width += (properties->bc->maxScoreLen * properties->charWidth) + (2 * properties->columnPadding);

      /* Set the height of the header line to the the same as the sequence header line */
      gtk_widget_set_size_request(properties->columnsHeader, -1, properties->seqHeaderRect.y + properties->seqHeaderRect.height);
      
      /* Set the height and width of the drawing area */
      gtk_layout_set_size(GTK_LAYOUT(properties->columnsArea), properties->columnsRect.width, properties->columnsRect.height);
      
      /* Force the width of the drawing widget to be the width of the drawing area */
      if (properties->columnsRect.width != properties->columnsArea->allocation.width)
        gtk_widget_set_size_request(properties->columnsArea, properties->columnsRect.width, -1);
    }
}


/* This function calculates the size of the drawing area for sequences based
 * on either a) the current allocated size of the sequence area or b) for a 
 * wrapped alignment, the full size required to draw everything.
 * It also updates the height of the columns area, if applicable, so that it is
 * the same height as the sequence area. */
void calculateDrawingSizes(GtkWidget *belvuAlignment)
{
  if (!belvuAlignment)
    return;
  
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);

  /* Recalculate the size of the drawing area for the sequences (both height
   * and width may have changed on a size-allocate) */
  properties->seqRect.width = getAlignmentDisplayWidth(properties);
  properties->seqRect.height = getAlignmentDisplayHeight(properties);
  
  /* Update the size of the drawing widget and signal that the scroll range
   * has changed (n/a for wrapped view because this gets done by the draw function. */
  if (properties->wrapWidth == UNSET_INT)
    {
      gtk_layout_set_size(GTK_LAYOUT(properties->seqArea), properties->seqRect.x + properties->seqRect.width, properties->seqRect.y + properties->seqRect.height);

      if (properties->hAdjustment)
        gtk_adjustment_changed(properties->hAdjustment); /* signal that the scroll range has changed */
      
      if (properties->vAdjustment)
        gtk_adjustment_changed(properties->vAdjustment);
    }
  
  /* Keep the sequence-header area the same width as the sequence area */
  properties->seqHeaderRect.width = properties->seqRect.width;

  if (properties->columnsArea)
    {
      /* Update the height of the drawing area (the width shouldn't have
       * changed for a size-allocate because the table cell does not expand) */
      properties->columnsRect.height = properties->seqRect.height;
      gtk_layout_set_size(GTK_LAYOUT(properties->columnsArea), properties->columnsRect.width, properties->columnsRect.height);
    }
}


static void onSizeAllocateBelvuAlignment(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
{
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  
  /* Update the size of the drawing areas */
  calculateDrawingSizes(belvuAlignment);
}


/***********************************************************
 *                Removing sequences                       *
 ***********************************************************/

void removeSelectedSequence(BelvuContext *bc, GtkWidget *belvuAlignment)
{ 
  if (!bc->selectedAln) 
    {
      g_critical("Please select a sequence to remove.\n");
      return;
    }
  
  const int idx = bc->selectedAln->nr - 1;
  g_array_remove_index(bc->alignArr, idx);
  arrayOrder(bc->alignArr);
  
  bc->saved = FALSE;
  
  g_message("Removed %s/%d-%d.  %d sequences left.\n\n", bc->selectedAln->name, bc->selectedAln->start, bc->selectedAln->end, bc->alignArr->len);
  
  bc->selectedAln = NULL;
  
  rmFinaliseGapRemoval(bc);
  updateOnVScrollSizeChaged(belvuAlignment);  
  onRowSelectionChanged(bc);
}


/* Get rid of seqs that are too gappy. */
void removeGappySeqs(BelvuContext *bc, GtkWidget *belvuAlignment, const double cutoff)
{
  rmGappySeqs(bc, cutoff);
  rmFinaliseGapRemoval(bc);
  updateOnVScrollSizeChaged(belvuAlignment);  
}

/* Get rid of partial seqs. */
void removePartialSeqs(BelvuContext *bc, GtkWidget *belvuAlignment)
{
  rmPartialSeqs(bc);
  updateOnVScrollSizeChaged(belvuAlignment);  
}

/* Get rid of redundant seqs (those that are more than the given % identical). */
void removeRedundantSeqs(BelvuContext *bc, GtkWidget *belvuAlignment, const double cutoff)
{
  mkNonRedundant(bc, cutoff);
  updateOnVScrollSizeChaged(belvuAlignment);  
}

/* Get rid of outlier seqs (those that are less than the given % identical to any other). */
void removeOutliers(BelvuContext *bc, GtkWidget *belvuAlignment, const double cutoff)
{
  rmOutliers(bc, cutoff);
  updateOnVScrollSizeChaged(belvuAlignment);  
}

/* Get rid of seqs that have a score below the given value */
void removeByScore(BelvuContext *bc, GtkWidget *belvuAlignment, const double cutoff)
{
  rmScore(bc, cutoff);
  updateOnVScrollSizeChaged(belvuAlignment);
  centerHighlighted(bc, belvuAlignment);
}


/* Scroll the alignment list if necessary to make sure that the currently
 * highlighted sequence is visible */
void centerHighlighted(BelvuContext *bc, GtkWidget *belvuAlignment)
{
  if (bc->selectedAln) 
    {
      BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
      GtkAdjustment *vAdjustment = properties->vAdjustment;
      
      /* Create the range of y coords that we want to be in range */
      const int newIdx = bc->selectedAln->nr - 1;
      gtk_adjustment_clamp_page(vAdjustment, newIdx, newIdx + 1);
    }
}


/* Utility to set the value of an adjustment and to bounds-limit it to lie
 * between 'lower' and 'upper - page_size' */
static void setAdjustmentValue(GtkAdjustment *adjustment, const int value)
{
  int newValue = value;

  /* _set_value checks the lower limit, but we need to check the upper... */
  if (newValue > adjustment->upper - adjustment->page_size)
    newValue = adjustment->upper - adjustment->page_size;
                     
  gtk_adjustment_set_value(adjustment, newValue);
}


/* Utility to scroll an adjustment to the start/end of its range */
static void adjustmentScrollExtremity(GtkAdjustment *adjustment, const gboolean start)
{
  if (start)
    setAdjustmentValue(adjustment, adjustment->lower);
  else
    setAdjustmentValue(adjustment, adjustment->upper);
}

/* Utility to scroll an adjustment up or down by one page */
static void adjustmentScrollPage(GtkAdjustment *adjustment, const gboolean lower)
{
  if (lower)
    setAdjustmentValue(adjustment, adjustment->value - adjustment->page_size);
  else
    setAdjustmentValue(adjustment, adjustment->value + adjustment->page_size);
}

/* Utility to scroll an adjustment up or down by the given value */
static void adjustmentScrollValue(GtkAdjustment *adjustment, const gboolean lower, const int value)
{
  if (lower)
    setAdjustmentValue(adjustment, adjustment->value - value);
  else
    setAdjustmentValue(adjustment, adjustment->value + value);
}


/* Scroll vertically to the start (top) or end (bottom) of the alignment list */
void vScrollStartEnd(GtkWidget *belvuAlignment, const gboolean start)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  adjustmentScrollExtremity(properties->vAdjustment, start);
}

/* Scroll horizontally to the start (leftmost) or end (rightmost) coord */
void hScrollStartEnd(GtkWidget *belvuAlignment, const gboolean start)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  adjustmentScrollExtremity(properties->hAdjustment, start);
}

/* Scroll vertically one page up or down */
void vScrollPageUpDown(GtkWidget *belvuAlignment, const gboolean up)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  adjustmentScrollPage(properties->vAdjustment, up);
}

/* Scroll horizontally one page left or right */
void hScrollPageLeftRight(GtkWidget *belvuAlignment, const gboolean left)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  adjustmentScrollPage(properties->hAdjustment, left);
}

/* Scroll vertically the given number of rows up or down */
void vScrollUpDown(GtkWidget *belvuAlignment, const gboolean up, const int numRows)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  adjustmentScrollValue(properties->vAdjustment, up, numRows);
}

/* Scroll horizontally the given number of characters left or right */
void hScrollLeftRight(GtkWidget *belvuAlignment, const gboolean left, const int numChars)
{
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  adjustmentScrollValue(properties->hAdjustment, left, numChars);
}




/* Select the row at the given y coord */
static void selectRowAtCoord(BelvuAlignmentProperties *properties, const int y)
{
  BelvuContext *bc = properties->bc;
  
  const int rowIdx = properties->vAdjustment->value + ((y - properties->seqRect.y) / properties->charHeight);
  
  /* Set the selected alignment. Note that some alignments are hidden so
   * we need to count how many visible alignments there are up to this row. */
  int i = 0;
  int count = -1;
  bc->selectedAln = NULL;
  
  for ( i = 0; i < (int)bc->alignArr->len; ++i)
    {
      ALN *alnp = g_array_index(bc->alignArr, ALN*, i);
      
      if (!alnp->hide)
        {
          ++count;
          if (count == rowIdx)
            {
              bc->selectedAln = alnp;
              break;
            }
        }
    }
}


/* Utility to find the column index (0-based from left edge) at the given
 * x coordinate in the sequence area of the alignment view. */
static int getColumnAtCoord(BelvuAlignmentProperties *properties, const int x)
{
  int result = (x - properties->seqRect.x) / properties->charWidth;
  return result;
}


/* Select the column at the given x coord */
static void selectColumnAtCoord(BelvuAlignmentProperties *properties, const int x, const gboolean highlightCol)
{
  BelvuContext *bc = properties->bc;

  const int colIdx = getColumnAtCoord(properties, x);
  
  if (colIdx >= 0 && colIdx < bc->maxLen)
    {
      bc->selectedCol = colIdx + properties->hAdjustment->value + 1;
      
      if (highlightCol)
        bc->highlightedCol = bc->selectedCol;
      else
        bc->highlightedCol = 0;
    }
}


/* Mouse button handler for the columns area of the alignment window */
static gboolean onButtonPressColumnsArea(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
  gboolean handled = FALSE;
  
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (event->type == GDK_BUTTON_PRESS && event->button == 1)  /* single click left button */
    {
      /* Select the clicked row */
      selectRowAtCoord(properties, event->y);
      onRowSelectionChanged(properties->bc);
      handled = TRUE;
    }
  else if (event->type == GDK_2BUTTON_PRESS && event->button == 1) /* double click left button */
    {
      if (properties->bc->removingSeqs)
        {
          /* Removed the clicked sequence (which will be the selected one) */
          removeSelectedSequence(properties->bc, belvuAlignment);
        }
      else 
        {
          /* Fetch the clicked sequence (i.e. the currently selected one) */
          fetchAln(properties->bc, properties->bc->selectedAln);
        }
    }
  
  return handled;
}


/* Mouse button handler for the sequence area of the alignment window */
static gboolean onButtonPressSeqArea(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
  gboolean handled = FALSE;
  
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (event->type == GDK_BUTTON_PRESS &&
      (event->button == 1 || event->button == 2))  /* single click left or middle buttons */
    {
      /* If the middle button was pressed, highlight the selected column */
      const gboolean highlightCol = (event->button == 2);
      
      /* If the left button was pressed, select the clicked row */
      if (event->button == 1)
        {
          selectRowAtCoord(properties, event->y);
          onRowSelectionChanged(properties->bc);
        }
      
      /* Select the clicked column */
      selectColumnAtCoord(properties, event->x, highlightCol);
      onColSelectionChanged(properties->bc);
      
      handled = TRUE;
    }
  else if (event->type == GDK_2BUTTON_PRESS && event->button == 1) /* double click left button */
    {
      if (properties->bc->removingSeqs)
        {
          /* Removed the clicked sequence (i.e. the currently selected one) */
          removeSelectedSequence(properties->bc, belvuAlignment);
        }
      else 
        {
          /* Fetch the clicked sequence (i.e. the currently selected one) */
          fetchAln(properties->bc, properties->bc->selectedAln);
        }
    }
  
  return handled;
}


/* Mouse button handler release for the sequence area of the alignment window */
static gboolean onButtonReleaseSeqArea(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
  gboolean handled = FALSE;
  
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (event->button == 2)  /* released middle button */
    {
      /* Scroll to centre on the current column */
      int colIdx = getColumnAtCoord(properties, event->x);
      double newValue = colIdx + properties->hAdjustment->value - (properties->hAdjustment->page_size / 2.0);
      
      if (newValue > properties->hAdjustment->upper - properties->hAdjustment->page_size)
        newValue = properties->hAdjustment->upper - properties->hAdjustment->page_size;
        
      gtk_adjustment_set_value(properties->hAdjustment, newValue);
      
      /* Clear the current column highlihting */
      properties->bc->highlightedCol = 0;
      onColSelectionChanged(properties->bc);
      
      handled = TRUE;
    }
  else if (event->type == GDK_2BUTTON_PRESS && event->button == 1) /* double click left button */
    {
      if (properties->bc->removingSeqs)
        {
          /* Removed the clicked sequence (which will be the selected one) */
          removeSelectedSequence(properties->bc, belvuAlignment);
        }
    }
  
  return handled;
}


/* Mouse move handler for the sequence area of the alignment window */
static gboolean onMouseMoveSeqArea(GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
  gboolean handled = FALSE;
  
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  if (event->state & GDK_BUTTON2_MASK) /* middle button pressed */
    {
      /* Update the selected/highlighted column */
      selectColumnAtCoord(properties, event->x, TRUE);
      onColSelectionChanged(properties->bc);
      
      handled = TRUE;
    }
  
  return handled;
}


/* Implement custom scrolling for horizontal mouse wheel movements over the alignment. */
static gboolean onScrollAlignment(GtkWidget *widget, GdkEventScroll *event, gpointer data)
{
  gboolean handled = FALSE;
  
  GtkWidget *belvuAlignment = GTK_WIDGET(data);
  BelvuAlignmentProperties *properties = belvuAlignmentGetProperties(belvuAlignment);
  
  switch (event->direction)
    {
      /* left/right scrolling for sequence area or sequence header only */
      case GDK_SCROLL_LEFT:
        {
          if (widget == properties->seqArea || widget == properties->seqHeader)
            hScrollLeftRight(belvuAlignment, TRUE, properties->hAdjustment->step_increment);
          handled = TRUE;
          break;
        }
        
      case GDK_SCROLL_RIGHT:
        {
          if (widget == properties->seqArea || widget == properties->seqHeader)
            hScrollLeftRight(belvuAlignment, FALSE, properties->hAdjustment->step_increment);
          handled = TRUE;
          break;
        }

      /* up/down scrolling for sequence area or columns area only */
      case GDK_SCROLL_UP:
        {
          if (widget == properties->seqArea || widget == properties->columnsArea)
            vScrollUpDown(belvuAlignment, TRUE, properties->vAdjustment->step_increment);
          handled = TRUE;
          break;
        }
        
      case GDK_SCROLL_DOWN:
        {
          if (widget == properties->seqArea || widget == properties->columnsArea)
            vScrollUpDown(belvuAlignment, FALSE, properties->vAdjustment->step_increment);
          handled = TRUE;
          break;
        }
        
      default:
        {
          handled = FALSE;
          break;
        }
    };
  
  return handled;
}

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

/* Set style properties for the belvu alignment widgets */
static void setBelvuAlignmentStyle(BelvuContext *bc, GtkWidget *seqArea, GtkWidget *columnsArea)
{
  GdkColor *bgColor = getGdkColor(BELCOLOR_BACKGROUND, bc->defaultColors, FALSE, FALSE);

  gtk_widget_modify_bg(seqArea, GTK_STATE_NORMAL, bgColor);

  if (columnsArea)
    gtk_widget_modify_bg(columnsArea, GTK_STATE_NORMAL, bgColor);
}


/* Create a widget that will draw the alignments. This type of widget is
 * used for both the standard and wrapped views - pass wrapWidth as UNSET_INT
 * for the standard view, or pass wrapWidth as the number of characters after
 * which to wrap for the wrapped view. */
GtkWidget* createBelvuAlignment(BelvuContext *bc, const char* title, const int wrapWidth)
{
  GtkWidget *belvuAlignment = NULL;      /* the outermost container */
  
  GtkWidget *seqArea = NULL;             /* the "sequence area", which will draw the actual peptide sequences */
  GtkWidget *columnsArea = NULL;         /* optional "headers" column for drawing names, coords etc. */
  GtkWidget *seqHeader = NULL;       /* header for the sequence area */
  GtkWidget *columnsHeader = NULL;   /* header for the columns area */
  
  GtkAdjustment *hAdjustment = NULL;
  GtkAdjustment *vAdjustment = NULL;
  
  if (wrapWidth == UNSET_INT)
    {
      /* The standard (unwrapped) view. Two columns that share the same vertical
       * scrollbar but only the sequence area will have a horizontal scrollbar. */
      
      /* Place everything in a table */
      belvuAlignment = gtk_table_new(3, 3, FALSE);
      const int xpad = 0, ypad = 0;

      /* Create the scrollbars */
      hAdjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, bc->maxLen, 5, 0, 0));
      vAdjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, bc->alignArr->len + 1, 1, 0, 0));
      GtkWidget *hScrollbar = gtk_hscrollbar_new(hAdjustment);
      GtkWidget *vScrollbar = gtk_vscrollbar_new(vAdjustment);
      
      /* Create the drawing areas */
      seqArea = gtk_layout_new(NULL, NULL);
      columnsArea = gtk_layout_new(NULL, NULL);
      
      /* Create a header widget for each drawing area */
      columnsHeader = gtk_drawing_area_new();
      seqHeader = gtk_drawing_area_new();
      
      /* Place all the widgets in the table */
      gtk_table_attach(GTK_TABLE(belvuAlignment), columnsHeader,0, 1, 0, 1, GTK_FILL, GTK_SHRINK, xpad, ypad);
      gtk_table_attach(GTK_TABLE(belvuAlignment), columnsArea,  0, 1, 1, 2, GTK_FILL, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), xpad, ypad);
      gtk_table_attach(GTK_TABLE(belvuAlignment), seqHeader,    1, 2, 0, 1, GTK_FILL, GTK_SHRINK, xpad, ypad);
      gtk_table_attach(GTK_TABLE(belvuAlignment), seqArea,      1, 2, 1, 2, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), xpad, ypad);
      gtk_table_attach(GTK_TABLE(belvuAlignment), hScrollbar,   1, 2, 2, 3, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), GTK_SHRINK, xpad, ypad);
      gtk_table_attach(GTK_TABLE(belvuAlignment), vScrollbar,   2, 3, 1, 2, GTK_SHRINK, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), xpad, ypad);

      /* Connect signals */
      gtk_widget_add_events(columnsArea, GDK_BUTTON_PRESS_MASK);
      g_signal_connect(G_OBJECT(columnsArea), "expose-event", G_CALLBACK(onExposeBelvuColumns), belvuAlignment);  
      g_signal_connect(G_OBJECT(columnsArea), "button-press-event", G_CALLBACK(onButtonPressColumnsArea), belvuAlignment);
      g_signal_connect(G_OBJECT(columnsArea), "scroll-event",  G_CALLBACK(onScrollAlignment), belvuAlignment);
      
      g_signal_connect(G_OBJECT(columnsHeader), "expose-event", G_CALLBACK(onExposeBelvuColumnsHeader), belvuAlignment);  

      gtk_widget_add_events(seqHeader, GDK_BUTTON_PRESS_MASK);
      g_signal_connect(G_OBJECT(seqHeader), "expose-event", G_CALLBACK(onExposeBelvuSequenceHeader), belvuAlignment);  
      g_signal_connect(G_OBJECT(seqHeader), "scroll-event",  G_CALLBACK(onScrollAlignment), belvuAlignment);

      gtk_widget_add_events(seqArea, GDK_BUTTON_RELEASE_MASK);
      gtk_widget_add_events(seqArea, GDK_POINTER_MOTION_MASK);
      g_signal_connect(G_OBJECT(seqArea), "button-press-event", G_CALLBACK(onButtonPressSeqArea), belvuAlignment);
      g_signal_connect(G_OBJECT(seqArea), "button-release-event", G_CALLBACK(onButtonReleaseSeqArea), belvuAlignment);
      g_signal_connect(G_OBJECT(seqArea), "motion-notify-event", G_CALLBACK(onMouseMoveSeqArea), belvuAlignment);
      g_signal_connect(G_OBJECT(seqArea), "scroll-event",  G_CALLBACK(onScrollAlignment), belvuAlignment);

      g_signal_connect(G_OBJECT(hAdjustment), "changed", G_CALLBACK(onHScrollRangeChangedBelvuAlignment), belvuAlignment);
      g_signal_connect(G_OBJECT(vAdjustment), "changed", G_CALLBACK(onVScrollRangeChangedBelvuAlignment), belvuAlignment);
      g_signal_connect(G_OBJECT(hAdjustment), "value-changed", G_CALLBACK(onHScrollPosChangedBelvuAlignment), belvuAlignment);
      g_signal_connect(G_OBJECT(vAdjustment), "value-changed", G_CALLBACK(onVScrollPosChangedBelvuAlignment), belvuAlignment);
    }
  else
    {
      /* For the wrapped view, we just have a single layout area. We'll need to
       * draw the entire contents at once so that it can be printed. We can
       * therefore just place it in a standard scrolled window. We also don't
       * need to worry about button-press events because this widget is not
       * interactive; it's just for printing. */
      seqArea = gtk_layout_new(NULL, NULL);

      belvuAlignment = gtk_scrolled_window_new(NULL, NULL);
      gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(belvuAlignment), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
      gtk_container_add(GTK_CONTAINER(belvuAlignment), seqArea);
      
      hAdjustment = gtk_layout_get_hadjustment(GTK_LAYOUT(seqArea));
      vAdjustment = gtk_layout_get_vadjustment(GTK_LAYOUT(seqArea));
    }

  gtk_widget_add_events(seqArea, GDK_BUTTON_PRESS_MASK);
  g_signal_connect(G_OBJECT(seqArea), "expose-event",  G_CALLBACK(onExposeBelvuSequence), belvuAlignment);  
  g_signal_connect(G_OBJECT(seqArea), "size-allocate", G_CALLBACK(onSizeAllocateBelvuAlignment), belvuAlignment);
  
  /* Set the style and properties */
  g_assert(belvuAlignment);
  setBelvuAlignmentStyle(bc, seqArea, columnsArea);
  belvuAlignmentCreateProperties(belvuAlignment, bc, columnsArea, columnsHeader, seqArea, seqHeader, hAdjustment, vAdjustment, title, wrapWidth);

  return belvuAlignment;
}