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