a huge set of changes to tempo+meter handling. testing feedback requested. the_CLA...
[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 <libgnomecanvasmm.h>
31
32 #include "pbd/error.h"
33 #include "pbd/memento_command.h"
34
35 #include <gtkmm2ext/utils.h>
36 #include <gtkmm2ext/gtk_ui.h>
37
38 #include "ardour/session.h"
39 #include "ardour/tempo.h"
40 #include <gtkmm2ext/doi.h>
41 #include <gtkmm2ext/utils.h>
42
43 #include "editor.h"
44 #include "marker.h"
45 #include "simpleline.h"
46 #include "tempo_dialog.h"
47 #include "rgb_macros.h"
48 #include "gui_thread.h"
49 #include "time_axis_view.h"
50 #include "ardour_ui.h"
51 #include "tempo_lines.h"
52 #include "utils.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, ARDOUR_UI::config()->canvasvar_MeterMarker.get(), buf,
89                                                                  *(const_cast<MeterSection*>(ms))));
90                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
91                         if (Config->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, ARDOUR_UI::config()->canvasvar_TempoMarker.get(), 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         compute_current_bbt_points(leftmost_frame, leftmost_frame + current_page_frames());
118         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
119         redraw_measures ();
120         update_tempo_based_rulers ();
121 }
122
123 void
124 Editor::redisplay_tempo (bool immediate_redraw)
125 {
126         if (!_session) {
127                 return;
128         }
129
130         compute_current_bbt_points (leftmost_frame, leftmost_frame + current_page_frames()); // redraw rulers and measures
131
132         if (immediate_redraw) {
133                 redraw_measures ();
134         } else {
135 #ifdef GTKOSX
136                 redraw_measures ();
137 #else
138                 Glib::signal_idle().connect (sigc::mem_fun (*this, &Editor::redraw_measures));
139 #endif
140         }
141         update_tempo_based_rulers (); // redraw rulers and measures
142 }
143
144 void
145 Editor::compute_current_bbt_points (framepos_t leftmost, framepos_t rightmost)
146 {
147         if (!_session) {
148                 return;
149         }
150
151         Timecode::BBT_Time previous_beat, next_beat; // the beats previous to the leftmost frame and after the rightmost frame
152
153         _session->bbt_time(leftmost, previous_beat);
154         _session->bbt_time(rightmost, next_beat);
155
156         if (previous_beat.beats > 1) {
157                 previous_beat.beats -= 1;
158         } else if (previous_beat.bars > 1) {
159                 previous_beat.bars--;
160                 previous_beat.beats += 1;
161         }
162         previous_beat.ticks = 0;
163
164         if (_session->tempo_map().meter_at(rightmost).divisions_per_bar () > next_beat.beats + 1) {
165                 next_beat.beats += 1;
166         } else {
167                 next_beat.bars += 1;
168                 next_beat.beats = 1;
169         }
170         next_beat.ticks = 0;
171
172         delete current_bbt_points;
173         current_bbt_points = 0;
174
175         current_bbt_points = _session->tempo_map().get_points (_session->tempo_map().frame_time (previous_beat), _session->tempo_map().frame_time (next_beat) + 1);
176 }
177
178 void
179 Editor::hide_measures ()
180 {
181         if (tempo_lines)
182                 tempo_lines->hide();
183 }
184
185 bool
186 Editor::redraw_measures ()
187 {
188         draw_measures ();
189         return false;
190 }
191
192 void
193 Editor::draw_measures ()
194 {
195         if (_session == 0 || _show_measures == false ||
196             !current_bbt_points || current_bbt_points->empty()) {
197                 return;
198         }
199
200         if (tempo_lines == 0) {
201                 tempo_lines = new TempoLines(*track_canvas, time_line_group, physical_screen_height(get_window()));
202         }
203
204         tempo_lines->draw(*current_bbt_points, frames_per_unit);
205 }
206
207 void
208 Editor::mouse_add_new_tempo_event (framepos_t frame)
209 {
210         if (_session == 0) {
211                 return;
212         }
213
214         TempoMap& map(_session->tempo_map());
215         TempoDialog tempo_dialog (map, frame, _("add"));
216
217         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
218         //this causes compiz to display no border.
219         //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
220
221         ensure_float (tempo_dialog);
222
223         switch (tempo_dialog.run()) {
224         case RESPONSE_ACCEPT:
225                 break;
226         default:
227                 return;
228         }
229
230         double bpm = 0;
231         Timecode::BBT_Time requested;
232
233         bpm = tempo_dialog.get_bpm ();
234         double nt = tempo_dialog.get_note_type();
235         bpm = max (0.01, bpm);
236
237         tempo_dialog.get_bbt_time (requested);
238
239         begin_reversible_command (_("add tempo mark"));
240         XMLNode &before = map.get_state();
241         map.add_tempo (Tempo (bpm,nt), requested);
242         XMLNode &after = map.get_state();
243         _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
244         commit_reversible_command ();
245
246         //map.dump (cerr);
247 }
248
249 void
250 Editor::mouse_add_new_meter_event (framepos_t frame)
251 {
252         if (_session == 0) {
253                 return;
254         }
255
256
257         TempoMap& map(_session->tempo_map());
258         MeterDialog meter_dialog (map, frame, _("add"));
259
260         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
261
262         //this causes compiz to display no border..
263         //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
264
265         ensure_float (meter_dialog);
266
267         switch (meter_dialog.run ()) {
268         case RESPONSE_ACCEPT:
269                 break;
270         default:
271                 return;
272         }
273
274         double bpb = meter_dialog.get_bpb ();
275         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
276
277         double note_type = meter_dialog.get_note_type ();
278         Timecode::BBT_Time requested;
279
280         meter_dialog.get_bbt_time (requested);
281
282         begin_reversible_command (_("add meter mark"));
283         XMLNode &before = map.get_state();
284         map.add_meter (Meter (bpb, note_type), requested);
285         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
286         commit_reversible_command ();
287
288         //map.dump (cerr);
289 }
290
291 void
292 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
293 {
294         Marker* marker;
295         TempoMarker* tempo_marker;
296
297         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
298                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
299                 /*NOTREACHED*/
300         }
301
302         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
303                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
304                 /*NOTREACHED*/
305         }
306
307         if (tempo_marker->tempo().movable()) {
308                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
309         }
310 }
311
312 void
313 Editor::edit_meter_section (MeterSection* section)
314 {
315         MeterDialog meter_dialog (*section, _("done"));
316
317         meter_dialog.set_position (Gtk::WIN_POS_MOUSE);
318
319         ensure_float (meter_dialog);
320
321         switch (meter_dialog.run()) {
322         case RESPONSE_ACCEPT:
323                 break;
324         default:
325                 return;
326         }
327
328         double bpb = meter_dialog.get_bpb ();
329         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
330
331         double note_type = meter_dialog.get_note_type ();
332
333         Timecode::BBT_Time when;
334         meter_dialog.get_bbt_time(when);
335
336         begin_reversible_command (_("replace tempo mark"));
337         XMLNode &before = _session->tempo_map().get_state();
338         _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), when);
339         XMLNode &after = _session->tempo_map().get_state();
340         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
341         commit_reversible_command ();
342 }
343
344 void
345 Editor::edit_tempo_section (TempoSection* section)
346 {
347         TempoDialog tempo_dialog (*section, _("done"));
348
349         tempo_dialog.set_position (Gtk::WIN_POS_MOUSE);
350
351         ensure_float (tempo_dialog);
352
353         switch (tempo_dialog.run ()) {
354         case RESPONSE_ACCEPT:
355                 break;
356         default:
357                 return;
358         }
359
360         double bpm = tempo_dialog.get_bpm ();
361         double nt = tempo_dialog.get_note_type ();
362         Timecode::BBT_Time when;
363         tempo_dialog.get_bbt_time(when);
364         bpm = max (0.01, bpm);
365
366         begin_reversible_command (_("replace tempo mark"));
367         XMLNode &before = _session->tempo_map().get_state();
368         _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), when);
369         XMLNode &after = _session->tempo_map().get_state();
370         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
371         commit_reversible_command ();
372 }
373
374 void
375 Editor::edit_tempo_marker (ArdourCanvas::Item *item)
376 {
377         Marker* marker;
378         TempoMarker* tempo_marker;
379
380         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
381                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
382                 /*NOTREACHED*/
383         }
384
385         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
386                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
387                 /*NOTREACHED*/
388         }
389
390         edit_tempo_section (&tempo_marker->tempo());
391 }
392
393 void
394 Editor::edit_meter_marker (ArdourCanvas::Item *item)
395 {
396         Marker* marker;
397         MeterMarker* meter_marker;
398
399         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
400                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
401                 /*NOTREACHED*/
402         }
403
404         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
405                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
406                 /*NOTREACHED*/
407         }
408
409         edit_meter_section (&meter_marker->meter());
410 }
411
412 gint
413 Editor::real_remove_tempo_marker (TempoSection *section)
414 {
415         begin_reversible_command (_("remove tempo mark"));
416         XMLNode &before = _session->tempo_map().get_state();
417         _session->tempo_map().remove_tempo (*section, true);
418         XMLNode &after = _session->tempo_map().get_state();
419         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
420         commit_reversible_command ();
421
422         return FALSE;
423 }
424
425 void
426 Editor::remove_meter_marker (ArdourCanvas::Item* item)
427 {
428         Marker* marker;
429         MeterMarker* meter_marker;
430
431         if ((marker = reinterpret_cast<Marker *> (item->get_data ("marker"))) == 0) {
432                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
433                 /*NOTREACHED*/
434         }
435
436         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
437                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
438                 /*NOTREACHED*/
439         }
440
441         if (meter_marker->meter().movable()) {
442           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
443         }
444 }
445
446 gint
447 Editor::real_remove_meter_marker (MeterSection *section)
448 {
449         begin_reversible_command (_("remove tempo mark"));
450         XMLNode &before = _session->tempo_map().get_state();
451         _session->tempo_map().remove_meter (*section, true);
452         XMLNode &after = _session->tempo_map().get_state();
453         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
454         commit_reversible_command ();
455
456         return FALSE;
457 }