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