Initial stab at tempo ramps.
[ardour.git] / gtk2_ardour / editor_tempodisplay.cc
1 /*
2     Copyright (C) 2002 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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <cstdio> // for sprintf, grrr
25 #include <cstdlib>
26 #include <cmath>
27 #include <string>
28 #include <climits>
29
30 #include "pbd/error.h"
31 #include "pbd/memento_command.h"
32
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/gtk_ui.h>
35
36 #include "ardour/session.h"
37 #include "ardour/tempo.h"
38 #include <gtkmm2ext/doi.h>
39 #include <gtkmm2ext/utils.h>
40
41 #include "canvas/canvas.h"
42 #include "canvas/item.h"
43 #include "canvas/line_set.h"
44
45 #include "editor.h"
46 #include "marker.h"
47 #include "tempo_dialog.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
50 #include "time_axis_view.h"
51 #include "tempo_lines.h"
52 #include "ui_config.h"
53
54 #include "i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtk;
60 using namespace Gtkmm2ext;
61 using namespace Editing;
62
63 void
64 Editor::remove_metric_marks ()
65 {
66         /* don't delete these while handling events, just punt till the GUI is idle */
67
68         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
69                 delete_when_idle (*x);
70         }
71         metric_marks.clear ();
72 }
73
74 void
75 Editor::draw_metric_marks (const Metrics& metrics)
76 {
77
78         const MeterSection *ms;
79         const TempoSection *ts;
80         char buf[64];
81
82         remove_metric_marks ();
83
84         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
85
86                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
87                         snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
88                         metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
89                                                                  *(const_cast<MeterSection*>(ms))));
90                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
91                         if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
92                                 snprintf (buf, sizeof (buf), "%.2f/%.0f", ts->beats_per_minute(), ts->note_type());
93                         } else {
94                                 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
95                         }
96                         metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), buf,
97                                                                  *(const_cast<TempoSection*>(ts))));
98                 }
99
100         }
101
102 }
103
104 void
105 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
106 {
107         if (!_session) {
108                 return;
109         }
110
111         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
112
113         if (tempo_lines) {
114                 tempo_lines->tempo_map_changed();
115         }
116
117         std::vector<TempoMap::BBTPoint> grid;
118
119         compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
120         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
121         draw_measures (grid);
122         update_tempo_based_rulers (grid);
123 }
124
125 void
126 Editor::redisplay_tempo (bool immediate_redraw)
127 {
128         if (!_session) {
129                 return;
130         }
131
132         if (immediate_redraw) {
133                 std::vector<TempoMap::BBTPoint> grid;
134
135                 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
136                 draw_measures (grid);
137                 update_tempo_based_rulers (grid); // redraw rulers and measure lines
138
139         } else {
140                 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
141         }
142 }
143
144 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
145 void
146 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
147 {
148         if (!_session) {
149                 return;
150         }
151
152         framecnt_t beat_before_lower_pos = _session->tempo_map().frame_at_beat (floor(_session->tempo_map().beat_at_frame (leftmost)));
153         framecnt_t beat_after_upper_pos = _session->tempo_map().frame_at_beat (floor (_session->tempo_map().beat_at_frame (rightmost)) + 1.0);
154
155         /* prevent negative values of leftmost from creeping into tempomap
156          */
157         _session->tempo_map().get_grid (grid, max (beat_before_lower_pos, (framepos_t) 0), beat_after_upper_pos);
158 }
159
160 void
161 Editor::hide_measures ()
162 {
163         if (tempo_lines) {
164                 tempo_lines->hide();
165         }
166 }
167
168 void
169 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
170 {
171         if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
172                 return;
173         }
174
175         if (tempo_lines == 0) {
176                 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
177         }
178
179         const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
180         tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
181 }
182
183 void
184 Editor::mouse_add_new_tempo_event (framepos_t frame)
185 {
186         if (_session == 0) {
187                 return;
188         }
189
190         TempoMap& map(_session->tempo_map());
191         TempoDialog tempo_dialog (map, frame, _("add"));
192
193         //this causes compiz to display no border.
194         //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
195
196         switch (tempo_dialog.run()) {
197         case RESPONSE_ACCEPT:
198                 break;
199         default:
200                 return;
201         }
202
203         double bpm = 0;
204         Timecode::BBT_Time requested;
205
206         bpm = tempo_dialog.get_bpm ();
207         double nt = tempo_dialog.get_note_type();
208         bpm = max (0.01, bpm);
209
210         tempo_dialog.get_bbt_time (requested);
211
212         begin_reversible_command (_("add tempo mark"));
213         XMLNode &before = map.get_state();
214         map.add_tempo (Tempo (bpm,nt), requested, tempo_dialog.get_tempo_type());
215         XMLNode &after = map.get_state();
216         _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
217         commit_reversible_command ();
218
219         //map.dump (cerr);
220 }
221
222 void
223 Editor::mouse_add_new_meter_event (framepos_t frame)
224 {
225         if (_session == 0) {
226                 return;
227         }
228
229
230         TempoMap& map(_session->tempo_map());
231         MeterDialog meter_dialog (map, frame, _("add"));
232
233         //this causes compiz to display no border..
234         //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
235
236         switch (meter_dialog.run ()) {
237         case RESPONSE_ACCEPT:
238                 break;
239         default:
240                 return;
241         }
242
243         double bpb = meter_dialog.get_bpb ();
244         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
245
246         double note_type = meter_dialog.get_note_type ();
247         Timecode::BBT_Time requested;
248
249         meter_dialog.get_bbt_time (requested);
250
251         begin_reversible_command (_("add meter mark"));
252         XMLNode &before = map.get_state();
253         map.add_meter (Meter (bpb, note_type), requested);
254         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
255         commit_reversible_command ();
256
257         //map.dump (cerr);
258 }
259
260 void
261 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
262 {
263         ArdourMarker* marker;
264         TempoMarker* tempo_marker;
265
266         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
267                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
268                 abort(); /*NOTREACHED*/
269         }
270
271         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
272                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
273                 abort(); /*NOTREACHED*/
274         }
275
276         if (tempo_marker->tempo().movable()) {
277                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
278         }
279 }
280
281 void
282 Editor::edit_meter_section (MeterSection* section)
283 {
284         MeterDialog meter_dialog (*section, _("done"));
285
286         switch (meter_dialog.run()) {
287         case RESPONSE_ACCEPT:
288                 break;
289         default:
290                 return;
291         }
292
293         double bpb = meter_dialog.get_bpb ();
294         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
295
296         double note_type = meter_dialog.get_note_type ();
297
298         Timecode::BBT_Time when;
299         meter_dialog.get_bbt_time(when);
300
301         begin_reversible_command (_("replace tempo mark"));
302         XMLNode &before = _session->tempo_map().get_state();
303         _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), when);
304         XMLNode &after = _session->tempo_map().get_state();
305         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
306         commit_reversible_command ();
307 }
308
309 void
310 Editor::edit_tempo_section (TempoSection* section)
311 {
312         TempoDialog tempo_dialog (*section, _("done"));
313
314         switch (tempo_dialog.run ()) {
315         case RESPONSE_ACCEPT:
316                 break;
317         default:
318                 return;
319         }
320
321         double bpm = tempo_dialog.get_bpm ();
322         double nt = tempo_dialog.get_note_type ();
323         Timecode::BBT_Time when;
324         tempo_dialog.get_bbt_time(when);
325         bpm = max (0.01, bpm);
326
327         begin_reversible_command (_("replace tempo mark"));
328         XMLNode &before = _session->tempo_map().get_state();
329         _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), when, tempo_dialog.get_tempo_type());
330         XMLNode &after = _session->tempo_map().get_state();
331         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
332         commit_reversible_command ();
333 }
334
335 void
336 Editor::edit_tempo_marker (TempoMarker& tm)
337 {
338         edit_tempo_section (&tm.tempo());
339 }
340
341 void
342 Editor::edit_meter_marker (MeterMarker& mm)
343 {
344         edit_meter_section (&mm.meter());
345 }
346
347 gint
348 Editor::real_remove_tempo_marker (TempoSection *section)
349 {
350         begin_reversible_command (_("remove tempo mark"));
351         XMLNode &before = _session->tempo_map().get_state();
352         _session->tempo_map().remove_tempo (*section, true);
353         XMLNode &after = _session->tempo_map().get_state();
354         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
355         commit_reversible_command ();
356
357         return FALSE;
358 }
359
360 void
361 Editor::remove_meter_marker (ArdourCanvas::Item* item)
362 {
363         ArdourMarker* marker;
364         MeterMarker* meter_marker;
365
366         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
367                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
368                 abort(); /*NOTREACHED*/
369         }
370
371         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
372                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
373                 abort(); /*NOTREACHED*/
374         }
375
376         if (meter_marker->meter().movable()) {
377           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
378         }
379 }
380
381 gint
382 Editor::real_remove_meter_marker (MeterSection *section)
383 {
384         begin_reversible_command (_("remove tempo mark"));
385         XMLNode &before = _session->tempo_map().get_state();
386         _session->tempo_map().remove_meter (*section, true);
387         XMLNode &after = _session->tempo_map().get_state();
388         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
389         commit_reversible_command ();
390
391         return FALSE;
392 }