Remove action-driven keyboard brush, which is the same as paste as far as I can see.
[ardour2.git] / gtk2_ardour / tempo_lines.cc
blob8fe877a27cca93258d8633a623ec66fef20cbf6d
1 /*
2 Copyright (C) 2002-2007 Paul Davis
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.
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.
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.
20 #include <libgnomecanvasmm/canvas.h>
21 #include <libgnomecanvasmm/group.h>
22 #include "tempo_lines.h"
23 #include "ardour_ui.h"
25 using namespace std;
27 #define MAX_CACHED_LINES 128
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)
38 void
39 TempoLines::tempo_map_changed()
41 _clean_left = DBL_MAX;
42 _clean_right = 0.0;
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;
57 void
58 TempoLines::show ()
60 for (Lines::iterator i = _lines.begin(); i != _lines.end(); ++i) {
61 i->second->show();
65 void
66 TempoLines::hide ()
68 for (Lines::iterator i = _lines.begin(); i != _lines.end(); ++i) {
69 i->second->hide();
73 void
74 TempoLines::draw (ARDOUR::TempoMap::BBTPointList& points, double frames_per_unit)
76 ARDOUR::TempoMap::BBTPointList::iterator i;
77 ArdourCanvas::SimpleLine *line = NULL;
78 gdouble xpos;
79 double who_cares;
80 double x1, x2, y1, beat_density;
82 uint32_t beats = 0;
83 uint32_t bars = 0;
84 uint32_t color;
86 const size_t needed = points.size();
88 _canvas.get_scroll_region (x1, y1, x2, who_cares);
90 /* get the first bar spacing */
92 i = points.end();
93 i--;
94 bars = (*i).bar - (*points.begin()).bar;
95 beats = points.size() - bars;
97 beat_density = (beats * 10.0f) / _canvas.get_width ();
99 if (beat_density > 4.0f) {
100 /* if the lines are too close together, they become useless */
101 tempo_map_changed();
102 return;
105 xpos = rint(((framepos_t)(*i).frame) / (double)frames_per_unit);
106 const double needed_right = xpos;
108 i = points.begin();
110 xpos = rint(((framepos_t)(*i).frame) / (double)frames_per_unit);
111 const double needed_left = xpos;
113 Lines::iterator left = _lines.lower_bound(xpos); // first line >= xpos
115 bool exhausted = (left == _lines.end());
116 Lines::iterator li = left;
117 if (li != _lines.end())
118 line = li->second;
120 // Tempo map hasn't changed and we're entirely within a clean
121 // range, don't need to do anything. Yay.
122 if (needed_left >= _clean_left && needed_right <= _clean_right) {
123 //cout << endl << "*** LINE CACHE PERFECT HIT" << endl;
124 return;
127 //cout << endl << "*** LINE CACHE MISS" << endl;
129 bool inserted_last_time = true;
130 bool invalidated = false;
132 for (i = points.begin(); i != points.end(); ++i) {
134 switch ((*i).type) {
135 case ARDOUR::TempoMap::Bar:
136 break;
138 case ARDOUR::TempoMap::Beat:
139 if ((*i).beat == 1) {
140 color = ARDOUR_UI::config()->canvasvar_MeasureLineBar.get();
141 } else {
142 color = ARDOUR_UI::config()->canvasvar_MeasureLineBeat.get();
143 if (beat_density > 2.0) {
144 break; /* only draw beat lines if the gaps between beats are large. */
148 xpos = rint(((framepos_t)(*i).frame) / (double)frames_per_unit);
150 if (inserted_last_time && !_lines.empty()) {
151 li = _lines.lower_bound(xpos); // first line >= xpos
154 line = (li != _lines.end()) ? li->second : NULL;
155 assert(!line || line->property_x1() == li->first);
157 Lines::iterator next = li;
158 if (next != _lines.end())
159 ++next;
161 exhausted = (next == _lines.end());
163 // Hooray, line is perfect
164 if (line && line->property_x1() == xpos) {
165 if (li != _lines.end())
166 ++li;
168 line->property_color_rgba() = color;
169 inserted_last_time = false; // don't search next time
171 // Use existing line, moving if necessary
172 } else if (!exhausted) {
173 Lines::iterator steal = _lines.end();
174 --steal;
176 // Steal from the right
177 if (left->first > needed_left && li != steal && steal->first > needed_right) {
178 //cout << "*** STEALING FROM RIGHT" << endl;
179 line = steal->second;
180 _lines.erase(steal);
181 line->property_x1() = xpos;
182 line->property_x2() = xpos;
183 line->property_color_rgba() = color;
184 _lines.insert(make_pair(xpos, line));
185 inserted_last_time = true; // search next time
186 invalidated = true;
188 // Shift clean range left
189 _clean_left = min(_clean_left, xpos);
190 _clean_right = min(_clean_right, steal->first);
192 // Move this line to where we need it
193 } else {
194 Lines::iterator existing = _lines.find(xpos);
195 if (existing != _lines.end()) {
196 //cout << "*** EXISTING LINE" << endl;
197 li = existing;
198 li->second->property_color_rgba() = color;
199 inserted_last_time = false; // don't search next time
200 } else {
201 //cout << "*** MOVING LINE" << endl;
202 const double x1 = line->property_x1();
203 const bool was_clean = x1 >= _clean_left && x1 <= _clean_right;
204 invalidated = invalidated || was_clean;
205 // Invalidate clean portion (XXX: too harsh?)
206 _clean_left = needed_left;
207 _clean_right = needed_right;
208 _lines.erase(li);
209 line->property_color_rgba() = color;
210 line->property_x1() = xpos;
211 line->property_x2() = xpos;
212 _lines.insert(make_pair(xpos, line));
213 inserted_last_time = true; // search next time
217 // Create a new line
218 } else if (_lines.size() < needed || _lines.size() < MAX_CACHED_LINES) {
219 //cout << "*** CREATING LINE" << endl;
220 assert(_lines.find(xpos) == _lines.end());
221 line = new ArdourCanvas::SimpleLine (*_group);
222 line->property_x1() = xpos;
223 line->property_x2() = xpos;
224 line->property_y1() = 0.0;
225 line->property_y2() = _height;
226 line->property_color_rgba() = color;
227 _lines.insert(make_pair(xpos, line));
228 inserted_last_time = true;
230 // Steal from the left
231 } else {
232 //cout << "*** STEALING FROM LEFT" << endl;
233 assert(_lines.find(xpos) == _lines.end());
234 Lines::iterator steal = _lines.begin();
235 line = steal->second;
236 _lines.erase(steal);
237 line->property_color_rgba() = color;
238 line->property_x1() = xpos;
239 line->property_x2() = xpos;
240 _lines.insert(make_pair(xpos, line));
241 inserted_last_time = true; // search next time
242 invalidated = true;
244 // Shift clean range right
245 _clean_left = max(_clean_left, steal->first);
246 _clean_right = max(_clean_right, xpos);
249 break;
253 // Extend range to what we've 'fixed'
254 if (!invalidated) {
255 _clean_left = min(_clean_left, needed_left);
256 _clean_right = max(_clean_right, needed_right);