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