Fix crash when X11 is not available for VST UIs
[ardour.git] / gtk2_ardour / editor_tempodisplay.cc
1 /*
2  * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2006-2015 David Robillard <d@drobilla.net>
5  * Copyright (C) 2006-2015 Tim Mayberry <mojofunk@gmail.com>
6  * Copyright (C) 2007 Doug McLain <doug@nostar.net>
7  * Copyright (C) 2008-2011 Carl Hetherington <carl@carlh.net>
8  * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
9  * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
10  * Copyright (C) 2017-2018 Ben Loftis <ben@harrisonconsoles.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #ifdef WAF_BUILD
28 #include "gtk2ardour-config.h"
29 #endif
30
31 #include <cstdio> // for sprintf, grrr
32 #include <cstdlib>
33 #include <cmath>
34 #include <string>
35 #include <climits>
36
37 #include "pbd/error.h"
38 #include "pbd/memento_command.h"
39
40 #include <gtkmm2ext/utils.h>
41 #include <gtkmm2ext/gtk_ui.h>
42
43 #include "ardour/session.h"
44 #include "ardour/tempo.h"
45 #include <gtkmm2ext/doi.h>
46 #include <gtkmm2ext/utils.h>
47
48 #include "canvas/canvas.h"
49 #include "canvas/item.h"
50 #include "canvas/line_set.h"
51
52 #include "editor.h"
53 #include "marker.h"
54 #include "tempo_dialog.h"
55 #include "rgb_macros.h"
56 #include "gui_thread.h"
57 #include "time_axis_view.h"
58 #include "grid_lines.h"
59 #include "ui_config.h"
60
61 #include "pbd/i18n.h"
62
63 using namespace std;
64 using namespace ARDOUR;
65 using namespace PBD;
66 using namespace Gtk;
67 using namespace Gtkmm2ext;
68 using namespace Editing;
69
70 void
71 Editor::remove_metric_marks ()
72 {
73         /* don't delete these while handling events, just punt till the GUI is idle */
74
75         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
76                 delete_when_idle (*x);
77         }
78         metric_marks.clear ();
79
80         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
81                 delete (*x);
82         }
83         tempo_curves.clear ();
84 }
85 struct CurveComparator {
86         bool operator() (TempoCurve const * a, TempoCurve const * b) {
87                 return a->tempo().sample() < b->tempo().sample();
88         }
89 };
90 void
91 Editor::draw_metric_marks (const Metrics& metrics)
92 {
93         char buf[64];
94         TempoSection* prev_ts = 0;
95         double max_tempo = 0.0;
96         double min_tempo = DBL_MAX;
97
98         remove_metric_marks (); // also clears tempo curves
99
100         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
101                 const MeterSection *ms;
102                 TempoSection *ts;
103
104                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
105                         snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
106                         if (ms->position_lock_style() == MusicTime) {
107                                 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker music"), buf,
108                                                                          *(const_cast<MeterSection*>(ms))));
109                         } else {
110                                 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
111                                                                          *(const_cast<MeterSection*>(ms))));
112                         }
113                 } else if ((ts = dynamic_cast<TempoSection*>(*i)) != 0) {
114
115                         max_tempo = max (max_tempo, ts->note_types_per_minute());
116                         max_tempo = max (max_tempo, ts->end_note_types_per_minute());
117                         min_tempo = min (min_tempo, ts->note_types_per_minute());
118                         min_tempo = min (min_tempo, ts->end_note_types_per_minute());
119                         uint32_t const tc_color = UIConfiguration::instance().color ("tempo curve");
120
121                         tempo_curves.push_back (new TempoCurve (*this, *tempo_group, tc_color,
122                                                                 *(const_cast<TempoSection*>(ts)), ts->sample(), false));
123
124                         const std::string tname (X_(""));
125                         if (ts->position_lock_style() == MusicTime) {
126                                 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker music"), tname,
127                                                                  *(const_cast<TempoSection*>(ts))));
128                         } else {
129                                 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), tname,
130                                                                  *(const_cast<TempoSection*>(ts))));
131                         }
132                         if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
133                                 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
134                         } else {
135                                 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker"));
136                         }
137                         prev_ts = ts;
138                 }
139
140         }
141         tempo_curves.sort (CurveComparator());
142
143         const double min_tempo_range = 5.0;
144         const double tempo_delta = fabs (max_tempo - min_tempo);
145
146         if (tempo_delta < min_tempo_range) {
147                 max_tempo += min_tempo_range - tempo_delta;
148                 min_tempo += tempo_delta - min_tempo_range;
149         }
150
151         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
152                 Curves::iterator tmp = x;
153                 (*x)->set_max_tempo (max_tempo);
154                 (*x)->set_min_tempo (min_tempo);
155                 ++tmp;
156                 if (tmp != tempo_curves.end()) {
157                         (*x)->set_position ((*x)->tempo().sample(), (*tmp)->tempo().sample());
158                 } else {
159                         (*x)->set_position ((*x)->tempo().sample(), UINT32_MAX);
160                 }
161
162                 if (!(*x)->tempo().active()) {
163                         (*x)->hide();
164                 } else {
165                         (*x)->show();
166                 }
167
168                 ++x;
169         }
170
171         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
172                 TempoMarker* tempo_marker;
173
174                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
175                         tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (10.0, max_tempo - min_tempo));
176                 }
177         }
178 }
179
180
181 void
182 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
183 {
184         if (!_session) {
185                 return;
186         }
187
188         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
189
190         compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
191
192         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
193         update_tempo_based_rulers ();
194
195         maybe_draw_grid_lines ();
196 }
197
198 void
199 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
200 {
201         if (!_session) {
202                 return;
203         }
204
205         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
206
207         TempoSection* prev_ts = 0;
208         double max_tempo = 0.0;
209         double min_tempo = DBL_MAX;
210
211         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
212                 TempoMarker* tempo_marker;
213                 MeterMarker* meter_marker;
214                 TempoSection *ts;
215                 const MeterSection *ms;
216
217                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
218                         if ((ts = &tempo_marker->tempo()) != 0) {
219
220                                 tempo_marker->set_position (ts->sample ());
221
222                                 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
223                                         tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
224                                 } else {
225                                         tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker"));
226                                 }
227
228                                 max_tempo = max (max_tempo, ts->note_types_per_minute());
229                                 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
230                                 min_tempo = min (min_tempo, ts->note_types_per_minute());
231                                 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
232
233                                 prev_ts = ts;
234                         }
235                 }
236                 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
237                         if ((ms = &meter_marker->meter()) != 0) {
238                                 meter_marker->set_position (ms->sample ());
239                         }
240                 }
241         }
242
243         tempo_curves.sort (CurveComparator());
244
245         const double min_tempo_range = 5.0;
246         const double tempo_delta = fabs (max_tempo - min_tempo);
247
248         if (tempo_delta < min_tempo_range) {
249                 max_tempo += min_tempo_range - tempo_delta;
250                 min_tempo += tempo_delta - min_tempo_range;
251         }
252
253         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
254                 Curves::iterator tmp = x;
255                 (*x)->set_max_tempo (max_tempo);
256                 (*x)->set_min_tempo (min_tempo);
257                 ++tmp;
258                 if (tmp != tempo_curves.end()) {
259                         (*x)->set_position ((*x)->tempo().sample(), (*tmp)->tempo().sample());
260                 } else {
261                         (*x)->set_position ((*x)->tempo().sample(), UINT32_MAX);
262                 }
263
264                 if (!(*x)->tempo().active()) {
265                         (*x)->hide();
266                 } else {
267                         (*x)->show();
268                 }
269
270                 ++x;
271         }
272
273         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
274                 TempoMarker* tempo_marker;
275                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
276                         tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
277                 }
278         }
279
280         compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
281
282         update_tempo_based_rulers ();
283
284         maybe_draw_grid_lines ();
285 }
286
287 void
288 Editor::redisplay_grid (bool immediate_redraw)
289 {
290         if (!_session) {
291                 return;
292         }
293
294         if (immediate_redraw) {
295
296                 update_tempo_based_rulers ();
297
298                 update_grid();
299                 
300         } else {
301                 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_grid), true), false));
302         }
303 }
304 void
305 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
306 {
307         if (ts == 0) {
308                 return;
309         }
310
311         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
312                 if (&(*x)->tempo() == ts) {
313                         if (yn) {
314                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
315                         } else {
316                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
317                         }
318                         break;
319                 }
320         }
321 }
322
323 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
324 void
325 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, samplepos_t leftmost, samplepos_t rightmost)
326 {
327         if (!_session) {
328                 return;
329         }
330
331         /* prevent negative values of leftmost from creeping into tempomap
332          */
333         const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_sample (leftmost))) - 1.0;
334         switch (bbt_ruler_scale) {
335
336         case bbt_show_quarters:
337         case bbt_show_eighths:
338         case bbt_show_sixteenths:
339         case bbt_show_thirtyseconds:
340                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost);
341                 break;
342
343         case bbt_show_1:
344                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 1);
345                 break;
346
347         case bbt_show_4:
348                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 4);
349                 break;
350
351         case bbt_show_16:
352                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 16);
353                 break;
354
355         case bbt_show_64:
356                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 64);
357                 break;
358
359         default:
360                 /* bbt_show_many */
361                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 128);
362                 break;
363         }
364 }
365
366 void
367 Editor::hide_grid_lines ()
368 {
369         if (grid_lines) {
370                 grid_lines->hide();
371         }
372 }
373
374 void
375 Editor::maybe_draw_grid_lines ()
376 {
377         if ( _session == 0 ) {
378                 return;
379         }
380
381         if (grid_lines == 0) {
382                 grid_lines = new GridLines (time_line_group, ArdourCanvas::LineSet::Vertical);
383         }
384
385         grid_marks.clear();
386         samplepos_t rightmost_sample = _leftmost_sample + current_page_samples();
387
388         if ( grid_musical() ) {
389                  metric_get_bbt (grid_marks, _leftmost_sample, rightmost_sample, 12);
390         } else if (_grid_type== GridTypeTimecode) {
391                  metric_get_timecode (grid_marks, _leftmost_sample, rightmost_sample, 12);
392         } else if (_grid_type == GridTypeCDFrame) {
393                 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
394         } else if (_grid_type == GridTypeMinSec) {
395                 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
396         }
397
398         grid_lines->draw ( grid_marks );
399         grid_lines->show();
400 }
401
402 void
403 Editor::mouse_add_new_tempo_event (samplepos_t sample)
404 {
405         if (_session == 0) {
406                 return;
407         }
408
409         TempoMap& map(_session->tempo_map());
410
411         begin_reversible_command (_("add tempo mark"));
412         const double pulse = map.exact_qn_at_sample (sample, get_grid_music_divisions (0)) / 4.0;
413
414         if (pulse > 0.0) {
415                 XMLNode &before = map.get_state();
416                 /* add music-locked ramped (?) tempo using the bpm/note type at sample*/
417                 map.add_tempo (map.tempo_at_sample (sample), pulse, 0, MusicTime);
418
419                 XMLNode &after = map.get_state();
420                 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
421                 commit_reversible_command ();
422         }
423
424         //map.dump (cerr);
425 }
426
427 void
428 Editor::mouse_add_new_meter_event (samplepos_t sample)
429 {
430         if (_session == 0) {
431                 return;
432         }
433
434
435         TempoMap& map(_session->tempo_map());
436         MeterDialog meter_dialog (map, sample, _("add"));
437
438         switch (meter_dialog.run ()) {
439         case RESPONSE_ACCEPT:
440                 break;
441         default:
442                 return;
443         }
444
445         double bpb = meter_dialog.get_bpb ();
446         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
447
448         double note_type = meter_dialog.get_note_type ();
449
450         Timecode::BBT_Time requested;
451         meter_dialog.get_bbt_time (requested);
452
453         const double al_sample = map.sample_at_bbt (requested);
454         begin_reversible_command (_("add meter mark"));
455         XMLNode &before = map.get_state();
456
457         if (meter_dialog.get_lock_style() == MusicTime) {
458                 map.add_meter (Meter (bpb, note_type), requested, 0, MusicTime);
459         } else {
460                 map.add_meter (Meter (bpb, note_type), requested, al_sample, AudioTime);
461         }
462
463         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
464         commit_reversible_command ();
465
466         //map.dump (cerr);
467 }
468
469 void
470 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
471 {
472         ArdourMarker* marker;
473         TempoMarker* tempo_marker;
474
475         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
476                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
477                 abort(); /*NOTREACHED*/
478         }
479
480         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
481                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
482                 abort(); /*NOTREACHED*/
483         }
484
485         if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
486                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
487         }
488 }
489
490 void
491 Editor::edit_meter_section (MeterSection* section)
492 {
493         MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
494
495         switch (meter_dialog.run()) {
496         case RESPONSE_ACCEPT:
497                 break;
498         default:
499                 return;
500         }
501
502         double bpb = meter_dialog.get_bpb ();
503         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
504
505         double const note_type = meter_dialog.get_note_type ();
506         const Meter meter (bpb, note_type);
507
508         Timecode::BBT_Time when;
509         meter_dialog.get_bbt_time (when);
510         const samplepos_t sample = _session->tempo_map().sample_at_bbt (when);
511         const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
512
513         begin_reversible_command (_("replace meter mark"));
514         XMLNode &before = _session->tempo_map().get_state();
515
516         _session->tempo_map().replace_meter (*section, meter, when, sample, pls);
517
518         XMLNode &after = _session->tempo_map().get_state();
519         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
520         commit_reversible_command ();
521 }
522
523 void
524 Editor::edit_tempo_section (TempoSection* section)
525 {
526         TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
527
528         switch (tempo_dialog.run ()) {
529         case RESPONSE_ACCEPT:
530                 break;
531         default:
532                 return;
533         }
534
535         double bpm = tempo_dialog.get_bpm ();
536         double end_bpm = tempo_dialog.get_end_bpm ();
537         double nt = tempo_dialog.get_note_type ();
538         bpm = max (0.01, bpm);
539         const Tempo tempo (bpm, nt, end_bpm);
540
541         Timecode::BBT_Time when;
542         tempo_dialog.get_bbt_time (when);
543
544         begin_reversible_command (_("replace tempo mark"));
545         XMLNode &before = _session->tempo_map().get_state();
546
547         if (tempo_dialog.get_lock_style() == AudioTime) {
548                 samplepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
549                 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
550         } else {
551                 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
552                 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
553         }
554
555         XMLNode &after = _session->tempo_map().get_state();
556         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
557         commit_reversible_command ();
558 }
559
560 void
561 Editor::edit_tempo_marker (TempoMarker& tm)
562 {
563         edit_tempo_section (&tm.tempo());
564 }
565
566 void
567 Editor::edit_meter_marker (MeterMarker& mm)
568 {
569         edit_meter_section (&mm.meter());
570 }
571
572 gint
573 Editor::real_remove_tempo_marker (TempoSection *section)
574 {
575         begin_reversible_command (_("remove tempo mark"));
576         XMLNode &before = _session->tempo_map().get_state();
577         _session->tempo_map().remove_tempo (*section, true);
578         XMLNode &after = _session->tempo_map().get_state();
579         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
580         commit_reversible_command ();
581
582         return FALSE;
583 }
584
585 void
586 Editor::remove_meter_marker (ArdourCanvas::Item* item)
587 {
588         ArdourMarker* marker;
589         MeterMarker* meter_marker;
590
591         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
592                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
593                 abort(); /*NOTREACHED*/
594         }
595
596         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
597                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
598                 abort(); /*NOTREACHED*/
599         }
600
601         if (!meter_marker->meter().initial()) {
602           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
603         }
604 }
605
606 gint
607 Editor::real_remove_meter_marker (MeterSection *section)
608 {
609         begin_reversible_command (_("remove tempo mark"));
610         XMLNode &before = _session->tempo_map().get_state();
611         _session->tempo_map().remove_meter (*section, true);
612         XMLNode &after = _session->tempo_map().get_state();
613         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
614         commit_reversible_command ();
615
616         return FALSE;
617 }