Merge branch 'export-dialog' into 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/line.h"
23 #include "canvas/canvas.h"
24 #include "canvas/debug.h"
25
26 #include "tempo_lines.h"
27 #include "ardour_ui.h"
28 #include "public_editor.h"
29
30 using namespace std;
31
32 TempoLines::TempoLines (ArdourCanvas::Canvas& canvas, ArdourCanvas::Group* group, double h)
33         : _canvas (canvas)
34         , _group (group)
35         , _height (h)
36 {
37 }
38
39 void
40 TempoLines::tempo_map_changed()
41 {
42         /* remove all lines from the group, put them in the cache (to avoid
43          * unnecessary object destruction+construction later), and clear _lines
44          */
45          
46         _group->clear ();
47         _cache.insert (_cache.end(), _lines.begin(), _lines.end());
48         _lines.clear ();
49 }
50
51 void
52 TempoLines::show ()
53 {
54         _group->show ();
55 }
56
57 void
58 TempoLines::hide ()
59 {
60         _group->hide ();
61 }
62
63 void
64 TempoLines::draw (const ARDOUR::TempoMap::BBTPointList::const_iterator& begin, 
65                   const ARDOUR::TempoMap::BBTPointList::const_iterator& end)
66 {
67         ARDOUR::TempoMap::BBTPointList::const_iterator i;
68         ArdourCanvas::Rect const visible = _canvas.visible_area ();
69         double  beat_density;
70
71         uint32_t beats = 0;
72         uint32_t bars = 0;
73         uint32_t color;
74
75         /* get the first bar spacing */
76
77         i = end;
78         i--;
79         bars = (*i).bar - (*begin).bar; 
80         beats = distance (begin, end) - bars;
81
82         beat_density = (beats * 10.0f) / visible.width ();
83
84         if (beat_density > 4.0f) {
85                 /* if the lines are too close together, they become useless */
86                 tempo_map_changed();
87                 return;
88         }
89
90         tempo_map_changed ();
91
92         for (i = begin; i != end; ++i) {
93
94                 if ((*i).is_bar()) {
95                         color = ARDOUR_UI::config()->get_canvasvar_MeasureLineBar();
96                 } else {
97                         if (beat_density > 2.0) {
98                                 continue; /* only draw beat lines if the gaps between beats are large. */
99                         }
100                         color = ARDOUR_UI::config()->get_canvasvar_MeasureLineBeat();
101                 }
102
103                 ArdourCanvas::Coord xpos = PublicEditor::instance().sample_to_pixel_unrounded ((*i).frame);
104
105                 ArdourCanvas::Line* line;
106
107                 if (!_cache.empty()) {
108                         line = _cache.back ();
109                         _cache.pop_back ();
110                         line->reparent (_group);
111                 } else {
112                         line = new ArdourCanvas::Line (_group);
113                         CANVAS_DEBUG_NAME (line, string_compose ("tempo measure line @ %1", (*i).frame));
114                         line->set_ignore_events (true);
115                 }
116
117                 line->set_x0 (xpos);
118                 line->set_x1 (xpos);
119                 line->set_y0 (0.0);
120                 line->set_y1 (_height);
121                 line->set_outline_color (color);
122                 line->show ();
123         }
124 }
125