5b73c877acb53281493dec49aaa0f7ac848ab495
[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 <libgnomecanvasmm/canvas.h>
21 #include <libgnomecanvasmm/group.h>
22 #include "tempo_lines.h"
23 #include "ardour_ui.h"
24
25 using namespace std;
26
27 #define MAX_CACHED_LINES 512
28         
29 TempoLines::TempoLines(ArdourCanvas::Canvas& canvas, ArdourCanvas::Group* group)
30         : _canvas(canvas)
31         , _group(group)
32         , _clean_left(DBL_MAX)
33         , _clean_right(0.0)
34 {
35 }
36
37 void
38 TempoLines::tempo_map_changed()
39 {
40         _clean_left = DBL_MAX;
41         _clean_right = 0.0;
42
43         size_t d = 0;
44         // TODO: Dirty/slow, but 'needed' for zoom :(
45         for (Lines::iterator i = _lines.begin(); i != _lines.end(); ++d) {
46                 Lines::iterator next = i;
47                 ++next;
48         i->second->property_x1() = DBL_MAX - d;
49         i->second->property_x2() = DBL_MAX - d;
50                 _lines.erase(i);
51                 _lines.insert(make_pair(DBL_MAX - d, i->second));
52                 i = next;
53         }
54 }
55
56 void
57 TempoLines::show ()
58 {
59         for (Lines::iterator i = _lines.begin(); i != _lines.end(); ++i) {
60         i->second->show();
61         }
62 }
63
64 void
65 TempoLines::hide ()
66 {
67         for (Lines::iterator i = _lines.begin(); i != _lines.end(); ++i) {
68         i->second->hide();
69         }
70 }
71
72 void
73 TempoLines::draw (ARDOUR::TempoMap::BBTPointList& points, double frames_per_unit)
74 {
75         ARDOUR::TempoMap::BBTPointList::iterator i;
76         ArdourCanvas::SimpleLine *line = NULL;
77         gdouble xpos;
78         double who_cares;
79         double x1, x2, y1, y2, beat_density;
80
81         uint32_t beats = 0;
82         uint32_t bars = 0;
83         uint32_t color;
84         
85         const size_t needed = points.size();
86
87         _canvas.get_scroll_region (x1, y1, x2, who_cares);
88         _canvas.root()->get_bounds(who_cares, who_cares, who_cares, y2);
89
90         /* get the first bar spacing */
91
92         i = points.end();
93         i--;
94         bars = (*i).bar - (*points.begin()).bar;
95         beats = points.size() - bars;
96
97         beat_density = (beats * 10.0f) / _canvas.get_width ();
98
99         if (beat_density > 4.0f) {
100                 /* if the lines are too close together, they become useless */
101                 return;
102         }
103
104         xpos = rint(((nframes64_t)(*i).frame) / (double)frames_per_unit);
105         const double needed_right = xpos;
106
107         i = points.begin();
108         
109         xpos = rint(((nframes64_t)(*i).frame) / (double)frames_per_unit);
110         const double needed_left = xpos;
111
112         Lines::iterator left = _lines.lower_bound(xpos); // first line >= xpos
113
114         bool exhausted = (left == _lines.end());
115         Lines::iterator li = left;
116         if (li != _lines.end())
117                 line = li->second;
118         
119         // Tempo map hasn't changed and we're entirely within a clean
120         // range, don't need to do anything.  Yay.
121         if (needed_left >= _clean_left && needed_right <= _clean_right) {
122                 //cout << endl << "*** LINE CACHE PERFECT HIT" << endl;
123                 return;
124         }
125
126         //cout << endl << "*** LINE CACHE MISS" << endl;
127
128         bool inserted_last_time = true;
129         bool invalidated = false;
130         
131         for (i = points.begin(); i != points.end(); ++i) {
132
133                 switch ((*i).type) {
134                 case ARDOUR::TempoMap::Bar:
135                         break;
136
137                 case ARDOUR::TempoMap::Beat:
138                         if ((*i).beat == 1) {
139                                 color = ARDOUR_UI::config()->canvasvar_MeasureLineBar.get();
140                         } else {
141                                 color = ARDOUR_UI::config()->canvasvar_MeasureLineBeat.get();
142                                 if (beat_density > 2.0) {
143                                         break; /* only draw beat lines if the gaps between beats are large. */
144                                 }
145                         }
146                         
147                         xpos = rint(((nframes64_t)(*i).frame) / (double)frames_per_unit);
148
149                         if (inserted_last_time && !_lines.empty()) {
150                                 li = _lines.lower_bound(xpos); // first line >= xpos
151                         }
152                                 
153                         line = (li != _lines.end()) ? li->second : NULL;
154                         assert(!line || line->property_x1() == li->first);
155                                 
156                         Lines::iterator next = li;
157                         if (next != _lines.end())
158                                 ++next;
159
160                         exhausted = (next == _lines.end());
161                         
162                         // Hooray, line is perfect                      
163                         if (line && line->property_x1() == xpos) {
164                                 if (li != _lines.end())
165                                         ++li;
166
167                                 line->property_color_rgba() = color;
168                                 inserted_last_time = false; // don't search next time
169
170                         // Use existing line, moving if necessary
171                         } else if (!exhausted) {
172                                 Lines::iterator steal = _lines.end();
173                                 --steal;
174                                 
175                                 // Steal from the right
176                                 if (left->first > needed_left && li != steal && steal->first > needed_right) {
177                                         //cout << "*** STEALING FROM RIGHT" << endl;
178                                         line = steal->second;
179                                         _lines.erase(steal);
180                                         line->property_x1() = xpos;
181                                         line->property_x2() = xpos;
182                                         line->property_color_rgba() = color;
183                                         _lines.insert(make_pair(xpos, line));
184                                         inserted_last_time = true; // search next time
185                                         invalidated = true;
186
187                                         // Shift clean range left
188                                         _clean_left = min(_clean_left, xpos);
189                                         _clean_right = min(_clean_right, steal->first);
190
191                                 // Move this line to where we need it
192                                 } else {
193                                         Lines::iterator existing = _lines.find(xpos);
194                                         if (existing != _lines.end()) {
195                                                 //cout << "*** EXISTING LINE" << endl;
196                                                 li = existing;
197                                                 li->second->property_color_rgba() = color;
198                                                 inserted_last_time = false; // don't search next time
199                                         } else {
200                                                 //cout << "*** MOVING LINE" << endl;
201                                                 const double x1 = line->property_x1();
202                                                 const bool was_clean = x1 >= _clean_left && x1 <= _clean_right;
203                                                 invalidated = invalidated || was_clean;
204                                                 // Invalidate clean portion (XXX: too harsh?)
205                                                 _clean_left  = needed_left;
206                                                 _clean_right = needed_right;
207                                                 _lines.erase(li);
208                                                 line->property_color_rgba() = color;
209                                                 line->property_x1() = xpos;
210                                                 line->property_x2() = xpos;
211                                                 _lines.insert(make_pair(xpos, line));
212                                                 inserted_last_time = true; // search next time
213                                         }
214                                 }
215
216                         // Create a new line
217                         } else if (_lines.size() < needed || _lines.size() < MAX_CACHED_LINES) {
218                                 //cout << "*** CREATING LINE" << endl;
219                                 assert(_lines.find(xpos) == _lines.end());
220                                 line = new ArdourCanvas::SimpleLine (*_group);
221                                 line->property_x1() = xpos;
222                                 line->property_x2() = xpos;
223                                 line->property_y2() = y2;
224                                 line->property_color_rgba() = color;
225                                 _lines.insert(make_pair(xpos, line));
226                                 inserted_last_time = true;
227
228                         // Steal from the left
229                         } else {
230                                 //cout << "*** STEALING FROM LEFT" << endl;
231                                 assert(_lines.find(xpos) == _lines.end());
232                                 Lines::iterator steal = _lines.begin();
233                                 line = steal->second;
234                                 _lines.erase(steal);
235                                 line->property_color_rgba() = color;
236                                 line->property_x1() = xpos;
237                                 line->property_x2() = xpos;
238                                 _lines.insert(make_pair(xpos, line));
239                                 inserted_last_time = true; // search next time
240                                 invalidated = true;
241                                 
242                                 // Shift clean range right
243                                 _clean_left = max(_clean_left, steal->first);
244                                 _clean_right = max(_clean_right, xpos);
245                         }
246
247                         break;
248                 }
249         }
250
251         // Extend range to what we've 'fixed'
252         if (!invalidated) {
253                 _clean_left  = min(_clean_left, needed_left);
254                 _clean_right = max(_clean_right, needed_right);
255         }
256 }
257