Merge branch 'cairocanvas'
[ardour.git] / gtk2_ardour / tempo_lines.cc
1 /*
2     Copyright (C) 2002-2007 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/compose.h"
21
22 #include "canvas/canvas.h"
23 #include "canvas/debug.h"
24
25 #include "tempo_lines.h"
26 #include "ardour_ui.h"
27 #include "public_editor.h"
28
29 using namespace std;
30
31 TempoLines::TempoLines (ArdourCanvas::Container* group, double)
32         : lines (group, ArdourCanvas::LineSet::Vertical)
33 {
34         lines.set_extent (ArdourCanvas::COORD_MAX);
35 }
36
37 void
38 TempoLines::tempo_map_changed()
39 {
40         lines.clear ();
41 }
42
43 void
44 TempoLines::show ()
45 {
46         lines.show ();
47 }
48
49 void
50 TempoLines::hide ()
51 {
52         lines.hide ();
53 }
54
55 void
56 TempoLines::draw (const ARDOUR::TempoMap::BBTPointList::const_iterator& begin, 
57                   const ARDOUR::TempoMap::BBTPointList::const_iterator& end)
58 {
59         ARDOUR::TempoMap::BBTPointList::const_iterator i;
60         double  beat_density;
61
62         uint32_t beats = 0;
63         uint32_t bars = 0;
64         uint32_t color;
65
66         /* get the first bar spacing */
67
68         i = end;
69         i--;
70         bars = (*i).bar - (*begin).bar; 
71         beats = distance (begin, end) - bars;
72
73         beat_density = (beats * 10.0f) / lines.canvas()->width();
74
75         if (beat_density > 4.0f) {
76                 /* if the lines are too close together, they become useless */
77                 lines.clear ();
78                 return;
79         }
80
81         lines.clear ();
82
83         for (i = begin; i != end; ++i) {
84
85                 if ((*i).is_bar()) {
86                         color = ARDOUR_UI::config()->get_canvasvar_MeasureLineBar();
87                 } else {
88                         if (beat_density > 0.3) {
89                                 continue; /* only draw beat lines if the gaps between beats are large. */
90                         }
91                         color = ARDOUR_UI::config()->get_canvasvar_MeasureLineBeat();
92                 }
93
94                 ArdourCanvas::Coord xpos = PublicEditor::instance().sample_to_pixel_unrounded ((*i).frame);
95
96                 lines.add (xpos, 1.0, color);
97         }
98 }
99