Fix saving Lua Callbacks when un/register succeeds
[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 "grid_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 struct CurveComparator {
79         bool operator() (TempoCurve const * a, TempoCurve const * b) {
80                 return a->tempo().sample() < b->tempo().sample();
81         }
82 };
83 void
84 Editor::draw_metric_marks (const Metrics& metrics)
85 {
86         char buf[64];
87         TempoSection* prev_ts = 0;
88         double max_tempo = 0.0;
89         double min_tempo = DBL_MAX;
90
91         remove_metric_marks (); // also clears tempo curves
92
93         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
94                 const MeterSection *ms;
95                 TempoSection *ts;
96
97                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
98                         snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
99                         if (ms->position_lock_style() == MusicTime) {
100                                 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker music"), buf,
101                                                                          *(const_cast<MeterSection*>(ms))));
102                         } else {
103                                 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
104                                                                          *(const_cast<MeterSection*>(ms))));
105                         }
106                 } else if ((ts = dynamic_cast<TempoSection*>(*i)) != 0) {
107
108                         max_tempo = max (max_tempo, ts->note_types_per_minute());
109                         max_tempo = max (max_tempo, ts->end_note_types_per_minute());
110                         min_tempo = min (min_tempo, ts->note_types_per_minute());
111                         min_tempo = min (min_tempo, ts->end_note_types_per_minute());
112                         uint32_t const tc_color = UIConfiguration::instance().color ("tempo curve");
113
114                         tempo_curves.push_back (new TempoCurve (*this, *tempo_group, tc_color,
115                                                                 *(const_cast<TempoSection*>(ts)), ts->sample(), false));
116
117                         const std::string tname (X_(""));
118                         if (ts->position_lock_style() == MusicTime) {
119                                 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker music"), tname,
120                                                                  *(const_cast<TempoSection*>(ts))));
121                         } else {
122                                 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), tname,
123                                                                  *(const_cast<TempoSection*>(ts))));
124                         }
125                         if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
126                                 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
127                         } else {
128                                 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker"));
129                         }
130                         prev_ts = ts;
131                 }
132
133         }
134         tempo_curves.sort (CurveComparator());
135
136         const double min_tempo_range = 5.0;
137         const double tempo_delta = fabs (max_tempo - min_tempo);
138
139         if (tempo_delta < min_tempo_range) {
140                 max_tempo += min_tempo_range - tempo_delta;
141                 min_tempo += tempo_delta - min_tempo_range;
142         }
143
144         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
145                 Curves::iterator tmp = x;
146                 (*x)->set_max_tempo (max_tempo);
147                 (*x)->set_min_tempo (min_tempo);
148                 ++tmp;
149                 if (tmp != tempo_curves.end()) {
150                         (*x)->set_position ((*x)->tempo().sample(), (*tmp)->tempo().sample());
151                 } else {
152                         (*x)->set_position ((*x)->tempo().sample(), UINT32_MAX);
153                 }
154
155                 if (!(*x)->tempo().active()) {
156                         (*x)->hide();
157                 } else {
158                         (*x)->show();
159                 }
160
161                 ++x;
162         }
163
164         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
165                 TempoMarker* tempo_marker;
166
167                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
168                         tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (10.0, max_tempo - min_tempo));
169                 }
170         }
171 }
172
173
174 void
175 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
176 {
177         if (!_session) {
178                 return;
179         }
180
181         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
182
183         compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
184
185         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
186         update_tempo_based_rulers ();
187
188         maybe_draw_grid_lines ();
189 }
190
191 void
192 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
193 {
194         if (!_session) {
195                 return;
196         }
197
198         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
199
200         TempoSection* prev_ts = 0;
201         double max_tempo = 0.0;
202         double min_tempo = DBL_MAX;
203
204         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
205                 TempoMarker* tempo_marker;
206                 MeterMarker* meter_marker;
207                 TempoSection *ts;
208                 const MeterSection *ms;
209
210                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
211                         if ((ts = &tempo_marker->tempo()) != 0) {
212
213                                 tempo_marker->set_position (ts->sample ());
214
215                                 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
216                                         tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
217                                 } else {
218                                         tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker"));
219                                 }
220
221                                 max_tempo = max (max_tempo, ts->note_types_per_minute());
222                                 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
223                                 min_tempo = min (min_tempo, ts->note_types_per_minute());
224                                 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
225
226                                 prev_ts = ts;
227                         }
228                 }
229                 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
230                         if ((ms = &meter_marker->meter()) != 0) {
231                                 meter_marker->set_position (ms->sample ());
232                         }
233                 }
234         }
235
236         tempo_curves.sort (CurveComparator());
237
238         const double min_tempo_range = 5.0;
239         const double tempo_delta = fabs (max_tempo - min_tempo);
240
241         if (tempo_delta < min_tempo_range) {
242                 max_tempo += min_tempo_range - tempo_delta;
243                 min_tempo += tempo_delta - min_tempo_range;
244         }
245
246         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
247                 Curves::iterator tmp = x;
248                 (*x)->set_max_tempo (max_tempo);
249                 (*x)->set_min_tempo (min_tempo);
250                 ++tmp;
251                 if (tmp != tempo_curves.end()) {
252                         (*x)->set_position ((*x)->tempo().sample(), (*tmp)->tempo().sample());
253                 } else {
254                         (*x)->set_position ((*x)->tempo().sample(), UINT32_MAX);
255                 }
256
257                 if (!(*x)->tempo().active()) {
258                         (*x)->hide();
259                 } else {
260                         (*x)->show();
261                 }
262
263                 ++x;
264         }
265
266         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
267                 TempoMarker* tempo_marker;
268                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
269                         tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
270                 }
271         }
272
273         compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
274
275         update_tempo_based_rulers ();
276
277         maybe_draw_grid_lines ();
278 }
279
280 void
281 Editor::redisplay_grid (bool immediate_redraw)
282 {
283         if (!_session) {
284                 return;
285         }
286
287         if (immediate_redraw) {
288
289                 update_tempo_based_rulers ();
290
291                 update_grid();
292                 
293         } else {
294                 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_grid), true), false));
295         }
296 }
297 void
298 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
299 {
300         if (ts == 0) {
301                 return;
302         }
303
304         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
305                 if (&(*x)->tempo() == ts) {
306                         if (yn) {
307                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
308                         } else {
309                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
310                         }
311                         break;
312                 }
313         }
314 }
315
316 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
317 void
318 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, samplepos_t leftmost, samplepos_t rightmost)
319 {
320         if (!_session) {
321                 return;
322         }
323
324         /* prevent negative values of leftmost from creeping into tempomap
325          */
326         const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_sample (leftmost))) - 1.0;
327         switch (bbt_ruler_scale) {
328
329         case bbt_show_quarters:
330         case bbt_show_eighths:
331         case bbt_show_sixteenths:
332         case bbt_show_thirtyseconds:
333                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost);
334                 break;
335
336         case bbt_show_1:
337                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 1);
338                 break;
339
340         case bbt_show_4:
341                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 4);
342                 break;
343
344         case bbt_show_16:
345                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 16);
346                 break;
347
348         case bbt_show_64:
349                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 64);
350                 break;
351
352         default:
353                 /* bbt_show_many */
354                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 128);
355                 break;
356         }
357 }
358
359 void
360 Editor::hide_grid_lines ()
361 {
362         if (grid_lines) {
363                 grid_lines->hide();
364         }
365 }
366
367 void
368 Editor::maybe_draw_grid_lines ()
369 {
370         if ( _session == 0 ) {
371                 return;
372         }
373
374         if (grid_lines == 0) {
375                 grid_lines = new GridLines (time_line_group, ArdourCanvas::LineSet::Vertical);
376         }
377
378         grid_marks.clear();
379         samplepos_t rightmost_sample = _leftmost_sample + current_page_samples();
380
381         if ( grid_musical() ) {
382                  metric_get_bbt (grid_marks, _leftmost_sample, rightmost_sample, 12);
383         } else if (_grid_type== GridTypeTimecode) {
384                  metric_get_timecode (grid_marks, _leftmost_sample, rightmost_sample, 12);
385         } else if (_grid_type == GridTypeCDFrame) {
386                 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
387         } else if (_grid_type == GridTypeMinSec) {
388                 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
389         }
390
391         grid_lines->draw ( grid_marks );
392         grid_lines->show();
393 }
394
395 void
396 Editor::mouse_add_new_tempo_event (samplepos_t sample)
397 {
398         if (_session == 0) {
399                 return;
400         }
401
402         TempoMap& map(_session->tempo_map());
403
404         begin_reversible_command (_("add tempo mark"));
405         const double pulse = map.exact_qn_at_sample (sample, get_grid_music_divisions (0)) / 4.0;
406
407         if (pulse > 0.0) {
408                 XMLNode &before = map.get_state();
409                 /* add music-locked ramped (?) tempo using the bpm/note type at sample*/
410                 map.add_tempo (map.tempo_at_sample (sample), pulse, 0, MusicTime);
411
412                 XMLNode &after = map.get_state();
413                 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
414                 commit_reversible_command ();
415         }
416
417         //map.dump (cerr);
418 }
419
420 void
421 Editor::mouse_add_new_meter_event (samplepos_t sample)
422 {
423         if (_session == 0) {
424                 return;
425         }
426
427
428         TempoMap& map(_session->tempo_map());
429         MeterDialog meter_dialog (map, sample, _("add"));
430
431         switch (meter_dialog.run ()) {
432         case RESPONSE_ACCEPT:
433                 break;
434         default:
435                 return;
436         }
437
438         double bpb = meter_dialog.get_bpb ();
439         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
440
441         double note_type = meter_dialog.get_note_type ();
442
443         Timecode::BBT_Time requested;
444         meter_dialog.get_bbt_time (requested);
445
446         const double al_sample = map.sample_at_bbt (requested);
447         begin_reversible_command (_("add meter mark"));
448         XMLNode &before = map.get_state();
449
450         if (meter_dialog.get_lock_style() == MusicTime) {
451                 map.add_meter (Meter (bpb, note_type), requested, 0, MusicTime);
452         } else {
453                 map.add_meter (Meter (bpb, note_type), requested, al_sample, AudioTime);
454         }
455
456         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
457         commit_reversible_command ();
458
459         //map.dump (cerr);
460 }
461
462 void
463 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
464 {
465         ArdourMarker* marker;
466         TempoMarker* tempo_marker;
467
468         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
469                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
470                 abort(); /*NOTREACHED*/
471         }
472
473         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
474                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
475                 abort(); /*NOTREACHED*/
476         }
477
478         if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
479                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
480         }
481 }
482
483 void
484 Editor::edit_meter_section (MeterSection* section)
485 {
486         MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
487
488         switch (meter_dialog.run()) {
489         case RESPONSE_ACCEPT:
490                 break;
491         default:
492                 return;
493         }
494
495         double bpb = meter_dialog.get_bpb ();
496         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
497
498         double const note_type = meter_dialog.get_note_type ();
499         const Meter meter (bpb, note_type);
500
501         Timecode::BBT_Time when;
502         meter_dialog.get_bbt_time (when);
503         const samplepos_t sample = _session->tempo_map().sample_at_bbt (when);
504         const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
505
506         begin_reversible_command (_("replace meter mark"));
507         XMLNode &before = _session->tempo_map().get_state();
508
509         _session->tempo_map().replace_meter (*section, meter, when, sample, pls);
510
511         XMLNode &after = _session->tempo_map().get_state();
512         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
513         commit_reversible_command ();
514 }
515
516 void
517 Editor::edit_tempo_section (TempoSection* section)
518 {
519         TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
520
521         switch (tempo_dialog.run ()) {
522         case RESPONSE_ACCEPT:
523                 break;
524         default:
525                 return;
526         }
527
528         double bpm = tempo_dialog.get_bpm ();
529         double end_bpm = tempo_dialog.get_end_bpm ();
530         double nt = tempo_dialog.get_note_type ();
531         bpm = max (0.01, bpm);
532         const Tempo tempo (bpm, nt, end_bpm);
533
534         Timecode::BBT_Time when;
535         tempo_dialog.get_bbt_time (when);
536
537         begin_reversible_command (_("replace tempo mark"));
538         XMLNode &before = _session->tempo_map().get_state();
539
540         if (tempo_dialog.get_lock_style() == AudioTime) {
541                 samplepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
542                 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
543         } else {
544                 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
545                 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
546         }
547
548         XMLNode &after = _session->tempo_map().get_state();
549         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
550         commit_reversible_command ();
551 }
552
553 void
554 Editor::edit_tempo_marker (TempoMarker& tm)
555 {
556         edit_tempo_section (&tm.tempo());
557 }
558
559 void
560 Editor::edit_meter_marker (MeterMarker& mm)
561 {
562         edit_meter_section (&mm.meter());
563 }
564
565 gint
566 Editor::real_remove_tempo_marker (TempoSection *section)
567 {
568         begin_reversible_command (_("remove tempo mark"));
569         XMLNode &before = _session->tempo_map().get_state();
570         _session->tempo_map().remove_tempo (*section, true);
571         XMLNode &after = _session->tempo_map().get_state();
572         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
573         commit_reversible_command ();
574
575         return FALSE;
576 }
577
578 void
579 Editor::remove_meter_marker (ArdourCanvas::Item* item)
580 {
581         ArdourMarker* marker;
582         MeterMarker* meter_marker;
583
584         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
585                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
586                 abort(); /*NOTREACHED*/
587         }
588
589         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
590                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
591                 abort(); /*NOTREACHED*/
592         }
593
594         if (!meter_marker->meter().initial()) {
595           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
596         }
597 }
598
599 gint
600 Editor::real_remove_meter_marker (MeterSection *section)
601 {
602         begin_reversible_command (_("remove tempo mark"));
603         XMLNode &before = _session->tempo_map().get_state();
604         _session->tempo_map().remove_meter (*section, true);
605         XMLNode &after = _session->tempo_map().get_state();
606         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
607         commit_reversible_command ();
608
609         return FALSE;
610 }