File: coverageview.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 (716 lines) | stat: -rw-r--r-- 23,987 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
/*  File: coverageview.c
 *  Author: Gemma Barson, 2011-03-21
 *  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: This view shows a plot of the number of alignments at each
 *              coordinate in the reference sequence.
 *----------------------------------------------------------------------------
 */

#include "blixemApp/coverageview.hpp"
#include "blixemApp/blxcontext.hpp"
#include "blixemApp/blixem_.hpp"
#include "blixemApp/blxpanel.hpp"
#include <gtk/gtk.h>
#include <math.h>

/* We should remove references to the big picture and blxwindow here. The blxwindow is only used
 * to get the big picture */
#include "blixemApp/bigpicture.hpp"
#include "blixemApp/blxwindow.hpp"


#define DEFAULT_COVERAGE_VIEW_Y_PADDING		3	  /* this provides space between the drawing area and the edge of the widget */
#define DEFAULT_NUM_V_CELLS			4	  /* number of vertical cells to show on the grid */
#define MIN_LINE_WIDTH				0.5	  /* this provides space between the drawing area and the edge of the widget */
#define COVERAGE_VIEW_NAME                      "CoverageView"



/***********************************************************
 *                    Class member functions               *
 ***********************************************************/

CoverageViewProperties::CoverageViewProperties(GtkWidget *widget_in, 
                                               GtkWidget *blxWindow_in,
                                               BlxContext *bc_in)
{
  m_widget = widget_in;
  m_bc = bc_in;
  m_panel = NULL;

  m_blxWindow = blxWindow_in;

  m_viewYPadding = DEFAULT_COVERAGE_VIEW_Y_PADDING;
  m_numVCells = DEFAULT_NUM_V_CELLS;
  m_rangePerCell = 0;
  
  if (bc_in)
    m_maxDepth = &bc_in->maxDepth;
}

/* Get the coverage view widget */
GtkWidget* CoverageViewProperties::widget()
{
  return m_widget;
}

/* Return the range of ref-seq coords that the coverage view currently displays */
const IntRange* CoverageViewProperties::displayRange()
{
  const IntRange *result = NULL;
  
  // The range is the same as the parent panel
  if (m_panel)
    result = &m_panel->displayRange;

  return result;
}

/* Return the range of ref-seq coords that should be highlighted in the highlight box, if any
 * (returns NULL if not applicable) */
const IntRange* CoverageViewProperties::highlightRange()
{
  const IntRange *result = NULL;

  // The range is the same as the parent panel
  if (m_panel)
    result = m_panel->highlightRange();

  return result;
}

/* Return the position of the left border. */
double CoverageViewProperties::contentXPos() const
{
  double result = 0.0;

  // Use the left border position of the parent panel
  if (m_panel)
    result = m_panel->contentXPos();

  return result;
}

/* Return the position of the left border. */
double CoverageViewProperties::contentWidth() const
{
  double result = 0.0;

  // Use the left border position of the parent panel
  if (m_panel)
    result = m_panel->contentWidth();

  return result;
}


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

CoverageViewProperties* coverageViewGetProperties(GtkWidget *widget)
{
  CoverageViewProperties *properties = NULL;

  if (widget)
    properties = (CoverageViewProperties*)(g_object_get_data(G_OBJECT(widget), "CoverageViewProperties"));
  
  return properties;
}

static void onDestroyCoverageView(GtkWidget *widget)
{
  CoverageViewProperties *properties = coverageViewGetProperties(widget);
  
  if (properties)
    {
      delete properties;
      properties = NULL;
      g_object_set_data(G_OBJECT(widget), "CoverageViewProperties", NULL);
    }
}

static CoverageViewProperties* coverageViewCreateProperties(GtkWidget *widget, 
                                                            GtkWidget *blxWindow,
                                                            BlxContext *bc)
{
  CoverageViewProperties *properties = NULL;

  if (widget)
    { 
      properties = new CoverageViewProperties(widget, blxWindow, bc);
      
      g_object_set_data(G_OBJECT(widget), "CoverageViewProperties", properties);
      g_signal_connect(G_OBJECT(widget), "destroy", G_CALLBACK(onDestroyCoverageView), NULL); 
    }

  return properties;
}


