enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 "pbd/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         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
74                 delete (*x);
75         }
76         tempo_curves.clear ();
77 }
78
79 void
80 Editor::draw_metric_marks (const Metrics& metrics)
81 {
82         char buf[64];
83         double max_tempo = 0.0;
84         double min_tempo = DBL_MAX;
85
86         remove_metric_marks (); // also clears tempo curves
87
88         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
89                 const MeterSection *ms;
90                 const TempoSection *ts;
91
92                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
93                         snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
94                         metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
95                                                                  *(const_cast<MeterSection*>(ms))));
96                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
97                         if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
98                                 snprintf (buf, sizeof (buf), "%.3f/%.0f", ts->beats_per_minute(), ts->note_type());
99                         } else {
100                                 snprintf (buf, sizeof (buf), "%.3f", ts->beats_per_minute());
101                         }
102
103                         max_tempo = max (max_tempo, ts->beats_per_minute());
104                         min_tempo = min (min_tempo, ts->beats_per_minute());
105
106                         tempo_curves.push_back (new TempoCurve (*this, *tempo_group, UIConfiguration::instance().color ("tempo curve"),
107                                                                 *(const_cast<TempoSection*>(ts)), ts->frame(), false));
108                         if (ts->position_lock_style() == MusicTime) {
109                                 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker music"), buf,
110                                                                  *(const_cast<TempoSection*>(ts))));
111                         } else {
112                                 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), buf,
113                                                                  *(const_cast<TempoSection*>(ts))));
114                         }
115
116                 }
117
118         }
119
120         const double min_tempo_range = 5.0;
121         const double tempo_delta = fabs (max_tempo - min_tempo);
122
123         if (tempo_delta < min_tempo_range) {
124                 max_tempo += min_tempo_range - tempo_delta;
125                 min_tempo += tempo_delta - min_tempo_range;
126         }
127
128         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
129                 Curves::iterator tmp = x;
130                 (*x)->set_max_tempo (max_tempo);
131                 (*x)->set_min_tempo (min_tempo);
132                 ++tmp;
133                 if (tmp != tempo_curves.end()) {
134                         (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
135                 } else {
136                         (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
137                 }
138                 ++x;
139         }
140
141         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
142                 TempoMarker* tempo_marker;
143
144                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
145                         tempo_marker->update_height_mark ((tempo_marker->tempo().beats_per_minute() - min_tempo) / max (10.0, max_tempo - min_tempo));
146                 }
147         }
148 }
149
150
151 void
152 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
153 {
154         if (!_session) {
155                 return;
156         }
157
158         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
159
160         if (tempo_lines) {
161                 tempo_lines->tempo_map_changed();
162         }
163
164         std::vector<TempoMap::BBTPoint> grid;
165         compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
166         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
167         draw_measures (grid);
168         update_tempo_based_rulers (grid);
169 }
170
171 struct CurveComparator {
172         bool operator() (TempoCurve const * a, TempoCurve const * b) {
173                 return a->position() < b->position();
174         }
175 };
176
177 void
178 Editor::marker_position_changed ()
179 {
180         if (!_session) {
181                 return;
182         }
183
184         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
185
186         if (tempo_lines) {
187                 tempo_lines->tempo_map_changed();
188         }
189
190         double max_tempo = 0.0;
191         double min_tempo = DBL_MAX;
192
193         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
194                 TempoMarker* tempo_marker;
195                 MeterMarker* meter_marker;
196                 const TempoSection *ts;
197                 const MeterSection *ms;
198
199                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
200                         if ((ts = &tempo_marker->tempo()) != 0) {
201                                 tempo_marker->set_position (ts->frame ());
202                                 char buf[64];
203                                 snprintf (buf, sizeof (buf), "%.3f", ts->beats_per_minute());
204                                 tempo_marker->set_name (buf);
205
206                                 max_tempo = max (max_tempo, ts->beats_per_minute());
207                                 min_tempo = min (min_tempo, ts->beats_per_minute());
208                         }
209                 }
210                 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
211                         if ((ms = &meter_marker->meter()) != 0) {
212                                 meter_marker->set_position (ms->frame ());
213                         }
214                 }
215         }
216
217         tempo_curves.sort (CurveComparator());
218
219         const double min_tempo_range = 5.0;
220         const double tempo_delta = fabs (max_tempo - min_tempo);
221
222         if (tempo_delta < min_tempo_range) {
223                 max_tempo += min_tempo_range - tempo_delta;
224                 min_tempo += tempo_delta - min_tempo_range;
225         }
226
227         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
228                 Curves::iterator tmp = x;
229                 (*x)->set_max_tempo (max_tempo);
230                 (*x)->set_min_tempo (min_tempo);
231                 ++tmp;
232                 if (tmp != tempo_curves.end()) {
233                         (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
234                 } else {
235                         (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
236                 }
237                 ++x;
238         }
239
240         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
241                 TempoMarker* tempo_marker;
242                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
243                         tempo_marker->update_height_mark ((tempo_marker->tempo().beats_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
244                 }
245         }
246
247         std::vector<TempoMap::BBTPoint> grid;
248         compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
249         draw_measures (grid);
250         update_tempo_based_rulers (grid);
251 }
252
253 void
254 Editor::redisplay_tempo (bool immediate_redraw)
255 {
256         if (!_session) {
257                 return;
258         }
259
260         if (immediate_redraw) {
261                 std::vector<TempoMap::BBTPoint> grid;
262
263                 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
264                 draw_measures (grid);
265                 update_tempo_based_rulers (grid); // redraw rulers and measure lines
266
267         } else {
268                 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
269         }
270 }
271
272 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
273 void
274 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
275 {
276         if (!_session) {
277                 return;
278         }
279
280         /* prevent negative values of leftmost from creeping into tempomap
281          */
282         const double lower_beat = floor (_session->tempo_map().beat_at_frame (leftmost)) - 1.0;
283         _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost);
284 }
285
286 void
287 Editor::hide_measures ()
288 {
289         if (tempo_lines) {
290                 tempo_lines->hide();
291         }
292 }
293
294 void
295 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
296 {
297         if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
298                 return;
299         }
300
301         if (tempo_lines == 0) {
302                 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
303         }
304
305         const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
306         tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
307 }
308
309 void
310 Editor::mouse_add_new_tempo_event (framepos_t frame)
311 {
312         if (_session == 0) {
313                 return;
314         }
315
316         TempoMap& map(_session->tempo_map());
317
318         begin_reversible_command (_("add tempo mark"));
319         const double pulse = map.pulse_at_frame (frame);
320
321         if (pulse > 0.0) {
322                 XMLNode &before = map.get_state();
323                 /* add music-locked ramped (?) tempo using the bpm/note type at frame*/
324                 map.add_tempo (map.tempo_at_frame (frame), pulse, 0, TempoSection::Ramp, MusicTime);
325
326                 XMLNode &after = map.get_state();
327                 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
328                 commit_reversible_command ();
329         }
330
331         //map.dump (cerr);
332 }
333
334 void
335 Editor::mouse_add_new_meter_event (framepos_t frame)
336 {
337         if (_session == 0) {
338                 return;
339         }
340
341
342         TempoMap& map(_session->tempo_map());
343         MeterDialog meter_dialog (map, frame, _("add"));
344
345         switch (meter_dialog.run ()) {
346         case RESPONSE_ACCEPT:
347                 break;
348         default:
349                 return;
350         }
351
352         double bpb = meter_dialog.get_bpb ();
353         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
354
355         double note_type = meter_dialog.get_note_type ();
356
357         Timecode::BBT_Time requested;
358         meter_dialog.get_bbt_time (requested);
359
360         const double beat = map.beat_at_bbt (requested);
361
362         begin_reversible_command (_("add meter mark"));
363         XMLNode &before = map.get_state();
364
365         if (meter_dialog.get_lock_style() == MusicTime) {
366                 map.add_meter (Meter (bpb, note_type), beat, requested,  map.frame_at_beat (beat), MusicTime);
367         } else {
368                 map.add_meter (Meter (bpb, note_type), beat, requested, map.frame_at_beat (beat), AudioTime);
369         }
370
371         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
372         commit_reversible_command ();
373
374         //map.dump (cerr);
375 }
376
377 void
378 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
379 {
380         ArdourMarker* marker;
381         TempoMarker* tempo_marker;
382
383         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
384                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
385                 abort(); /*NOTREACHED*/
386         }
387
388         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
389                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
390                 abort(); /*NOTREACHED*/
391         }
392
393         if (tempo_marker->tempo().movable()) {
394                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
395         }
396 }
397
398 void
399 Editor::edit_meter_section (MeterSection* section)
400 {
401         MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
402
403         switch (meter_dialog.run()) {
404         case RESPONSE_ACCEPT:
405                 break;
406         default:
407                 return;
408         }
409
410         double bpb = meter_dialog.get_bpb ();
411         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
412
413         double const note_type = meter_dialog.get_note_type ();
414         const Meter meter (bpb, note_type);
415
416         Timecode::BBT_Time when;
417         meter_dialog.get_bbt_time (when);
418
419         framepos_t const frame = _session->tempo_map().frame_at_bbt (when);
420         const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
421
422         begin_reversible_command (_("replace meter mark"));
423         XMLNode &before = _session->tempo_map().get_state();
424
425         _session->tempo_map().replace_meter (*section, meter, when, frame, pls);
426
427         XMLNode &after = _session->tempo_map().get_state();
428         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
429         commit_reversible_command ();
430 }
431
432 void
433 Editor::edit_tempo_section (TempoSection* section)
434 {
435         TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
436
437         switch (tempo_dialog.run ()) {
438         case RESPONSE_ACCEPT:
439                 break;
440         default:
441                 return;
442         }
443
444         double bpm = tempo_dialog.get_bpm ();
445         double nt = tempo_dialog.get_note_type ();
446         bpm = max (0.01, bpm);
447         const Tempo tempo (bpm, nt);
448
449         Timecode::BBT_Time when;
450         tempo_dialog.get_bbt_time (when);
451
452         const TempoSection::Type ttype (tempo_dialog.get_tempo_type());
453
454         begin_reversible_command (_("replace tempo mark"));
455         XMLNode &before = _session->tempo_map().get_state();
456
457         if (tempo_dialog.get_lock_style() == AudioTime) {
458                 framepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
459                 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, ttype, AudioTime);
460         } else {
461                 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
462                 _session->tempo_map().replace_tempo (*section, tempo, p, 0, ttype, MusicTime);
463         }
464
465         XMLNode &after = _session->tempo_map().get_state();
466         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
467         commit_reversible_command ();
468 }
469
470 void
471 Editor::edit_tempo_marker (TempoMarker& tm)
472 {
473         edit_tempo_section (&tm.tempo());
474 }
475
476 void
477 Editor::edit_meter_marker (MeterMarker& mm)
478 {
479         edit_meter_section (&mm.meter());
480 }
481
482 gint
483 Editor::real_remove_tempo_marker (TempoSection *section)
484 {
485         begin_reversible_command (_("remove tempo mark"));
486         XMLNode &before = _session->tempo_map().get_state();
487         _session->tempo_map().remove_tempo (*section, true);
488         XMLNode &after = _session->tempo_map().get_state();
489         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
490         commit_reversible_command ();
491
492         return FALSE;
493 }
494
495 void
496 Editor::remove_meter_marker (ArdourCanvas::Item* item)
497 {
498         ArdourMarker* marker;
499         MeterMarker* meter_marker;
500
501         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
502                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
503                 abort(); /*NOTREACHED*/
504         }
505
506         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
507                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
508                 abort(); /*NOTREACHED*/
509         }
510
511         if (meter_marker->meter().movable()) {
512           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
513         }
514 }
515
516 gint
517 Editor::real_remove_meter_marker (MeterSection *section)
518 {
519         begin_reversible_command (_("remove tempo mark"));
520         XMLNode &before = _session->tempo_map().get_state();
521         _session->tempo_map().remove_meter (*section, true);
522         XMLNode &after = _session->tempo_map().get_state();
523         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
524         commit_reversible_command ();
525
526         return FALSE;
527 }