TempoLines deletes its bfc on destruction
[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 "public_editor.h"
27 #include "rgb_macros.h"
28 #include "ui_config.h"
29
30 using namespace std;
31
32 TempoLines::TempoLines (ArdourCanvas::Container* group, double, ARDOUR::BeatsFramesConverter* bfc)
33         : lines (group, ArdourCanvas::LineSet::Vertical)
34         , _bfc (bfc)
35 {
36         lines.set_extent (ArdourCanvas::COORD_MAX);
37 }
38
39 TempoLines::~TempoLines ()
40 {
41         delete _bfc;
42         _bfc = 0;
43 }
44
45 void
46 TempoLines::tempo_map_changed()
47 {
48         lines.clear ();
49 }
50
51 void
52 TempoLines::show ()
53 {
54         lines.show ();
55 }
56
57 void
58 TempoLines::hide ()
59 {
60         lines.hide ();
61 }
62
63 void
64 TempoLines::draw_ticks (std::vector<ARDOUR::TempoMap::BBTPoint>& grid,
65                         unsigned                                              divisions,
66                         framecnt_t                                            leftmost_frame,
67                         framecnt_t                                            frame_rate)
68 {
69         const uint32_t base = UIConfiguration::instance().color_mod("measure line beat", "measure line beat");
70
71         for (unsigned l = 1; l < divisions; ++l) {
72                 /* find the coarsest division level this tick falls on */
73                 unsigned level = divisions;
74                 for (unsigned d = divisions; d >= 4; d /= 2) {
75                         if (l % (divisions / d) == 0) {
76                                 level = d;
77                         }
78                 }
79                 /* draw line with alpha corresponding to coarsest level */
80                 const uint8_t    a = max(8, (int)rint(UINT_RGBA_A(base) / (0.8 * log2(level))));
81                 const uint32_t   c = UINT_RGBA_CHANGE_A(base, a);
82                 const framepos_t f = _bfc->to (Evoral::Beats (grid.begin()->qn + (l / (double) divisions)));
83
84                 if (f > leftmost_frame) {
85                         lines.add (PublicEditor::instance().sample_to_pixel_unrounded (f), 1.0, c);
86                 }
87         }
88 }
89
90 void
91 TempoLines::draw (std::vector<ARDOUR::TempoMap::BBTPoint>& grid,
92                   unsigned                                              divisions,
93                   framecnt_t                                            leftmost_frame,
94                   framecnt_t                                            frame_rate)
95 {
96         std::vector<ARDOUR::TempoMap::BBTPoint>::const_iterator i;
97         double  beat_density;
98
99         uint32_t beats = 0;
100         uint32_t bars = 0;
101         const uint32_t bar_color = UIConfiguration::instance().color ("measure line bar");
102         const uint32_t beat_color = UIConfiguration::instance().color_mod ("measure line beat", "measure line beat");
103         uint32_t color;
104
105         bool all_bars = false;
106         /* get the first bar spacing */
107
108         i = grid.end();
109         i--;
110         bars = (*i).bar - (*grid.begin()).bar;
111
112         int32_t bar_mod = 4;
113
114         if (bars < distance (grid.begin(), grid.end()) - 1) {
115                 /* grid contains beats and bars */
116                 beats = distance (grid.begin(), grid.end()) - bars;
117         } else {
118                 /* grid contains only bars */
119                 beats = distance (grid.begin(), grid.end());
120
121                 if (i != grid.begin()) {
122                         const int32_t last_bar = (*i).bar;
123                         i--;
124                         bar_mod = (last_bar - (*i).bar) * 4;
125                 }
126
127                 all_bars = true;
128         }
129
130         double canvas_width_used = 1.0;
131         if (leftmost_frame < grid.front().frame) {
132                 const framecnt_t frame_distance = max ((framecnt_t) 1, grid.back().frame - grid.front().frame);
133                 canvas_width_used = 1.0 - ((grid.front().frame - leftmost_frame) / (double) (frame_distance + grid.front().frame));
134         }
135
136         beat_density = (beats * 10.0f) / (lines.canvas()->width() * canvas_width_used);
137
138         if (beat_density > 2.0f) {
139                 /* if the lines are too close together, they become useless */
140                 lines.clear ();
141                 return;
142         }
143
144         /* constrain divisions to a log2 factor to cap line density */
145         while (divisions > 3 && beat_density * divisions > 0.4) {
146                 divisions /= 2;
147         }
148
149         lines.clear ();
150         if (beat_density <= 0.12 && grid.begin() != grid.end() && grid.begin()->frame > 0 && !all_bars) {
151                 /* draw subdivisions of the beat before the first visible beat line XX this shouldn't happen now */
152                 std::vector<ARDOUR::TempoMap::BBTPoint> vec;
153                 vec.push_back (*i);
154                 draw_ticks (vec, divisions, leftmost_frame, frame_rate);
155         }
156
157         for (i = grid.begin(); i != grid.end(); ++i) {
158
159                 if ((*i).is_bar()) {
160                         /* keep all_bar beat density down */
161                         if (all_bars && beat_density > 0.3 && ((*i).bar % bar_mod) != 1) {
162                                 continue;
163                         }
164
165                         color = bar_color;
166                 } else {
167                         if (beat_density > 0.3) {
168                                 continue; /* only draw beat lines if the gaps between beats are large. */
169                         }
170                         color = beat_color;
171                 }
172
173                 ArdourCanvas::Coord xpos = PublicEditor::instance().sample_to_pixel_unrounded ((*i).frame);
174
175                 lines.add (xpos, 1.0, color);
176
177                 if (beat_density <= 0.12 && !all_bars) {
178                         /* draw subdivisions of this beat */
179                         std::vector<ARDOUR::TempoMap::BBTPoint> vec;
180                         vec.push_back (*i);
181                         draw_ticks (vec, divisions, leftmost_frame, frame_rate);
182                 }
183         }
184 }
185