/* Set the pointer to the parent panel that this coverage view belongs to */
void CoverageViewProperties::setPanel(BlxPanel *panel)
{
  m_panel = panel;
}


/* This function should be called whenever the coverage depth data has changed */
void CoverageViewProperties::updateDepth()
{
  /* Set up a list of 'nice' values to round to for displaying labels */
  static GSList *roundValues = NULL;
  
  if (!roundValues)
    {
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(1));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(2));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(5));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(10));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(25));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(50));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(100));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(200));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(500));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(1000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(2500));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(5000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(10000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(25000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(50000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(100000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(250000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(500000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(1000000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(2500000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(5000000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(10000000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(25000000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(50000000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(100000000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(250000000));
      roundValues = g_slist_prepend(roundValues, GINT_TO_POINTER(500000000));
    }

  if (!m_rangePerCell)
    {
      /* First time round, calculate the range per cell, aiming for 
       * around 5 cells. (If we enter this function again, it's because the
       * user has manually entered the range per cell so we just need to calculate
       * the relevant number of cells) */
      m_numVCells = 5;
      m_rangePerCell = ceil((gdouble)*m_maxDepth / (gdouble)m_numVCells);

      /* Round the result and recalculate the number of cells */
      m_rangePerCell = roundUpToValueFromList(m_rangePerCell, roundValues, NULL);
      
      if (m_rangePerCell < 1)
        m_rangePerCell = 1;
    }
  
  m_numVCells = (gdouble)*m_maxDepth / m_rangePerCell;
  
  recalculate();
}

double CoverageViewProperties::depthPerCell()
{
  return m_rangePerCell;
}

gboolean CoverageViewProperties::setDepthPerCell(const double depthPerCell_in)
{
  if (depthPerCell_in <= 0.0)
    return FALSE;
  
  m_rangePerCell = depthPerCell_in;
  updateDepth();

  return TRUE;
}

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

/* Clear the cached drawable and re-draw the coverage view */
void CoverageViewProperties::redraw()
{
  widgetClearCachedDrawable(m_widget, NULL);
  gtk_widget_queue_draw(m_widget);
}


/* Recalculate the size of the coverage view widget and redraw */
void CoverageViewProperties::recalculate()
{
  calculateBorders();
  redraw();
}


/* Draw a single bar of the coverage bar graph */
static void drawCoverageBar(const double x1,
			    const double x2,
			    double y1,
			    const double y2,
			    cairo_t *cr)
{
  double height = y2 - y1;
  double width = x2 - x1;

  if (height <= MIN_LINE_WIDTH)
    {
      height = MIN_LINE_WIDTH;
      y1 = y2 - height;
    }
  
  if (width <= 0)
    return;
  
  cairo_rectangle(cr, x1 - MIN_LINE_WIDTH, y1, width + (2 * MIN_LINE_WIDTH), height);
  cairo_fill(cr);
}


/* Utility to get the max depth that the coverage view shows, based on 
 * the number of cells and the range per cell. Note that this returns the
 * max lable value, i.e. the value of the top gridline; the real max
 * depth may be slightly greater than this, and may extend above the top
 * gridline (the height of the widget is made big enough to accommodate this). */
int CoverageViewProperties::maxLabeledDepth()
{
  /* to do: ideally we would round numcells to the nearest int rather than
   * truncating, but there is a bug with that where the horizontal grid lines
   * are sometimes not drawn with the correct labels */
  int numCells = (int)(m_numVCells);
  const int result = m_rangePerCell * numCells;
  return result;
}


double CoverageViewProperties::charWidth() const
{
  double result = 0.0;

  if (m_bc)
    result = m_bc->charWidth();

  return result;
}


/* Draw the actual coverage data as a bar chart */
void CoverageViewProperties::drawPlot(GdkDrawable *drawable)
{
  const IntRange *dispRange = displayRange();
  g_return_if_fail(m_bc && m_bc->maxDepth > 0 && dispRange);

  cairo_t *cr = gdk_cairo_create(drawable);

  const GdkColor *color = getGdkColor(BLXCOLOR_COVERAGE_PLOT, m_bc->defaultColors, FALSE, m_bc->usePrintColors);
  gdk_cairo_set_source_color(cr, color);
  
  const double pixelsPerVal = (double)m_viewRect.height / (double)*m_maxDepth;
  const int bottomBorder = m_viewRect.y + m_viewRect.height;
  
  /* Loop through each coord in the display range */
  
  double startX = -1.0;
  double prevX = -1.0;
  double prevY = -1.0;
  int coord = dispRange->min();
  
  for ( ; coord <= dispRange->max(); ++coord)
    {
      /* Get the x position for this coord (always pass displayRev as false because
       * display coords are already inverted if the display is reversed). */
      const double x = convertBaseIdxToRectPos(coord, &m_viewRect, dispRange, TRUE, FALSE, TRUE);
      const int depth = m_bc->getDepth(coord);

      /* Calculate the y position based on the depth */
      const double height = (pixelsPerVal * (double)depth);
      const double y = (double)bottomBorder - height;

      /* First time round, don't draw the line - just get the starting x and y.
       * Also, don't draw the line if y is the same as prevY; drawing is quite
       * expensive, so if we have multiple y values that are the same we remember
       * the first x coord at this y value and draw from there all in one go. */
      if (prevX == -1.0 && prevY == -1.0)
        {
          startX = x;
        }
      else if (y != prevY || coord == dispRange->max())
        {
          /* If we had multiple positions where y was the same, draw a horizontal
           * line at that y position. If there was only one position at the previous y value then
           * this will draw a single column in the bar chart (i.e. startX==prevX). */
          drawCoverageBar(startX, x, prevY, bottomBorder, cr);
          
          /* If it's the last coord, also draw the current column, because there won't be another
           * loop to take care of this */
          if (coord == dispRange->max())
            {
              const int endX = convertBaseIdxToRectPos(coord + 1, &m_viewRect, dispRange, TRUE, FALSE, TRUE);
              drawCoverageBar(x, endX, y, bottomBorder, cr);
            }

          /* Reset the starting point */
          startX = x;
        }

      prevX = x;
      prevY = y;
    }
  
  cairo_destroy(cr);
}


/* Main function for drawing the coverage view */
void CoverageViewProperties::draw(GdkDrawable *drawable)
{
  /* We should move the gridlines functions to BlxPanel so that we can remove references to
   * bigpicture here */
  GtkWidget *bigPicture = blxWindowGetBigPicture(m_blxWindow);
  BigPictureProperties *bpProperties = bigPictureGetProperties(bigPicture);

  drawVerticalGridLines(&m_viewRect, &m_highlightRect, 
			m_viewYPadding, m_bc, bpProperties, drawable);
  
  drawHorizontalGridLines(m_widget, bigPicture, &m_viewRect, m_bc, bpProperties, drawable,
			  (int)(m_numVCells), m_rangePerCell, (gdouble)*m_maxDepth, TRUE, "");
  
  drawPlot(drawable);
}


/* Calculate the borders of the highlight box (the shaded area that indicates the
 * selection range). (This is just a convenience way to call calculateHighlightBoxBorders
 * from an external function.) */
void CoverageViewProperties::calculateHighlightBoxBorders()
{  
  if (m_bc)
    {
      m_bc->highlightBoxCalcBorders(&m_displayRect, &m_highlightRect, 
                                    displayRange(), highlightRange(),
                                    0);
    }
}


/* Calculate the borders of the view */
void CoverageViewProperties::calculateBorders()
{
  /* We should move cell height to BlxPanel so that we can get rid of references to big picture here */
  GtkWidget *bigPicture = blxWindowGetBigPicture(m_blxWindow);
  
  /* Calculate the height based on the number of cells */
  const int height = ceil(m_numVCells * (double)bigPictureGetCellHeight(bigPicture));
  const int gridHeight = (int)m_numVCells * bigPictureGetCellHeight(bigPicture);
  
  m_displayRect.x = roundNearest(contentXPos());
  m_displayRect.y = height - gridHeight;
  
  m_viewRect.x = m_displayRect.x;
  m_viewRect.y = m_displayRect.y + HIGHLIGHT_BOX_Y_PAD + DEFAULT_COVERAGE_VIEW_Y_PADDING;

  m_displayRect.width = contentWidth();
  m_displayRect.height = height + 2 * (HIGHLIGHT_BOX_Y_PAD + DEFAULT_COVERAGE_VIEW_Y_PADDING);

  /* If the width isn't set (e.g. we don't have a parent panel) then use the allocation width */
  if (m_displayRect.width == 0.0)
    m_displayRect.width = m_widget->allocation.width - m_viewRect.x;

  m_viewRect.width = m_displayRect.width;
  m_viewRect.height = gridHeight;
  
  /* Get the boundaries of the highlight box */
  calculateHighlightBoxBorders();
  
  /* Set the size request to our desired height. We want a fixed heigh but don't set the
   * width, because we want the user to be able to resize that. */
  gtk_widget_set_size_request(m_widget, 0, m_displayRect.height);
}


/* Prepare the coverage view for printing (draws the transient hightlight box
 * onto the cached drawable). */
void CoverageViewProperties::prepareForPrinting()
{
  GdkDrawable *drawable = widgetGetDrawable(m_widget);
  
  if (drawable)
    {
      GdkColor *highlightBoxColor = getGdkColor(BLXCOLOR_HIGHLIGHT_BOX, m_bc->defaultColors, FALSE, m_bc->usePrintColors);
      drawHighlightBox(drawable, &m_highlightRect, HIGHLIGHT_BOX_MIN_WIDTH, highlightBoxColor);
    }
}


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

/* Expose handler. */
static gboolean onExposeCoverageView(GtkWidget *coverageView, GdkEventExpose *event, gpointer data)
{
  gboolean result = TRUE;
  CoverageViewProperties *properties = coverageViewGetProperties(coverageView);

  if (properties)
    result = properties->expose(event, data);

  return result;
}


gboolean CoverageViewProperties::expose(GdkEventExpose *event, gpointer data)
{
  gboolean result = TRUE;

  GdkDrawable *window = GTK_LAYOUT(m_widget)->bin_window;
  
  if (window)
    {
      GdkDrawable *bitmap = widgetGetDrawable(m_widget);

      if (!bitmap)
        {
          /* There isn't a bitmap yet. Create it now. */
	  bitmap = createBlankPixmap(m_widget);
          draw(bitmap);
        }
      
      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 highlight box on top of it */
          GdkColor *highlightBoxColor = getGdkColor(BLXCOLOR_HIGHLIGHT_BOX, m_bc->defaultColors, FALSE, m_bc->usePrintColors);
          drawHighlightBox(window, &m_highlightRect, HIGHLIGHT_BOX_MIN_WIDTH, highlightBoxColor);
          
          /* Draw the preview box too, if set */
          if (m_panel)
            m_panel->drawPreviewBox(window, &m_viewRect, &m_highlightRect);
        }
      else
	{
	  g_warning("Failed to draw coverageView [%p] - could not create bitmap.\n", m_widget);
	}
    }
  
  return result;
}


static void onSizeAllocateCoverageView(GtkWidget *coverageView, GtkAllocation *allocation, gpointer data)\
{
  CoverageViewProperties *coverageViewP = coverageViewGetProperties(coverageView);

  if (coverageViewP)
    coverageViewP->calculateBorders();
}


static gboolean onButtonPressCoverageView(GtkWidget *coverageView, GdkEventButton *event, gpointer data)
{
  gboolean handled = FALSE;

  CoverageViewProperties *properties = coverageViewGetProperties(coverageView);

  if (properties)
    handled = properties->buttonPress(event, data);

  return handled;
}

  
gboolean CoverageViewProperties::buttonPress(GdkEventButton *event, gpointer data)
{
  gboolean handled = FALSE;
  
  if (event->button == 2 ||
      (event->button == 1 && !handled && 
       (event->type == GDK_2BUTTON_PRESS || 
        clickedInRect(event, &m_highlightRect, HIGHLIGHT_BOX_MIN_WIDTH))))
    {
      /* Draw the preview box (draw it on the other big picture components as well) */
      int x = event->x;
      
      if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
        x = m_highlightRect.x + m_highlightRect.width / 2;
      
      if (m_panel)
        m_panel->startPreviewBox(event->x, TRUE, x - event->x);

      handled = TRUE;
    }
  
  return handled;
}


static gboolean onButtonReleaseCoverageView(GtkWidget *coverageView, GdkEventButton *event, gpointer data)
{
  gboolean handled = FALSE;
  CoverageViewProperties *properties = coverageViewGetProperties(coverageView);

  if (properties)
    handled = properties->buttonRelease(event, data);

  return handled;
}

gboolean CoverageViewProperties::buttonRelease(GdkEventButton *event, gpointer data)
{
  if (event->button == 1 || event->button == 2) /* left or middle button */
    {
      if (m_panel)
        m_panel->finishPreviewBox(event->x, &m_viewRect, &m_highlightRect);
    }
  
  return TRUE;
}
  


/* Implement custom scrolling for horizontal mouse wheel movements over the coverageView.
 * This scrolls the position of the highlight box, i.e. it scrolls the display
 * range in the detail view. */
static gboolean onScrollCoverageView(GtkWidget *coverageView, GdkEventScroll *event, gpointer data)
{
  gboolean handled = FALSE;
  CoverageViewProperties *properties = coverageViewGetProperties(coverageView);

  if (properties)
    handled = properties->scroll(event, data);

  return handled;
}


gboolean CoverageViewProperties::scroll(GdkEventScroll *event, gpointer data)
{  
  gboolean handled = FALSE;

  /* We should make the scroll functions here virtual functions that can be called from the base
   * class BlxPanel. Then we can get rid of references to bigpicture */

  switch (event->direction)
  {
    case GDK_SCROLL_LEFT:
    {
      scrollBigPictureLeftStep(blxWindowGetBigPicture(m_blxWindow));
      handled = TRUE;
      break;
    }
      
    case GDK_SCROLL_RIGHT:
    {
      scrollBigPictureRightStep(blxWindowGetBigPicture(m_blxWindow));
      handled = TRUE;
      break;
    }
      
    default:
    {
      handled = FALSE;
      break;
    }
  };
  
  return handled;
}


gboolean CoverageViewProperties::mouseMove(GdkEventMotion *event, gpointer data)
{
  if ((event->state & GDK_BUTTON1_MASK) || /* left or middle button */
       (event->state & GDK_BUTTON2_MASK))
    {
      /* Draw a preview box at the mouse pointer location */
      if (m_panel)
        m_panel->startPreviewBox(event->x, FALSE, 0);
    }
  
  return TRUE;
}

static gboolean onMouseMoveCoverageView(GtkWidget *coverageView, GdkEventMotion *event, gpointer data)
{
  gboolean handled = FALSE;
  CoverageViewProperties *properties = coverageViewGetProperties(coverageView);

  if (properties)
    handled = properties->mouseMove(event, data);

  return handled;
}



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

CoverageViewProperties* createCoverageView(GtkWidget *blxWindow, 
                                           BlxContext *bc)
{
  GtkWidget *coverageView = gtk_layout_new(NULL, NULL);

  /* Style properties */
  gtk_widget_set_redraw_on_allocate(coverageView, FALSE);
  gtk_widget_set_name(coverageView, COVERAGE_VIEW_NAME);

  /* Connect signals */
  gtk_widget_add_events(coverageView, GDK_BUTTON_PRESS_MASK);
  gtk_widget_add_events(coverageView, GDK_BUTTON_RELEASE_MASK);
  gtk_widget_add_events(coverageView, GDK_POINTER_MOTION_MASK);
  
  g_signal_connect(G_OBJECT(coverageView), "expose-event",          G_CALLBACK(onExposeCoverageView),                 NULL);  
  g_signal_connect(G_OBJECT(coverageView), "size-allocate",	    G_CALLBACK(onSizeAllocateCoverageView),           NULL);
  g_signal_connect(G_OBJECT(coverageView), "button-press-event",    G_CALLBACK(onButtonPressCoverageView),	      NULL);
  g_signal_connect(G_OBJECT(coverageView), "button-release-event",  G_CALLBACK(onButtonReleaseCoverageView),	      NULL);
  g_signal_connect(G_OBJECT(coverageView), "motion-notify-event",   G_CALLBACK(onMouseMoveCoverageView),	      NULL);
  g_signal_connect(G_OBJECT(coverageView), "scroll-event",	    G_CALLBACK(onScrollCoverageView),                 NULL);

  /* Set required data in the coverageView. */
  CoverageViewProperties *cvProperties = coverageViewCreateProperties(coverageView, blxWindow, bc);
  
  return cvProperties;
}